Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1587)

Unified Diff: net/http/disk_based_cert_cache.cc

Issue 329733002: Disk Based Certificate Cache Implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improved flow and added set overwrite functionality. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/http/disk_based_cert_cache.cc
diff --git a/net/http/disk_based_cert_cache.cc b/net/http/disk_based_cert_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..45dec7b4fb9dfc4f07cd2b6093175f54656bc2bb
--- /dev/null
+++ b/net/http/disk_based_cert_cache.cc
@@ -0,0 +1,217 @@
+// Copyright (c) 2014 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/http/disk_based_cert_cache.h"
+
+#include <string>
wtc 2014/06/12 03:13:16 Nit: since the .h file includes <string>, the .cc
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/callback_helpers.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "base/strings/string_number_conversions.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/cert/x509_certificate.h"
+#include "net/disk_cache/disk_cache.h"
wtc 2014/06/12 03:13:16 The headers that are included by the .h file don't
+
+namespace net {
+
+DiskBasedCertCache::DiskBasedCertCache(disk_cache::Backend* backend)
+ : backend_(backend),
+ active_entry_(NULL),
+ state_(NONE),
+ create_failed_(false),
+ active_entry_size_(0),
+ weak_factory_(this) {
+ DCHECK(backend_);
+ io_callback_ =
+ base::Bind(&DiskBasedCertCache::OnIOComplete, weak_factory_.GetWeakPtr());
+}
+
+DiskBasedCertCache::~DiskBasedCertCache() {
+ weak_factory_.InvalidateWeakPtrs();
+}
+
+void DiskBasedCertCache::Get(
+ std::string& key,
+ base::Callback<void(X509Certificate::OSCertHandle cert_handle)> cb) {
+ user_read_callback_ = cb;
+
+ state_ = START_READ;
+
+ int rv = backend_->OpenEntry(key, &active_entry_, io_callback_);
+
+ DoLoop(rv);
+}
+
+int DiskBasedCertCache::DoStartRead(int rv) {
+ // todo(brandonsalmon) implement error handling
wtc 2014/06/12 03:13:16 Our convention is all caps TODO.
+ if (rv <= 0)
+ NOTIMPLEMENTED();
+
+ active_entry_size_ = active_entry_->GetDataSize(0 /* index */);
+ buffer = new IOBuffer(active_entry_size_);
+
+ state_ = FINISH_READ;
+
+ return active_entry_->ReadData(
+ 0 /* index */, 0 /* offset */, buffer, active_entry_size_, io_callback_);
+}
+
+int DiskBasedCertCache::DoFinishRead(int rv) {
+ // todo(brandonsalmon) implement error handling
+ if (rv <= 0)
+ NOTIMPLEMENTED();
+
+ if (user_read_callback_.is_null()) {
+ ResetState();
+ return OK; // todo(brandonsalmon): is this ERR_ABORTED?
+ }
+
+ active_cert_handle_ = X509Certificate::CreateOSCertHandleFromBytes(
+ buffer->data(), active_entry_size_);
+
+ CHECK(active_cert_handle_);
+
+ base::ResetAndReturn(&user_read_callback_).Run(active_cert_handle_);
+
+ ResetState();
+ return OK;
+}
+
+void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle,
+ base::Callback<void(const std::string&)> cb) {
+ DCHECK(!cb.is_null());
+
+ active_cert_handle_ = cert_handle;
+
+ state_ = CREATE_OR_OPEN;
+ user_write_callback_ = cb;
+ DoLoop(OK);
+}
+
+std::string DiskBasedCertCache::Key() {
+ CHECK(active_cert_handle_);
+
+ SHA1HashValue fingerprint =
+ X509Certificate::CalculateFingerprint(active_cert_handle_);
+
+ // should update to store the key so the data doesn't have to be encoded
+ // multiple times.
+
+ return "cert:" + base::HexEncode(fingerprint.data, 20);
wtc 2014/06/12 03:13:16 Avoid the use of 20. Try arraysize(fingerprint.dat
+}
+
+void DiskBasedCertCache::DoLoop(int rv) {
+ do {
+ switch (state_) {
+ case CREATE_OR_OPEN:
+ rv = DoCreateOrOpen(rv);
+ break;
+ case START_WRITE:
+ rv = DoStartWrite(rv);
+ break;
+ case FINISH_CREATE_OR_OPEN:
+ rv = DoFinishCreateOrOpen(rv);
+ break;
+ case FINISH_WRITE:
+ rv = DoFinishWrite(rv);
+ break;
+ case START_READ:
+ rv = DoStartRead(rv);
+ break;
+ case FINISH_READ:
+ rv = DoFinishRead(rv);
+ break;
+ case NONE:
+ break;
+ }
+ } while (rv != ERR_IO_PENDING && state_ != NONE);
+}
+
+void DiskBasedCertCache::OnIOComplete(int rv) {
+ // todo(brandonsalmon) Check for fatal errors?
+ DoLoop(rv);
+}
+
+int DiskBasedCertCache::DoStartWrite(int rv) {
+ // todo(brandonsalmon) implement error handling
+ if (rv <= 0)
+ NOTIMPLEMENTED();
+
+ std::string write_data;
+ // todo(brandonsalmon) deal with faulty encoding.
+ bool encoded =
+ X509Certificate::GetDEREncoded(active_cert_handle_, &write_data);
+
+ if (!encoded)
+ NOTIMPLEMENTED();
+
+ buffer = new IOBuffer(write_data.size());
+ memcpy(buffer->data(), write_data.data(), write_data.size());
+
+ state_ = FINISH_WRITE;
+
+ return active_entry_->WriteData(0 /* index */,
+ 0 /* offset */,
+ buffer,
+ write_data.size(),
+ io_callback_,
+ true /* truncate */);
+}
+
+int DiskBasedCertCache::DoCreateOrOpen(int rv) {
+ DCHECK(active_entry_ == NULL);
+
+ state_ = FINISH_CREATE_OR_OPEN;
+
+ if (create_failed_) {
+ return backend_->OpenEntry(Key(), &active_entry_, io_callback_);
+ }
+
+ return backend_->CreateEntry(Key(), &active_entry_, io_callback_);
+}
+
+int DiskBasedCertCache::DoFinishCreateOrOpen(int rv) {
+ // ERR_FAILED implies create entry failed, and we should try opening instead.
+ //!create_failed is checked to make sure we only try to open once.
+ if (rv == ERR_FAILED && !create_failed_) {
+ create_failed_ = true;
+ state_ = CREATE_OR_OPEN;
+ return OK;
+ } else if (rv <= 0) {
+ NOTIMPLEMENTED();
+ }
+
+ state_ = START_WRITE;
+ return OK;
+}
+
+int DiskBasedCertCache::DoFinishWrite(int rv) {
+ // todo(brandonsalmon) implement error handling
+ if (rv <= 0)
+ NOTIMPLEMENTED();
+
+ if (user_write_callback_.is_null()) {
+ ResetState();
+ return OK; // todo(brandonsalmon): is this ERR_ABORTED?
+ }
+
+ base::ResetAndReturn(&user_write_callback_).Run(Key());
+ ResetState();
+ return OK;
+}
+
+void DiskBasedCertCache::ResetState() {
+ state_ = NONE;
+ active_entry_->Close();
+ active_entry_ = NULL;
+ active_cert_handle_ = NULL;
+ user_write_callback_.Reset();
+ user_read_callback_.Reset();
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698