OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "components/cronet/cert/cert_verifier_cache_persister.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/metrics/histogram_macros.h" | |
Ryan Sleevi
2016/06/21 01:22:10
Unused?
ramant (doing other things)
2016/06/21 16:52:02
Done.
| |
12 #include "base/strings/string_piece.h" | |
13 #include "base/time/time.h" | |
14 #include "components/cronet/cert/proto/cert_verification.pb.h" | |
15 #include "net/base/hash_value.h" | |
16 #include "net/cert/caching_cert_verifier.h" | |
17 #include "net/cert/cert_type.h" | |
Ryan Sleevi
2016/06/21 01:22:10
Why are you including this? Seems unused, but is a
ramant (doing other things)
2016/06/21 16:52:01
Done.
| |
18 #include "net/cert/cert_verify_result.h" | |
19 #include "net/cert/x509_cert_types.h" | |
Ryan Sleevi
2016/06/21 01:22:10
Unused
ramant (doing other things)
2016/06/21 16:52:02
Done.
| |
20 #include "net/cert/x509_certificate.h" | |
21 | |
22 namespace cronet { | |
23 | |
24 namespace { | |
25 | |
26 typedef std::vector<std::string> CertVector; | |
27 typedef std::map<std::string, size_t> SerializedCertMap; | |
28 typedef std::map<size_t, std::string> DeserializedCertMap; | |
29 | |
30 bool SerializeCertHandle(const net::X509Certificate::OSCertHandle& cert_handle, | |
Ryan Sleevi
2016/06/21 01:22:10
Document
ramant (doing other things)
2016/06/21 16:52:02
Done.
| |
31 SerializedCertMap* serialized_certs, | |
32 size_t* cert_number) { | |
33 std::string encoded; | |
34 if (!net::X509Certificate::GetDEREncoded(cert_handle, &encoded)) { | |
35 NOTREACHED(); | |
36 return false; | |
37 } | |
38 // Determine if |cert_handle| was already serialized. If so, simply return a | |
39 // reference to that entry. Otherwise, add a new entry to the set of certs to | |
40 // be serialized (|serialized_certs|). | |
41 auto result = | |
42 serialized_certs->insert({encoded, serialized_certs->size() + 1}); | |
43 *cert_number = result.first->second; | |
44 return true; | |
45 } | |
46 | |
47 // Update |certificate| with certificate number and updates |serialized_certs| | |
48 // with DER-encoded representation of certificate if the certicate is not in | |
49 // |serialized_certs|. Returns true if data is serialized correctly. | |
50 bool SerializeCertificate(net::X509Certificate* cert, | |
51 SerializedCertMap* serialized_certs, | |
52 cronet_pb::CertVerificationCertificate* certificate) { | |
53 size_t cert_number = 0; | |
54 if (!SerializeCertHandle(cert->os_cert_handle(), serialized_certs, | |
55 &cert_number)) { | |
56 NOTREACHED(); | |
57 return false; | |
58 } | |
59 certificate->add_cert_numbers(cert_number); | |
60 const net::X509Certificate::X509Certificate::OSCertHandles& | |
61 intermediate_ca_certs = cert->GetIntermediateCertificates(); | |
62 for (size_t i = 0; i < intermediate_ca_certs.size(); ++i) { | |
63 if (!SerializeCertHandle(intermediate_ca_certs[i], serialized_certs, | |
64 &cert_number)) { | |
65 NOTREACHED(); | |
66 return false; | |
67 } | |
68 certificate->add_cert_numbers(cert_number); | |
69 } | |
70 return true; | |
71 } | |
72 | |
73 // Deserializes |certificate| using the certificate database provided in | |
74 // |deserialized_certs|. Returns the parsed certificate on success, or nullptr | |
75 // if deserialization failed. | |
76 scoped_refptr<net::X509Certificate> DeserializeCertificate( | |
77 const cronet_pb::CertVerificationCertificate& certificate, | |
78 const DeserializedCertMap& deserialized_certs) { | |
79 if (0 == certificate.cert_numbers_size()) | |
80 return nullptr; | |
81 std::vector<base::StringPiece> der_cert_pieces( | |
82 certificate.cert_numbers_size()); | |
83 for (int i = 0; i < certificate.cert_numbers_size(); i++) { | |
84 size_t cert_number = certificate.cert_numbers(i); | |
85 DeserializedCertMap::const_iterator it = | |
86 deserialized_certs.find(cert_number); | |
87 if (it == deserialized_certs.end()) | |
88 return nullptr; | |
89 der_cert_pieces[i] = base::StringPiece(it->second); | |
90 } | |
91 return net::X509Certificate::CreateFromDERCertChain(der_cert_pieces); | |
92 } | |
93 | |
94 // Serializes |params| into |request_params|, updating |serialized_certs| with | |
95 // the set of raw certificates that will be needed to deserialize the | |
96 // certificate in |request_params| via DeserializeCertificate(). | |
97 bool SerializeRequestParams( | |
98 const net::CertVerifier::RequestParams& params, | |
99 SerializedCertMap* serialized_certs, | |
100 cronet_pb::CertVerificationRequestParams* request_params) { | |
101 cronet_pb::CertVerificationCertificate* certificate = | |
102 request_params->mutable_certificate(); | |
103 if (!SerializeCertificate(params.certificate().get(), serialized_certs, | |
104 certificate)) { | |
105 NOTREACHED(); | |
106 return false; | |
107 } | |
108 request_params->set_hostname(params.hostname()); | |
109 request_params->set_flags(params.flags()); | |
110 request_params->set_ocsp_response(params.ocsp_response()); | |
111 for (const scoped_refptr<net::X509Certificate>& cert : | |
112 params.additional_trust_anchors()) { | |
113 certificate = request_params->add_additional_trust_anchors(); | |
114 if (!SerializeCertificate(cert.get(), serialized_certs, certificate)) { | |
115 NOTREACHED(); | |
116 return false; | |
117 } | |
118 } | |
119 return true; | |
120 } | |
121 | |
122 // Serializes |result| into |cached_result|, updating |serialized_certs| with | |
123 // the set of raw certificates that will be needed to deserialize the | |
124 // certificate in |cached_result| via DeserializeCertificate(). | |
125 bool SerializeCachedResult( | |
126 const net::CertVerifyResult& result, | |
127 SerializedCertMap* serialized_certs, | |
128 cronet_pb::CertVerificationCachedResult* cached_result) { | |
129 cronet_pb::CertVerificationResult* cert_verification_result = | |
130 cached_result->mutable_result(); | |
131 cronet_pb::CertVerificationCertificate* certificate = | |
132 cert_verification_result->mutable_verified_cert(); | |
133 if (!SerializeCertificate(result.verified_cert.get(), serialized_certs, | |
134 certificate)) { | |
135 NOTREACHED(); | |
136 return false; | |
137 } | |
138 cert_verification_result->set_cert_status(result.cert_status); | |
139 cert_verification_result->set_has_md2(result.has_md2); | |
140 cert_verification_result->set_has_md4(result.has_md4); | |
141 cert_verification_result->set_has_md5(result.has_md5); | |
142 cert_verification_result->set_has_sha1(result.has_sha1); | |
143 cert_verification_result->set_has_sha1_leaf(result.has_sha1_leaf); | |
144 for (const net::HashValue& value : result.public_key_hashes) | |
145 cert_verification_result->add_public_key_hashes(value.ToString()); | |
146 cert_verification_result->set_is_issued_by_known_root( | |
147 result.is_issued_by_known_root); | |
148 cert_verification_result->set_is_issued_by_additional_trust_anchor( | |
149 result.is_issued_by_additional_trust_anchor); | |
150 cert_verification_result->set_common_name_fallback_used( | |
151 result.common_name_fallback_used); | |
152 return true; | |
153 } | |
154 | |
155 // Deserializes |cached_result| using the certificate database provided in | |
156 // |deserialized_certs|. Returns the parsed net::CertVerifyResult on success, or | |
157 // nullptr if deserialization failed. | |
158 bool DeserializeCachedResult( | |
159 const cronet_pb::CertVerificationCachedResult& cached_result, | |
160 const DeserializedCertMap& deserialized_certs, | |
161 int* error, | |
162 net::CertVerifyResult* result) { | |
163 if (!cached_result.has_error() || !cached_result.has_result()) | |
164 return false; | |
165 | |
166 const cronet_pb::CertVerificationResult& cert_verification_result = | |
167 cached_result.result(); | |
168 if (!cert_verification_result.has_verified_cert() || | |
169 !cert_verification_result.has_cert_status()) { | |
170 return false; | |
171 } | |
172 | |
173 *error = cached_result.error(); | |
174 | |
175 result->verified_cert = DeserializeCertificate( | |
176 cert_verification_result.verified_cert(), deserialized_certs); | |
177 if (!result->verified_cert) | |
178 return false; | |
179 | |
180 for (int i = 0; i < cert_verification_result.public_key_hashes_size(); ++i) { | |
181 const std::string& public_key_hash = | |
182 cert_verification_result.public_key_hashes(i); | |
183 net::HashValue hash; | |
184 if (!hash.FromString(public_key_hash)) | |
185 return false; | |
186 result->public_key_hashes.push_back(hash); | |
187 } | |
188 result->cert_status = cert_verification_result.cert_status(); | |
189 result->has_md2 = cert_verification_result.has_md2(); | |
190 result->has_md4 = cert_verification_result.has_md4(); | |
191 result->has_md5 = cert_verification_result.has_md5(); | |
192 result->has_sha1 = cert_verification_result.has_sha1(); | |
193 result->has_sha1_leaf = cert_verification_result.has_sha1_leaf(); | |
194 result->is_issued_by_known_root = | |
195 cert_verification_result.is_issued_by_known_root(); | |
196 result->is_issued_by_additional_trust_anchor = | |
197 cert_verification_result.is_issued_by_additional_trust_anchor(); | |
198 result->common_name_fallback_used = | |
199 cert_verification_result.common_name_fallback_used(); | |
200 return true; | |
201 } | |
202 | |
203 // Serializes |params|, |error|, |verify_result| and |verification_time| into | |
204 // |cert_cache|, updating |serialized_certs| with the set of raw certificates | |
205 // that will be needed to deserialize the certificate in |cert_cache| via | |
206 // DeserializeCertificate(). | |
207 bool SerializeCachedEntry(const net::CachingCertVerifier::RequestParams& params, | |
208 int error, | |
209 const net::CertVerifyResult& verify_result, | |
210 base::Time verification_time, | |
211 cronet_pb::CertVerificationCache* cert_cache, | |
212 SerializedCertMap* serialized_certs) { | |
213 cronet_pb::CertVerificationCacheEntry* cache_entry = | |
214 cert_cache->add_cache_entry(); | |
215 | |
216 cronet_pb::CertVerificationRequestParams* request_params = | |
217 cache_entry->mutable_request_params(); | |
218 if (!SerializeRequestParams(params, serialized_certs, request_params)) | |
219 return false; | |
220 | |
221 cronet_pb::CertVerificationCachedResult* cached_result = | |
222 cache_entry->mutable_cached_result(); | |
223 if (!SerializeCachedResult(verify_result, serialized_certs, cached_result)) | |
224 return false; | |
225 cached_result->set_error(error); | |
226 | |
227 cache_entry->set_verification_time(verification_time.ToInternalValue()); | |
228 return true; | |
229 } | |
230 | |
231 class CacheVisitor : public net::CachingCertVerifier::CacheVisitor { | |
232 public: | |
233 CacheVisitor() : failed_to_serialize_(false) {} | |
234 ~CacheVisitor() override {} | |
235 | |
236 bool VisitEntry(const net::CachingCertVerifier::RequestParams& params, | |
237 int error, | |
238 const net::CertVerifyResult& verify_result, | |
239 base::Time verification_time, | |
240 base::Time expiration_time) override { | |
241 if (!SerializeCachedEntry(params, error, verify_result, verification_time, | |
242 &cert_cache_, &serialized_certs_)) { | |
243 Reset(); | |
244 return false; | |
245 } | |
246 return true; | |
247 } | |
248 | |
249 void Reset() { | |
250 cert_cache_ = cronet_pb::CertVerificationCache(); | |
251 failed_to_serialize_ = true; | |
252 } | |
253 | |
254 void SerializeCerts() { | |
255 for (auto cert : serialized_certs_) { | |
256 cronet_pb::CertVerificationCertificateData* cert_entry = | |
257 cert_cache_.add_cert_entry(); | |
258 cert_entry->set_cert(cert.first); | |
259 cert_entry->set_cert_number(cert.second); | |
260 } | |
261 } | |
262 | |
263 const cronet_pb::CertVerificationCache& cert_cache() const { | |
264 return cert_cache_; | |
265 } | |
266 | |
267 bool failed_to_serialize() const { return failed_to_serialize_; } | |
268 | |
269 cronet_pb::CertVerificationCache cert_cache_; | |
270 SerializedCertMap serialized_certs_; | |
271 bool failed_to_serialize_; | |
272 }; | |
273 | |
274 struct CertVerifierCacheEntry { | |
275 CertVerifierCacheEntry(const net::CertVerifier::RequestParams& params, | |
276 int error, | |
277 const net::CertVerifyResult& result, | |
278 base::Time verification_time) | |
279 : params(params), | |
280 error(error), | |
281 result(result), | |
282 verification_time(verification_time) {} | |
283 | |
284 net::CertVerifier::RequestParams params; | |
285 int error; | |
286 net::CertVerifyResult result; | |
287 base::Time verification_time; | |
288 }; | |
289 | |
290 } // namespace | |
291 | |
292 cronet_pb::CertVerificationCache SerializeCertVerifierCache( | |
293 const net::CachingCertVerifier& verifier) { | |
294 CacheVisitor visitor; | |
295 verifier.VisitEntries(&visitor); | |
296 | |
297 if (!visitor.failed_to_serialize()) | |
298 visitor.SerializeCerts(); | |
299 return visitor.cert_cache(); | |
300 } | |
301 | |
302 bool DeserializeCertVerifierCache( | |
303 const cronet_pb::CertVerificationCache& cert_cache, | |
304 net::CachingCertVerifier* verifier) { | |
305 DeserializedCertMap deserialized_certs; | |
306 | |
307 if (cert_cache.cert_entry_size() == 0u || | |
308 cert_cache.cache_entry_size() == 0u) { | |
309 return false; | |
310 } | |
311 | |
312 // Build |deserialized_certs|'s certificate map. | |
313 for (int i = 0; i < cert_cache.cert_entry_size(); ++i) { | |
314 const cronet_pb::CertVerificationCertificateData& cert_entry = | |
315 cert_cache.cert_entry(i); | |
316 if (!cert_entry.has_cert() || !cert_entry.has_cert_number()) | |
317 return false; | |
318 deserialized_certs.insert({cert_entry.cert_number(), cert_entry.cert()}); | |
319 } | |
320 | |
321 std::vector<std::unique_ptr<CertVerifierCacheEntry>> | |
322 cert_verifier_cache_entries; | |
323 for (int i = 0; i < cert_cache.cache_entry_size(); ++i) { | |
324 const cronet_pb::CertVerificationCacheEntry& cache_entry = | |
325 cert_cache.cache_entry(i); | |
326 | |
327 // Verify |cache_entry|'s data. | |
328 if (!cache_entry.has_request_params() || | |
329 !cache_entry.has_verification_time() || | |
330 !cache_entry.has_cached_result()) { | |
331 return false; | |
332 } | |
333 | |
334 const cronet_pb::CertVerificationRequestParams& request_params = | |
335 cache_entry.request_params(); | |
336 | |
337 // Verify |request_params|'s data. | |
338 if (!request_params.has_certificate() || !request_params.has_hostname() || | |
339 request_params.hostname().empty() || !request_params.has_flags() || | |
340 !request_params.has_ocsp_response()) { | |
341 return false; | |
342 } | |
343 | |
344 // Deserialize |request_params|'s certificate using the certificate | |
345 // database provided in |deserialized_certs|. | |
346 scoped_refptr<net::X509Certificate> certificate = DeserializeCertificate( | |
347 request_params.certificate(), deserialized_certs); | |
348 if (!certificate) | |
349 return false; | |
350 | |
351 // Deserialize |request_params|'s trust anchor certificates using the | |
352 // certificate database provided in |deserialized_certs|. | |
353 net::CertificateList additional_trust_anchors; | |
354 for (int i = 0; i < request_params.additional_trust_anchors_size(); ++i) { | |
355 const cronet_pb::CertVerificationCertificate& certificate = | |
356 request_params.additional_trust_anchors(i); | |
357 scoped_refptr<net::X509Certificate> cert = | |
358 DeserializeCertificate(certificate, deserialized_certs); | |
359 if (!cert) | |
360 return false; | |
361 additional_trust_anchors.push_back(cert); | |
362 } | |
363 | |
364 net::CertVerifier::RequestParams params( | |
365 certificate, request_params.hostname(), request_params.flags(), | |
366 request_params.ocsp_response(), additional_trust_anchors); | |
367 | |
368 // Deserialize |cached_result| into |result|. | |
369 const cronet_pb::CertVerificationCachedResult& cached_result = | |
370 cache_entry.cached_result(); | |
371 net::CertVerifyResult result; | |
372 int error; | |
373 if (!DeserializeCachedResult(cached_result, deserialized_certs, &error, | |
374 &result)) { | |
375 return false; | |
376 } | |
377 | |
378 base::Time verification_time = | |
379 base::Time::FromInternalValue(cache_entry.verification_time()); | |
380 if (verification_time.is_null() || verification_time >= base::Time::Now()) | |
381 return false; | |
382 | |
383 std::unique_ptr<CertVerifierCacheEntry> cert_verifier_cache_entry( | |
384 new CertVerifierCacheEntry(params, error, result, verification_time)); | |
385 cert_verifier_cache_entries.push_back(std::move(cert_verifier_cache_entry)); | |
386 } | |
387 | |
388 for (const auto& entry : cert_verifier_cache_entries) { | |
389 verifier->AddEntry(entry->params, entry->error, entry->result, | |
390 entry->verification_time); | |
391 } | |
392 return true; | |
393 } | |
394 | |
395 } // namespace cronet | |
OLD | NEW |