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