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/stl_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "crypto/signature_verifier.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_log.h"
17 #include "net/cert/asn1_util.h"
18 #include "net/cert/cert_status_flags.h"
19 #include "net/cert/cert_verifier.h"
20 #include "net/cert/cert_verify_result.h"
21 #include "net/cert/single_request_cert_verifier.h"
22 #include "net/cert/x509_certificate.h"
23 #include "net/cert/x509_util.h"
24 #include "net/quic/crypto/crypto_protocol.h"
25 #include "net/ssl/ssl_config_service.h"
27 using base::StringPiece
;
28 using base::StringPrintf
;
34 // A Job handles the verification of a single proof. It is owned by the
35 // ProofVerifier. If the verification can not complete synchronously, it
36 // will notify the ProofVerifier upon completion.
37 class ProofVerifierChromium::Job
{
39 Job(ProofVerifierChromium
* proof_verifier
,
40 CertVerifier
* cert_verifier
,
41 const BoundNetLog
& net_log
);
43 // Starts the proof verification. If |QUIC_PENDING| is returned, then
44 // |callback| will be invoked asynchronously when the verification completes.
45 QuicAsyncStatus
VerifyProof(const std::string
& hostname
,
46 const std::string
& server_config
,
47 const std::vector
<std::string
>& certs
,
48 const std::string
& signature
,
49 std::string
* error_details
,
50 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
51 ProofVerifierCallback
* callback
);
57 STATE_VERIFY_CERT_COMPLETE
,
60 int DoLoop(int last_io_result
);
61 void OnIOComplete(int result
);
62 int DoVerifyCert(int result
);
63 int DoVerifyCertComplete(int result
);
65 bool VerifySignature(const std::string
& signed_data
,
66 const std::string
& signature
,
67 const std::string
& cert
);
69 // Proof verifier to notify when this jobs completes.
70 ProofVerifierChromium
* proof_verifier_
;
72 // The underlying verifier used for verifying certificates.
73 scoped_ptr
<SingleRequestCertVerifier
> verifier_
;
75 // |hostname| specifies the hostname for which |certs| is a valid chain.
76 std::string hostname_
;
78 scoped_ptr
<ProofVerifierCallback
> callback_
;
79 scoped_ptr
<ProofVerifyDetailsChromium
> verify_details_
;
80 std::string error_details_
;
82 // X509Certificate from a chain of DER encoded certificates.
83 scoped_refptr
<X509Certificate
> cert_
;
89 DISALLOW_COPY_AND_ASSIGN(Job
);
92 ProofVerifierChromium::Job::Job(ProofVerifierChromium
* proof_verifier
,
93 CertVerifier
* cert_verifier
,
94 const BoundNetLog
& net_log
)
95 : proof_verifier_(proof_verifier
),
96 verifier_(new SingleRequestCertVerifier(cert_verifier
)),
97 next_state_(STATE_NONE
),
101 QuicAsyncStatus
ProofVerifierChromium::Job::VerifyProof(
102 const string
& hostname
,
103 const string
& server_config
,
104 const vector
<string
>& certs
,
105 const string
& signature
,
106 std::string
* error_details
,
107 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
108 ProofVerifierCallback
* callback
) {
109 DCHECK(error_details
);
110 DCHECK(verify_details
);
113 error_details
->clear();
115 if (STATE_NONE
!= next_state_
) {
116 *error_details
= "Certificate is already set and VerifyProof has begun";
117 DLOG(DFATAL
) << *error_details
;
121 verify_details_
.reset(new ProofVerifyDetailsChromium
);
124 *error_details
= "Failed to create certificate chain. Certs are empty.";
125 DLOG(WARNING
) << *error_details
;
126 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
127 *verify_details
= verify_details_
.Pass();
131 // Convert certs to X509Certificate.
132 vector
<StringPiece
> cert_pieces(certs
.size());
133 for (unsigned i
= 0; i
< certs
.size(); i
++) {
134 cert_pieces
[i
] = base::StringPiece(certs
[i
]);
136 cert_
= X509Certificate::CreateFromDERCertChain(cert_pieces
);
138 *error_details
= "Failed to create certificate chain";
139 DLOG(WARNING
) << *error_details
;
140 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
141 *verify_details
= verify_details_
.Pass();
145 // We call VerifySignature first to avoid copying of server_config and
147 if (!VerifySignature(server_config
, signature
, certs
[0])) {
148 *error_details
= "Failed to verify signature of server config";
149 DLOG(WARNING
) << *error_details
;
150 verify_details_
->cert_verify_result
.cert_status
= CERT_STATUS_INVALID
;
151 *verify_details
= verify_details_
.Pass();
155 hostname_
= hostname
;
157 next_state_
= STATE_VERIFY_CERT
;
158 switch (DoLoop(OK
)) {
160 *verify_details
= verify_details_
.Pass();
163 callback_
.reset(callback
);
166 *error_details
= error_details_
;
167 *verify_details
= verify_details_
.Pass();
172 int ProofVerifierChromium::Job::DoLoop(int last_result
) {
173 int rv
= last_result
;
175 State state
= next_state_
;
176 next_state_
= STATE_NONE
;
178 case STATE_VERIFY_CERT
:
180 rv
= DoVerifyCert(rv
);
182 case STATE_VERIFY_CERT_COMPLETE
:
183 rv
= DoVerifyCertComplete(rv
);
188 LOG(DFATAL
) << "unexpected state " << state
;
191 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
195 void ProofVerifierChromium::Job::OnIOComplete(int result
) {
196 int rv
= DoLoop(result
);
197 if (rv
!= ERR_IO_PENDING
) {
198 scoped_ptr
<ProofVerifierCallback
> callback(callback_
.Pass());
199 // Callback expects ProofVerifyDetails not ProofVerifyDetailsChromium.
200 scoped_ptr
<ProofVerifyDetails
> verify_details(verify_details_
.Pass());
201 callback
->Run(rv
== OK
, error_details_
, &verify_details
);
202 // Will delete |this|.
203 proof_verifier_
->OnJobComplete(this);
207 int ProofVerifierChromium::Job::DoVerifyCert(int result
) {
208 next_state_
= STATE_VERIFY_CERT_COMPLETE
;
211 return verifier_
->Verify(
215 SSLConfigService::GetCRLSet().get(),
216 &verify_details_
->cert_verify_result
,
217 base::Bind(&ProofVerifierChromium::Job::OnIOComplete
,
218 base::Unretained(this)),
222 int ProofVerifierChromium::Job::DoVerifyCertComplete(int result
) {
226 error_details_
= StringPrintf("Failed to verify certificate chain: %s",
227 ErrorToString(result
));
228 DLOG(WARNING
) << error_details_
;
231 // Exit DoLoop and return the result to the caller to VerifyProof.
232 DCHECK_EQ(STATE_NONE
, next_state_
);
236 bool ProofVerifierChromium::Job::VerifySignature(const string
& signed_data
,
237 const string
& signature
,
238 const string
& cert
) {
240 if (!asn1::ExtractSPKIFromDERCert(cert
, &spki
)) {
241 DLOG(WARNING
) << "ExtractSPKIFromDERCert failed";
245 crypto::SignatureVerifier verifier
;
248 X509Certificate::PublicKeyType type
;
249 X509Certificate::GetPublicKeyInfo(cert_
->os_cert_handle(), &size_bits
,
251 if (type
== X509Certificate::kPublicKeyTypeRSA
) {
252 crypto::SignatureVerifier::HashAlgorithm hash_alg
=
253 crypto::SignatureVerifier::SHA256
;
254 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg
= hash_alg
;
255 unsigned int hash_len
= 32; // 32 is the length of a SHA-256 hash.
257 bool ok
= verifier
.VerifyInitRSAPSS(
258 hash_alg
, mask_hash_alg
, hash_len
,
259 reinterpret_cast<const uint8
*>(signature
.data()), signature
.size(),
260 reinterpret_cast<const uint8
*>(spki
.data()), spki
.size());
262 DLOG(WARNING
) << "VerifyInitRSAPSS failed";
265 } else if (type
== X509Certificate::kPublicKeyTypeECDSA
) {
266 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT.
268 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
269 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 }
271 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or
272 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field
273 // as an AlgorithmIdentifier, the encoding MUST omit the parameters
274 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one
275 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-
276 // SHA384, or ecdsa-with-SHA512.
277 // See also RFC 5480, Appendix A.
278 static const uint8 kECDSAWithSHA256AlgorithmID
[] = {
281 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
284 if (!verifier
.VerifyInit(
285 kECDSAWithSHA256AlgorithmID
, sizeof(kECDSAWithSHA256AlgorithmID
),
286 reinterpret_cast<const uint8
*>(signature
.data()),
288 reinterpret_cast<const uint8
*>(spki
.data()),
290 DLOG(WARNING
) << "VerifyInit failed";
294 LOG(ERROR
) << "Unsupported public key type " << type
;
298 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(kProofSignatureLabel
),
299 sizeof(kProofSignatureLabel
));
300 verifier
.VerifyUpdate(reinterpret_cast<const uint8
*>(signed_data
.data()),
303 if (!verifier
.VerifyFinal()) {
304 DLOG(WARNING
) << "VerifyFinal failed";
308 DVLOG(1) << "VerifyFinal success";
312 ProofVerifierChromium::ProofVerifierChromium(CertVerifier
* cert_verifier
)
313 : cert_verifier_(cert_verifier
) {}
315 ProofVerifierChromium::~ProofVerifierChromium() {
316 STLDeleteElements(&active_jobs_
);
319 QuicAsyncStatus
ProofVerifierChromium::VerifyProof(
320 const std::string
& hostname
,
321 const std::string
& server_config
,
322 const std::vector
<std::string
>& certs
,
323 const std::string
& signature
,
324 const ProofVerifyContext
* verify_context
,
325 std::string
* error_details
,
326 scoped_ptr
<ProofVerifyDetails
>* verify_details
,
327 ProofVerifierCallback
* callback
) {
328 if (!verify_context
) {
329 *error_details
= "Missing context";
332 const ProofVerifyContextChromium
* chromium_context
=
333 reinterpret_cast<const ProofVerifyContextChromium
*>(verify_context
);
334 scoped_ptr
<Job
> job(new Job(this, cert_verifier_
, chromium_context
->net_log
));
335 QuicAsyncStatus status
= job
->VerifyProof(hostname
, server_config
, certs
,
336 signature
, error_details
,
337 verify_details
, callback
);
338 if (status
== QUIC_PENDING
) {
339 active_jobs_
.insert(job
.release());
344 void ProofVerifierChromium::OnJobComplete(Job
* job
) {
345 active_jobs_
.erase(job
);