OLD | NEW |
---|---|
(Empty) | |
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 | |
7 #include "base/bind.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/strings/stringprintf.h" | |
13 #include "crypto/signature_verifier.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/net_log.h" | |
16 #include "net/cert/asn1_util.h" | |
17 #include "net/cert/cert_status_flags.h" | |
18 #include "net/cert/cert_verifier.h" | |
19 #include "net/cert/cert_verify_result.h" | |
20 #include "net/cert/single_request_cert_verifier.h" | |
21 #include "net/cert/x509_certificate.h" | |
22 #include "net/cert/x509_util.h" | |
23 #include "net/quic/crypto/crypto_protocol.h" | |
24 #include "net/ssl/ssl_config_service.h" | |
25 | |
26 using base::StringPiece; | |
27 using base::StringPrintf; | |
28 using std::string; | |
29 using std::vector; | |
30 | |
31 namespace net { | |
32 | |
33 ProofVerifierChromium::ProofVerifierChromium(CertVerifier* cert_verifier, | |
34 const BoundNetLog& net_log) | |
35 : cert_verifier_(cert_verifier), | |
36 generation_counter_(0), | |
37 next_state_(STATE_NONE), | |
38 net_log_(net_log) { | |
39 } | |
40 | |
41 ProofVerifierChromium::~ProofVerifierChromium() { | |
42 verifier_.reset(); | |
43 | |
44 // Reset object state. | |
45 callback_.Reset(); | |
46 cert_verify_result_.Reset(); | |
47 } | |
48 | |
49 int ProofVerifierChromium::VerifyProof(const string& hostname, | |
50 const string& server_config, | |
51 const vector<string>& certs, | |
52 const string& signature, | |
53 uint64 generation_counter, | |
54 const CompletionCallback& callback) { | |
55 generation_counter_ = generation_counter; | |
56 error_details_.clear(); | |
57 | |
58 DCHECK_EQ(STATE_NONE, next_state_); | |
59 if (STATE_NONE != next_state_) { | |
60 error_details_ = "Certificate is already set and VerifyProof has begun"; | |
61 DLOG(WARNING) << error_details_; | |
62 return ERR_FAILED; | |
63 } | |
64 | |
65 if (certs.empty()) { | |
66 error_details_ = "Failed to create certificate chain. Certs are empty."; | |
67 DLOG(WARNING) << error_details_; | |
68 return ERR_FAILED; | |
69 } | |
70 | |
71 // Convert certs to X509Certificate. | |
72 vector<StringPiece> cert_pieces(certs.size()); | |
73 for (unsigned i = 0; i < certs.size(); i++) { | |
74 cert_pieces[i] = base::StringPiece(certs[i]); | |
75 } | |
76 cert_ = X509Certificate::CreateFromDERCertChain(cert_pieces); | |
77 if (!cert_.get()) { | |
78 cert_verify_result_.Reset(); | |
79 cert_verify_result_.cert_status = CERT_STATUS_INVALID; | |
80 error_details_ = "Failed to create certificate chain"; | |
81 DLOG(WARNING) << error_details_; | |
82 return ERR_FAILED; | |
83 } | |
84 | |
85 // We call VerifySignature first to avoid copying of server_config and | |
86 // signature. | |
87 if (!VerifySignature(server_config, signature, certs[0])) { | |
88 error_details_ = "Failed to verify signature of server config"; | |
89 DLOG(WARNING) << error_details_; | |
90 return ERR_FAILED; | |
91 } | |
92 | |
93 hostname_ = hostname; | |
94 callback_ = callback; | |
95 | |
96 next_state_ = STATE_VERIFY_CERT; | |
97 return DoLoop(OK); | |
98 } | |
99 | |
100 string ProofVerifierChromium::error_details() const { | |
101 return error_details_; | |
102 } | |
103 | |
104 uint64 ProofVerifierChromium::generation_counter() const { | |
105 return generation_counter_; | |
106 } | |
107 | |
108 int ProofVerifierChromium::DoLoop(int last_result) { | |
109 int rv = last_result; | |
110 do { | |
111 State state = next_state_; | |
112 next_state_ = STATE_NONE; | |
113 switch (state) { | |
114 case STATE_VERIFY_CERT: | |
115 DCHECK(rv == OK); | |
116 rv = DoVerifyCert(rv); | |
117 break; | |
118 case STATE_VERIFY_CERT_COMPLETE: | |
119 rv = DoVerifyCertComplete(rv); | |
120 break; | |
121 case STATE_NONE: | |
122 default: | |
123 rv = ERR_UNEXPECTED; | |
124 LOG(DFATAL) << "unexpected state " << state; | |
125 break; | |
126 } | |
127 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | |
128 return rv; | |
129 } | |
130 | |
131 void ProofVerifierChromium::OnIOComplete(int result) { | |
132 int rv = DoLoop(result); | |
133 if (rv != ERR_IO_PENDING) { | |
134 base::ResetAndReturn(&callback_).Run(rv); | |
135 } | |
136 } | |
137 | |
138 int ProofVerifierChromium::DoVerifyCert(int result) { | |
139 next_state_ = STATE_VERIFY_CERT_COMPLETE; | |
140 | |
141 int flags = CertVerifier::VERIFY_REV_CHECKING_ENABLED | | |
agl
2013/07/02 15:20:08
I would remove both of these.
VERIFY_REV_CHECKING
ramant (doing other things)
2013/07/02 19:45:51
Done.
| |
142 CertVerifier::VERIFY_CERT_IO_ENABLED; | |
143 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | |
144 return verifier_->Verify( | |
145 cert_.get(), | |
146 hostname_, | |
147 flags, | |
148 SSLConfigService::GetCRLSet().get(), | |
149 &cert_verify_result_, | |
150 base::Bind(&ProofVerifierChromium::OnIOComplete, | |
151 base::Unretained(this)), | |
152 net_log_); | |
153 } | |
154 | |
155 int ProofVerifierChromium::DoVerifyCertComplete(int result) { | |
156 verifier_.reset(); | |
157 | |
158 if (result <= ERR_FAILED) { | |
159 error_details_ = StringPrintf("Failed to verify certificate chain: %s", | |
160 ErrorToString(result)); | |
161 DLOG(WARNING) << error_details_; | |
162 result = ERR_FAILED; | |
163 } | |
164 | |
165 // Exit DoLoop and return the result to the caller to VerifyProof. | |
166 DCHECK_EQ(STATE_NONE, next_state_); | |
167 return result; | |
168 } | |
169 | |
170 bool ProofVerifierChromium::VerifySignature(const string& signed_data, | |
171 const string& signature, | |
172 const string& cert) { | |
173 StringPiece spki; | |
174 if (!asn1::ExtractSPKIFromDERCert(cert, &spki)) { | |
175 DLOG(WARNING) << "ExtractSPKIFromDERCert failed"; | |
176 return false; | |
177 } | |
178 | |
179 crypto::SignatureVerifier verifier; | |
180 | |
181 size_t size_bits; | |
182 X509Certificate::PublicKeyType type; | |
183 X509Certificate::GetPublicKeyInfo(cert_->os_cert_handle(), &size_bits, | |
184 &type); | |
185 if (type == X509Certificate::kPublicKeyTypeRSA) { | |
186 crypto::SignatureVerifier::HashAlgorithm hash_alg = | |
187 crypto::SignatureVerifier::SHA256; | |
188 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg = hash_alg; | |
189 unsigned int hash_len = 32; // 32 is the length of a SHA-256 hash. | |
190 // TODO(wtc): change this to hash_len when we can change the wire format. | |
191 unsigned int salt_len = signature.size() - hash_len - 2; | |
192 | |
193 bool ok = verifier.VerifyInitRSAPSS( | |
194 hash_alg, mask_hash_alg, salt_len, | |
195 reinterpret_cast<const uint8*>(signature.data()), signature.size(), | |
196 reinterpret_cast<const uint8*>(spki.data()), spki.size()); | |
197 if (!ok) { | |
198 DLOG(WARNING) << "VerifyInitRSAPSS failed"; | |
199 return false; | |
200 } | |
201 } else if (type == X509Certificate::kPublicKeyTypeECDSA) { | |
202 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT. | |
203 // RFC 5758: | |
204 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) | |
205 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } | |
206 // ... | |
207 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or | |
208 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field | |
209 // as an AlgorithmIdentifier, the encoding MUST omit the parameters | |
210 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one | |
211 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with- | |
212 // SHA384, or ecdsa-with-SHA512. | |
213 // See also RFC 5480, Appendix A. | |
214 static const uint8 kECDSAWithSHA256AlgorithmID[] = { | |
215 0x30, 0x0a, | |
216 0x06, 0x08, | |
217 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, | |
218 }; | |
219 | |
220 if (!verifier.VerifyInit( | |
221 kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID), | |
222 reinterpret_cast<const uint8*>(signature.data()), | |
223 signature.size(), | |
224 reinterpret_cast<const uint8*>(spki.data()), | |
225 spki.size())) { | |
226 DLOG(WARNING) << "VerifyInit failed"; | |
227 return false; | |
228 } | |
229 } else { | |
230 LOG(ERROR) << "Unsupported public key type " << type; | |
231 return false; | |
232 } | |
233 | |
234 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(kProofSignatureLabel), | |
235 sizeof(kProofSignatureLabel)); | |
236 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(signed_data.data()), | |
237 signed_data.size()); | |
238 | |
239 if (!verifier.VerifyFinal()) { | |
240 DLOG(WARNING) << "VerifyFinal failed"; | |
241 return false; | |
242 } | |
243 | |
244 DLOG(INFO) << "VerifyFinal success"; | |
245 return true; | |
246 } | |
247 | |
248 } // namespace net | |
OLD | NEW |