1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 /*
3  * Copyright (C) 2010 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.security.Provider;
21 
22 /**
23  * Provider that uses BoringSSL to perform the actual cryptographic operations.
24  * <p>
25  * Every algorithm should have its IANA assigned OID as an alias. See the following URLs for each
26  * type: <ul> <li><a
27  * href="http://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xml">Hash
28  * functions</a></li> <li><a href="http://www.iana.org/assignments/dssc/dssc.xml">Signature
29  * algorithms</a></li> <li><a
30  * href="http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html">NIST cryptographic
31  * algorithms</a></li>
32  * </ul>
33  * @hide This class is not part of the Android public SDK API
34  */
35 @libcore.
36 api.IntraCoreApi
37 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
38 @Internal
39 public final class OpenSSLProvider extends Provider {
40     private static final long serialVersionUID = 2996752495318905136L;
41 
42     private static final String PREFIX = OpenSSLProvider.class.getPackage().getName() + ".";
43 
44     private static final String STANDARD_EC_PRIVATE_KEY_INTERFACE_CLASS_NAME =
45             "java.security.interfaces.ECPrivateKey";
46     private static final String STANDARD_XEC_PRIVATE_KEY_INTERFACE_CLASS_NAME =
47             "java.security.interfaces.XECPrivateKey";
48     private static final String STANDARD_RSA_PRIVATE_KEY_INTERFACE_CLASS_NAME =
49             "java.security.interfaces.RSAPrivateKey";
50     private static final String STANDARD_RSA_PUBLIC_KEY_INTERFACE_CLASS_NAME =
51             "java.security.interfaces.RSAPublicKey";
52 
53     @android.compat.annotation
54             .UnsupportedAppUsage
55             @libcore.api
56             .IntraCoreApi
57             @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
OpenSSLProvider()58             public OpenSSLProvider() {
59         this(Platform.getDefaultProviderName());
60     }
61 
OpenSSLProvider(String providerName)62     public OpenSSLProvider(String providerName) {
63         this(providerName, Platform.provideTrustManagerByDefault(), "TLSv1.3");
64     }
65 
OpenSSLProvider(String providerName, boolean includeTrustManager, String defaultTlsProtocol)66     OpenSSLProvider(String providerName, boolean includeTrustManager, String defaultTlsProtocol) {
67         super(providerName, 1.0, "Android's OpenSSL-backed security provider");
68 
69         // Ensure that the native library has been loaded.
70         NativeCrypto.checkAvailability();
71 
72         // Make sure the platform is initialized.
73         Platform.setup();
74 
75         /* === SSL Contexts === */
76         String classOpenSSLContextImpl = PREFIX + "OpenSSLContextImpl";
77         String tls12SSLContextSuffix = "$TLSv12";
78         String tls13SSLContextSuffix = "$TLSv13";
79         String defaultSSLContextSuffix;
80         switch (defaultTlsProtocol) {
81             case "TLSv1.2":
82                 defaultSSLContextSuffix = tls12SSLContextSuffix;
83                 break;
84             case "TLSv1.3":
85                 defaultSSLContextSuffix = tls13SSLContextSuffix;
86                 break;
87             default:
88                 throw new IllegalArgumentException(
89                         "Choice of default protocol is unsupported: " + defaultTlsProtocol);
90         }
91         // Keep SSL as an alias to TLS
92         put("SSLContext.SSL", classOpenSSLContextImpl + defaultSSLContextSuffix);
93         put("SSLContext.TLS", classOpenSSLContextImpl + defaultSSLContextSuffix);
94         put("SSLContext.TLSv1", classOpenSSLContextImpl + "$TLSv1");
95         put("SSLContext.TLSv1.1", classOpenSSLContextImpl + "$TLSv11");
96         put("SSLContext.TLSv1.2", classOpenSSLContextImpl + tls12SSLContextSuffix);
97         put("SSLContext.TLSv1.3", classOpenSSLContextImpl + tls13SSLContextSuffix);
98         put("SSLContext.Default", PREFIX + "DefaultSSLContextImpl" + defaultSSLContextSuffix);
99 
100         if (includeTrustManager) {
101             put("TrustManagerFactory.PKIX", TrustManagerFactoryImpl.class.getName());
102             put("Alg.Alias.TrustManagerFactory.X509", "PKIX");
103         }
104 
105         /* === AlgorithmParameters === */
106         put("AlgorithmParameters.AES", PREFIX + "IvParameters$AES");
107         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.2", "AES");
108         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.22", "AES");
109         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.42", "AES");
110 
111         put("AlgorithmParameters.ChaCha20", PREFIX + "IvParameters$ChaCha20");
112 
113         put("AlgorithmParameters.DESEDE", PREFIX + "IvParameters$DESEDE");
114         put("Alg.Alias.AlgorithmParameters.TDEA", "DESEDE");
115         put("Alg.Alias.AlgorithmParameters.1.2.840.113549.3.7", "DESEDE");
116 
117         put("AlgorithmParameters.GCM", PREFIX + "GCMParameters");
118         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.6", "GCM");
119         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.26", "GCM");
120         put("Alg.Alias.AlgorithmParameters.2.16.840.1.101.3.4.1.46", "GCM");
121         put("AlgorithmParameters.OAEP", PREFIX + "OAEPParameters");
122         put("AlgorithmParameters.PSS", PREFIX + "PSSParameters");
123         put("AlgorithmParameters.EC", PREFIX + "ECParameters");
124 
125         /* === Message Digests === */
126         put("MessageDigest.SHA-1", PREFIX + "OpenSSLMessageDigestJDK$SHA1");
127         put("Alg.Alias.MessageDigest.SHA1", "SHA-1");
128         put("Alg.Alias.MessageDigest.SHA", "SHA-1");
129         put("Alg.Alias.MessageDigest.1.3.14.3.2.26", "SHA-1");
130 
131         put("MessageDigest.SHA-224", PREFIX + "OpenSSLMessageDigestJDK$SHA224");
132         put("Alg.Alias.MessageDigest.SHA224", "SHA-224");
133         put("Alg.Alias.MessageDigest.2.16.840.1.101.3.4.2.4", "SHA-224");
134 
135         put("MessageDigest.SHA-256", PREFIX + "OpenSSLMessageDigestJDK$SHA256");
136         put("Alg.Alias.MessageDigest.SHA256", "SHA-256");
137         put("Alg.Alias.MessageDigest.2.16.840.1.101.3.4.2.1", "SHA-256");
138 
139         put("MessageDigest.SHA-384", PREFIX + "OpenSSLMessageDigestJDK$SHA384");
140         put("Alg.Alias.MessageDigest.SHA384", "SHA-384");
141         put("Alg.Alias.MessageDigest.2.16.840.1.101.3.4.2.2", "SHA-384");
142 
143         put("MessageDigest.SHA-512", PREFIX + "OpenSSLMessageDigestJDK$SHA512");
144         put("Alg.Alias.MessageDigest.SHA512", "SHA-512");
145         put("Alg.Alias.MessageDigest.2.16.840.1.101.3.4.2.3", "SHA-512");
146 
147         // iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) md5(5)
148         put("MessageDigest.MD5", PREFIX + "OpenSSLMessageDigestJDK$MD5");
149         put("Alg.Alias.MessageDigest.1.2.840.113549.2.5", "MD5");
150 
151         /* == KeyGenerators == */
152         put("KeyGenerator.ARC4", PREFIX + "KeyGeneratorImpl$ARC4");
153         put("Alg.Alias.KeyGenerator.RC4", "ARC4");
154         put("Alg.Alias.KeyGenerator.1.2.840.113549.3.4", "ARC4");
155 
156         put("KeyGenerator.AES", PREFIX + "KeyGeneratorImpl$AES");
157 
158         put("KeyGenerator.ChaCha20", PREFIX + "KeyGeneratorImpl$ChaCha20");
159 
160         put("KeyGenerator.DESEDE", PREFIX + "KeyGeneratorImpl$DESEDE");
161         put("Alg.Alias.KeyGenerator.TDEA", "DESEDE");
162 
163         put("KeyGenerator.HmacMD5", PREFIX + "KeyGeneratorImpl$HmacMD5");
164         put("Alg.Alias.KeyGenerator.1.3.6.1.5.5.8.1.1", "HmacMD5");
165         put("Alg.Alias.KeyGenerator.HMAC-MD5", "HmacMD5");
166         put("Alg.Alias.KeyGenerator.HMAC/MD5", "HmacMD5");
167 
168         put("KeyGenerator.HmacSHA1", PREFIX + "KeyGeneratorImpl$HmacSHA1");
169         put("Alg.Alias.KeyGenerator.1.2.840.113549.2.7", "HmacSHA1");
170         put("Alg.Alias.KeyGenerator.1.3.6.1.5.5.8.1.2", "HmacSHA1");
171         put("Alg.Alias.KeyGenerator.HMAC-SHA1", "HmacSHA1");
172         put("Alg.Alias.KeyGenerator.HMAC/SHA1", "HmacSHA1");
173 
174         put("KeyGenerator.HmacSHA224", PREFIX + "KeyGeneratorImpl$HmacSHA224");
175         put("Alg.Alias.KeyGenerator.1.2.840.113549.2.8", "HmacSHA224");
176         put("Alg.Alias.KeyGenerator.HMAC-SHA224", "HmacSHA224");
177         put("Alg.Alias.KeyGenerator.HMAC/SHA224", "HmacSHA224");
178 
179         put("KeyGenerator.HmacSHA256", PREFIX + "KeyGeneratorImpl$HmacSHA256");
180         put("Alg.Alias.KeyGenerator.1.2.840.113549.2.9", "HmacSHA256");
181         put("Alg.Alias.KeyGenerator.2.16.840.1.101.3.4.2.1", "HmacSHA256");
182         put("Alg.Alias.KeyGenerator.HMAC-SHA256", "HmacSHA256");
183         put("Alg.Alias.KeyGenerator.HMAC/SHA256", "HmacSHA256");
184 
185         put("KeyGenerator.HmacSHA384", PREFIX + "KeyGeneratorImpl$HmacSHA384");
186         put("Alg.Alias.KeyGenerator.1.2.840.113549.2.10", "HmacSHA384");
187         put("Alg.Alias.KeyGenerator.HMAC-SHA384", "HmacSHA384");
188         put("Alg.Alias.KeyGenerator.HMAC/SHA384", "HmacSHA384");
189 
190         put("KeyGenerator.HmacSHA512", PREFIX + "KeyGeneratorImpl$HmacSHA512");
191         put("Alg.Alias.KeyGenerator.1.2.840.113549.2.11", "HmacSHA512");
192         put("Alg.Alias.KeyGenerator.HMAC-SHA512", "HmacSHA512");
193         put("Alg.Alias.KeyGenerator.HMAC/SHA512", "HmacSHA512");
194 
195         /* == KeyPairGenerators == */
196         put("KeyPairGenerator.RSA", PREFIX + "OpenSSLRSAKeyPairGenerator");
197         put("Alg.Alias.KeyPairGenerator.1.2.840.113549.1.1.1", "RSA");
198         put("Alg.Alias.KeyPairGenerator.1.2.840.113549.1.1.7", "RSA");
199         put("Alg.Alias.KeyPairGenerator.2.5.8.1.1", "RSA");
200 
201         put("KeyPairGenerator.EC", PREFIX + "OpenSSLECKeyPairGenerator");
202         put("Alg.Alias.KeyPairGenerator.1.2.840.10045.2.1", "EC");
203         put("Alg.Alias.KeyPairGenerator.1.3.133.16.840.63.0.2", "EC");
204 
205         put("KeyPairGenerator.XDH", PREFIX + "OpenSSLXDHKeyPairGenerator");
206         put("Alg.Alias.KeyPairGenerator.1.3.101.110", "XDH");
207 
208         /* == KeyFactory == */
209         put("KeyFactory.RSA", PREFIX + "OpenSSLRSAKeyFactory");
210         put("Alg.Alias.KeyFactory.1.2.840.113549.1.1.1", "RSA");
211         put("Alg.Alias.KeyFactory.1.2.840.113549.1.1.7", "RSA");
212         put("Alg.Alias.KeyFactory.2.5.8.1.1", "RSA");
213 
214         put("KeyFactory.EC", PREFIX + "OpenSSLECKeyFactory");
215         put("Alg.Alias.KeyFactory.1.2.840.10045.2.1", "EC");
216         put("Alg.Alias.KeyFactory.1.3.133.16.840.63.0.2", "EC");
217 
218         put("KeyFactory.XDH", PREFIX + "OpenSSLXDHKeyFactory");
219         put("Alg.Alias.KeyFactory.1.3.101.110", "XDH");
220 
221         /* == SecretKeyFactory == */
222         put("SecretKeyFactory.DESEDE", PREFIX + "DESEDESecretKeyFactory");
223         put("Alg.Alias.SecretKeyFactory.TDEA", "DESEDE");
224 
225         /* == KeyAgreement == */
226         putECDHKeyAgreementImplClass("OpenSSLECDHKeyAgreement");
227         putXDHKeyAgreementImplClass("OpenSSLXDHKeyAgreement");
228 
229         /* == Signatures == */
230         putSignatureImplClass("MD5withRSA", "OpenSSLSignature$MD5RSA");
231         put("Alg.Alias.Signature.MD5withRSAEncryption", "MD5withRSA");
232         put("Alg.Alias.Signature.MD5/RSA", "MD5withRSA");
233         put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5withRSA");
234         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.4", "MD5withRSA");
235         put("Alg.Alias.Signature.1.2.840.113549.2.5with1.2.840.113549.1.1.1", "MD5withRSA");
236 
237         putSignatureImplClass("SHA1withRSA", "OpenSSLSignature$SHA1RSA");
238         put("Alg.Alias.Signature.SHA1withRSAEncryption", "SHA1withRSA");
239         put("Alg.Alias.Signature.SHA1/RSA", "SHA1withRSA");
240         put("Alg.Alias.Signature.SHA-1/RSA", "SHA1withRSA");
241         put("Alg.Alias.Signature.1.2.840.113549.1.1.5", "SHA1withRSA");
242         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.5", "SHA1withRSA");
243         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.1", "SHA1withRSA");
244         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.5", "SHA1withRSA");
245         put("Alg.Alias.Signature.1.3.14.3.2.29", "SHA1withRSA");
246         put("Alg.Alias.Signature.OID.1.3.14.3.2.29", "SHA1withRSA");
247 
248         putSignatureImplClass("SHA224withRSA", "OpenSSLSignature$SHA224RSA");
249         put("Alg.Alias.Signature.SHA224withRSAEncryption", "SHA224withRSA");
250         put("Alg.Alias.Signature.SHA224/RSA", "SHA224withRSA");
251         put("Alg.Alias.Signature.1.2.840.113549.1.1.14", "SHA224withRSA");
252         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.14", "SHA224withRSA");
253         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.1",
254                 "SHA224withRSA");
255         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.14",
256                 "SHA224withRSA");
257 
258         putSignatureImplClass("SHA256withRSA", "OpenSSLSignature$SHA256RSA");
259         put("Alg.Alias.Signature.SHA256withRSAEncryption", "SHA256withRSA");
260         put("Alg.Alias.Signature.SHA256/RSA", "SHA256withRSA");
261         put("Alg.Alias.Signature.1.2.840.113549.1.1.11", "SHA256withRSA");
262         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.11", "SHA256withRSA");
263         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.1",
264                 "SHA256withRSA");
265         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.11",
266                 "SHA256withRSA");
267 
268         putSignatureImplClass("SHA384withRSA", "OpenSSLSignature$SHA384RSA");
269         put("Alg.Alias.Signature.SHA384withRSAEncryption", "SHA384withRSA");
270         put("Alg.Alias.Signature.SHA384/RSA", "SHA384withRSA");
271         put("Alg.Alias.Signature.1.2.840.113549.1.1.12", "SHA384withRSA");
272         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.12", "SHA384withRSA");
273         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.113549.1.1.1",
274                 "SHA384withRSA");
275 
276         putSignatureImplClass("SHA512withRSA", "OpenSSLSignature$SHA512RSA");
277         put("Alg.Alias.Signature.SHA512withRSAEncryption", "SHA512withRSA");
278         put("Alg.Alias.Signature.SHA512/RSA", "SHA512withRSA");
279         put("Alg.Alias.Signature.1.2.840.113549.1.1.13", "SHA512withRSA");
280         put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.13", "SHA512withRSA");
281         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.113549.1.1.1",
282                 "SHA512withRSA");
283 
284         putRAWRSASignatureImplClass("OpenSSLSignatureRawRSA");
285 
286         putSignatureImplClass("NONEwithECDSA", "OpenSSLSignatureRawECDSA");
287 
288         putSignatureImplClass("SHA1withECDSA", "OpenSSLSignature$SHA1ECDSA");
289         put("Alg.Alias.Signature.ECDSA", "SHA1withECDSA");
290         put("Alg.Alias.Signature.ECDSAwithSHA1", "SHA1withECDSA");
291         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA1(1)
292         put("Alg.Alias.Signature.1.2.840.10045.4.1", "SHA1withECDSA");
293         put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.10045.2.1", "SHA1withECDSA");
294 
295         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
296         putSignatureImplClass("SHA224withECDSA", "OpenSSLSignature$SHA224ECDSA");
297         put("Alg.Alias.Signature.SHA224/ECDSA", "SHA224withECDSA");
298         // ecdsa-with-SHA224(1)
299         put("Alg.Alias.Signature.1.2.840.10045.4.3.1", "SHA224withECDSA");
300         put("Alg.Alias.Signature.OID.1.2.840.10045.4.3.1", "SHA224withECDSA");
301         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.10045.2.1", "SHA224withECDSA");
302 
303         // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
304         putSignatureImplClass("SHA256withECDSA", "OpenSSLSignature$SHA256ECDSA");
305         put("Alg.Alias.Signature.SHA256/ECDSA", "SHA256withECDSA");
306         // ecdsa-with-SHA256(2)
307         put("Alg.Alias.Signature.1.2.840.10045.4.3.2", "SHA256withECDSA");
308         put("Alg.Alias.Signature.OID.1.2.840.10045.4.3.2", "SHA256withECDSA");
309         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.10045.2.1", "SHA256withECDSA");
310 
311         putSignatureImplClass("SHA384withECDSA", "OpenSSLSignature$SHA384ECDSA");
312         put("Alg.Alias.Signature.SHA384/ECDSA", "SHA384withECDSA");
313         // ecdsa-with-SHA384(3)
314         put("Alg.Alias.Signature.1.2.840.10045.4.3.3", "SHA384withECDSA");
315         put("Alg.Alias.Signature.OID.1.2.840.10045.4.3.3", "SHA384withECDSA");
316         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.10045.2.1", "SHA384withECDSA");
317 
318         putSignatureImplClass("SHA512withECDSA", "OpenSSLSignature$SHA512ECDSA");
319         put("Alg.Alias.Signature.SHA512/ECDSA", "SHA512withECDSA");
320         // ecdsa-with-SHA512(4)
321         put("Alg.Alias.Signature.1.2.840.10045.4.3.4", "SHA512withECDSA");
322         put("Alg.Alias.Signature.OID.1.2.840.10045.4.3.4", "SHA512withECDSA");
323         put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.10045.2.1", "SHA512withECDSA");
324 
325         putSignatureImplClass("SHA1withRSA/PSS", "OpenSSLSignature$SHA1RSAPSS");
326         put("Alg.Alias.Signature.SHA1withRSAandMGF1", "SHA1withRSA/PSS");
327 
328         putSignatureImplClass("SHA224withRSA/PSS", "OpenSSLSignature$SHA224RSAPSS");
329         put("Alg.Alias.Signature.SHA224withRSAandMGF1", "SHA224withRSA/PSS");
330 
331         putSignatureImplClass("SHA256withRSA/PSS", "OpenSSLSignature$SHA256RSAPSS");
332         put("Alg.Alias.Signature.SHA256withRSAandMGF1", "SHA256withRSA/PSS");
333 
334         putSignatureImplClass("SHA384withRSA/PSS", "OpenSSLSignature$SHA384RSAPSS");
335         put("Alg.Alias.Signature.SHA384withRSAandMGF1", "SHA384withRSA/PSS");
336 
337         putSignatureImplClass("SHA512withRSA/PSS", "OpenSSLSignature$SHA512RSAPSS");
338         put("Alg.Alias.Signature.SHA512withRSAandMGF1", "SHA512withRSA/PSS");
339 
340         /* === SecureRandom === */
341         /*
342          * We have to specify SHA1PRNG because various documentation mentions
343          * that algorithm by name instead of just recommending calling
344          * "new SecureRandom()"
345          */
346         put("SecureRandom.SHA1PRNG", PREFIX + "OpenSSLRandom");
347         put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
348 
349         /* === Cipher === */
350         putRSACipherImplClass("RSA/ECB/NoPadding", "OpenSSLCipherRSA$Raw");
351         put("Alg.Alias.Cipher.RSA/None/NoPadding", "RSA/ECB/NoPadding");
352         putRSACipherImplClass("RSA/ECB/PKCS1Padding", "OpenSSLCipherRSA$PKCS1");
353         put("Alg.Alias.Cipher.RSA/None/PKCS1Padding", "RSA/ECB/PKCS1Padding");
354 
355         putRSACipherImplClass("RSA/ECB/OAEPPadding", "OpenSSLCipherRSA$OAEP$SHA1");
356         put("Alg.Alias.Cipher.RSA/None/OAEPPadding", "RSA/ECB/OAEPPadding");
357         putRSACipherImplClass("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", "OpenSSLCipherRSA$OAEP$SHA1");
358         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-1AndMGF1Padding",
359                 "RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
360         putRSACipherImplClass(
361                 "RSA/ECB/OAEPWithSHA-224AndMGF1Padding", "OpenSSLCipherRSA$OAEP$SHA224");
362         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-224AndMGF1Padding",
363                 "RSA/ECB/OAEPWithSHA-224AndMGF1Padding");
364         putRSACipherImplClass(
365                 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "OpenSSLCipherRSA$OAEP$SHA256");
366         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-256AndMGF1Padding",
367                 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
368         putRSACipherImplClass(
369                 "RSA/ECB/OAEPWithSHA-384AndMGF1Padding", "OpenSSLCipherRSA$OAEP$SHA384");
370         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-384AndMGF1Padding",
371                 "RSA/ECB/OAEPWithSHA-384AndMGF1Padding");
372         putRSACipherImplClass(
373                 "RSA/ECB/OAEPWithSHA-512AndMGF1Padding", "OpenSSLCipherRSA$OAEP$SHA512");
374         put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-512AndMGF1Padding",
375                 "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
376 
377         /*
378          * OpenSSL only supports a subset of modes, so we'll name them
379          * explicitly here.
380          *
381          * Moreover, OpenSSL only supports PKCS#7 padding. PKCS#5 padding
382          * is also supported because it's a special case of PKCS#7 for 64-bit
383          * blocks. PKCS#5 technically supports only 64-bit blocks and won't
384          * produce the same result as PKCS#7 for blocks that are not 64 bits
385          * long. However, everybody assumes PKCS#7 when they say PKCS#5. For
386          * example, lots of code uses PKCS#5 with AES whose blocks are longer
387          * than 64 bits. We solve this confusion by making PKCS7Padding an
388          * alias for PKCS5Padding.
389          */
390         putSymmetricCipherImplClass("AES/ECB/NoPadding", "OpenSSLEvpCipherAES$AES$ECB$NoPadding");
391         putSymmetricCipherImplClass(
392                 "AES/ECB/PKCS5Padding", "OpenSSLEvpCipherAES$AES$ECB$PKCS5Padding");
393         put("Alg.Alias.Cipher.AES/ECB/PKCS7Padding", "AES/ECB/PKCS5Padding");
394         putSymmetricCipherImplClass("AES/CBC/NoPadding", "OpenSSLEvpCipherAES$AES$CBC$NoPadding");
395         putSymmetricCipherImplClass(
396                 "AES/CBC/PKCS5Padding", "OpenSSLEvpCipherAES$AES$CBC$PKCS5Padding");
397         put("Alg.Alias.Cipher.AES/CBC/PKCS7Padding", "AES/CBC/PKCS5Padding");
398         putSymmetricCipherImplClass("AES/CTR/NoPadding", "OpenSSLEvpCipherAES$AES$CTR");
399 
400         putSymmetricCipherImplClass(
401                 "AES_128/ECB/NoPadding", "OpenSSLEvpCipherAES$AES_128$ECB$NoPadding");
402         putSymmetricCipherImplClass(
403                 "AES_128/ECB/PKCS5Padding", "OpenSSLEvpCipherAES$AES_128$ECB$PKCS5Padding");
404         put("Alg.Alias.Cipher.AES_128/ECB/PKCS7Padding", "AES_128/ECB/PKCS5Padding");
405         putSymmetricCipherImplClass(
406                 "AES_128/CBC/NoPadding", "OpenSSLEvpCipherAES$AES_128$CBC$NoPadding");
407         putSymmetricCipherImplClass(
408                 "AES_128/CBC/PKCS5Padding", "OpenSSLEvpCipherAES$AES_128$CBC$PKCS5Padding");
409         put("Alg.Alias.Cipher.AES_128/CBC/PKCS7Padding", "AES_128/CBC/PKCS5Padding");
410 
411         put("Alg.Alias.Cipher.PBEWithHmacSHA1AndAES_128", "AES_128/CBC/PKCS5PADDING");
412         put("Alg.Alias.Cipher.PBEWithHmacSHA224AndAES_128", "AES_128/CBC/PKCS5PADDING");
413         put("Alg.Alias.Cipher.PBEWithHmacSHA256AndAES_128", "AES_128/CBC/PKCS5PADDING");
414         put("Alg.Alias.Cipher.PBEWithHmacSHA384AndAES_128", "AES_128/CBC/PKCS5PADDING");
415         put("Alg.Alias.Cipher.PBEWithHmacSHA512AndAES_128", "AES_128/CBC/PKCS5PADDING");
416 
417         putSymmetricCipherImplClass(
418                 "AES_256/ECB/NoPadding", "OpenSSLEvpCipherAES$AES_256$ECB$NoPadding");
419         putSymmetricCipherImplClass(
420                 "AES_256/ECB/PKCS5Padding", "OpenSSLEvpCipherAES$AES_256$ECB$PKCS5Padding");
421         put("Alg.Alias.Cipher.AES_256/ECB/PKCS7Padding", "AES_256/ECB/PKCS5Padding");
422         putSymmetricCipherImplClass(
423                 "AES_256/CBC/NoPadding", "OpenSSLEvpCipherAES$AES_256$CBC$NoPadding");
424         putSymmetricCipherImplClass(
425                 "AES_256/CBC/PKCS5Padding", "OpenSSLEvpCipherAES$AES_256$CBC$PKCS5Padding");
426         put("Alg.Alias.Cipher.AES_256/CBC/PKCS7Padding", "AES_256/CBC/PKCS5Padding");
427 
428         put("Alg.Alias.Cipher.PBEWithHmacSHA1AndAES_256", "AES_256/CBC/PKCS5PADDING");
429         put("Alg.Alias.Cipher.PBEWithHmacSHA224AndAES_256", "AES_256/CBC/PKCS5PADDING");
430         put("Alg.Alias.Cipher.PBEWithHmacSHA256AndAES_256", "AES_256/CBC/PKCS5PADDING");
431         put("Alg.Alias.Cipher.PBEWithHmacSHA384AndAES_256", "AES_256/CBC/PKCS5PADDING");
432         put("Alg.Alias.Cipher.PBEWithHmacSHA512AndAES_256", "AES_256/CBC/PKCS5PADDING");
433 
434         putSymmetricCipherImplClass("DESEDE/CBC/NoPadding", "OpenSSLEvpCipherDESEDE$CBC$NoPadding");
435         putSymmetricCipherImplClass(
436                 "DESEDE/CBC/PKCS5Padding", "OpenSSLEvpCipherDESEDE$CBC$PKCS5Padding");
437         put("Alg.Alias.Cipher.DESEDE/CBC/PKCS7Padding", "DESEDE/CBC/PKCS5Padding");
438 
439         putSymmetricCipherImplClass("ARC4", "OpenSSLEvpCipherARC4");
440         put("Alg.Alias.Cipher.ARCFOUR", "ARC4");
441         put("Alg.Alias.Cipher.RC4", "ARC4");
442         put("Alg.Alias.Cipher.1.2.840.113549.3.4", "ARC4");
443         put("Alg.Alias.Cipher.OID.1.2.840.113549.3.4", "ARC4");
444 
445         putSymmetricCipherImplClass("AES/GCM/NoPadding", "OpenSSLAeadCipherAES$GCM");
446         put("Alg.Alias.Cipher.GCM", "AES/GCM/NoPadding");
447         put("Alg.Alias.Cipher.2.16.840.1.101.3.4.1.6", "AES/GCM/NoPadding");
448         put("Alg.Alias.Cipher.2.16.840.1.101.3.4.1.26", "AES/GCM/NoPadding");
449         put("Alg.Alias.Cipher.2.16.840.1.101.3.4.1.46", "AES/GCM/NoPadding");
450         putSymmetricCipherImplClass("AES_128/GCM/NoPadding", "OpenSSLAeadCipherAES$GCM$AES_128");
451         putSymmetricCipherImplClass("AES_256/GCM/NoPadding", "OpenSSLAeadCipherAES$GCM$AES_256");
452 
453         putSymmetricCipherImplClass("AES/GCM-SIV/NoPadding", "OpenSSLAeadCipherAES$GCM_SIV");
454         putSymmetricCipherImplClass(
455                 "AES_128/GCM-SIV/NoPadding", "OpenSSLAeadCipherAES$GCM_SIV$AES_128");
456         putSymmetricCipherImplClass(
457                 "AES_256/GCM-SIV/NoPadding", "OpenSSLAeadCipherAES$GCM_SIV$AES_256");
458 
459         putSymmetricCipherImplClass("ChaCha20",
460                 "OpenSSLCipherChaCha20");
461         putSymmetricCipherImplClass("ChaCha20/Poly1305/NoPadding", "OpenSSLAeadCipherChaCha20");
462         put("Alg.Alias.Cipher.ChaCha20-Poly1305", "ChaCha20/Poly1305/NoPadding");
463 
464         /* === Mac === */
465 
466         putMacImplClass("HmacMD5", "OpenSSLMac$HmacMD5");
467         put("Alg.Alias.Mac.1.3.6.1.5.5.8.1.1", "HmacMD5");
468         put("Alg.Alias.Mac.HMAC-MD5", "HmacMD5");
469         put("Alg.Alias.Mac.HMAC/MD5", "HmacMD5");
470 
471         // PKCS#2 - iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2)
472         // http://www.oid-info.com/get/1.2.840.113549.2
473 
474         // HMAC-SHA-1 PRF (7)
475         putMacImplClass("HmacSHA1", "OpenSSLMac$HmacSHA1");
476         put("Alg.Alias.Mac.1.2.840.113549.2.7", "HmacSHA1");
477         put("Alg.Alias.Mac.1.3.6.1.5.5.8.1.2", "HmacSHA1");
478         put("Alg.Alias.Mac.HMAC-SHA1", "HmacSHA1");
479         put("Alg.Alias.Mac.HMAC/SHA1", "HmacSHA1");
480 
481         // id-hmacWithSHA224 (8)
482         putMacImplClass("HmacSHA224", "OpenSSLMac$HmacSHA224");
483         put("Alg.Alias.Mac.1.2.840.113549.2.8", "HmacSHA224");
484         put("Alg.Alias.Mac.HMAC-SHA224", "HmacSHA224");
485         put("Alg.Alias.Mac.HMAC/SHA224", "HmacSHA224");
486         put("Alg.Alias.Mac.PBEWITHHMACSHA224", "HmacSHA224");
487 
488         // id-hmacWithSHA256 (9)
489         putMacImplClass("HmacSHA256", "OpenSSLMac$HmacSHA256");
490         put("Alg.Alias.Mac.1.2.840.113549.2.9", "HmacSHA256");
491         put("Alg.Alias.Mac.2.16.840.1.101.3.4.2.1", "HmacSHA256");
492         put("Alg.Alias.Mac.HMAC-SHA256", "HmacSHA256");
493         put("Alg.Alias.Mac.HMAC/SHA256", "HmacSHA256");
494         put("Alg.Alias.Mac.PBEWITHHMACSHA256", "HmacSHA256");
495 
496         // id-hmacWithSHA384 (10)
497         putMacImplClass("HmacSHA384", "OpenSSLMac$HmacSHA384");
498         put("Alg.Alias.Mac.1.2.840.113549.2.10", "HmacSHA384");
499         put("Alg.Alias.Mac.HMAC-SHA384", "HmacSHA384");
500         put("Alg.Alias.Mac.HMAC/SHA384", "HmacSHA384");
501         put("Alg.Alias.Mac.PBEWITHHMACSHA384", "HmacSHA384");
502 
503         // id-hmacWithSHA384 (11)
504         putMacImplClass("HmacSHA512", "OpenSSLMac$HmacSHA512");
505         put("Alg.Alias.Mac.1.2.840.113549.2.11", "HmacSHA512");
506         put("Alg.Alias.Mac.HMAC-SHA512", "HmacSHA512");
507         put("Alg.Alias.Mac.HMAC/SHA512", "HmacSHA512");
508         put("Alg.Alias.Mac.PBEWITHHMACSHA512", "HmacSHA512");
509 
510         putMacImplClass("AESCMAC", "OpenSSLMac$AesCmac");
511 
512         /* === Certificate === */
513 
514         put("CertificateFactory.X509", PREFIX + "OpenSSLX509CertificateFactory");
515         put("Alg.Alias.CertificateFactory.X.509", "X509");
516     }
517 
putMacImplClass(String algorithm, String className)518     private void putMacImplClass(String algorithm, String className) {
519         // Accept only keys for which any of the following is true:
520         // * the key is from this provider (subclass of OpenSSLKeyHolder),
521         // * the key provides its key material in "RAW" encoding via Key.getEncoded.
522         String supportedKeyClasses = PREFIX + "OpenSSLKeyHolder";
523         String supportedKeyFormats = "RAW";
524         putImplClassWithKeyConstraints(
525                 "Mac." + algorithm,
526                 PREFIX + className,
527                 supportedKeyClasses,
528                 supportedKeyFormats);
529     }
530 
putSymmetricCipherImplClass(String transformation, String className)531     private void putSymmetricCipherImplClass(String transformation, String className) {
532         // Accept only keys for which any of the following is true:
533         // * the key provides its key material in "RAW" encoding via Key.getEncoded.
534         String supportedKeyClasses = null; // ignored -- filtered based on encoding format only
535         String supportedKeyFormats = "RAW";
536         putImplClassWithKeyConstraints(
537                 "Cipher." + transformation,
538                 PREFIX + className,
539                 supportedKeyClasses,
540                 supportedKeyFormats);
541     }
542 
putRSACipherImplClass(String transformation, String className)543     private void putRSACipherImplClass(String transformation, String className) {
544         // Accept only keys for which any of the following is true:
545         // * the key is instance of OpenSSLRSAPrivateKey, RSAPrivateKey, OpenSSLRSAPublicKey, or
546         //   RSAPublicKey.
547         String supportedKeyClasses = PREFIX + "OpenSSLRSAPrivateKey"
548                 + "|" + STANDARD_RSA_PRIVATE_KEY_INTERFACE_CLASS_NAME
549                 + "|" + PREFIX + "OpenSSLRSAPublicKey"
550                 + "|" + STANDARD_RSA_PUBLIC_KEY_INTERFACE_CLASS_NAME;
551         String supportedKeyFormats = null; // ignored -- filtered based on class only
552         putImplClassWithKeyConstraints(
553                 "Cipher." + transformation,
554                 PREFIX + className,
555                 supportedKeyClasses,
556                 supportedKeyFormats);
557     }
558 
putSignatureImplClass(String algorithm, String className)559     private void putSignatureImplClass(String algorithm, String className) {
560         // Accept only keys for which any of the following is true:
561         // * the key is from this provider (subclass of OpenSSLKeyHolder),
562         // * the key provides its key material in "PKCS#8" or "X.509" encodings via Key.getEncoded.
563         // * the key is a transparent private key (subclass of RSAPrivateKey or ECPrivateKey). For
564         //   some reason this provider's Signature implementation does not unconditionally accept
565         //   transparent public keys -- it only accepts them if they provide their key material in
566         //   encoded form (see above).
567         String supportedKeyClasses = PREFIX + "OpenSSLKeyHolder"
568                 + "|" + STANDARD_RSA_PRIVATE_KEY_INTERFACE_CLASS_NAME
569                 + "|" + STANDARD_EC_PRIVATE_KEY_INTERFACE_CLASS_NAME
570                 + "|" + STANDARD_RSA_PUBLIC_KEY_INTERFACE_CLASS_NAME;
571         String supportedKeyFormats = "PKCS#8|X.509";
572         putImplClassWithKeyConstraints(
573                 "Signature." + algorithm,
574                 PREFIX + className,
575                 supportedKeyClasses,
576                 supportedKeyFormats);
577     }
578 
putRAWRSASignatureImplClass(String className)579     private void putRAWRSASignatureImplClass(String className) {
580         // Accept only keys for which any of the following is true:
581         // * the key is instance of OpenSSLRSAPrivateKey, RSAPrivateKey, OpenSSLRSAPublicKey, or
582         //   RSAPublicKey.
583         String supportedKeyClasses = PREFIX + "OpenSSLRSAPrivateKey"
584                 + "|" + STANDARD_RSA_PRIVATE_KEY_INTERFACE_CLASS_NAME
585                 + "|" + PREFIX + "OpenSSLRSAPublicKey"
586                 + "|" + STANDARD_RSA_PUBLIC_KEY_INTERFACE_CLASS_NAME;
587         String supportedKeyFormats = null; // ignored -- filtered based on class only
588         putImplClassWithKeyConstraints(
589                 "Signature.NONEwithRSA",
590                 PREFIX + className,
591                 supportedKeyClasses,
592                 supportedKeyFormats);
593     }
594 
putECDHKeyAgreementImplClass(String className)595     private void putECDHKeyAgreementImplClass(String className) {
596         // Accept only keys for which any of the following is true:
597         // * the key is from this provider (subclass of OpenSSLKeyHolder),
598         // * the key provides its key material in "PKCS#8" encoding via Key.getEncoded.
599         // * the key is a transparent EC private key (subclass of ECPrivateKey).
600         String supportedKeyClasses = PREFIX + "OpenSSLKeyHolder"
601                 + "|" + STANDARD_EC_PRIVATE_KEY_INTERFACE_CLASS_NAME;
602         String supportedKeyFormats = "PKCS#8";
603         putImplClassWithKeyConstraints(
604                 "KeyAgreement.ECDH",
605                 PREFIX + className,
606                 supportedKeyClasses,
607                 supportedKeyFormats);
608     }
609 
putXDHKeyAgreementImplClass(String className)610     private void putXDHKeyAgreementImplClass(String className) {
611         // Accept only keys for which any of the following is true:
612         // * the key is from this provider (subclass of OpenSSLKeyHolder),
613         // * the key provides its key material in "PKCS#8" encoding via Key.getEncoded.
614         // * the key is a transparent XEC private key (subclass of XECPrivateKey).
615         String supportedKeyClasses = PREFIX + "OpenSSLKeyHolder"
616                 + "|" + STANDARD_XEC_PRIVATE_KEY_INTERFACE_CLASS_NAME + "|" + PREFIX
617                 + "OpenSSLX25519PrivateKey";
618         String supportedKeyFormats = "PKCS#8";
619         putImplClassWithKeyConstraints(
620                 "KeyAgreement.XDH", PREFIX + className, supportedKeyClasses, supportedKeyFormats);
621     }
622 
putImplClassWithKeyConstraints(String typeAndAlgName, String fullyQualifiedClassName, String supportedKeyClasses, String supportedKeyFormats)623     private void putImplClassWithKeyConstraints(String typeAndAlgName,
624             String fullyQualifiedClassName,
625             String supportedKeyClasses,
626             String supportedKeyFormats) {
627         put(typeAndAlgName, fullyQualifiedClassName);
628         if (supportedKeyClasses != null) {
629             put(typeAndAlgName + " SupportedKeyClasses", supportedKeyClasses);
630         }
631         if (supportedKeyFormats != null) {
632             put(typeAndAlgName + " SupportedKeyFormats", supportedKeyFormats);
633         }
634     }
635 }
636