jdk/src/share/classes/com/sun/crypto/provider/TlsKeyMaterialGenerator.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
child 16045 9d08c3b9a6a0
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
     2
 * Copyright (c) 2005, 2011, 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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.internal.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static com.sun.crypto.provider.TlsPrfGenerator.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * KeyGenerator implementation for the SSL/TLS master secret derivation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class TlsKeyMaterialGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final static String MSG = "TlsKeyMaterialGenerator must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        + "initialized using a TlsKeyMaterialParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private TlsKeyMaterialParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public TlsKeyMaterialGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (params instanceof TlsKeyMaterialParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.spec = (TlsKeyMaterialParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    67
            throw new InvalidAlgorithmParameterException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    68
                "Key format must be RAW");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    70
        protocolVersion = (spec.getMajorVersion() << 8)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    71
            | spec.getMinorVersion();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    72
        if ((protocolVersion < 0x0300) || (protocolVersion > 0x0303)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    73
            throw new InvalidAlgorithmParameterException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    74
                "Only SSL 3.0, TLS 1.0/1.1/1.2 supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (spec == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    84
            throw new IllegalStateException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    85
                "TlsKeyMaterialGenerator must be initialized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return engineGenerateKey0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private SecretKey engineGenerateKey0() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        byte[] masterSecret = spec.getMasterSecret().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        byte[] clientRandom = spec.getClientRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte[] serverRandom = spec.getServerRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SecretKey clientMacKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SecretKey serverMacKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        SecretKey clientCipherKey = null;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   103
        SecretKey serverCipherKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        IvParameterSpec clientIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        IvParameterSpec serverIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int macLength = spec.getMacKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int expandedKeyLength = spec.getExpandedCipherKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        boolean isExportable = (expandedKeyLength != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int keyLength = spec.getCipherKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int ivLength = spec.getIvLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   113
        int keyBlockLen = macLength + keyLength
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   114
            + (isExportable ? 0 : ivLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        keyBlockLen <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        byte[] keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   118
        // These may be used again later for exportable suite calculations.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   119
        MessageDigest md5 = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   120
        MessageDigest sha = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // generate key block
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   123
        if (protocolVersion >= 0x0303) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   124
            // TLS 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            byte[] seed = concat(serverRandom, clientRandom);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   126
            keyBlock = doTLS12PRF(masterSecret, LABEL_KEY_EXPANSION, seed,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   127
                        keyBlockLen, spec.getPRFHashAlg(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   128
                        spec.getPRFHashLength(), spec.getPRFBlockSize());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   129
        } else if (protocolVersion >= 0x0301) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   130
            // TLS 1.0/1.1
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   131
            md5 = MessageDigest.getInstance("MD5");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   132
            sha = MessageDigest.getInstance("SHA1");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   133
            byte[] seed = concat(serverRandom, clientRandom);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   134
            keyBlock = doTLS10PRF(masterSecret, LABEL_KEY_EXPANSION, seed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        keyBlockLen, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // SSL
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   138
            md5 = MessageDigest.getInstance("MD5");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   139
            sha = MessageDigest.getInstance("SHA1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            byte[] tmp = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (int i = 0, remaining = keyBlockLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                 remaining > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 i++, remaining -= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                sha.update(SSL3_CONST[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                sha.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                sha.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                sha.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                sha.digest(tmp, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                md5.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                md5.update(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (remaining >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    md5.digest(keyBlock, i << 4, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    md5.digest(tmp, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    System.arraycopy(tmp, 0, keyBlock, i << 4, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // partition keyblock into individual secrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int ofs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        byte[] tmp = new byte[macLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // mac keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        clientMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        serverMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (keyLength == 0) { // SSL_RSA_WITH_NULL_* ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return new TlsKeyMaterialSpec(clientMacKey, serverMacKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String alg = spec.getCipherAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   185
        // cipher keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte[] clientKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.arraycopy(keyBlock, ofs, clientKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        byte[] serverKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        System.arraycopy(keyBlock, ofs, serverKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (isExportable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // cipher keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            clientCipherKey = new SecretKeySpec(clientKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            serverCipherKey = new SecretKeySpec(serverKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   199
            // IV keys if needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   212
            // if exportable suites, calculate the alternate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // cipher key expansion and IV generation
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   214
            if (protocolVersion >= 0x0302) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   215
                // TLS 1.1+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   216
                throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   217
                    "Internal Error:  TLS 1.1+ should not be negotiating" +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   218
                    "exportable ciphersuites");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   219
            } else if (protocolVersion == 0x0301) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   220
                // TLS 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                byte[] seed = concat(clientRandom, serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   223
                tmp = doTLS10PRF(clientKeyBytes, LABEL_CLIENT_WRITE_KEY, seed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   227
                tmp = doTLS10PRF(serverKeyBytes, LABEL_SERVER_WRITE_KEY, seed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    tmp = new byte[ivLength];
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   233
                    byte[] block = doTLS10PRF(null, LABEL_IV_BLOCK, seed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                ivLength << 1, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    System.arraycopy(block, 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    System.arraycopy(block, ivLength, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   241
                // SSLv3
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                tmp = new byte[expandedKeyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                md5.update(clientKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                md5.update(serverKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return new TlsKeyMaterialSpec(clientMacKey, serverMacKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            clientCipherKey, clientIv, serverCipherKey, serverIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}