jdk/src/share/classes/sun/security/ssl/DHCrypt.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.KeyAgreement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.interfaces.DHPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class implements the Diffie-Hellman key exchange algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * D-H means combining your private key with your partners public key to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * generate a number. The peer does the same with its private key and our
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * public key. Through the magic of Diffie-Hellman we both come up with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * same number. This number is secret (discounting MITM attacks) and hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * called the shared secret. It has the same length as the modulus, e.g. 512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * or 1024 bit. Man-in-the-middle attacks are typically countered by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * independent authentication step using certificates (RSA, DSA, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The thing to note is that the shared secret is constant for two partners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * with constant private keys. This is often not what we want, which is why
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * it is generally a good idea to create a new private key for each session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Generating a private key involves one modular exponentiation assuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * suitable D-H parameters are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * General usage of this class (TLS DHE case):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . if we are server, call DHCrypt(keyLength,random). This generates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    an ephemeral keypair of the request length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  . if we are client, call DHCrypt(modulus, base, random). This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    generates an ephemeral keypair using the parameters specified by the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *  . send parameters and public value to remote peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  . receive peers ephemeral public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  . call getAgreedSecret() to calculate the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * In TLS the server chooses the parameter values itself, the client must use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * those sent to it by the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The use of ephemeral keys as described above also achieves what is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * "forward secrecy". This means that even if the authentication keys are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * broken at a later date, the shared secret remains secure. The session is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * compromised only if the authentication keys are already broken at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * time the key exchange takes place and an active MITM attack is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * This is in contrast to straightforward encrypting RSA key exchanges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
final class DHCrypt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // group parameters (prime modulus and generator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private BigInteger modulus;                 // P (aka N)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private BigInteger base;                    // G (aka alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // our private key (including private component x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // public component of our key, X = (g ^ x) mod p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private BigInteger publicValue;             // X (aka y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Generate a Diffie-Hellman keypair of the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    DHCrypt(int keyLength, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            kpg.initialize(keyLength, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            publicValue = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            modulus = spec.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            base = spec.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Generate a Diffie-Hellman keypair using the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param modulus the Diffie-Hellman modulus P
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param base the Diffie-Hellman base G
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    DHCrypt(BigInteger modulus, BigInteger base, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.modulus = modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.base = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            DHParameterSpec params = new DHParameterSpec(modulus, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            kpg.initialize(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            publicValue = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (key instanceof DHPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            DHPublicKey dhKey = (DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return new DHPublicKeySpec(dhKey.getY(), params.getP(), params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            KeyFactory factory = JsseJce.getKeyFactory("DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return (DHPublicKeySpec)factory.getKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                            (key, DHPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /** Returns the Diffie-Hellman modulus. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /** Returns the Diffie-Hellman base (generator).  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    BigInteger getBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Gets the public key of this end of the key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    BigInteger getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return publicValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Get the secret data that has been agreed on through Diffie-Hellman
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * key agreement protocol.  Note that in the two party protocol, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the peer keys are already known, no other data needs to be sent in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * order to agree on a secret.  That is, a secured message may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * sent without any mandatory round-trip overheads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <P>It is illegal to call this member function if the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * has not been set (or generated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param peerPublicKey the peer's public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @returns the secret, which is an unsigned big-endian integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *  the same size as the Diffie-Hellman modulus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    SecretKey getAgreedSecret(BigInteger peerPublicValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            DHPublicKeySpec spec =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        new DHPublicKeySpec(peerPublicValue, modulus, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            PublicKey publicKey = kf.generatePublic(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ka.init(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            ka.doPhase(publicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return ka.generateSecret("TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new RuntimeException("Could not generate secret", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}