| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/x509_openssl_util.h" | 5 #include "net/base/x509_openssl_util.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 10 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 11 #include "base/time.h" | 10 #include "net/base/x509_cert_types.h" |
| 12 | 11 |
| 13 namespace net { | 12 namespace net { |
| 14 | 13 |
| 15 namespace x509_openssl_util { | 14 namespace x509_openssl_util { |
| 16 | 15 |
| 17 namespace { | |
| 18 | |
| 19 // Helper for ParseDate. |*field| must contain at least |field_len| characters. | |
| 20 // |*field| will be advanced by |field_len| on exit. |*ok| is set to false if | |
| 21 // there is an error in parsing the number, but left untouched otherwise. | |
| 22 // Returns the parsed integer. | |
| 23 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { | |
| 24 int result = 0; | |
| 25 *ok &= base::StringToInt(*field, *field + field_len, &result); | |
| 26 *field += field_len; | |
| 27 return result; | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 bool ParsePrincipalKeyAndValueByIndex(X509_NAME* name, | 16 bool ParsePrincipalKeyAndValueByIndex(X509_NAME* name, |
| 33 int index, | 17 int index, |
| 34 std::string* key, | 18 std::string* key, |
| 35 std::string* value) { | 19 std::string* value) { |
| 36 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, index); | 20 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, index); |
| 37 if (!entry) | 21 if (!entry) |
| 38 return false; | 22 return false; |
| 39 | 23 |
| 40 if (key) { | 24 if (key) { |
| 41 ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(entry); | 25 ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(entry); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 71 } | 55 } |
| 72 | 56 |
| 73 bool ParseDate(ASN1_TIME* x509_time, base::Time* time) { | 57 bool ParseDate(ASN1_TIME* x509_time, base::Time* time) { |
| 74 if (!x509_time || | 58 if (!x509_time || |
| 75 (x509_time->type != V_ASN1_UTCTIME && | 59 (x509_time->type != V_ASN1_UTCTIME && |
| 76 x509_time->type != V_ASN1_GENERALIZEDTIME)) | 60 x509_time->type != V_ASN1_GENERALIZEDTIME)) |
| 77 return false; | 61 return false; |
| 78 | 62 |
| 79 base::StringPiece str_date(reinterpret_cast<const char*>(x509_time->data), | 63 base::StringPiece str_date(reinterpret_cast<const char*>(x509_time->data), |
| 80 x509_time->length); | 64 x509_time->length); |
| 81 // UTCTime: YYMMDDHHMMSSZ | |
| 82 // GeneralizedTime: YYYYMMDDHHMMSSZ | |
| 83 size_t year_length = x509_time->type == V_ASN1_UTCTIME ? 2 : 4; | |
| 84 | 65 |
| 85 if (str_date.length() < 11 + year_length) | 66 CertDateFormat format = x509_time->type == V_ASN1_UTCTIME ? |
| 86 return false; | 67 CERT_DATE_FORMAT_UTC_TIME : CERT_DATE_FORMAT_GENERALIZED_TIME; |
| 87 | 68 return ParseCertificateDate(str_date, format, time); |
| 88 const char* field = str_date.data(); | |
| 89 bool valid = true; | |
| 90 base::Time::Exploded exploded = {0}; | |
| 91 | |
| 92 exploded.year = ParseIntAndAdvance(&field, year_length, &valid); | |
| 93 exploded.month = ParseIntAndAdvance(&field, 2, &valid); | |
| 94 exploded.day_of_month = ParseIntAndAdvance(&field, 2, &valid); | |
| 95 exploded.hour = ParseIntAndAdvance(&field, 2, &valid); | |
| 96 exploded.minute = ParseIntAndAdvance(&field, 2, &valid); | |
| 97 exploded.second = ParseIntAndAdvance(&field, 2, &valid); | |
| 98 if (valid && year_length == 2) | |
| 99 exploded.year += exploded.year < 50 ? 2000 : 1900; | |
| 100 | |
| 101 valid &= exploded.HasValidValues(); | |
| 102 | |
| 103 if (!valid) { | |
| 104 NOTREACHED() << "can't parse x509 date " << str_date; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 *time = base::Time::FromUTCExploded(exploded); | |
| 109 return true; | |
| 110 } | 69 } |
| 111 | 70 |
| 112 // TODO(joth): Investigate if we can upstream this into the OpenSSL library, | 71 // TODO(joth): Investigate if we can upstream this into the OpenSSL library, |
| 113 // to avoid duplicating this logic across projects. | 72 // to avoid duplicating this logic across projects. |
| 114 bool VerifyHostname(const std::string& hostname, | 73 bool VerifyHostname(const std::string& hostname, |
| 115 const std::vector<std::string>& cert_names) { | 74 const std::vector<std::string>& cert_names) { |
| 116 DCHECK(!hostname.empty()); | 75 DCHECK(!hostname.empty()); |
| 117 | 76 |
| 118 // Simple host name validation. A valid domain name must only contain | 77 // Simple host name validation. A valid domain name must only contain |
| 119 // alpha, digits, hyphens, and dots. An IP address may have digits and dots, | 78 // alpha, digits, hyphens, and dots. An IP address may have digits and dots, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 // detect these and skip those candidate cert names. | 177 // detect these and skip those candidate cert names. |
| 219 if (cert_match == wildcard_domain) | 178 if (cert_match == wildcard_domain) |
| 220 return true; | 179 return true; |
| 221 } | 180 } |
| 222 DVLOG(1) << "Could not find any match for " << hostname | 181 DVLOG(1) << "Could not find any match for " << hostname |
| 223 << " (canonicalized as " << reference_name | 182 << " (canonicalized as " << reference_name |
| 224 << ") in cert names " << JoinString(cert_names, '|'); | 183 << ") in cert names " << JoinString(cert_names, '|'); |
| 225 return false; | 184 return false; |
| 226 } | 185 } |
| 227 | 186 |
| 228 } // namespace x509_openssl_util | 187 } // namespace x509_openssl_util |
| 229 | 188 |
| 230 } // namespace net | 189 } // namespace net |
| OLD | NEW |