• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 /*
3  * Copyright (C) 2012 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.org.conscrypt;
19 
20 import java.math.BigInteger;
21 import java.security.InvalidKeyException;
22 import java.security.Key;
23 import java.security.KeyFactorySpi;
24 import java.security.PrivateKey;
25 import java.security.PublicKey;
26 import java.security.interfaces.RSAPrivateCrtKey;
27 import java.security.interfaces.RSAPrivateKey;
28 import java.security.interfaces.RSAPublicKey;
29 import java.security.spec.InvalidKeySpecException;
30 import java.security.spec.KeySpec;
31 import java.security.spec.PKCS8EncodedKeySpec;
32 import java.security.spec.RSAPrivateCrtKeySpec;
33 import java.security.spec.RSAPrivateKeySpec;
34 import java.security.spec.RSAPublicKeySpec;
35 import java.security.spec.X509EncodedKeySpec;
36 
37 /**
38  * An implementation of {@link java.security.KeyFactory} which uses BoringSSL to perform all the
39  * operations.
40  * @hide This class is not part of the Android public SDK API
41  */
42 @Internal
43 public final class OpenSSLRSAKeyFactory extends KeyFactorySpi {
OpenSSLRSAKeyFactory()44     public OpenSSLRSAKeyFactory() {}
45 
46     @Override
engineGeneratePublic(KeySpec keySpec)47     protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
48         if (keySpec == null) {
49             throw new InvalidKeySpecException("keySpec == null");
50         }
51 
52         if (keySpec instanceof RSAPublicKeySpec) {
53             return new OpenSSLRSAPublicKey((RSAPublicKeySpec) keySpec);
54         } else if (keySpec instanceof X509EncodedKeySpec) {
55             return OpenSSLKey.getPublicKey((X509EncodedKeySpec) keySpec, NativeConstants.EVP_PKEY_RSA);
56         }
57         throw new InvalidKeySpecException("Must use RSAPublicKeySpec or X509EncodedKeySpec; was "
58                 + keySpec.getClass().getName());
59     }
60 
61     @Override
engineGeneratePrivate(KeySpec keySpec)62     protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
63         if (keySpec == null) {
64             throw new InvalidKeySpecException("keySpec == null");
65         }
66 
67         if (keySpec instanceof RSAPrivateCrtKeySpec) {
68             return new OpenSSLRSAPrivateCrtKey((RSAPrivateCrtKeySpec) keySpec);
69         } else if (keySpec instanceof RSAPrivateKeySpec) {
70             return new OpenSSLRSAPrivateKey((RSAPrivateKeySpec) keySpec);
71         } else if (keySpec instanceof PKCS8EncodedKeySpec) {
72             return OpenSSLKey.getPrivateKey((PKCS8EncodedKeySpec) keySpec,
73                     NativeConstants.EVP_PKEY_RSA);
74         }
75         throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was "
76                 + keySpec.getClass().getName());
77     }
78 
79     @Override
engineGetKeySpec(Key key, Class<T> keySpec)80     protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
81             throws InvalidKeySpecException {
82         if (key == null) {
83             throw new InvalidKeySpecException("key == null");
84         }
85 
86         if (keySpec == null) {
87             throw new InvalidKeySpecException("keySpec == null");
88         }
89 
90         if (!"RSA".equals(key.getAlgorithm())) {
91             throw new InvalidKeySpecException("Key must be a RSA key");
92         }
93 
94         if (key instanceof RSAPublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
95             RSAPublicKey rsaKey = (RSAPublicKey) key;
96             @SuppressWarnings("unchecked")
97             T result = (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
98             return result;
99         } else if (key instanceof PublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
100             final byte[] encoded = key.getEncoded();
101             if (!"X.509".equals(key.getFormat()) || encoded == null) {
102                 throw new InvalidKeySpecException("Not a valid X.509 encoding");
103             }
104             RSAPublicKey rsaKey =
105                     (RSAPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
106             @SuppressWarnings("unchecked")
107             T result = (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
108             return result;
109         } else if (key instanceof RSAPrivateCrtKey
110                 && RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
111             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
112             @SuppressWarnings("unchecked")
113             T result = (T) new RSAPrivateCrtKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent(),
114                     rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
115                     rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(),
116                     rsaKey.getCrtCoefficient());
117             return result;
118         } else if (key instanceof RSAPrivateCrtKey
119                 && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
120             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
121             @SuppressWarnings("unchecked")
122             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
123             return result;
124         } else if (key instanceof RSAPrivateKey
125                 && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
126             RSAPrivateKey rsaKey = (RSAPrivateKey) key;
127             @SuppressWarnings("unchecked")
128             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
129             return result;
130         } else if (key instanceof PrivateKey
131                 && RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
132             final byte[] encoded = key.getEncoded();
133             if (!"PKCS#8".equals(key.getFormat()) || encoded == null) {
134                 throw new InvalidKeySpecException("Not a valid PKCS#8 encoding");
135             }
136             RSAPrivateKey privKey =
137                     (RSAPrivateKey) engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
138             if (privKey instanceof RSAPrivateCrtKey) {
139                 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) privKey;
140                 @SuppressWarnings("unchecked")
141                 T result = (T) new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
142                         rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(),
143                         rsaKey.getPrimeQ(), rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(),
144                         rsaKey.getCrtCoefficient());
145                 return result;
146             } else {
147                 throw new InvalidKeySpecException("Encoded key is not an RSAPrivateCrtKey");
148             }
149         } else if (key instanceof PrivateKey && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
150             final byte[] encoded = key.getEncoded();
151             if (!"PKCS#8".equals(key.getFormat()) || encoded == null) {
152                 throw new InvalidKeySpecException("Not a valid PKCS#8 encoding");
153             }
154             RSAPrivateKey rsaKey =
155                     (RSAPrivateKey) engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
156             @SuppressWarnings("unchecked")
157             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
158             return result;
159         } else if (key instanceof PrivateKey
160                 && PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
161             final byte[] encoded = key.getEncoded();
162             if (!"PKCS#8".equals(key.getFormat())) {
163                 throw new InvalidKeySpecException("Encoding type must be PKCS#8; was "
164                         + key.getFormat());
165             } else if (encoded == null) {
166                 throw new InvalidKeySpecException("Key is not encodable");
167             }
168             @SuppressWarnings("unchecked") T result = (T) new PKCS8EncodedKeySpec(encoded);
169             return result;
170         } else if (key instanceof PublicKey && X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
171             final byte[] encoded = key.getEncoded();
172             if (!"X.509".equals(key.getFormat())) {
173                 throw new InvalidKeySpecException("Encoding type must be X.509; was "
174                         + key.getFormat());
175             } else if (encoded == null) {
176                 throw new InvalidKeySpecException("Key is not encodable");
177             }
178             @SuppressWarnings("unchecked") T result = (T) new X509EncodedKeySpec(encoded);
179             return result;
180         } else {
181             throw new InvalidKeySpecException("Unsupported key type and key spec combination; key="
182                     + key.getClass().getName() + ", keySpec=" + keySpec.getName());
183         }
184     }
185 
186     @Override
engineTranslateKey(Key key)187     protected Key engineTranslateKey(Key key) throws InvalidKeyException {
188         if (key == null) {
189             throw new InvalidKeyException("key == null");
190         }
191 
192         if ((key instanceof OpenSSLRSAPublicKey) || (key instanceof OpenSSLRSAPrivateKey)) {
193             return key;
194         } else if (key instanceof RSAPublicKey) {
195             RSAPublicKey rsaKey = (RSAPublicKey) key;
196 
197             try {
198                 return engineGeneratePublic(new RSAPublicKeySpec(rsaKey.getModulus(),
199                         rsaKey.getPublicExponent()));
200             } catch (InvalidKeySpecException e) {
201                 throw new InvalidKeyException(e);
202             }
203         } else if (key instanceof RSAPrivateCrtKey) {
204             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
205             BigInteger modulus = rsaKey.getModulus();
206             BigInteger publicExponent = rsaKey.getPublicExponent();
207             BigInteger privateExponent = rsaKey.getPrivateExponent();
208             BigInteger primeP = rsaKey.getPrimeP();
209             BigInteger primeQ = rsaKey.getPrimeQ();
210             BigInteger primeExponentP = rsaKey.getPrimeExponentP();
211             BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
212             BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
213 
214             try {
215                 return engineGeneratePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent,
216                         privateExponent, primeP, primeQ, primeExponentP, primeExponentQ,
217                         crtCoefficient));
218             } catch (InvalidKeySpecException e) {
219                 throw new InvalidKeyException(e);
220             }
221         } else if (key instanceof RSAPrivateKey) {
222             RSAPrivateKey rsaKey = (RSAPrivateKey) key;
223             BigInteger modulus = rsaKey.getModulus();
224             BigInteger privateExponent = rsaKey.getPrivateExponent();
225 
226             try {
227                 return engineGeneratePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
228             } catch (InvalidKeySpecException e) {
229                 throw new InvalidKeyException(e);
230             }
231         } else if ((key instanceof PrivateKey) && "PKCS#8".equals(key.getFormat())) {
232             byte[] encoded = key.getEncoded();
233             if (encoded == null) {
234                 throw new InvalidKeyException("Key does not support encoding");
235             }
236             try {
237                 return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
238             } catch (InvalidKeySpecException e) {
239                 throw new InvalidKeyException(e);
240             }
241         } else if ((key instanceof PublicKey) && "X.509".equals(key.getFormat())) {
242             byte[] encoded = key.getEncoded();
243             if (encoded == null) {
244                 throw new InvalidKeyException("Key does not support encoding");
245             }
246             try {
247                 return engineGeneratePublic(new X509EncodedKeySpec(encoded));
248             } catch (InvalidKeySpecException e) {
249                 throw new InvalidKeyException(e);
250             }
251         } else {
252             throw new InvalidKeyException("Key must be an RSA public or private key; was "
253                     + key.getClass().getName());
254         }
255     }
256 }
257