This source file includes following definitions.
- certificate
- trust
- trust_
- Add
- Clear
- IsEmpty
- Contains
- Init
#include "net/cert/test_root_certs.h"
#include <cert.h>
#include "base/logging.h"
#include "base/stl_util.h"
#include "crypto/nss_util.h"
#include "net/cert/x509_certificate.h"
#if defined(OS_IOS)
#include "net/cert/x509_util_ios.h"
#endif
namespace net {
class TestRootCerts::TrustEntry {
public:
TrustEntry(CERTCertificate* certificate, const CERTCertTrust& trust);
~TrustEntry();
CERTCertificate* certificate() const { return certificate_; }
const CERTCertTrust& trust() const { return trust_; }
private:
CERTCertificate* certificate_;
CERTCertTrust trust_;
DISALLOW_COPY_AND_ASSIGN(TrustEntry);
};
TestRootCerts::TrustEntry::TrustEntry(CERTCertificate* certificate,
const CERTCertTrust& trust)
: certificate_(CERT_DupCertificate(certificate)),
trust_(trust) {
}
TestRootCerts::TrustEntry::~TrustEntry() {
CERT_DestroyCertificate(certificate_);
}
bool TestRootCerts::Add(X509Certificate* certificate) {
#if defined(OS_IOS)
x509_util_ios::NSSCertificate nss_certificate(certificate->os_cert_handle());
CERTCertificate* cert_handle = nss_certificate.cert_handle();
#else
CERTCertificate* cert_handle = certificate->os_cert_handle();
#endif
CERTCertTrust original_trust;
SECStatus rv = CERT_GetCertTrust(cert_handle, &original_trust);
if (rv != SECSuccess) {
rv = CERT_DecodeTrustString(&original_trust, "c,c,c");
}
CERTCertTrust new_trust;
rv = CERT_DecodeTrustString(&new_trust, "TCu,Cu,Tu");
if (rv != SECSuccess) {
LOG(ERROR) << "Cannot decode certificate trust string.";
return false;
}
rv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), cert_handle, &new_trust);
if (rv != SECSuccess) {
LOG(ERROR) << "Cannot change certificate trust.";
return false;
}
trust_cache_.push_back(new TrustEntry(cert_handle, original_trust));
return true;
}
void TestRootCerts::Clear() {
for (std::list<TrustEntry*>::reverse_iterator it = trust_cache_.rbegin();
it != trust_cache_.rend(); ++it) {
CERTCertTrust original_trust = (*it)->trust();
SECStatus rv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(),
(*it)->certificate(),
&original_trust);
DCHECK_EQ(SECSuccess, rv) << "Cannot restore certificate trust.";
}
STLDeleteElements(&trust_cache_);
}
bool TestRootCerts::IsEmpty() const {
return trust_cache_.empty();
}
#if defined(USE_NSS)
bool TestRootCerts::Contains(CERTCertificate* cert) const {
for (std::list<TrustEntry*>::const_iterator it = trust_cache_.begin();
it != trust_cache_.end(); ++it) {
if (X509Certificate::IsSameOSCert(cert, (*it)->certificate()))
return true;
}
return false;
}
#endif
TestRootCerts::~TestRootCerts() {
Clear();
}
void TestRootCerts::Init() {
crypto::EnsureNSSInit();
}
}