jdk/src/share/classes/sun/security/ssl/krb5/KerberosPreMasterSecret.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5506 202f599c92aa
child 7039 6464c8e62a18
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    26
package sun.security.ssl.krb5;
2
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.*;
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
    30
import java.util.Arrays;
2
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
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    39
import sun.security.ssl.Debug;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    40
import sun.security.ssl.HandshakeInStream;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    41
import sun.security.ssl.HandshakeMessage;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    42
import sun.security.ssl.ProtocolVersion;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This is the Kerberos premaster secret in the Kerberos client key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * exchange message (CLIENT --> SERVER); it holds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Kerberos-encrypted pre-master secret. The secret is encrypted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Kerberos session key.  The padding and size of the resulting message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * depends on the session key type, but the pre-master secret is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * always exactly 48 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
final class KerberosPreMasterSecret {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private ProtocolVersion protocolVersion; // preMaster [0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte preMaster[];           // 48 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte encrypted[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructor used by client to generate premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Client randomly creates a pre-master secret and encrypts it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * using the Kerberos session key; only the server can decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * it, using the session key available in the service ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param protocolVersion used to set preMaster[0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param generator random number generator for generating premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param sessionKey Kerberos session key for encrypting premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    KerberosPreMasterSecret(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SecureRandom generator, EncryptionKey sessionKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (sessionKey.getEType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
               "session keys with des3-cbc-hmac-sha1-kd encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
               "are not supported for TLS Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        preMaster = generatePreMaster(generator, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // Encrypt premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            EncryptedData eData = new EncryptedData(sessionKey, preMaster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                KeyUsage.KU_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            encrypted = eData.getBytes();  // not ASN.1 encoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw (SSLKeyException)new SSLKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ("Kerberos premaster secret error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Constructor used by server to decrypt encrypted premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The protocol version in preMaster[0,1] must match either currentVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * or clientVersion, otherwise, the premaster secret is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * a random one to foil possible attack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param currentVersion version of protocol being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param clientVersion version requested by client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param generator random number generator used to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *        bogus premaster secret if premaster secret verification fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param input input stream from which to read the encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *        premaster secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param sessionKey Kerberos session key to be used for decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    KerberosPreMasterSecret(ProtocolVersion currentVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ProtocolVersion clientVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        SecureRandom generator, HandshakeInStream input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        EncryptionKey sessionKey) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         // Extract encrypted premaster secret from message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         encrypted = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (encrypted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                Debug.println(System.out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                     "encrypted premaster secret", encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (sessionKey.getEType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
               "session keys with des3-cbc-hmac-sha1-kd encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
               "are not supported for TLS Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   131
        // Decrypt premaster secret
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   132
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            EncryptedData data = new EncryptedData(sessionKey.getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        null /* optional kvno */, encrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            byte[] temp = data.decrypt(sessionKey, KeyUsage.KU_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                 if (encrypted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                     Debug.println(System.out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                         "decrypted premaster secret", temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   144
            // Remove padding bytes after decryption. Only DES and DES3 have
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   145
            // paddings and we don't support DES3 in TLS (see above)
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   146
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   147
            if (temp.length == 52 &&
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   148
                    data.getEType() == EncryptedData.ETYPE_DES_CBC_CRC) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   149
                // For des-cbc-crc, 4 paddings. Value can be 0x04 or 0x00.
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   150
                if (paddingByteIs(temp, 52, (byte)4) ||
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   151
                        paddingByteIs(temp, 52, (byte)0)) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   152
                    temp = Arrays.copyOf(temp, 48);
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   153
                }
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   154
            } else if (temp.length == 56 &&
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   155
                    data.getEType() == EncryptedData.ETYPE_DES_CBC_MD5) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   156
                // For des-cbc-md5, 8 paddings with 0x08, or no padding
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   157
                if (paddingByteIs(temp, 56, (byte)8)) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   158
                    temp = Arrays.copyOf(temp, 48);
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   159
                }
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   160
            }
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   161
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   162
            preMaster = temp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            protocolVersion = ProtocolVersion.valueOf(preMaster[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                 preMaster[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 System.out.println("Kerberos PreMasterSecret version: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // catch exception & process below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            preMaster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            protocolVersion = currentVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // check if the premaster secret version is ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // the specification says that it must be the maximum version supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // by the client from its ClientHello message. However, many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // implementations send the negotiated version, so accept both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // NOTE that we may be comparing two unsupported version numbers in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // the second case, which is why we cannot use object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // equality in this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        boolean versionMismatch = (protocolVersion != currentVersion) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                  (protocolVersion.v != clientVersion.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * Bogus decrypted ClientKeyExchange? If so, conjure a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * a random preMaster secret that will fail later during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * Finished message processing. This is a countermeasure against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * the "interactive RSA PKCS#1 encryption envelop attack" reported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * in June 1998. Preserving the executation path will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * mitigate timing attacks and force consistent error handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * that will prevent an attacking client from differentiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * different kinds of decrypted ClientKeyExchange bogosities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         if ((preMaster == null) || (preMaster.length != 48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                || versionMismatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                System.out.println("Kerberos PreMasterSecret error, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                   + "generating random secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (preMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    Debug.println(System.out, "Invalid secret", preMaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            preMaster = generatePreMaster(generator, currentVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            protocolVersion = currentVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   211
    /**
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   212
     * Checks if all paddings of data are b
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   213
     * @param data the block with padding
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   214
     * @param len length of data, >= 48
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   215
     * @param b expected padding byte
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   216
     */
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   217
    private static boolean paddingByteIs(byte[] data, int len, byte b) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   218
        for (int i=48; i<len; i++) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   219
            if (data[i] != b) return false;
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   220
        }
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   221
        return true;
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   222
    }
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   223
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Used by server to generate premaster secret in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * problem decoding ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param protocolVersion used for preMaster[0,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param generator random number generator to use for generating secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    KerberosPreMasterSecret(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        SecureRandom generator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        preMaster = generatePreMaster(generator, protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static byte[] generatePreMaster(SecureRandom rand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        ProtocolVersion ver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        byte[] pm = new byte[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        rand.nextBytes(pm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        pm[0] = ver.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        pm[1] = ver.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return pm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   249
    // Clone not needed; internal use only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    byte[] getUnencrypted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return preMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   254
    // Clone not needed; internal use only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    byte[] getEncrypted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return encrypted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
}