src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java
author darcy
Wed, 09 Oct 2019 09:57:41 -0700
changeset 58519 6e017b301287
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
permissions -rw-r--r--
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes Reviewed-by: mullan, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50918
diff changeset
     2
 * Copyright (c) 2003, 2019, 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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    32
import java.security.spec.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.x509.X509Key;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    37
import sun.security.x509.AlgorithmId;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    38
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    39
import static sun.security.rsa.RSAUtil.KeyType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    42
 * RSA public key implementation for "RSA", "RSASSA-PSS" algorithms.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see RSAPrivateCrtKeyImpl
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    47
 * @see RSAPrivateKeyImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see RSAKeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50918
diff changeset
    55
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final long serialVersionUID = 2644735423591199609L;
48567
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
    57
    private static final BigInteger THREE = BigInteger.valueOf(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private BigInteger n;       // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private BigInteger e;       // public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    62
    // optional parameters associated with this RSA key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    63
    // specified in the encoding of its AlgorithmId
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    64
    // must be null for "RSA" keys.
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
    65
    @SuppressWarnings("serial") // Not statically typed as Serializable
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    66
    private AlgorithmParameterSpec keyParams;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    69
     * Generate a new RSAPublicKey from the specified encoding.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    70
     * Used by SunPKCS11 provider.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    71
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    72
    public static RSAPublicKey newKey(byte[] encoded)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    73
            throws InvalidKeyException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    74
        return new RSAPublicKeyImpl(encoded);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    75
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    76
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    77
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    78
     * Generate a new RSAPublicKey from the specified type and components.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    79
     * Used by SunPKCS11 provider.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    81
    public static RSAPublicKey newKey(KeyType type,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    82
            AlgorithmParameterSpec params, BigInteger n, BigInteger e)
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    83
            throws InvalidKeyException {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    84
        AlgorithmId rsaId = RSAUtil.createAlgorithmId(type, params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    85
        return new RSAPublicKeyImpl(rsaId, n, e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    86
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    87
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    88
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    89
     * Construct a RSA key from AlgorithmId and its components. Used by
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    90
     * RSAKeyFactory and RSAKeyPairGenerator.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    91
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    92
    RSAPublicKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger e)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    93
            throws InvalidKeyException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    94
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    95
        checkExponentRange(n, e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    96
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.e = e;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
    99
        this.keyParams = RSAUtil.getParamSpec(rsaId);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   100
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // generate the encoding
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   102
        algid = rsaId;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            out.putInteger(e);
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   107
            byte[] keyArray =
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   108
                new DerValue(DerValue.tag_Sequence,
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   109
                             out.toByteArray()).toByteArray();
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   110
            setKey(new BitArray(keyArray.length*8, keyArray));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Construct a key from its encoding. Used by RSAKeyFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   120
    RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
50918
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   121
        if (encoded == null || encoded.length == 0) {
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   122
            throw new InvalidKeyException("Missing key encoding");
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   123
        }
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   124
        decode(encoded); // this sets n and e value
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   125
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   126
        checkExponentRange(n, e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   127
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   128
        try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   129
            // this will check the validity of params
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   130
            this.keyParams = RSAUtil.getParamSpec(algid);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   131
        } catch (ProviderException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   132
            throw new InvalidKeyException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   133
        }
48567
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   134
    }
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   135
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   136
    // pkg private utility method for checking RSA modulus and public exponent
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   137
    static void checkExponentRange(BigInteger mod, BigInteger exp)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   138
            throws InvalidKeyException {
48567
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   139
        // the exponent should be smaller than the modulus
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   140
        if (exp.compareTo(mod) >= 0) {
48567
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   141
            throw new InvalidKeyException("exponent is larger than modulus");
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   142
        }
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   143
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   144
        // the exponent should be at least 3
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   145
        if (exp.compareTo(THREE) < 0) {
48567
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   146
            throw new InvalidKeyException("exponent is smaller than 3");
c4de888db380 8174756: Extra validation for public keys
apetcher
parents: 47216
diff changeset
   147
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   151
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public String getAlgorithm() {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   153
        return algid.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   157
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   163
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   168
    // see JCA doc
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   169
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   170
    public AlgorithmParameterSpec getParams() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   171
        return keyParams;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   172
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   173
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Parse the key. Called by X509Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   179
            DerInputStream in = new DerInputStream(getKey().toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            DerInputStream data = derValue.data;
44260
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 25859
diff changeset
   185
            n = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 25859
diff changeset
   186
            e = data.getPositiveBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (derValue.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new IOException("Extra data available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new InvalidKeyException("Invalid RSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // return a string representation of this key for debugging
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   196
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public String toString() {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   198
        return "Sun " + getAlgorithm() + " public key, " + n.bitLength()
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   199
               + " bits" + "\n  params: " + keyParams + "\n  modulus: " + n
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 48567
diff changeset
   200
               + "\n  public exponent: " + e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50918
diff changeset
   203
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return new KeyRep(KeyRep.Type.PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}