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