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