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/stl_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "crypto/signature_verifier.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_log.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/single_request_cert_verifier.h"
23 #include "net/cert/x509_certificate.h"
24 #include "net/cert/x509_util.h"
25 #include "net/http/transport_security_state.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 scoped_ptr
<SingleRequestCertVerifier
> verifier_
;
84 TransportSecurityState
* transport_security_state_
;
86 // |hostname| specifies the hostname for which |certs| is a valid chain.
87 std::string hostname_
;
89 scoped_ptr
<ProofVerifierCallback
> callback_
;
90 scoped_ptr
<ProofVerifyDetailsChromium
> verify_details_
;
91 std::string error_details_
;
93 // X509Certificate from a chain of DER encoded certificates.
94 scoped_refptr
<X509Certificate
> cert_
;
100 DISALLOW_COPY_AND_ASSIGN(Job
);
103 ProofVerifierChromium::Job::Job(
104 ProofVerifierChromium
* proof_verifier
,
105 CertVerifier
* cert_verifier
,
106 TransportSecurityState
* transport_security_state
,
107 const BoundNetLog
& net_log
)
108 : proof_verifier_(proof_verifier
),
109 verifier_(new SingleRequestCertVerifier(cert_verifier
)),
110 transport_security_state_(transport_security_state
),
111 next_state_(STATE_NONE
),
115 QuicAsyncStatus
ProofVerifierChromium::Job::VerifyProof(
116 const string
& hostname
,
117 const string
& server_config
,
118 const vector
<string
>& certs
,
119 const string
& signature
,
120 std::string
* error_details
,
121 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
122 ProofVerifierCallback
* callback
) {
123 DCHECK(error_details
);
124 DCHECK(verify_details
);
127 error_details
->clear();
129 if (STATE_NONE
!= next_state_
) {
130 *error_details
= "Certificate is already set and VerifyProof has begun";
131 DLOG(DFATAL
) << *error_details
;
135 verify_details_
.reset(new ProofVerifyDetailsChromium
);
138 *error_details
= "Failed to create certificate chain. Certs are empty.";
139 DLOG(WARNING
) << *error_details
;
140 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
141 *verify_details
= verify_details_
.Pass();
145 // Convert certs to X509Certificate.
146 vector
<StringPiece
> cert_pieces(certs
.size());
147 for (unsigned i
= 0; i
< certs
.size(); i
++) {
148 cert_pieces
[i
] = base::StringPiece(certs
[i
]);
150 cert_
= X509Certificate::CreateFromDERCertChain(cert_pieces
);
152 *error_details
= "Failed to create certificate chain";
153 DLOG(WARNING
) << *error_details
;
154 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
155 *verify_details
= verify_details_
.Pass();
159 // We call VerifySignature first to avoid copying of server_config and
161 if (!VerifySignature(server_config
, signature
, certs
[0])) {
162 *error_details
= "Failed to verify signature of server config";
163 DLOG(WARNING
) << *error_details
;
164 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
165 *verify_details
= verify_details_
.Pass();
169 hostname_
= hostname
;
171 next_state_
= STATE_VERIFY_CERT
;
172 switch (DoLoop(OK
)) {
174 *verify_details
= verify_details_
.Pass();
177 callback_
.reset(callback
);
180 *error_details
= error_details_
;
181 *verify_details
= verify_details_
.Pass();
186 int ProofVerifierChromium::Job::DoLoop(int last_result
) {
187 int rv
= last_result
;
189 State state
= next_state_
;
190 next_state_
= STATE_NONE
;
192 case STATE_VERIFY_CERT
:
194 rv
= DoVerifyCert(rv
);
196 case STATE_VERIFY_CERT_COMPLETE
:
197 rv
= DoVerifyCertComplete(rv
);
202 LOG(DFATAL
) << "unexpected state " << state
;
205 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
209 void ProofVerifierChromium::Job::OnIOComplete(int result
) {
210 int rv
= DoLoop(result
);
211 if (rv
!= ERR_IO_PENDING
) {
212 scoped_ptr
<ProofVerifierCallback
> callback(callback_
.Pass());
213 // Callback expects ProofVerifyDetails not ProofVerifyDetailsChromium.
214 scoped_ptr
<ProofVerifyDetails
> verify_details(verify_details_
.Pass());
215 callback
->Run(rv
== OK
, error_details_
, &verify_details
);
216 // Will delete |this|.
217 proof_verifier_
->OnJobComplete(this);
221 int ProofVerifierChromium::Job::DoVerifyCert(int result
) {
222 next_state_
= STATE_VERIFY_CERT_COMPLETE
;
225 return verifier_
->Verify(
229 SSLConfigService::GetCRLSet().get(),
230 &verify_details_
->cert_verify_result
,
231 base::Bind(&ProofVerifierChromium::Job::OnIOComplete
,
232 base::Unretained(this)),
236 int ProofVerifierChromium::Job::DoVerifyCertComplete(int result
) {
239 const CertVerifyResult
& cert_verify_result
=
240 verify_details_
->cert_verify_result
;
241 const CertStatus cert_status
= cert_verify_result
.cert_status
;
242 if (transport_security_state_
&&
244 (IsCertificateError(result
) && IsCertStatusMinorError(cert_status
))) &&
245 !transport_security_state_
->CheckPublicKeyPins(
247 true, /* sni_available */
248 cert_verify_result
.is_issued_by_known_root
,
249 cert_verify_result
.public_key_hashes
,
250 &verify_details_
->pinning_failure_log
)) {
251 result
= ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN
;
255 std::string error_string
= ErrorToString(result
);
256 error_details_
= StringPrintf("Failed to verify certificate chain: %s",
257 error_string
.c_str());
258 DLOG(WARNING
) << error_details_
;
261 // Exit DoLoop and return the result to the caller to VerifyProof.
262 DCHECK_EQ(STATE_NONE
, next_state_
);
266 bool ProofVerifierChromium::Job::VerifySignature(const string
& signed_data
,
267 const string
& signature
,
268 const string
& cert
) {
270 if (!asn1::ExtractSPKIFromDERCert(cert
, &spki
)) {
271 DLOG(WARNING
) << "ExtractSPKIFromDERCert failed";
275 crypto::SignatureVerifier verifier
;
278 X509Certificate::PublicKeyType type
;
279 X509Certificate::GetPublicKeyInfo(cert_
->os_cert_handle(), &size_bits
,
281 if (type
== X509Certificate::kPublicKeyTypeRSA
) {
282 crypto::SignatureVerifier::HashAlgorithm hash_alg
=
283 crypto::SignatureVerifier::SHA256
;
284 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg
= hash_alg
;
285 unsigned int hash_len
= 32; // 32 is the length of a SHA-256 hash.
287 bool ok
= verifier
.VerifyInitRSAPSS(
288 hash_alg
, mask_hash_alg
, hash_len
,
289 reinterpret_cast<const uint8
*>(signature
.data()), signature
.size(),
290 reinterpret_cast<const uint8
*>(spki
.data()), spki
.size());
292 DLOG(WARNING
) << "VerifyInitRSAPSS failed";
295 } else if (type
== X509Certificate::kPublicKeyTypeECDSA
) {
296 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT.
298 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
299 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 }
301 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or
302 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field
303 // as an AlgorithmIdentifier, the encoding MUST omit the parameters
304 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one
305 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-
306 // SHA384, or ecdsa-with-SHA512.
307 // See also RFC 5480, Appendix A.
308 static const uint8 kECDSAWithSHA256AlgorithmID
[] = {
311 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
314 if (!verifier
.VerifyInit(
315 kECDSAWithSHA256AlgorithmID
, sizeof(kECDSAWithSHA256AlgorithmID
),
316 reinterpret_cast<const uint8
*>(signature
.data()),
318 reinterpret_cast<const uint8
*>(spki
.data()),
320 DLOG(WARNING
) << "VerifyInit failed";
324 LOG(ERROR
) << "Unsupported public key type " << type
;
328 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(kProofSignatureLabel
),
329 sizeof(kProofSignatureLabel
));
330 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(signed_data
.data()),
333 if (!verifier
.VerifyFinal()) {
334 DLOG(WARNING
) << "VerifyFinal failed";
338 DVLOG(1) << "VerifyFinal success";
342 ProofVerifierChromium::ProofVerifierChromium(
343 CertVerifier
* cert_verifier
,
344 TransportSecurityState
* transport_security_state
)
345 : cert_verifier_(cert_verifier
),
346 transport_security_state_(transport_security_state
) {
349 ProofVerifierChromium::~ProofVerifierChromium() {
350 STLDeleteElements(&active_jobs_
);
353 QuicAsyncStatus
ProofVerifierChromium::VerifyProof(
354 const std::string
& hostname
,
355 const std::string
& server_config
,
356 const std::vector
<std::string
>& certs
,
357 const std::string
& signature
,
358 const ProofVerifyContext
* verify_context
,
359 std::string
* error_details
,
360 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
361 ProofVerifierCallback
* callback
) {
362 if (!verify_context
) {
363 *error_details
= "Missing context";
366 const ProofVerifyContextChromium
* chromium_context
=
367 reinterpret_cast<const ProofVerifyContextChromium
*>(verify_context
);
368 scoped_ptr
<Job
> job(new Job(this,
370 transport_security_state_
,
371 chromium_context
->net_log
));
372 QuicAsyncStatus status
= job
->VerifyProof(hostname
, server_config
, certs
,
373 signature
, error_details
,
374 verify_details
, callback
);
375 if (status
== QUIC_PENDING
) {
376 active_jobs_
.insert(job
.release());
381 void ProofVerifierChromium::OnJobComplete(Job
* job
) {
382 active_jobs_
.erase(job
);