Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/time.h" | 9 #include "net/base/x509_cert_types.h" |
| 11 | 10 |
| 12 namespace net { | 11 namespace net { |
| 13 | 12 |
| 14 namespace x509_openssl_util { | 13 namespace x509_openssl_util { |
| 15 | 14 |
| 16 namespace { | |
| 17 | |
| 18 // Helper for ParseDate. |*field| must contain at least |field_len| characters. | |
| 19 // |*field| will be advanced by |field_len| on exit. |*ok| is set to false if | |
| 20 // there is an error in parsing the number, but left untouched otherwise. | |
| 21 // Returns the parsed integer. | |
| 22 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { | |
| 23 int result = 0; | |
| 24 *ok &= base::StringToInt(*field, *field + field_len, &result); | |
| 25 *field += field_len; | |
| 26 return result; | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 bool ParsePrincipalKeyAndValueByIndex(X509_NAME* name, | 15 bool ParsePrincipalKeyAndValueByIndex(X509_NAME* name, |
| 32 int index, | 16 int index, |
| 33 std::string* key, | 17 std::string* key, |
| 34 std::string* value) { | 18 std::string* value) { |
| 35 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, index); | 19 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, index); |
| 36 if (!entry) | 20 if (!entry) |
| 37 return false; | 21 return false; |
| 38 | 22 |
| 39 if (key) { | 23 if (key) { |
| 40 ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(entry); | 24 ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(entry); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 70 } | 54 } |
| 71 | 55 |
| 72 bool ParseDate(ASN1_TIME* x509_time, base::Time* time) { | 56 bool ParseDate(ASN1_TIME* x509_time, base::Time* time) { |
| 73 if (!x509_time || | 57 if (!x509_time || |
| 74 (x509_time->type != V_ASN1_UTCTIME && | 58 (x509_time->type != V_ASN1_UTCTIME && |
| 75 x509_time->type != V_ASN1_GENERALIZEDTIME)) | 59 x509_time->type != V_ASN1_GENERALIZEDTIME)) |
| 76 return false; | 60 return false; |
| 77 | 61 |
| 78 base::StringPiece str_date(reinterpret_cast<const char*>(x509_time->data), | 62 base::StringPiece str_date(reinterpret_cast<const char*>(x509_time->data), |
| 79 x509_time->length); | 63 x509_time->length); |
| 80 // UTCTime: YYMMDDHHMMSSZ | |
| 81 // GeneralizedTime: YYYYMMDDHHMMSSZ | |
| 82 size_t year_length = x509_time->type == V_ASN1_UTCTIME ? 2 : 4; | |
| 83 | 64 |
| 84 if (str_date.length() < 11 + year_length) | 65 CertificateDateFormat format = |
| 85 return false; | 66 x509_time->type == V_ASN1_UTCTIME ? CERT_DATE_FORMAT_UTC_TIME |
| 86 | 67 : CERT_DATE_FORMAT_GENERALIZED_TIME; |
|
wtc
2010/11/11 01:33:52
Nit: we have a convention to break an expression a
| |
| 87 const char* field = str_date.data(); | 68 return ParseCertificateDate(str_date, format, time); |
| 88 bool valid = true; | |
| 89 base::Time::Exploded exploded = {0}; | |
| 90 | |
| 91 exploded.year = ParseIntAndAdvance(&field, year_length, &valid); | |
| 92 exploded.month = ParseIntAndAdvance(&field, 2, &valid); | |
| 93 exploded.day_of_month = ParseIntAndAdvance(&field, 2, &valid); | |
| 94 exploded.hour = ParseIntAndAdvance(&field, 2, &valid); | |
| 95 exploded.minute = ParseIntAndAdvance(&field, 2, &valid); | |
| 96 exploded.second = ParseIntAndAdvance(&field, 2, &valid); | |
| 97 if (valid && year_length == 2) | |
| 98 exploded.year += exploded.year < 50 ? 2000 : 1900; | |
| 99 | |
| 100 valid &= exploded.HasValidValues(); | |
| 101 | |
| 102 if (!valid) { | |
| 103 NOTREACHED() << "can't parse x509 date " << str_date; | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 *time = base::Time::FromUTCExploded(exploded); | |
| 108 return true; | |
| 109 } | 69 } |
| 110 | 70 |
| 111 } // namespace x509_openssl_util | 71 } // namespace x509_openssl_util |
| 112 | 72 |
| 113 } // namespace net | 73 } // namespace net |
| OLD | NEW |