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 @libcore.api.IntraCoreApi
43 @Internal
44 public final class OpenSSLRSAKeyFactory extends KeyFactorySpi {
45     @libcore.api.IntraCoreApi
OpenSSLRSAKeyFactory()46     public OpenSSLRSAKeyFactory() {}
47 
48     @Override
engineGeneratePublic(KeySpec keySpec)49     protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
50         if (keySpec == null) {
51             throw new InvalidKeySpecException("keySpec == null");
52         }
53 
54         if (keySpec instanceof RSAPublicKeySpec) {
55             return new OpenSSLRSAPublicKey((RSAPublicKeySpec) keySpec);
56         } else if (keySpec instanceof X509EncodedKeySpec) {
57             return OpenSSLKey.getPublicKey((X509EncodedKeySpec) keySpec, NativeConstants.EVP_PKEY_RSA);
58         }
59         throw new InvalidKeySpecException("Must use RSAPublicKeySpec or X509EncodedKeySpec; was "
60                 + keySpec.getClass().getName());
61     }
62 
63     @Override
engineGeneratePrivate(KeySpec keySpec)64     protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
65         if (keySpec == null) {
66             throw new InvalidKeySpecException("keySpec == null");
67         }
68 
69         if (keySpec instanceof RSAPrivateCrtKeySpec) {
70             return new OpenSSLRSAPrivateCrtKey((RSAPrivateCrtKeySpec) keySpec);
71         } else if (keySpec instanceof RSAPrivateKeySpec) {
72             return new OpenSSLRSAPrivateKey((RSAPrivateKeySpec) keySpec);
73         } else if (keySpec instanceof PKCS8EncodedKeySpec) {
74             return OpenSSLKey.getPrivateKey((PKCS8EncodedKeySpec) keySpec,
75                     NativeConstants.EVP_PKEY_RSA);
76         }
77         throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was "
78                 + keySpec.getClass().getName());
79     }
80 
81     @Override
engineGetKeySpec(Key key, Class<T> keySpec)82     protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
83             throws InvalidKeySpecException {
84         if (key == null) {
85             throw new InvalidKeySpecException("key == null");
86         }
87 
88         if (keySpec == null) {
89             throw new InvalidKeySpecException("keySpec == null");
90         }
91 
92         if (!"RSA".equals(key.getAlgorithm())) {
93             throw new InvalidKeySpecException("Key must be a RSA key");
94         }
95 
96         if (key instanceof RSAPublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
97             RSAPublicKey rsaKey = (RSAPublicKey) key;
98             @SuppressWarnings("unchecked")
99             T result = (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
100             return result;
101         } else if (key instanceof PublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
102             final byte[] encoded = key.getEncoded();
103             if (!"X.509".equals(key.getFormat()) || encoded == null) {
104                 throw new InvalidKeySpecException("Not a valid X.509 encoding");
105             }
106             RSAPublicKey rsaKey =
107                     (RSAPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
108             @SuppressWarnings("unchecked")
109             T result = (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
110             return result;
111         } else if (key instanceof RSAPrivateCrtKey
112                 && RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
113             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
114             @SuppressWarnings("unchecked")
115             T result = (T) new RSAPrivateCrtKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent(),
116                     rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
117                     rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(),
118                     rsaKey.getCrtCoefficient());
119             return result;
120         } else if (key instanceof RSAPrivateCrtKey
121                 && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
122             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
123             @SuppressWarnings("unchecked")
124             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
125             return result;
126         } else if (key instanceof RSAPrivateKey
127                 && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
128             RSAPrivateKey rsaKey = (RSAPrivateKey) key;
129             @SuppressWarnings("unchecked")
130             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
131             return result;
132         } else if (key instanceof PrivateKey
133                 && RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
134             final byte[] encoded = key.getEncoded();
135             if (!"PKCS#8".equals(key.getFormat()) || encoded == null) {
136                 throw new InvalidKeySpecException("Not a valid PKCS#8 encoding");
137             }
138             RSAPrivateKey privKey =
139                     (RSAPrivateKey) engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
140             if (privKey instanceof RSAPrivateCrtKey) {
141                 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) privKey;
142                 @SuppressWarnings("unchecked")
143                 T result = (T) new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
144                         rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(),
145                         rsaKey.getPrimeQ(), rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(),
146                         rsaKey.getCrtCoefficient());
147                 return result;
148             } else {
149                 throw new InvalidKeySpecException("Encoded key is not an RSAPrivateCrtKey");
150             }
151         } else if (key instanceof PrivateKey && RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
152             final byte[] encoded = key.getEncoded();
153             if (!"PKCS#8".equals(key.getFormat()) || encoded == null) {
154                 throw new InvalidKeySpecException("Not a valid PKCS#8 encoding");
155             }
156             RSAPrivateKey rsaKey =
157                     (RSAPrivateKey) engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
158             @SuppressWarnings("unchecked")
159             T result = (T) new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
160             return result;
161         } else if (key instanceof PrivateKey
162                 && PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
163             final byte[] encoded = key.getEncoded();
164             if (!"PKCS#8".equals(key.getFormat())) {
165                 throw new InvalidKeySpecException("Encoding type must be PKCS#8; was "
166                         + key.getFormat());
167             } else if (encoded == null) {
168                 throw new InvalidKeySpecException("Key is not encodable");
169             }
170             @SuppressWarnings("unchecked") T result = (T) new PKCS8EncodedKeySpec(encoded);
171             return result;
172         } else if (key instanceof PublicKey && X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
173             final byte[] encoded = key.getEncoded();
174             if (!"X.509".equals(key.getFormat())) {
175                 throw new InvalidKeySpecException("Encoding type must be X.509; was "
176                         + key.getFormat());
177             } else if (encoded == null) {
178                 throw new InvalidKeySpecException("Key is not encodable");
179             }
180             @SuppressWarnings("unchecked") T result = (T) new X509EncodedKeySpec(encoded);
181             return result;
182         } else {
183             throw new InvalidKeySpecException("Unsupported key type and key spec combination; key="
184                     + key.getClass().getName() + ", keySpec=" + keySpec.getName());
185         }
186     }
187 
188     @Override
engineTranslateKey(Key key)189     protected Key engineTranslateKey(Key key) throws InvalidKeyException {
190         if (key == null) {
191             throw new InvalidKeyException("key == null");
192         }
193 
194         if ((key instanceof OpenSSLRSAPublicKey) || (key instanceof OpenSSLRSAPrivateKey)) {
195             return key;
196         } else if (key instanceof RSAPublicKey) {
197             RSAPublicKey rsaKey = (RSAPublicKey) key;
198 
199             try {
200                 return engineGeneratePublic(new RSAPublicKeySpec(rsaKey.getModulus(),
201                         rsaKey.getPublicExponent()));
202             } catch (InvalidKeySpecException e) {
203                 throw new InvalidKeyException(e);
204             }
205         } else if (key instanceof RSAPrivateCrtKey) {
206             RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
207             BigInteger modulus = rsaKey.getModulus();
208             BigInteger publicExponent = rsaKey.getPublicExponent();
209             BigInteger privateExponent = rsaKey.getPrivateExponent();
210             BigInteger primeP = rsaKey.getPrimeP();
211             BigInteger primeQ = rsaKey.getPrimeQ();
212             BigInteger primeExponentP = rsaKey.getPrimeExponentP();
213             BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
214             BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
215 
216             try {
217                 return engineGeneratePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent,
218                         privateExponent, primeP, primeQ, primeExponentP, primeExponentQ,
219                         crtCoefficient));
220             } catch (InvalidKeySpecException e) {
221                 throw new InvalidKeyException(e);
222             }
223         } else if (key instanceof RSAPrivateKey) {
224             RSAPrivateKey rsaKey = (RSAPrivateKey) key;
225             BigInteger modulus = rsaKey.getModulus();
226             BigInteger privateExponent = rsaKey.getPrivateExponent();
227 
228             try {
229                 return engineGeneratePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
230             } catch (InvalidKeySpecException e) {
231                 throw new InvalidKeyException(e);
232             }
233         } else if ((key instanceof PrivateKey) && "PKCS#8".equals(key.getFormat())) {
234             byte[] encoded = key.getEncoded();
235             if (encoded == null) {
236                 throw new InvalidKeyException("Key does not support encoding");
237             }
238             try {
239                 return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
240             } catch (InvalidKeySpecException e) {
241                 throw new InvalidKeyException(e);
242             }
243         } else if ((key instanceof PublicKey) && "X.509".equals(key.getFormat())) {
244             byte[] encoded = key.getEncoded();
245             if (encoded == null) {
246                 throw new InvalidKeyException("Key does not support encoding");
247             }
248             try {
249                 return engineGeneratePublic(new X509EncodedKeySpec(encoded));
250             } catch (InvalidKeySpecException e) {
251                 throw new InvalidKeyException(e);
252             }
253         } else {
254             throw new InvalidKeyException("Key must be an RSA public or private key; was "
255                     + key.getClass().getName());
256         }
257     }
258 }
259