0N/A/*
2362N/A * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs11;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Aimport sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
0N/A
0N/Aimport static sun.security.pkcs11.TemplateManager.*;
0N/Aimport sun.security.pkcs11.wrapper.*;
0N/Aimport static sun.security.pkcs11.wrapper.PKCS11Constants.*;
0N/A
0N/A/**
0N/A * KeyGenerator for the SSL/TLS RSA premaster secret.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.6
0N/A */
0N/Afinal class P11TlsRsaPremasterSecretGenerator extends KeyGeneratorSpi {
0N/A
0N/A private final static String MSG = "TlsRsaPremasterSecretGenerator must be "
0N/A + "initialized using a TlsRsaPremasterSecretParameterSpec";
0N/A
0N/A // token instance
0N/A private final Token token;
0N/A
0N/A // algorithm name
0N/A private final String algorithm;
0N/A
0N/A // mechanism id
0N/A private long mechanism;
0N/A
0N/A private TlsRsaPremasterSecretParameterSpec spec;
0N/A
0N/A P11TlsRsaPremasterSecretGenerator(Token token, String algorithm, long mechanism)
0N/A throws PKCS11Exception {
0N/A super();
0N/A this.token = token;
0N/A this.algorithm = algorithm;
0N/A this.mechanism = mechanism;
0N/A }
0N/A
0N/A protected void engineInit(SecureRandom random) {
0N/A throw new InvalidParameterException(MSG);
0N/A }
0N/A
0N/A protected void engineInit(AlgorithmParameterSpec params,
0N/A SecureRandom random) throws InvalidAlgorithmParameterException {
0N/A if (params instanceof TlsRsaPremasterSecretParameterSpec == false) {
0N/A throw new InvalidAlgorithmParameterException(MSG);
0N/A }
0N/A this.spec = (TlsRsaPremasterSecretParameterSpec)params;
0N/A }
0N/A
0N/A protected void engineInit(int keysize, SecureRandom random) {
0N/A throw new InvalidParameterException(MSG);
0N/A }
0N/A
0N/A protected SecretKey engineGenerateKey() {
0N/A if (spec == null) {
0N/A throw new IllegalStateException
0N/A ("TlsRsaPremasterSecretGenerator must be initialized");
0N/A }
0N/A CK_VERSION version =
0N/A new CK_VERSION(spec.getMajorVersion(), spec.getMinorVersion());
0N/A Session session = null;
0N/A try {
0N/A session = token.getObjSession();
0N/A CK_ATTRIBUTE[] attributes = token.getAttributes
0N/A (O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
0N/A long keyID = token.p11.C_GenerateKey
0N/A (session.id(), new CK_MECHANISM(mechanism, version), attributes);
0N/A SecretKey key = P11Key.secretKey
0N/A (session, keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);
0N/A return key;
0N/A } catch (PKCS11Exception e) {
0N/A throw new ProviderException("Could not generate premaster secret", e);
0N/A } finally {
0N/A token.releaseSession(session);
0N/A }
0N/A }
0N/A
0N/A}