OLD | NEW |
---|---|
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/x509_util.h" | 5 #include "net/cert/x509_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "crypto/ec_private_key.h" | 10 #include "crypto/ec_private_key.h" |
11 #include "crypto/rsa_private_key.h" | 11 #include "crypto/rsa_private_key.h" |
12 #include "net/base/hash_value.h" | 12 #include "net/base/hash_value.h" |
13 #include "net/cert/internal/name_constraints.h" | |
13 #include "net/cert/internal/parse_certificate.h" | 14 #include "net/cert/internal/parse_certificate.h" |
15 #include "net/cert/internal/parse_name.h" | |
14 #include "net/cert/internal/signature_algorithm.h" | 16 #include "net/cert/internal/signature_algorithm.h" |
15 #include "net/cert/x509_certificate.h" | 17 #include "net/cert/x509_certificate.h" |
16 | 18 |
17 namespace net { | 19 namespace net { |
18 | 20 |
21 namespace { | |
22 | |
23 bool GetCommonName(const net::der::Input& tlv, std::string* common_name) { | |
davidben
2016/09/06 17:21:45
No need for net:: prefix since we're already in ne
jam
2016/09/06 17:43:24
Done.
| |
24 net::RDNSequence rdn_sequence; | |
25 if (!net::ParseName(tlv, &rdn_sequence)) | |
26 return false; | |
27 | |
28 for (const net::RelativeDistinguishedName& rdn : rdn_sequence) { | |
davidben
2016/09/06 17:21:45
Any reason to use auto some of the time and the ty
jam
2016/09/06 17:43:24
no reason, switched to auto
| |
29 for (const auto& atv : rdn) { | |
30 if (atv.type == net::TypeCommonNameOid()) { | |
31 return atv.ValueAsStringUnsafe(common_name); | |
32 } | |
33 } | |
34 } | |
35 return true; | |
36 } | |
37 | |
38 bool DecodeTime(const net::der::GeneralizedTime& generalized_time, | |
39 base::Time* time) { | |
40 base::Time::Exploded exploded = {0}; | |
41 exploded.year = generalized_time.year; | |
42 exploded.month = generalized_time.month; | |
43 exploded.day_of_month = generalized_time.day; | |
44 exploded.hour = generalized_time.hours; | |
45 exploded.minute = generalized_time.minutes; | |
46 exploded.second = generalized_time.seconds; | |
47 return base::Time::FromUTCExploded(exploded, time); | |
48 } | |
davidben
2016/09/06 17:21:45
Nit: newline
jam
2016/09/06 17:43:24
Done.
| |
49 } | |
davidben
2016/09/06 17:21:45
Nit: // namespace
jam
2016/09/06 17:43:24
Done.
| |
50 | |
19 namespace x509_util { | 51 namespace x509_util { |
20 | 52 |
21 // RSA keys created by CreateKeyAndSelfSignedCert will be of this length. | 53 // RSA keys created by CreateKeyAndSelfSignedCert will be of this length. |
22 static const uint16_t kRSAKeyLength = 1024; | 54 static const uint16_t kRSAKeyLength = 1024; |
23 | 55 |
24 // Certificates made by CreateKeyAndSelfSignedCert and | 56 // Certificates made by CreateKeyAndSelfSignedCert and |
25 // CreateKeyAndChannelIDEC will be signed using this digest algorithm. | 57 // CreateKeyAndChannelIDEC will be signed using this digest algorithm. |
26 static const DigestAlgorithm kSignatureDigestAlgorithm = DIGEST_SHA256; | 58 static const DigestAlgorithm kSignatureDigestAlgorithm = DIGEST_SHA256; |
27 | 59 |
28 ClientCertSorter::ClientCertSorter() : now_(base::Time::Now()) {} | 60 ClientCertSorter::ClientCertSorter() : now_(base::Time::Now()) {} |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 serial_number, | 107 serial_number, |
76 not_valid_before, | 108 not_valid_before, |
77 not_valid_after, | 109 not_valid_after, |
78 der_cert); | 110 der_cert); |
79 if (success) | 111 if (success) |
80 key->reset(new_key.release()); | 112 key->reset(new_key.release()); |
81 | 113 |
82 return success; | 114 return success; |
83 } | 115 } |
84 | 116 |
117 bool ParseCertificateSandboxed(const base::StringPiece& certificate, | |
118 std::string* subject, | |
119 std::string* issuer, | |
120 base::Time* not_before, | |
121 base::Time* not_after, | |
122 std::vector<std::string>* dns_names, | |
123 std::vector<std::string>* ip_addresses) { | |
124 net::der::Input cert_data(certificate); | |
davidben
2016/09/06 17:21:46
#include "net/der/input.h"
jam
2016/09/06 17:43:24
Done.
| |
125 net::der::Input tbs_cert, signature_alg; | |
126 net::der::BitString signature_value; | |
davidben
2016/09/06 17:21:46
#include "net/der/parse_values.h"
jam
2016/09/06 17:43:24
Done.
| |
127 bool rv = net::ParseCertificate(cert_data, &tbs_cert, &signature_alg, | |
128 &signature_value); | |
129 if (!rv) | |
davidben
2016/09/06 17:21:45
Nit: Rather than bool rv, could just write
if (!P
jam
2016/09/06 17:43:24
Done.
| |
130 return false; | |
131 | |
132 net::ParsedTbsCertificate parsed_tbs_cert; | |
133 rv = net::ParseTbsCertificate(tbs_cert, net::ParseCertificateOptions(), | |
134 &parsed_tbs_cert); | |
135 if (!rv) | |
136 return false; | |
137 | |
138 if (!GetCommonName(parsed_tbs_cert.subject_tlv, subject)) | |
139 return false; | |
140 | |
141 if (!GetCommonName(parsed_tbs_cert.issuer_tlv, issuer)) | |
142 return false; | |
143 | |
144 if (!DecodeTime(parsed_tbs_cert.validity_not_before, not_before)) | |
145 return false; | |
146 | |
147 if (!DecodeTime(parsed_tbs_cert.validity_not_after, not_after)) | |
148 return false; | |
149 | |
150 if (!parsed_tbs_cert.has_extensions) | |
151 return true; | |
152 | |
153 std::map<net::der::Input, net::ParsedExtension> extensions; | |
154 if (!net::ParseExtensions(parsed_tbs_cert.extensions_tlv, &extensions)) | |
155 return false; | |
156 | |
157 std::vector<std::string> san; | |
158 if (extensions.find(net::SubjectAltNameOid()) != extensions.end()) { | |
davidben
2016/09/06 17:21:45
Not that it matters, but may as well save a lookup
jam
2016/09/06 17:43:24
Done.
| |
159 std::unique_ptr<net::GeneralNames> subject_alt_names = | |
160 net::GeneralNames::CreateFromDer( | |
161 extensions[net::SubjectAltNameOid()].value); | |
162 if (subject_alt_names) { | |
163 *dns_names = subject_alt_names->dns_names; | |
164 for (const net::IPAddress& ip : subject_alt_names->ip_addresses) | |
165 ip_addresses->push_back(ip.ToString()); | |
166 } | |
167 } | |
168 | |
169 return true; | |
170 } | |
171 | |
85 } // namespace x509_util | 172 } // namespace x509_util |
86 | 173 |
87 } // namespace net | 174 } // namespace net |
OLD | NEW |