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