| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/test_root_certs.h" | 5 #include "net/cert/test_root_certs.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "crypto/nss_util.h" | 12 #include "crypto/nss_util.h" |
| 13 #include "net/cert/x509_certificate.h" | 13 #include "net/cert/x509_certificate.h" |
| 14 | 14 |
| 15 #if defined(OS_IOS) | |
| 16 #include "net/cert/x509_util_ios.h" | |
| 17 #endif | |
| 18 | |
| 19 namespace net { | 15 namespace net { |
| 20 | 16 |
| 21 | 17 |
| 22 TestRootCerts::TrustEntry::TrustEntry(CERTCertificate* certificate, | 18 TestRootCerts::TrustEntry::TrustEntry(CERTCertificate* certificate, |
| 23 const CERTCertTrust& trust) | 19 const CERTCertTrust& trust) |
| 24 : certificate_(CERT_DupCertificate(certificate)), | 20 : certificate_(CERT_DupCertificate(certificate)), |
| 25 trust_(trust) { | 21 trust_(trust) { |
| 26 } | 22 } |
| 27 | 23 |
| 28 TestRootCerts::TrustEntry::~TrustEntry() { | 24 TestRootCerts::TrustEntry::~TrustEntry() { |
| 29 CERT_DestroyCertificate(certificate_); | 25 CERT_DestroyCertificate(certificate_); |
| 30 } | 26 } |
| 31 | 27 |
| 32 bool TestRootCerts::Add(X509Certificate* certificate) { | 28 bool TestRootCerts::Add(X509Certificate* certificate) { |
| 33 #if defined(OS_IOS) | |
| 34 x509_util_ios::NSSCertificate nss_certificate(certificate->os_cert_handle()); | |
| 35 CERTCertificate* cert_handle = nss_certificate.cert_handle(); | |
| 36 #else | |
| 37 CERTCertificate* cert_handle = certificate->os_cert_handle(); | 29 CERTCertificate* cert_handle = certificate->os_cert_handle(); |
| 38 #endif | |
| 39 // Preserve the original trust bits so that they can be restored when | 30 // Preserve the original trust bits so that they can be restored when |
| 40 // the certificate is removed. | 31 // the certificate is removed. |
| 41 CERTCertTrust original_trust; | 32 CERTCertTrust original_trust; |
| 42 SECStatus rv = CERT_GetCertTrust(cert_handle, &original_trust); | 33 SECStatus rv = CERT_GetCertTrust(cert_handle, &original_trust); |
| 43 if (rv != SECSuccess) { | 34 if (rv != SECSuccess) { |
| 44 // CERT_GetCertTrust will fail if the certificate does not have any | 35 // CERT_GetCertTrust will fail if the certificate does not have any |
| 45 // particular trust settings associated with it, and attempts to use | 36 // particular trust settings associated with it, and attempts to use |
| 46 // |original_trust| later to restore the original trust settings will not | 37 // |original_trust| later to restore the original trust settings will not |
| 47 // cause the trust settings to be revoked. If the certificate has no | 38 // cause the trust settings to be revoked. If the certificate has no |
| 48 // particular trust settings associated with it, mark the certificate as | 39 // particular trust settings associated with it, mark the certificate as |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // occur after Clear() has been called. | 76 // occur after Clear() has been called. |
| 86 DCHECK_EQ(SECSuccess, rv) << "Cannot restore certificate trust."; | 77 DCHECK_EQ(SECSuccess, rv) << "Cannot restore certificate trust."; |
| 87 } | 78 } |
| 88 trust_cache_.clear(); | 79 trust_cache_.clear(); |
| 89 } | 80 } |
| 90 | 81 |
| 91 bool TestRootCerts::IsEmpty() const { | 82 bool TestRootCerts::IsEmpty() const { |
| 92 return trust_cache_.empty(); | 83 return trust_cache_.empty(); |
| 93 } | 84 } |
| 94 | 85 |
| 95 #if defined(USE_NSS_CERTS) | |
| 96 bool TestRootCerts::Contains(CERTCertificate* cert) const { | 86 bool TestRootCerts::Contains(CERTCertificate* cert) const { |
| 97 for (const auto& item : trust_cache_) | 87 for (const auto& item : trust_cache_) |
| 98 if (X509Certificate::IsSameOSCert(cert, item->certificate())) | 88 if (X509Certificate::IsSameOSCert(cert, item->certificate())) |
| 99 return true; | 89 return true; |
| 100 | 90 |
| 101 return false; | 91 return false; |
| 102 } | 92 } |
| 103 #endif | |
| 104 | 93 |
| 105 TestRootCerts::~TestRootCerts() { | 94 TestRootCerts::~TestRootCerts() { |
| 106 Clear(); | 95 Clear(); |
| 107 } | 96 } |
| 108 | 97 |
| 109 void TestRootCerts::Init() { | 98 void TestRootCerts::Init() { |
| 110 crypto::EnsureNSSInit(); | 99 crypto::EnsureNSSInit(); |
| 111 } | 100 } |
| 112 | 101 |
| 113 } // namespace net | 102 } // namespace net |
| OLD | NEW |