jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11RSACipher.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 25859 3317bb8137f4
child 27936 ca9ee8e3d527
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22309
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 11521
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 5291
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: 5291
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: 5291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
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
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
    32
import java.util.Locale;
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
    40
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
    41
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * RSA Cipher implementation class. We currently only support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * PKCS#1 v1.5 padding on top of CKM_RSA_PKCS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
final class P11RSACipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // minimum length of PKCS#1 v1.5 padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final static int PKCS1_MIN_PADDING_LENGTH = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // constant byte[] of length 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final static byte[] B0 = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // mode constant for public key encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static int MODE_ENCRYPT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // mode constant for private key decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static int MODE_DECRYPT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // mode constant for private key encryption (signing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static int MODE_SIGN    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // mode constant for public key decryption (verifying)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static int MODE_VERIFY  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    67
    // padding type constant for NoPadding
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    68
    private final static int PAD_NONE = 1;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    69
    // padding type constant for PKCS1Padding
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    70
    private final static int PAD_PKCS1 = 2;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    71
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // algorithm name (always "RSA")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // associated session, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // mode, one of MODE_* above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    87
    // padding, one of PAD_* above
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    88
    private int padType;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
    89
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private byte[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // key, if init() was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // flag indicating whether an operation is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private boolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // maximum input data size allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // for decryption, this is the length of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // for encryption, length of the key minus minimum padding length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private int maxInputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // maximum output size. this is the length of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   107
    // cipher parameter for TLS RSA premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   108
    private AlgorithmParameterSpec spec = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   109
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   110
    // the source of randomness
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   111
    private SecureRandom random;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   112
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    P11RSACipher(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.algorithm = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // modes do not make sense for RSA, but allow ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (mode.equalsIgnoreCase("ECB") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NoSuchAlgorithmException("Unsupported mode " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throws NoSuchPaddingException {
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
   131
        String lowerPadding = padding.toLowerCase(Locale.ENGLISH);
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   132
        if (lowerPadding.equals("pkcs1padding")) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   133
            padType = PAD_PKCS1;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   134
        } else if (lowerPadding.equals("nopadding")) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   135
            padType = PAD_NONE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new NoSuchPaddingException("Unsupported padding " + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // return 0 as block size, we are not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // return the output size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // no IV, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // no parameters, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        implInit(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   176
            if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   177
                throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   178
                        "Parameters not supported");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   179
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   180
            spec = params;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   181
            this.random = random;   // for TLS RSA premaster secret
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        implInit(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected void engineInit(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   191
            throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   192
                        "Parameters not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        implInit(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private void implInit(int opmode, Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        p11Key = P11KeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (opmode == Cipher.ENCRYPT_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else if (opmode == Cipher.DECRYPT_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (opmode == Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (p11Key.isPublic() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                ("Wrap has to be used with public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   210
            // No further setup needed for C_Wrap(). We'll initialize later if
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   211
            // we can't use C_Wrap().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else if (opmode == Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (p11Key.isPrivate() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                ("Unwrap has to be used with private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 290
diff changeset
   218
            // No further setup needed for C_Unwrap(). We'll initialize later
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 290
diff changeset
   219
            // if we can't use C_Unwrap().
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 290
diff changeset
   220
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new InvalidKeyException("Unsupported mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (p11Key.isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else if (p11Key.isPrivate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new InvalidKeyException("Unknown key type: " + p11Key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 8578
diff changeset
   231
        int n = (p11Key.length() + 7) >> 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        buffer = new byte[n];
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   234
        maxInputSize = ((padType == PAD_PKCS1 && encrypt) ?
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   235
                            (n - PKCS1_MIN_PADDING_LENGTH) : n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void cancelOperation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ((session == null) || (token.explicitCancel == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (session.hasObjects() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            session = token.killSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int inLen = maxInputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            int outLen = buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                p11.C_Encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                p11.C_Decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                byte[] tmpBuffer = new byte[maxInputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                p11.C_Sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        (session.id(), tmpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                p11.C_VerifyRecover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new ProviderException("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // XXX ensure this always works, ignore error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private void ensureInitialized() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void initialize() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        CK_MECHANISM ckMechanism = new CK_MECHANISM(mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            p11.C_EncryptInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            p11.C_DecryptInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            p11.C_SignInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            p11.C_VerifyRecoverInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new AssertionError("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private void implUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if ((inLen == 0) || (in == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (bufOfs + inLen > maxInputSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            bufOfs = maxInputSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private int implDoFinal(byte[] out, int outOfs, int outLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (bufOfs > maxInputSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                + "than " + maxInputSize + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                n = p11.C_Encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                n = p11.C_Decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                byte[] tmpBuffer = new byte[bufOfs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                tmpBuffer = p11.C_Sign(session.id(), tmpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (tmpBuffer.length > outLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    throw new BadPaddingException("Output buffer too small");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                n = tmpBuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                n = p11.C_VerifyRecover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw new ProviderException("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw (BadPaddingException)new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                ("doFinal() failed").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            session = token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    protected int engineUpdate(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            byte[] out, int outOfs) throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int n = implDoFinal(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        byte[] out = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        System.arraycopy(buffer, 0, out, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected int engineDoFinal(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            byte[] out, int outOfs) throws ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return implDoFinal(out, outOfs, out.length - outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   413
    private byte[] doFinal() throws BadPaddingException,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   414
            IllegalBlockSizeException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        byte[] t = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int n = implDoFinal(t, 0, t.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        byte[] out = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        System.arraycopy(t, 0, out, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        String keyAlg = key.getAlgorithm();
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   426
        P11Key sKey = null;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   427
        try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   428
            // The conversion may fail, e.g. trying to wrap an AES key on
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   429
            // a token that does not support AES, or when the key size is
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   430
            // not within the range supported by the token.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   431
            sKey = P11SecretKeyFactory.convertKey(token, key, keyAlg);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   432
        } catch (InvalidKeyException ike) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   433
            byte[] toBeWrappedKey = key.getEncoded();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   434
            if (toBeWrappedKey == null) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   435
                throw new InvalidKeyException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   436
                        ("wrap() failed, no encoding available", ike);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   437
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   438
            // Directly encrypt the key encoding when key conversion failed
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   439
            implInit(Cipher.ENCRYPT_MODE, p11Key);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   440
            implUpdate(toBeWrappedKey, 0, toBeWrappedKey.length);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   441
            try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   442
                return doFinal();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   443
            } catch (BadPaddingException bpe) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   444
                // should not occur
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   445
                throw new InvalidKeyException("wrap() failed", bpe);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   446
            } finally {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   447
                // Restore original mode
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   448
                implInit(Cipher.WRAP_MODE, p11Key);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   449
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   450
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        Session s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            s = token.getOpSession();
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   454
            return token.p11.C_WrapKey(s.id(), new CK_MECHANISM(mechanism),
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   455
                p11Key.keyID, sKey.keyID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new InvalidKeyException("wrap() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            token.releaseSession(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
22309
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 11521
diff changeset
   466
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   467
        boolean isTlsRsaPremasterSecret =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   468
                algorithm.equals("TlsRsaPremasterSecret");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   469
        Exception failover = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   470
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   471
        SecureRandom secureRandom = random;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   472
        if (secureRandom == null && isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   473
            secureRandom = new SecureRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   475
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   476
        // Should C_Unwrap be preferred for non-TLS RSA premaster secret?
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   477
        if (token.supportsRawSecretKeyImport()) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   478
            // XXX implement unwrap using C_Unwrap() for all keys
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   479
            implInit(Cipher.DECRYPT_MODE, p11Key);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   480
            if (wrappedKey.length > maxInputSize) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   481
                throw new InvalidKeyException("Key is too long for unwrapping");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   482
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   483
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   484
            byte[] encoded = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   485
            implUpdate(wrappedKey, 0, wrappedKey.length);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   486
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   487
                encoded = doFinal();
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   488
            } catch (BadPaddingException e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   489
                if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   490
                    failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   491
                } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   492
                    throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   493
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   494
            } catch (IllegalBlockSizeException e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   495
                // should not occur, handled with length check above
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   496
                throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   497
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   498
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   499
            if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   500
                if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   501
                    throw new IllegalStateException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   502
                            "No TlsRsaPremasterSecretParameterSpec specified");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   503
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   504
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   505
                // polish the TLS premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   506
                TlsRsaPremasterSecretParameterSpec psps =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   507
                        (TlsRsaPremasterSecretParameterSpec)spec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   508
                encoded = KeyUtil.checkTlsPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   509
                        psps.getClientVersion(), psps.getServerVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   510
                        secureRandom, encoded, (failover != null));
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   511
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   512
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return ConstructKeys.constructKey(encoded, algorithm, type);
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   514
        } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   515
            Session s = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   516
            SecretKey secretKey = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   517
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   518
                try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   519
                    s = token.getObjSession();
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   520
                    long keyType = CKK_GENERIC_SECRET;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   521
                    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   522
                            new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   523
                            new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   524
                        };
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   525
                    attributes = token.getAttributes(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   526
                            O_IMPORT, CKO_SECRET_KEY, keyType, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   527
                    long keyID = token.p11.C_UnwrapKey(s.id(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   528
                            new CK_MECHANISM(mechanism), p11Key.keyID,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   529
                            wrappedKey, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   530
                    secretKey = P11Key.secretKey(s, keyID,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   531
                            algorithm, 48 << 3, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   532
                } catch (PKCS11Exception e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   533
                    if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   534
                        failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   535
                    } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   536
                        throw new InvalidKeyException("unwrap() failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   537
                    }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   538
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   539
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   540
                if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   541
                    byte[] replacer = new byte[48];
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   542
                    if (failover == null) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   543
                        // Does smart compiler dispose this operation?
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   544
                        secureRandom.nextBytes(replacer);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   545
                    }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   546
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   547
                    TlsRsaPremasterSecretParameterSpec psps =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   548
                            (TlsRsaPremasterSecretParameterSpec)spec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   549
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   550
                    // Please use the tricky failover and replacer byte array
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   551
                    // as the parameters so that smart compiler won't dispose
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   552
                    // the unused variable .
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   553
                    secretKey = polishPreMasterSecretKey(token, s,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   554
                            failover, replacer, secretKey,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   555
                            psps.getClientVersion(), psps.getServerVersion());
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   556
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   557
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   558
                return secretKey;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   559
            } finally {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   560
                token.releaseSession(s);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   561
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 8578
diff changeset
   567
        int n = P11KeyFactory.convertKey(token, key, algorithm).length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   570
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   571
    private static SecretKey polishPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   572
            Token token, Session session,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   573
            Exception failover, byte[] replacer, SecretKey secretKey,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   574
            int clientVersion, int serverVersion) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   575
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   576
        if (failover != null) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   577
            CK_VERSION version = new CK_VERSION(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   578
                    (clientVersion >>> 8) & 0xFF, clientVersion & 0xFF);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   579
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   580
                CK_ATTRIBUTE[] attributes = token.getAttributes(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   581
                        O_GENERATE, CKO_SECRET_KEY,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   582
                        CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   583
                long keyID = token.p11.C_GenerateKey(session.id(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   584
                    // new CK_MECHANISM(CKM_TLS_PRE_MASTER_KEY_GEN, version),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   585
                        new CK_MECHANISM(CKM_SSL3_PRE_MASTER_KEY_GEN, version),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   586
                        attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   587
                return P11Key.secretKey(session,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   588
                        keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   589
            } catch (PKCS11Exception e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   590
                throw new ProviderException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   591
                        "Could not generate premaster secret", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   592
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   593
        }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   594
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   595
        return secretKey;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   596
    }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   597
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
final class ConstructKeys {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Construct a public key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param encodedKey the encoding of a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param encodedKeyAlgorithm the algorithm the encodedKey is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @return a public key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private static final PublicKey constructPublicKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            String encodedKeyAlgorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            KeyFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                KeyFactory.getInstance(encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return keyFactory.generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            throw new NoSuchAlgorithmException("No installed providers " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                               "can create keys for the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                               encodedKeyAlgorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                               "algorithm", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        } catch (InvalidKeySpecException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            throw new InvalidKeyException("Cannot construct public key", ike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Construct a private key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param encodedKey the encoding of a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param encodedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @return a private key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private static final PrivateKey constructPrivateKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            String encodedKeyAlgorithm) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            KeyFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                KeyFactory.getInstance(encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return keyFactory.generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            throw new NoSuchAlgorithmException("No installed providers " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                               "can create keys for the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                               encodedKeyAlgorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                               "algorithm", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        } catch (InvalidKeySpecException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            throw new InvalidKeyException("Cannot construct private key", ike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Construct a secret key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @param encodedKey the encoding of a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param encodedKeyAlgorithm the algorithm the secret key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return a secret key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private static final SecretKey constructSecretKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            String encodedKeyAlgorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return new SecretKeySpec(encodedKey, encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    static final Key constructKey(byte[] encoding, String keyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            int keyType) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        switch (keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        case Cipher.SECRET_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return constructSecretKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        case Cipher.PRIVATE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return constructPrivateKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        case Cipher.PUBLIC_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return constructPublicKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            throw new InvalidKeyException("Unknown keytype " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
}