[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/quic/crypto/proof_verifier_chromium.h" |
| 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
| 11 | #include "base/callback_helpers.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 12 | #include "base/logging.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 14 | #include "base/stl_util.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
| 16 | #include "crypto/signature_verifier.h" |
estark | 1a66df7 | 2015-07-28 15:24:00 | [diff] [blame] | 17 | #include "net/base/host_port_pair.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 18 | #include "net/base/net_errors.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 19 | #include "net/cert/asn1_util.h" |
| 20 | #include "net/cert/cert_status_flags.h" |
| 21 | #include "net/cert/cert_verifier.h" |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 22 | #include "net/cert/ct_policy_enforcer.h" |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 23 | #include "net/cert/ct_policy_status.h" |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 24 | #include "net/cert/ct_verifier.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 25 | #include "net/cert/x509_util.h" |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 26 | #include "net/http/transport_security_state.h" |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 27 | #include "net/quic/crypto/crypto_protocol.h" |
| 28 | #include "net/ssl/ssl_config_service.h" |
| 29 | |
| 30 | using base::StringPiece; |
| 31 | using base::StringPrintf; |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
| 35 | namespace net { |
| 36 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 37 | ProofVerifyDetailsChromium::ProofVerifyDetailsChromium() |
| 38 | : pkp_bypassed(false) {} |
| 39 | |
| 40 | ProofVerifyDetailsChromium::~ProofVerifyDetailsChromium() {} |
| 41 | |
| 42 | ProofVerifyDetailsChromium::ProofVerifyDetailsChromium( |
| 43 | const ProofVerifyDetailsChromium&) = default; |
| 44 | |
[email protected] | 92bc621d | 2014-07-29 21:42:13 | [diff] [blame] | 45 | ProofVerifyDetails* ProofVerifyDetailsChromium::Clone() const { |
| 46 | ProofVerifyDetailsChromium* other = new ProofVerifyDetailsChromium; |
| 47 | other->cert_verify_result = cert_verify_result; |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 48 | other->ct_verify_result = ct_verify_result; |
[email protected] | 92bc621d | 2014-07-29 21:42:13 | [diff] [blame] | 49 | return other; |
| 50 | } |
| 51 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 52 | // A Job handles the verification of a single proof. It is owned by the |
| 53 | // ProofVerifier. If the verification can not complete synchronously, it |
| 54 | // will notify the ProofVerifier upon completion. |
| 55 | class ProofVerifierChromium::Job { |
| 56 | public: |
| 57 | Job(ProofVerifierChromium* proof_verifier, |
| 58 | CertVerifier* cert_verifier, |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 59 | CTPolicyEnforcer* ct_policy_enforcer, |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 60 | TransportSecurityState* transport_security_state, |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 61 | CTVerifier* cert_transparency_verifier, |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 62 | int cert_verify_flags, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 63 | const BoundNetLog& net_log); |
rtenneti | dd4f1bd7 | 2015-12-23 19:17:50 | [diff] [blame] | 64 | ~Job(); |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 65 | |
[email protected] | 05bfc260f | 2014-06-07 06:31:25 | [diff] [blame] | 66 | // Starts the proof verification. If |QUIC_PENDING| is returned, then |
| 67 | // |callback| will be invoked asynchronously when the verification completes. |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 68 | QuicAsyncStatus VerifyProof( |
| 69 | const std::string& hostname, |
| 70 | const uint16_t port, |
| 71 | const std::string& server_config, |
| 72 | QuicVersion quic_version, |
| 73 | base::StringPiece chlo_hash, |
| 74 | const std::vector<std::string>& certs, |
| 75 | const std::string& cert_sct, |
| 76 | const std::string& signature, |
| 77 | std::string* error_details, |
| 78 | std::unique_ptr<ProofVerifyDetails>* verify_details, |
| 79 | ProofVerifierCallback* callback); |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | enum State { |
| 83 | STATE_NONE, |
| 84 | STATE_VERIFY_CERT, |
| 85 | STATE_VERIFY_CERT_COMPLETE, |
| 86 | }; |
| 87 | |
| 88 | int DoLoop(int last_io_result); |
| 89 | void OnIOComplete(int result); |
| 90 | int DoVerifyCert(int result); |
| 91 | int DoVerifyCertComplete(int result); |
| 92 | |
| 93 | bool VerifySignature(const std::string& signed_data, |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 94 | QuicVersion quic_version, |
| 95 | StringPiece chlo_hash, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 96 | const std::string& signature, |
| 97 | const std::string& cert); |
| 98 | |
| 99 | // Proof verifier to notify when this jobs completes. |
| 100 | ProofVerifierChromium* proof_verifier_; |
| 101 | |
| 102 | // The underlying verifier used for verifying certificates. |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 103 | CertVerifier* verifier_; |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 104 | std::unique_ptr<CertVerifier::Request> cert_verifier_request_; |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 105 | |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 106 | CTPolicyEnforcer* policy_enforcer_; |
rsleevi | 9541f863 | 2015-07-31 00:07:00 | [diff] [blame] | 107 | |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 108 | TransportSecurityState* transport_security_state_; |
| 109 | |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 110 | CTVerifier* cert_transparency_verifier_; |
| 111 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 112 | // |hostname| specifies the hostname for which |certs| is a valid chain. |
| 113 | std::string hostname_; |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 114 | // |port| specifies the target port for the connection. |
| 115 | uint16_t port_; |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 116 | |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 117 | std::unique_ptr<ProofVerifierCallback> callback_; |
| 118 | std::unique_ptr<ProofVerifyDetailsChromium> verify_details_; |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 119 | std::string error_details_; |
| 120 | |
| 121 | // X509Certificate from a chain of DER encoded certificates. |
| 122 | scoped_refptr<X509Certificate> cert_; |
| 123 | |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 124 | // |cert_verify_flags| is bitwise OR'd of CertVerifier::VerifyFlags and it is |
| 125 | // passed to CertVerifier::Verify. |
| 126 | int cert_verify_flags_; |
| 127 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 128 | State next_state_; |
| 129 | |
rtenneti | dd4f1bd7 | 2015-12-23 19:17:50 | [diff] [blame] | 130 | base::TimeTicks start_time_; |
| 131 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 132 | BoundNetLog net_log_; |
| 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(Job); |
| 135 | }; |
| 136 | |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 137 | ProofVerifierChromium::Job::Job( |
| 138 | ProofVerifierChromium* proof_verifier, |
| 139 | CertVerifier* cert_verifier, |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 140 | CTPolicyEnforcer* ct_policy_enforcer, |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 141 | TransportSecurityState* transport_security_state, |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 142 | CTVerifier* cert_transparency_verifier, |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 143 | int cert_verify_flags, |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 144 | const BoundNetLog& net_log) |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 145 | : proof_verifier_(proof_verifier), |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 146 | verifier_(cert_verifier), |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 147 | policy_enforcer_(ct_policy_enforcer), |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 148 | transport_security_state_(transport_security_state), |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 149 | cert_transparency_verifier_(cert_transparency_verifier), |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 150 | cert_verify_flags_(cert_verify_flags), |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 151 | next_state_(STATE_NONE), |
rtenneti | dd4f1bd7 | 2015-12-23 19:17:50 | [diff] [blame] | 152 | start_time_(base::TimeTicks::Now()), |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 153 | net_log_(net_log) { |
| 154 | DCHECK(proof_verifier_); |
| 155 | DCHECK(verifier_); |
| 156 | DCHECK(policy_enforcer_); |
| 157 | DCHECK(transport_security_state_); |
| 158 | DCHECK(cert_transparency_verifier_); |
| 159 | } |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 160 | |
rtenneti | dd4f1bd7 | 2015-12-23 19:17:50 | [diff] [blame] | 161 | ProofVerifierChromium::Job::~Job() { |
| 162 | base::TimeTicks end_time = base::TimeTicks::Now(); |
| 163 | UMA_HISTOGRAM_TIMES("Net.QuicSession.VerifyProofTime", |
| 164 | end_time - start_time_); |
| 165 | // |hostname_| will always be canonicalized to lowercase. |
| 166 | if (hostname_.compare("www.google.com") == 0) { |
| 167 | UMA_HISTOGRAM_TIMES("Net.QuicSession.VerifyProofTime.google", |
| 168 | end_time - start_time_); |
| 169 | } |
| 170 | } |
| 171 | |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 172 | QuicAsyncStatus ProofVerifierChromium::Job::VerifyProof( |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 173 | const string& hostname, |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 174 | const uint16_t port, |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 175 | const string& server_config, |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 176 | QuicVersion quic_version, |
| 177 | StringPiece chlo_hash, |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 178 | const vector<string>& certs, |
rjshade | c86dbfa | 2015-11-12 20:16:25 | [diff] [blame] | 179 | const std::string& cert_sct, |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 180 | const string& signature, |
| 181 | std::string* error_details, |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 182 | std::unique_ptr<ProofVerifyDetails>* verify_details, |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 183 | ProofVerifierCallback* callback) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 184 | DCHECK(error_details); |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 185 | DCHECK(verify_details); |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 186 | DCHECK(callback); |
| 187 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 188 | error_details->clear(); |
| 189 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 190 | if (STATE_NONE != next_state_) { |
| 191 | *error_details = "Certificate is already set and VerifyProof has begun"; |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 192 | DLOG(DFATAL) << *error_details; |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 193 | return QUIC_FAILURE; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 196 | verify_details_.reset(new ProofVerifyDetailsChromium); |
| 197 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 198 | if (certs.empty()) { |
| 199 | *error_details = "Failed to create certificate chain. Certs are empty."; |
| 200 | DLOG(WARNING) << *error_details; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 201 | verify_details_->cert_verify_result.cert_status = CERT_STATUS_INVALID; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 202 | *verify_details = std::move(verify_details_); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 203 | return QUIC_FAILURE; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Convert certs to X509Certificate. |
| 207 | vector<StringPiece> cert_pieces(certs.size()); |
| 208 | for (unsigned i = 0; i < certs.size(); i++) { |
| 209 | cert_pieces[i] = base::StringPiece(certs[i]); |
| 210 | } |
| 211 | cert_ = X509Certificate::CreateFromDERCertChain(cert_pieces); |
| 212 | if (!cert_.get()) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 213 | *error_details = "Failed to create certificate chain"; |
| 214 | DLOG(WARNING) << *error_details; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 215 | verify_details_->cert_verify_result.cert_status = CERT_STATUS_INVALID; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 216 | *verify_details = std::move(verify_details_); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 217 | return QUIC_FAILURE; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 218 | } |
| 219 | |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 220 | if (!cert_sct.empty()) { |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 221 | // Note that this is a completely synchronous operation: The CT Log Verifier |
| 222 | // gets all the data it needs for SCT verification and does not do any |
| 223 | // external communication. |
rtenneti | a5b15047 | 2016-05-20 03:13:56 | [diff] [blame] | 224 | cert_transparency_verifier_->Verify(cert_.get(), std::string(), cert_sct, |
| 225 | &verify_details_->ct_verify_result, |
| 226 | net_log_); |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 229 | // We call VerifySignature first to avoid copying of server_config and |
| 230 | // signature. |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 231 | if (!VerifySignature(server_config, quic_version, chlo_hash, signature, |
| 232 | certs[0])) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 233 | *error_details = "Failed to verify signature of server config"; |
| 234 | DLOG(WARNING) << *error_details; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 235 | verify_details_->cert_verify_result.cert_status = CERT_STATUS_INVALID; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 236 | *verify_details = std::move(verify_details_); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 237 | return QUIC_FAILURE; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | hostname_ = hostname; |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 241 | port_ = port; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 242 | |
| 243 | next_state_ = STATE_VERIFY_CERT; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 244 | switch (DoLoop(OK)) { |
| 245 | case OK: |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 246 | *verify_details = std::move(verify_details_); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 247 | return QUIC_SUCCESS; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 248 | case ERR_IO_PENDING: |
[email protected] | 3ce3bf2 | 2014-04-03 08:02:01 | [diff] [blame] | 249 | callback_.reset(callback); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 250 | return QUIC_PENDING; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 251 | default: |
| 252 | *error_details = error_details_; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 253 | *verify_details = std::move(verify_details_); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 254 | return QUIC_FAILURE; |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 255 | } |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 258 | int ProofVerifierChromium::Job::DoLoop(int last_result) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 259 | int rv = last_result; |
| 260 | do { |
| 261 | State state = next_state_; |
| 262 | next_state_ = STATE_NONE; |
| 263 | switch (state) { |
| 264 | case STATE_VERIFY_CERT: |
| 265 | DCHECK(rv == OK); |
| 266 | rv = DoVerifyCert(rv); |
| 267 | break; |
| 268 | case STATE_VERIFY_CERT_COMPLETE: |
| 269 | rv = DoVerifyCertComplete(rv); |
| 270 | break; |
| 271 | case STATE_NONE: |
| 272 | default: |
| 273 | rv = ERR_UNEXPECTED; |
| 274 | LOG(DFATAL) << "unexpected state " << state; |
| 275 | break; |
| 276 | } |
| 277 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 278 | return rv; |
| 279 | } |
| 280 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 281 | void ProofVerifierChromium::Job::OnIOComplete(int result) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 282 | int rv = DoLoop(result); |
| 283 | if (rv != ERR_IO_PENDING) { |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 284 | std::unique_ptr<ProofVerifierCallback> callback(std::move(callback_)); |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 285 | // Callback expects ProofVerifyDetails not ProofVerifyDetailsChromium. |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 286 | std::unique_ptr<ProofVerifyDetails> verify_details( |
| 287 | std::move(verify_details_)); |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 288 | callback->Run(rv == OK, error_details_, &verify_details); |
| 289 | // Will delete |this|. |
| 290 | proof_verifier_->OnJobComplete(this); |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 294 | int ProofVerifierChromium::Job::DoVerifyCert(int result) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 295 | next_state_ = STATE_VERIFY_CERT_COMPLETE; |
| 296 | |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 297 | return verifier_->Verify( |
rsleevi | 06bd7855 | 2016-06-08 22:34:46 | [diff] [blame] | 298 | CertVerifier::RequestParams(cert_, hostname_, cert_verify_flags_, |
| 299 | std::string(), CertificateList()), |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 300 | SSLConfigService::GetCRLSet().get(), &verify_details_->cert_verify_result, |
| 301 | base::Bind(&ProofVerifierChromium::Job::OnIOComplete, |
| 302 | base::Unretained(this)), |
| 303 | &cert_verifier_request_, net_log_); |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 306 | int ProofVerifierChromium::Job::DoVerifyCertComplete(int result) { |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 307 | cert_verifier_request_.reset(); |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 308 | |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 309 | const CertVerifyResult& cert_verify_result = |
| 310 | verify_details_->cert_verify_result; |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 311 | const CertStatus cert_status = cert_verify_result.cert_status; |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 312 | verify_details_->ct_verify_result.ct_policies_applied = result == OK; |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 313 | verify_details_->ct_verify_result.ev_policy_compliance = |
| 314 | ct::EVPolicyCompliance::EV_POLICY_DOES_NOT_APPLY; |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 315 | if (result == OK) { |
estark | 0fc8d078 | 2016-02-25 20:41:20 | [diff] [blame] | 316 | if ((cert_verify_result.cert_status & CERT_STATUS_IS_EV)) { |
| 317 | ct::EVPolicyCompliance ev_policy_compliance = |
| 318 | policy_enforcer_->DoesConformToCTEVPolicy( |
| 319 | cert_verify_result.verified_cert.get(), |
| 320 | SSLConfigService::GetEVCertsWhitelist().get(), |
| 321 | verify_details_->ct_verify_result.verified_scts, net_log_); |
| 322 | verify_details_->ct_verify_result.ev_policy_compliance = |
| 323 | ev_policy_compliance; |
| 324 | if (ev_policy_compliance != |
| 325 | ct::EVPolicyCompliance::EV_POLICY_DOES_NOT_APPLY && |
| 326 | ev_policy_compliance != |
| 327 | ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_WHITELIST && |
| 328 | ev_policy_compliance != |
| 329 | ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS) { |
| 330 | verify_details_->cert_verify_result.cert_status |= |
| 331 | CERT_STATUS_CT_COMPLIANCE_FAILED; |
| 332 | verify_details_->cert_verify_result.cert_status &= ~CERT_STATUS_IS_EV; |
| 333 | } |
rsleevi | 9541f863 | 2015-07-31 00:07:00 | [diff] [blame] | 334 | } |
estark | 0fc8d078 | 2016-02-25 20:41:20 | [diff] [blame] | 335 | |
| 336 | verify_details_->ct_verify_result.cert_policy_compliance = |
| 337 | policy_enforcer_->DoesConformToCertPolicy( |
| 338 | cert_verify_result.verified_cert.get(), |
| 339 | verify_details_->ct_verify_result.verified_scts, net_log_); |
rsleevi | 9541f863 | 2015-07-31 00:07:00 | [diff] [blame] | 340 | } |
| 341 | |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 342 | if ((result == OK || |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 343 | (IsCertificateError(result) && IsCertStatusMinorError(cert_status))) && |
[email protected] | 8d60aa5 | 2014-08-08 21:22:45 | [diff] [blame] | 344 | !transport_security_state_->CheckPublicKeyPins( |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 345 | HostPortPair(hostname_, port_), |
[email protected] | 8d60aa5 | 2014-08-08 21:22:45 | [diff] [blame] | 346 | cert_verify_result.is_issued_by_known_root, |
estark | 1a66df7 | 2015-07-28 15:24:00 | [diff] [blame] | 347 | cert_verify_result.public_key_hashes, cert_.get(), |
| 348 | cert_verify_result.verified_cert.get(), |
| 349 | TransportSecurityState::ENABLE_PIN_REPORTS, |
[email protected] | 8d60aa5 | 2014-08-08 21:22:45 | [diff] [blame] | 350 | &verify_details_->pinning_failure_log)) { |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 351 | if (cert_verify_result.is_issued_by_known_root) { |
| 352 | verify_details_->cert_verify_result.cert_status |= |
| 353 | CERT_STATUS_PINNED_KEY_MISSING; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 354 | result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 355 | } else { |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 356 | verify_details_->pkp_bypassed = true; |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 357 | } |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 358 | } |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 359 | |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 360 | if (result != OK) { |
[email protected] | 3030374e3 | 2014-08-07 16:12:06 | [diff] [blame] | 361 | std::string error_string = ErrorToString(result); |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 362 | error_details_ = StringPrintf("Failed to verify certificate chain: %s", |
[email protected] | 3030374e3 | 2014-08-07 16:12:06 | [diff] [blame] | 363 | error_string.c_str()); |
[email protected] | 72e6599 | 2013-07-30 17:16:14 | [diff] [blame] | 364 | DLOG(WARNING) << error_details_; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Exit DoLoop and return the result to the caller to VerifyProof. |
| 368 | DCHECK_EQ(STATE_NONE, next_state_); |
| 369 | return result; |
| 370 | } |
| 371 | |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 372 | bool ProofVerifierChromium::Job::VerifySignature(const string& signed_data, |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 373 | QuicVersion quic_version, |
| 374 | StringPiece chlo_hash, |
[email protected] | 3030374e3 | 2014-08-07 16:12:06 | [diff] [blame] | 375 | const string& signature, |
| 376 | const string& cert) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 377 | StringPiece spki; |
| 378 | if (!asn1::ExtractSPKIFromDERCert(cert, &spki)) { |
| 379 | DLOG(WARNING) << "ExtractSPKIFromDERCert failed"; |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | crypto::SignatureVerifier verifier; |
| 384 | |
| 385 | size_t size_bits; |
| 386 | X509Certificate::PublicKeyType type; |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 387 | X509Certificate::GetPublicKeyInfo(cert_->os_cert_handle(), &size_bits, &type); |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 388 | if (type == X509Certificate::kPublicKeyTypeRSA) { |
| 389 | crypto::SignatureVerifier::HashAlgorithm hash_alg = |
| 390 | crypto::SignatureVerifier::SHA256; |
| 391 | crypto::SignatureVerifier::HashAlgorithm mask_hash_alg = hash_alg; |
| 392 | unsigned int hash_len = 32; // 32 is the length of a SHA-256 hash. |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 393 | |
| 394 | bool ok = verifier.VerifyInitRSAPSS( |
[email protected] | d5c9e4ba | 2013-09-14 05:25:58 | [diff] [blame] | 395 | hash_alg, mask_hash_alg, hash_len, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 396 | reinterpret_cast<const uint8_t*>(signature.data()), signature.size(), |
| 397 | reinterpret_cast<const uint8_t*>(spki.data()), spki.size()); |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 398 | if (!ok) { |
| 399 | DLOG(WARNING) << "VerifyInitRSAPSS failed"; |
| 400 | return false; |
| 401 | } |
| 402 | } else if (type == X509Certificate::kPublicKeyTypeECDSA) { |
davidben | 9c97a36 | 2016-03-03 16:18:26 | [diff] [blame] | 403 | if (!verifier.VerifyInit(crypto::SignatureVerifier::ECDSA_SHA256, |
| 404 | reinterpret_cast<const uint8_t*>(signature.data()), |
| 405 | signature.size(), |
| 406 | reinterpret_cast<const uint8_t*>(spki.data()), |
| 407 | spki.size())) { |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 408 | DLOG(WARNING) << "VerifyInit failed"; |
| 409 | return false; |
| 410 | } |
| 411 | } else { |
| 412 | LOG(ERROR) << "Unsupported public key type " << type; |
| 413 | return false; |
| 414 | } |
| 415 | |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 416 | if (quic_version <= QUIC_VERSION_30) { |
| 417 | verifier.VerifyUpdate( |
| 418 | reinterpret_cast<const uint8_t*>(kProofSignatureLabelOld), |
| 419 | sizeof(kProofSignatureLabelOld)); |
| 420 | } else { |
| 421 | verifier.VerifyUpdate( |
| 422 | reinterpret_cast<const uint8_t*>(kProofSignatureLabel), |
| 423 | sizeof(kProofSignatureLabel)); |
| 424 | uint32_t len = chlo_hash.length(); |
| 425 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(&len), sizeof(len)); |
| 426 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(chlo_hash.data()), |
| 427 | len); |
| 428 | } |
| 429 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 430 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(signed_data.data()), |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 431 | signed_data.size()); |
| 432 | |
| 433 | if (!verifier.VerifyFinal()) { |
| 434 | DLOG(WARNING) << "VerifyFinal failed"; |
| 435 | return false; |
| 436 | } |
| 437 | |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 438 | DVLOG(1) << "VerifyFinal success"; |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 442 | ProofVerifierChromium::ProofVerifierChromium( |
| 443 | CertVerifier* cert_verifier, |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 444 | CTPolicyEnforcer* ct_policy_enforcer, |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 445 | TransportSecurityState* transport_security_state, |
| 446 | CTVerifier* cert_transparency_verifier) |
[email protected] | 080b7793 | 2014-08-04 01:22:46 | [diff] [blame] | 447 | : cert_verifier_(cert_verifier), |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 448 | ct_policy_enforcer_(ct_policy_enforcer), |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 449 | transport_security_state_(transport_security_state), |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 450 | cert_transparency_verifier_(cert_transparency_verifier) { |
| 451 | DCHECK(cert_verifier_); |
| 452 | DCHECK(ct_policy_enforcer_); |
| 453 | DCHECK(transport_security_state_); |
| 454 | DCHECK(cert_transparency_verifier_); |
| 455 | } |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 456 | |
| 457 | ProofVerifierChromium::~ProofVerifierChromium() { |
| 458 | STLDeleteElements(&active_jobs_); |
| 459 | } |
| 460 | |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 461 | QuicAsyncStatus ProofVerifierChromium::VerifyProof( |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 462 | const std::string& hostname, |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 463 | const uint16_t port, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 464 | const std::string& server_config, |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 465 | QuicVersion quic_version, |
| 466 | base::StringPiece chlo_hash, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 467 | const std::vector<std::string>& certs, |
rjshade | c86dbfa | 2015-11-12 20:16:25 | [diff] [blame] | 468 | const std::string& cert_sct, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 469 | const std::string& signature, |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 470 | const ProofVerifyContext* verify_context, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 471 | std::string* error_details, |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 472 | std::unique_ptr<ProofVerifyDetails>* verify_details, |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 473 | ProofVerifierCallback* callback) { |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 474 | if (!verify_context) { |
| 475 | *error_details = "Missing context"; |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 476 | return QUIC_FAILURE; |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 477 | } |
| 478 | const ProofVerifyContextChromium* chromium_context = |
| 479 | reinterpret_cast<const ProofVerifyContextChromium*>(verify_context); |
danakj | ad1777e | 2016-04-16 00:56:42 | [diff] [blame] | 480 | std::unique_ptr<Job> job( |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 481 | new Job(this, cert_verifier_, ct_policy_enforcer_, |
rtenneti | 052774e | 2015-11-24 21:00:12 | [diff] [blame] | 482 | transport_security_state_, cert_transparency_verifier_, |
| 483 | chromium_context->cert_verify_flags, chromium_context->net_log)); |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 484 | QuicAsyncStatus status = job->VerifyProof( |
elawrence | 954bb547 | 2016-04-04 22:03:11 | [diff] [blame] | 485 | hostname, port, server_config, quic_version, chlo_hash, certs, cert_sct, |
rch | 28f6469d | 2016-03-13 21:13:08 | [diff] [blame] | 486 | signature, error_details, verify_details, callback); |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 487 | if (status == QUIC_PENDING) { |
[email protected] | 5c78ce6 | 2014-03-13 19:48:01 | [diff] [blame] | 488 | active_jobs_.insert(job.release()); |
| 489 | } |
| 490 | return status; |
| 491 | } |
| 492 | |
| 493 | void ProofVerifierChromium::OnJobComplete(Job* job) { |
| 494 | active_jobs_.erase(job); |
| 495 | delete job; |
| 496 | } |
| 497 | |
[email protected] | 2662ed56 | 2013-07-03 10:27:46 | [diff] [blame] | 498 | } // namespace net |