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

Side by Side Diff: net/base/origin_bound_cert_service.temp.h

Issue 7565023: Gave the GetOriginBoundCertificate an asynchronous interface because certificate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 NET_BASE_CERT_VERIFIER_H_ 5 #ifndef NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_
6 #define NET_BASE_CERT_VERIFIER_H_ 6 #define NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h" 15 #include "base/threading/non_thread_safe.h"
15 #include "base/time.h"
16 #include "net/base/cert_database.h"
17 #include "net/base/cert_verify_result.h"
18 #include "net/base/completion_callback.h" 16 #include "net/base/completion_callback.h"
19 #include "net/base/net_api.h" 17 #include "net/base/net_api.h"
20 #include "net/base/x509_cert_types.h"
21 18
22 namespace net { 19 namespace net {
23 20
24 class CertVerifierJob; 21 class OriginBoundCertServiceJob;
25 class CertVerifierWorker; 22 class OriginBoundCertServiceWorker;
26 class X509Certificate; 23 class OriginBoundCertStore;
27 24
28 // CachedCertVerifyResult contains the result of a certificate verification. 25 // A class for creating and fetching origin bound certs.
29 struct CachedCertVerifyResult { 26 // Inherits from NonThreadSafe in order to use the function
30 CachedCertVerifyResult(); 27 // |CalledOnValidThread|.
31 ~CachedCertVerifyResult(); 28 class NET_API OriginBoundCertService
32 29 : public base::RefCountedThreadSafe<OriginBoundCertService>,
33 // Returns true if |current_time| is greater than or equal to |expiry|. 30 NON_EXPORTED_BASE(public base::NonThreadSafe) {
34 bool HasExpired(base::Time current_time) const;
35
36 int error; // The return value of CertVerifier::Verify.
37 CertVerifyResult result; // The output of CertVerifier::Verify.
38
39 // The time at which the certificate verification result expires.
40 base::Time expiry;
41 };
42
43 // CertVerifier represents a service for verifying certificates.
44 //
45 // CertVerifier can handle multiple requests at a time, so when canceling a
46 // request the RequestHandle that was returned by Verify() needs to be
47 // given. A simpler alternative for consumers that only have 1 outstanding
48 // request at a time is to create a SingleRequestCertVerifier wrapper around
49 // CertVerifier (which will automatically cancel the single request when it
50 // goes out of scope).
51 class NET_API CertVerifier : NON_EXPORTED_BASE(public base::NonThreadSafe),
52 public CertDatabase::Observer {
53 public: 31 public:
54 // Opaque type used to cancel a request. 32 // Opaque type used to cancel a request.
55 typedef void* RequestHandle; 33 typedef void* RequestHandle;
56 34
57 // CertVerifier must not call base::Time::Now() directly. It must call 35 // This object owns origin_bound_cert_store.
58 // time_service_->Now(). This allows unit tests to mock the current time. 36 explicit OriginBoundCertService(
59 class TimeService { 37 OriginBoundCertStore* origin_bound_cert_store);
60 public:
61 virtual ~TimeService() {}
62 38
63 virtual base::Time Now() = 0; 39 ~OriginBoundCertService();
64 };
65 40
66 CertVerifier(); 41 // TODO(rkn): Specify certificate type (RSA or DSA).
67 42 // Fetches the origin bound cert for the specified origin if one exists
68 // Used by unit tests to mock the current time. Takes ownership of 43 // and creates one otherwise. On success, |private_key_result| stores a
69 // |time_service|. 44 // DER-encoded PrivateKeyInfo struct, and |cert_result| stores a DER-encoded
70 explicit CertVerifier(TimeService* time_service); 45 // certificate.
71 46 int GetOriginBoundCert(const std::string& origin,
72 // When the verifier is destroyed, all certificate verifications requests are 47 std::string* private_key,
73 // canceled, and their completion callbacks will not be called. 48 std::string* cert,
74 virtual ~CertVerifier(); 49 CompletionCallback* callback,
75 50 RequestHandle* out_req);
76 // Verifies the given certificate against the given hostname. Returns OK if
77 // successful or an error code upon failure.
78 //
79 // The |*verify_result| structure, including the |verify_result->cert_status|
80 // bitmask, is always filled out regardless of the return value. If the
81 // certificate has multiple errors, the corresponding status flags are set in
82 // |verify_result->cert_status|, and the error code for the most serious
83 // error is returned.
84 //
85 // |flags| is bitwise OR'd of X509Certificate::VerifyFlags.
86 // If VERIFY_REV_CHECKING_ENABLED is set in |flags|, certificate revocation
87 // checking is performed.
88 //
89 // If VERIFY_EV_CERT is set in |flags| too, EV certificate verification is
90 // performed. If |flags| is VERIFY_EV_CERT (that is,
91 // VERIFY_REV_CHECKING_ENABLED is not set), EV certificate verification will
92 // not be performed.
93 //
94 // |callback| must not be null. ERR_IO_PENDING is returned if the operation
95 // could not be completed synchronously, in which case the result code will
96 // be passed to the callback when available.
97 //
98 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to
99 // the async request. This handle is not valid after the request has
100 // completed.
101 int Verify(X509Certificate* cert,
102 const std::string& hostname,
103 int flags,
104 CertVerifyResult* verify_result,
105 CompletionCallback* callback,
106 RequestHandle* out_req);
107 51
108 // Cancels the specified request. |req| is the handle returned by Verify(). 52 // Cancels the specified request. |req| is the handle returned by Verify().
109 // After a request is canceled, its completion callback will not be called. 53 // After a request is canceled, its completion callback will not be called.
110 void CancelRequest(RequestHandle req); 54 void CancelRequest(RequestHandle req);
111 55
112 // Clears the verification result cache. 56 // Public only for unit testing.
113 void ClearCache(); 57 int GetCertCount();
114
115 size_t GetCacheSize() const;
116
117 uint64 requests() const { return requests_; } 58 uint64 requests() const { return requests_; }
118 uint64 cache_hits() const { return cache_hits_; } 59 uint64 cache_hits() const {return cache_hits_; }
119 uint64 inflight_joins() const { return inflight_joins_; } 60 uint64 inflight_joins() const {return inflight_joins_; }
120 61
121 private: 62 private:
122 friend class CertVerifierWorker; // Calls HandleResult. 63 friend class OriginBoundCertServiceWorker; // Calls HandleResult.
123 64
124 // Input parameters of a certificate verification request. 65 // On success, |private_key_result| stores a DER-encoded PrivateKeyInfo
125 struct RequestParams { 66 // struct, and |cert_result| stores a DER-encoded certificate. Returns
126 bool operator==(const RequestParams& other) const { 67 // OK if successful and ERR_FAILED otherwise.
127 // |flags| is compared before |cert_fingerprint| and |hostname| under 68 // |serial_number| is passed in because it is created with the function
128 // assumption that integer comparisons are faster than memory and string 69 // base::RandInt, which opens the file /dev/urandom, which cannot be done on
129 // comparisons. 70 // a worker thread.
130 return (flags == other.flags && 71 int GenerateCert(const std::string& origin,
131 memcmp(cert_fingerprint.data, other.cert_fingerprint.data, 72 uint32 serial_number,
132 sizeof(cert_fingerprint.data)) == 0 && 73 std::string* private_key,
133 hostname == other.hostname); 74 std::string* cert);
134 }
135 75
136 bool operator<(const RequestParams& other) const { 76 void HandleResult(const std::string& origin,
137 // |flags| is compared before |cert_fingerprint| and |hostname| under 77 int error,
138 // assumption that integer comparisons are faster than memory and string 78 const std::string& private_key,
139 // comparisons. 79 const std::string& cert);
140 if (flags != other.flags)
141 return flags < other.flags;
142 int rv = memcmp(cert_fingerprint.data, other.cert_fingerprint.data,
143 sizeof(cert_fingerprint.data));
144 if (rv != 0)
145 return rv < 0;
146 return hostname < other.hostname;
147 }
148 80
149 SHA1Fingerprint cert_fingerprint; 81 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_;
150 std::string hostname;
151 int flags;
152 };
153 82
154 void HandleResult(X509Certificate* cert, 83 // inflight_ maps from an origin to an active generation which is taking
155 const std::string& hostname,
156 int flags,
157 int error,
158 const CertVerifyResult& verify_result);
159
160 // CertDatabase::Observer methods:
161 virtual void OnCertTrustChanged(const X509Certificate* cert);
162
163 // cache_ maps from a request to a cached result. The cached result may
164 // have expired and the size of |cache_| must be <= kMaxCacheEntries.
165 std::map<RequestParams, CachedCertVerifyResult> cache_;
166
167 // inflight_ maps from a request to an active verification which is taking
168 // place. 84 // place.
169 std::map<RequestParams, CertVerifierJob*> inflight_; 85 std::map<std::string, OriginBoundCertServiceJob*> inflight_;
170
171 scoped_ptr<TimeService> time_service_;
172 86
173 uint64 requests_; 87 uint64 requests_;
174 uint64 cache_hits_; 88 uint64 cache_hits_;
175 uint64 inflight_joins_; 89 uint64 inflight_joins_;
176 90
177 DISALLOW_COPY_AND_ASSIGN(CertVerifier); 91 DISALLOW_COPY_AND_ASSIGN(OriginBoundCertService);
178 };
179
180 // This class represents the task of verifying a certificate. It wraps
181 // CertVerifier to verify only a single certificate at a time and cancels this
182 // request when going out of scope.
183 class SingleRequestCertVerifier {
184 public:
185 // |cert_verifier| must remain valid for the lifetime of |this|.
186 explicit SingleRequestCertVerifier(CertVerifier* cert_verifier);
187
188 // If a completion callback is pending when the verifier is destroyed, the
189 // certificate verification is canceled, and the completion callback will
190 // not be called.
191 ~SingleRequestCertVerifier();
192
193 // Verifies the given certificate, filling out the |verify_result| object
194 // upon success. See CertVerifier::Verify() for details.
195 int Verify(X509Certificate* cert,
196 const std::string& hostname,
197 int flags,
198 CertVerifyResult* verify_result,
199 CompletionCallback* callback);
200
201 private:
202 // Callback for when the request to |cert_verifier_| completes, so we
203 // dispatch to the user's callback.
204 void OnVerifyCompletion(int result);
205
206 // The actual certificate verifier that will handle the request.
207 CertVerifier* const cert_verifier_;
208
209 // The current request (if any).
210 CertVerifier::RequestHandle cur_request_;
211 CompletionCallback* cur_request_callback_;
212
213 // Completion callback for when request to |cert_verifier_| completes.
214 CompletionCallbackImpl<SingleRequestCertVerifier> callback_;
215
216 DISALLOW_COPY_AND_ASSIGN(SingleRequestCertVerifier);
217 }; 92 };
218 93
219 } // namespace net 94 } // namespace net
220 95
221 #endif // NET_BASE_CERT_VERIFIER_H_ 96 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698