| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MULTI_THREADED_CERT_VERIFIER_H_ |
| 6 #define NET_BASE_CERT_VERIFIER_H_ | 6 #define NET_BASE_MULTI_THREADED_CERT_VERIFIER_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/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 16 #include "net/base/cert_database.h" | 16 #include "net/base/cert_database.h" |
| 17 #include "net/base/cert_verifier.h" |
| 17 #include "net/base/cert_verify_result.h" | 18 #include "net/base/cert_verify_result.h" |
| 18 #include "net/base/completion_callback.h" | 19 #include "net/base/completion_callback.h" |
| 19 #include "net/base/expiring_cache.h" | 20 #include "net/base/expiring_cache.h" |
| 20 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 21 #include "net/base/x509_cert_types.h" | 22 #include "net/base/x509_cert_types.h" |
| 22 | 23 |
| 23 namespace net { | 24 namespace net { |
| 24 | 25 |
| 25 class BoundNetLog; | |
| 26 class CertVerifierJob; | 26 class CertVerifierJob; |
| 27 class CertVerifierRequest; |
| 27 class CertVerifierWorker; | 28 class CertVerifierWorker; |
| 28 class CRLSet; | |
| 29 class X509Certificate; | |
| 30 | 29 |
| 31 // CertVerifier represents a service for verifying certificates. | 30 // MultiThreadedCertVerifier is a CertVerifier implementation that runs |
| 32 // | 31 // synchronous CertVerifier implementations on worker threads. |
| 33 // CertVerifier can handle multiple requests at a time, so when canceling a | 32 class NET_EXPORT MultiThreadedCertVerifier : |
| 34 // request the RequestHandle that was returned by Verify() needs to be | 33 public CertVerifier, |
| 35 // given. A simpler alternative for consumers that only have 1 outstanding | 34 NON_EXPORTED_BASE(public base::NonThreadSafe), |
| 36 // request at a time is to create a SingleRequestCertVerifier wrapper around | 35 public CertDatabase::Observer { |
| 37 // CertVerifier (which will automatically cancel the single request when it | |
| 38 // goes out of scope). | |
| 39 class NET_EXPORT CertVerifier : NON_EXPORTED_BASE(public base::NonThreadSafe), | |
| 40 public CertDatabase::Observer { | |
| 41 public: | 36 public: |
| 42 // Opaque type used to cancel a request. | 37 MultiThreadedCertVerifier(); |
| 43 typedef void* RequestHandle; | |
| 44 | |
| 45 CertVerifier(); | |
| 46 | 38 |
| 47 // When the verifier is destroyed, all certificate verifications requests are | 39 // When the verifier is destroyed, all certificate verifications requests are |
| 48 // canceled, and their completion callbacks will not be called. | 40 // canceled, and their completion callbacks will not be called. |
| 49 virtual ~CertVerifier(); | 41 virtual ~MultiThreadedCertVerifier(); |
| 50 | 42 |
| 51 // Verifies the given certificate against the given hostname. Returns OK if | 43 // CertVerifier implementation |
| 52 // successful or an error code upon failure. | 44 virtual int Verify(X509Certificate* cert, |
| 53 // | 45 const std::string& hostname, |
| 54 // The |*verify_result| structure, including the |verify_result->cert_status| | 46 int flags, |
| 55 // bitmask, is always filled out regardless of the return value. If the | 47 CRLSet* crl_set, |
| 56 // certificate has multiple errors, the corresponding status flags are set in | 48 CertVerifyResult* verify_result, |
| 57 // |verify_result->cert_status|, and the error code for the most serious | 49 const CompletionCallback& callback, |
| 58 // error is returned. | 50 CertVerifier::RequestHandle* out_req, |
| 59 // | 51 const BoundNetLog& net_log) OVERRIDE; |
| 60 // |flags| is bitwise OR'd of X509Certificate::VerifyFlags. | |
| 61 // If VERIFY_REV_CHECKING_ENABLED is set in |flags|, certificate revocation | |
| 62 // checking is performed. | |
| 63 // | |
| 64 // If VERIFY_EV_CERT is set in |flags| too, EV certificate verification is | |
| 65 // performed. If |flags| is VERIFY_EV_CERT (that is, | |
| 66 // VERIFY_REV_CHECKING_ENABLED is not set), EV certificate verification will | |
| 67 // not be performed. | |
| 68 // | |
| 69 // |crl_set| points to an optional CRLSet structure which can be used to | |
| 70 // avoid revocation checks over the network. | |
| 71 // | |
| 72 // |callback| must not be null. ERR_IO_PENDING is returned if the operation | |
| 73 // could not be completed synchronously, in which case the result code will | |
| 74 // be passed to the callback when available. | |
| 75 // | |
| 76 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to | |
| 77 // the async request. This handle is not valid after the request has | |
| 78 // completed. | |
| 79 int Verify(X509Certificate* cert, | |
| 80 const std::string& hostname, | |
| 81 int flags, | |
| 82 CRLSet* crl_set, | |
| 83 CertVerifyResult* verify_result, | |
| 84 const CompletionCallback& callback, | |
| 85 RequestHandle* out_req, | |
| 86 const BoundNetLog& net_log); | |
| 87 | 52 |
| 88 // Cancels the specified request. |req| is the handle returned by Verify(). | 53 virtual void CancelRequest(CertVerifier::RequestHandle req) OVERRIDE; |
| 89 // After a request is canceled, its completion callback will not be called. | |
| 90 void CancelRequest(RequestHandle req); | |
| 91 | 54 |
| 92 private: | 55 private: |
| 93 friend class CertVerifierWorker; // Calls HandleResult. | 56 friend class CertVerifierWorker; // Calls HandleResult. |
| 94 friend class CertVerifierRequest; | 57 friend class CertVerifierRequest; |
| 95 friend class CertVerifierJob; | 58 friend class CertVerifierJob; |
| 96 FRIEND_TEST_ALL_PREFIXES(CertVerifierTest, CacheHit); | 59 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCertVerifierTest, CacheHit); |
| 97 FRIEND_TEST_ALL_PREFIXES(CertVerifierTest, DifferentCACerts); | 60 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCertVerifierTest, DifferentCACerts); |
| 98 FRIEND_TEST_ALL_PREFIXES(CertVerifierTest, InflightJoin); | 61 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCertVerifierTest, InflightJoin); |
| 99 FRIEND_TEST_ALL_PREFIXES(CertVerifierTest, CancelRequest); | 62 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCertVerifierTest, CancelRequest); |
| 100 FRIEND_TEST_ALL_PREFIXES(CertVerifierTest, RequestParamsComparators); | 63 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCertVerifierTest, |
| 64 RequestParamsComparators); |
| 101 | 65 |
| 102 // Input parameters of a certificate verification request. | 66 // Input parameters of a certificate verification request. |
| 103 struct RequestParams { | 67 struct RequestParams { |
| 104 RequestParams(const SHA1Fingerprint& cert_fingerprint_arg, | 68 RequestParams(const SHA1Fingerprint& cert_fingerprint_arg, |
| 105 const SHA1Fingerprint& ca_fingerprint_arg, | 69 const SHA1Fingerprint& ca_fingerprint_arg, |
| 106 const std::string& hostname_arg, | 70 const std::string& hostname_arg, |
| 107 int flags_arg) | 71 int flags_arg) |
| 108 : cert_fingerprint(cert_fingerprint_arg), | 72 : cert_fingerprint(cert_fingerprint_arg), |
| 109 ca_fingerprint(ca_fingerprint_arg), | 73 ca_fingerprint(ca_fingerprint_arg), |
| 110 hostname(hostname_arg), | 74 hostname(hostname_arg), |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 CertVerifierCache cache_; | 127 CertVerifierCache cache_; |
| 164 | 128 |
| 165 // inflight_ maps from a request to an active verification which is taking | 129 // inflight_ maps from a request to an active verification which is taking |
| 166 // place. | 130 // place. |
| 167 std::map<RequestParams, CertVerifierJob*> inflight_; | 131 std::map<RequestParams, CertVerifierJob*> inflight_; |
| 168 | 132 |
| 169 uint64 requests_; | 133 uint64 requests_; |
| 170 uint64 cache_hits_; | 134 uint64 cache_hits_; |
| 171 uint64 inflight_joins_; | 135 uint64 inflight_joins_; |
| 172 | 136 |
| 173 DISALLOW_COPY_AND_ASSIGN(CertVerifier); | 137 DISALLOW_COPY_AND_ASSIGN(MultiThreadedCertVerifier); |
| 174 }; | |
| 175 | |
| 176 // This class represents the task of verifying a certificate. It wraps | |
| 177 // CertVerifier to verify only a single certificate at a time and cancels this | |
| 178 // request when going out of scope. | |
| 179 class SingleRequestCertVerifier { | |
| 180 public: | |
| 181 // |cert_verifier| must remain valid for the lifetime of |this|. | |
| 182 explicit SingleRequestCertVerifier(CertVerifier* cert_verifier); | |
| 183 | |
| 184 // If a completion callback is pending when the verifier is destroyed, the | |
| 185 // certificate verification is canceled, and the completion callback will | |
| 186 // not be called. | |
| 187 ~SingleRequestCertVerifier(); | |
| 188 | |
| 189 // Verifies the given certificate, filling out the |verify_result| object | |
| 190 // upon success. See CertVerifier::Verify() for details. | |
| 191 int Verify(X509Certificate* cert, | |
| 192 const std::string& hostname, | |
| 193 int flags, | |
| 194 CRLSet* crl_set, | |
| 195 CertVerifyResult* verify_result, | |
| 196 const CompletionCallback& callback, | |
| 197 const BoundNetLog& net_log); | |
| 198 | |
| 199 private: | |
| 200 // Callback for when the request to |cert_verifier_| completes, so we | |
| 201 // dispatch to the user's callback. | |
| 202 void OnVerifyCompletion(int result); | |
| 203 | |
| 204 // The actual certificate verifier that will handle the request. | |
| 205 CertVerifier* const cert_verifier_; | |
| 206 | |
| 207 // The current request (if any). | |
| 208 CertVerifier::RequestHandle cur_request_; | |
| 209 CompletionCallback cur_request_callback_; | |
| 210 | |
| 211 DISALLOW_COPY_AND_ASSIGN(SingleRequestCertVerifier); | |
| 212 }; | 138 }; |
| 213 | 139 |
| 214 } // namespace net | 140 } // namespace net |
| 215 | 141 |
| 216 #endif // NET_BASE_CERT_VERIFIER_H_ | 142 #endif // NET_BASE_MULTI_THREADED_CERT_VERIFIER_H_ |
| OLD | NEW |