Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
|
agl
2011/08/08 22:36:27
Should this file be included in the CL?
| |
| 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/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" | 14 #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" | 15 #include "net/base/completion_callback.h" |
| 19 #include "net/base/net_api.h" | 16 #include "net/base/net_api.h" |
| 20 #include "net/base/x509_cert_types.h" | |
| 21 | 17 |
| 22 namespace net { | 18 namespace net { |
| 23 | 19 |
| 24 class CertVerifierJob; | 20 class OriginBoundCertServiceJob; |
| 25 class CertVerifierWorker; | 21 class OriginBoundCertServiceWorker; |
| 26 class X509Certificate; | 22 class OriginBoundCertStore; |
| 27 | 23 |
| 28 // CachedCertVerifyResult contains the result of a certificate verification. | 24 // A class for creating and fetching origin bound certs. |
| 29 struct CachedCertVerifyResult { | 25 // Inherits from NonThreadSafe in order to use the function |
| 30 CachedCertVerifyResult(); | 26 // |CalledOnValidThread|. |
| 31 ~CachedCertVerifyResult(); | 27 class NET_API OriginBoundCertService |
| 32 | 28 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 33 // Returns true if |current_time| is greater than or equal to |expiry|. | |
| 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: | 29 public: |
| 54 // Opaque type used to cancel a request. | 30 // Opaque type used to cancel a request. |
| 55 typedef void* RequestHandle; | 31 typedef void* RequestHandle; |
| 56 | 32 |
| 57 // CertVerifier must not call base::Time::Now() directly. It must call | 33 // This object owns origin_bound_cert_store. |
| 58 // time_service_->Now(). This allows unit tests to mock the current time. | 34 explicit OriginBoundCertService( |
| 59 class TimeService { | 35 OriginBoundCertStore* origin_bound_cert_store); |
| 60 public: | |
| 61 virtual ~TimeService() {} | |
| 62 | 36 |
| 63 virtual base::Time Now() = 0; | 37 ~OriginBoundCertService(); |
| 64 }; | |
| 65 | 38 |
| 66 CertVerifier(); | 39 // TODO(rkn): Specify certificate type (RSA or DSA). |
| 67 | |
| 68 // Used by unit tests to mock the current time. Takes ownership of | |
| 69 // |time_service|. | |
| 70 explicit CertVerifier(TimeService* time_service); | |
| 71 | |
| 72 // When the verifier is destroyed, all certificate verifications requests are | |
| 73 // canceled, and their completion callbacks will not be called. | |
| 74 virtual ~CertVerifier(); | |
| 75 | |
| 76 // Verifies the given certificate against the given hostname. Returns OK if | |
| 77 // successful or an error code upon failure. | |
| 78 // | 40 // |
| 79 // The |*verify_result| structure, including the |verify_result->cert_status| | 41 // Fetches the origin bound cert for the specified origin if one exists |
| 80 // bitmask, is always filled out regardless of the return value. If the | 42 // and creates one otherwise. Returns OK if successful or an error code upon |
| 81 // certificate has multiple errors, the corresponding status flags are set in | 43 // failure. |
| 82 // |verify_result->cert_status|, and the error code for the most serious | |
| 83 // error is returned. | |
| 84 // | 44 // |
| 85 // |flags| is bitwise OR'd of X509Certificate::VerifyFlags. | 45 // On success, |private_key| stores a DER-encoded PrivateKeyInfo |
| 86 // If VERIFY_REV_CHECKING_ENABLED is set in |flags|, certificate revocation | 46 // struct, and |cert| stores a DER-encoded certificate. |
| 87 // checking is performed. | |
| 88 // | 47 // |
| 89 // If VERIFY_EV_CERT is set in |flags| too, EV certificate verification is | 48 // |callback| must not be null. ERR_IO_PENDING is returned if the operation |
| 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 | 49 // could not be completed synchronously, in which case the result code will |
| 96 // be passed to the callback when available. | 50 // be passed to the callback when available. |
| 97 // | 51 // |
| 98 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to | 52 // If |out_req| is non_NULL, then |*out_req| will be filled with a handle to |
|
wtc
2011/08/09 00:43:34
"non-NULL" is correct, right? We should not use a
| |
| 99 // the async request. This handle is not valid after the request has | 53 // the async request. This handle is not valid after the request has |
| 100 // completed. | 54 // completed. |
| 101 int Verify(X509Certificate* cert, | 55 int GetOriginBoundCert(const std::string& origin, |
| 102 const std::string& hostname, | 56 std::string* private_key, |
| 103 int flags, | 57 std::string* cert, |
| 104 CertVerifyResult* verify_result, | 58 CompletionCallback* callback, |
| 105 CompletionCallback* callback, | 59 RequestHandle* out_req); |
| 106 RequestHandle* out_req); | |
| 107 | 60 |
| 108 // Cancels the specified request. |req| is the handle returned by Verify(). | 61 // Cancels the specified request. |req| is the handle returned by |
| 109 // After a request is canceled, its completion callback will not be called. | 62 // GetOriginBoundCert(). After a request is canceled, its completion |
| 63 // callback will not be called. | |
| 110 void CancelRequest(RequestHandle req); | 64 void CancelRequest(RequestHandle req); |
| 111 | 65 |
| 112 // Clears the verification result cache. | 66 // Public only for unit testing. |
| 113 void ClearCache(); | 67 int get_cert_count(); |
|
wtc
2011/08/09 00:43:34
Remove "get_". The getters don't need the "get_"
| |
| 114 | |
| 115 size_t GetCacheSize() const; | |
| 116 | |
| 117 uint64 requests() const { return requests_; } | 68 uint64 requests() const { return requests_; } |
| 118 uint64 cache_hits() const { return cache_hits_; } | 69 uint64 cache_hits() const {return cache_hits_; } |
| 119 uint64 inflight_joins() const { return inflight_joins_; } | 70 uint64 inflight_joins() const {return inflight_joins_; } |
|
wtc
2011/08/09 00:43:34
Add a space after the opening curly brace '{' on l
| |
| 120 | 71 |
| 121 private: | 72 private: |
| 122 friend class CertVerifierWorker; // Calls HandleResult. | 73 friend class OriginBoundCertServiceWorker; // Calls HandleResult. |
| 123 | 74 |
| 124 // Input parameters of a certificate verification request. | 75 // On success, |private_key| stores a DER-encoded PrivateKeyInfo |
| 125 struct RequestParams { | 76 // struct, and |cert| stores a DER-encoded certificate. Returns |
| 126 bool operator==(const RequestParams& other) const { | 77 // OK if successful and an error code otherwise. |
| 127 // |flags| is compared before |cert_fingerprint| and |hostname| under | 78 // |serial_number| is passed in because it is created with the function |
| 128 // assumption that integer comparisons are faster than memory and string | 79 // base::RandInt, which opens the file /dev/urandom. This is done with a |
|
wtc
2011/08/09 00:43:34
This is done => /dev/urandom is opened
| |
| 129 // comparisons. | 80 // LazyInstance, which is not allowed on a worker thread. |
| 130 return (flags == other.flags && | 81 static int GenerateCert(const std::string& origin, |
| 131 memcmp(cert_fingerprint.data, other.cert_fingerprint.data, | 82 uint32 serial_number, |
| 132 sizeof(cert_fingerprint.data)) == 0 && | 83 std::string* private_key, |
| 133 hostname == other.hostname); | 84 std::string* cert); |
| 134 } | |
| 135 | 85 |
| 136 bool operator<(const RequestParams& other) const { | 86 void HandleResult(const std::string& origin, |
| 137 // |flags| is compared before |cert_fingerprint| and |hostname| under | 87 int error, |
| 138 // assumption that integer comparisons are faster than memory and string | 88 const std::string& private_key, |
| 139 // comparisons. | 89 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 | 90 |
| 149 SHA1Fingerprint cert_fingerprint; | 91 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_; |
| 150 std::string hostname; | |
| 151 int flags; | |
| 152 }; | |
| 153 | 92 |
| 154 void HandleResult(X509Certificate* cert, | 93 // 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. | 94 // place. |
| 169 std::map<RequestParams, CertVerifierJob*> inflight_; | 95 std::map<std::string, OriginBoundCertServiceJob*> inflight_; |
| 170 | |
| 171 scoped_ptr<TimeService> time_service_; | |
| 172 | 96 |
| 173 uint64 requests_; | 97 uint64 requests_; |
| 174 uint64 cache_hits_; | 98 uint64 cache_hits_; |
|
wtc
2011/08/09 00:43:34
"cache hits" is not right because we aren't using
| |
| 175 uint64 inflight_joins_; | 99 uint64 inflight_joins_; |
| 176 | 100 |
| 177 DISALLOW_COPY_AND_ASSIGN(CertVerifier); | 101 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 }; | 102 }; |
| 218 | 103 |
| 219 } // namespace net | 104 } // namespace net |
| 220 | 105 |
| 221 #endif // NET_BASE_CERT_VERIFIER_H_ | 106 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ |
| OLD | NEW |