jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11RSACipher.java
author katleman
Thu, 07 May 2015 10:19:33 -0700
changeset 30400 3901aa49fb70
parent 27936 ca9ee8e3d527
child 35287 e59d934ce2ba
permissions -rw-r--r--
Added tag jdk9-b63 for changeset fe75ee04f0d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2014, 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
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
   172
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   177
            if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   178
                throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   179
                        "Parameters not supported");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   180
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   181
            spec = params;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   182
            this.random = random;   // for TLS RSA premaster secret
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        implInit(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected void engineInit(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   192
            throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   193
                        "Parameters not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        implInit(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private void implInit(int opmode, Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        p11Key = P11KeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (opmode == Cipher.ENCRYPT_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else if (opmode == Cipher.DECRYPT_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else if (opmode == Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (p11Key.isPublic() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                ("Wrap has to be used with public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   211
            // 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
   212
            // we can't use C_Wrap().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else if (opmode == Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (p11Key.isPrivate() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                ("Unwrap has to be used with private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
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
   219
            // 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
   220
            // 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
   221
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new InvalidKeyException("Unsupported mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (p11Key.isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (p11Key.isPrivate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new InvalidKeyException("Unknown key type: " + p11Key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 8578
diff changeset
   232
        int n = (p11Key.length() + 7) >> 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        buffer = new byte[n];
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   235
        maxInputSize = ((padType == PAD_PKCS1 && encrypt) ?
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 6122
diff changeset
   236
                            (n - PKCS1_MIN_PADDING_LENGTH) : n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private void cancelOperation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if ((session == null) || (token.explicitCancel == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (session.hasObjects() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            session = token.killSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            int inLen = maxInputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int outLen = buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                p11.C_Encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                p11.C_Decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                byte[] tmpBuffer = new byte[maxInputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                p11.C_Sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        (session.id(), tmpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                p11.C_VerifyRecover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        (session.id(), buffer, 0, inLen, buffer, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                throw new ProviderException("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // XXX ensure this always works, ignore error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private void ensureInitialized() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void initialize() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        CK_MECHANISM ckMechanism = new CK_MECHANISM(mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            p11.C_EncryptInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            p11.C_DecryptInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            p11.C_SignInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            p11.C_VerifyRecoverInit(session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throw new AssertionError("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private void implUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if ((inLen == 0) || (in == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (bufOfs + inLen > maxInputSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            bufOfs = maxInputSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private int implDoFinal(byte[] out, int outOfs, int outLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (bufOfs > maxInputSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                + "than " + maxInputSize + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            PKCS11 p11 = token.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                n = p11.C_Encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                n = p11.C_Decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                byte[] tmpBuffer = new byte[bufOfs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                tmpBuffer = p11.C_Sign(session.id(), tmpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (tmpBuffer.length > outLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    throw new BadPaddingException("Output buffer too small");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                n = tmpBuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                n = p11.C_VerifyRecover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                throw new ProviderException("internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw (BadPaddingException)new BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                ("doFinal() failed").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            session = token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    protected int engineUpdate(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            byte[] out, int outOfs) throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        int n = implDoFinal(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        byte[] out = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        System.arraycopy(buffer, 0, out, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected int engineDoFinal(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            byte[] out, int outOfs) throws ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        implUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return implDoFinal(out, outOfs, out.length - outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   414
    private byte[] doFinal() throws BadPaddingException,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   415
            IllegalBlockSizeException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        byte[] t = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int n = implDoFinal(t, 0, t.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        byte[] out = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        System.arraycopy(t, 0, out, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        String keyAlg = key.getAlgorithm();
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   427
        P11Key sKey = null;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   428
        try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   429
            // 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
   430
            // 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
   431
            // 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
   432
            sKey = P11SecretKeyFactory.convertKey(token, key, keyAlg);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   433
        } catch (InvalidKeyException ike) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   434
            byte[] toBeWrappedKey = key.getEncoded();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   435
            if (toBeWrappedKey == null) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   436
                throw new InvalidKeyException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   437
                        ("wrap() failed, no encoding available", ike);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   438
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   439
            // 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
   440
            implInit(Cipher.ENCRYPT_MODE, p11Key);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   441
            implUpdate(toBeWrappedKey, 0, toBeWrappedKey.length);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   442
            try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   443
                return doFinal();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   444
            } catch (BadPaddingException bpe) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   445
                // should not occur
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   446
                throw new InvalidKeyException("wrap() failed", bpe);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   447
            } finally {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   448
                // Restore original mode
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   449
                implInit(Cipher.WRAP_MODE, p11Key);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   450
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   451
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Session s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            s = token.getOpSession();
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   455
            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
   456
                p11Key.keyID, sKey.keyID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw new InvalidKeyException("wrap() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            token.releaseSession(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    // see JCE spec
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
   465
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
22309
1990211a42e5 8023069: Enhance TLS connections
xuelei
parents: 11521
diff changeset
   468
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   469
        boolean isTlsRsaPremasterSecret =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   470
                algorithm.equals("TlsRsaPremasterSecret");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   471
        Exception failover = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   472
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   473
        SecureRandom secureRandom = random;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   474
        if (secureRandom == null && isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   475
            secureRandom = new SecureRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   477
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   478
        // 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
   479
        if (token.supportsRawSecretKeyImport()) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   480
            // 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
   481
            implInit(Cipher.DECRYPT_MODE, p11Key);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   482
            if (wrappedKey.length > maxInputSize) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   483
                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
   484
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   485
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   486
            byte[] encoded = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   487
            implUpdate(wrappedKey, 0, wrappedKey.length);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   488
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   489
                encoded = doFinal();
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   490
            } catch (BadPaddingException e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   491
                if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   492
                    failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   493
                } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   494
                    throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   495
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   496
            } catch (IllegalBlockSizeException e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   497
                // should not occur, handled with length check above
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   498
                throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   499
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   500
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   501
            if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   502
                if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   503
                    throw new IllegalStateException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   504
                            "No TlsRsaPremasterSecretParameterSpec specified");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   505
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   506
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   507
                // polish the TLS premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   508
                TlsRsaPremasterSecretParameterSpec psps =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   509
                        (TlsRsaPremasterSecretParameterSpec)spec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   510
                encoded = KeyUtil.checkTlsPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   511
                        psps.getClientVersion(), psps.getServerVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   512
                        secureRandom, encoded, (failover != null));
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   513
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   514
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return ConstructKeys.constructKey(encoded, algorithm, type);
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   516
        } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   517
            Session s = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   518
            SecretKey secretKey = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   519
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   520
                try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   521
                    s = token.getObjSession();
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   522
                    long keyType = CKK_GENERIC_SECRET;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   523
                    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   524
                            new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   525
                            new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   526
                        };
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   527
                    attributes = token.getAttributes(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   528
                            O_IMPORT, CKO_SECRET_KEY, keyType, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   529
                    long keyID = token.p11.C_UnwrapKey(s.id(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   530
                            new CK_MECHANISM(mechanism), p11Key.keyID,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   531
                            wrappedKey, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   532
                    secretKey = P11Key.secretKey(s, keyID,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   533
                            algorithm, 48 << 3, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   534
                } catch (PKCS11Exception e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   535
                    if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   536
                        failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   537
                    } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   538
                        throw new InvalidKeyException("unwrap() failed", e);
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
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   541
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   542
                if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   543
                    byte[] replacer = new byte[48];
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   544
                    if (failover == null) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   545
                        // Does smart compiler dispose this operation?
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   546
                        secureRandom.nextBytes(replacer);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   547
                    }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   548
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   549
                    TlsRsaPremasterSecretParameterSpec psps =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   550
                            (TlsRsaPremasterSecretParameterSpec)spec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   551
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   552
                    // 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
   553
                    // 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
   554
                    // the unused variable .
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   555
                    secretKey = polishPreMasterSecretKey(token, s,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   556
                            failover, replacer, secretKey,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   557
                            psps.getClientVersion(), psps.getServerVersion());
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   558
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   559
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   560
                return secretKey;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   561
            } finally {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   562
                token.releaseSession(s);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   563
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    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
   569
        int n = P11KeyFactory.convertKey(token, key, algorithm).length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   572
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   573
    private static SecretKey polishPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   574
            Token token, Session session,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   575
            Exception failover, byte[] replacer, SecretKey secretKey,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   576
            int clientVersion, int serverVersion) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   577
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   578
        if (failover != null) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   579
            CK_VERSION version = new CK_VERSION(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   580
                    (clientVersion >>> 8) & 0xFF, clientVersion & 0xFF);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   581
            try {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   582
                CK_ATTRIBUTE[] attributes = token.getAttributes(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   583
                        O_GENERATE, CKO_SECRET_KEY,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   584
                        CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   585
                long keyID = token.p11.C_GenerateKey(session.id(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   586
                    // 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
   587
                        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
   588
                        attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   589
                return P11Key.secretKey(session,
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   590
                        keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   591
            } catch (PKCS11Exception e) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   592
                throw new ProviderException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   593
                        "Could not generate premaster secret", e);
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
        }
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
        return secretKey;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   598
    }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 22309
diff changeset
   599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
final class ConstructKeys {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Construct a public key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param encodedKey the encoding of a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param encodedKeyAlgorithm the algorithm the encodedKey is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @return a public key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private static final PublicKey constructPublicKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            String encodedKeyAlgorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            KeyFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                KeyFactory.getInstance(encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return keyFactory.generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            throw new NoSuchAlgorithmException("No installed providers " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                               "can create keys for the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                               encodedKeyAlgorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                               "algorithm", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } catch (InvalidKeySpecException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            throw new InvalidKeyException("Cannot construct public key", ike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Construct a private key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param encodedKey the encoding of a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param encodedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return a private key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    private static final PrivateKey constructPrivateKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            String encodedKeyAlgorithm) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            KeyFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                KeyFactory.getInstance(encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            return keyFactory.generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            throw new NoSuchAlgorithmException("No installed providers " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                               "can create keys for the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                               encodedKeyAlgorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                               "algorithm", nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } catch (InvalidKeySpecException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new InvalidKeyException("Cannot construct private key", ike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Construct a secret key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param encodedKey the encoding of a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @param encodedKeyAlgorithm the algorithm the secret key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @return a secret key constructed from the encodedKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private static final SecretKey constructSecretKey(byte[] encodedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            String encodedKeyAlgorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return new SecretKeySpec(encodedKey, encodedKeyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    static final Key constructKey(byte[] encoding, String keyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            int keyType) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        switch (keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        case Cipher.SECRET_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return constructSecretKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        case Cipher.PRIVATE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return constructPrivateKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        case Cipher.PUBLIC_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return constructPublicKey(encoding, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            throw new InvalidKeyException("Unknown keytype " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}