summaryrefslogtreecommitdiffstats
path: root/net/http/cert_status_cache.cc
blob: f96baec4f7c2fdb098838574f4e4d236d5a00eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "net/base/cert_status_flags.h"
#include "net/http/cert_status_cache.h"

namespace net {

CertStatusCache::CertStatusCache() {
}

CertStatusCache::~CertStatusCache() {
  for (HostMap::iterator iter = fingerprint_to_bad_hosts_.begin();
       iter != fingerprint_to_bad_hosts_.end(); ++iter) {
    delete iter->second;
  }
}

int CertStatusCache::GetCertStatus(const X509Certificate& cert,
                                   const std::string& host) const {
  StatusMap::const_iterator iter =
      fingerprint_to_cert_status_.find(cert.fingerprint());
  if (iter != fingerprint_to_cert_status_.end()) {
    int cert_status = iter->second;

    // We get the CERT_STATUS_COMMON_NAME_INVALID error based on the host.
    HostMap::const_iterator fp_iter =
        fingerprint_to_bad_hosts_.find(cert.fingerprint());
    if (fp_iter != fingerprint_to_bad_hosts_.end()) {
      StringSet* bad_hosts = fp_iter->second;
      StringSet::const_iterator host_iter = bad_hosts->find(host);
      if (host_iter != bad_hosts->end())
        cert_status |= net::CERT_STATUS_COMMON_NAME_INVALID;
    }

    return cert_status;
  }
  return 0;  // The cert has never had errors.
}

void CertStatusCache::SetCertStatus(const X509Certificate& cert,
                                    const std::string& host,
                                    int status) {
  // We store the CERT_STATUS_COMMON_NAME_INVALID status separately as it is
  // host related.
  fingerprint_to_cert_status_[cert.fingerprint()] =
      status & ~net::CERT_STATUS_COMMON_NAME_INVALID;

  if ((status & net::CERT_STATUS_COMMON_NAME_INVALID) != 0) {
    StringSet* bad_hosts;
    HostMap::const_iterator iter =
        fingerprint_to_bad_hosts_.find(cert.fingerprint());
    if (iter == fingerprint_to_bad_hosts_.end()) {
      bad_hosts = new StringSet;
      fingerprint_to_bad_hosts_[cert.fingerprint()] = bad_hosts;
    } else {
      bad_hosts = iter->second;
    }
    bad_hosts->insert(host);
  }
}

}