Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsRsaPremasterSecretGenerator.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 javax.crypto.spec.*;3233import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;3435import static sun.security.pkcs11.TemplateManager.*;36import sun.security.pkcs11.wrapper.*;37import static sun.security.pkcs11.wrapper.PKCS11Constants.*;3839/**40* KeyGenerator for the SSL/TLS RSA premaster secret.41*42* @author Andreas Sterbenz43* @since 1.644*/45final class P11TlsRsaPremasterSecretGenerator extends KeyGeneratorSpi {4647private static final String MSG = "TlsRsaPremasterSecretGenerator must be "48+ "initialized using a TlsRsaPremasterSecretParameterSpec";4950// token instance51private final Token token;5253// algorithm name54private final String algorithm;5556// mechanism id57private long mechanism;5859@SuppressWarnings("deprecation")60private TlsRsaPremasterSecretParameterSpec spec;6162// whether SSLv3 is supported63private final boolean supportSSLv3;6465P11TlsRsaPremasterSecretGenerator(Token token, String algorithm, long mechanism)66throws PKCS11Exception {67super();68this.token = token;69this.algorithm = algorithm;70this.mechanism = mechanism;7172// Given the current lookup order specified in SunPKCS11.java,73// if CKM_SSL3_PRE_MASTER_KEY_GEN is not used to construct this object,74// it means that this mech is disabled or unsupported.75this.supportSSLv3 = (mechanism == CKM_SSL3_PRE_MASTER_KEY_GEN);76}7778protected void engineInit(SecureRandom random) {79throw new InvalidParameterException(MSG);80}8182@SuppressWarnings("deprecation")83protected void engineInit(AlgorithmParameterSpec params,84SecureRandom random) throws InvalidAlgorithmParameterException {85if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {86throw new InvalidAlgorithmParameterException(MSG);87}8889TlsRsaPremasterSecretParameterSpec spec =90(TlsRsaPremasterSecretParameterSpec) params;91int tlsVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();9293if ((tlsVersion == 0x0300 && !supportSSLv3) ||94(tlsVersion < 0x0300) || (tlsVersion > 0x0303)) {95throw new InvalidAlgorithmParameterException96("Only" + (supportSSLv3? " SSL 3.0,": "") +97" TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" +98tlsVersion + ")");99}100this.spec = spec;101}102103protected void engineInit(int keysize, SecureRandom random) {104throw new InvalidParameterException(MSG);105}106107// Only can be used in client side to generate TLS RSA premaster secret.108protected SecretKey engineGenerateKey() {109if (spec == null) {110throw new IllegalStateException111("TlsRsaPremasterSecretGenerator must be initialized");112}113114CK_VERSION version = new CK_VERSION(115spec.getMajorVersion(), spec.getMinorVersion());116Session session = null;117try {118session = token.getObjSession();119CK_ATTRIBUTE[] attributes = token.getAttributes(120O_GENERATE, CKO_SECRET_KEY,121CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);122long keyID = token.p11.C_GenerateKey(session.id(),123new CK_MECHANISM(mechanism, version), attributes);124SecretKey key = P11Key.secretKey(session,125keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);126return key;127} catch (PKCS11Exception e) {128throw new ProviderException(129"Could not generate premaster secret", e);130} finally {131token.releaseSession(session);132}133}134135}136137138