src/java.base/share/classes/sun/security/rsa/RSAKeyFactory.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56592 b1902b22005e
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: 2596
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: 2596
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: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    34
import sun.security.action.GetPropertyAction;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    35
import sun.security.x509.AlgorithmId;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    36
import static sun.security.rsa.RSAUtil.KeyType;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    39
 * KeyFactory for RSA keys, e.g. "RSA", "RSASSA-PSS".
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
 * Keys must be instances of PublicKey or PrivateKey
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
 * and getAlgorithm() must return a value which matches the type which are
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
 * specified during construction time of the KeyFactory object.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    43
 * For such keys, it supports conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * between the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * For public keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  . PublicKey with an X.509 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  . RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  . RSAPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  . X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For private keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  . PrivateKey with a PKCS#8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *  . RSAPrivateCrtKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  . RSAPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  . RSAPrivateCrtKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *  . PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * (of course, CRT variants only for CRT keys)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Note: as always, 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
 */
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    66
public class RSAKeyFactory extends KeyFactorySpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    68
    private static final Class<?> RSA_PUB_KEYSPEC_CLS = RSAPublicKeySpec.class;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    69
    private static final Class<?> RSA_PRIV_KEYSPEC_CLS =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    70
            RSAPrivateKeySpec.class;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    71
    private static final Class<?> RSA_PRIVCRT_KEYSPEC_CLS =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    72
            RSAPrivateCrtKeySpec.class;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    73
    private static final Class<?> X509_KEYSPEC_CLS = X509EncodedKeySpec.class;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    74
    private static final Class<?> PKCS8_KEYSPEC_CLS = PKCS8EncodedKeySpec.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    76
    public static final int MIN_MODLEN = 512;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    77
    public static final int MAX_MODLEN = 16384;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    78
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    79
    private final KeyType type;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    80
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    81
    /*
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    82
     * If the modulus length is above this value, restrict the size of
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    83
     * the exponent to something that can be reasonably computed.  We
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    84
     * could simply hardcode the exp len to something like 64 bits, but
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    85
     * this approach allows flexibility in case impls would like to use
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    86
     * larger module and exponent values.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    87
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    88
    public static final int MAX_MODLEN_RESTRICT_EXP = 3072;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    89
    public static final int MAX_RESTRICTED_EXPLEN = 64;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    90
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    91
    private static final boolean restrictExpLen =
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    92
        "true".equalsIgnoreCase(GetPropertyAction.privilegedGetProperty(
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 32649
diff changeset
    93
                "sun.security.rsa.restrictRSAExponent", "true"));
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    94
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
    static RSAKeyFactory getInstance(KeyType type) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
        return new RSAKeyFactory(type);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    97
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    99
    // Internal utility method for checking key algorithm
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
    private static void checkKeyAlgo(Key key, String expectedAlg)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   101
            throws InvalidKeyException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
        String keyAlg = key.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
        if (!(keyAlg.equalsIgnoreCase(expectedAlg))) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
            throw new InvalidKeyException("Expected a " + expectedAlg
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
                    + " key, but got " + keyAlg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   110
     * Static method to convert Key into an instance of RSAPublicKeyImpl
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   111
     * or RSAPrivate(Crt)KeyImpl. If the key is not an RSA key or cannot be
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   112
     * used, throw an InvalidKeyException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Used by RSASignature and RSACipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static RSAKey toRSAKey(Key key) throws InvalidKeyException {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   117
        if ((key instanceof RSAPrivateKeyImpl) ||
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   118
            (key instanceof RSAPrivateCrtKeyImpl) ||
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   119
            (key instanceof RSAPublicKeyImpl)) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   120
            return (RSAKey)key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   122
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   123
                String keyAlgo = key.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   124
                KeyType type = KeyType.lookup(keyAlgo);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   125
                RSAKeyFactory kf = RSAKeyFactory.getInstance(type);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   126
                return (RSAKey) kf.engineTranslateKey(key);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   127
            } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   128
                throw new InvalidKeyException(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   129
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   133
    /*
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   134
     * Single test entry point for all of the mechanisms in the SunRsaSign
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   135
     * provider (RSA*KeyImpls).  All of the tests are the same.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   136
     *
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   137
     * For compatibility, we round up to the nearest byte here:
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   138
     * some Key impls might pass in a value within a byte of the
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   139
     * real value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   141
    static void checkRSAProviderKeyLengths(int modulusLen, BigInteger exponent)
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   142
            throws InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   143
        checkKeyLengths(((modulusLen + 7) & ~7), exponent,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   144
            RSAKeyFactory.MIN_MODLEN, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   148
     * Check the length of an RSA key modulus/exponent to make sure it
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   149
     * is not too short or long.  Some impls have their own min and
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   150
     * max key sizes that may or may not match with a system defined value.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   151
     *
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   152
     * @param modulusLen the bit length of the RSA modulus.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   153
     * @param exponent the RSA exponent
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   154
     * @param minModulusLen if {@literal > 0}, check to see if modulusLen is at
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   155
     *        least this long, otherwise unused.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   156
     * @param maxModulusLen caller will allow this max number of bits.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   157
     *        Allow the smaller of the system-defined maximum and this param.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   158
     *
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   159
     * @throws InvalidKeyException if any of the values are unacceptable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   161
     public static void checkKeyLengths(int modulusLen, BigInteger exponent,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   162
            int minModulusLen, int maxModulusLen) throws InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   163
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   164
        if ((minModulusLen > 0) && (modulusLen < (minModulusLen))) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   165
            throw new InvalidKeyException( "RSA keys must be at least " +
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   166
                minModulusLen + " bits long");
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   167
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   168
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   169
        // Even though our policy file may allow this, we don't want
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   170
        // either value (mod/exp) to be too big.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   171
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   172
        int maxLen = Math.min(maxModulusLen, MAX_MODLEN);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   173
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   174
        // If a RSAPrivateKey/RSAPublicKey, make sure the
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   175
        // modulus len isn't too big.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   176
        if (modulusLen > maxLen) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   177
            throw new InvalidKeyException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   178
                "RSA keys must be no longer than " + maxLen + " bits");
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   179
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   180
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   181
        // If a RSAPublicKey, make sure the exponent isn't too big.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   182
        if (restrictExpLen && (exponent != null) &&
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   183
                (modulusLen > MAX_MODLEN_RESTRICT_EXP) &&
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   184
                (exponent.bitLength() > MAX_RESTRICTED_EXPLEN)) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   185
            throw new InvalidKeyException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   186
                "RSA exponents can be no longer than " +
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   187
                MAX_RESTRICTED_EXPLEN + " bits " +
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   188
                " if modulus is greater than " +
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   189
                MAX_MODLEN_RESTRICT_EXP + " bits");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   193
    // disallowed as KeyType is required
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   194
    private RSAKeyFactory() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   195
        this.type = KeyType.RSA;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   196
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   197
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   198
    public RSAKeyFactory(KeyType type) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   199
        this.type = type;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   200
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Translate an RSA key into a SunRsaSign RSA key. If conversion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * not possible, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * See also JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   211
        // ensure the key algorithm matches the current KeyFactory instance
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   212
        checkKeyAlgo(key, type.keyAlgo());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   213
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   214
        // no translation needed if the key is already our own impl 
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   215
        if ((key instanceof RSAPrivateKeyImpl) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   216
            (key instanceof RSAPrivateCrtKeyImpl) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   217
            (key instanceof RSAPublicKeyImpl)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   218
            return key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return translatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return translatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    // internal implementation of translateKey() for public keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private PublicKey translatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                return new RSAPublicKeyImpl(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   260
                    RSAUtil.createAlgorithmId(type, rsaKey.getParams()),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    rsaKey.getModulus(),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   262
                    rsaKey.getPublicExponent());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   263
            } catch (ProviderException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                // catch providers that incorrectly implement RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new InvalidKeyException("Invalid key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            byte[] encoded = key.getEncoded();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   269
            RSAPublicKey translated = new RSAPublicKeyImpl(encoded);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   270
            // ensure the key algorithm matches the current KeyFactory instance
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   271
            checkKeyAlgo(translated, type.keyAlgo());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   272
            return translated;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new InvalidKeyException("Public keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                + "of RSAPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    // internal implementation of translateKey() for private keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private PrivateKey translatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                return new RSAPrivateCrtKeyImpl(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   286
                    RSAUtil.createAlgorithmId(type, rsaKey.getParams()),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    rsaKey.getPublicExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    rsaKey.getPrivateExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    rsaKey.getPrimeP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    rsaKey.getPrimeQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    rsaKey.getPrimeExponentP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    rsaKey.getPrimeExponentQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    rsaKey.getCrtCoefficient()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                );
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   296
            } catch (ProviderException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                // catch providers that incorrectly implement RSAPrivateCrtKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                throw new InvalidKeyException("Invalid key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else if (key instanceof RSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            RSAPrivateKey rsaKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                return new RSAPrivateKeyImpl(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   304
                    RSAUtil.createAlgorithmId(type, rsaKey.getParams()),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    rsaKey.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                );
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   308
            } catch (ProviderException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // catch providers that incorrectly implement RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                throw new InvalidKeyException("Invalid key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            byte[] encoded = key.getEncoded();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   314
            RSAPrivateKey translated = RSAPrivateCrtKeyImpl.newKey(encoded);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   315
            // ensure the key algorithm matches the current KeyFactory instance
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   316
            checkKeyAlgo(translated, type.keyAlgo());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   317
            return translated;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new InvalidKeyException("Private keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                + "of RSAPrivate(Crt)Key or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    // internal implementation of generatePublic. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private PublicKey generatePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   329
            RSAPublicKey generated = new RSAPublicKeyImpl(x509Spec.getEncoded());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   330
            // ensure the key algorithm matches the current KeyFactory instance
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   331
            checkKeyAlgo(generated, type.keyAlgo());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   332
            return generated;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else if (keySpec instanceof RSAPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            RSAPublicKeySpec rsaSpec = (RSAPublicKeySpec)keySpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   335
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   336
                return new RSAPublicKeyImpl(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   337
                    RSAUtil.createAlgorithmId(type, rsaSpec.getParams()),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   338
                    rsaSpec.getModulus(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   339
                    rsaSpec.getPublicExponent()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   340
                );
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   341
            } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   342
                throw new InvalidKeySpecException(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   343
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new InvalidKeySpecException("Only RSAPublicKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                + "and X509EncodedKeySpec supported for RSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    // internal implementation of generatePrivate. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private PrivateKey generatePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   355
            RSAPrivateKey generated = RSAPrivateCrtKeyImpl.newKey(pkcsSpec.getEncoded());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   356
            // ensure the key algorithm matches the current KeyFactory instance
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   357
            checkKeyAlgo(generated, type.keyAlgo());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   358
            return generated;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else if (keySpec instanceof RSAPrivateCrtKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            RSAPrivateCrtKeySpec rsaSpec = (RSAPrivateCrtKeySpec)keySpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   361
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   362
                return new RSAPrivateCrtKeyImpl(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   363
                    RSAUtil.createAlgorithmId(type, rsaSpec.getParams()),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   364
                    rsaSpec.getModulus(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   365
                    rsaSpec.getPublicExponent(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   366
                    rsaSpec.getPrivateExponent(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   367
                    rsaSpec.getPrimeP(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   368
                    rsaSpec.getPrimeQ(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   369
                    rsaSpec.getPrimeExponentP(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   370
                    rsaSpec.getPrimeExponentQ(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   371
                    rsaSpec.getCrtCoefficient()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   372
                );
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   373
            } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   374
                throw new InvalidKeySpecException(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   375
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else if (keySpec instanceof RSAPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            RSAPrivateKeySpec rsaSpec = (RSAPrivateKeySpec)keySpec;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   378
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   379
                return new RSAPrivateKeyImpl(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   380
                    RSAUtil.createAlgorithmId(type, rsaSpec.getParams()),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   381
                    rsaSpec.getModulus(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   382
                    rsaSpec.getPrivateExponent()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   383
                );
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   384
            } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   385
                throw new InvalidKeySpecException(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   386
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                + "and PKCS8EncodedKeySpec supported for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // convert key to one of our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // this also verifies that the key is a valid RSA key and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // that the encoding is X.509/PKCS#8 for public/private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            key = engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            RSAPublicKey rsaKey = (RSAPublicKey)key;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   405
            if (RSA_PUB_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   406
                return keySpec.cast(new RSAPublicKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    rsaKey.getModulus(),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   408
                    rsaKey.getPublicExponent(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   409
                    rsaKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   410
                ));
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   411
            } else if (X509_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   412
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        ("KeySpec must be RSAPublicKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        + "X509EncodedKeySpec for RSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } else if (key instanceof RSAPrivateKey) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   419
            if (PKCS8_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   420
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   421
            } else if (RSA_PRIVCRT_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey)key;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   424
                    return keySpec.cast(new RSAPrivateCrtKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        crtKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        crtKey.getPublicExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        crtKey.getPrivateExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        crtKey.getPrimeP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        crtKey.getPrimeQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        crtKey.getPrimeExponentP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        crtKey.getPrimeExponentQ(),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   432
                        crtKey.getCrtCoefficient(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   433
                        crtKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   434
                    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    ("RSAPrivateCrtKeySpec can only be used with CRT keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   439
            } else if (RSA_PRIV_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                RSAPrivateKey rsaKey = (RSAPrivateKey)key;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   441
                return keySpec.cast(new RSAPrivateKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    rsaKey.getModulus(),
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   443
                    rsaKey.getPrivateExponent(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   444
                    rsaKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   445
                ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        ("KeySpec must be RSAPrivate(Crt)KeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        + "PKCS8EncodedKeySpec for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // should not occur, caught in engineTranslateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new InvalidKeySpecException("Neither public nor private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   456
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   457
    public static final class Legacy extends RSAKeyFactory {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   458
        public Legacy() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   459
            super(KeyType.RSA);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   460
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   461
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   462
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   463
    public static final class PSS extends RSAKeyFactory {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   464
        public PSS() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   465
            super(KeyType.PSS);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   466
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   467
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
}