jdk/src/share/classes/sun/security/ssl/KerberosPreMasterSecret.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
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 2003-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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.EncryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.crypto.KeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This is the Kerberos premaster secret in the Kerberos client key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * exchange message (CLIENT --> SERVER); it holds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Kerberos-encrypted pre-master secret. The secret is encrypted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Kerberos session key.  The padding and size of the resulting message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * depends on the session key type, but the pre-master secret is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * always exactly 48 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class KerberosPreMasterSecret {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ProtocolVersion protocolVersion; // preMaster [0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte preMaster[];           // 48 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private byte encrypted[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructor used by client to generate premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Client randomly creates a pre-master secret and encrypts it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * using the Kerberos session key; only the server can decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * it, using the session key available in the service ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param protocolVersion used to set preMaster[0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param generator random number generator for generating premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param sessionKey Kerberos session key for encrypting premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    KerberosPreMasterSecret(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        SecureRandom generator, EncryptionKey sessionKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (sessionKey.getEType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               "session keys with des3-cbc-hmac-sha1-kd encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
               "are not supported for TLS Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        preMaster = generatePreMaster(generator, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Encrypt premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            EncryptedData eData = new EncryptedData(sessionKey, preMaster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                KeyUsage.KU_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            encrypted = eData.getBytes();  // not ASN.1 encoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw (SSLKeyException)new SSLKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                ("Kerberos premaster secret error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructor used by server to decrypt encrypted premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The protocol version in preMaster[0,1] must match either currentVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * or clientVersion, otherwise, the premaster secret is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * a random one to foil possible attack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param currentVersion version of protocol being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param clientVersion version requested by client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param generator random number generator used to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *        bogus premaster secret if premaster secret verification fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param input input stream from which to read the encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *        premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param sessionKey Kerberos session key to be used for decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    KerberosPreMasterSecret(ProtocolVersion currentVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ProtocolVersion clientVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        SecureRandom generator, HandshakeInStream input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        EncryptionKey sessionKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         // Extract encrypted premaster secret from message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         encrypted = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (encrypted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                Debug.println(System.out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                     "encrypted premaster secret", encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (sessionKey.getEType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
               "session keys with des3-cbc-hmac-sha1-kd encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
               "are not supported for TLS Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         // Decrypt premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            EncryptedData data = new EncryptedData(sessionKey.getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        null /* optional kvno */, encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            byte[] temp = data.decrypt(sessionKey, KeyUsage.KU_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                 if (encrypted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                     Debug.println(System.out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                         "decrypted premaster secret", temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Reset data stream after decryption, remove redundant bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            preMaster =  data.reset(temp, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            protocolVersion = ProtocolVersion.valueOf(preMaster[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                 preMaster[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 System.out.println("Kerberos PreMasterSecret version: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // catch exception & process below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            preMaster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            protocolVersion = currentVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // check if the premaster secret version is ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // the specification says that it must be the maximum version supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // by the client from its ClientHello message. However, many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // implementations send the negotiated version, so accept both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // NOTE that we may be comparing two unsupported version numbers in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // the second case, which is why we cannot use object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // equality in this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        boolean versionMismatch = (protocolVersion != currentVersion) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                  (protocolVersion.v != clientVersion.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * Bogus decrypted ClientKeyExchange? If so, conjure a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * a random preMaster secret that will fail later during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * Finished message processing. This is a countermeasure against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * the "interactive RSA PKCS#1 encryption envelop attack" reported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * in June 1998. Preserving the executation path will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * mitigate timing attacks and force consistent error handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * that will prevent an attacking client from differentiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * different kinds of decrypted ClientKeyExchange bogosities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         if ((preMaster == null) || (preMaster.length != 48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                || versionMismatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                System.out.println("Kerberos PreMasterSecret error, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                   + "generating random secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (preMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    Debug.println(System.out, "Invalid secret", preMaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            preMaster = generatePreMaster(generator, currentVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            protocolVersion = currentVersion;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Used by server to generate premaster secret in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * problem decoding ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param protocolVersion used for preMaster[0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param generator random number generator to use for generating secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    KerberosPreMasterSecret(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        SecureRandom generator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        preMaster = generatePreMaster(generator, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static byte[] generatePreMaster(SecureRandom rand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        ProtocolVersion ver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] pm = new byte[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        rand.nextBytes(pm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        pm[0] = ver.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        pm[1] = ver.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return pm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // Clone not needed; package internal use only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    byte[] getUnencrypted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // Clone not needed; package internal use only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    byte[] getEncrypted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return encrypted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}