jdk/src/share/classes/com/sun/crypto/provider/TlsKeyMaterialGenerator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
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 2005-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 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
        SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (params instanceof TlsKeyMaterialParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.spec = (TlsKeyMaterialParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new InvalidAlgorithmParameterException("Key format must be RAW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        protocolVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if ((protocolVersion < 0x0300) || (protocolVersion > 0x0302)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                ("Only SSL 3.0, TLS 1.0, and TLS 1.1 supported");
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ("TlsKeyMaterialGenerator must be initialized");
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        IvParameterSpec clientIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        SecretKey serverCipherKey = 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int keyBlockLen = macLength + keyLength + (isExportable ? 0 : ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        keyBlockLen <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        byte[] keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        MessageDigest md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        MessageDigest sha = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // generate key block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (protocolVersion >= 0x0301) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            byte[] seed = concat(serverRandom, clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            keyBlock = doPRF(masterSecret, LABEL_KEY_EXPANSION, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        keyBlockLen, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            keyBlock = new byte[keyBlockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            byte[] tmp = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            for (int i = 0, remaining = keyBlockLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                 remaining > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                 i++, remaining -= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                sha.update(SSL3_CONST[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                sha.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                sha.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                sha.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                sha.digest(tmp, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                md5.update(masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                md5.update(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (remaining >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    md5.digest(keyBlock, i << 4, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    md5.digest(tmp, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    System.arraycopy(tmp, 0, keyBlock, i << 4, remaining);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // partition keyblock into individual secrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int ofs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        byte[] tmp = new byte[macLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // mac keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        clientMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        ofs += macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        serverMacKey = new SecretKeySpec(tmp, "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (keyLength == 0) { // SSL_RSA_WITH_NULL_* ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return new TlsKeyMaterialSpec(clientMacKey, serverMacKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String alg = spec.getCipherAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        byte[] clientKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.arraycopy(keyBlock, ofs, clientKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] serverKeyBytes = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        System.arraycopy(keyBlock, ofs, serverKeyBytes, 0, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ofs += keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (isExportable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // cipher keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            clientCipherKey = new SecretKeySpec(clientKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            serverCipherKey = new SecretKeySpec(serverKeyBytes, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ofs += ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // cipher key expansion and IV generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (protocolVersion >= 0x0301) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                byte[] seed = concat(clientRandom, serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                tmp = doPRF(clientKeyBytes, LABEL_CLIENT_WRITE_KEY, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                tmp = doPRF(serverKeyBytes, LABEL_SERVER_WRITE_KEY, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            expandedKeyLength, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    byte[] block = doPRF(null, LABEL_IV_BLOCK, seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                ivLength << 1, md5, sha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    System.arraycopy(block, 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    System.arraycopy(block, ivLength, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    serverIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                tmp = new byte[expandedKeyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                md5.update(clientKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                clientCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                md5.update(serverKeyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                serverCipherKey = new SecretKeySpec(tmp, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (ivLength != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    tmp = new byte[ivLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    clientIv = new IvParameterSpec(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    md5.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    md5.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    serverIv = new IvParameterSpec(tmp);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return new TlsKeyMaterialSpec(clientMacKey, serverMacKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            clientCipherKey, clientIv, serverCipherKey, serverIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}