src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsPrfGenerator.java
author mbalao
Tue, 15 Jan 2019 19:24:07 -0300
changeset 53340 142b179dd60e
parent 53257 5170dc2bcf64
child 59024 b046ba510bbc
permissions -rw-r--r--
8217088: Disable JDK-6913047 fix (SunPKCS11 memory leak) after JDK-8216597 (SIGBUS error in getNativeKeyInfo) Summary: Disable JDK-6913047 fix (SunPKCS11 memory leak) temporarily until JDK-8216597 (SIGBUS error in getNativeKeyInfo) is fixed. Reviewed-by: mullan
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.internal.spec.TlsPrfParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * KeyGenerator for the TLS PRF. Note that although the PRF is used in a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of places during the handshake, this class is usually only used to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the Finished messages. The reason is that for those other uses more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * PKCS#11 mechanisms have been defined (CKM_SSL3_MASTER_KEY_DERIVE, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>This class supports the CKM_TLS_PRF mechanism from PKCS#11 v2.20 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the older NSS private mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
final class P11TlsPrfGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String MSG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            "TlsPrfGenerator must be initialized using a TlsPrfParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    66
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private TlsPrfParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    P11TlsPrfGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    83
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (params instanceof TlsPrfParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.spec = (TlsPrfParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SecretKey key = spec.getSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            key = NULL_KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            p11Key = P11SecretKeyFactory.convertKey(token, key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidAlgorithmParameterException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
19186
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   101
    // SecretKeySpec does not allow zero length keys, so we define our
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   102
    // own class.
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   103
    //
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   104
    // As an anonymous class cannot make any guarantees about serialization
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   105
    // compatibility, it is nonsensical for an anonymous class to define a
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   106
    // serialVersionUID. Suppress warnings relative to missing serialVersionUID
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   107
    // field in the anonymous subclass of serializable SecretKey.
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   108
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final SecretKey NULL_KEY = new SecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return "Generic";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throw new InvalidParameterException(MSG);
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 SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IllegalStateException("TlsPrfGenerator must be initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   129
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   130
        byte[] seed = spec.getSeed();
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   131
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   132
        // TLS 1.2
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   133
        if (mechanism == CKM_TLS_MAC) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   134
            SecretKey k = null;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   135
            int ulServerOrClient = 0;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   136
            if (spec.getLabel().equals("server finished")) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   137
                ulServerOrClient = 1;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   138
            }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   139
            if (spec.getLabel().equals("client finished")) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   140
                ulServerOrClient = 2;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   141
            }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   142
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   143
            if (ulServerOrClient != 0) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   144
                // Finished message
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   145
                CK_TLS_MAC_PARAMS params = new CK_TLS_MAC_PARAMS(
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   146
                        Functions.getHashMechId(spec.getPRFHashAlg()),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   147
                        spec.getOutputLength(), ulServerOrClient);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   148
                Session session = null;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   149
                long keyID = p11Key.getKeyID();
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   150
                try {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   151
                    session = token.getOpSession();
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   152
                    token.p11.C_SignInit(session.id(),
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   153
                            new CK_MECHANISM(mechanism, params), keyID);
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   154
                    token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   155
                    byte[] out = token.p11.C_SignFinal
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   156
                                        (session.id(), spec.getOutputLength());
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   157
                    return new SecretKeySpec(out, "TlsPrf");
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   158
                } catch (PKCS11Exception e) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   159
                    throw new ProviderException("Could not calculate PRF", e);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   160
                } finally {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   161
                    p11Key.releaseKeyID();
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   162
                    token.releaseSession(session);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   163
                }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   164
            } else {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   165
                throw new ProviderException("Only Finished message authentication code"+
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   166
                                            " generation supported for TLS 1.2.");
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   167
            }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   168
        }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   169
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        byte[] label = P11Util.getBytesUTF8(spec.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (mechanism == CKM_NSS_TLS_PRF_GENERAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Session session = null;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   174
            long keyID = p11Key.getKeyID();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                token.p11.C_SignInit
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   178
                    (session.id(), new CK_MECHANISM(mechanism), keyID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                token.p11.C_SignUpdate(session.id(), 0, label, 0, label.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                byte[] out = token.p11.C_SignFinal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                    (session.id(), spec.getOutputLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } finally {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   187
                p11Key.releaseKeyID();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // mechanism == CKM_TLS_PRF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        byte[] out = new byte[spec.getOutputLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        CK_TLS_PRF_PARAMS params = new CK_TLS_PRF_PARAMS(seed, label, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Session session = null;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   198
        long keyID = p11Key.getKeyID();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            session = token.getOpSession();
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   201
            token.p11.C_DeriveKey(session.id(),
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   202
                new CK_MECHANISM(mechanism, params), keyID, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } finally {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   207
            p11Key.releaseKeyID();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}