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