Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
41161 views
1
/*
2
* Copyright (c) 2009, 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.ec;
27
28
import java.security.AccessController;
29
import java.security.InvalidParameterException;
30
import java.security.NoSuchAlgorithmException;
31
import java.security.PrivilegedAction;
32
import java.security.Provider;
33
import java.security.ProviderException;
34
import java.util.ArrayList;
35
import java.util.Collection;
36
import java.util.Collections;
37
import java.util.HashMap;
38
import java.util.Iterator;
39
import java.util.List;
40
41
import sun.security.ec.ed.EdDSAAlgorithmParameters;
42
import sun.security.ec.ed.EdDSAKeyFactory;
43
import sun.security.ec.ed.EdDSAKeyPairGenerator;
44
import sun.security.ec.ed.EdDSASignature;
45
import sun.security.util.CurveDB;
46
import sun.security.util.KnownOIDs;
47
import sun.security.util.NamedCurve;
48
49
import static sun.security.util.SecurityConstants.PROVIDER_VER;
50
import static sun.security.util.SecurityProviderConstants.*;
51
52
/**
53
* Provider class for the Elliptic Curve provider.
54
*/
55
public final class SunEC extends Provider {
56
57
private static final long serialVersionUID = -2279741672933606418L;
58
59
private static class ProviderServiceA extends ProviderService {
60
ProviderServiceA(Provider p, String type, String algo, String cn,
61
HashMap<String, String> attrs) {
62
super(p, type, algo, cn, getAliases(algo), attrs);
63
}
64
}
65
66
private static class ProviderService extends Provider.Service {
67
68
ProviderService(Provider p, String type, String algo, String cn) {
69
super(p, type, algo, cn, null, null);
70
}
71
72
ProviderService(Provider p, String type, String algo, String cn,
73
List<String> aliases, HashMap<String, String> attrs) {
74
super(p, type, algo, cn, aliases, attrs);
75
}
76
77
@Override
78
public Object newInstance(Object ctrParamObj)
79
throws NoSuchAlgorithmException {
80
String type = getType();
81
if (ctrParamObj != null) {
82
throw new InvalidParameterException
83
("constructorParameter not used with " + type + " engines");
84
}
85
86
String algo = getAlgorithm();
87
try {
88
if (type.equals("Signature")) {
89
90
if (algo.equalsIgnoreCase("EdDSA")) {
91
return new EdDSASignature();
92
} else if (algo.equalsIgnoreCase("Ed25519")) {
93
return new EdDSASignature.Ed25519();
94
} else if (algo.equalsIgnoreCase("Ed448")) {
95
return new EdDSASignature.Ed448();
96
}
97
98
boolean inP1363 = algo.endsWith("inP1363Format");
99
if (inP1363) {
100
algo = algo.substring(0, algo.length() - 13);
101
}
102
if (algo.equals("SHA1withECDSA")) {
103
return (inP1363? new ECDSASignature.SHA1inP1363Format() :
104
new ECDSASignature.SHA1());
105
} else if (algo.equals("SHA224withECDSA")) {
106
return (inP1363? new ECDSASignature.SHA224inP1363Format() :
107
new ECDSASignature.SHA224());
108
} else if (algo.equals("SHA256withECDSA")) {
109
return (inP1363? new ECDSASignature.SHA256inP1363Format() :
110
new ECDSASignature.SHA256());
111
} else if (algo.equals("SHA384withECDSA")) {
112
return (inP1363? new ECDSASignature.SHA384inP1363Format() :
113
new ECDSASignature.SHA384());
114
} else if (algo.equals("SHA512withECDSA")) {
115
return (inP1363? new ECDSASignature.SHA512inP1363Format() :
116
new ECDSASignature.SHA512());
117
} else if (algo.equals("NONEwithECDSA")) {
118
return (inP1363? new ECDSASignature.RawinP1363Format() :
119
new ECDSASignature.Raw());
120
} else if (algo.equals("SHA3-224withECDSA")) {
121
return (inP1363? new ECDSASignature.SHA3_224inP1363Format() :
122
new ECDSASignature.SHA3_224());
123
} else if (algo.equals("SHA3-256withECDSA")) {
124
return (inP1363? new ECDSASignature.SHA3_256inP1363Format() :
125
new ECDSASignature.SHA3_256());
126
} else if (algo.equals("SHA3-384withECDSA")) {
127
return (inP1363? new ECDSASignature.SHA3_384inP1363Format() :
128
new ECDSASignature.SHA3_384());
129
} else if (algo.equals("SHA3-512withECDSA")) {
130
return (inP1363? new ECDSASignature.SHA3_512inP1363Format() :
131
new ECDSASignature.SHA3_512());
132
}
133
} else if (type.equals("KeyFactory")) {
134
if (algo.equals("EC")) {
135
return new ECKeyFactory();
136
} else if (algo.equals("XDH")) {
137
return new XDHKeyFactory();
138
} else if (algo.equals("X25519")) {
139
return new XDHKeyFactory.X25519();
140
} else if (algo.equals("X448")) {
141
return new XDHKeyFactory.X448();
142
} else if (algo.equalsIgnoreCase("EdDSA")) {
143
return new EdDSAKeyFactory();
144
} else if (algo.equalsIgnoreCase("Ed25519")) {
145
return new EdDSAKeyFactory.Ed25519();
146
} else if (algo.equalsIgnoreCase("Ed448")) {
147
return new EdDSAKeyFactory.Ed448();
148
}
149
} else if (type.equals("AlgorithmParameters")) {
150
if (algo.equals("EC")) {
151
return new sun.security.util.ECParameters();
152
}
153
} else if (type.equals("KeyPairGenerator")) {
154
if (algo.equals("EC")) {
155
return new ECKeyPairGenerator();
156
} else if (algo.equals("XDH")) {
157
return new XDHKeyPairGenerator();
158
} else if (algo.equals("X25519")) {
159
return new XDHKeyPairGenerator.X25519();
160
} else if (algo.equals("X448")) {
161
return new XDHKeyPairGenerator.X448();
162
} else if (algo.equalsIgnoreCase("EdDSA")) {
163
return new EdDSAKeyPairGenerator();
164
} else if (algo.equalsIgnoreCase("Ed25519")) {
165
return new EdDSAKeyPairGenerator.Ed25519();
166
} else if (algo.equalsIgnoreCase("Ed448")) {
167
return new EdDSAKeyPairGenerator.Ed448();
168
}
169
} else if (type.equals("KeyAgreement")) {
170
if (algo.equals("ECDH")) {
171
return new ECDHKeyAgreement();
172
} else if (algo.equals("XDH")) {
173
return new XDHKeyAgreement();
174
} else if (algo.equals("X25519")) {
175
return new XDHKeyAgreement.X25519();
176
} else if (algo.equals("X448")) {
177
return new XDHKeyAgreement.X448();
178
}
179
}
180
} catch (Exception ex) {
181
throw new NoSuchAlgorithmException("Error constructing " +
182
type + " for " + algo + " using SunEC", ex);
183
}
184
throw new ProviderException("No impl for " + algo +
185
" " + type);
186
}
187
}
188
189
@SuppressWarnings("removal")
190
public SunEC() {
191
super("SunEC", PROVIDER_VER, "Sun Elliptic Curve provider");
192
AccessController.doPrivileged(new PrivilegedAction<Void>() {
193
public Void run() {
194
putEntries();
195
return null;
196
}
197
});
198
}
199
200
void putEntries() {
201
HashMap<String, String> ATTRS = new HashMap<>(3);
202
ATTRS.put("ImplementedIn", "Software");
203
String ecKeyClasses = "java.security.interfaces.ECPublicKey" +
204
"|java.security.interfaces.ECPrivateKey";
205
ATTRS.put("SupportedKeyClasses", ecKeyClasses);
206
ATTRS.put("KeySize", "256");
207
208
/*
209
* Key Factory engine
210
*/
211
putService(new ProviderService(this, "KeyFactory",
212
"EC", "sun.security.ec.ECKeyFactory",
213
List.of("EllipticCurve"), ATTRS));
214
215
/*
216
* Algorithm Parameter engine
217
*/
218
// "AlgorithmParameters.EC SupportedCurves" prop used by unit test
219
boolean firstCurve = true;
220
StringBuilder names = new StringBuilder();
221
222
for (NamedCurve namedCurve :
223
List.of(
224
CurveDB.lookup("secp256r1"),
225
CurveDB.lookup("secp384r1"),
226
CurveDB.lookup("secp521r1"))) {
227
if (!firstCurve) {
228
names.append("|");
229
} else {
230
firstCurve = false;
231
}
232
233
names.append("[");
234
String[] commonNames = namedCurve.getNameAndAliases();
235
for (String commonName : commonNames) {
236
names.append(commonName);
237
names.append(",");
238
}
239
240
names.append(namedCurve.getObjectId());
241
names.append("]");
242
}
243
244
HashMap<String, String> apAttrs = new HashMap<>(ATTRS);
245
apAttrs.put("SupportedCurves", names.toString());
246
247
putService(new ProviderServiceA(this, "AlgorithmParameters",
248
"EC", "sun.security.util.ECParameters", apAttrs));
249
250
putXDHEntries();
251
putEdDSAEntries();
252
253
/*
254
* Signature engines
255
*/
256
putService(new ProviderService(this, "Signature",
257
"NONEwithECDSA", "sun.security.ec.ECDSASignature$Raw",
258
null, ATTRS));
259
putService(new ProviderServiceA(this, "Signature",
260
"SHA1withECDSA", "sun.security.ec.ECDSASignature$SHA1",
261
ATTRS));
262
putService(new ProviderServiceA(this, "Signature",
263
"SHA224withECDSA", "sun.security.ec.ECDSASignature$SHA224",
264
ATTRS));
265
putService(new ProviderServiceA(this, "Signature",
266
"SHA256withECDSA", "sun.security.ec.ECDSASignature$SHA256",
267
ATTRS));
268
putService(new ProviderServiceA(this, "Signature",
269
"SHA384withECDSA", "sun.security.ec.ECDSASignature$SHA384",
270
ATTRS));
271
putService(new ProviderServiceA(this, "Signature",
272
"SHA512withECDSA", "sun.security.ec.ECDSASignature$SHA512",
273
ATTRS));
274
putService(new ProviderServiceA(this, "Signature",
275
"SHA3-224withECDSA", "sun.security.ec.ECDSASignature$SHA3_224",
276
ATTRS));
277
putService(new ProviderServiceA(this, "Signature",
278
"SHA3-256withECDSA", "sun.security.ec.ECDSASignature$SHA3_256",
279
ATTRS));
280
putService(new ProviderServiceA(this, "Signature",
281
"SHA3-384withECDSA", "sun.security.ec.ECDSASignature$SHA3_384",
282
ATTRS));
283
putService(new ProviderServiceA(this, "Signature",
284
"SHA3-512withECDSA", "sun.security.ec.ECDSASignature$SHA3_512",
285
ATTRS));
286
287
putService(new ProviderService(this, "Signature",
288
"NONEwithECDSAinP1363Format",
289
"sun.security.ec.ECDSASignature$RawinP1363Format"));
290
putService(new ProviderService(this, "Signature",
291
"SHA1withECDSAinP1363Format",
292
"sun.security.ec.ECDSASignature$SHA1inP1363Format"));
293
putService(new ProviderService(this, "Signature",
294
"SHA224withECDSAinP1363Format",
295
"sun.security.ec.ECDSASignature$SHA224inP1363Format"));
296
putService(new ProviderService(this, "Signature",
297
"SHA256withECDSAinP1363Format",
298
"sun.security.ec.ECDSASignature$SHA256inP1363Format"));
299
putService(new ProviderService(this, "Signature",
300
"SHA384withECDSAinP1363Format",
301
"sun.security.ec.ECDSASignature$SHA384inP1363Format"));
302
putService(new ProviderService(this, "Signature",
303
"SHA512withECDSAinP1363Format",
304
"sun.security.ec.ECDSASignature$SHA512inP1363Format"));
305
306
putService(new ProviderService(this, "Signature",
307
"SHA3-224withECDSAinP1363Format",
308
"sun.security.ec.ECDSASignature$SHA3_224inP1363Format"));
309
putService(new ProviderService(this, "Signature",
310
"SHA3-256withECDSAinP1363Format",
311
"sun.security.ec.ECDSASignature$SHA3_256inP1363Format"));
312
putService(new ProviderService(this, "Signature",
313
"SHA3-384withECDSAinP1363Format",
314
"sun.security.ec.ECDSASignature$SHA3_384inP1363Format"));
315
putService(new ProviderService(this, "Signature",
316
"SHA3-512withECDSAinP1363Format",
317
"sun.security.ec.ECDSASignature$SHA3_512inP1363Format"));
318
319
/*
320
* Key Pair Generator engine
321
*/
322
putService(new ProviderService(this, "KeyPairGenerator",
323
"EC", "sun.security.ec.ECKeyPairGenerator",
324
List.of("EllipticCurve"), ATTRS));
325
326
/*
327
* Key Agreement engine
328
*/
329
putService(new ProviderService(this, "KeyAgreement",
330
"ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));
331
}
332
333
private void putXDHEntries() {
334
335
HashMap<String, String> ATTRS = new HashMap<>(1);
336
ATTRS.put("ImplementedIn", "Software");
337
338
putService(new ProviderService(this, "KeyFactory",
339
"XDH", "sun.security.ec.XDHKeyFactory", null, ATTRS));
340
putService(new ProviderServiceA(this, "KeyFactory",
341
"X25519", "sun.security.ec.XDHKeyFactory.X25519",
342
ATTRS));
343
putService(new ProviderServiceA(this, "KeyFactory",
344
"X448", "sun.security.ec.XDHKeyFactory.X448",
345
ATTRS));
346
347
putService(new ProviderService(this, "KeyPairGenerator",
348
"XDH", "sun.security.ec.XDHKeyPairGenerator", null, ATTRS));
349
putService(new ProviderServiceA(this, "KeyPairGenerator",
350
"X25519", "sun.security.ec.XDHKeyPairGenerator.X25519",
351
ATTRS));
352
putService(new ProviderServiceA(this, "KeyPairGenerator",
353
"X448", "sun.security.ec.XDHKeyPairGenerator.X448",
354
ATTRS));
355
356
putService(new ProviderService(this, "KeyAgreement",
357
"XDH", "sun.security.ec.XDHKeyAgreement", null, ATTRS));
358
putService(new ProviderServiceA(this, "KeyAgreement",
359
"X25519", "sun.security.ec.XDHKeyAgreement.X25519",
360
ATTRS));
361
putService(new ProviderServiceA(this, "KeyAgreement",
362
"X448", "sun.security.ec.XDHKeyAgreement.X448",
363
ATTRS));
364
}
365
366
private void putEdDSAEntries() {
367
368
HashMap<String, String> ATTRS = new HashMap<>(1);
369
ATTRS.put("ImplementedIn", "Software");
370
371
putService(new ProviderService(this, "KeyFactory",
372
"EdDSA", "sun.security.ec.ed.EdDSAKeyFactory", null, ATTRS));
373
putService(new ProviderServiceA(this, "KeyFactory",
374
"Ed25519", "sun.security.ec.ed.EdDSAKeyFactory.Ed25519", ATTRS));
375
putService(new ProviderServiceA(this, "KeyFactory",
376
"Ed448", "sun.security.ec.ed.EdDSAKeyFactory.Ed448", ATTRS));
377
378
putService(new ProviderService(this, "KeyPairGenerator",
379
"EdDSA", "sun.security.ec.ed.EdDSAKeyPairGenerator", null, ATTRS));
380
putService(new ProviderServiceA(this, "KeyPairGenerator",
381
"Ed25519", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed25519",
382
ATTRS));
383
putService(new ProviderServiceA(this, "KeyPairGenerator",
384
"Ed448", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed448",
385
ATTRS));
386
387
putService(new ProviderService(this, "Signature",
388
"EdDSA", "sun.security.ec.ed.EdDSASignature", null, ATTRS));
389
putService(new ProviderServiceA(this, "Signature",
390
"Ed25519", "sun.security.ec.ed.EdDSASignature.Ed25519", ATTRS));
391
putService(new ProviderServiceA(this, "Signature",
392
"Ed448", "sun.security.ec.ed.EdDSASignature.Ed448", ATTRS));
393
394
}
395
}
396
397