Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/internal/spec/TlsRsaPremasterSecretParameterSpec.java
38922 views
/*1* Copyright (c) 2005, 2013, 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;28import java.security.AccessController;29import java.security.PrivilegedAction;3031/**32* Parameters for SSL/TLS RSA premaster secret.33*34* <p>Instances of this class are immutable.35*36* @since 1.637* @author Andreas Sterbenz38* @deprecated Sun JDK internal use only --- WILL BE REMOVED in a future39* release.40*/41@Deprecated42public class TlsRsaPremasterSecretParameterSpec43implements AlgorithmParameterSpec {4445private final byte[] encodedSecret;4647/*48* The TLS spec says that the version in the RSA premaster secret must49* be the maximum version supported by the client (i.e. the version it50* requested in its client hello version). However, we (and other51* implementations) used to send the active negotiated version. The52* system property below allows to toggle the behavior.53*/54private final static String PROP_NAME =55"com.sun.net.ssl.rsaPreMasterSecretFix";5657/*58* Default is "false" (old behavior) for compatibility reasons in59* SSLv3/TLSv1. Later protocols (TLSv1.1+) do not use this property.60*/61private final static boolean rsaPreMasterSecretFix =62AccessController.doPrivileged(new PrivilegedAction<Boolean>() {63public Boolean run() {64String value = System.getProperty(PROP_NAME);65if (value != null && value.equalsIgnoreCase("true")) {66return Boolean.TRUE;67}6869return Boolean.FALSE;70}71});7273private final int clientVersion;74private final int serverVersion;7576/**77* Constructs a new TlsRsaPremasterSecretParameterSpec.78*79* @param clientVersion the version of the TLS protocol by which the80* client wishes to communicate during this session81* @param serverVersion the negotiated version of the TLS protocol which82* contains the lower of that suggested by the client in the client83* hello and the highest supported by the server.84*85* @throws IllegalArgumentException if clientVersion or serverVersion are86* negative or larger than (2^16 - 1)87*/88public TlsRsaPremasterSecretParameterSpec(89int clientVersion, int serverVersion) {9091this.clientVersion = checkVersion(clientVersion);92this.serverVersion = checkVersion(serverVersion);93this.encodedSecret = null;94}9596/**97* Constructs a new TlsRsaPremasterSecretParameterSpec.98*99* @param clientVersion the version of the TLS protocol by which the100* client wishes to communicate during this session101* @param serverVersion the negotiated version of the TLS protocol which102* contains the lower of that suggested by the client in the client103* hello and the highest supported by the server.104* @param encodedSecret the encoded secret key105*106* @throws IllegalArgumentException if clientVersion or serverVersion are107* negative or larger than (2^16 - 1) or if encodedSecret is not108* exactly 48 bytes109*/110public TlsRsaPremasterSecretParameterSpec(111int clientVersion, int serverVersion, byte[] encodedSecret) {112113this.clientVersion = checkVersion(clientVersion);114this.serverVersion = checkVersion(serverVersion);115if (encodedSecret == null || encodedSecret.length != 48) {116throw new IllegalArgumentException(117"Encoded secret is not exactly 48 bytes");118}119this.encodedSecret = encodedSecret.clone();120}121122/**123* Returns the version of the TLS protocol by which the client wishes to124* communicate during this session.125*126* @return the version of the TLS protocol in ClientHello message127*/128public int getClientVersion() {129return clientVersion;130}131132/**133* Returns the negotiated version of the TLS protocol which contains the134* lower of that suggested by the client in the client hello and the135* highest supported by the server.136*137* @return the negotiated version of the TLS protocol in ServerHello message138*/139public int getServerVersion() {140return serverVersion;141}142143/**144* Returns the major version used in RSA premaster secret.145*146* @return the major version used in RSA premaster secret.147*/148public int getMajorVersion() {149if (rsaPreMasterSecretFix || clientVersion >= 0x0302) {150// 0x0302: TLSv1.1151return (clientVersion >>> 8) & 0xFF;152}153154return (serverVersion >>> 8) & 0xFF;155}156157/**158* Returns the minor version used in RSA premaster secret.159*160* @return the minor version used in RSA premaster secret.161*/162public int getMinorVersion() {163if (rsaPreMasterSecretFix || clientVersion >= 0x0302) {164// 0x0302: TLSv1.1165return clientVersion & 0xFF;166}167168return serverVersion & 0xFF;169}170171private int checkVersion(int version) {172if ((version < 0) || (version > 0xFFFF)) {173throw new IllegalArgumentException(174"Version must be between 0 and 65,535");175}176return version;177}178179/**180* Returns the encoded secret.181*182* @return the encoded secret, may be null if no encoded secret.183*/184public byte[] getEncodedSecret() {185return encodedSecret == null ? null : encodedSecret.clone();186}187}188189190