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

Unified Diff: components/certificate_transparency/log_proof_fetcher.cc

Issue 1405293009: Certificate Transparency: Fetching consistency proofs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing review comments. Created 5 years, 1 month 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: components/certificate_transparency/log_proof_fetcher.cc
diff --git a/components/certificate_transparency/log_proof_fetcher.cc b/components/certificate_transparency/log_proof_fetcher.cc
index 7e51858708b0acb5d60dbd550cba646ff6ac5ab6..b4016c04210928fc96efac70595272801b25d1c1 100644
--- a/components/certificate_transparency/log_proof_fetcher.cc
+++ b/components/certificate_transparency/log_proof_fetcher.cc
@@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/stl_util.h"
+#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "components/safe_json/safe_json_parser.h"
#include "net/base/io_buffer.h"
@@ -40,16 +41,22 @@ int GetNetErrorFromURLRequestStatus(const net::URLRequestStatus& status) {
}
}
+enum LogRequestType { SIGNED_TREE_HEAD, CONSISTENCY_PROOF };
+
} // namespace
struct LogProofFetcher::FetchState {
FetchState(const std::string& log_id,
- const SignedTreeHeadFetchedCallback& fetched_callback,
+ LogRequestType request_type,
+ SignedTreeHeadFetchedCallback sth_fetch_callback,
+ ConsistencyProofFetchedCallback proof_fetch_callback,
svaldez 2015/11/16 17:33:07 const ...FetchedCallback&
Eran Messeri 2015/11/17 10:47:31 Done.
const FetchFailedCallback& failed_callback);
~FetchState();
std::string log_id;
- SignedTreeHeadFetchedCallback fetched_callback;
+ LogRequestType request_type;
+ SignedTreeHeadFetchedCallback sth_fetch_callback;
+ ConsistencyProofFetchedCallback proof_fetch_callback;
FetchFailedCallback failed_callback;
scoped_refptr<net::IOBufferWithSize> response_buffer;
std::string assembled_response;
@@ -57,12 +64,18 @@ struct LogProofFetcher::FetchState {
LogProofFetcher::FetchState::FetchState(
const std::string& log_id,
- const SignedTreeHeadFetchedCallback& fetched_callback,
+ LogRequestType request_type,
+ SignedTreeHeadFetchedCallback sth_fetch_callback,
+ ConsistencyProofFetchedCallback proof_fetch_callback,
const FetchFailedCallback& failed_callback)
: log_id(log_id),
- fetched_callback(fetched_callback),
+ request_type(request_type),
+ sth_fetch_callback(sth_fetch_callback),
+ proof_fetch_callback(proof_fetch_callback),
failed_callback(failed_callback),
- response_buffer(new net::IOBufferWithSize(kMaxLogResponseSizeInBytes)) {}
+ response_buffer(new net::IOBufferWithSize(kMaxLogResponseSizeInBytes)) {
+ DCHECK(!(sth_fetch_callback.is_null() && proof_fetch_callback.is_null()));
+}
LogProofFetcher::FetchState::~FetchState() {}
@@ -83,16 +96,36 @@ void LogProofFetcher::FetchSignedTreeHead(
const FetchFailedCallback& failed_callback) {
DCHECK(base_log_url.SchemeIsHTTPOrHTTPS());
GURL fetch_url(base_log_url.Resolve("ct/v1/get-sth"));
- scoped_ptr<net::URLRequest> request =
- request_context_->CreateRequest(fetch_url, net::DEFAULT_PRIORITY, this);
- request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
- net::LOAD_DO_NOT_SAVE_COOKIES |
- net::LOAD_DO_NOT_SEND_AUTH_DATA);
+ net::URLRequest* request = CreateRequest(fetch_url);
FetchState* fetch_state =
- new FetchState(log_id, fetched_callback, failed_callback);
+ new FetchState(log_id, LogRequestType::SIGNED_TREE_HEAD, fetched_callback,
+ ConsistencyProofFetchedCallback(), failed_callback);
svaldez 2015/11/16 17:33:07 Might be able to just pass 'nullptr'?
Eran Messeri 2015/11/17 10:47:31 Can't - Callback doesn't have a single argument c'
request->Start();
- inflight_requests_.insert(std::make_pair(request.release(), fetch_state));
+ inflight_requests_.insert(std::make_pair(request, fetch_state));
+}
+
+void LogProofFetcher::FetchConsistencyProof(
+ const GURL& base_log_url,
+ const std::string& log_id,
+ size_t old_tree_size,
+ size_t new_tree_size,
+ const ConsistencyProofFetchedCallback& fetched_callback,
+ const FetchFailedCallback& failed_callback) {
+ DCHECK(base_log_url.SchemeIsHTTPOrHTTPS());
+
+ std::string relative =
+ base::StringPrintf("ct/v1/get-sth-consistency?first=%lu&second=%lu",
+ old_tree_size, new_tree_size);
+ GURL fetch_url = base_log_url.Resolve(relative);
+
+ net::URLRequest* request = CreateRequest(fetch_url);
+
+ FetchState* fetch_state = new FetchState(
+ log_id, LogRequestType::CONSISTENCY_PROOF,
+ SignedTreeHeadFetchedCallback(), fetched_callback, failed_callback);
svaldez 2015/11/16 17:33:07 ditto.
Eran Messeri 2015/11/17 10:47:31 See comment above.
+ request->Start();
+ inflight_requests_.insert(std::make_pair(request, fetch_state));
}
void LogProofFetcher::OnResponseStarted(net::URLRequest* request) {
@@ -185,12 +218,23 @@ void LogProofFetcher::RequestComplete(net::URLRequest* request) {
// Get rid of the buffer as it really isn't necessary.
fetch_state->response_buffer = nullptr;
- safe_json::SafeJsonParser::Parse(
- fetch_state->assembled_response,
- base::Bind(&LogProofFetcher::OnSTHJsonParseSuccess,
- weak_factory_.GetWeakPtr(), request),
- base::Bind(&LogProofFetcher::OnSTHJsonParseError,
- weak_factory_.GetWeakPtr(), request));
+ if (fetch_state->request_type == LogRequestType::SIGNED_TREE_HEAD) {
+ safe_json::SafeJsonParser::Parse(
+ fetch_state->assembled_response,
+ base::Bind(&LogProofFetcher::OnSTHJsonParseSuccess,
+ weak_factory_.GetWeakPtr(), request),
+ base::Bind(&LogProofFetcher::OnSTHJsonParseError,
+ weak_factory_.GetWeakPtr(), request));
+ } else if (fetch_state->request_type == LogRequestType::CONSISTENCY_PROOF) {
+ safe_json::SafeJsonParser::Parse(
+ fetch_state->assembled_response,
+ base::Bind(&LogProofFetcher::OnConsistencyProofJsonParseSuccess,
+ weak_factory_.GetWeakPtr(), request),
+ base::Bind(&LogProofFetcher::OnConsistencyProofJsonParseError,
+ weak_factory_.GetWeakPtr(), request));
+ } else {
+ NOTREACHED();
+ }
}
void LogProofFetcher::CleanupRequest(net::URLRequest* request) {
@@ -225,7 +269,8 @@ void LogProofFetcher::OnSTHJsonParseSuccess(
FetchState* fetch_state = inflight_requests_.find(request)->second;
net::ct::SignedTreeHead signed_tree_head;
if (net::ct::FillSignedTreeHead(*parsed_json.get(), &signed_tree_head)) {
- fetch_state->fetched_callback.Run(fetch_state->log_id, signed_tree_head);
+ DCHECK(!(fetch_state->sth_fetch_callback.is_null()));
+ fetch_state->sth_fetch_callback.Run(fetch_state->log_id, signed_tree_head);
} else {
fetch_state->failed_callback.Run(fetch_state->log_id,
net::ERR_CT_STH_INCOMPLETE, net::HTTP_OK);
@@ -239,4 +284,39 @@ void LogProofFetcher::OnSTHJsonParseError(net::URLRequest* request,
InvokeFailureCallback(request, net::ERR_CT_STH_PARSING_FAILED, net::HTTP_OK);
}
+void LogProofFetcher::OnConsistencyProofJsonParseSuccess(
+ net::URLRequest* request,
+ scoped_ptr<base::Value> parsed_json) {
+ DCHECK(inflight_requests_.count(request));
+ FetchState* fetch_state = inflight_requests_.find(request)->second;
+ std::vector<std::string> consistency_proof;
+ if (net::ct::FillConsistencyProof(*parsed_json.get(), &consistency_proof)) {
+ DCHECK(!(fetch_state->proof_fetch_callback.is_null()));
+ fetch_state->proof_fetch_callback.Run(fetch_state->log_id,
+ consistency_proof);
+ } else {
+ fetch_state->failed_callback.Run(
+ fetch_state->log_id, net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED,
+ net::HTTP_OK);
+ }
+
+ CleanupRequest(request);
+}
+
+void LogProofFetcher::OnConsistencyProofJsonParseError(
+ net::URLRequest* request,
+ const std::string& error) {
+ InvokeFailureCallback(request, net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED,
+ net::HTTP_OK);
+}
+
+net::URLRequest* LogProofFetcher::CreateRequest(const GURL& url) {
+ scoped_ptr<net::URLRequest> request =
+ request_context_->CreateRequest(url, net::DEFAULT_PRIORITY, this);
+ request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
+ net::LOAD_DO_NOT_SAVE_COOKIES |
+ net::LOAD_DO_NOT_SEND_AUTH_DATA);
+ return request.release();
+}
+
} // namespace certificate_transparency

Powered by Google App Engine
This is Rietveld 408576698