Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSAKeyFactory.java
41154 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.pkcs11;
27
28
import java.math.BigInteger;
29
30
import java.security.*;
31
import java.security.interfaces.*;
32
import java.security.spec.*;
33
34
import sun.security.rsa.RSAPublicKeyImpl;
35
import sun.security.rsa.RSAPrivateCrtKeyImpl;
36
import sun.security.rsa.RSAUtil.KeyType;
37
import static sun.security.pkcs11.TemplateManager.*;
38
import sun.security.pkcs11.wrapper.*;
39
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
40
41
import sun.security.rsa.RSAKeyFactory;
42
43
/**
44
* RSA KeyFactory implementation.
45
*
46
* @author Andreas Sterbenz
47
* @since 1.5
48
*/
49
final class P11RSAKeyFactory extends P11KeyFactory {
50
51
P11RSAKeyFactory(Token token, String algorithm) {
52
super(token, algorithm);
53
}
54
55
PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
56
try {
57
if (key instanceof RSAPublicKey) {
58
RSAPublicKey rsaKey = (RSAPublicKey)key;
59
return generatePublic(
60
rsaKey.getModulus(),
61
rsaKey.getPublicExponent()
62
);
63
} else {
64
// let SunRsaSign provider parse for us, then recurse
65
key = RSAPublicKeyImpl.newKey(KeyType.RSA, key.getFormat(),
66
key.getEncoded());
67
return implTranslatePublicKey(key);
68
}
69
} catch (PKCS11Exception e) {
70
throw new InvalidKeyException("Could not create RSA public key", e);
71
}
72
}
73
74
PrivateKey implTranslatePrivateKey(PrivateKey key)
75
throws InvalidKeyException {
76
try {
77
if (key instanceof RSAPrivateCrtKey) {
78
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
79
return generatePrivate(
80
rsaKey.getModulus(),
81
rsaKey.getPublicExponent(),
82
rsaKey.getPrivateExponent(),
83
rsaKey.getPrimeP(),
84
rsaKey.getPrimeQ(),
85
rsaKey.getPrimeExponentP(),
86
rsaKey.getPrimeExponentQ(),
87
rsaKey.getCrtCoefficient()
88
);
89
} else if (key instanceof RSAPrivateKey) {
90
RSAPrivateKey rsaKey = (RSAPrivateKey)key;
91
return generatePrivate(
92
rsaKey.getModulus(),
93
rsaKey.getPrivateExponent()
94
);
95
} else {
96
// let SunRsaSign provider parse for us, then recurse
97
key = RSAPrivateCrtKeyImpl.newKey(KeyType.RSA, key.getFormat(),
98
key.getEncoded());
99
return implTranslatePrivateKey(key);
100
}
101
} catch (PKCS11Exception e) {
102
throw new InvalidKeyException("Could not create RSA private key", e);
103
}
104
}
105
106
// see JCA spec
107
protected PublicKey engineGeneratePublic(KeySpec keySpec)
108
throws InvalidKeySpecException {
109
token.ensureValid();
110
if (keySpec instanceof X509EncodedKeySpec) {
111
try {
112
PublicKey key = RSAPublicKeyImpl.newKey(KeyType.RSA, "X.509",
113
((X509EncodedKeySpec)keySpec).getEncoded());
114
return implTranslatePublicKey(key);
115
} catch (InvalidKeyException e) {
116
throw new InvalidKeySpecException
117
("Could not create RSA public key", e);
118
}
119
}
120
if (keySpec instanceof RSAPublicKeySpec == false) {
121
throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
122
+ "X509EncodedKeySpec supported for RSA public keys");
123
}
124
try {
125
RSAPublicKeySpec rs = (RSAPublicKeySpec)keySpec;
126
return generatePublic(
127
rs.getModulus(),
128
rs.getPublicExponent()
129
);
130
} catch (PKCS11Exception | InvalidKeyException e) {
131
throw new InvalidKeySpecException
132
("Could not create RSA public key", e);
133
}
134
}
135
136
// see JCA spec
137
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
138
throws InvalidKeySpecException {
139
token.ensureValid();
140
if (keySpec instanceof PKCS8EncodedKeySpec) {
141
try {
142
PrivateKey key = RSAPrivateCrtKeyImpl.newKey(KeyType.RSA,
143
"PKCS#8", ((PKCS8EncodedKeySpec)keySpec).getEncoded());
144
return implTranslatePrivateKey(key);
145
} catch (GeneralSecurityException e) {
146
throw new InvalidKeySpecException
147
("Could not create RSA private key", e);
148
}
149
}
150
try {
151
if (keySpec instanceof RSAPrivateCrtKeySpec) {
152
RSAPrivateCrtKeySpec rs = (RSAPrivateCrtKeySpec)keySpec;
153
return generatePrivate(
154
rs.getModulus(),
155
rs.getPublicExponent(),
156
rs.getPrivateExponent(),
157
rs.getPrimeP(),
158
rs.getPrimeQ(),
159
rs.getPrimeExponentP(),
160
rs.getPrimeExponentQ(),
161
rs.getCrtCoefficient()
162
);
163
} else if (keySpec instanceof RSAPrivateKeySpec) {
164
RSAPrivateKeySpec rs = (RSAPrivateKeySpec)keySpec;
165
return generatePrivate(
166
rs.getModulus(),
167
rs.getPrivateExponent()
168
);
169
} else {
170
throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
171
+ "and PKCS8EncodedKeySpec supported for RSA private keys");
172
}
173
} catch (PKCS11Exception | InvalidKeyException e) {
174
throw new InvalidKeySpecException
175
("Could not create RSA private key", e);
176
}
177
}
178
179
private PublicKey generatePublic(BigInteger n, BigInteger e)
180
throws PKCS11Exception, InvalidKeyException {
181
RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
182
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
183
new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
184
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
185
new CK_ATTRIBUTE(CKA_MODULUS, n),
186
new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
187
};
188
attributes = token.getAttributes
189
(O_IMPORT, CKO_PUBLIC_KEY, CKK_RSA, attributes);
190
Session session = null;
191
try {
192
session = token.getObjSession();
193
long keyID = token.p11.C_CreateObject(session.id(), attributes);
194
return P11Key.publicKey
195
(session, keyID, "RSA", n.bitLength(), attributes);
196
} finally {
197
token.releaseSession(session);
198
}
199
}
200
201
private PrivateKey generatePrivate(BigInteger n, BigInteger d)
202
throws PKCS11Exception, InvalidKeyException {
203
RSAKeyFactory.checkKeyLengths(n.bitLength(), null, -1, 64 * 1024);
204
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
205
new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
206
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
207
new CK_ATTRIBUTE(CKA_MODULUS, n),
208
new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
209
};
210
attributes = token.getAttributes
211
(O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
212
Session session = null;
213
try {
214
session = token.getObjSession();
215
long keyID = token.p11.C_CreateObject(session.id(), attributes);
216
return P11Key.privateKey
217
(session, keyID, "RSA", n.bitLength(), attributes);
218
} finally {
219
token.releaseSession(session);
220
}
221
}
222
223
private PrivateKey generatePrivate(BigInteger n, BigInteger e,
224
BigInteger d, BigInteger p, BigInteger q, BigInteger pe,
225
BigInteger qe, BigInteger coeff) throws PKCS11Exception,
226
InvalidKeyException {
227
RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
228
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
229
new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
230
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
231
new CK_ATTRIBUTE(CKA_MODULUS, n),
232
new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
233
new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
234
new CK_ATTRIBUTE(CKA_PRIME_1, p),
235
new CK_ATTRIBUTE(CKA_PRIME_2, q),
236
new CK_ATTRIBUTE(CKA_EXPONENT_1, pe),
237
new CK_ATTRIBUTE(CKA_EXPONENT_2, qe),
238
new CK_ATTRIBUTE(CKA_COEFFICIENT, coeff),
239
};
240
attributes = token.getAttributes
241
(O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
242
Session session = null;
243
try {
244
session = token.getObjSession();
245
long keyID = token.p11.C_CreateObject(session.id(), attributes);
246
return P11Key.privateKey
247
(session, keyID, "RSA", n.bitLength(), attributes);
248
} finally {
249
token.releaseSession(session);
250
}
251
}
252
253
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
254
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
255
if (keySpec.isAssignableFrom(RSAPublicKeySpec.class)) {
256
session[0] = token.getObjSession();
257
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
258
new CK_ATTRIBUTE(CKA_MODULUS),
259
new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
260
};
261
long keyID = key.getKeyID();
262
try {
263
token.p11.C_GetAttributeValue(session[0].id(), keyID, attributes);
264
} finally {
265
key.releaseKeyID();
266
}
267
KeySpec spec = new RSAPublicKeySpec(
268
attributes[0].getBigInteger(),
269
attributes[1].getBigInteger()
270
);
271
return keySpec.cast(spec);
272
} else { // X.509 handled in superclass
273
throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
274
+ "X509EncodedKeySpec supported for RSA public keys");
275
}
276
}
277
278
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
279
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
280
if (key.sensitive || !key.extractable) {
281
throw new InvalidKeySpecException("Key is sensitive or not extractable");
282
}
283
// If the key is both extractable and not sensitive, then when it was converted into a P11Key
284
// it was also converted into subclass of RSAPrivateKey which encapsulates all of the logic
285
// necessary to retrieve the attributes we need. This sub-class will also cache these attributes
286
// so that we do not need to query them more than once.
287
// Rather than rewrite this logic and make possibly slow calls to the token, we'll just use
288
// that existing logic.
289
if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class)) {
290
// All supported keyspecs (other than PKCS8EncodedKeySpec) descend from RSAPrivateCrtKeySpec
291
if (key instanceof RSAPrivateCrtKey) {
292
RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey)key;
293
return keySpec.cast(new RSAPrivateCrtKeySpec(
294
crtKey.getModulus(),
295
crtKey.getPublicExponent(),
296
crtKey.getPrivateExponent(),
297
crtKey.getPrimeP(),
298
crtKey.getPrimeQ(),
299
crtKey.getPrimeExponentP(),
300
crtKey.getPrimeExponentQ(),
301
crtKey.getCrtCoefficient(),
302
crtKey.getParams()
303
));
304
} else { // RSAPrivateKey (non-CRT)
305
if (!keySpec.isAssignableFrom(RSAPrivateKeySpec.class)) {
306
throw new InvalidKeySpecException
307
("RSAPrivateCrtKeySpec can only be used with CRT keys");
308
}
309
310
if (!(key instanceof RSAPrivateKey)) {
311
// We should never reach here as P11Key.privateKey() should always produce an instance
312
// of RSAPrivateKey when the RSA key is both extractable and non-sensitive.
313
throw new InvalidKeySpecException
314
("Key must be an instance of RSAPrivateKeySpec. Was " + key.getClass());
315
}
316
317
// fall through to RSAPrivateKey (non-CRT)
318
RSAPrivateKey rsaKey = (RSAPrivateKey) key;
319
return keySpec.cast(new RSAPrivateKeySpec(
320
rsaKey.getModulus(),
321
rsaKey.getPrivateExponent(),
322
rsaKey.getParams()
323
));
324
}
325
} else { // PKCS#8 handled in superclass
326
throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
327
+ "and PKCS8EncodedKeySpec supported for RSA private keys");
328
}
329
}
330
331
KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
332
return KeyFactory.getInstance("RSA", P11Util.getSunRsaSignProvider());
333
}
334
335
}
336
337