Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
41159 views
1
/*
2
* Copyright (c) 2003, 2021, 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
import java.util.Arrays;
35
36
import sun.security.util.*;
37
import sun.security.pkcs.PKCS8Key;
38
39
import sun.security.rsa.RSAUtil.KeyType;
40
41
/**
42
* RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT
43
* form (modulus, private exponent only). For CRT private keys, see
44
* RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior
45
* in instanceof checks, etc.
46
*
47
* Note: RSA keys must be at least 512 bits long
48
*
49
* @see RSAPrivateCrtKeyImpl
50
* @see RSAKeyFactory
51
*
52
* @since 1.5
53
* @author Andreas Sterbenz
54
*/
55
public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
56
57
@java.io.Serial
58
private static final long serialVersionUID = -33106691987952810L;
59
60
private final BigInteger n; // modulus
61
private final BigInteger d; // private exponent
62
63
private transient final KeyType type;
64
65
// optional parameters associated with this RSA key
66
// specified in the encoding of its AlgorithmId.
67
// must be null for "RSA" keys.
68
private transient final AlgorithmParameterSpec keyParams;
69
70
/**
71
* Construct a key from its components. Used by the
72
* RSAKeyFactory and the RSAKeyPairGenerator.
73
*/
74
RSAPrivateKeyImpl(KeyType type, AlgorithmParameterSpec keyParams,
75
BigInteger n, BigInteger d) throws InvalidKeyException {
76
77
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);
78
79
this.n = n;
80
this.d = d;
81
82
try {
83
// validate and generate the algid encoding
84
algid = RSAUtil.createAlgorithmId(type, keyParams);
85
} catch (ProviderException pe) {
86
throw new InvalidKeyException(pe);
87
}
88
89
this.type = type;
90
this.keyParams = keyParams;
91
92
try {
93
// generate the key encoding
94
byte[] nbytes = n.toByteArray();
95
byte[] dbytes = d.toByteArray();
96
DerOutputStream out = new DerOutputStream(
97
nbytes.length + dbytes.length + 50);
98
// Enough for 7 zeroes (21) and 2 tag+length(4)
99
out.putInteger(0); // version must be 0
100
out.putInteger(nbytes);
101
Arrays.fill(nbytes, (byte)0);
102
out.putInteger(0);
103
out.putInteger(dbytes);
104
Arrays.fill(dbytes, (byte)0);
105
out.putInteger(0);
106
out.putInteger(0);
107
out.putInteger(0);
108
out.putInteger(0);
109
out.putInteger(0);
110
DerValue val = DerValue.wrap(DerValue.tag_Sequence, out);
111
key = val.toByteArray();
112
val.clear();
113
} catch (IOException exc) {
114
// should never occur
115
throw new InvalidKeyException(exc);
116
}
117
}
118
119
// see JCA doc
120
@Override
121
public String getAlgorithm() {
122
return type.keyAlgo;
123
}
124
125
// see JCA doc
126
@Override
127
public BigInteger getModulus() {
128
return n;
129
}
130
131
// see JCA doc
132
@Override
133
public BigInteger getPrivateExponent() {
134
return d;
135
}
136
137
// see JCA doc
138
@Override
139
public AlgorithmParameterSpec getParams() {
140
return keyParams;
141
}
142
143
// return a string representation of this key for debugging
144
@Override
145
public String toString() {
146
return "Sun " + type.keyAlgo + " private key, " + n.bitLength()
147
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
148
+ "\n private exponent: " + d;
149
}
150
}
151
152