Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/RSAPrivateKeyImpl.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.AlgorithmParameterSpec;32import java.security.interfaces.*;3334import sun.security.util.*;35import sun.security.x509.AlgorithmId;36import sun.security.pkcs.PKCS8Key;3738/**39* RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT40* form (modulus, private exponent only). For CRT private keys, see41* RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior42* in instanceof checks, etc.43*44* Note: RSA keys must be at least 512 bits long45*46* @see RSAPrivateCrtKeyImpl47* @see RSAKeyFactory48*49* @since 1.550* @author Andreas Sterbenz51*/52public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {5354private static final long serialVersionUID = -33106691987952810L;5556private final BigInteger n; // modulus57private final BigInteger d; // private exponent5859// optional parameters associated with this RSA key60// specified in the encoding of its AlgorithmId.61// must be null for "RSA" keys.62private final AlgorithmParameterSpec keyParams;6364/**65* Construct a key from its components. Used by the66* RSAKeyFactory and the RSAKeyPairGenerator.67*/68RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d)69throws InvalidKeyException {70RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);7172this.n = n;73this.d = d;74this.keyParams = RSAUtil.getParamSpec(rsaId);7576// generate the encoding77algid = rsaId;78try {79DerOutputStream out = new DerOutputStream();80out.putInteger(0); // version must be 081out.putInteger(n);82out.putInteger(0);83out.putInteger(d);84out.putInteger(0);85out.putInteger(0);86out.putInteger(0);87out.putInteger(0);88out.putInteger(0);89DerValue val =90new DerValue(DerValue.tag_Sequence, out.toByteArray());91key = val.toByteArray();92} catch (IOException exc) {93// should never occur94throw new InvalidKeyException(exc);95}96}9798// see JCA doc99@Override100public String getAlgorithm() {101return algid.getName();102}103104// see JCA doc105@Override106public BigInteger getModulus() {107return n;108}109110// see JCA doc111@Override112public BigInteger getPrivateExponent() {113return d;114}115116// see JCA doc117@Override118public AlgorithmParameterSpec getParams() {119return keyParams;120}121122// return a string representation of this key for debugging123@Override124public String toString() {125return "Sun " + getAlgorithm() + " private key, " + n.bitLength()126+ " bits" + "\n params: " + keyParams + "\n modulus: " + n127+ "\n private exponent: " + d;128}129}130131132