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.
5 #include "net/quic/crypto/proof_verifier_chromium.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/stl_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "crypto/signature_verifier.h"
17 #include "net/base/net_errors.h"
18 #include "net/cert/asn1_util.h"
19 #include "net/cert/cert_status_flags.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/cert/cert_verify_result.h"
22 #include "net/cert/x509_certificate.h"
23 #include "net/cert/x509_util.h"
24 #include "net/http/transport_security_state.h"
25 #include "net/log/net_log.h"
26 #include "net/quic/crypto/crypto_protocol.h"
27 #include "net/ssl/ssl_config_service.h"
29 using base::StringPiece
;
30 using base::StringPrintf
;
36 ProofVerifyDetails
* ProofVerifyDetailsChromium::Clone() const {
37 ProofVerifyDetailsChromium
* other
= new ProofVerifyDetailsChromium
;
38 other
->cert_verify_result
= cert_verify_result
;
42 // A Job handles the verification of a single proof. It is owned by the
43 // ProofVerifier. If the verification can not complete synchronously, it
44 // will notify the ProofVerifier upon completion.
45 class ProofVerifierChromium::Job
{
47 Job(ProofVerifierChromium
* proof_verifier
,
48 CertVerifier
* cert_verifier
,
49 TransportSecurityState
* transport_security_state
,
50 const BoundNetLog
& net_log
);
52 // Starts the proof verification. If |QUIC_PENDING| is returned, then
53 // |callback| will be invoked asynchronously when the verification completes.
54 QuicAsyncStatus
VerifyProof(const std::string
& hostname
,
55 const std::string
& server_config
,
56 const std::vector
<std::string
>& certs
,
57 const std::string
& signature
,
58 std::string
* error_details
,
59 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
60 ProofVerifierCallback
* callback
);
66 STATE_VERIFY_CERT_COMPLETE
,
69 int DoLoop(int last_io_result
);
70 void OnIOComplete(int result
);
71 int DoVerifyCert(int result
);
72 int DoVerifyCertComplete(int result
);
74 bool VerifySignature(const std::string
& signed_data
,
75 const std::string
& signature
,
76 const std::string
& cert
);
78 // Proof verifier to notify when this jobs completes.
79 ProofVerifierChromium
* proof_verifier_
;
81 // The underlying verifier used for verifying certificates.
82 CertVerifier
* verifier_
;
83 scoped_ptr
<CertVerifier::Request
> cert_verifier_request_
;
85 TransportSecurityState
* transport_security_state_
;
87 // |hostname| specifies the hostname for which |certs| is a valid chain.
88 std::string hostname_
;
90 scoped_ptr
<ProofVerifierCallback
> callback_
;
91 scoped_ptr
<ProofVerifyDetailsChromium
> verify_details_
;
92 std::string error_details_
;
94 // X509Certificate from a chain of DER encoded certificates.
95 scoped_refptr
<X509Certificate
> cert_
;
101 DISALLOW_COPY_AND_ASSIGN(Job
);
104 ProofVerifierChromium::Job::Job(
105 ProofVerifierChromium
* proof_verifier
,
106 CertVerifier
* cert_verifier
,
107 TransportSecurityState
* transport_security_state
,
108 const BoundNetLog
& net_log
)
109 : proof_verifier_(proof_verifier
),
110 verifier_(cert_verifier
),
111 transport_security_state_(transport_security_state
),
112 next_state_(STATE_NONE
),
116 QuicAsyncStatus
ProofVerifierChromium::Job::VerifyProof(
117 const string
& hostname
,
118 const string
& server_config
,
119 const vector
<string
>& certs
,
120 const string
& signature
,
121 std::string
* error_details
,
122 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
123 ProofVerifierCallback
* callback
) {
124 DCHECK(error_details
);
125 DCHECK(verify_details
);
128 error_details
->clear();
130 if (STATE_NONE
!= next_state_
) {
131 *error_details
= "Certificate is already set and VerifyProof has begun";
132 DLOG(DFATAL
) << *error_details
;
136 verify_details_
.reset(new ProofVerifyDetailsChromium
);
139 *error_details
= "Failed to create certificate chain. Certs are empty.";
140 DLOG(WARNING
) << *error_details
;
141 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
142 *verify_details
= verify_details_
.Pass();
146 // Convert certs to X509Certificate.
147 vector
<StringPiece
> cert_pieces(certs
.size());
148 for (unsigned i
= 0; i
< certs
.size(); i
++) {
149 cert_pieces
[i
] = base::StringPiece(certs
[i
]);
151 cert_
= X509Certificate::CreateFromDERCertChain(cert_pieces
);
153 *error_details
= "Failed to create certificate chain";
154 DLOG(WARNING
) << *error_details
;
155 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
156 *verify_details
= verify_details_
.Pass();
160 // We call VerifySignature first to avoid copying of server_config and
162 if (!VerifySignature(server_config
, signature
, certs
[0])) {
163 *error_details
= "Failed to verify signature of server config";
164 DLOG(WARNING
) << *error_details
;
165 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
166 *verify_details
= verify_details_
.Pass();
170 hostname_
= hostname
;
172 next_state_
= STATE_VERIFY_CERT
;
173 switch (DoLoop(OK
)) {
175 *verify_details
= verify_details_
.Pass();
178 callback_
.reset(callback
);
181 *error_details
= error_details_
;
182 *verify_details
= verify_details_
.Pass();
187 int ProofVerifierChromium::Job::DoLoop(int last_result
) {
188 int rv
= last_result
;
190 State state
= next_state_
;
191 next_state_
= STATE_NONE
;
193 case STATE_VERIFY_CERT
:
195 rv
= DoVerifyCert(rv
);
197 case STATE_VERIFY_CERT_COMPLETE
:
198 rv
= DoVerifyCertComplete(rv
);
203 LOG(DFATAL
) << "unexpected state " << state
;
206 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
210 void ProofVerifierChromium::Job::OnIOComplete(int result
) {
211 int rv
= DoLoop(result
);
212 if (rv
!= ERR_IO_PENDING
) {
213 scoped_ptr
<ProofVerifierCallback
> callback(callback_
.Pass());
214 // Callback expects ProofVerifyDetails not ProofVerifyDetailsChromium.
215 scoped_ptr
<ProofVerifyDetails
> verify_details(verify_details_
.Pass());
216 callback
->Run(rv
== OK
, error_details_
, &verify_details
);
217 // Will delete |this|.
218 proof_verifier_
->OnJobComplete(this);
222 int ProofVerifierChromium::Job::DoVerifyCert(int result
) {
223 next_state_
= STATE_VERIFY_CERT_COMPLETE
;
226 return verifier_
->Verify(cert_
.get(), hostname_
, std::string(), flags
,
227 SSLConfigService::GetCRLSet().get(),
228 &verify_details_
->cert_verify_result
,
229 base::Bind(&ProofVerifierChromium::Job::OnIOComplete
,
230 base::Unretained(this)),
231 &cert_verifier_request_
, net_log_
);
234 int ProofVerifierChromium::Job::DoVerifyCertComplete(int result
) {
235 cert_verifier_request_
.reset();
237 const CertVerifyResult
& cert_verify_result
=
238 verify_details_
->cert_verify_result
;
239 const CertStatus cert_status
= cert_verify_result
.cert_status
;
240 if (transport_security_state_
&&
242 (IsCertificateError(result
) && IsCertStatusMinorError(cert_status
))) &&
243 !transport_security_state_
->CheckPublicKeyPins(
245 cert_verify_result
.is_issued_by_known_root
,
246 cert_verify_result
.public_key_hashes
,
247 &verify_details_
->pinning_failure_log
)) {
248 result
= ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN
;
251 scoped_refptr
<ct::EVCertsWhitelist
> ev_whitelist
=
252 SSLConfigService::GetEVCertsWhitelist();
253 if ((cert_status
& CERT_STATUS_IS_EV
) && ev_whitelist
.get() &&
254 ev_whitelist
->IsValid()) {
255 const SHA256HashValue
fingerprint(
256 X509Certificate::CalculateFingerprint256(cert_
->os_cert_handle()));
258 UMA_HISTOGRAM_BOOLEAN(
259 "Net.SSL_EVCertificateInWhitelist",
260 ev_whitelist
->ContainsCertificateHash(
261 std::string(reinterpret_cast<const char*>(fingerprint
.data
), 8)));
265 std::string error_string
= ErrorToString(result
);
266 error_details_
= StringPrintf("Failed to verify certificate chain: %s",
267 error_string
.c_str());
268 DLOG(WARNING
) << error_details_
;
271 // Exit DoLoop and return the result to the caller to VerifyProof.
272 DCHECK_EQ(STATE_NONE
, next_state_
);
276 bool ProofVerifierChromium::Job::VerifySignature(const string
& signed_data
,
277 const string
& signature
,
278 const string
& cert
) {
279 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
280 tracked_objects::ScopedTracker
tracking_profile(
281 FROM_HERE_WITH_EXPLICIT_FUNCTION(
282 "422516 ProofVerifierChromium::Job::VerifySignature"));
285 if (!asn1::ExtractSPKIFromDERCert(cert
, &spki
)) {
286 DLOG(WARNING
) << "ExtractSPKIFromDERCert failed";
290 crypto::SignatureVerifier verifier
;
293 X509Certificate::PublicKeyType type
;
294 X509Certificate::GetPublicKeyInfo(cert_
->os_cert_handle(), &size_bits
,
296 if (type
== X509Certificate::kPublicKeyTypeRSA
) {
297 crypto::SignatureVerifier::HashAlgorithm hash_alg
=
298 crypto::SignatureVerifier::SHA256
;
299 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg
= hash_alg
;
300 unsigned int hash_len
= 32; // 32 is the length of a SHA-256 hash.
302 bool ok
= verifier
.VerifyInitRSAPSS(
303 hash_alg
, mask_hash_alg
, hash_len
,
304 reinterpret_cast<const uint8
*>(signature
.data()), signature
.size(),
305 reinterpret_cast<const uint8
*>(spki
.data()), spki
.size());
307 DLOG(WARNING
) << "VerifyInitRSAPSS failed";
310 } else if (type
== X509Certificate::kPublicKeyTypeECDSA
) {
311 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT.
313 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
314 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 }
316 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or
317 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field
318 // as an AlgorithmIdentifier, the encoding MUST omit the parameters
319 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one
320 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-
321 // SHA384, or ecdsa-with-SHA512.
322 // See also RFC 5480, Appendix A.
323 static const uint8 kECDSAWithSHA256AlgorithmID
[] = {
326 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
329 if (!verifier
.VerifyInit(
330 kECDSAWithSHA256AlgorithmID
, sizeof(kECDSAWithSHA256AlgorithmID
),
331 reinterpret_cast<const uint8
*>(signature
.data()),
333 reinterpret_cast<const uint8
*>(spki
.data()),
335 DLOG(WARNING
) << "VerifyInit failed";
339 LOG(ERROR
) << "Unsupported public key type " << type
;
343 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(kProofSignatureLabel
),
344 sizeof(kProofSignatureLabel
));
345 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(signed_data
.data()),
348 if (!verifier
.VerifyFinal()) {
349 DLOG(WARNING
) << "VerifyFinal failed";
353 DVLOG(1) << "VerifyFinal success";
357 ProofVerifierChromium::ProofVerifierChromium(
358 CertVerifier
* cert_verifier
,
359 TransportSecurityState
* transport_security_state
)
360 : cert_verifier_(cert_verifier
),
361 transport_security_state_(transport_security_state
) {
364 ProofVerifierChromium::~ProofVerifierChromium() {
365 STLDeleteElements(&active_jobs_
);
368 QuicAsyncStatus
ProofVerifierChromium::VerifyProof(
369 const std::string
& hostname
,
370 const std::string
& server_config
,
371 const std::vector
<std::string
>& certs
,
372 const std::string
& signature
,
373 const ProofVerifyContext
* verify_context
,
374 std::string
* error_details
,
375 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
376 ProofVerifierCallback
* callback
) {
377 if (!verify_context
) {
378 *error_details
= "Missing context";
381 const ProofVerifyContextChromium
* chromium_context
=
382 reinterpret_cast<const ProofVerifyContextChromium
*>(verify_context
);
383 scoped_ptr
<Job
> job(new Job(this,
385 transport_security_state_
,
386 chromium_context
->net_log
));
387 QuicAsyncStatus status
= job
->VerifyProof(hostname
, server_config
, certs
,
388 signature
, error_details
,
389 verify_details
, callback
);
390 if (status
== QUIC_PENDING
) {
391 active_jobs_
.insert(job
.release());
396 void ProofVerifierChromium::OnJobComplete(Job
* job
) {
397 active_jobs_
.erase(job
);