summaryrefslogtreecommitdiffstats
path: root/net/base/default_server_bound_cert_store.cc
blob: 5f0cf921178f41e55e40b3fb6ca5674b1e4d2816 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/default_server_bound_cert_store.h"

#include "base/bind.h"
#include "base/message_loop.h"

namespace net {

// static
const size_t DefaultServerBoundCertStore::kMaxCerts = 3300;

DefaultServerBoundCertStore::DefaultServerBoundCertStore(
    PersistentStore* store)
    : initialized_(false),
      store_(store) {}

void DefaultServerBoundCertStore::FlushStore(
    const base::Closure& completion_task) {
  base::AutoLock autolock(lock_);

  if (initialized_ && store_)
    store_->Flush(completion_task);
  else if (!completion_task.is_null())
    MessageLoop::current()->PostTask(FROM_HERE, completion_task);
}

bool DefaultServerBoundCertStore::GetServerBoundCert(
    const std::string& server_identifier,
    SSLClientCertType* type,
    base::Time* creation_time,
    base::Time* expiration_time,
    std::string* private_key_result,
    std::string* cert_result) {
  base::AutoLock autolock(lock_);
  InitIfNecessary();

  ServerBoundCertMap::iterator it = server_bound_certs_.find(server_identifier);

  if (it == server_bound_certs_.end())
    return false;

  ServerBoundCert* cert = it->second;
  *type = cert->type();
  *creation_time = cert->creation_time();
  *expiration_time = cert->expiration_time();
  *private_key_result = cert->private_key();
  *cert_result = cert->cert();

  return true;
}

void DefaultServerBoundCertStore::SetServerBoundCert(
    const std::string& server_identifier,
    SSLClientCertType type,
    base::Time creation_time,
    base::Time expiration_time,
    const std::string& private_key,
    const std::string& cert) {
  base::AutoLock autolock(lock_);
  InitIfNecessary();

  InternalDeleteServerBoundCert(server_identifier);
  InternalInsertServerBoundCert(
      server_identifier,
      new ServerBoundCert(
          server_identifier, type, creation_time, expiration_time, private_key,
          cert));
}

void DefaultServerBoundCertStore::DeleteServerBoundCert(
    const std::string& server_identifier) {
  base::AutoLock autolock(lock_);
  InitIfNecessary();
  InternalDeleteServerBoundCert(server_identifier);
}

void DefaultServerBoundCertStore::DeleteAllCreatedBetween(
    base::Time delete_begin,
    base::Time delete_end) {
  base::AutoLock autolock(lock_);
  InitIfNecessary();
  for (ServerBoundCertMap::iterator it = server_bound_certs_.begin();
       it != server_bound_certs_.end();) {
    ServerBoundCertMap::iterator cur = it;
    ++it;
    ServerBoundCert* cert = cur->second;
    if ((delete_begin.is_null() || cert->creation_time() >= delete_begin) &&
        (delete_end.is_null() || cert->creation_time() < delete_end)) {
      if (store_)
        store_->DeleteServerBoundCert(*cert);
      delete cert;
      server_bound_certs_.erase(cur);
    }
  }
}

void DefaultServerBoundCertStore::DeleteAll() {
  DeleteAllCreatedBetween(base::Time(), base::Time());
}

void DefaultServerBoundCertStore::GetAllServerBoundCerts(
    ServerBoundCertList* server_bound_certs) {
  base::AutoLock autolock(lock_);
  InitIfNecessary();
  for (ServerBoundCertMap::iterator it = server_bound_certs_.begin();
       it != server_bound_certs_.end(); ++it) {
    server_bound_certs->push_back(*it->second);
  }
}

int DefaultServerBoundCertStore::GetCertCount() {
  base::AutoLock autolock(lock_);
  InitIfNecessary();

  return server_bound_certs_.size();
}

void DefaultServerBoundCertStore::SetForceKeepSessionState() {
  base::AutoLock autolock(lock_);
  InitIfNecessary();

  if (store_)
    store_->SetForceKeepSessionState();
}

DefaultServerBoundCertStore::~DefaultServerBoundCertStore() {
  DeleteAllInMemory();
}

void DefaultServerBoundCertStore::DeleteAllInMemory() {
  base::AutoLock autolock(lock_);

  for (ServerBoundCertMap::iterator it = server_bound_certs_.begin();
       it != server_bound_certs_.end(); ++it) {
    delete it->second;
  }
  server_bound_certs_.clear();
}

void DefaultServerBoundCertStore::InitStore() {
  lock_.AssertAcquired();

  DCHECK(store_) << "Store must exist to initialize";

  // Initialize the store and sync in any saved persistent certs.
  std::vector<ServerBoundCert*> certs;
  // Reserve space for the maximum amount of certs a database should have.
  // This prevents multiple vector growth / copies as we append certs.
  certs.reserve(kMaxCerts);
  store_->Load(&certs);

  for (std::vector<ServerBoundCert*>::const_iterator it = certs.begin();
       it != certs.end(); ++it) {
    server_bound_certs_[(*it)->server_identifier()] = *it;
  }
}

void DefaultServerBoundCertStore::InternalDeleteServerBoundCert(
    const std::string& server_identifier) {
  lock_.AssertAcquired();

  ServerBoundCertMap::iterator it = server_bound_certs_.find(server_identifier);
  if (it == server_bound_certs_.end())
    return;  // There is nothing to delete.

  ServerBoundCert* cert = it->second;
  if (store_)
    store_->DeleteServerBoundCert(*cert);
  server_bound_certs_.erase(it);
  delete cert;
}

void DefaultServerBoundCertStore::InternalInsertServerBoundCert(
    const std::string& server_identifier,
    ServerBoundCert* cert) {
  lock_.AssertAcquired();

  if (store_)
    store_->AddServerBoundCert(*cert);
  server_bound_certs_[server_identifier] = cert;
}

DefaultServerBoundCertStore::PersistentStore::PersistentStore() {}

DefaultServerBoundCertStore::PersistentStore::~PersistentStore() {}

}  // namespace net