src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44260 jdk/src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java@dd947f766e11
child 48567 c4de888db380
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44260
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2017, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.x509.X509Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Key implementation for RSA public keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see RSAPrivateCrtKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see RSAKeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long serialVersionUID = 2644735423591199609L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private BigInteger n;       // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private BigInteger e;       // public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Construct a key from its components. Used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    59
    public RSAPublicKeyImpl(BigInteger n, BigInteger e)
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    60
            throws InvalidKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.e = e;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    63
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // generate the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        algid = RSAPrivateCrtKeyImpl.rsaId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            out.putInteger(e);
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
    70
            byte[] keyArray =
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
    71
                new DerValue(DerValue.tag_Sequence,
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
    72
                             out.toByteArray()).toByteArray();
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
    73
            setKey(new BitArray(keyArray.length*8, keyArray));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Construct a key from its encoding. Used by RSAKeyFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        decode(encoded);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    85
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Parse the key. Called by X509Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 5506
diff changeset
   108
            DerInputStream in = new DerInputStream(getKey().toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            DerInputStream data = derValue.data;
44260
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 25859
diff changeset
   114
            n = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 25859
diff changeset
   115
            e = data.getPositiveBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (derValue.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new IOException("Extra data available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new InvalidKeyException("Invalid RSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // return a string representation of this key for debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return "Sun RSA public key, " + n.bitLength() + " bits\n  modulus: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                + n + "\n  public exponent: " + e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return new KeyRep(KeyRep.Type.PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}