author | vinnie |
Thu, 13 Oct 2011 12:00:51 +0100 | |
changeset 10781 | f8a00c400655 |
parent 10336 | 0bb1999251f8 |
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:
7668
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.pkcs11; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.math.BigInteger; |
|
30 |
||
31 |
import java.security.*; |
|
32 |
import java.security.interfaces.*; |
|
33 |
import java.security.spec.*; |
|
34 |
||
35 |
import sun.security.ec.ECPublicKeyImpl; |
|
36 |
import sun.security.ec.ECParameters; |
|
37 |
import sun.security.ec.NamedCurve; |
|
38 |
||
39 |
import static sun.security.pkcs11.TemplateManager.*; |
|
40 |
import sun.security.pkcs11.wrapper.*; |
|
41 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
42 |
||
4809
c00eed67999d
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents:
2
diff
changeset
|
43 |
import sun.security.util.DerValue; |
c00eed67999d
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents:
2
diff
changeset
|
44 |
|
2 | 45 |
/** |
46 |
* EC KeyFactory implemenation. |
|
47 |
* |
|
48 |
* @author Andreas Sterbenz |
|
49 |
* @since 1.6 |
|
50 |
*/ |
|
51 |
final class P11ECKeyFactory extends P11KeyFactory { |
|
52 |
||
53 |
P11ECKeyFactory(Token token, String algorithm) { |
|
54 |
super(token, algorithm); |
|
55 |
} |
|
56 |
||
57 |
static ECParameterSpec getECParameterSpec(String name) { |
|
58 |
return NamedCurve.getECParameterSpec(name); |
|
59 |
} |
|
60 |
||
61 |
static ECParameterSpec getECParameterSpec(int keySize) { |
|
62 |
return NamedCurve.getECParameterSpec(keySize); |
|
63 |
} |
|
64 |
||
65 |
// Check that spec is a known supported curve and convert it to our |
|
66 |
// ECParameterSpec subclass. If not possible, return null. |
|
67 |
static ECParameterSpec getECParameterSpec(ECParameterSpec spec) { |
|
68 |
return ECParameters.getNamedCurve(spec); |
|
69 |
} |
|
70 |
||
71 |
static ECParameterSpec decodeParameters(byte[] params) throws IOException { |
|
72 |
return ECParameters.decodeParameters(params); |
|
73 |
} |
|
74 |
||
75 |
static byte[] encodeParameters(ECParameterSpec params) { |
|
76 |
return ECParameters.encodeParameters(params); |
|
77 |
} |
|
78 |
||
79 |
static ECPoint decodePoint(byte[] encoded, EllipticCurve curve) throws IOException { |
|
80 |
return ECParameters.decodePoint(encoded, curve); |
|
81 |
} |
|
82 |
||
83 |
// Used by ECDH KeyAgreement |
|
84 |
static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException { |
|
85 |
if (key instanceof ECPublicKeyImpl) { |
|
86 |
return ((ECPublicKeyImpl)key).getEncodedPublicValue(); |
|
87 |
} else if (key instanceof ECPublicKey) { |
|
88 |
ECPublicKey ecKey = (ECPublicKey)key; |
|
89 |
ECPoint w = ecKey.getW(); |
|
90 |
ECParameterSpec params = ecKey.getParams(); |
|
91 |
return ECParameters.encodePoint(w, params.getCurve()); |
|
92 |
} else { |
|
93 |
// should never occur |
|
94 |
throw new InvalidKeyException |
|
95 |
("Key class not yet supported: " + key.getClass().getName()); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException { |
|
100 |
try { |
|
101 |
if (key instanceof ECPublicKey) { |
|
102 |
ECPublicKey ecKey = (ECPublicKey)key; |
|
103 |
return generatePublic( |
|
104 |
ecKey.getW(), |
|
105 |
ecKey.getParams() |
|
106 |
); |
|
107 |
} else if ("X.509".equals(key.getFormat())) { |
|
108 |
// let Sun provider parse for us, then recurse |
|
109 |
byte[] encoded = key.getEncoded(); |
|
110 |
key = new sun.security.ec.ECPublicKeyImpl(encoded); |
|
111 |
return implTranslatePublicKey(key); |
|
112 |
} else { |
|
113 |
throw new InvalidKeyException("PublicKey must be instance " |
|
114 |
+ "of ECPublicKey or have X.509 encoding"); |
|
115 |
} |
|
116 |
} catch (PKCS11Exception e) { |
|
117 |
throw new InvalidKeyException("Could not create EC public key", e); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
PrivateKey implTranslatePrivateKey(PrivateKey key) |
|
122 |
throws InvalidKeyException { |
|
123 |
try { |
|
124 |
if (key instanceof ECPrivateKey) { |
|
125 |
ECPrivateKey ecKey = (ECPrivateKey)key; |
|
126 |
return generatePrivate( |
|
127 |
ecKey.getS(), |
|
128 |
ecKey.getParams() |
|
129 |
); |
|
130 |
} else if ("PKCS#8".equals(key.getFormat())) { |
|
131 |
// let Sun provider parse for us, then recurse |
|
132 |
byte[] encoded = key.getEncoded(); |
|
133 |
key = new sun.security.ec.ECPrivateKeyImpl(encoded); |
|
134 |
return implTranslatePrivateKey(key); |
|
135 |
} else { |
|
136 |
throw new InvalidKeyException("PrivateKey must be instance " |
|
137 |
+ "of ECPrivateKey or have PKCS#8 encoding"); |
|
138 |
} |
|
139 |
} catch (PKCS11Exception e) { |
|
140 |
throw new InvalidKeyException("Could not create EC private key", e); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
// see JCA spec |
|
145 |
protected PublicKey engineGeneratePublic(KeySpec keySpec) |
|
146 |
throws InvalidKeySpecException { |
|
147 |
token.ensureValid(); |
|
148 |
if (keySpec instanceof X509EncodedKeySpec) { |
|
149 |
try { |
|
150 |
byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded(); |
|
151 |
PublicKey key = new sun.security.ec.ECPublicKeyImpl(encoded); |
|
152 |
return implTranslatePublicKey(key); |
|
153 |
} catch (InvalidKeyException e) { |
|
154 |
throw new InvalidKeySpecException |
|
155 |
("Could not create EC public key", e); |
|
156 |
} |
|
157 |
} |
|
158 |
if (keySpec instanceof ECPublicKeySpec == false) { |
|
159 |
throw new InvalidKeySpecException("Only ECPublicKeySpec and " |
|
160 |
+ "X509EncodedKeySpec supported for EC public keys"); |
|
161 |
} |
|
162 |
try { |
|
163 |
ECPublicKeySpec ec = (ECPublicKeySpec)keySpec; |
|
164 |
return generatePublic( |
|
165 |
ec.getW(), |
|
166 |
ec.getParams() |
|
167 |
); |
|
168 |
} catch (PKCS11Exception e) { |
|
169 |
throw new InvalidKeySpecException |
|
170 |
("Could not create EC public key", e); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
// see JCA spec |
|
175 |
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) |
|
176 |
throws InvalidKeySpecException { |
|
177 |
token.ensureValid(); |
|
178 |
if (keySpec instanceof PKCS8EncodedKeySpec) { |
|
179 |
try { |
|
180 |
byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); |
|
181 |
PrivateKey key = new sun.security.ec.ECPrivateKeyImpl(encoded); |
|
182 |
return implTranslatePrivateKey(key); |
|
183 |
} catch (GeneralSecurityException e) { |
|
184 |
throw new InvalidKeySpecException |
|
185 |
("Could not create EC private key", e); |
|
186 |
} |
|
187 |
} |
|
188 |
if (keySpec instanceof ECPrivateKeySpec == false) { |
|
189 |
throw new InvalidKeySpecException("Only ECPrivateKeySpec and " |
|
190 |
+ "PKCS8EncodedKeySpec supported for EC private keys"); |
|
191 |
} |
|
192 |
try { |
|
193 |
ECPrivateKeySpec ec = (ECPrivateKeySpec)keySpec; |
|
194 |
return generatePrivate( |
|
195 |
ec.getS(), |
|
196 |
ec.getParams() |
|
197 |
); |
|
198 |
} catch (PKCS11Exception e) { |
|
199 |
throw new InvalidKeySpecException |
|
200 |
("Could not create EC private key", e); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
private PublicKey generatePublic(ECPoint point, ECParameterSpec params) throws PKCS11Exception { |
|
205 |
byte[] encodedParams = ECParameters.encodeParameters(params); |
|
10781
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
206 |
byte[] encodedPoint = |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
207 |
ECParameters.encodePoint(point, params.getCurve()); |
4809
c00eed67999d
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents:
2
diff
changeset
|
208 |
|
10781
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
209 |
// Check whether the X9.63 encoding of an EC point shall be wrapped |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
210 |
// in an ASN.1 OCTET STRING |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
211 |
if (!token.config.getUseEcX963Encoding()) { |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
212 |
try { |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
213 |
encodedPoint = |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
214 |
new DerValue(DerValue.tag_OctetString, encodedPoint) |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
215 |
.toByteArray(); |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
216 |
} catch (IOException e) { |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
217 |
throw new |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
218 |
IllegalArgumentException("Could not DER encode point", e); |
f8a00c400655
7099228: Use a PKCS11 config attribute to control encoding of an EC point
vinnie
parents:
10336
diff
changeset
|
219 |
} |
4809
c00eed67999d
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents:
2
diff
changeset
|
220 |
} |
c00eed67999d
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents:
2
diff
changeset
|
221 |
|
2 | 222 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
223 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY), |
|
224 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC), |
|
225 |
new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint), |
|
226 |
new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams), |
|
227 |
}; |
|
228 |
attributes = token.getAttributes |
|
229 |
(O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes); |
|
230 |
Session session = null; |
|
231 |
try { |
|
232 |
session = token.getObjSession(); |
|
233 |
long keyID = token.p11.C_CreateObject(session.id(), attributes); |
|
234 |
return P11Key.publicKey |
|
235 |
(session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes); |
|
236 |
} finally { |
|
237 |
token.releaseSession(session); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params) throws PKCS11Exception { |
|
242 |
byte[] encodedParams = ECParameters.encodeParameters(params); |
|
243 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
|
244 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY), |
|
245 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC), |
|
246 |
new CK_ATTRIBUTE(CKA_VALUE, s), |
|
247 |
new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams), |
|
248 |
}; |
|
249 |
attributes = token.getAttributes |
|
250 |
(O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attributes); |
|
251 |
Session session = null; |
|
252 |
try { |
|
253 |
session = token.getObjSession(); |
|
254 |
long keyID = token.p11.C_CreateObject(session.id(), attributes); |
|
255 |
return P11Key.privateKey |
|
256 |
(session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes); |
|
257 |
} finally { |
|
258 |
token.releaseSession(session); |
|
259 |
} |
|
260 |
} |
|
261 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
262 |
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
263 |
Session[] session) throws PKCS11Exception, InvalidKeySpecException { |
2 | 264 |
if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) { |
265 |
session[0] = token.getObjSession(); |
|
266 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
|
267 |
new CK_ATTRIBUTE(CKA_EC_POINT), |
|
268 |
new CK_ATTRIBUTE(CKA_EC_PARAMS), |
|
269 |
}; |
|
270 |
token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes); |
|
271 |
try { |
|
272 |
ECParameterSpec params = decodeParameters(attributes[1].getByteArray()); |
|
273 |
ECPoint point = decodePoint(attributes[0].getByteArray(), params.getCurve()); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
274 |
return keySpec.cast(new ECPublicKeySpec(point, params)); |
2 | 275 |
} catch (IOException e) { |
276 |
throw new InvalidKeySpecException("Could not parse key", e); |
|
277 |
} |
|
278 |
} else { // X.509 handled in superclass |
|
279 |
throw new InvalidKeySpecException("Only ECPublicKeySpec and " |
|
280 |
+ "X509EncodedKeySpec supported for EC public keys"); |
|
281 |
} |
|
282 |
} |
|
283 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
284 |
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
285 |
Session[] session) throws PKCS11Exception, InvalidKeySpecException { |
2 | 286 |
if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) { |
287 |
session[0] = token.getObjSession(); |
|
288 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
|
289 |
new CK_ATTRIBUTE(CKA_VALUE), |
|
290 |
new CK_ATTRIBUTE(CKA_EC_PARAMS), |
|
291 |
}; |
|
292 |
token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes); |
|
293 |
try { |
|
294 |
ECParameterSpec params = decodeParameters(attributes[1].getByteArray()); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
295 |
return keySpec.cast( |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
296 |
new ECPrivateKeySpec(attributes[0].getBigInteger(), params)); |
2 | 297 |
} catch (IOException e) { |
298 |
throw new InvalidKeySpecException("Could not parse key", e); |
|
299 |
} |
|
300 |
} else { // PKCS#8 handled in superclass |
|
301 |
throw new InvalidKeySpecException("Only ECPrivateKeySpec " |
|
302 |
+ "and PKCS8EncodedKeySpec supported for EC private keys"); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
KeyFactory implGetSoftwareFactory() throws GeneralSecurityException { |
|
307 |
return sun.security.ec.ECKeyFactory.INSTANCE; |
|
308 |
} |
|
309 |
||
310 |
} |