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 "chrome/common/net/x509_certificate_model.h" | 5 #include "chrome/common/net/x509_certificate_model.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <cms.h> | 8 #include <cms.h> |
9 #include <hasht.h> | 9 #include <hasht.h> |
10 #include <keyhi.h> // SECKEY_DestroyPrivateKey | 10 #include <keyhi.h> // SECKEY_DestroyPrivateKey |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 const std::string& non_critical_label, | 64 const std::string& non_critical_label, |
65 CERTCertExtension* extension) { | 65 CERTCertExtension* extension) { |
66 std::string criticality = | 66 std::string criticality = |
67 extension->critical.data && extension->critical.data[0] ? | 67 extension->critical.data && extension->critical.data[0] ? |
68 critical_label : non_critical_label; | 68 critical_label : non_critical_label; |
69 return criticality + "\n" + | 69 return criticality + "\n" + |
70 psm::ProcessExtensionData(SECOID_FindOIDTag(&extension->id), | 70 psm::ProcessExtensionData(SECOID_FindOIDTag(&extension->id), |
71 &extension->value); | 71 &extension->value); |
72 } | 72 } |
73 | 73 |
74 std::string GetNickname(net::X509Certificate::OSCertHandle cert_handle) { | |
75 std::string name; | |
76 if (cert_handle->nickname) { | |
77 name = cert_handle->nickname; | |
78 // Hack copied from mozilla: Cut off text before first :, which seems to | |
79 // just be the token name. | |
80 size_t colon_pos = name.find(':'); | |
81 if (colon_pos != std::string::npos) | |
82 name = name.substr(colon_pos + 1); | |
83 } | |
84 return name; | |
85 } | |
86 | |
74 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
75 // NSS certificate export functions. | 88 // NSS certificate export functions. |
76 | 89 |
77 struct NSSCMSMessageDeleter { | 90 struct NSSCMSMessageDeleter { |
78 inline void operator()(NSSCMSMessage* x) const { | 91 inline void operator()(NSSCMSMessage* x) const { |
79 NSS_CMSMessage_Destroy(x); | 92 NSS_CMSMessage_Destroy(x); |
80 } | 93 } |
81 }; | 94 }; |
82 typedef scoped_ptr<NSSCMSMessage, NSSCMSMessageDeleter> ScopedNSSCMSMessage; | 95 typedef scoped_ptr<NSSCMSMessage, NSSCMSMessageDeleter> ScopedNSSCMSMessage; |
83 | 96 |
(...skipping 13 matching lines...) Expand all Loading... | |
97 using std::string; | 110 using std::string; |
98 | 111 |
99 string GetCertNameOrNickname(X509Certificate::OSCertHandle cert_handle) { | 112 string GetCertNameOrNickname(X509Certificate::OSCertHandle cert_handle) { |
100 string name = ProcessIDN( | 113 string name = ProcessIDN( |
101 Stringize(CERT_GetCommonName(&cert_handle->subject), std::string())); | 114 Stringize(CERT_GetCommonName(&cert_handle->subject), std::string())); |
102 if (!name.empty()) | 115 if (!name.empty()) |
103 return name; | 116 return name; |
104 return GetNickname(cert_handle); | 117 return GetNickname(cert_handle); |
105 } | 118 } |
106 | 119 |
107 string GetNickname(X509Certificate::OSCertHandle cert_handle) { | |
108 string name; | |
109 if (cert_handle->nickname) { | |
110 name = cert_handle->nickname; | |
111 // Hack copied from mozilla: Cut off text before first :, which seems to | |
112 // just be the token name. | |
113 size_t colon_pos = name.find(':'); | |
114 if (colon_pos != string::npos) | |
115 name = name.substr(colon_pos + 1); | |
116 } | |
117 return name; | |
118 } | |
119 | |
120 string GetTokenName(X509Certificate::OSCertHandle cert_handle) { | 120 string GetTokenName(X509Certificate::OSCertHandle cert_handle) { |
121 return psm::GetCertTokenName(cert_handle); | 121 return psm::GetCertTokenName(cert_handle); |
122 } | 122 } |
123 | 123 |
124 string GetVersion(X509Certificate::OSCertHandle cert_handle) { | 124 string GetVersion(X509Certificate::OSCertHandle cert_handle) { |
125 // If the version field is omitted from the certificate, the default | 125 // If the version field is omitted from the certificate, the default |
126 // value is v1(0). | 126 // value is v1(0). |
127 unsigned long version = 0; | 127 unsigned long version = 0; |
128 if (cert_handle->version.len == 0 || | 128 if (cert_handle->version.len == 0 || |
129 SEC_ASN1DecodeInteger(&cert_handle->version, &version) == SECSuccess) { | 129 SEC_ASN1DecodeInteger(&cert_handle->version, &version) == SECSuccess) { |
130 return base::UintToString(version + 1); | 130 return base::UintToString(version + 1); |
131 } | 131 } |
132 return std::string(); | 132 return std::string(); |
133 } | 133 } |
134 | 134 |
135 net::CertType GetType(X509Certificate::OSCertHandle cert_handle) { | 135 net::CertType GetType(X509Certificate::OSCertHandle cert_handle) { |
136 return psm::GetCertType(cert_handle); | 136 return psm::GetCertType(cert_handle); |
137 } | 137 } |
138 | 138 |
139 string GetEmailAddress(X509Certificate::OSCertHandle cert_handle) { | |
140 if (cert_handle->emailAddr) | |
141 return cert_handle->emailAddr; | |
142 return std::string(); | |
143 } | |
144 | |
145 void GetUsageStrings(X509Certificate::OSCertHandle cert_handle, | 139 void GetUsageStrings(X509Certificate::OSCertHandle cert_handle, |
146 std::vector<string>* usages) { | 140 std::vector<string>* usages) { |
147 psm::GetCertUsageStrings(cert_handle, usages); | 141 psm::GetCertUsageStrings(cert_handle, usages); |
148 } | 142 } |
149 | 143 |
150 string GetKeyUsageString(X509Certificate::OSCertHandle cert_handle) { | |
151 SECItem key_usage; | |
152 key_usage.data = NULL; | |
153 string key_usage_str; | |
154 if (CERT_FindKeyUsageExtension(cert_handle, &key_usage) == SECSuccess) { | |
155 key_usage_str = psm::ProcessKeyUsageBitString(&key_usage, ','); | |
156 PORT_Free(key_usage.data); | |
157 } | |
158 return key_usage_str; | |
159 } | |
160 | |
161 string GetSerialNumberHexified(X509Certificate::OSCertHandle cert_handle, | 144 string GetSerialNumberHexified(X509Certificate::OSCertHandle cert_handle, |
162 const string& alternative_text) { | 145 const string& alternative_text) { |
163 return Stringize(CERT_Hexify(&cert_handle->serialNumber, true), | 146 return Stringize(CERT_Hexify(&cert_handle->serialNumber, true), |
164 alternative_text); | 147 alternative_text); |
165 } | 148 } |
166 | 149 |
167 string GetIssuerCommonName(X509Certificate::OSCertHandle cert_handle, | 150 string GetIssuerCommonName(X509Certificate::OSCertHandle cert_handle, |
168 const string& alternative_text) { | 151 const string& alternative_text) { |
169 return Stringize(CERT_GetCommonName(&cert_handle->issuer), alternative_text); | 152 return Stringize(CERT_GetCommonName(&cert_handle->issuer), alternative_text); |
170 } | 153 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 } | 194 } |
212 | 195 |
213 string GetIssuerName(X509Certificate::OSCertHandle cert_handle) { | 196 string GetIssuerName(X509Certificate::OSCertHandle cert_handle) { |
214 return psm::ProcessName(&cert_handle->issuer); | 197 return psm::ProcessName(&cert_handle->issuer); |
215 } | 198 } |
216 | 199 |
217 string GetSubjectName(X509Certificate::OSCertHandle cert_handle) { | 200 string GetSubjectName(X509Certificate::OSCertHandle cert_handle) { |
218 return psm::ProcessName(&cert_handle->subject); | 201 return psm::ProcessName(&cert_handle->subject); |
219 } | 202 } |
220 | 203 |
221 void GetEmailAddresses(X509Certificate::OSCertHandle cert_handle, | |
222 std::vector<string>* email_addresses) { | |
223 for (const char* addr = CERT_GetFirstEmailAddress(cert_handle); | |
224 addr; addr = CERT_GetNextEmailAddress(cert_handle, addr)) { | |
225 // The first email addr (from Subject) may be duplicated in Subject | |
226 // Alternative Name, so check subsequent addresses are not equal to the | |
227 // first one before adding to the list. | |
228 if (!email_addresses->size() || (*email_addresses)[0] != addr) | |
229 email_addresses->push_back(addr); | |
230 } | |
231 } | |
232 | |
233 void GetNicknameStringsFromCertList( | |
234 const std::vector<scoped_refptr<X509Certificate> >& certs, | |
235 const string& cert_expired, | |
236 const string& cert_not_yet_valid, | |
237 std::vector<string>* nick_names) { | |
238 CERTCertList* cert_list = CERT_NewCertList(); | |
239 for (size_t i = 0; i < certs.size(); ++i) { | |
240 CERT_AddCertToListTail( | |
241 cert_list, | |
242 CERT_DupCertificate(certs[i]->os_cert_handle())); | |
243 } | |
244 // Would like to use CERT_GetCertNicknameWithValidity on each cert | |
245 // individually instead of having to build a CERTCertList for this, but that | |
246 // function is not exported. | |
247 CERTCertNicknames* cert_nicknames = CERT_NicknameStringsFromCertList( | |
248 cert_list, | |
249 const_cast<char*>(cert_expired.c_str()), | |
250 const_cast<char*>(cert_not_yet_valid.c_str())); | |
251 DCHECK_EQ(cert_nicknames->numnicknames, | |
252 static_cast<int>(certs.size())); | |
253 | |
254 for (int i = 0; i < cert_nicknames->numnicknames; ++i) | |
255 nick_names->push_back(cert_nicknames->nicknames[i]); | |
256 | |
257 CERT_FreeNicknames(cert_nicknames); | |
258 CERT_DestroyCertList(cert_list); | |
259 } | |
260 | |
261 void GetExtensions( | 204 void GetExtensions( |
262 const string& critical_label, | 205 const string& critical_label, |
263 const string& non_critical_label, | 206 const string& non_critical_label, |
264 X509Certificate::OSCertHandle cert_handle, | 207 X509Certificate::OSCertHandle cert_handle, |
265 Extensions* extensions) { | 208 Extensions* extensions) { |
266 if (cert_handle->extensions) { | 209 if (cert_handle->extensions) { |
210 psm::RegisterDynamicOids(); | |
Ryan Sleevi
2014/05/08 00:34:38
This needs to be called before getting the subject
mattm
2014/05/13 00:57:40
Hm, seems easiest to just move it to GetOIDText, w
| |
267 for (size_t i = 0; cert_handle->extensions[i] != NULL; ++i) { | 211 for (size_t i = 0; cert_handle->extensions[i] != NULL; ++i) { |
268 Extension extension; | 212 Extension extension; |
269 extension.name = psm::GetOIDText(&cert_handle->extensions[i]->id); | 213 extension.name = psm::GetOIDText(&cert_handle->extensions[i]->id); |
270 extension.value = ProcessExtension( | 214 extension.value = ProcessExtension( |
271 critical_label, non_critical_label, cert_handle->extensions[i]); | 215 critical_label, non_critical_label, cert_handle->extensions[i]); |
272 extensions->push_back(extension); | 216 extensions->push_back(extension); |
273 } | 217 } |
274 } | 218 } |
275 } | 219 } |
276 | 220 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 | 320 |
377 string ProcessSubjectPublicKeyInfo(X509Certificate::OSCertHandle cert_handle) { | 321 string ProcessSubjectPublicKeyInfo(X509Certificate::OSCertHandle cert_handle) { |
378 return psm::ProcessSubjectPublicKeyInfo(&cert_handle->subjectPublicKeyInfo); | 322 return psm::ProcessSubjectPublicKeyInfo(&cert_handle->subjectPublicKeyInfo); |
379 } | 323 } |
380 | 324 |
381 string ProcessRawBitsSignatureWrap(X509Certificate::OSCertHandle cert_handle) { | 325 string ProcessRawBitsSignatureWrap(X509Certificate::OSCertHandle cert_handle) { |
382 return ProcessRawBits(cert_handle->signatureWrap.signature.data, | 326 return ProcessRawBits(cert_handle->signatureWrap.signature.data, |
383 cert_handle->signatureWrap.signature.len); | 327 cert_handle->signatureWrap.signature.len); |
384 } | 328 } |
385 | 329 |
386 void RegisterDynamicOids() { | |
387 psm::RegisterDynamicOids(); | |
388 } | |
389 | |
390 } // namespace x509_certificate_model | 330 } // namespace x509_certificate_model |
OLD | NEW |