jdk/src/share/classes/com/sun/crypto/provider/TlsKeyMaterialGenerator.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3353 ddbd63234844
child 7043 5e2d1edeb2c7
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     2
 * Copyright (c) 2005, 2009, 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.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.internal.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static com.sun.crypto.provider.TlsPrfGenerator.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * KeyGenerator implementation for the SSL/TLS master secret derivation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class TlsKeyMaterialGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final static String MSG = "TlsKeyMaterialGenerator must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        + "initialized using a TlsKeyMaterialParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private TlsKeyMaterialParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public TlsKeyMaterialGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (params instanceof TlsKeyMaterialParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.spec = (TlsKeyMaterialParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new InvalidAlgorithmParameterException("Key format must be RAW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        protocolVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if ((protocolVersion < 0x0300) || (protocolVersion > 0x0302)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                ("Only SSL 3.0, TLS 1.0, and TLS 1.1 supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                ("TlsKeyMaterialGenerator must be initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return engineGenerateKey0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private SecretKey engineGenerateKey0() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        byte[] masterSecret = spec.getMasterSecret().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        byte[] clientRandom = spec.getClientRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        byte[] serverRandom = spec.getServerRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        SecretKey clientMacKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SecretKey serverMacKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SecretKey clientCipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        IvParameterSpec clientIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        SecretKey serverCipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        IvParameterSpec serverIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int macLength = spec.getMacKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int expandedKeyLength = spec.getExpandedCipherKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        boolean isExportable = (expandedKeyLength != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int keyLength = spec.getCipherKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int ivLength = spec.getIvLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int keyBlockLen = macLength + keyLength + (isExportable ? 0 : ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        keyBlockLen <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        byte[] keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        MessageDigest md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        MessageDigest sha = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // generate key block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (protocolVersion >= 0x0301) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            byte[] seed = concat(serverRandom, clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            keyBlock = doPRF(masterSecret, LABEL_KEY_EXPANSION, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        keyBlockLen, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            byte[] tmp = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            for (int i = 0, remaining = keyBlockLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                 remaining > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                 i++, remaining -= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                sha.update(SSL3_CONST[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                sha.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                sha.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                sha.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                sha.digest(tmp, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                md5.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                md5.update(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (remaining >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    md5.digest(keyBlock, i << 4, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    md5.digest(tmp, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    System.arraycopy(tmp, 0, keyBlock, i << 4, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // partition keyblock into individual secrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int ofs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        byte[] tmp = new byte[macLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // mac keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        clientMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        serverMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (keyLength == 0) { // SSL_RSA_WITH_NULL_* ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return new TlsKeyMaterialSpec(clientMacKey, serverMacKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        String alg = spec.getCipherAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        byte[] clientKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.arraycopy(keyBlock, ofs, clientKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        byte[] serverKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        System.arraycopy(keyBlock, ofs, serverKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (isExportable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // cipher keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            clientCipherKey = new SecretKeySpec(clientKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            serverCipherKey = new SecretKeySpec(serverKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // cipher key expansion and IV generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (protocolVersion >= 0x0301) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                byte[] seed = concat(clientRandom, serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                tmp = doPRF(clientKeyBytes, LABEL_CLIENT_WRITE_KEY, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                tmp = doPRF(serverKeyBytes, LABEL_SERVER_WRITE_KEY, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    byte[] block = doPRF(null, LABEL_IV_BLOCK, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                ivLength << 1, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    System.arraycopy(block, 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    System.arraycopy(block, ivLength, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                tmp = new byte[expandedKeyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                md5.update(clientKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                md5.update(serverKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return new TlsKeyMaterialSpec(clientMacKey, serverMacKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            clientCipherKey, clientIv, serverCipherKey, serverIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}