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/RSAPublicKeyImpl.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.*;
33
import java.security.interfaces.*;
34
35
import sun.security.util.*;
36
import sun.security.x509.X509Key;
37
38
import sun.security.rsa.RSAUtil.KeyType;
39
40
/**
41
* RSA public key implementation for "RSA", "RSASSA-PSS" algorithms.
42
*
43
* Note: RSA keys must be at least 512 bits long
44
*
45
* @see RSAPrivateCrtKeyImpl
46
* @see RSAPrivateKeyImpl
47
* @see RSAKeyFactory
48
*
49
* @since 1.5
50
* @author Andreas Sterbenz
51
*/
52
public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
53
54
@java.io.Serial
55
private static final long serialVersionUID = 2644735423591199609L;
56
private static final BigInteger THREE = BigInteger.valueOf(3);
57
58
private BigInteger n; // modulus
59
private BigInteger e; // public exponent
60
61
private transient KeyType type;
62
63
// optional parameters associated with this RSA key
64
// specified in the encoding of its AlgorithmId
65
// must be null for "RSA" keys.
66
private transient AlgorithmParameterSpec keyParams;
67
68
/**
69
* Generate a new RSAPublicKey from the specified type, format, and
70
* encoding.
71
* Also used by SunPKCS11 provider.
72
*/
73
public static RSAPublicKey newKey(KeyType type, String format,
74
byte[] encoded) throws InvalidKeyException {
75
RSAPublicKey key;
76
switch (format) {
77
case "X.509":
78
key = new RSAPublicKeyImpl(encoded);
79
RSAKeyFactory.checkKeyAlgo(key, type.keyAlgo);
80
break;
81
case "PKCS#1":
82
try {
83
BigInteger[] comps = parseASN1(encoded);
84
key = new RSAPublicKeyImpl(type, null, comps[0], comps[1]);
85
} catch (IOException ioe) {
86
throw new InvalidKeyException("Invalid PKCS#1 encoding", ioe);
87
}
88
break;
89
default:
90
throw new InvalidKeyException("Unsupported RSA PublicKey format: " +
91
format);
92
}
93
return key;
94
}
95
96
/**
97
* Generate a new RSAPublicKey from the specified type and components.
98
* Also used by SunPKCS11 provider.
99
*/
100
public static RSAPublicKey newKey(KeyType type,
101
AlgorithmParameterSpec params, BigInteger n, BigInteger e)
102
throws InvalidKeyException {
103
return new RSAPublicKeyImpl(type, params, n, e);
104
}
105
106
/**
107
* Construct a RSA key from the specified type and components. Used by
108
* RSAKeyFactory and RSAKeyPairGenerator.
109
*/
110
RSAPublicKeyImpl(KeyType type, AlgorithmParameterSpec keyParams,
111
BigInteger n, BigInteger e) throws InvalidKeyException {
112
113
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
114
checkExponentRange(n, e);
115
116
this.n = n;
117
this.e = e;
118
119
try {
120
// validate and generate algid encoding
121
algid = RSAUtil.createAlgorithmId(type, keyParams);
122
} catch (ProviderException pe) {
123
throw new InvalidKeyException(pe);
124
}
125
126
this.type = type;
127
this.keyParams = keyParams;
128
129
try {
130
// generate the key encoding
131
DerOutputStream out = new DerOutputStream();
132
out.putInteger(n);
133
out.putInteger(e);
134
byte[] keyArray =
135
new DerValue(DerValue.tag_Sequence,
136
out.toByteArray()).toByteArray();
137
setKey(new BitArray(keyArray.length*8, keyArray));
138
} catch (IOException exc) {
139
// should never occur
140
throw new InvalidKeyException(exc);
141
}
142
}
143
144
/**
145
* Construct a key from its encoding.
146
*/
147
private RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
148
if (encoded == null || encoded.length == 0) {
149
throw new InvalidKeyException("Missing key encoding");
150
}
151
decode(encoded); // this sets n and e value
152
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
153
checkExponentRange(n, e);
154
155
try {
156
// check the validity of oid and params
157
Object[] o = RSAUtil.getTypeAndParamSpec(algid);
158
this.type = (KeyType) o[0];
159
this.keyParams = (AlgorithmParameterSpec) o[1];
160
} catch (ProviderException e) {
161
throw new InvalidKeyException(e);
162
}
163
}
164
165
// pkg private utility method for checking RSA modulus and public exponent
166
static void checkExponentRange(BigInteger mod, BigInteger exp)
167
throws InvalidKeyException {
168
// the exponent should be smaller than the modulus
169
if (exp.compareTo(mod) >= 0) {
170
throw new InvalidKeyException("exponent is larger than modulus");
171
}
172
173
// the exponent should be at least 3
174
if (exp.compareTo(THREE) < 0) {
175
throw new InvalidKeyException("exponent is smaller than 3");
176
}
177
}
178
179
// see JCA doc
180
@Override
181
public String getAlgorithm() {
182
return type.keyAlgo;
183
}
184
185
// see JCA doc
186
@Override
187
public BigInteger getModulus() {
188
return n;
189
}
190
191
// see JCA doc
192
@Override
193
public BigInteger getPublicExponent() {
194
return e;
195
}
196
197
// see JCA doc
198
@Override
199
public AlgorithmParameterSpec getParams() {
200
return keyParams;
201
}
202
203
// utility method for parsing DER encoding of RSA public keys in PKCS#1
204
// format as defined in RFC 8017 Appendix A.1.1, i.e. SEQ of n and e.
205
private static BigInteger[] parseASN1(byte[] raw) throws IOException {
206
DerValue derValue = new DerValue(raw);
207
if (derValue.tag != DerValue.tag_Sequence) {
208
throw new IOException("Not a SEQUENCE");
209
}
210
BigInteger[] result = new BigInteger[2]; // n, e
211
result[0] = derValue.data.getPositiveBigInteger();
212
result[1] = derValue.data.getPositiveBigInteger();
213
if (derValue.data.available() != 0) {
214
throw new IOException("Extra data available");
215
}
216
return result;
217
}
218
219
/**
220
* Parse the key. Called by X509Key.
221
*/
222
protected void parseKeyBits() throws InvalidKeyException {
223
try {
224
BigInteger[] comps = parseASN1(getKey().toByteArray());
225
n = comps[0];
226
e = comps[1];
227
} catch (IOException e) {
228
throw new InvalidKeyException("Invalid RSA public key", e);
229
}
230
}
231
232
// return a string representation of this key for debugging
233
@Override
234
public String toString() {
235
return "Sun " + type.keyAlgo + " public key, " + n.bitLength()
236
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
237
+ "\n public exponent: " + e;
238
}
239
240
@java.io.Serial
241
protected Object writeReplace() throws java.io.ObjectStreamException {
242
return new KeyRep(KeyRep.Type.PUBLIC,
243
getAlgorithm(),
244
getFormat(),
245
getEncoded());
246
}
247
}
248
249