Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 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.net.config; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.ArraySet; |
| 21 | import libcore.io.IoUtils; |
| 22 | import java.io.InputStream; |
| 23 | import java.security.cert.Certificate; |
| 24 | import java.security.cert.CertificateException; |
| 25 | import java.security.cert.CertificateFactory; |
| 26 | import java.security.cert.X509Certificate; |
| 27 | import java.util.Collection; |
Chad Brubaker | aa6c3c3 | 2015-12-18 13:43:28 -0800 | [diff] [blame] | 28 | import java.util.Collections; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 31 | import com.android.org.conscrypt.TrustedCertificateIndex; |
| 32 | |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 33 | /** |
| 34 | * {@link CertificateSource} based on certificates contained in an application resource file. |
| 35 | * @hide |
| 36 | */ |
| 37 | public class ResourceCertificateSource implements CertificateSource { |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 38 | private final Object mLock = new Object(); |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 39 | private final int mResourceId; |
| 40 | |
| 41 | private Set<X509Certificate> mCertificates; |
| 42 | private Context mContext; |
| 43 | private TrustedCertificateIndex mIndex; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 44 | |
| 45 | public ResourceCertificateSource(int resourceId, Context context) { |
| 46 | mResourceId = resourceId; |
Chad Brubaker | 7879b8f | 2016-02-22 11:04:57 -0800 | [diff] [blame] | 47 | mContext = context; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 50 | private void ensureInitialized() { |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 51 | synchronized (mLock) { |
| 52 | if (mCertificates != null) { |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 53 | return; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 54 | } |
| 55 | Set<X509Certificate> certificates = new ArraySet<X509Certificate>(); |
| 56 | Collection<? extends Certificate> certs; |
| 57 | InputStream in = null; |
| 58 | try { |
| 59 | CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
| 60 | in = mContext.getResources().openRawResource(mResourceId); |
| 61 | certs = factory.generateCertificates(in); |
| 62 | } catch (CertificateException e) { |
| 63 | throw new RuntimeException("Failed to load trust anchors from id " + mResourceId, |
| 64 | e); |
| 65 | } finally { |
| 66 | IoUtils.closeQuietly(in); |
| 67 | } |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 68 | TrustedCertificateIndex indexLocal = new TrustedCertificateIndex(); |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 69 | for (Certificate cert : certs) { |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 70 | certificates.add((X509Certificate) cert); |
| 71 | indexLocal.index((X509Certificate) cert); |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 72 | } |
| 73 | mCertificates = certificates; |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 74 | mIndex = indexLocal; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 75 | mContext = null; |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
Chad Brubaker | d3af962 | 2015-11-16 10:48:20 -0800 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | public Set<X509Certificate> getCertificates() { |
| 81 | ensureInitialized(); |
| 82 | return mCertificates; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public X509Certificate findBySubjectAndPublicKey(X509Certificate cert) { |
| 87 | ensureInitialized(); |
| 88 | java.security.cert.TrustAnchor anchor = mIndex.findBySubjectAndPublicKey(cert); |
| 89 | if (anchor == null) { |
| 90 | return null; |
| 91 | } |
| 92 | return anchor.getTrustedCert(); |
| 93 | } |
Chad Brubaker | fa9beeb | 2015-11-25 13:12:55 -0800 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public X509Certificate findByIssuerAndSignature(X509Certificate cert) { |
| 97 | ensureInitialized(); |
| 98 | java.security.cert.TrustAnchor anchor = mIndex.findByIssuerAndSignature(cert); |
| 99 | if (anchor == null) { |
| 100 | return null; |
| 101 | } |
| 102 | return anchor.getTrustedCert(); |
| 103 | } |
Chad Brubaker | aa6c3c3 | 2015-12-18 13:43:28 -0800 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public Set<X509Certificate> findAllByIssuerAndSignature(X509Certificate cert) { |
| 107 | ensureInitialized(); |
| 108 | Set<java.security.cert.TrustAnchor> anchors = mIndex.findAllByIssuerAndSignature(cert); |
| 109 | if (anchors.isEmpty()) { |
| 110 | return Collections.<X509Certificate>emptySet(); |
| 111 | } |
| 112 | Set<X509Certificate> certs = new ArraySet<X509Certificate>(anchors.size()); |
| 113 | for (java.security.cert.TrustAnchor anchor : anchors) { |
| 114 | certs.add(anchor.getTrustedCert()); |
| 115 | } |
| 116 | return certs; |
| 117 | } |
Chad Brubaker | bf9a82a | 2016-03-25 10:12:19 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public void handleTrustStorageUpdate() { |
| 121 | // Nothing to do, resource sources never change. |
| 122 | } |
Chad Brubaker | 6bc1e39 | 2015-10-23 15:33:56 -0700 | [diff] [blame] | 123 | } |