blob: 78669c51f76c8d8e75cdee45ae36201913e58cc3 [file] [log] [blame]
Chad Brubaker6bc1e392015-10-23 15:33:56 -07001/*
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
17package android.security.net.config;
18
19import android.content.Context;
20import android.util.ArraySet;
21import libcore.io.IoUtils;
22import java.io.InputStream;
23import java.security.cert.Certificate;
24import java.security.cert.CertificateException;
25import java.security.cert.CertificateFactory;
26import java.security.cert.X509Certificate;
27import java.util.Collection;
Chad Brubakeraa6c3c32015-12-18 13:43:28 -080028import java.util.Collections;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070029import java.util.Set;
30
Chad Brubakerd3af9622015-11-16 10:48:20 -080031import com.android.org.conscrypt.TrustedCertificateIndex;
32
Chad Brubaker6bc1e392015-10-23 15:33:56 -070033/**
34 * {@link CertificateSource} based on certificates contained in an application resource file.
35 * @hide
36 */
37public class ResourceCertificateSource implements CertificateSource {
Chad Brubaker6bc1e392015-10-23 15:33:56 -070038 private final Object mLock = new Object();
Chad Brubakerd3af9622015-11-16 10:48:20 -080039 private final int mResourceId;
40
41 private Set<X509Certificate> mCertificates;
42 private Context mContext;
43 private TrustedCertificateIndex mIndex;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070044
45 public ResourceCertificateSource(int resourceId, Context context) {
46 mResourceId = resourceId;
Chad Brubaker7879b8f2016-02-22 11:04:57 -080047 mContext = context;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070048 }
49
Chad Brubakerd3af9622015-11-16 10:48:20 -080050 private void ensureInitialized() {
Chad Brubaker6bc1e392015-10-23 15:33:56 -070051 synchronized (mLock) {
52 if (mCertificates != null) {
Chad Brubakerd3af9622015-11-16 10:48:20 -080053 return;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070054 }
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 Brubakerd3af9622015-11-16 10:48:20 -080068 TrustedCertificateIndex indexLocal = new TrustedCertificateIndex();
Chad Brubaker6bc1e392015-10-23 15:33:56 -070069 for (Certificate cert : certs) {
Chad Brubakerd3af9622015-11-16 10:48:20 -080070 certificates.add((X509Certificate) cert);
71 indexLocal.index((X509Certificate) cert);
Chad Brubaker6bc1e392015-10-23 15:33:56 -070072 }
73 mCertificates = certificates;
Chad Brubakerd3af9622015-11-16 10:48:20 -080074 mIndex = indexLocal;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070075 mContext = null;
Chad Brubaker6bc1e392015-10-23 15:33:56 -070076 }
77 }
Chad Brubakerd3af9622015-11-16 10:48:20 -080078
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 Brubakerfa9beeb2015-11-25 13:12:55 -080094
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 Brubakeraa6c3c32015-12-18 13:43:28 -0800104
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 Brubakerbf9a82a2016-03-25 10:12:19 -0700118
119 @Override
120 public void handleTrustStorageUpdate() {
121 // Nothing to do, resource sources never change.
122 }
Chad Brubaker6bc1e392015-10-23 15:33:56 -0700123}