jdk/src/java.base/share/classes/com/sun/crypto/provider/RSACipher.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29915 88af03f531f0
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29915
88af03f531f0 8071726: Better RSA optimizations
valeriep
parents: 27804
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    69
    private static final byte[] B0 = new byte[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // mode constant for public key encryption
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    72
    private static final int MODE_ENCRYPT = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // mode constant for private key decryption
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    74
    private static final int MODE_DECRYPT = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // mode constant for private key encryption (signing)
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    76
    private static final int MODE_SIGN    = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // mode constant for public key decryption (verifying)
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    78
    private static final int MODE_VERIFY  = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // constant for raw RSA
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    81
    private static final String PAD_NONE  = "NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // constant for PKCS#1 v1.5 RSA
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    83
    private static final String PAD_PKCS1 = "PKCS1Padding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // constant for PKCS#2 v2.0 OAEP with MGF1
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29915
diff changeset
    85
    private static final String PAD_OAEP_MGF1  = "OAEP";
2
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
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   246
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private void init(int opmode, Key key, SecureRandom random,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        switch (opmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        case Cipher.ENCRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        case Cipher.WRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        case Cipher.DECRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        case Cipher.UNWRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new InvalidKeyException("Unknown mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            publicKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else { // must be RSAPrivateKey per check in toRSAKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            privateKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int n = RSACore.getByteLength(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (paddingType == PAD_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            padding = RSAPadding.getInstance(RSAPadding.PAD_NONE, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } else if (paddingType == PAD_PKCS1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (params != null) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   285
                if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   286
                    throw new InvalidAlgorithmParameterException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   287
                            "Parameters not supported");
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
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   290
                spec = params;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   291
                this.random = random;   // for TLS RSA premaster secret
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            int blockType = (mode <= MODE_DECRYPT) ? RSAPadding.PAD_BLOCKTYPE_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                                   : RSAPadding.PAD_BLOCKTYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            padding = RSAPadding.getInstance(blockType, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else { // PAD_OAEP_MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if ((mode == MODE_SIGN) || (mode == MODE_VERIFY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        ("OAEP cannot be used to sign or verify signatures");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (!(params instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        ("Wrong Parameters for OAEP Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   312
                spec = params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   314
                spec = new OAEPParameterSpec(oaepHashAlgorithm, "MGF1",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            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
   318
                random, (OAEPParameterSpec)spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                buffer = new byte[n];
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // internal update method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private void update(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if ((inLen == 0) || (in == null)) {
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
        if (bufOfs + inLen > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            bufOfs = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    // internal doFinal() method. Here we perform the actual RSA operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private byte[] doFinal() throws BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (bufOfs > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                + "than " + buffer.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                data = padding.pad(buffer, 0, bufOfs);
29915
88af03f531f0 8071726: Better RSA optimizations
valeriep
parents: 27804
diff changeset
   353
                return RSACore.rsa(data, privateKey, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                data = RSACore.rsa(verifyBuffer, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                return RSACore.rsa(data, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
29915
88af03f531f0 8071726: Better RSA optimizations
valeriep
parents: 27804
diff changeset
   363
                data = RSACore.rsa(decryptBuffer, privateKey, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                throw new AssertionError("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    protected int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            int outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    protected int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            int outOfs) throws ShortBufferException, BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (outputSize > out.length - outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                ("Need " + outputSize + " bytes for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        byte[] result = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        int n = result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        System.arraycopy(result, 0, out, outOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if ((encoded == null) || (encoded.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new InvalidKeyException("Could not obtain encoded key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (encoded.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throw new InvalidKeyException("Key is too long for wrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        update(encoded, 0, encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new InvalidKeyException("Wrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // see JCE spec
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   428
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (wrappedKey.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new InvalidKeyException("Key is too long for unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   434
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   435
        boolean isTlsRsaPremasterSecret =
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   436
                algorithm.equals("TlsRsaPremasterSecret");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   437
        Exception failover = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   438
        byte[] encoded = null;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   439
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        update(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        try {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   442
            encoded = doFinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } catch (BadPaddingException e) {
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   444
            if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   445
                failover = e;
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   446
            } else {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   447
                throw new InvalidKeyException("Unwrapping failed", e);
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   448
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            // should not occur, handled with length check above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
23733
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   453
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   454
        if (isTlsRsaPremasterSecret) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   455
            if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   456
                throw new IllegalStateException(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   457
                        "No TlsRsaPremasterSecretParameterSpec specified");
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   458
            }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   459
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   460
            // polish the TLS premaster secret
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   461
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   462
                ((TlsRsaPremasterSecretParameterSpec)spec).getClientVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   463
                ((TlsRsaPremasterSecretParameterSpec)spec).getServerVersion(),
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   464
                random, encoded, (failover != null));
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   465
        }
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   466
b9b80421cfa7 8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents: 16909
diff changeset
   467
        return ConstructKeys.constructKey(encoded, algorithm, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return rsaKey.getModulus().bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}