author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
child 13661 | 7c894680910a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ec; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.interfaces.*; |
|
30 |
import java.security.spec.*; |
|
31 |
||
32 |
/** |
|
33 |
* KeyFactory for EC keys. Keys must be instances of PublicKey or PrivateKey |
|
34 |
* and getAlgorithm() must return "EC". For such keys, it supports conversion |
|
35 |
* between the following: |
|
36 |
* |
|
37 |
* For public keys: |
|
38 |
* . PublicKey with an X.509 encoding |
|
39 |
* . ECPublicKey |
|
40 |
* . ECPublicKeySpec |
|
41 |
* . X509EncodedKeySpec |
|
42 |
* |
|
43 |
* For private keys: |
|
44 |
* . PrivateKey with a PKCS#8 encoding |
|
45 |
* . ECPrivateKey |
|
46 |
* . ECPrivateKeySpec |
|
47 |
* . PKCS8EncodedKeySpec |
|
48 |
* |
|
49 |
* @since 1.6 |
|
50 |
* @author Andreas Sterbenz |
|
51 |
*/ |
|
52 |
public final class ECKeyFactory extends KeyFactorySpi { |
|
53 |
||
54 |
// Used by translateKey() and the SunPKCS11 provider |
|
55 |
public final static KeyFactory INSTANCE; |
|
56 |
||
57 |
// Internal provider object we can obtain the KeyFactory and |
|
58 |
// AlgorithmParameters from. Used by ECParameters and AlgorithmId. |
|
59 |
// This can go away once we have EC always available in the SUN provider. |
|
60 |
// Used by ECParameters and AlgorithmId. |
|
61 |
public final static Provider ecInternalProvider; |
|
62 |
||
63 |
static { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
64 |
final Provider p = new Provider("SunEC-Internal", 1.0d, null) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
65 |
private static final long serialVersionUID = 970685700309471261L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
66 |
}; |
2 | 67 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
68 |
public Void run() { |
|
69 |
p.put("KeyFactory.EC", "sun.security.ec.ECKeyFactory"); |
|
70 |
p.put("AlgorithmParameters.EC", "sun.security.ec.ECParameters"); |
|
71 |
p.put("Alg.Alias.AlgorithmParameters.1.2.840.10045.2.1", "EC"); |
|
72 |
return null; |
|
73 |
} |
|
74 |
}); |
|
75 |
try { |
|
76 |
INSTANCE = KeyFactory.getInstance("EC", p); |
|
77 |
} catch (NoSuchAlgorithmException e) { |
|
78 |
throw new RuntimeException(e); |
|
79 |
} |
|
80 |
ecInternalProvider = p; |
|
81 |
} |
|
82 |
||
83 |
public ECKeyFactory() { |
|
84 |
// empty |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Static method to convert Key into a useable instance of |
|
89 |
* ECPublicKey or ECPrivateKey. Check the key and convert it |
|
90 |
* to a Sun key if necessary. If the key is not an EC key |
|
91 |
* or cannot be used, throw an InvalidKeyException. |
|
92 |
* |
|
93 |
* The difference between this method and engineTranslateKey() is that |
|
94 |
* we do not convert keys of other providers that are already an |
|
95 |
* instance of ECPublicKey or ECPrivateKey. |
|
96 |
* |
|
97 |
* To be used by future Java ECDSA and ECDH implementations. |
|
98 |
*/ |
|
99 |
public static ECKey toECKey(Key key) throws InvalidKeyException { |
|
100 |
if (key instanceof ECKey) { |
|
101 |
ECKey ecKey = (ECKey)key; |
|
102 |
checkKey(ecKey); |
|
103 |
return ecKey; |
|
104 |
} else { |
|
105 |
return (ECKey)INSTANCE.translateKey(key); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Check that the given EC key is valid. |
|
111 |
*/ |
|
112 |
private static void checkKey(ECKey key) throws InvalidKeyException { |
|
113 |
// check for subinterfaces, omit additional checks for our keys |
|
114 |
if (key instanceof ECPublicKey) { |
|
115 |
if (key instanceof ECPublicKeyImpl) { |
|
116 |
return; |
|
117 |
} |
|
118 |
} else if (key instanceof ECPrivateKey) { |
|
119 |
if (key instanceof ECPrivateKeyImpl) { |
|
120 |
return; |
|
121 |
} |
|
122 |
} else { |
|
123 |
throw new InvalidKeyException("Neither a public nor a private key"); |
|
124 |
} |
|
125 |
// ECKey does not extend Key, so we need to do a cast |
|
126 |
String keyAlg = ((Key)key).getAlgorithm(); |
|
127 |
if (keyAlg.equals("EC") == false) { |
|
128 |
throw new InvalidKeyException("Not an EC key: " + keyAlg); |
|
129 |
} |
|
130 |
// XXX further sanity checks about whether this key uses supported |
|
131 |
// fields, point formats, etc. would go here |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Translate an EC key into a Sun EC key. If conversion is |
|
136 |
* not possible, throw an InvalidKeyException. |
|
137 |
* See also JCA doc. |
|
138 |
*/ |
|
139 |
protected Key engineTranslateKey(Key key) throws InvalidKeyException { |
|
140 |
if (key == null) { |
|
141 |
throw new InvalidKeyException("Key must not be null"); |
|
142 |
} |
|
143 |
String keyAlg = key.getAlgorithm(); |
|
144 |
if (keyAlg.equals("EC") == false) { |
|
145 |
throw new InvalidKeyException("Not an EC key: " + keyAlg); |
|
146 |
} |
|
147 |
if (key instanceof PublicKey) { |
|
148 |
return implTranslatePublicKey((PublicKey)key); |
|
149 |
} else if (key instanceof PrivateKey) { |
|
150 |
return implTranslatePrivateKey((PrivateKey)key); |
|
151 |
} else { |
|
152 |
throw new InvalidKeyException("Neither a public nor a private key"); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
// see JCA doc |
|
157 |
protected PublicKey engineGeneratePublic(KeySpec keySpec) |
|
158 |
throws InvalidKeySpecException { |
|
159 |
try { |
|
160 |
return implGeneratePublic(keySpec); |
|
161 |
} catch (InvalidKeySpecException e) { |
|
162 |
throw e; |
|
163 |
} catch (GeneralSecurityException e) { |
|
164 |
throw new InvalidKeySpecException(e); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
// see JCA doc |
|
169 |
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) |
|
170 |
throws InvalidKeySpecException { |
|
171 |
try { |
|
172 |
return implGeneratePrivate(keySpec); |
|
173 |
} catch (InvalidKeySpecException e) { |
|
174 |
throw e; |
|
175 |
} catch (GeneralSecurityException e) { |
|
176 |
throw new InvalidKeySpecException(e); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
// internal implementation of translateKey() for public keys. See JCA doc |
|
181 |
private PublicKey implTranslatePublicKey(PublicKey key) |
|
182 |
throws InvalidKeyException { |
|
183 |
if (key instanceof ECPublicKey) { |
|
184 |
if (key instanceof ECPublicKeyImpl) { |
|
185 |
return key; |
|
186 |
} |
|
187 |
ECPublicKey ecKey = (ECPublicKey)key; |
|
188 |
return new ECPublicKeyImpl( |
|
189 |
ecKey.getW(), |
|
190 |
ecKey.getParams() |
|
191 |
); |
|
192 |
} else if ("X.509".equals(key.getFormat())) { |
|
193 |
byte[] encoded = key.getEncoded(); |
|
194 |
return new ECPublicKeyImpl(encoded); |
|
195 |
} else { |
|
196 |
throw new InvalidKeyException("Public keys must be instance " |
|
197 |
+ "of ECPublicKey or have X.509 encoding"); |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
// internal implementation of translateKey() for private keys. See JCA doc |
|
202 |
private PrivateKey implTranslatePrivateKey(PrivateKey key) |
|
203 |
throws InvalidKeyException { |
|
204 |
if (key instanceof ECPrivateKey) { |
|
205 |
if (key instanceof ECPrivateKeyImpl) { |
|
206 |
return key; |
|
207 |
} |
|
208 |
ECPrivateKey ecKey = (ECPrivateKey)key; |
|
209 |
return new ECPrivateKeyImpl( |
|
210 |
ecKey.getS(), |
|
211 |
ecKey.getParams() |
|
212 |
); |
|
213 |
} else if ("PKCS#8".equals(key.getFormat())) { |
|
214 |
return new ECPrivateKeyImpl(key.getEncoded()); |
|
215 |
} else { |
|
216 |
throw new InvalidKeyException("Private keys must be instance " |
|
217 |
+ "of ECPrivateKey or have PKCS#8 encoding"); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
// internal implementation of generatePublic. See JCA doc |
|
222 |
private PublicKey implGeneratePublic(KeySpec keySpec) |
|
223 |
throws GeneralSecurityException { |
|
224 |
if (keySpec instanceof X509EncodedKeySpec) { |
|
225 |
X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec; |
|
226 |
return new ECPublicKeyImpl(x509Spec.getEncoded()); |
|
227 |
} else if (keySpec instanceof ECPublicKeySpec) { |
|
228 |
ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec; |
|
229 |
return new ECPublicKeyImpl( |
|
230 |
ecSpec.getW(), |
|
231 |
ecSpec.getParams() |
|
232 |
); |
|
233 |
} else { |
|
234 |
throw new InvalidKeySpecException("Only ECPublicKeySpec " |
|
235 |
+ "and X509EncodedKeySpec supported for EC public keys"); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
// internal implementation of generatePrivate. See JCA doc |
|
240 |
private PrivateKey implGeneratePrivate(KeySpec keySpec) |
|
241 |
throws GeneralSecurityException { |
|
242 |
if (keySpec instanceof PKCS8EncodedKeySpec) { |
|
243 |
PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec; |
|
244 |
return new ECPrivateKeyImpl(pkcsSpec.getEncoded()); |
|
245 |
} else if (keySpec instanceof ECPrivateKeySpec) { |
|
246 |
ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec; |
|
247 |
return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams()); |
|
248 |
} else { |
|
249 |
throw new InvalidKeySpecException("Only ECPrivateKeySpec " |
|
250 |
+ "and PKCS8EncodedKeySpec supported for EC private keys"); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) |
|
255 |
throws InvalidKeySpecException { |
|
256 |
try { |
|
257 |
// convert key to one of our keys |
|
258 |
// this also verifies that the key is a valid EC key and ensures |
|
259 |
// that the encoding is X.509/PKCS#8 for public/private keys |
|
260 |
key = engineTranslateKey(key); |
|
261 |
} catch (InvalidKeyException e) { |
|
262 |
throw new InvalidKeySpecException(e); |
|
263 |
} |
|
264 |
if (key instanceof ECPublicKey) { |
|
265 |
ECPublicKey ecKey = (ECPublicKey)key; |
|
266 |
if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
267 |
return keySpec.cast(new ECPublicKeySpec( |
2 | 268 |
ecKey.getW(), |
269 |
ecKey.getParams() |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
270 |
)); |
2 | 271 |
} else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
272 |
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded())); |
2 | 273 |
} else { |
274 |
throw new InvalidKeySpecException |
|
275 |
("KeySpec must be ECPublicKeySpec or " |
|
276 |
+ "X509EncodedKeySpec for EC public keys"); |
|
277 |
} |
|
278 |
} else if (key instanceof ECPrivateKey) { |
|
279 |
if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
280 |
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded())); |
2 | 281 |
} else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) { |
282 |
ECPrivateKey ecKey = (ECPrivateKey)key; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
283 |
return keySpec.cast(new ECPrivateKeySpec( |
2 | 284 |
ecKey.getS(), |
285 |
ecKey.getParams() |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
286 |
)); |
2 | 287 |
} else { |
288 |
throw new InvalidKeySpecException |
|
289 |
("KeySpec must be ECPrivateKeySpec or " |
|
290 |
+ "PKCS8EncodedKeySpec for EC private keys"); |
|
291 |
} |
|
292 |
} else { |
|
293 |
// should not occur, caught in engineTranslateKey() |
|
294 |
throw new InvalidKeySpecException("Neither public nor private key"); |
|
295 |
} |
|
296 |
} |
|
297 |
} |