Path: blob/master/src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
41159 views
/*1* Copyright (c) 2003, 2021, 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.*;33import java.util.Arrays;3435import sun.security.util.*;36import sun.security.pkcs.PKCS8Key;3738import sun.security.rsa.RSAUtil.KeyType;3940/**41* RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT42* form (modulus, private exponent only). For CRT private keys, see43* RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior44* in instanceof checks, etc.45*46* Note: RSA keys must be at least 512 bits long47*48* @see RSAPrivateCrtKeyImpl49* @see RSAKeyFactory50*51* @since 1.552* @author Andreas Sterbenz53*/54public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {5556@java.io.Serial57private static final long serialVersionUID = -33106691987952810L;5859private final BigInteger n; // modulus60private final BigInteger d; // private exponent6162private transient final KeyType type;6364// optional parameters associated with this RSA key65// specified in the encoding of its AlgorithmId.66// must be null for "RSA" keys.67private transient final AlgorithmParameterSpec keyParams;6869/**70* Construct a key from its components. Used by the71* RSAKeyFactory and the RSAKeyPairGenerator.72*/73RSAPrivateKeyImpl(KeyType type, AlgorithmParameterSpec keyParams,74BigInteger n, BigInteger d) throws InvalidKeyException {7576RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);7778this.n = n;79this.d = d;8081try {82// validate and generate the algid encoding83algid = RSAUtil.createAlgorithmId(type, keyParams);84} catch (ProviderException pe) {85throw new InvalidKeyException(pe);86}8788this.type = type;89this.keyParams = keyParams;9091try {92// generate the key encoding93byte[] nbytes = n.toByteArray();94byte[] dbytes = d.toByteArray();95DerOutputStream out = new DerOutputStream(96nbytes.length + dbytes.length + 50);97// Enough for 7 zeroes (21) and 2 tag+length(4)98out.putInteger(0); // version must be 099out.putInteger(nbytes);100Arrays.fill(nbytes, (byte)0);101out.putInteger(0);102out.putInteger(dbytes);103Arrays.fill(dbytes, (byte)0);104out.putInteger(0);105out.putInteger(0);106out.putInteger(0);107out.putInteger(0);108out.putInteger(0);109DerValue val = DerValue.wrap(DerValue.tag_Sequence, out);110key = val.toByteArray();111val.clear();112} catch (IOException exc) {113// should never occur114throw new InvalidKeyException(exc);115}116}117118// see JCA doc119@Override120public String getAlgorithm() {121return type.keyAlgo;122}123124// see JCA doc125@Override126public BigInteger getModulus() {127return n;128}129130// see JCA doc131@Override132public BigInteger getPrivateExponent() {133return d;134}135136// see JCA doc137@Override138public AlgorithmParameterSpec getParams() {139return keyParams;140}141142// return a string representation of this key for debugging143@Override144public String toString() {145return "Sun " + type.keyAlgo + " private key, " + n.bitLength()146+ " bits" + "\n params: " + keyParams + "\n modulus: " + n147+ "\n private exponent: " + d;148}149}150151152