move SK_DISABLE_DITHER_32BIT_GRADIENT from SkUserConfig.h to skia.gyp, so we can...
[chromium-blink-merge.git] / net / base / cert_database_nss.cc
blobe0f4ca6850133487d0a46bae64427381af238519
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/cert_database.h"
7 #include <cert.h>
8 #include <pk11pub.h>
9 #include <secmod.h>
11 #include "base/logging.h"
12 #include "base/observer_list_threadsafe.h"
13 #include "crypto/nss_util.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/nss_cert_database.h"
16 #include "net/base/x509_certificate.h"
18 namespace net {
20 // Helper that observes events from the NSSCertDatabase and forwards them to
21 // the given CertDatabase.
22 class CertDatabase::Notifier : public NSSCertDatabase::Observer {
23 public:
24 explicit Notifier(CertDatabase* cert_db) : cert_db_(cert_db) {
25 NSSCertDatabase::GetInstance()->AddObserver(this);
28 virtual ~Notifier() {
29 NSSCertDatabase::GetInstance()->RemoveObserver(this);
32 // NSSCertDatabase::Observer implementation:
33 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE {
34 cert_db_->NotifyObserversOfCertAdded(cert);
37 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE {
38 cert_db_->NotifyObserversOfCertRemoved(cert);
41 virtual void OnCertTrustChanged(const X509Certificate* cert) OVERRIDE {
42 cert_db_->NotifyObserversOfCertTrustChanged(cert);
45 private:
46 CertDatabase* cert_db_;
48 DISALLOW_COPY_AND_ASSIGN(Notifier);
51 CertDatabase::CertDatabase()
52 : observer_list_(new ObserverListThreadSafe<Observer>) {
53 // Observe NSSCertDatabase events and forward them to observers of
54 // CertDatabase. This also makes sure that NSS has been initialized.
55 notifier_.reset(new Notifier(this));
58 CertDatabase::~CertDatabase() {}
60 int CertDatabase::CheckUserCert(X509Certificate* cert_obj) {
61 if (!cert_obj)
62 return ERR_CERT_INVALID;
63 if (cert_obj->HasExpired())
64 return ERR_CERT_DATE_INVALID;
66 // Check if the private key corresponding to the certificate exist
67 // We shouldn't accept any random client certificate sent by a CA.
69 // Note: The NSS source documentation wrongly suggests that this
70 // also imports the certificate if the private key exists. This
71 // doesn't seem to be the case.
73 CERTCertificate* cert = cert_obj->os_cert_handle();
74 PK11SlotInfo* slot = PK11_KeyForCertExists(cert, NULL, NULL);
75 if (!slot)
76 return ERR_NO_PRIVATE_KEY_FOR_CERT;
78 PK11_FreeSlot(slot);
80 return OK;
83 int CertDatabase::AddUserCert(X509Certificate* cert_obj) {
84 CERTCertificate* cert = cert_obj->os_cert_handle();
85 PK11SlotInfo* slot = NULL;
88 crypto::AutoNSSWriteLock lock;
89 slot = PK11_ImportCertForKey(
90 cert,
91 cert_obj->GetDefaultNickname(net::USER_CERT).c_str(),
92 NULL);
95 if (!slot) {
96 LOG(ERROR) << "Couldn't import user certificate.";
97 return ERR_ADD_USER_CERT_FAILED;
99 PK11_FreeSlot(slot);
100 NotifyObserversOfCertAdded(cert_obj);
101 return OK;
104 } // namespace net