jdk/src/share/classes/com/sun/crypto/provider/TlsMasterSecretGenerator.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.internal.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.internal.spec.TlsMasterSecretParameterSpec;
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 TlsMasterSecretGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final static String MSG = "TlsMasterSecretGenerator must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        + "initialized using a TlsMasterSecretParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private TlsMasterSecretParameterSpec 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 TlsMasterSecretGenerator() {
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 TlsMasterSecretParameterSpec == 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 = (TlsMasterSecretParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if ("RAW".equals(spec.getPremasterSecret().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
                "TlsMasterSecretGenerator must be initialized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SecretKey premasterKey = spec.getPremasterSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte[] premaster = premasterKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int premasterMajor, premasterMinor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (premasterKey.getAlgorithm().equals("TlsRsaPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            premasterMajor = premaster[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            premasterMinor = premaster[1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // DH, KRB5, others
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            premasterMajor = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            premasterMinor = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            byte[] master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            byte[] clientRandom = spec.getClientRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            byte[] serverRandom = spec.getServerRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (protocolVersion >= 0x0301) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                byte[] seed = concat(clientRandom, serverRandom);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   108
                master = ((protocolVersion >= 0x0303) ?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   109
                    doTLS12PRF(premaster, LABEL_MASTER_SECRET, seed, 48,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   110
                        spec.getPRFHashAlg(), spec.getPRFHashLength(),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   111
                        spec.getPRFBlockSize()) :
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   112
                    doTLS10PRF(premaster, LABEL_MASTER_SECRET, seed, 48));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                master = new byte[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                MessageDigest md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                MessageDigest sha = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                byte[] tmp = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    sha.update(SSL3_CONST[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    sha.update(premaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    sha.update(clientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    sha.update(serverRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    sha.digest(tmp, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    md5.update(premaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    md5.update(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    md5.digest(master, i << 4, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   133
            return new TlsMasterSecretKey(master, premasterMajor,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   134
                premasterMinor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final class TlsMasterSecretKey implements TlsMasterSecret {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   143
        private static final long serialVersionUID = 1019571680375368880L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        private final int majorVersion, minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        TlsMasterSecretKey(byte[] key, int majorVersion, int minorVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            this.majorVersion = majorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            this.minorVersion = minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public int getMajorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return majorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public int getMinorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return "TlsMasterSecret";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return key.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}