1 /*
2 * Copyright (c) 2003, 2013, 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.pkcs11;
27
28 import java.math.BigInteger;
29
30 import java.security.*;
31 import java.security.interfaces.*;
32 import java.security.spec.*;
33
34 import static sun.security.pkcs11.TemplateManager.*;
35 import sun.security.pkcs11.wrapper.*;
36 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
37
38 import sun.security.rsa.RSAKeyFactory;
39
40 /**
41 * RSA KeyFactory implementation.
42 *
43 * @author Andreas Sterbenz
44 * @since 1.5
45 */
46 final 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(),
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 | InvalidKeyException e) {
134 throw new InvalidKeySpecException
135 ("Could not create RSA public key", e);
136 }
|
1 /*
2 * Copyright (c) 2003, 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.pkcs11;
27
28 import java.math.BigInteger;
29
30 import java.security.*;
31 import java.security.interfaces.*;
32 import java.security.spec.*;
33
34 import sun.security.rsa.RSAPublicKeyImpl;
35 import static sun.security.pkcs11.TemplateManager.*;
36 import sun.security.pkcs11.wrapper.*;
37 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
38
39 import sun.security.rsa.RSAKeyFactory;
40
41 /**
42 * RSA KeyFactory implementation.
43 *
44 * @author Andreas Sterbenz
45 * @since 1.5
46 */
47 final class P11RSAKeyFactory extends P11KeyFactory {
48
49 P11RSAKeyFactory(Token token, String algorithm) {
50 super(token, algorithm);
51 }
52
53 PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
54 try {
55 if (key instanceof RSAPublicKey) {
56 RSAPublicKey rsaKey = (RSAPublicKey)key;
57 return generatePublic(
58 rsaKey.getModulus(),
59 rsaKey.getPublicExponent()
60 );
61 } else if ("X.509".equals(key.getFormat())) {
62 // let SunRsaSign provider parse for us, then recurse
63 byte[] encoded = key.getEncoded();
64 key = RSAPublicKeyImpl.newKey(encoded);
65 return implTranslatePublicKey(key);
66 } else {
67 throw new InvalidKeyException("PublicKey must be instance "
68 + "of RSAPublicKey or have X.509 encoding");
69 }
70 } catch (PKCS11Exception e) {
71 throw new InvalidKeyException("Could not create RSA public key", e);
72 }
73 }
74
75 PrivateKey implTranslatePrivateKey(PrivateKey key)
76 throws InvalidKeyException {
77 try {
78 if (key instanceof RSAPrivateCrtKey) {
79 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
80 return generatePrivate(
81 rsaKey.getModulus(),
82 rsaKey.getPublicExponent(),
83 rsaKey.getPrivateExponent(),
84 rsaKey.getPrimeP(),
97 // let SunRsaSign provider parse for us, then recurse
98 byte[] encoded = key.getEncoded();
99 key = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
100 return implTranslatePrivateKey(key);
101 } else {
102 throw new InvalidKeyException("Private key must be instance "
103 + "of RSAPrivate(Crt)Key or have PKCS#8 encoding");
104 }
105 } catch (PKCS11Exception e) {
106 throw new InvalidKeyException("Could not create RSA private key", e);
107 }
108 }
109
110 // see JCA spec
111 protected PublicKey engineGeneratePublic(KeySpec keySpec)
112 throws InvalidKeySpecException {
113 token.ensureValid();
114 if (keySpec instanceof X509EncodedKeySpec) {
115 try {
116 byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();
117 PublicKey key = RSAPublicKeyImpl.newKey(encoded);
118 return implTranslatePublicKey(key);
119 } catch (InvalidKeyException e) {
120 throw new InvalidKeySpecException
121 ("Could not create RSA public key", e);
122 }
123 }
124 if (keySpec instanceof RSAPublicKeySpec == false) {
125 throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
126 + "X509EncodedKeySpec supported for RSA public keys");
127 }
128 try {
129 RSAPublicKeySpec rs = (RSAPublicKeySpec)keySpec;
130 return generatePublic(
131 rs.getModulus(),
132 rs.getPublicExponent()
133 );
134 } catch (PKCS11Exception | InvalidKeyException e) {
135 throw new InvalidKeySpecException
136 ("Could not create RSA public key", e);
137 }
|