Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/P11TlsRsaPremasterSecretGenerator.java
38919 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 final static 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;5859private int version;6061private TlsRsaPremasterSecretParameterSpec spec;6263P11TlsRsaPremasterSecretGenerator(Token token, String algorithm, long mechanism)64throws PKCS11Exception {65super();66this.token = token;67this.algorithm = algorithm;68this.mechanism = mechanism;69}7071protected void engineInit(SecureRandom random) {72throw new InvalidParameterException(MSG);73}7475protected void engineInit(AlgorithmParameterSpec params,76SecureRandom random) throws InvalidAlgorithmParameterException {77if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {78throw new InvalidAlgorithmParameterException(MSG);79}80this.spec = (TlsRsaPremasterSecretParameterSpec)params;81version = (spec.getMajorVersion() << 8) | spec.getMinorVersion();82if ((version < 0x0300) && (version > 0x0303)) {83throw new InvalidAlgorithmParameterException84("Only SSL 3.0, TLS 1.0, TLS 1.1, and TLS 1.2 are supported");85}86}8788protected void engineInit(int keysize, SecureRandom random) {89throw new InvalidParameterException(MSG);90}9192// Only can be used in client side to generate TLS RSA premaster secret.93protected SecretKey engineGenerateKey() {94if (spec == null) {95throw new IllegalStateException96("TlsRsaPremasterSecretGenerator must be initialized");97}9899CK_VERSION version = new CK_VERSION(100spec.getMajorVersion(), spec.getMinorVersion());101Session session = null;102try {103session = token.getObjSession();104CK_ATTRIBUTE[] attributes = token.getAttributes(105O_GENERATE, CKO_SECRET_KEY,106CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);107long keyID = token.p11.C_GenerateKey(session.id(),108new CK_MECHANISM(mechanism, version), attributes);109SecretKey key = P11Key.secretKey(session,110keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);111return key;112} catch (PKCS11Exception e) {113throw new ProviderException(114"Could not generate premaster secret", e);115} finally {116token.releaseSession(session);117}118}119120}121122123