src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsKeyMaterialGenerator.java
author mbalao
Wed, 12 Sep 2018 13:09:51 +0200
changeset 51800 bccd9966f1ed
parent 47216 71c04702a3d5
child 53257 5170dc2bcf64
permissions -rw-r--r--
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider Summary: TLS v1.2 algorithms for key and MAC derivation added to SunPKCS11 crypto provider. Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.internal.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.internal.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.pkcs11.wrapper.*;
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * KeyGenerator to calculate the SSL/TLS key material (cipher keys and ivs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * mac keys) from the master secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class P11TlsKeyMaterialGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final static String MSG = "TlsKeyMaterialGenerator must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        + "initialized using a TlsKeyMaterialParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
    65
    private int tlsVersion;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // parameter spec
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    68
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private TlsKeyMaterialParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // master secret as a P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    74
    // whether SSLv3 is supported
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    75
    private final boolean supportSSLv3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    P11TlsKeyMaterialGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.mechanism = mechanism;
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    83
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    84
        // Given the current lookup order specified in SunPKCS11.java,
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    85
        // if CKM_SSL3_KEY_AND_MAC_DERIVE is not used to construct this object,
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    86
        // it means that this mech is disabled or unsupported.
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
    87
        this.supportSSLv3 = (mechanism == CKM_SSL3_KEY_AND_MAC_DERIVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    94
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (params instanceof TlsKeyMaterialParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   100
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   101
        TlsKeyMaterialParameterSpec spec = (TlsKeyMaterialParameterSpec)params;
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   102
        tlsVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   103
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   104
        if ((tlsVersion == 0x0300 && !supportSSLv3) ||
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   105
                (tlsVersion < 0x0300) || (tlsVersion > 0x0303)) {
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   106
             throw new InvalidAlgorithmParameterException
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   107
                    ("Only" + (supportSSLv3? " SSL 3.0,": "") +
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   108
                     " TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" +
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   109
                     tlsVersion + ")");
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   110
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            p11Key = P11SecretKeyFactory.convertKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            (token, spec.getMasterSecret(), "TlsMasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new InvalidAlgorithmParameterException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
41123
66eaead2a150 8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents: 27936
diff changeset
   117
        this.spec = spec;
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   118
        if (tlsVersion == 0x0300) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   119
            mechanism = CKM_SSL3_KEY_AND_MAC_DERIVE;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   120
        } else if (tlsVersion == 0x0301 || tlsVersion == 0x0302) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   121
            mechanism = CKM_TLS_KEY_AND_MAC_DERIVE;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   122
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
   129
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                ("TlsKeyMaterialGenerator must be initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int macBits = spec.getMacKeyLength() << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int ivBits = spec.getIvLength() << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int expandedKeyBits = spec.getExpandedCipherKeyLength() << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int keyBits = spec.getCipherKeyLength() << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        boolean isExportable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (expandedKeyBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            isExportable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            isExportable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            expandedKeyBits = keyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        CK_SSL3_RANDOM_DATA random = new CK_SSL3_RANDOM_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                            (spec.getClientRandom(), spec.getServerRandom());
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   150
        Object params = null;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   151
        CK_MECHANISM ckMechanism = null;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   152
        if (tlsVersion < 0x0303) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   153
            params = new CK_SSL3_KEY_MAT_PARAMS
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   154
                    (macBits, keyBits, ivBits, isExportable, random);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   155
            ckMechanism = new CK_MECHANISM(mechanism, (CK_SSL3_KEY_MAT_PARAMS)params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   156
        } else if (tlsVersion == 0x0303) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   157
            params = new CK_TLS12_KEY_MAT_PARAMS
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   158
                    (macBits, keyBits, ivBits, isExportable, random,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   159
                    Functions.getHashMechId(spec.getPRFHashAlg()));
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   160
            ckMechanism = new CK_MECHANISM(mechanism, (CK_TLS12_KEY_MAT_PARAMS)params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   161
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String cipherAlgorithm = spec.getCipherAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        long keyType = P11SecretKeyFactory.getKeyType(cipherAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (keyType < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (keyBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            ("Unknown algorithm: " + spec.getCipherAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // NULL encryption ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                keyType = CKK_GENERIC_SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            CK_ATTRIBUTE[] attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (keyBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    new CK_ATTRIBUTE(CKA_VALUE_LEN, expandedKeyBits >> 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // ciphersuites with NULL ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                attributes = new CK_ATTRIBUTE[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // the returned keyID is a dummy, ignore
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   192
            token.p11.C_DeriveKey(session.id(),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   193
                ckMechanism, p11Key.keyID, attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   195
            CK_SSL3_KEY_MAT_OUT out = null;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   196
            if (params instanceof CK_SSL3_KEY_MAT_PARAMS) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   197
                out = ((CK_SSL3_KEY_MAT_PARAMS)params).pReturnedKeyMaterial;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   198
            } else if (params instanceof CK_TLS12_KEY_MAT_PARAMS) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   199
                out = ((CK_TLS12_KEY_MAT_PARAMS)params).pReturnedKeyMaterial;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   200
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // Note that the MAC keys do not inherit all attributes from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // template, but they do inherit the sensitive/extractable/token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // flags, which is all P11Key cares about.
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   204
            SecretKey clientMacKey, serverMacKey;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   205
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   206
            // The MAC size may be zero for GCM mode.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   207
            //
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   208
            // PKCS11 does not support GCM mode as the author made the comment,
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   209
            // so the macBits is unlikely to be zero. It's only a place holder.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   210
            if (macBits != 0) {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   211
                clientMacKey = P11Key.secretKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    (session, out.hClientMacSecret, "MAC", macBits, attributes);
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   213
                serverMacKey = P11Key.secretKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    (session, out.hServerMacSecret, "MAC", macBits, attributes);
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   215
            } else {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   216
                clientMacKey = null;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   217
                serverMacKey = null;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   218
            }
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   219
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            SecretKey clientCipherKey, serverCipherKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (keyBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                clientCipherKey = P11Key.secretKey(session, out.hClientKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        cipherAlgorithm, expandedKeyBits, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                serverCipherKey = P11Key.secretKey(session, out.hServerKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        cipherAlgorithm, expandedKeyBits, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                clientCipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                serverCipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            IvParameterSpec clientIv = (out.pIVClient == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                    ? null : new IvParameterSpec(out.pIVClient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            IvParameterSpec serverIv = (out.pIVServer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                    ? null : new IvParameterSpec(out.pIVServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return new TlsKeyMaterialSpec(clientMacKey, serverMacKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    clientCipherKey, clientIv, serverCipherKey, serverIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new ProviderException("Could not generate key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}