Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/RSAPublicKeyImpl.java
38830 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.rsa;2627import java.io.IOException;28import java.math.BigInteger;2930import java.security.*;31import java.security.spec.*;32import java.security.interfaces.*;3334import sun.security.util.*;35import sun.security.x509.X509Key;36import sun.security.x509.AlgorithmId;3738import static sun.security.rsa.RSAUtil.KeyType;3940/**41* RSA public key implementation for "RSA", "RSASSA-PSS" algorithms.42*43* Note: RSA keys must be at least 512 bits long44*45* @see RSAPrivateCrtKeyImpl46* @see RSAPrivateKeyImpl47* @see RSAKeyFactory48*49* @since 1.550* @author Andreas Sterbenz51*/52public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {5354private static final long serialVersionUID = 2644735423591199609L;55private static final BigInteger THREE = BigInteger.valueOf(3);5657private BigInteger n; // modulus58private BigInteger e; // public exponent5960// optional parameters associated with this RSA key61// specified in the encoding of its AlgorithmId62// must be null for "RSA" keys.63private AlgorithmParameterSpec keyParams;6465/**66* Generate a new RSAPublicKey from the specified encoding.67* Used by SunPKCS11 provider.68*/69public static RSAPublicKey newKey(byte[] encoded)70throws InvalidKeyException {71return new RSAPublicKeyImpl(encoded);72}7374/**75* Generate a new RSAPublicKey from the specified type and components.76* Used by SunPKCS11 provider.77*/78public static RSAPublicKey newKey(KeyType type,79AlgorithmParameterSpec params, BigInteger n, BigInteger e)80throws InvalidKeyException {81AlgorithmId rsaId = RSAUtil.createAlgorithmId(type, params);82return new RSAPublicKeyImpl(rsaId, n, e);83}8485/**86* Construct a RSA key from AlgorithmId and its components. Used by87* RSAKeyFactory and RSAKeyPairGenerator.88*/89RSAPublicKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger e)90throws InvalidKeyException {91RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);92checkExponentRange(n, e);9394this.n = n;95this.e = e;96this.keyParams = RSAUtil.getParamSpec(rsaId);9798// generate the encoding99algid = rsaId;100try {101DerOutputStream out = new DerOutputStream();102out.putInteger(n);103out.putInteger(e);104byte[] keyArray =105new DerValue(DerValue.tag_Sequence,106out.toByteArray()).toByteArray();107setKey(new BitArray(keyArray.length*8, keyArray));108} catch (IOException exc) {109// should never occur110throw new InvalidKeyException(exc);111}112}113114/**115* Construct a key from its encoding. Used by RSAKeyFactory.116*/117RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {118if (encoded == null || encoded.length == 0) {119throw new InvalidKeyException("Missing key encoding");120}121decode(encoded); // this sets n and e value122RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);123checkExponentRange(n, e);124125try {126// this will check the validity of params127this.keyParams = RSAUtil.getParamSpec(algid);128} catch (ProviderException e) {129throw new InvalidKeyException(e);130}131}132133// pkg private utility method for checking RSA modulus and public exponent134static void checkExponentRange(BigInteger mod, BigInteger exp)135throws InvalidKeyException {136// the exponent should be smaller than the modulus137if (exp.compareTo(mod) >= 0) {138throw new InvalidKeyException("exponent is larger than modulus");139}140141// the exponent should be at least 3142if (exp.compareTo(THREE) < 0) {143throw new InvalidKeyException("exponent is smaller than 3");144}145}146147// see JCA doc148@Override149public String getAlgorithm() {150return algid.getName();151}152153// see JCA doc154@Override155public BigInteger getModulus() {156return n;157}158159// see JCA doc160@Override161public BigInteger getPublicExponent() {162return e;163}164165// see JCA doc166@Override167public AlgorithmParameterSpec getParams() {168return keyParams;169}170171/**172* Parse the key. Called by X509Key.173*/174protected void parseKeyBits() throws InvalidKeyException {175try {176DerInputStream in = new DerInputStream(getKey().toByteArray());177DerValue derValue = in.getDerValue();178if (derValue.tag != DerValue.tag_Sequence) {179throw new IOException("Not a SEQUENCE");180}181DerInputStream data = derValue.data;182n = data.getPositiveBigInteger();183e = data.getPositiveBigInteger();184if (derValue.data.available() != 0) {185throw new IOException("Extra data available");186}187} catch (IOException e) {188throw new InvalidKeyException("Invalid RSA public key", e);189}190}191192// return a string representation of this key for debugging193@Override194public String toString() {195return "Sun " + getAlgorithm() + " public key, " + n.bitLength()196+ " bits" + "\n params: " + keyParams + "\n modulus: " + n197+ "\n public exponent: " + e;198}199200protected Object writeReplace() throws java.io.ObjectStreamException {201return new KeyRep(KeyRep.Type.PUBLIC,202getAlgorithm(),203getFormat(),204getEncoded());205}206}207208209