Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(567)

Side by Side Diff: net/cert/cert_verify_proc.cc

Issue 2100303002: Add OCSPVerifyResult for tracking stapled OCSP responses cross-platform. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ocsp-date-check
Patch Set: Optional was optional. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/cert/cert_verify_proc.h" 5 #include "net/cert/cert_verify_proc.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/sha1.h" 13 #include "base/sha1.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #include "net/base/url_util.h" 20 #include "net/base/url_util.h"
21 #include "net/cert/cert_status_flags.h" 21 #include "net/cert/cert_status_flags.h"
22 #include "net/cert/cert_verifier.h" 22 #include "net/cert/cert_verifier.h"
23 #include "net/cert/cert_verify_proc_whitelist.h" 23 #include "net/cert/cert_verify_proc_whitelist.h"
24 #include "net/cert/cert_verify_result.h" 24 #include "net/cert/cert_verify_result.h"
25 #include "net/cert/crl_set.h" 25 #include "net/cert/crl_set.h"
26 #include "net/cert/internal/parse_ocsp.h"
27 #include "net/cert/ocsp_revocation_status.h"
26 #include "net/cert/x509_certificate.h" 28 #include "net/cert/x509_certificate.h"
29 #include "net/der/encode_values.h"
27 #include "url/url_canon.h" 30 #include "url/url_canon.h"
28 31
29 #if defined(USE_NSS_CERTS) 32 #if defined(USE_NSS_CERTS)
30 #include "net/cert/cert_verify_proc_nss.h" 33 #include "net/cert/cert_verify_proc_nss.h"
31 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) 34 #elif defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
32 #include "net/cert/cert_verify_proc_openssl.h" 35 #include "net/cert/cert_verify_proc_openssl.h"
33 #elif defined(OS_ANDROID) 36 #elif defined(OS_ANDROID)
34 #include "net/cert/cert_verify_proc_android.h" 37 #include "net/cert/cert_verify_proc_android.h"
35 #elif defined(OS_IOS) 38 #elif defined(OS_IOS)
36 #include "net/cert/cert_verify_proc_ios.h" 39 #include "net/cert/cert_verify_proc_ios.h"
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 bool IsPastSHA1DeprecationDate(const X509Certificate& cert) { 178 bool IsPastSHA1DeprecationDate(const X509Certificate& cert) {
176 const base::Time& start = cert.valid_start(); 179 const base::Time& start = cert.valid_start();
177 if (start.is_max() || start.is_null()) 180 if (start.is_max() || start.is_null())
178 return true; 181 return true;
179 // 2016-01-01 00:00:00 UTC. 182 // 2016-01-01 00:00:00 UTC.
180 const base::Time kSHA1DeprecationDate = 183 const base::Time kSHA1DeprecationDate =
181 base::Time::FromInternalValue(INT64_C(13096080000000000)); 184 base::Time::FromInternalValue(INT64_C(13096080000000000));
182 return start >= kSHA1DeprecationDate; 185 return start >= kSHA1DeprecationDate;
183 } 186 }
184 187
188 // Checks if the given RFC 6960 OCSPCertID structure |cert_id| has the same
189 // serial number as |certificate|.
190 //
191 // TODO(dadrian): Verify name and key hashes. https://crbug.com/620005
192 bool CheckCertIDMatchesCertificate(const OCSPCertID& cert_id,
193 const X509Certificate& certificate) {
194 der::Input serial(&certificate.serial_number());
195 return cert_id.serial_number == serial;
196 }
197
198 // Populates |ocsp_result| with revocation information for |certificate|, based
199 // on the unparsed OCSP response in |raw_response|.
200 void CheckOCSP(const std::string& raw_response,
201 const X509Certificate& certificate,
202 OCSPVerifyResult* ocsp_result) {
203 // The maximum age for an OCSP response, implemented as time since the
204 // |this_update| field in OCSPSingleREsponse. Responses older than |max_age|
205 // will be considered invalid.
206 static base::TimeDelta max_age = base::TimeDelta::FromDays(7);
207 *ocsp_result = OCSPVerifyResult();
208
209 if (raw_response.empty()) {
210 ocsp_result->response_status = OCSPVerifyResult::MISSING;
211 return;
212 }
213
214 der::Input response_der(&raw_response);
215 OCSPResponse response;
216 if (!ParseOCSPResponse(response_der, &response)) {
217 ocsp_result->response_status = OCSPVerifyResult::PARSE_RESPONSE_ERROR;
218 return;
219 }
220
221 // RFC 6960 defines all responses |response_status| != SUCCESSFUL as error
222 // responses. No revocation information is provided on error responses, and
223 // the OCSPResponseData structure is not set.
224 if (response.status != OCSPResponse::ResponseStatus::SUCCESSFUL) {
225 ocsp_result->response_status = OCSPVerifyResult::ERROR_RESPONSE;
226 return;
227 }
228
229 // Actual revocation information is contained within the BasicOCSPResponse as
230 // a ResponseData structure. The BasicOCSPResponse was parsed above, and
231 // contains an unparsed ResponseData. From RFC 6960:
232 //
233 // BasicOCSPResponse ::= SEQUENCE {
234 // tbsResponseData ResponseData,
235 // signatureAlgorithm AlgorithmIdentifier,
236 // signature BIT STRING,
237 // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
238 //
239 // ResponseData ::= SEQUENCE {
240 // version [0] EXPLICIT Version DEFAULT v1,
241 // responderID ResponderID,
242 // producedAt GeneralizedTime,
243 // responses SEQUENCE OF SingleResponse,
244 // responseExtensions [1] EXPLICIT Extensions OPTIONAL }
245 OCSPResponseData response_data;
246 if (!ParseOCSPResponseData(response.data, &response_data)) {
247 ocsp_result->response_status = OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR;
248 return;
249 }
250
251 // If producedAt is outside of the certificate validity period, reject the
252 // response.
253 der::GeneralizedTime not_before, not_after;
254 if (!der::EncodeTimeAsGeneralizedTime(certificate.valid_start(),
255 &not_before) ||
256 !der::EncodeTimeAsGeneralizedTime(certificate.valid_expiry(),
257 &not_after)) {
258 ocsp_result->response_status = OCSPVerifyResult::BAD_PRODUCED_AT;
259 return;
260 }
261 if (response_data.produced_at < not_before ||
262 response_data.produced_at > not_after) {
263 ocsp_result->response_status = OCSPVerifyResult::BAD_PRODUCED_AT;
264 return;
265 }
266
267 // TODO(svaldez): Unify with GetOCSPCertStatus. https://crbug.com/629249
268 base::Time verify_time = base::Time::Now();
269 ocsp_result->response_status = OCSPVerifyResult::NO_MATCHING_RESPONSE;
270 for (const auto& single_response_der : response_data.responses) {
271 // In the common case, there should only be one SingleResponse in the
272 // ResponseData (matching the certificate requested and used on this
273 // connection). However, it is possible for the OCSP responder to provide
274 // multiple responses for multiple certificates. Look through all the
275 // provided SingleResponses, and check to see if any match the certificate.
276 // A SingleResponse matches a certificate if it has the same serial number.
277 OCSPSingleResponse single_response;
278 if (!ParseOCSPSingleResponse(single_response_der, &single_response))
279 continue;
280 OCSPCertID cert_id;
281 if (!ParseOCSPCertID(single_response.cert_id_tlv, &cert_id))
282 continue;
283 if (!CheckCertIDMatchesCertificate(cert_id, certificate))
284 continue;
285 // The SingleResponse matches the certificate, but may be out of date. Out
Ryan Sleevi 2016/07/18 22:56:37 s/ / / (single space)
dadrian 2016/07/18 23:20:26 Done. Vim auto-line-breaker has a love of extra s
286 // of date responses are matching responses are noted seperate from
Ryan Sleevi 2016/07/18 22:56:37 grammar: are matching response are ?
dadrian 2016/07/18 23:20:26 Done.
287 // responses with mismatched serial numbers. If an OCSP responder provides
288 // both an up to date response and an expired response, the up to date
289 // response takes precedence (PROVIDED > INVALID_DATE).
290 if (!CheckOCSPDateValid(single_response, verify_time, max_age)) {
291 if (ocsp_result->response_status != OCSPVerifyResult::PROVIDED)
292 ocsp_result->response_status = OCSPVerifyResult::INVALID_DATE;
293 continue;
294 }
295
296 // In the case with multiple matching and up to date responses, keep only
297 // the strictest status (REVOKED > UNKNOWN > GOOD). The current
298 // |revocation_status| is only valid if |response_status| is already set to
299 // PROVIDED.
300 OCSPRevocationStatus current_status = OCSPRevocationStatus::GOOD;
301 if (ocsp_result->response_status == OCSPVerifyResult::PROVIDED) {
302 current_status = ocsp_result->revocation_status;
303 }
304 if (current_status == OCSPRevocationStatus::GOOD ||
305 single_response.cert_status.status == OCSPRevocationStatus::REVOKED) {
306 ocsp_result->revocation_status = single_response.cert_status.status;
307 }
308 ocsp_result->response_status = OCSPVerifyResult::PROVIDED;
309 }
310 }
311
185 // Comparison functor used for binary searching whether a given HashValue, 312 // Comparison functor used for binary searching whether a given HashValue,
186 // which MUST be a SHA-256 hash, is contained with an array of SHA-256 313 // which MUST be a SHA-256 hash, is contained with an array of SHA-256
187 // hashes. 314 // hashes.
188 struct HashToArrayComparator { 315 struct HashToArrayComparator {
189 template <size_t N> 316 template <size_t N>
190 bool operator()(const uint8_t(&lhs)[N], const HashValue& rhs) const { 317 bool operator()(const uint8_t(&lhs)[N], const HashValue& rhs) const {
191 static_assert(N == crypto::kSHA256Length, 318 static_assert(N == crypto::kSHA256Length,
192 "Only SHA-256 hashes are supported"); 319 "Only SHA-256 hashes are supported");
193 return memcmp(lhs, rhs.data(), crypto::kSHA256Length) < 0; 320 return memcmp(lhs, rhs.data(), crypto::kSHA256Length) < 0;
194 } 321 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 int rv = VerifyInternal(cert, hostname, ocsp_response, flags, crl_set, 378 int rv = VerifyInternal(cert, hostname, ocsp_response, flags, crl_set,
252 additional_trust_anchors, verify_result); 379 additional_trust_anchors, verify_result);
253 380
254 UMA_HISTOGRAM_BOOLEAN("Net.CertCommonNameFallback", 381 UMA_HISTOGRAM_BOOLEAN("Net.CertCommonNameFallback",
255 verify_result->common_name_fallback_used); 382 verify_result->common_name_fallback_used);
256 if (!verify_result->is_issued_by_known_root) { 383 if (!verify_result->is_issued_by_known_root) {
257 UMA_HISTOGRAM_BOOLEAN("Net.CertCommonNameFallbackPrivateCA", 384 UMA_HISTOGRAM_BOOLEAN("Net.CertCommonNameFallbackPrivateCA",
258 verify_result->common_name_fallback_used); 385 verify_result->common_name_fallback_used);
259 } 386 }
260 387
388 CheckOCSP(ocsp_response, *verify_result->verified_cert,
389 &verify_result->ocsp_result);
390
261 // This check is done after VerifyInternal so that VerifyInternal can fill 391 // This check is done after VerifyInternal so that VerifyInternal can fill
262 // in the list of public key hashes. 392 // in the list of public key hashes.
263 if (IsPublicKeyBlacklisted(verify_result->public_key_hashes)) { 393 if (IsPublicKeyBlacklisted(verify_result->public_key_hashes)) {
264 verify_result->cert_status |= CERT_STATUS_REVOKED; 394 verify_result->cert_status |= CERT_STATUS_REVOKED;
265 rv = MapCertStatusToNetError(verify_result->cert_status); 395 rv = MapCertStatusToNetError(verify_result->cert_status);
266 } 396 }
267 397
268 std::vector<std::string> dns_names, ip_addrs; 398 std::vector<std::string> dns_names, ip_addrs;
269 cert->GetSubjectAltName(&dns_names, &ip_addrs); 399 cert->GetSubjectAltName(&dns_names, &ip_addrs);
270 if (HasNameConstraintsViolation(verify_result->public_key_hashes, 400 if (HasNameConstraintsViolation(verify_result->public_key_hashes,
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 return true; 706 return true;
577 707
578 // For certificates issued after 1 April 2015: 39 months. 708 // For certificates issued after 1 April 2015: 39 months.
579 if (start >= time_2015_04_01 && month_diff > 39) 709 if (start >= time_2015_04_01 && month_diff > 39)
580 return true; 710 return true;
581 711
582 return false; 712 return false;
583 } 713 }
584 714
585 } // namespace net 715 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698