jdk/src/share/classes/sun/security/internal/spec/TlsKeyMaterialParameterSpec.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7039 6464c8e62a18
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: 2
diff changeset
     2
 * Copyright (c) 2005, 2007, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.security.internal.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Parameters for SSL/TLS key material generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class is used to initialize KeyGenerator of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * "TlsKeyMaterial". The keys returned by such KeyGenerators will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * instances of {@link TlsKeyMaterialSpec}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @deprecated Sun JDK internal use only --- WILL BE REMOVED in Dolphin (JDK 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class TlsKeyMaterialParameterSpec implements AlgorithmParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final SecretKey masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final int majorVersion, minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final byte[] clientRandom, serverRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final String cipherAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final int cipherKeyLength, ivLength, macKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final int expandedCipherKeyLength; // == 0 for domestic ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs a new TlsKeyMaterialParameterSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param masterSecret the master secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param majorVersion the major number of the protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param minorVersion the minor number of the protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param clientRandom the client's random value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param serverRandom the server's random value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param cipherAlgorithm the algorithm name of the cipher keys to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *    be generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param cipherKeyLength if 0, no cipher keys will be generated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *    otherwise, the length in bytes of cipher keys to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *    generated for domestic cipher suites; for cipher suites defined as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *    exportable, the number of key material bytes to be generated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param expandedCipherKeyLength 0 for domestic cipher suites; for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *    exportable cipher suites the length in bytes of the key to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *    generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param ivLength the length in bytes of the initialization vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *    to be generated, or 0 if no initialization vector is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param macKeyLength the length in bytes of the MAC key to be generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @throws NullPointerException if masterSecret, clientRandom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *   serverRandom, or cipherAlgorithm are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws IllegalArgumentException if the algorithm of masterSecret is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *   not TlsMasterSecret, or if majorVersion or minorVersion are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *   negative or larger than 255; or if cipherKeyLength, expandedKeyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *   ivLength, or macKeyLength are negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public TlsKeyMaterialParameterSpec(SecretKey masterSecret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int majorVersion, int minorVersion, byte[] clientRandom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            byte[] serverRandom, String cipherAlgorithm, int cipherKeyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int expandedCipherKeyLength, int ivLength, int macKeyLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (masterSecret.getAlgorithm().equals("TlsMasterSecret") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IllegalArgumentException("Not a TLS master secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (cipherAlgorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.masterSecret = masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.majorVersion = TlsMasterSecretParameterSpec.checkVersion(majorVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.minorVersion = TlsMasterSecretParameterSpec.checkVersion(minorVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.clientRandom = clientRandom.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.serverRandom = serverRandom.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.cipherAlgorithm = cipherAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.cipherKeyLength = checkSign(cipherKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.expandedCipherKeyLength = checkSign(expandedCipherKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.ivLength = checkSign(ivLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.macKeyLength = checkSign(macKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static int checkSign(int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (k < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalArgumentException("Value must not be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Returns the master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return the master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public SecretKey getMasterSecret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Returns the major version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the major version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int getMajorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return majorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the minor version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the minor version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public int getMinorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns a copy of the client's random value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return a copy of the client's random value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public byte[] getClientRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return clientRandom.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns a copy of the server's random value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return a copy of the server's random value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public byte[] getServerRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return serverRandom.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns the cipher algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the cipher algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public String getCipherAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return cipherAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Returns the length in bytes of the encryption key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the length in bytes of the encryption key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int getCipherKeyLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return cipherKeyLength;
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
     * Returns the length in bytes of the expanded encryption key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the length in bytes of the expanded encryption key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public int getExpandedCipherKeyLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return expandedCipherKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the length in bytes of the initialization vector to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return the length in bytes of the initialization vector to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public int getIvLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return ivLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns the length in bytes of the MAC key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the length in bytes of the MAC key to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public int getMacKeyLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return macKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}