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

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

Issue 1405293009: Certificate Transparency: Fetching consistency proofs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switching to simpler callbacks model Created 4 years, 11 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_
6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h>
9 10
10 #include <map> 11 #include <set>
11 #include <string> 12 #include <string>
13 #include <vector>
12 14
13 #include "base/callback.h" 15 #include "base/callback.h"
14 #include "base/macros.h" 16 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
17 #include "net/url_request/url_request.h"
18 19
19 namespace base { 20 namespace base {
20 class Value; 21 class Value;
21 } // namespace base 22 } // namespace base
22 23
23 namespace net { 24 namespace net {
24 25
25 class URLRequestContext; 26 class URLRequestContext;
26 27
27 namespace ct { 28 namespace ct {
28 struct SignedTreeHead; 29 struct SignedTreeHead;
29 } // namespace ct 30 } // namespace ct
30 31
31 } // namespace net 32 } // namespace net
32 33
33 class GURL; 34 class GURL;
34 35
35 namespace certificate_transparency { 36 namespace certificate_transparency {
36 37
37 // Fetches Signed Tree Heads (STHs) and consistency proofs from Certificate 38 // Fetches Signed Tree Heads (STHs) and consistency proofs from Certificate
38 // Transparency logs using the URLRequestContext provided during the instance 39 // Transparency logs using the URLRequestContext provided during the instance
39 // construction. 40 // construction.
40 // Must outlive the provided URLRequestContext. 41 // Must outlive the provided URLRequestContext.
41 class LogProofFetcher : public net::URLRequest::Delegate { 42 class LogProofFetcher {
42 public: 43 public:
43 static const size_t kMaxLogResponseSizeInBytes = 600; 44 // Buffer size for log replies - currently the reply to
45 // get-consistency-proof is the biggest one this class handles. 1500 bytes
46 // should be enough to accommodate 31 proof nodes + JSON overhead, supporting
47 // trees with up to 100 million entries.
48 static const size_t kMaxLogResponseSizeInBytes = 1500;
44 49
45 // Callback for successful retrieval of Signed Tree Heads. Called 50 // Callback for successful retrieval of Signed Tree Heads. Called
46 // with the log_id of the log the STH belogs to (as supplied by the caller 51 // with the log_id of the log the STH belogs to (as supplied by the caller
47 // to FetchSignedTreeHead) and the STH itself. 52 // to FetchSignedTreeHead) and the STH itself.
48 using SignedTreeHeadFetchedCallback = 53 using SignedTreeHeadFetchedCallback =
49 base::Callback<void(const std::string& log_id, 54 base::Callback<void(const std::string& log_id,
50 const net::ct::SignedTreeHead& signed_tree_head)>; 55 const net::ct::SignedTreeHead& signed_tree_head)>;
51 56
52 // Callback for failure of Signed Tree Head retrieval. Called with the log_id 57 // Callback for failure of Signed Tree Head retrieval. Called with the log_id
53 // that the log fetching was requested for and a net error code of the 58 // that the log fetching was requested for and a net error code of the
54 // failure. 59 // failure.
55 using FetchFailedCallback = base::Callback< 60 using FetchFailedCallback = base::Callback<
56 void(const std::string& log_id, int net_error, int http_response_code)>; 61 void(const std::string& log_id, int net_error, int http_response_code)>;
57 62
63 // Callback for successful retrieval of consistency proofs between two
64 // STHs. Called with the log_id of the log the consistency belongs to (as
65 // supplied by the caller to FetchConsistencyProof) and the vector of
66 // proof nodes.
67 using ConsistencyProofFetchedCallback =
68 base::Callback<void(const std::string& log_id,
69 const std::vector<std::string>& consistency_proof)>;
70
58 explicit LogProofFetcher(net::URLRequestContext* request_context); 71 explicit LogProofFetcher(net::URLRequestContext* request_context);
59 ~LogProofFetcher() override; 72 ~LogProofFetcher();
60 73
61 // Fetch the latest Signed Tree Head from the log identified by |log_id| 74 // Fetch the latest Signed Tree Head from the log identified by |log_id|
62 // from |base_log_url|. The |log_id| will be passed into the callbacks to 75 // from |base_log_url|. The |log_id| will be passed into the callbacks to
63 // identify the log the retrieved Signed Tree Head belongs to. 76 // identify the log the retrieved Signed Tree Head belongs to.
64 // The callbacks won't be invoked if the request is destroyed before 77 // The callbacks won't be invoked if the request is destroyed before
65 // fetching is completed. 78 // fetching is completed.
66 // It is possible, but does not make a lot of sense, to have multiple 79 // It is possible, but does not make a lot of sense, to have multiple
67 // Signed Tree Head fetching requests going out to the same log, since 80 // Signed Tree Head fetching requests going out to the same log, since
68 // they are likely to return the same result. 81 // they are likely to return the same result.
69 // TODO(eranm): Think further about whether multiple requests to the same 82 // TODO(eranm): Think further about whether multiple requests to the same
70 // log imply cancellation of previous requests, should be coalesced or handled 83 // log imply cancellation of previous requests, should be coalesced or handled
71 // independently. 84 // independently.
72 void FetchSignedTreeHead( 85 void FetchSignedTreeHead(
73 const GURL& base_log_url, 86 const GURL& base_log_url,
74 const std::string& log_id, 87 const std::string& log_id,
75 const SignedTreeHeadFetchedCallback& fetched_callback, 88 const SignedTreeHeadFetchedCallback& fetched_callback,
76 const FetchFailedCallback& failed_callback); 89 const FetchFailedCallback& failed_callback);
77 90
78 // net::URLRequest::Delegate 91 // Fetch a consistency proof between the Merkle trees identified by
79 void OnResponseStarted(net::URLRequest* request) override; 92 // |old_tree_size| and |new_tree_size| of the log identified by |log_id|
80 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; 93 // from |base_log_url|.
94 //
95 // See the documentation of FetchSignedTreeHead regarding request destruction
96 // and multiple requests to the same log.
97 void FetchConsistencyProof(
98 const GURL& base_log_url,
99 const std::string& log_id,
100 uint64_t old_tree_size,
101 uint64_t new_tree_size,
102 const ConsistencyProofFetchedCallback& fetched_callback,
103 const FetchFailedCallback& failed_callback);
81 104
82 private: 105 private:
83 struct FetchState; 106 class LogResponseHandler;
84 // Handles the final result of a URLRequest::Read call on |request|. 107 class GetSTHLogResponseHandler;
85 // Returns true if another read should be started, false if the read 108 class GetConsistencyProofLogResponseHandler;
86 // failed completely or we have to wait for OnResponseStarted to 109 class LogFetcher;
Ryan Sleevi 2016/01/23 00:34:23 So LogFetcher definitely doesn't need to be pre-de
Eran Messeri 2016/01/25 17:07:38 Done (here and in the cc, obviously)
87 // be called.
88 bool HandleReadResult(net::URLRequest* request,
89 FetchState* params,
90 int bytes_read);
91 110
92 // Calls URLRequest::Read on |request| repeatedly, until HandleReadResult 111 // Starts the fetch (by delegating to the LogResponseHandler)
93 // indicates it should no longer be called. Usually this would be when there 112 // and stores the |log_handler| in |inflight_fetches_| for later
94 // is pending IO that requires waiting for OnResponseStarted to be called. 113 // cleanup.
95 void StartNextRead(net::URLRequest* request, FetchState* params); 114 void StartFetch(const GURL& request_url, LogResponseHandler* log_handler);
96 115
97 // Performs post-report cleanup. 116 // Callback for when the fetch was done (successfully or not).
98 void RequestComplete(net::URLRequest* request); 117 // Deletes, and removes, the |log_handler| from the |inflight_fetches_|.
99 // Deletes the request and associated FetchState from the internal map. 118 // Additionally, invokes |caller_callback| which is typically
100 void CleanupRequest(net::URLRequest* request); 119 // one of the callbacks provided to the Fetch... method, bound to
101 // Invokes the failure callback with the supplied arguments, then cleans up 120 // success/failure parameters.
102 // the request. 121 void OnFetchDone(LogResponseHandler* log_handler,
103 void InvokeFailureCallback(net::URLRequest* request, 122 const base::Closure& caller_callback);
104 int net_error,
105 int http_response_code);
106
107 // Callbacks for parsing the STH's JSON by the SafeJsonParser
108 void OnSTHJsonParseSuccess(net::URLRequest* request,
109 scoped_ptr<base::Value> parsed_json);
110 void OnSTHJsonParseError(net::URLRequest* request, const std::string& error);
111 123
112 net::URLRequestContext* const request_context_; 124 net::URLRequestContext* const request_context_;
113 125
114 // Owns the contained requests, as well as FetchState. 126 std::set<LogResponseHandler*> inflight_fetches_;
115 std::map<net::URLRequest*, FetchState*> inflight_requests_;
116 127
117 base::WeakPtrFactory<LogProofFetcher> weak_factory_; 128 base::WeakPtrFactory<LogProofFetcher> weak_factory_;
118 129
119 DISALLOW_COPY_AND_ASSIGN(LogProofFetcher); 130 DISALLOW_COPY_AND_ASSIGN(LogProofFetcher);
120 }; 131 };
121 132
122 } // namespace certificate_transparency 133 } // namespace certificate_transparency
123 134
124 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ 135 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698