1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.security.keystore;
18 
19 import java.security.Provider;
20 
21 /**
22  * {@link Provider} of JCA crypto operations operating on Android KeyStore keys.
23  *
24  * <p>This provider was separated out of {@link AndroidKeyStoreProvider} to work around the issue
25  * that Bouncy Castle provider incorrectly declares that it accepts arbitrary keys (incl. Android
26  * KeyStore ones). This causes JCA to select the Bouncy Castle's implementation of JCA crypto
27  * operations for Android KeyStore keys unless Android KeyStore's own implementations are installed
28  * as higher-priority than Bouncy Castle ones. The purpose of this provider is to do just that: to
29  * offer crypto operations operating on Android KeyStore keys and to be installed at higher priority
30  * than the Bouncy Castle provider.
31  *
32  * <p>Once Bouncy Castle provider is fixed, this provider can be merged into the
33  * {@code AndroidKeyStoreProvider}.
34  *
35  * @hide
36  */
37 class AndroidKeyStoreBCWorkaroundProvider extends Provider {
38 
39     // IMPLEMENTATION NOTE: Class names are hard-coded in this provider to avoid loading these
40     // classes when this provider is instantiated and installed early on during each app's
41     // initialization process.
42 
43     private static final String PACKAGE_NAME = "android.security.keystore";
44     private static final String KEYSTORE_SECRET_KEY_CLASS_NAME =
45             PACKAGE_NAME + ".AndroidKeyStoreSecretKey";
46     private static final String KEYSTORE_PRIVATE_KEY_CLASS_NAME =
47             PACKAGE_NAME + ".AndroidKeyStorePrivateKey";
48     private static final String KEYSTORE_PUBLIC_KEY_CLASS_NAME =
49             PACKAGE_NAME + ".AndroidKeyStorePublicKey";
50 
AndroidKeyStoreBCWorkaroundProvider()51     AndroidKeyStoreBCWorkaroundProvider() {
52         super("AndroidKeyStoreBCWorkaround",
53                 1.0,
54                 "Android KeyStore security provider to work around Bouncy Castle");
55 
56         // --------------------- javax.crypto.Mac
57         putMacImpl("HmacSHA1", PACKAGE_NAME + ".AndroidKeyStoreHmacSpi$HmacSHA1");
58         put("Alg.Alias.Mac.1.2.840.113549.2.7", "HmacSHA1");
59         put("Alg.Alias.Mac.HMAC-SHA1", "HmacSHA1");
60         put("Alg.Alias.Mac.HMAC/SHA1", "HmacSHA1");
61 
62         putMacImpl("HmacSHA224", PACKAGE_NAME + ".AndroidKeyStoreHmacSpi$HmacSHA224");
63         put("Alg.Alias.Mac.1.2.840.113549.2.9", "HmacSHA224");
64         put("Alg.Alias.Mac.HMAC-SHA224", "HmacSHA224");
65         put("Alg.Alias.Mac.HMAC/SHA224", "HmacSHA224");
66 
67         putMacImpl("HmacSHA256", PACKAGE_NAME + ".AndroidKeyStoreHmacSpi$HmacSHA256");
68         put("Alg.Alias.Mac.1.2.840.113549.2.9", "HmacSHA256");
69         put("Alg.Alias.Mac.HMAC-SHA256", "HmacSHA256");
70         put("Alg.Alias.Mac.HMAC/SHA256", "HmacSHA256");
71 
72         putMacImpl("HmacSHA384", PACKAGE_NAME + ".AndroidKeyStoreHmacSpi$HmacSHA384");
73         put("Alg.Alias.Mac.1.2.840.113549.2.10", "HmacSHA384");
74         put("Alg.Alias.Mac.HMAC-SHA384", "HmacSHA384");
75         put("Alg.Alias.Mac.HMAC/SHA384", "HmacSHA384");
76 
77         putMacImpl("HmacSHA512", PACKAGE_NAME + ".AndroidKeyStoreHmacSpi$HmacSHA512");
78         put("Alg.Alias.Mac.1.2.840.113549.2.11", "HmacSHA512");
79         put("Alg.Alias.Mac.HMAC-SHA512", "HmacSHA512");
80         put("Alg.Alias.Mac.HMAC/SHA512", "HmacSHA512");
81 
82         // --------------------- javax.crypto.Cipher
83         putSymmetricCipherImpl("AES/ECB/NoPadding",
84                 PACKAGE_NAME + ".AndroidKeyStoreUnauthenticatedAESCipherSpi$ECB$NoPadding");
85         putSymmetricCipherImpl("AES/ECB/PKCS7Padding",
86                 PACKAGE_NAME + ".AndroidKeyStoreUnauthenticatedAESCipherSpi$ECB$PKCS7Padding");
87 
88         putSymmetricCipherImpl("AES/CBC/NoPadding",
89                 PACKAGE_NAME + ".AndroidKeyStoreUnauthenticatedAESCipherSpi$CBC$NoPadding");
90         putSymmetricCipherImpl("AES/CBC/PKCS7Padding",
91                 PACKAGE_NAME + ".AndroidKeyStoreUnauthenticatedAESCipherSpi$CBC$PKCS7Padding");
92 
93         putSymmetricCipherImpl("AES/CTR/NoPadding",
94                 PACKAGE_NAME + ".AndroidKeyStoreUnauthenticatedAESCipherSpi$CTR$NoPadding");
95 
96         putSymmetricCipherImpl("AES/GCM/NoPadding",
97                 PACKAGE_NAME + ".AndroidKeyStoreAuthenticatedAESCipherSpi$GCM$NoPadding");
98 
99         putAsymmetricCipherImpl("RSA/ECB/NoPadding",
100                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$NoPadding");
101         put("Alg.Alias.Cipher.RSA/None/NoPadding", "RSA/ECB/NoPadding");
102         putAsymmetricCipherImpl("RSA/ECB/PKCS1Padding",
103                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$PKCS1Padding");
104         put("Alg.Alias.Cipher.RSA/None/PKCS1Padding", "RSA/ECB/PKCS1Padding");
105         putAsymmetricCipherImpl("RSA/ECB/OAEPPadding",
106                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA1AndMGF1Padding");
107         put("Alg.Alias.Cipher.RSA/None/OAEPPadding", "RSA/ECB/OAEPPadding");
108         putAsymmetricCipherImpl("RSA/ECB/OAEPWithSHA-1AndMGF1Padding",
109                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA1AndMGF1Padding");
110         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-1AndMGF1Padding",
111                 "RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
112         putAsymmetricCipherImpl("RSA/ECB/OAEPWithSHA-224AndMGF1Padding",
113                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA224AndMGF1Padding");
114         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-224AndMGF1Padding",
115                 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
116         putAsymmetricCipherImpl("RSA/ECB/OAEPWithSHA-256AndMGF1Padding",
117                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA256AndMGF1Padding");
118         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-256AndMGF1Padding",
119                 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
120         putAsymmetricCipherImpl("RSA/ECB/OAEPWithSHA-384AndMGF1Padding",
121                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA384AndMGF1Padding");
122         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-384AndMGF1Padding",
123                 "RSA/ECB/OAEPWithSHA-384AndMGF1Padding");
124         putAsymmetricCipherImpl("RSA/ECB/OAEPWithSHA-512AndMGF1Padding",
125                 PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA512AndMGF1Padding");
126         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-512AndMGF1Padding",
127                 "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
128 
129         // --------------------- java.security.Signature
130         putSignatureImpl("NONEwithRSA",
131                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$NONEWithPKCS1Padding");
132 
133         putSignatureImpl("MD5withRSA",
134                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$MD5WithPKCS1Padding");
135         put("Alg.Alias.Signature.MD5WithRSAEncryption", "MD5withRSA");
136         put("Alg.Alias.Signature.MD5/RSA", "MD5withRSA");
137         put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5withRSA");
138         put("Alg.Alias.Signature.1.2.840.113549.2.5with1.2.840.113549.1.1.1", "MD5withRSA");
139 
140         putSignatureImpl("SHA1withRSA",
141                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA1WithPKCS1Padding");
142         put("Alg.Alias.Signature.SHA1WithRSAEncryption", "SHA1withRSA");
143         put("Alg.Alias.Signature.SHA1/RSA", "SHA1withRSA");
144         put("Alg.Alias.Signature.SHA-1/RSA", "SHA1withRSA");
145         put("Alg.Alias.Signature.1.2.840.113549.1.1.5", "SHA1withRSA");
146         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.1", "SHA1withRSA");
147         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.5", "SHA1withRSA");
148         put("Alg.Alias.Signature.1.3.14.3.2.29", "SHA1withRSA");
149 
150         putSignatureImpl("SHA224withRSA",
151                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA224WithPKCS1Padding");
152         put("Alg.Alias.Signature.SHA224WithRSAEncryption", "SHA224withRSA");
153         put("Alg.Alias.Signature.1.2.840.113549.1.1.11", "SHA224withRSA");
154         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.1",
155                 "SHA224withRSA");
156         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.11",
157                 "SHA224withRSA");
158 
159         putSignatureImpl("SHA256withRSA",
160                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA256WithPKCS1Padding");
161         put("Alg.Alias.Signature.SHA256WithRSAEncryption", "SHA256withRSA");
162         put("Alg.Alias.Signature.1.2.840.113549.1.1.11", "SHA256withRSA");
163         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.1",
164                 "SHA256withRSA");
165         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.11",
166                 "SHA256withRSA");
167 
168         putSignatureImpl("SHA384withRSA",
169                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA384WithPKCS1Padding");
170         put("Alg.Alias.Signature.SHA384WithRSAEncryption", "SHA384withRSA");
171         put("Alg.Alias.Signature.1.2.840.113549.1.1.12", "SHA384withRSA");
172         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.113549.1.1.1",
173                 "SHA384withRSA");
174 
175         putSignatureImpl("SHA512withRSA",
176                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA512WithPKCS1Padding");
177         put("Alg.Alias.Signature.SHA512WithRSAEncryption", "SHA512withRSA");
178         put("Alg.Alias.Signature.1.2.840.113549.1.1.13", "SHA512withRSA");
179         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.113549.1.1.1",
180                 "SHA512withRSA");
181 
182         putSignatureImpl("SHA1withRSA/PSS",
183                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA1WithPSSPadding");
184         putSignatureImpl("SHA224withRSA/PSS",
185                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA224WithPSSPadding");
186         putSignatureImpl("SHA256withRSA/PSS",
187                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA256WithPSSPadding");
188         putSignatureImpl("SHA384withRSA/PSS",
189                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA384WithPSSPadding");
190         putSignatureImpl("SHA512withRSA/PSS",
191                 PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA512WithPSSPadding");
192 
193         putSignatureImpl("NONEwithECDSA",
194                 PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$NONE");
195 
196         putSignatureImpl("SHA1withECDSA", PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA1");
197         put("Alg.Alias.Signature.ECDSA", "SHA1withECDSA");
198         put("Alg.Alias.Signature.ECDSAwithSHA1", "SHA1withECDSA");
199         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA1(1)
200         put("Alg.Alias.Signature.1.2.840.10045.4.1", "SHA1withECDSA");
201         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.10045.2.1", "SHA1withECDSA");
202 
203         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
204         putSignatureImpl("SHA224withECDSA",
205                 PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA224");
206         // ecdsa-with-SHA224(1)
207         put("Alg.Alias.Signature.1.2.840.10045.4.3.1", "SHA224withECDSA");
208         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.10045.2.1", "SHA224withECDSA");
209 
210         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
211         putSignatureImpl("SHA256withECDSA",
212                 PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA256");
213         // ecdsa-with-SHA256(2)
214         put("Alg.Alias.Signature.1.2.840.10045.4.3.2", "SHA256withECDSA");
215         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.10045.2.1", "SHA256withECDSA");
216 
217         putSignatureImpl("SHA384withECDSA",
218                 PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA384");
219         // ecdsa-with-SHA384(3)
220         put("Alg.Alias.Signature.1.2.840.10045.4.3.3", "SHA384withECDSA");
221         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.10045.2.1", "SHA384withECDSA");
222 
223         putSignatureImpl("SHA512withECDSA",
224                 PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA512");
225         // ecdsa-with-SHA512(4)
226         put("Alg.Alias.Signature.1.2.840.10045.4.3.4", "SHA512withECDSA");
227         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.10045.2.1", "SHA512withECDSA");
228     }
229 
putMacImpl(String algorithm, String implClass)230     private void putMacImpl(String algorithm, String implClass) {
231         put("Mac." + algorithm, implClass);
232         put("Mac." + algorithm + " SupportedKeyClasses", KEYSTORE_SECRET_KEY_CLASS_NAME);
233     }
234 
putSymmetricCipherImpl(String transformation, String implClass)235     private void putSymmetricCipherImpl(String transformation, String implClass) {
236         put("Cipher." + transformation, implClass);
237         put("Cipher." + transformation + " SupportedKeyClasses", KEYSTORE_SECRET_KEY_CLASS_NAME);
238     }
239 
putAsymmetricCipherImpl(String transformation, String implClass)240     private void putAsymmetricCipherImpl(String transformation, String implClass) {
241         put("Cipher." + transformation, implClass);
242         put("Cipher." + transformation + " SupportedKeyClasses",
243                 KEYSTORE_PRIVATE_KEY_CLASS_NAME + "|" + KEYSTORE_PUBLIC_KEY_CLASS_NAME);
244     }
245 
putSignatureImpl(String algorithm, String implClass)246     private void putSignatureImpl(String algorithm, String implClass) {
247         put("Signature." + algorithm, implClass);
248         put("Signature." + algorithm + " SupportedKeyClasses",
249                 KEYSTORE_PRIVATE_KEY_CLASS_NAME + "|" + KEYSTORE_PUBLIC_KEY_CLASS_NAME);
250     }
251 
getSupportedEcdsaSignatureDigests()252     public static String[] getSupportedEcdsaSignatureDigests() {
253         return new String[] {"NONE", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"};
254     }
255 
getSupportedRsaSignatureWithPkcs1PaddingDigests()256     public static String[] getSupportedRsaSignatureWithPkcs1PaddingDigests() {
257         return new String[] {"NONE", "MD5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"};
258     }
259 }
260