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

Side by Side Diff: components/certificate_transparency/log_proof_fetcher.cc

Issue 1222953002: Certificate Transparency: Add STH Fetching capability. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merging with master Created 5 years, 5 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 unified diff | Download patch
OLDNEW
(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/strings/stringprintf.h"
11 #include "base/values.h"
12 #include "components/safe_json/safe_json_parser.h"
13 #include "net/base/load_flags.h"
14 #include "net/base/request_priority.h"
15 #include "net/cert/ct_log_response_parser.h"
16 #include "net/cert/ct_log_verifier.h"
17 #include "net/cert/signed_tree_head.h"
18 #include "net/url_request/url_request_context.h"
19
20 const int kMaxLogResponseSizeInBytes = 600;
21
22 namespace certificate_transparency {
23
24 struct LogProofFetcher::FetchParams {
davidben 2015/07/16 21:53:38 FetchState perhaps, since you're modifying it and
Eran Messeri 2015/07/17 15:40:23 Done.
25 explicit FetchParams(const std::string& id,
davidben 2015/07/16 21:53:38 This doesn't need the explicit.
Eran Messeri 2015/07/17 15:40:23 Done.
26 FetchSTHCallback fetched_callback,
27 FetchFailedCallback failed_callback);
28 ~FetchParams();
29
30 std::string log_id;
31 FetchSTHCallback fetched_callback;
32 FetchFailedCallback failed_callback;
33 scoped_refptr<net::IOBufferWithSize> response_buffer;
34 int read_bytes;
35 std::string assembled_response;
36 int total_response_size;
37 };
38
39 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context)
40 : request_context_(request_context), weak_factory_(this) {
davidben 2015/07/16 21:53:38 I think you can just define these methods all inli
Eran Messeri 2015/07/17 15:40:23 By "these methods", do you mean c'tor and d'tor? I
41 DCHECK(request_context);
42 }
43
44 LogProofFetcher::~LogProofFetcher() {
45 for (auto it = inflight_requests_.begin(); it != inflight_requests_.end();
46 ++it)
47 it->first->Cancel();
48 STLDeleteContainerPairPointers(inflight_requests_.begin(),
49 inflight_requests_.end());
50 }
51
52 void LogProofFetcher::FetchSTH(const GURL& log_url,
53 const std::string& log_id,
54 FetchSTHCallback fetched_cb,
55 FetchFailedCallback failed_cb) {
56 GURL fetch_url(log_url.Resolve("ct/v1/get-sth"));
57 scoped_ptr<net::URLRequest> request = CreateURLRequest(fetch_url);
58
59 FetchParams* params = new FetchParams(log_id, fetched_cb, failed_cb);
60 auto it = inflight_requests_.insert(std::make_pair(request.release(), params))
61 .first;
62 it->first->Start();
63 }
64
65 void LogProofFetcher::OnResponseStarted(net::URLRequest* request) {
66 const net::URLRequestStatus& status(request->status());
davidben 2015/07/16 21:53:39 Nit: I'd probably just use equals rather than pare
Eran Messeri 2015/07/17 15:40:23 Using equals throughout (except when a raw pointer
67 auto it(inflight_requests_.find(request));
68 auto params(it->second);
davidben 2015/07/16 21:53:38 Mind spelling the type out for this one? I think i
Eran Messeri 2015/07/17 15:40:23 Done.
69
70 DCHECK(it != inflight_requests_.end());
71 if (!status.is_success() || request->GetResponseCode() != 200) {
72 std::string error_string;
73 if (!status.is_success()) {
74 DVLOG(1) << "Fetching STH from " << request->original_url()
75 << " failed. status:" << status.status()
76 << " error:" << status.error();
77 error_string =
78 base::StringPrintf("%d %d", status.status(), status.error());
davidben 2015/07/16 21:53:39 This seems a slightly unhelpful error string. Wher
Eran Messeri 2015/07/17 15:40:23 You're right - the error string is unhelpful, as I
79 } else { // request->GetResponseCode != 200
80 DVLOG(1) << "Fetch STH HTTP status: " << request->GetResponseCode();
81 error_string = base::StringPrintf("HTTP %d", request->GetResponseCode());
82 }
83
84 scoped_ptr<FetchParams> params(CleanupRequest(request));
85 params->failed_callback.Run(params->log_id, error_string);
86 return;
87 }
88
89 bool continue_reading = true;
90 while (continue_reading) {
91 KickOffARead(request, params);
92 continue_reading = CheckReadStatus(request, params);
93 }
davidben 2015/07/16 21:53:39 This Read loop is somewhat interesting. :-) It doe
Eran Messeri 2015/07/17 15:40:23 Interesting as in "there are some odd edge cases i
94 }
95
96 void LogProofFetcher::OnReadCompleted(net::URLRequest* request,
97 int bytes_read) {
98 auto it(inflight_requests_.find(request));
99 DCHECK(it != inflight_requests_.end());
100 FetchParams* params(it->second);
101
102 params->read_bytes = bytes_read;
103
104 bool another_read = CheckReadStatus(request, params);
105
106 while (another_read) {
107 KickOffARead(request, params);
108 another_read = CheckReadStatus(request, params);
109 }
110 }
111
112 bool LogProofFetcher::CheckReadStatus(net::URLRequest* request,
113 FetchParams* params) {
114 const int bytes_read = params->read_bytes;
115 // Start by checking for an error condition.
116 if (bytes_read == -1 || !request->status().is_success()) {
117 auto status = request->status();
118 DVLOG(1) << "Read error: " << status.status() << " " << status.error();
119 scoped_ptr<FetchParams> params(CleanupRequest(request));
120 std::string error_string(
121 base::StringPrintf("%d %d", status.status(), status.error()));
davidben 2015/07/16 21:53:38 Ditto about not being a useful error string.
Eran Messeri 2015/07/17 15:40:23 Done.
122 params->failed_callback.Run(params->log_id, error_string);
123 return false;
124 }
125
126 // Not an error, but no data available, so wait for OnReadComplete
127 // callback.
128 if (request->status().is_io_pending()) {
129 DVLOG(1) << "IO Pending: Will wait.";
130 return false;
131 }
132
133 // Nothing more to read from the stream - finish handling the response.
134 if (bytes_read == 0) {
135 DVLOG(1) << "No more bytes to read, finishing.";
136 RequestComplete(request);
137 return false;
138 }
139
140 // We have data, collect it and indicate another read is needed.
141 DVLOG(1) << "Have " << bytes_read << " bytes to assemble.";
142 DCHECK(bytes_read > 0);
143 base::StringPiece sth_fragment(params->response_buffer->data(), bytes_read);
144 sth_fragment.AppendToString(&params->assembled_response);
145 params->total_response_size += bytes_read;
146 // Indicate this fragment was read.
147 params->read_bytes = 0;
148 return true;
149 }
150
151 void LogProofFetcher::KickOffARead(net::URLRequest* request,
152 FetchParams* params) {
153 params->read_bytes = 0;
154 request->Read(params->response_buffer.get(), params->response_buffer->size(),
155 &params->read_bytes);
156 }
157
158 void LogProofFetcher::RequestComplete(net::URLRequest* request) {
159 auto it(inflight_requests_.find(request));
160 DCHECK(it != inflight_requests_.end());
161 if (it->first != request) {
162 NOTREACHED();
163 return;
164 }
165
166 FetchParams* params = it->second;
167
168 // Extract STH json an parse it.
169 DCHECK(params->total_response_size < kMaxLogResponseSizeInBytes);
170
171 // Explicitly copy the request params as it'll be cleaned up by
172 // CleanupRequest later on.
173 FetchParams request_params(*params);
174 // Get rid of the buffer as it really isn't necessary.
175 request_params.response_buffer = nullptr;
176 safe_json::SafeJsonParser::Parse(
177 params->assembled_response,
178 base::Bind(&LogProofFetcher::OnSTHJsonParseSuccess,
179 weak_factory_.GetWeakPtr(), request_params),
180 base::Bind(&LogProofFetcher::OnSTHJsonParseError,
181 weak_factory_.GetWeakPtr(), request_params));
182
183 CleanupRequest(request);
184 }
185
186 scoped_ptr<LogProofFetcher::FetchParams> LogProofFetcher::CleanupRequest(
187 net::URLRequest* request) {
188 DVLOG(1) << "Cleaning up request to " << request->original_url();
189 auto it(inflight_requests_.find(request));
190 DCHECK(it != inflight_requests_.end());
191
192 scoped_ptr<FetchParams> params(it->second);
193 // Delete the request, remove from map.
194 scoped_ptr<net::URLRequest> url_request(it->first);
195 // Both pointers will be deleted when exiting the method.
196 inflight_requests_.erase(it);
197 return params.Pass();
198 }
199
200 scoped_ptr<net::URLRequest> LogProofFetcher::CreateURLRequest(
201 const GURL& fetch_sth_url) {
202 DCHECK(request_context_);
203
204 scoped_ptr<net::URLRequest> request = request_context_->CreateRequest(
205 fetch_sth_url, net::DEFAULT_PRIORITY, this);
206 request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
207 net::LOAD_DO_NOT_SAVE_COOKIES);
208 request->set_method("GET");
209 return request.Pass();
210 }
211
212 void LogProofFetcher::OnSTHJsonParseSuccess(
213 FetchParams params,
214 scoped_ptr<base::Value> parsed_json) {
215 net::ct::SignedTreeHead sth;
216 if (!net::ct::FillSignedTreeHead(*parsed_json.get(), &sth)) {
217 params.failed_callback.Run(params.log_id, "Invalid sth.");
218 return;
219 }
220
221 params.fetched_callback.Run(params.log_id, sth);
222 }
223
224 void LogProofFetcher::OnSTHJsonParseError(FetchParams params,
225 const std::string& error) {
226 params.failed_callback.Run(params.log_id, error);
227 }
228
229 LogProofFetcher::FetchParams::FetchParams(const std::string& id,
230 FetchSTHCallback fetched_callback,
231 FetchFailedCallback failed_callback)
232 : log_id(id),
233 fetched_callback(fetched_callback),
234 failed_callback(failed_callback),
235 response_buffer(new net::IOBufferWithSize(kMaxLogResponseSizeInBytes)),
236 total_response_size(0) {
237 }
238
239 LogProofFetcher::FetchParams::~FetchParams() {
240 }
241
242 } // namespace certificate_transparency
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698