File: log_proof_fetcher.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (129 lines) | stat: -rw-r--r-- 4,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_
#define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_

#include <stddef.h>
#include <stdint.h>

#include <set>
#include <string>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"

namespace net {

class URLRequestContext;

namespace ct {
struct SignedTreeHead;
}  // namespace ct

}  // namespace net

class GURL;

namespace certificate_transparency {

class LogResponseHandler;

// Fetches Signed Tree Heads (STHs) and consistency proofs from Certificate
// Transparency logs using the URLRequestContext provided during the instance
// construction.
// Must outlive the provided URLRequestContext.
class LogProofFetcher {
 public:
  // Buffer size for log replies - currently the reply to
  // get-consistency-proof is the biggest one this class handles. 1500 bytes
  // should be enough to accommodate 31 proof nodes + JSON overhead, supporting
  // trees with up to 100 million entries.
  static const size_t kMaxLogResponseSizeInBytes = 1500;

  // Callback for successful retrieval of Signed Tree Heads. Called
  // with the log_id of the log the STH belogs to (as supplied by the caller
  // to FetchSignedTreeHead) and the STH itself.
  using SignedTreeHeadFetchedCallback =
      base::Callback<void(const std::string& log_id,
                          const net::ct::SignedTreeHead& signed_tree_head)>;

  // Callback for failure of Signed Tree Head retrieval. Called with the log_id
  // that the log fetching was requested for and a net error code of the
  // failure.
  // |http_response_code| is meaningful only if |net_error| is net::OK.
  using FetchFailedCallback = base::Callback<
      void(const std::string& log_id, int net_error, int http_response_code)>;

  // Callback for successful retrieval of consistency proofs between two
  // STHs. Called with the log_id of the log the consistency belongs to (as
  // supplied by the caller to FetchConsistencyProof) and the vector of
  // proof nodes.
  using ConsistencyProofFetchedCallback =
      base::Callback<void(const std::string& log_id,
                          const std::vector<std::string>& consistency_proof)>;

  explicit LogProofFetcher(net::URLRequestContext* request_context);
  ~LogProofFetcher();

  // Fetch the latest Signed Tree Head from the log identified by |log_id|
  // from |base_log_url|. The |log_id| will be passed into the callbacks to
  // identify the log the retrieved Signed Tree Head belongs to.
  // The callbacks won't be invoked if the request is destroyed before
  // fetching is completed.
  // It is possible, but does not make a lot of sense, to have multiple
  // Signed Tree Head fetching requests going out to the same log, since
  // they are likely to return the same result.
  // TODO(eranm): Think further about whether multiple requests to the same
  // log imply cancellation of previous requests, should be coalesced or handled
  // independently.
  void FetchSignedTreeHead(
      const GURL& base_log_url,
      const std::string& log_id,
      const SignedTreeHeadFetchedCallback& fetched_callback,
      const FetchFailedCallback& failed_callback);

  // Fetch a consistency proof between the Merkle trees identified by
  // |old_tree_size| and |new_tree_size| of the log identified by |log_id|
  // from |base_log_url|.
  //
  // See the documentation of FetchSignedTreeHead regarding request destruction
  // and multiple requests to the same log.
  void FetchConsistencyProof(
      const GURL& base_log_url,
      const std::string& log_id,
      uint64_t old_tree_size,
      uint64_t new_tree_size,
      const ConsistencyProofFetchedCallback& fetched_callback,
      const FetchFailedCallback& failed_callback);

 private:
  // Starts the fetch (by delegating to the LogResponseHandler)
  // and stores the |log_handler| in |inflight_fetches_| for later
  // cleanup.
  void StartFetch(const GURL& request_url,
                  std::unique_ptr<LogResponseHandler> log_request);

  // Callback for when the fetch was done (successfully or not).
  // Deletes, and removes, the |log_handler| from the |inflight_fetches_|.
  // Additionally, invokes |caller_callback| which is typically
  // one of the callbacks provided to the Fetch... method, bound to
  // success/failure parameters.
  void OnFetchDone(LogResponseHandler* log_handler,
                   const base::Closure& caller_callback);

  net::URLRequestContext* const request_context_;

  std::set<std::unique_ptr<LogResponseHandler>> inflight_fetches_;

  base::WeakPtrFactory<LogProofFetcher> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(LogProofFetcher);
};

}  // namespace certificate_transparency

#endif  // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_