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.TlsMasterSecretParameterSpec;
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 master secret.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.6
0N/A */
0N/Apublic final class P11TlsMasterSecretGenerator extends KeyGeneratorSpi {
0N/A
0N/A private final static String MSG = "TlsMasterSecretGenerator must be "
0N/A + "initialized using a TlsMasterSecretParameterSpec";
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 TlsMasterSecretParameterSpec spec;
0N/A private P11Key p11Key;
0N/A
0N/A int version;
0N/A
0N/A P11TlsMasterSecretGenerator(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 TlsMasterSecretParameterSpec == false) {
0N/A throw new InvalidAlgorithmParameterException(MSG);
0N/A }
0N/A this.spec = (TlsMasterSecretParameterSpec)params;
0N/A SecretKey key = spec.getPremasterSecret();
0N/A // algorithm should be either TlsRsaPremasterSecret or TlsPremasterSecret,
0N/A // but we omit the check
0N/A try {
0N/A p11Key = P11SecretKeyFactory.convertKey(token, key, null);
0N/A } catch (InvalidKeyException e) {
0N/A throw new InvalidAlgorithmParameterException("init() failed", e);
0N/A }
0N/A version = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
0N/A if ((version < 0x0300) || (version > 0x0302)) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Only SSL 3.0, TLS 1.0, and TLS 1.1 supported");
0N/A }
0N/A // We assume the token supports the required mechanism. If it does not,
0N/A // generateKey() will fail and the failover should take care of us.
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 ("TlsMasterSecretGenerator must be initialized");
0N/A }
0N/A CK_VERSION ckVersion;
0N/A if (p11Key.getAlgorithm().equals("TlsRsaPremasterSecret")) {
0N/A mechanism = (version == 0x0300) ? CKM_SSL3_MASTER_KEY_DERIVE
0N/A : CKM_TLS_MASTER_KEY_DERIVE;
0N/A ckVersion = new CK_VERSION(0, 0);
0N/A } else {
0N/A // Note: we use DH for all non-RSA premaster secrets. That includes
0N/A // Kerberos. That should not be a problem because master secret
0N/A // calculation is always a straightforward application of the
0N/A // TLS PRF (or the SSL equivalent).
0N/A // The only thing special about RSA master secret calculation is
0N/A // that it extracts the version numbers from the premaster secret.
0N/A mechanism = (version == 0x0300) ? CKM_SSL3_MASTER_KEY_DERIVE_DH
0N/A : CKM_TLS_MASTER_KEY_DERIVE_DH;
0N/A ckVersion = null;
0N/A }
0N/A byte[] clientRandom = spec.getClientRandom();
0N/A byte[] serverRandom = spec.getServerRandom();
0N/A CK_SSL3_RANDOM_DATA random =
0N/A new CK_SSL3_RANDOM_DATA(clientRandom, serverRandom);
0N/A CK_SSL3_MASTER_KEY_DERIVE_PARAMS params =
0N/A new CK_SSL3_MASTER_KEY_DERIVE_PARAMS(random, ckVersion);
0N/A
0N/A Session session = null;
0N/A try {
0N/A session = token.getObjSession();
0N/A CK_ATTRIBUTE[] attributes = token.getAttributes(O_GENERATE,
0N/A CKO_SECRET_KEY, CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
0N/A long keyID = token.p11.C_DeriveKey(session.id(),
0N/A new CK_MECHANISM(mechanism, params), p11Key.keyID, attributes);
0N/A int major, minor;
0N/A ckVersion = params.pVersion;
0N/A if (ckVersion == null) {
0N/A major = -1;
0N/A minor = -1;
0N/A } else {
0N/A major = ckVersion.major;
0N/A minor = ckVersion.minor;
0N/A }
0N/A SecretKey key = P11Key.masterSecretKey(session, keyID,
0N/A "TlsMasterSecret", 48 << 3, attributes, major, minor);
0N/A return key;
0N/A } catch (Exception e) {
0N/A throw new ProviderException("Could not generate key", e);
0N/A } finally {
0N/A token.releaseSession(session);
0N/A }
0N/A }
0N/A
0N/A}