History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/pkcs11/P11RSAKeyFactory.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/pkcs11/P11RSAKeyFactory.java+331−0
@@ -0,0 +1,331 @@
1/*
2 * Copyright (c) 2003, 2008, 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
26package sun.security.pkcs11;
27
28import java.math.BigInteger;
29
30import java.security.*;
31import java.security.interfaces.*;
32import java.security.spec.*;
33
34import static sun.security.pkcs11.TemplateManager.*;
35import sun.security.pkcs11.wrapper.*;
36import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
37
38import sun.security.rsa.RSAKeyFactory;
39
40/**
41 * RSA KeyFactory implemenation.
42 *
43 * @author Andreas Sterbenz
44 * @since 1.5
45 */
46final class P11RSAKeyFactory extends P11KeyFactory {
47
48 P11RSAKeyFactory(Token token, String algorithm) {
49 super(token, algorithm);
50 }
51
52 PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
53 try {
54 if (key instanceof RSAPublicKey) {
55 RSAPublicKey rsaKey = (RSAPublicKey)key;
56 return generatePublic(
57 rsaKey.getModulus(),
58 rsaKey.getPublicExponent()
59 );
60 } else if ("X.509".equals(key.getFormat())) {
61 // let SunRsaSign provider parse for us, then recurse
62 byte[] encoded = key.getEncoded();
63 key = new sun.security.rsa.RSAPublicKeyImpl(encoded);
64 return implTranslatePublicKey(key);
65 } else {
66 throw new InvalidKeyException("PublicKey must be instance "
67 + "of RSAPublicKey or have X.509 encoding");
68 }
69 } catch (PKCS11Exception e) {
70 throw new InvalidKeyException("Could not create RSA public key", e);
71 }
72 }
73
74 PrivateKey implTranslatePrivateKey(PrivateKey key)
75 throws InvalidKeyException {
76 try {
77 if (key instanceof RSAPrivateCrtKey) {
78 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
79 return generatePrivate(
80 rsaKey.getModulus(),
81 rsaKey.getPublicExponent(),
82 rsaKey.getPrivateExponent(),
83 rsaKey.getPrimeP(),
84 rsaKey.getPrimeQ(),
85 rsaKey.getPrimeExponentP(),
86 rsaKey.getPrimeExponentQ(),
87 rsaKey.getCrtCoefficient()
88 );
89 } else if (key instanceof RSAPrivateKey) {
90 RSAPrivateKey rsaKey = (RSAPrivateKey)key;
91 return generatePrivate(
92 rsaKey.getModulus(),
93 rsaKey.getPrivateExponent()
94 );
95 } else if ("PKCS#8".equals(key.getFormat())) {
96 // let SunRsaSign provider parse for us, then recurse
97 byte[] encoded = key.getEncoded();
98 key = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
99 return implTranslatePrivateKey(key);
100 } else {
101 throw new InvalidKeyException("Private key must be instance "
102 + "of RSAPrivate(Crt)Key or have PKCS#8 encoding");
103 }
104 } catch (PKCS11Exception e) {
105 throw new InvalidKeyException("Could not create RSA private key", e);
106 }
107 }
108
109 // see JCA spec
110 protected PublicKey engineGeneratePublic(KeySpec keySpec)
111 throws InvalidKeySpecException {
112 token.ensureValid();
113 if (keySpec instanceof X509EncodedKeySpec) {
114 try {
115 byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();
116 PublicKey key = new sun.security.rsa.RSAPublicKeyImpl(encoded);
117 return implTranslatePublicKey(key);
118 } catch (InvalidKeyException e) {
119 throw new InvalidKeySpecException
120 ("Could not create RSA public key", e);
121 }
122 }
123 if (keySpec instanceof RSAPublicKeySpec == false) {
124 throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
125 + "X509EncodedKeySpec supported for RSA public keys");
126 }
127 try {
128 RSAPublicKeySpec rs = (RSAPublicKeySpec)keySpec;
129 return generatePublic(
130 rs.getModulus(),
131 rs.getPublicExponent()
132 );
133 } catch (PKCS11Exception e) {
134 throw new InvalidKeySpecException
135 ("Could not create RSA public key", e);
136 } catch (InvalidKeyException e) {
137 throw new InvalidKeySpecException
138 ("Could not create RSA public key", e);
139 }
140 }
141
142 // see JCA spec
143 protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
144 throws InvalidKeySpecException {
145 token.ensureValid();
146 if (keySpec instanceof PKCS8EncodedKeySpec) {
147 try {
148 byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
149 PrivateKey key =
150 sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
151 return implTranslatePrivateKey(key);
152 } catch (GeneralSecurityException e) {
153 throw new InvalidKeySpecException
154 ("Could not create RSA private key", e);
155 }
156 }
157 try {
158 if (keySpec instanceof RSAPrivateCrtKeySpec) {
159 RSAPrivateCrtKeySpec rs = (RSAPrivateCrtKeySpec)keySpec;
160 return generatePrivate(
161 rs.getModulus(),
162 rs.getPublicExponent(),
163 rs.getPrivateExponent(),
164 rs.getPrimeP(),
165 rs.getPrimeQ(),
166 rs.getPrimeExponentP(),
167 rs.getPrimeExponentQ(),
168 rs.getCrtCoefficient()
169 );
170 } else if (keySpec instanceof RSAPrivateKeySpec) {
171 RSAPrivateKeySpec rs = (RSAPrivateKeySpec)keySpec;
172 return generatePrivate(
173 rs.getModulus(),
174 rs.getPrivateExponent()
175 );
176 } else {
177 throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
178 + "and PKCS8EncodedKeySpec supported for RSA private keys");
179 }
180 } catch (PKCS11Exception e) {
181 throw new InvalidKeySpecException
182 ("Could not create RSA private key", e);
183 } catch (InvalidKeyException e) {
184 throw new InvalidKeySpecException
185 ("Could not create RSA private key", e);
186 }
187 }
188
189 private PublicKey generatePublic(BigInteger n, BigInteger e)
190 throws PKCS11Exception, InvalidKeyException {
191 RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
192 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
193 new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
194 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
195 new CK_ATTRIBUTE(CKA_MODULUS, n),
196 new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
197 };
198 attributes = token.getAttributes
199 (O_IMPORT, CKO_PUBLIC_KEY, CKK_RSA, attributes);
200 Session session = null;
201 try {
202 session = token.getObjSession();
203 long keyID = token.p11.C_CreateObject(session.id(), attributes);
204 return P11Key.publicKey
205 (session, keyID, "RSA", n.bitLength(), attributes);
206 } finally {
207 token.releaseSession(session);
208 }
209 }
210
211 private PrivateKey generatePrivate(BigInteger n, BigInteger d)
212 throws PKCS11Exception, InvalidKeyException {
213 RSAKeyFactory.checkKeyLengths(n.bitLength(), null, -1, 64 * 1024);
214 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
215 new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
216 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
217 new CK_ATTRIBUTE(CKA_MODULUS, n),
218 new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
219 };
220 attributes = token.getAttributes
221 (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
222 Session session = null;
223 try {
224 session = token.getObjSession();
225 long keyID = token.p11.C_CreateObject(session.id(), attributes);
226 return P11Key.privateKey
227 (session, keyID, "RSA", n.bitLength(), attributes);
228 } finally {
229 token.releaseSession(session);
230 }
231 }
232
233 private PrivateKey generatePrivate(BigInteger n, BigInteger e,
234 BigInteger d, BigInteger p, BigInteger q, BigInteger pe,
235 BigInteger qe, BigInteger coeff) throws PKCS11Exception,
236 InvalidKeyException {
237 RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
238 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
239 new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
240 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
241 new CK_ATTRIBUTE(CKA_MODULUS, n),
242 new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
243 new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
244 new CK_ATTRIBUTE(CKA_PRIME_1, p),
245 new CK_ATTRIBUTE(CKA_PRIME_2, q),
246 new CK_ATTRIBUTE(CKA_EXPONENT_1, pe),
247 new CK_ATTRIBUTE(CKA_EXPONENT_2, qe),
248 new CK_ATTRIBUTE(CKA_COEFFICIENT, coeff),
249 };
250 attributes = token.getAttributes
251 (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
252 Session session = null;
253 try {
254 session = token.getObjSession();
255 long keyID = token.p11.C_CreateObject(session.id(), attributes);
256 return P11Key.privateKey
257 (session, keyID, "RSA", n.bitLength(), attributes);
258 } finally {
259 token.releaseSession(session);
260 }
261 }
262
263 KeySpec implGetPublicKeySpec(P11Key key, Class keySpec, Session[] session)
264 throws PKCS11Exception, InvalidKeySpecException {
265 if (RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
266 session[0] = token.getObjSession();
267 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
268 new CK_ATTRIBUTE(CKA_MODULUS),
269 new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
270 };
271 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
272 KeySpec spec = new RSAPublicKeySpec(
273 attributes[0].getBigInteger(),
274 attributes[1].getBigInteger()
275 );
276 return spec;
277 } else { // X.509 handled in superclass
278 throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
279 + "X509EncodedKeySpec supported for RSA public keys");
280 }
281 }
282
283 KeySpec implGetPrivateKeySpec(P11Key key, Class keySpec, Session[] session)
284 throws PKCS11Exception, InvalidKeySpecException {
285 if (RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
286 session[0] = token.getObjSession();
287 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
288 new CK_ATTRIBUTE(CKA_MODULUS),
289 new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
290 new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
291 new CK_ATTRIBUTE(CKA_PRIME_1),
292 new CK_ATTRIBUTE(CKA_PRIME_2),
293 new CK_ATTRIBUTE(CKA_EXPONENT_1),
294 new CK_ATTRIBUTE(CKA_EXPONENT_2),
295 new CK_ATTRIBUTE(CKA_COEFFICIENT),
296 };
297 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
298 KeySpec spec = new RSAPrivateCrtKeySpec(
299 attributes[0].getBigInteger(),
300 attributes[1].getBigInteger(),
301 attributes[2].getBigInteger(),
302 attributes[3].getBigInteger(),
303 attributes[4].getBigInteger(),
304 attributes[5].getBigInteger(),
305 attributes[6].getBigInteger(),
306 attributes[7].getBigInteger()
307 );
308 return spec;
309 } else if (RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
310 session[0] = token.getObjSession();
311 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
312 new CK_ATTRIBUTE(CKA_MODULUS),
313 new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
314 };
315 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
316 KeySpec spec = new RSAPrivateKeySpec(
317 attributes[0].getBigInteger(),
318 attributes[1].getBigInteger()
319 );
320 return spec;
321 } else { // PKCS#8 handled in superclass
322 throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
323 + "and PKCS8EncodedKeySpec supported for RSA private keys");
324 }
325 }
326
327 KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
328 return KeyFactory.getInstance("RSA", P11Util.getSunRsaSignProvider());
329 }
330
331}