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