blob: c68f3850e8f425d2b1bb41afedd3966cdd64ef9e [file] [log] [blame]
Chad Brubaker5a1078f2015-11-10 12:26:18 -08001/*
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.util.ArraySet;
20import java.security.KeyStore;
21import java.security.KeyStoreException;
22import java.security.cert.Certificate;
23import java.security.cert.X509Certificate;
Chad Brubakeraa6c3c32015-12-18 13:43:28 -080024import java.util.Collections;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080025import java.util.Enumeration;
26import java.util.Set;
27
Chad Brubakerd3af9622015-11-16 10:48:20 -080028import com.android.org.conscrypt.TrustedCertificateIndex;
29
Chad Brubaker5a1078f2015-11-10 12:26:18 -080030/**
31 * {@link CertificateSource} which provides certificates from trusted certificate entries of a
32 * {@link KeyStore}.
33 */
34class KeyStoreCertificateSource implements CertificateSource {
35 private final Object mLock = new Object();
36 private final KeyStore mKeyStore;
Chad Brubakerd3af9622015-11-16 10:48:20 -080037 private TrustedCertificateIndex mIndex;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080038 private Set<X509Certificate> mCertificates;
39
40 public KeyStoreCertificateSource(KeyStore ks) {
41 mKeyStore = ks;
42 }
43
44 @Override
45 public Set<X509Certificate> getCertificates() {
Chad Brubakerd3af9622015-11-16 10:48:20 -080046 ensureInitialized();
47 return mCertificates;
48 }
49
50 private void ensureInitialized() {
Chad Brubaker5a1078f2015-11-10 12:26:18 -080051 synchronized (mLock) {
52 if (mCertificates != null) {
Chad Brubakerd3af9622015-11-16 10:48:20 -080053 return;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080054 }
Chad Brubakerd3af9622015-11-16 10:48:20 -080055
Chad Brubaker5a1078f2015-11-10 12:26:18 -080056 try {
Chad Brubakerd3af9622015-11-16 10:48:20 -080057 TrustedCertificateIndex localIndex = new TrustedCertificateIndex();
Chad Brubaker5a1078f2015-11-10 12:26:18 -080058 Set<X509Certificate> certificates = new ArraySet<>(mKeyStore.size());
59 for (Enumeration<String> en = mKeyStore.aliases(); en.hasMoreElements();) {
60 String alias = en.nextElement();
Chad Brubaker5a1078f2015-11-10 12:26:18 -080061 X509Certificate cert = (X509Certificate) mKeyStore.getCertificate(alias);
62 if (cert != null) {
63 certificates.add(cert);
Chad Brubakerd3af9622015-11-16 10:48:20 -080064 localIndex.index(cert);
Chad Brubaker5a1078f2015-11-10 12:26:18 -080065 }
66 }
Chad Brubakerd3af9622015-11-16 10:48:20 -080067 mIndex = localIndex;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080068 mCertificates = certificates;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080069 } catch (KeyStoreException e) {
70 throw new RuntimeException("Failed to load certificates from KeyStore", e);
71 }
72 }
73 }
Chad Brubakerd3af9622015-11-16 10:48:20 -080074
75 @Override
76 public X509Certificate findBySubjectAndPublicKey(X509Certificate cert) {
77 ensureInitialized();
78 java.security.cert.TrustAnchor anchor = mIndex.findBySubjectAndPublicKey(cert);
79 if (anchor == null) {
80 return null;
81 }
82 return anchor.getTrustedCert();
83 }
Chad Brubakerfa9beeb2015-11-25 13:12:55 -080084
85 @Override
86 public X509Certificate findByIssuerAndSignature(X509Certificate cert) {
87 ensureInitialized();
88 java.security.cert.TrustAnchor anchor = mIndex.findByIssuerAndSignature(cert);
89 if (anchor == null) {
90 return null;
91 }
92 return anchor.getTrustedCert();
93 }
Chad Brubakeraa6c3c32015-12-18 13:43:28 -080094
95 @Override
96 public Set<X509Certificate> findAllByIssuerAndSignature(X509Certificate cert) {
97 ensureInitialized();
98 Set<java.security.cert.TrustAnchor> anchors = mIndex.findAllByIssuerAndSignature(cert);
99 if (anchors.isEmpty()) {
100 return Collections.<X509Certificate>emptySet();
101 }
102 Set<X509Certificate> certs = new ArraySet<X509Certificate>(anchors.size());
103 for (java.security.cert.TrustAnchor anchor : anchors) {
104 certs.add(anchor.getTrustedCert());
105 }
106 return certs;
107 }
Chad Brubakerbf9a82a2016-03-25 10:12:19 -0700108
109 @Override
110 public void handleTrustStorageUpdate() {
111 // Nothing to do.
112 }
Chad Brubaker5a1078f2015-11-10 12:26:18 -0800113}