| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/x509_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
| 6 | 6 |
| 7 #include "base/numerics/safe_conversions.h" | 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "crypto/openssl_util.h" | 9 #include "crypto/openssl_util.h" |
| 10 #include "net/base/ip_address.h" | 10 #include "net/base/ip_address.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 base::Time::Exploded exploded = {0}; | 35 base::Time::Exploded exploded = {0}; |
| 36 exploded.year = generalized.year; | 36 exploded.year = generalized.year; |
| 37 exploded.month = generalized.month; | 37 exploded.month = generalized.month; |
| 38 exploded.day_of_month = generalized.day; | 38 exploded.day_of_month = generalized.day; |
| 39 exploded.hour = generalized.hours; | 39 exploded.hour = generalized.hours; |
| 40 exploded.minute = generalized.minutes; | 40 exploded.minute = generalized.minutes; |
| 41 exploded.second = generalized.seconds; | 41 exploded.second = generalized.seconds; |
| 42 return base::Time::FromUTCExploded(exploded, result); | 42 return base::Time::FromUTCExploded(exploded, result); |
| 43 } | 43 } |
| 44 | 44 |
| 45 ParseCertificateOptions DefaultParseCertificateOptions() { |
| 46 ParseCertificateOptions options; |
| 47 options.allow_invalid_serial_numbers = true; |
| 48 return options; |
| 49 } |
| 50 |
| 45 // Sets |value| to the Value from a DER Sequence Tag-Length-Value and return | 51 // Sets |value| to the Value from a DER Sequence Tag-Length-Value and return |
| 46 // true, or return false if the TLV was not a valid DER Sequence. | 52 // true, or return false if the TLV was not a valid DER Sequence. |
| 47 WARN_UNUSED_RESULT bool GetSequenceValue(const der::Input& tlv, | 53 WARN_UNUSED_RESULT bool GetSequenceValue(const der::Input& tlv, |
| 48 der::Input* value) { | 54 der::Input* value) { |
| 49 der::Parser parser(tlv); | 55 der::Parser parser(tlv); |
| 50 return parser.ReadTag(der::kSequence, value) && !parser.HasMore(); | 56 return parser.ReadTag(der::kSequence, value) && !parser.HasMore(); |
| 51 } | 57 } |
| 52 | 58 |
| 53 // Normalize |cert|'s Issuer and store it in |out_normalized_issuer|, returning | 59 // Normalize |cert|'s Issuer and store it in |out_normalized_issuer|, returning |
| 54 // true on success or false if there was a parsing error. | 60 // true on success or false if there was a parsing error. |
| 55 bool GetNormalizedCertIssuer(CRYPTO_BUFFER* cert, | 61 bool GetNormalizedCertIssuer(CRYPTO_BUFFER* cert, |
| 56 std::string* out_normalized_issuer) { | 62 std::string* out_normalized_issuer) { |
| 57 der::Input tbs_certificate_tlv; | 63 der::Input tbs_certificate_tlv; |
| 58 der::Input signature_algorithm_tlv; | 64 der::Input signature_algorithm_tlv; |
| 59 der::BitString signature_value; | 65 der::BitString signature_value; |
| 60 if (!ParseCertificate( | 66 if (!ParseCertificate( |
| 61 der::Input(CRYPTO_BUFFER_data(cert), CRYPTO_BUFFER_len(cert)), | 67 der::Input(CRYPTO_BUFFER_data(cert), CRYPTO_BUFFER_len(cert)), |
| 62 &tbs_certificate_tlv, &signature_algorithm_tlv, &signature_value, | 68 &tbs_certificate_tlv, &signature_algorithm_tlv, &signature_value, |
| 63 nullptr)) { | 69 nullptr)) { |
| 64 return false; | 70 return false; |
| 65 } | 71 } |
| 66 ParsedTbsCertificate tbs; | 72 ParsedTbsCertificate tbs; |
| 67 if (!ParseTbsCertificate(tbs_certificate_tlv, {}, &tbs, nullptr)) | 73 if (!ParseTbsCertificate(tbs_certificate_tlv, |
| 74 DefaultParseCertificateOptions(), &tbs, nullptr)) |
| 68 return false; | 75 return false; |
| 69 | 76 |
| 70 der::Input issuer_value; | 77 der::Input issuer_value; |
| 71 if (!GetSequenceValue(tbs.issuer_tlv, &issuer_value)) | 78 if (!GetSequenceValue(tbs.issuer_tlv, &issuer_value)) |
| 72 return false; | 79 return false; |
| 73 | 80 |
| 74 return NormalizeName(issuer_value, out_normalized_issuer); | 81 return NormalizeName(issuer_value, out_normalized_issuer); |
| 75 } | 82 } |
| 76 | 83 |
| 77 // Fills |principal| from the DER encoded |name_tlv|, returning true on success | 84 // Fills |principal| from the DER encoded |name_tlv|, returning true on success |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 der::BitString signature_value; | 168 der::BitString signature_value; |
| 162 | 169 |
| 163 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle_), | 170 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle_), |
| 164 CRYPTO_BUFFER_len(cert_handle_)), | 171 CRYPTO_BUFFER_len(cert_handle_)), |
| 165 &tbs_certificate_tlv, &signature_algorithm_tlv, | 172 &tbs_certificate_tlv, &signature_algorithm_tlv, |
| 166 &signature_value, nullptr)) { | 173 &signature_value, nullptr)) { |
| 167 return false; | 174 return false; |
| 168 } | 175 } |
| 169 | 176 |
| 170 ParsedTbsCertificate tbs; | 177 ParsedTbsCertificate tbs; |
| 171 if (!ParseTbsCertificate(tbs_certificate_tlv, {}, &tbs, nullptr)) | 178 if (!ParseTbsCertificate(tbs_certificate_tlv, |
| 179 DefaultParseCertificateOptions(), &tbs, nullptr)) |
| 172 return false; | 180 return false; |
| 173 | 181 |
| 174 if (!ParsePrincipal(tbs.subject_tlv, &subject_) || | 182 if (!ParsePrincipal(tbs.subject_tlv, &subject_) || |
| 175 !ParsePrincipal(tbs.issuer_tlv, &issuer_)) { | 183 !ParsePrincipal(tbs.issuer_tlv, &issuer_)) { |
| 176 return false; | 184 return false; |
| 177 } | 185 } |
| 178 | 186 |
| 179 if (!GeneralizedTimeToBaseTime(tbs.validity_not_before, &valid_start_) || | 187 if (!GeneralizedTimeToBaseTime(tbs.validity_not_before, &valid_start_) || |
| 180 !GeneralizedTimeToBaseTime(tbs.validity_not_after, &valid_expiry_)) { | 188 !GeneralizedTimeToBaseTime(tbs.validity_not_after, &valid_expiry_)) { |
| 181 return false; | 189 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 196 der::Input signature_algorithm_tlv; | 204 der::Input signature_algorithm_tlv; |
| 197 der::BitString signature_value; | 205 der::BitString signature_value; |
| 198 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle_), | 206 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle_), |
| 199 CRYPTO_BUFFER_len(cert_handle_)), | 207 CRYPTO_BUFFER_len(cert_handle_)), |
| 200 &tbs_certificate_tlv, &signature_algorithm_tlv, | 208 &tbs_certificate_tlv, &signature_algorithm_tlv, |
| 201 &signature_value, nullptr)) { | 209 &signature_value, nullptr)) { |
| 202 return false; | 210 return false; |
| 203 } | 211 } |
| 204 | 212 |
| 205 ParsedTbsCertificate tbs; | 213 ParsedTbsCertificate tbs; |
| 206 if (!ParseTbsCertificate(tbs_certificate_tlv, {}, &tbs, nullptr)) | 214 if (!ParseTbsCertificate(tbs_certificate_tlv, |
| 215 DefaultParseCertificateOptions(), &tbs, nullptr)) |
| 207 return false; | 216 return false; |
| 208 if (!tbs.has_extensions) | 217 if (!tbs.has_extensions) |
| 209 return false; | 218 return false; |
| 210 | 219 |
| 211 std::map<der::Input, ParsedExtension> extensions; | 220 std::map<der::Input, ParsedExtension> extensions; |
| 212 if (!ParseExtensions(tbs.extensions_tlv, &extensions)) | 221 if (!ParseExtensions(tbs.extensions_tlv, &extensions)) |
| 213 return false; | 222 return false; |
| 214 | 223 |
| 215 ParsedExtension subject_alt_names_extension; | 224 ParsedExtension subject_alt_names_extension; |
| 216 if (!ConsumeExtension(SubjectAltNameOid(), &extensions, | 225 if (!ConsumeExtension(SubjectAltNameOid(), &extensions, |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 der::Input tbs_certificate_tlv; | 429 der::Input tbs_certificate_tlv; |
| 421 der::Input signature_algorithm_tlv; | 430 der::Input signature_algorithm_tlv; |
| 422 der::BitString signature_value; | 431 der::BitString signature_value; |
| 423 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle), | 432 if (!ParseCertificate(der::Input(CRYPTO_BUFFER_data(cert_handle), |
| 424 CRYPTO_BUFFER_len(cert_handle)), | 433 CRYPTO_BUFFER_len(cert_handle)), |
| 425 &tbs_certificate_tlv, &signature_algorithm_tlv, | 434 &tbs_certificate_tlv, &signature_algorithm_tlv, |
| 426 &signature_value, nullptr)) { | 435 &signature_value, nullptr)) { |
| 427 return false; | 436 return false; |
| 428 } | 437 } |
| 429 ParsedTbsCertificate tbs; | 438 ParsedTbsCertificate tbs; |
| 430 if (!ParseTbsCertificate(tbs_certificate_tlv, {}, &tbs, nullptr)) { | 439 if (!ParseTbsCertificate(tbs_certificate_tlv, |
| 440 DefaultParseCertificateOptions(), &tbs, nullptr)) { |
| 431 return false; | 441 return false; |
| 432 } | 442 } |
| 433 | 443 |
| 434 der::Input subject_value; | 444 der::Input subject_value; |
| 435 std::string normalized_subject; | 445 std::string normalized_subject; |
| 436 if (!GetSequenceValue(tbs.subject_tlv, &subject_value) || | 446 if (!GetSequenceValue(tbs.subject_tlv, &subject_value) || |
| 437 !NormalizeName(subject_value, &normalized_subject)) { | 447 !NormalizeName(subject_value, &normalized_subject)) { |
| 438 return false; | 448 return false; |
| 439 } | 449 } |
| 440 der::Input issuer_value; | 450 der::Input issuer_value; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 482 |
| 473 // static | 483 // static |
| 474 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, | 484 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, |
| 475 base::Pickle* pickle) { | 485 base::Pickle* pickle) { |
| 476 return pickle->WriteData( | 486 return pickle->WriteData( |
| 477 reinterpret_cast<const char*>(CRYPTO_BUFFER_data(cert_handle)), | 487 reinterpret_cast<const char*>(CRYPTO_BUFFER_data(cert_handle)), |
| 478 CRYPTO_BUFFER_len(cert_handle)); | 488 CRYPTO_BUFFER_len(cert_handle)); |
| 479 } | 489 } |
| 480 | 490 |
| 481 } // namespace net | 491 } // namespace net |
| OLD | NEW |