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/XDHKeyFactory.java
41161 views
1
/*
2
* Copyright (c) 2018, 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.KeyFactorySpi;
29
import java.security.Key;
30
import java.security.PublicKey;
31
import java.security.PrivateKey;
32
import java.security.InvalidKeyException;
33
import java.security.ProviderException;
34
import java.security.interfaces.XECKey;
35
import java.security.interfaces.XECPrivateKey;
36
import java.security.interfaces.XECPublicKey;
37
import java.security.spec.AlgorithmParameterSpec;
38
import java.security.spec.NamedParameterSpec;
39
import java.security.spec.KeySpec;
40
import java.security.spec.InvalidKeySpecException;
41
import java.security.spec.PKCS8EncodedKeySpec;
42
import java.security.spec.X509EncodedKeySpec;
43
import java.security.spec.XECPublicKeySpec;
44
import java.security.spec.XECPrivateKeySpec;
45
import java.util.Arrays;
46
import java.util.function.Function;
47
48
public class XDHKeyFactory extends KeyFactorySpi {
49
50
private XECParameters lockedParams = null;
51
52
XDHKeyFactory() {
53
// do nothing
54
}
55
56
protected XDHKeyFactory(AlgorithmParameterSpec paramSpec) {
57
lockedParams = XECParameters.get(ProviderException::new, paramSpec);
58
}
59
60
@Override
61
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
62
63
if (key == null) {
64
throw new InvalidKeyException("Key must not be null");
65
}
66
67
if (key instanceof XECKey) {
68
XECKey xecKey = (XECKey) key;
69
XECParameters params = XECParameters.get(InvalidKeyException::new,
70
xecKey.getParams());
71
checkLockedParams(InvalidKeyException::new, params);
72
73
if (xecKey instanceof XECPublicKey) {
74
XECPublicKey publicKey = (XECPublicKey) xecKey;
75
return new XDHPublicKeyImpl(params, publicKey.getU());
76
} else if (xecKey instanceof XECPrivateKey) {
77
XECPrivateKey privateKey = (XECPrivateKey) xecKey;
78
byte[] scalar = privateKey.getScalar().orElseThrow(
79
() -> new InvalidKeyException("No private key data"));
80
return new XDHPrivateKeyImpl(params, scalar);
81
} else {
82
throw new InvalidKeyException("Unsupported XECKey subclass");
83
}
84
} else if (key instanceof PublicKey &&
85
key.getFormat().equals("X.509")) {
86
XDHPublicKeyImpl result = new XDHPublicKeyImpl(key.getEncoded());
87
checkLockedParams(InvalidKeyException::new, result.getParams());
88
return result;
89
} else if (key instanceof PrivateKey &&
90
key.getFormat().equals("PKCS#8")) {
91
byte[] encoded = key.getEncoded();
92
try {
93
XDHPrivateKeyImpl result = new XDHPrivateKeyImpl(encoded);
94
checkLockedParams(InvalidKeyException::new, result.getParams());
95
return result;
96
} finally {
97
Arrays.fill(encoded, (byte)0);
98
}
99
} else {
100
throw new InvalidKeyException("Unsupported key type or format");
101
}
102
}
103
104
private
105
<T extends Throwable>
106
void checkLockedParams(Function<String, T> exception,
107
AlgorithmParameterSpec spec) throws T {
108
109
XECParameters params = XECParameters.get(exception, spec);
110
checkLockedParams(exception, params);
111
}
112
113
private
114
<T extends Throwable>
115
void checkLockedParams(Function<String, T> exception,
116
XECParameters params) throws T {
117
118
if (lockedParams != null && lockedParams != params) {
119
throw exception.apply("Parameters must be " +
120
lockedParams.getName());
121
}
122
}
123
124
@Override
125
protected PublicKey engineGeneratePublic(KeySpec keySpec)
126
throws InvalidKeySpecException {
127
128
try {
129
return generatePublicImpl(keySpec);
130
} catch (InvalidKeyException ex) {
131
throw new InvalidKeySpecException(ex);
132
}
133
}
134
135
@Override
136
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
137
throws InvalidKeySpecException {
138
139
try {
140
return generatePrivateImpl(keySpec);
141
} catch (InvalidKeyException ex) {
142
throw new InvalidKeySpecException(ex);
143
}
144
}
145
146
147
private PublicKey generatePublicImpl(KeySpec keySpec)
148
throws InvalidKeyException, InvalidKeySpecException {
149
150
if (keySpec instanceof X509EncodedKeySpec) {
151
X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
152
XDHPublicKeyImpl result =
153
new XDHPublicKeyImpl(x509Spec.getEncoded());
154
checkLockedParams(InvalidKeySpecException::new,
155
result.getParams());
156
return result;
157
} else if (keySpec instanceof XECPublicKeySpec) {
158
XECPublicKeySpec publicKeySpec = (XECPublicKeySpec) keySpec;
159
XECParameters params = XECParameters.get(
160
InvalidKeySpecException::new, publicKeySpec.getParams());
161
checkLockedParams(InvalidKeySpecException::new, params);
162
return new XDHPublicKeyImpl(params, publicKeySpec.getU());
163
} else {
164
throw new InvalidKeySpecException(
165
"Only X509EncodedKeySpec and XECPublicKeySpec are supported");
166
}
167
}
168
169
private PrivateKey generatePrivateImpl(KeySpec keySpec)
170
throws InvalidKeyException, InvalidKeySpecException {
171
172
if (keySpec instanceof PKCS8EncodedKeySpec) {
173
PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec) keySpec;
174
byte[] encoded = pkcsSpec.getEncoded();
175
try {
176
XDHPrivateKeyImpl result = new XDHPrivateKeyImpl(encoded);
177
checkLockedParams(InvalidKeySpecException::new,
178
result.getParams());
179
return result;
180
} finally {
181
Arrays.fill(encoded, (byte) 0);
182
}
183
} else if (keySpec instanceof XECPrivateKeySpec) {
184
XECPrivateKeySpec privateKeySpec = (XECPrivateKeySpec) keySpec;
185
XECParameters params = XECParameters.get(
186
InvalidKeySpecException::new, privateKeySpec.getParams());
187
checkLockedParams(InvalidKeySpecException::new, params);
188
byte[] scalar = privateKeySpec.getScalar();
189
try {
190
return new XDHPrivateKeyImpl(params, scalar);
191
} finally {
192
Arrays.fill(scalar, (byte)0);
193
}
194
} else {
195
throw new InvalidKeySpecException(
196
"Only PKCS8EncodedKeySpec and XECPrivateKeySpec supported");
197
}
198
}
199
200
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
201
throws InvalidKeySpecException {
202
203
if (key instanceof XECPublicKey) {
204
checkLockedParams(InvalidKeySpecException::new,
205
((XECPublicKey) key).getParams());
206
207
if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {
208
if (!key.getFormat().equals("X.509")) {
209
throw new InvalidKeySpecException("Format is not X.509");
210
}
211
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
212
} else if (keySpec.isAssignableFrom(XECPublicKeySpec.class)) {
213
XECPublicKey xecKey = (XECPublicKey) key;
214
return keySpec.cast(
215
new XECPublicKeySpec(xecKey.getParams(), xecKey.getU()));
216
} else {
217
throw new InvalidKeySpecException(
218
"KeySpec must be X509EncodedKeySpec or XECPublicKeySpec");
219
}
220
} else if (key instanceof XECPrivateKey) {
221
checkLockedParams(InvalidKeySpecException::new,
222
((XECPrivateKey) key).getParams());
223
224
if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) {
225
if (!key.getFormat().equals("PKCS#8")) {
226
throw new InvalidKeySpecException("Format is not PKCS#8");
227
}
228
byte[] encoded = key.getEncoded();
229
try {
230
return keySpec.cast(new PKCS8EncodedKeySpec(encoded));
231
} finally {
232
Arrays.fill(encoded, (byte)0);
233
}
234
} else if (keySpec.isAssignableFrom(XECPrivateKeySpec.class)) {
235
XECPrivateKey xecKey = (XECPrivateKey) key;
236
byte[] scalar = xecKey.getScalar().orElseThrow(
237
() -> new InvalidKeySpecException("No private key value")
238
);
239
try {
240
return keySpec.cast(
241
new XECPrivateKeySpec(xecKey.getParams(), scalar));
242
} finally {
243
Arrays.fill(scalar, (byte)0);
244
}
245
} else {
246
throw new InvalidKeySpecException
247
("KeySpec must be PKCS8EncodedKeySpec or XECPrivateKeySpec");
248
}
249
} else {
250
throw new InvalidKeySpecException("Unsupported key type");
251
}
252
}
253
254
static class X25519 extends XDHKeyFactory {
255
256
public X25519() {
257
super(NamedParameterSpec.X25519);
258
}
259
}
260
261
static class X448 extends XDHKeyFactory {
262
263
public X448() {
264
super(NamedParameterSpec.X448);
265
}
266
}
267
}
268
269