Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
38830 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.rsa;
27
28
import java.io.IOException;
29
import java.math.BigInteger;
30
31
import java.security.*;
32
import java.security.spec.AlgorithmParameterSpec;
33
import java.security.interfaces.*;
34
35
import sun.security.util.*;
36
import sun.security.x509.AlgorithmId;
37
import sun.security.pkcs.PKCS8Key;
38
39
/**
40
* RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT
41
* form (modulus, private exponent only). For CRT private keys, see
42
* RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior
43
* in instanceof checks, etc.
44
*
45
* Note: RSA keys must be at least 512 bits long
46
*
47
* @see RSAPrivateCrtKeyImpl
48
* @see RSAKeyFactory
49
*
50
* @since 1.5
51
* @author Andreas Sterbenz
52
*/
53
public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
54
55
private static final long serialVersionUID = -33106691987952810L;
56
57
private final BigInteger n; // modulus
58
private final BigInteger d; // private exponent
59
60
// optional parameters associated with this RSA key
61
// specified in the encoding of its AlgorithmId.
62
// must be null for "RSA" keys.
63
private final AlgorithmParameterSpec keyParams;
64
65
/**
66
* Construct a key from its components. Used by the
67
* RSAKeyFactory and the RSAKeyPairGenerator.
68
*/
69
RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d)
70
throws InvalidKeyException {
71
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);
72
73
this.n = n;
74
this.d = d;
75
this.keyParams = RSAUtil.getParamSpec(rsaId);
76
77
// generate the encoding
78
algid = rsaId;
79
try {
80
DerOutputStream out = new DerOutputStream();
81
out.putInteger(0); // version must be 0
82
out.putInteger(n);
83
out.putInteger(0);
84
out.putInteger(d);
85
out.putInteger(0);
86
out.putInteger(0);
87
out.putInteger(0);
88
out.putInteger(0);
89
out.putInteger(0);
90
DerValue val =
91
new DerValue(DerValue.tag_Sequence, out.toByteArray());
92
key = val.toByteArray();
93
} catch (IOException exc) {
94
// should never occur
95
throw new InvalidKeyException(exc);
96
}
97
}
98
99
// see JCA doc
100
@Override
101
public String getAlgorithm() {
102
return algid.getName();
103
}
104
105
// see JCA doc
106
@Override
107
public BigInteger getModulus() {
108
return n;
109
}
110
111
// see JCA doc
112
@Override
113
public BigInteger getPrivateExponent() {
114
return d;
115
}
116
117
// see JCA doc
118
@Override
119
public AlgorithmParameterSpec getParams() {
120
return keyParams;
121
}
122
123
// return a string representation of this key for debugging
124
@Override
125
public String toString() {
126
return "Sun " + getAlgorithm() + " private key, " + n.bitLength()
127
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
128
+ "\n private exponent: " + d;
129
}
130
}
131
132