Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "components/certificate_transparency/log_proof_fetcher.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/strings/string_piece.h" | |
| 10 #include "base/values.h" | |
| 11 #include "components/certificate_transparency/log_response_parser.h" | |
| 12 #include "components/safe_json_parser/safe_json_parser.h" | |
| 13 #include "net/base/load_flags.h" | |
| 14 #include "net/base/request_priority.h" | |
| 15 #include "net/cert/ct_log_verifier.h" | |
| 16 #include "net/cert/signed_tree_head.h" | |
| 17 #include "net/url_request/url_request_context.h" | |
| 18 | |
| 19 const int kMaxSTHSizeInBytes = 600; | |
| 20 | |
| 21 namespace certificate_transparency { | |
| 22 | |
| 23 struct LogProofFetcher::FetchParams { | |
| 24 explicit FetchParams(const std::string& id, FetchSTHCallback callback); | |
| 25 ~FetchParams(); | |
| 26 | |
| 27 std::string log_id; | |
| 28 FetchSTHCallback fetched_callback; | |
| 29 scoped_refptr<net::IOBufferWithSize> sth_buffer; | |
| 30 int read_bytes; | |
| 31 }; | |
| 32 | |
| 33 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context) | |
| 34 : request_context_(request_context), weak_factory_(this) { | |
| 35 DCHECK(request_context); | |
| 36 } | |
| 37 | |
| 38 LogProofFetcher::~LogProofFetcher() { | |
| 39 for (auto it = inflight_requests_.begin(); it != inflight_requests_.end(); | |
|
Ryan Sleevi
2015/06/29 11:58:13
const auto& ||
for (inflight_request : inflight_r
| |
| 40 ++it) | |
| 41 it->first->Cancel(); | |
| 42 STLDeleteContainerPairPointers(inflight_requests_.begin(), | |
| 43 inflight_requests_.end()); | |
| 44 } | |
| 45 | |
| 46 scoped_ptr<net::URLRequest> LogProofFetcher::CreateURLRequest( | |
| 47 const GURL& fetch_sth_url) { | |
| 48 DCHECK(request_context_); | |
| 49 | |
| 50 scoped_ptr<net::URLRequest> request = request_context_->CreateRequest( | |
| 51 fetch_sth_url, net::DEFAULT_PRIORITY, this); | |
| 52 request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
|
Ryan Sleevi
2015/06/29 11:58:13
TBD: Whether or not we should send AUTH_DATA or an
| |
| 53 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 54 return request.Pass(); | |
| 55 } | |
| 56 | |
| 57 void LogProofFetcher::FetchSTH(const GURL& log_url, | |
| 58 const std::string& log_id, | |
| 59 FetchSTHCallback fetched_cb) { | |
| 60 GURL fetch_url(log_url.Resolve("ct/v1/get-sth")); | |
| 61 VLOG(1) << "About to fetch STH from " << fetch_url; | |
| 62 scoped_ptr<net::URLRequest> request = CreateURLRequest(fetch_url); | |
| 63 | |
| 64 request->set_method("GET"); | |
| 65 | |
| 66 net::URLRequest* raw_request = request.get(); | |
| 67 FetchParams* params = new FetchParams(log_id, fetched_cb); | |
| 68 inflight_requests_.insert(std::make_pair(request.release(), params)); | |
| 69 raw_request->Start(); | |
| 70 } | |
| 71 | |
| 72 void LogProofFetcher::OnResponseStarted(net::URLRequest* request) { | |
| 73 const net::URLRequestStatus& status(request->status()); | |
| 74 VLOG(1) << "Response started for fetch-sth from " << request->original_url(); | |
| 75 if (!status.is_success()) { | |
| 76 LOG(WARNING) << "Fetching STH from " << request->original_url() | |
| 77 << " failed. status:" << status.status() | |
| 78 << " error:" << status.error(); | |
| 79 CleanupRequest(request); | |
| 80 } else if (request->GetResponseCode() != 200) { | |
| 81 LOG(WARNING) << "Fetch STH HTTP status: " << request->GetResponseCode(); | |
| 82 CleanupRequest(request); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 auto it(inflight_requests_.find(request)); | |
| 87 DCHECK(it != inflight_requests_.end()); | |
| 88 scoped_refptr<net::IOBufferWithSize> buffer(it->second->sth_buffer); | |
| 89 VLOG(1) << "Kicking off read."; | |
| 90 request->Read(buffer.get(), buffer->size(), &(it->second->read_bytes)); | |
| 91 if (request->status().is_io_pending()) { | |
| 92 VLOG(1) << "IO Pending: Will wait."; | |
| 93 return; | |
| 94 } else { | |
| 95 VLOG(1) << "Data available: Invoking OnReadComplete."; | |
| 96 OnReadCompleted(request, it->second->read_bytes); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void LogProofFetcher::OnReadCompleted(net::URLRequest* request, | |
| 101 int bytes_read) { | |
| 102 VLOG(1) << "Read complete, got " << bytes_read << " bytes."; | |
| 103 RequestComplete(request, bytes_read); | |
| 104 } | |
| 105 | |
| 106 void LogProofFetcher::RequestComplete(net::URLRequest* request, | |
| 107 int bytes_read) { | |
| 108 auto it(inflight_requests_.find(request)); | |
| 109 DCHECK(it != inflight_requests_.end()); | |
| 110 if (it->first != request) { | |
| 111 NOTREACHED(); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 FetchParams* params(it->second); | |
| 116 VLOG(1) << "Got " << bytes_read << " bytes."; | |
| 117 | |
| 118 // Extract STH json an parse it | |
| 119 std::string json_sth(params->sth_buffer->data(), bytes_read); | |
| 120 VLOG(1) << "STH data: " << json_sth; | |
| 121 | |
| 122 FetchParams request_params(*params); | |
| 123 // Get rid of the buffer as it really isn't necessary. | |
| 124 request_params.sth_buffer = nullptr; | |
| 125 scoped_refptr<safe_json_parser::SafeJsonParser> parser = | |
|
Ryan Sleevi
2015/06/29 11:58:13
nit: s/parser = /parser(/
| |
| 126 new safe_json_parser::SafeJsonParser( | |
| 127 json_sth, | |
| 128 base::Bind(&LogProofFetcher::OnSTHJsonParseSuccess, | |
| 129 weak_factory_.GetWeakPtr(), | |
| 130 request_params), | |
| 131 base::Bind(&LogProofFetcher::OnSTHJsonParseError, | |
| 132 weak_factory_.GetWeakPtr(), | |
| 133 request_params)); | |
| 134 // The parser will call us back via one of the callbacks. | |
| 135 parser->Start(); | |
| 136 | |
| 137 CleanupRequest(request); | |
| 138 } | |
| 139 | |
| 140 void LogProofFetcher::CleanupRequest(net::URLRequest* request) { | |
| 141 VLOG(1) << "Cleaning up request to " << request->original_url(); | |
| 142 auto it(inflight_requests_.find(request)); | |
| 143 DCHECK(it != inflight_requests_.end()); | |
| 144 | |
| 145 scoped_ptr<FetchParams> params(it->second); | |
| 146 // Delete the request, remove from map. | |
| 147 scoped_ptr<net::URLRequest> url_request(it->first); | |
| 148 // Both pointers will be deleted when exiting the method. | |
| 149 VLOG(1) << "Erasing iterator."; | |
| 150 inflight_requests_.erase(it); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 void LogProofFetcher::OnSTHJsonParseSuccess( | |
| 155 FetchParams params, | |
| 156 scoped_ptr<base::Value> parsed_json) { | |
| 157 VLOG(1) << "Successfully parsed STH JSON."; | |
| 158 net::ct::SignedTreeHead sth; | |
| 159 if (!CTLogResponseParser::FillSignedTreeHead(parsed_json.Pass(), &sth)) { | |
| 160 LOG(ERROR) << "Invalid STH."; | |
| 161 return; | |
| 162 } | |
| 163 | |
| 164 VLOG(1) << "Parsed STH successfully."; | |
| 165 params.fetched_callback.Run(params.log_id, sth); | |
| 166 VLOG(1) << "Invoked callback."; | |
| 167 } | |
| 168 | |
| 169 void LogProofFetcher::OnSTHJsonParseError( | |
| 170 FetchParams params, | |
| 171 const std::string& error) { | |
| 172 VLOG(1) << "Parsing of STH JSON failed."; | |
| 173 } | |
| 174 | |
| 175 LogProofFetcher::FetchParams::FetchParams(const std::string& id, | |
| 176 FetchSTHCallback callback) | |
| 177 : log_id(id), | |
| 178 fetched_callback(callback), | |
| 179 sth_buffer(new net::IOBufferWithSize(kMaxSTHSizeInBytes)) { | |
| 180 } | |
| 181 | |
| 182 LogProofFetcher::FetchParams::~FetchParams() { | |
| 183 } | |
| 184 | |
| 185 } // namespace certificate_transparency | |
| OLD | NEW |