jdk/src/share/classes/com/sun/crypto/provider/RSACipher.java
author xuelei
Wed, 09 Apr 2014 12:49:51 +0000
changeset 23733 b9b80421cfa7
parent 16909 78a1749a43e2
permissions -rw-r--r--
8028192: Use of PKCS11-NSS provider in FIPS mode broken Reviewed-by: ahgross, ascarpino, asmotrak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.rsa.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.jca.Providers;
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
    42
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
    43
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * RSA cipher implementation. Supports RSA en/decryption and signing/verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * using PKCS#1 v1.5 padding and without padding (raw RSA). Note that raw RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * is supported mostly for completeness and should only be used in rare cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Objects should be instantiated by calling Cipher.getInstance() using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * following algorithm names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  . "RSA/ECB/PKCS1Padding" (or "RSA") for PKCS#1 padding. The mode (blocktype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    is selected based on the en/decryption mode and public/private key used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . "RSA/ECB/NoPadding" for rsa RSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * We only do one RSA operation per doFinal() call. If the application passes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * more data via calls to update() or doFinal(), we throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * IllegalBlockSizeException when doFinal() is called (see JCE API spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Bulk encryption using RSA does not make sense and is not standardized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Note: RSA keys should be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public final class RSACipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // constant for an empty byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static byte[] B0 = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // mode constant for public key encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static int MODE_ENCRYPT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // mode constant for private key decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final static int MODE_DECRYPT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // mode constant for private key encryption (signing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final static int MODE_SIGN    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // mode constant for public key decryption (verifying)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static int MODE_VERIFY  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // constant for raw RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final static String PAD_NONE  = "NoPadding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // constant for PKCS#1 v1.5 RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private final static String PAD_PKCS1 = "PKCS1Padding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // constant for PKCS#2 v2.0 OAEP with MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final static String PAD_OAEP_MGF1  = "OAEP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // current mode, one of MODE_* above. Set when init() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // active padding type, one of PAD_* above. Set by setPadding()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private String paddingType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // padding object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private RSAPadding padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
    96
    // cipher parameter for OAEP padding and TLS RSA premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
    97
    private AlgorithmParameterSpec spec = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // buffer for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private byte[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // offset into the buffer (number of bytes buffered)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // size of the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // the public key, if we were initialized using a public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private RSAPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // the private key, if we were initialized using a private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private RSAPrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // hash algorithm for OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private String oaepHashAlgorithm = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   115
    // the source of randomness
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   116
    private SecureRandom random;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   117
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public RSACipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // modes do not make sense for RSA, but allow ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (mode.equalsIgnoreCase("ECB") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NoSuchAlgorithmException("Unsupported mode " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // set the padding type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void engineSetPadding(String paddingName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (paddingName.equalsIgnoreCase(PAD_NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            paddingType = PAD_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (paddingName.equalsIgnoreCase(PAD_PKCS1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String lowerPadding = paddingName.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (lowerPadding.equals("oaeppadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } else if (lowerPadding.startsWith("oaepwith") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                       lowerPadding.endsWith("andmgf1padding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // "oaepwith".length() == 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // "andmgf1padding".length() == 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                oaepHashAlgorithm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        paddingName.substring(8, paddingName.length() - 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // check if MessageDigest appears to be available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // avoid getInstance() call here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (Providers.getProviderList().getService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        ("MessageDigest", oaepHashAlgorithm) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        ("MessageDigest not available for " + paddingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    ("Padding " + paddingName + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // return 0 as block size, we are not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // return the output size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // no iv, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected AlgorithmParameters engineGetParameters() {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   183
        if (spec != null && spec instanceof OAEPParameterSpec) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                AlgorithmParameters params =
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 10336
diff changeset
   186
                    AlgorithmParameters.getInstance("OAEP",
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 10336
diff changeset
   187
                        SunJCE.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                params.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                throw new RuntimeException("Cannot find OAEP " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    " AlgorithmParameters implementation in SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                throw new RuntimeException("OAEPParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // never thrown when null parameters are used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // but re-throw it just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            InvalidKeyException ike =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                new InvalidKeyException("Wrong parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            ike.initCause(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        init(opmode, key, random, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            AlgorithmParameters params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   233
                OAEPParameterSpec spec =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   234
                        params.getParameterSpec(OAEPParameterSpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                init(opmode, key, random, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                InvalidAlgorithmParameterException iape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    new InvalidAlgorithmParameterException("Wrong parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                iape.initCause(ipse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw iape;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // initialize this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private void init(int opmode, Key key, SecureRandom random,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        switch (opmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        case Cipher.ENCRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        case Cipher.WRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        case Cipher.DECRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        case Cipher.UNWRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new InvalidKeyException("Unknown mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            publicKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else { // must be RSAPrivateKey per check in toRSAKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            privateKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int n = RSACore.getByteLength(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (paddingType == PAD_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            padding = RSAPadding.getInstance(RSAPadding.PAD_NONE, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else if (paddingType == PAD_PKCS1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   284
                if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   285
                    throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   286
                            "Parameters not supported");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   287
                }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   288
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   289
                spec = params;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   290
                this.random = random;   // for TLS RSA premaster secret
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            int blockType = (mode <= MODE_DECRYPT) ? RSAPadding.PAD_BLOCKTYPE_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                                   : RSAPadding.PAD_BLOCKTYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            padding = RSAPadding.getInstance(blockType, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else { // PAD_OAEP_MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if ((mode == MODE_SIGN) || (mode == MODE_VERIFY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        ("OAEP cannot be used to sign or verify signatures");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (!(params instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        ("Wrong Parameters for OAEP Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   311
                spec = params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } else {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   313
                spec = new OAEPParameterSpec(oaepHashAlgorithm, "MGF1",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            padding = RSAPadding.getInstance(RSAPadding.PAD_OAEP_MGF1, n,
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   317
                random, (OAEPParameterSpec)spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // internal update method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private void update(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if ((inLen == 0) || (in == null)) {
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
        if (bufOfs + inLen > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            bufOfs = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // internal doFinal() method. Here we perform the actual RSA operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private byte[] doFinal() throws BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (bufOfs > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                + "than " + buffer.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return RSACore.rsa(data, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                data = RSACore.rsa(verifyBuffer, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return RSACore.rsa(data, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                data = RSACore.rsa(decryptBuffer, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new AssertionError("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    protected int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            int outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    protected int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int outOfs) throws ShortBufferException, BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (outputSize > out.length - outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                ("Need " + outputSize + " bytes for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        byte[] result = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int n = result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        System.arraycopy(result, 0, out, outOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if ((encoded == null) || (encoded.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throw new InvalidKeyException("Could not obtain encoded key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (encoded.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new InvalidKeyException("Key is too long for wrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        update(encoded, 0, encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw new InvalidKeyException("Wrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (wrappedKey.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new InvalidKeyException("Key is too long for unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   432
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   433
        boolean isTlsRsaPremasterSecret =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   434
                algorithm.equals("TlsRsaPremasterSecret");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   435
        Exception failover = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   436
        byte[] encoded = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   437
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        update(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        try {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   440
            encoded = doFinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } catch (BadPaddingException e) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   442
            if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   443
                failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   444
            } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   445
                throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   446
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // should not occur, handled with length check above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   451
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   452
        if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   453
            if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   454
                throw new IllegalStateException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   455
                        "No TlsRsaPremasterSecretParameterSpec specified");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   456
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   457
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   458
            // polish the TLS premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   459
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   460
                ((TlsRsaPremasterSecretParameterSpec)spec).getClientVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   461
                ((TlsRsaPremasterSecretParameterSpec)spec).getServerVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   462
                random, encoded, (failover != null));
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   463
        }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   464
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   465
        return ConstructKeys.constructKey(encoded, algorithm, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return rsaKey.getModulus().bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
}