Path: blob/master/src/java.base/share/classes/sun/security/internal/spec/TlsMasterSecretParameterSpec.java
41161 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.internal.spec;2627import java.security.spec.AlgorithmParameterSpec;2829import javax.crypto.SecretKey;3031/**32* Parameters for SSL/TLS master secret generation.33* This class encapsulates the information necessary to calculate a SSL/TLS34* master secret from the premaster secret and other parameters.35* It is used to initialize KeyGenerators of the type "TlsMasterSecret".36*37* <p>Instances of this class are immutable.38*39* @since 1.640* @author Andreas Sterbenz41* @deprecated Sun JDK internal use only --- WILL BE REMOVED in a future42* release.43*/44@Deprecated45public class TlsMasterSecretParameterSpec implements AlgorithmParameterSpec {4647private final SecretKey premasterSecret;48private final int majorVersion, minorVersion;49private final byte[] clientRandom, serverRandom;50private final byte[] extendedMasterSecretSessionHash;51private final String prfHashAlg;52private final int prfHashLength;53private final int prfBlockSize;5455/**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 exchange60* algorithm was RSA and <code>"TlsPremasterSecret"</code> otherwise.61*62* @param premasterSecret the premaster secret63* @param majorVersion the major number of the protocol version64* @param minorVersion the minor number of the protocol version65* @param clientRandom the client's random value66* @param serverRandom the server's random value67* @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 null76* @throws IllegalArgumentException if minorVersion or majorVersion are77* negative or larger than 25578*/79public TlsMasterSecretParameterSpec(SecretKey premasterSecret,80int majorVersion, int minorVersion,81byte[] clientRandom, byte[] serverRandom,82String prfHashAlg, int prfHashLength, int prfBlockSize) {83this(premasterSecret, majorVersion, minorVersion,84clientRandom, serverRandom,85new byte[0],86prfHashAlg, prfHashLength, prfBlockSize);87}8889/**90* Constructs a new TlsMasterSecretParameterSpec.91*92* <p>The <code>getAlgorithm()</code> method of <code>premasterSecret</code>93* should return <code>"TlsRsaPremasterSecret"</code> if the key exchange94* algorithm was RSA and <code>"TlsPremasterSecret"</code> otherwise.95*96* @param premasterSecret the premaster secret97* @param majorVersion the major number of the protocol version98* @param minorVersion the minor number of the protocol version99* @param extendedMasterSecretSessionHash the session hash for100* Extended Master Secret101* @param prfHashAlg the name of the TLS PRF hash algorithm to use.102* Used only for TLS 1.2+. TLS1.1 and earlier use a fixed PRF.103* @param prfHashLength the output length of the TLS PRF hash algorithm.104* Used only for TLS 1.2+.105* @param prfBlockSize the input block size of the TLS PRF hash algorithm.106* Used only for TLS 1.2+.107*108* @throws NullPointerException if premasterSecret is null109* @throws IllegalArgumentException if minorVersion or majorVersion are110* negative or larger than 255111*/112public TlsMasterSecretParameterSpec(SecretKey premasterSecret,113int majorVersion, int minorVersion,114byte[] extendedMasterSecretSessionHash,115String prfHashAlg, int prfHashLength, int prfBlockSize) {116this(premasterSecret, majorVersion, minorVersion,117new byte[0], new byte[0],118extendedMasterSecretSessionHash,119prfHashAlg, prfHashLength, prfBlockSize);120}121122private TlsMasterSecretParameterSpec(SecretKey premasterSecret,123int majorVersion, int minorVersion,124byte[] clientRandom, byte[] serverRandom,125byte[] extendedMasterSecretSessionHash,126String prfHashAlg, int prfHashLength, int prfBlockSize) {127if (premasterSecret == null) {128throw new NullPointerException("premasterSecret must not be null");129}130this.premasterSecret = premasterSecret;131this.majorVersion = checkVersion(majorVersion);132this.minorVersion = checkVersion(minorVersion);133this.clientRandom = clientRandom.clone();134this.serverRandom = serverRandom.clone();135this.extendedMasterSecretSessionHash =136(extendedMasterSecretSessionHash != null ?137extendedMasterSecretSessionHash.clone() : new byte[0]);138this.prfHashAlg = prfHashAlg;139this.prfHashLength = prfHashLength;140this.prfBlockSize = prfBlockSize;141}142143static int checkVersion(int version) {144if ((version < 0) || (version > 255)) {145throw new IllegalArgumentException(146"Version must be between 0 and 255");147}148return version;149}150151/**152* Returns the premaster secret.153*154* @return the premaster secret.155*/156public SecretKey getPremasterSecret() {157return premasterSecret;158}159160/**161* Returns the major version number.162*163* @return the major version number.164*/165public int getMajorVersion() {166return majorVersion;167}168169/**170* Returns the minor version number.171*172* @return the minor version number.173*/174public int getMinorVersion() {175return minorVersion;176}177178/**179* Returns a copy of the client's random value.180*181* @return a copy of the client's random value.182*/183public byte[] getClientRandom() {184return clientRandom.clone();185}186187/**188* Returns a copy of the server's random value.189*190* @return a copy of the server's random value.191*/192public byte[] getServerRandom() {193return serverRandom.clone();194}195196/**197* Returns a copy of the Extended Master Secret session hash.198*199* @return a copy of the Extended Master Secret session hash, or an empty200* array if no extended master secret session hash was provided201* at instantiation time202*/203public byte[] getExtendedMasterSecretSessionHash() {204return extendedMasterSecretSessionHash.clone();205}206207/**208* Obtains the PRF hash algorithm to use in the PRF calculation.209*210* @return the hash algorithm.211*/212public String getPRFHashAlg() {213return prfHashAlg;214}215216/**217* Obtains the length of the PRF hash algorithm.218*219* @return the hash algorithm length.220*/221public int getPRFHashLength() {222return prfHashLength;223}224225/**226* Obtains the block size of the PRF hash algorithm.227*228* @return the hash algorithm block size.229*/230public int getPRFBlockSize() {231return prfBlockSize;232}233}234235236