Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsMasterSecretGenerator.java
41154 views
/*1* Copyright (c) 2005, 2018, 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.pkcs11;2627import java.security.*;28import java.security.spec.AlgorithmParameterSpec;2930import javax.crypto.*;31import sun.security.internal.spec.TlsMasterSecretParameterSpec;3233import static sun.security.pkcs11.TemplateManager.*;34import sun.security.pkcs11.wrapper.*;3536import static sun.security.pkcs11.wrapper.PKCS11Constants.*;3738/**39* KeyGenerator for the SSL/TLS master secret.40*41* @author Andreas Sterbenz42* @since 1.643*/44public final class P11TlsMasterSecretGenerator extends KeyGeneratorSpi {4546private static final String MSG = "TlsMasterSecretGenerator must be "47+ "initialized using a TlsMasterSecretParameterSpec";4849// token instance50private final Token token;5152// algorithm name53private final String algorithm;5455// mechanism id56private long mechanism;5758private int tlsVersion;5960@SuppressWarnings("deprecation")61private TlsMasterSecretParameterSpec spec;62private P11Key p11Key;6364CK_VERSION ckVersion;6566// whether SSLv3 is supported67private final boolean supportSSLv3;6869P11TlsMasterSecretGenerator(Token token, String algorithm, long mechanism)70throws PKCS11Exception {71super();72this.token = token;73this.algorithm = algorithm;74this.mechanism = mechanism;7576// Given the current lookup order specified in SunPKCS11.java, if77// CKM_SSL3_MASTER_KEY_DERIVE is not used to construct this object,78// it means that this mech is disabled or unsupported.79supportSSLv3 = (mechanism == CKM_SSL3_MASTER_KEY_DERIVE);80}8182protected void engineInit(SecureRandom random) {83throw new InvalidParameterException(MSG);84}8586@SuppressWarnings("deprecation")87protected void engineInit(AlgorithmParameterSpec params,88SecureRandom random) throws InvalidAlgorithmParameterException {89if (params instanceof TlsMasterSecretParameterSpec == false) {90throw new InvalidAlgorithmParameterException(MSG);91}9293TlsMasterSecretParameterSpec spec = (TlsMasterSecretParameterSpec)params;94tlsVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();95if ((tlsVersion == 0x0300 && !supportSSLv3) ||96(tlsVersion < 0x0300) || (tlsVersion > 0x0303)) {97throw new InvalidAlgorithmParameterException98("Only" + (supportSSLv3? " SSL 3.0,": "") +99" TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" +100tlsVersion + ")");101}102103SecretKey key = spec.getPremasterSecret();104// algorithm should be either TlsRsaPremasterSecret or TlsPremasterSecret,105// but we omit the check106try {107p11Key = P11SecretKeyFactory.convertKey(token, key, null);108} catch (InvalidKeyException e) {109throw new InvalidAlgorithmParameterException("init() failed", e);110}111this.spec = spec;112final boolean isTlsRsaPremasterSecret =113p11Key.getAlgorithm().equals("TlsRsaPremasterSecret");114if (tlsVersion == 0x0300) {115mechanism = isTlsRsaPremasterSecret ?116CKM_SSL3_MASTER_KEY_DERIVE : CKM_SSL3_MASTER_KEY_DERIVE_DH;117} else if (tlsVersion == 0x0301 || tlsVersion == 0x0302) {118mechanism = isTlsRsaPremasterSecret ?119CKM_TLS_MASTER_KEY_DERIVE : CKM_TLS_MASTER_KEY_DERIVE_DH;120} else if (tlsVersion == 0x0303) {121mechanism = isTlsRsaPremasterSecret ?122CKM_TLS12_MASTER_KEY_DERIVE : CKM_TLS12_MASTER_KEY_DERIVE_DH;123}124if (isTlsRsaPremasterSecret) {125ckVersion = new CK_VERSION(0, 0);126} else {127// Note: we use DH for all non-RSA premaster secrets. That includes128// Kerberos. That should not be a problem because master secret129// calculation is always a straightforward application of the130// TLS PRF (or the SSL equivalent).131// The only thing special about RSA master secret calculation is132// that it extracts the version numbers from the premaster secret.133ckVersion = null;134}135}136137protected void engineInit(int keysize, SecureRandom random) {138throw new InvalidParameterException(MSG);139}140141protected SecretKey engineGenerateKey() {142if (spec == null) {143throw new IllegalStateException144("TlsMasterSecretGenerator must be initialized");145}146byte[] clientRandom = spec.getClientRandom();147byte[] serverRandom = spec.getServerRandom();148CK_SSL3_RANDOM_DATA random =149new CK_SSL3_RANDOM_DATA(clientRandom, serverRandom);150CK_MECHANISM ckMechanism = null;151if (tlsVersion < 0x0303) {152CK_SSL3_MASTER_KEY_DERIVE_PARAMS params =153new CK_SSL3_MASTER_KEY_DERIVE_PARAMS(random, ckVersion);154ckMechanism = new CK_MECHANISM(mechanism, params);155} else if (tlsVersion == 0x0303) {156CK_TLS12_MASTER_KEY_DERIVE_PARAMS params =157new CK_TLS12_MASTER_KEY_DERIVE_PARAMS(random, ckVersion,158Functions.getHashMechId(spec.getPRFHashAlg()));159ckMechanism = new CK_MECHANISM(mechanism, params);160}161Session session = null;162long p11KeyID = p11Key.getKeyID();163try {164session = token.getObjSession();165CK_ATTRIBUTE[] attributes = token.getAttributes(O_GENERATE,166CKO_SECRET_KEY, CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);167long keyID = token.p11.C_DeriveKey(session.id(),168ckMechanism, p11KeyID, attributes);169int major, minor;170if (ckVersion == null) {171major = -1;172minor = -1;173} else {174major = ckVersion.major;175minor = ckVersion.minor;176}177return P11Key.masterSecretKey(session, keyID,178"TlsMasterSecret", 48 << 3, attributes, major, minor);179} catch (Exception e) {180throw new ProviderException("Could not generate key", e);181} finally {182p11Key.releaseKeyID();183token.releaseSession(session);184}185}186}187188189