1/*
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.internal.spec;
27
28import java.security.spec.AlgorithmParameterSpec;
29
30import javax.crypto.SecretKey;
31
32/**
33 * Parameters for SSL/TLS master secret generation.
34 * This class encapsulates the information necessary to calculate a SSL/TLS
35 * master secret from the premaster secret and other parameters.
36 * It is used to initialize KeyGenerators of the type "TlsMasterSecret".
37 *
38 * <p>Instances of this class are immutable.
39 *
40 * @since   1.6
41 * @author  Andreas Sterbenz
42 * @deprecated Sun JDK internal use only --- WILL BE REMOVED in a future
43 * release.
44 */
45@Deprecated
46public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec {
47
48    private final SecretKey premasterSecret;
49    private final int majorVersion, minorVersion;
50    private final byte[] clientRandom, serverRandom;
51    private final String prfHashAlg;
52    private final int prfHashLength;
53    private final int prfBlockSize;
54
55    /**
56     * Constructs a new TlsMasterSecretParameterSpec.
57     *
58     * <p>The <code>getAlgorithm()</code> method of <code>premasterSecret</code>
59     * should return <code>"TlsRsaPremasterSecret"</code> if the key exchange
60     * algorithm was RSA and <code>"TlsPremasterSecret"</code> otherwise.
61     *
62     * @param premasterSecret the premaster secret
63     * @param majorVersion the major number of the protocol version
64     * @param minorVersion the minor number of the protocol version
65     * @param clientRandom the client's random value
66     * @param serverRandom the server's random value
67     * @param prfHashAlg the name of the TLS PRF hash algorithm to use.
68     *        Used only for TLS 1.2+.  TLS1.1 and earlier use a fixed PRF.
69     * @param prfHashLength the output length of the TLS PRF hash algorithm.
70     *        Used only for TLS 1.2+.
71     * @param prfBlockSize the input block size of the TLS PRF hash algorithm.
72     *        Used only for TLS 1.2+.
73     *
74     * @throws NullPointerException if premasterSecret, clientRandom,
75     *   or serverRandom are null
76     * @throws IllegalArgumentException if minorVersion or majorVersion are
77     *   negative or larger than 255
78     */
79    public TlsMasterSecretParameterSpec(SecretKey premasterSecret,
80            int majorVersion, int minorVersion,
81            byte[] clientRandom, byte[] serverRandom,
82            String prfHashAlg, int prfHashLength, int prfBlockSize) {
83        if (premasterSecret == null) {
84            throw new NullPointerException("premasterSecret must not be null");
85        }
86        this.premasterSecret = premasterSecret;
87        this.majorVersion = checkVersion(majorVersion);
88        this.minorVersion = checkVersion(minorVersion);
89        this.clientRandom = clientRandom.clone();
90        this.serverRandom = serverRandom.clone();
91        this.prfHashAlg = prfHashAlg;
92        this.prfHashLength = prfHashLength;
93        this.prfBlockSize = prfBlockSize;
94    }
95
96    static int checkVersion(int version) {
97        if ((version < 0) || (version > 255)) {
98            throw new IllegalArgumentException(
99                        "Version must be between 0 and 255");
100        }
101        return version;
102    }
103
104    /**
105     * Returns the premaster secret.
106     *
107     * @return the premaster secret.
108     */
109    public SecretKey getPremasterSecret() {
110        return premasterSecret;
111    }
112
113    /**
114     * Returns the major version number.
115     *
116     * @return the major version number.
117     */
118    public int getMajorVersion() {
119        return majorVersion;
120    }
121
122    /**
123     * Returns the minor version number.
124     *
125     * @return the minor version number.
126     */
127    public int getMinorVersion() {
128        return minorVersion;
129    }
130
131    /**
132     * Returns a copy of the client's random value.
133     *
134     * @return a copy of the client's random value.
135     */
136    public byte[] getClientRandom() {
137        return clientRandom.clone();
138    }
139
140    /**
141     * Returns a copy of the server's random value.
142     *
143     * @return a copy of the server's random value.
144     */
145    public byte[] getServerRandom() {
146        return serverRandom.clone();
147    }
148
149    /**
150     * Obtains the PRF hash algorithm to use in the PRF calculation.
151     *
152     * @return the hash algorithm.
153     */
154    public String getPRFHashAlg() {
155        return prfHashAlg;
156    }
157
158    /**
159     * Obtains the length of the PRF hash algorithm.
160     *
161     * @return the hash algorithm length.
162     */
163    public int getPRFHashLength() {
164        return prfHashLength;
165    }
166
167    /**
168     * Obtains the block size of the PRF hash algorithm.
169     *
170     * @return the hash algorithm block size.
171     */
172    public int getPRFBlockSize() {
173        return prfBlockSize;
174    }
175}
176