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_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
6 | 6 |
7 #include <openssl/asn1.h> | 7 #include <openssl/asn1.h> |
8 #include <openssl/crypto.h> | 8 #include <openssl/crypto.h> |
9 #include <openssl/obj_mac.h> | 9 #include <openssl/obj_mac.h> |
10 #include <openssl/pem.h> | 10 #include <openssl/pem.h> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) |
27 #include "base/logging.h" | 27 #include "base/logging.h" |
28 #include "net/android/network_library.h" | 28 #include "net/android/network_library.h" |
29 #endif | 29 #endif |
30 | 30 |
31 namespace net { | 31 namespace net { |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 void CreateOSCertHandlesFromPKCS7Bytes( | 35 void CreateOSCertHandlesFromPKCS7Bytes( |
36 const char* data, int length, | 36 const char* data, |
| 37 int length, |
37 X509Certificate::OSCertHandles* handles) { | 38 X509Certificate::OSCertHandles* handles) { |
38 crypto::EnsureOpenSSLInit(); | 39 crypto::EnsureOpenSSLInit(); |
39 const unsigned char* der_data = reinterpret_cast<const unsigned char*>(data); | 40 const unsigned char* der_data = reinterpret_cast<const unsigned char*>(data); |
40 crypto::ScopedOpenSSL<PKCS7, PKCS7_free> pkcs7_cert( | 41 crypto::ScopedOpenSSL<PKCS7, PKCS7_free> pkcs7_cert( |
41 d2i_PKCS7(NULL, &der_data, length)); | 42 d2i_PKCS7(NULL, &der_data, length)); |
42 if (!pkcs7_cert.get()) | 43 if (!pkcs7_cert.get()) |
43 return; | 44 return; |
44 | 45 |
45 STACK_OF(X509)* certs = NULL; | 46 STACK_OF(X509)* certs = NULL; |
46 int nid = OBJ_obj2nid(pkcs7_cert.get()->type); | 47 int nid = OBJ_obj2nid(pkcs7_cert.get()->type); |
(...skipping 23 matching lines...) Expand all Loading... |
70 fields->push_back(field); | 71 fields->push_back(field); |
71 } | 72 } |
72 } | 73 } |
73 | 74 |
74 void ParsePrincipal(X509Certificate::OSCertHandle cert, | 75 void ParsePrincipal(X509Certificate::OSCertHandle cert, |
75 X509_NAME* x509_name, | 76 X509_NAME* x509_name, |
76 CertPrincipal* principal) { | 77 CertPrincipal* principal) { |
77 if (!x509_name) | 78 if (!x509_name) |
78 return; | 79 return; |
79 | 80 |
80 ParsePrincipalValues(x509_name, NID_streetAddress, | 81 ParsePrincipalValues( |
81 &principal->street_addresses); | 82 x509_name, NID_streetAddress, &principal->street_addresses); |
82 ParsePrincipalValues(x509_name, NID_organizationName, | 83 ParsePrincipalValues( |
83 &principal->organization_names); | 84 x509_name, NID_organizationName, &principal->organization_names); |
84 ParsePrincipalValues(x509_name, NID_organizationalUnitName, | 85 ParsePrincipalValues(x509_name, |
| 86 NID_organizationalUnitName, |
85 &principal->organization_unit_names); | 87 &principal->organization_unit_names); |
86 ParsePrincipalValues(x509_name, NID_domainComponent, | 88 ParsePrincipalValues( |
87 &principal->domain_components); | 89 x509_name, NID_domainComponent, &principal->domain_components); |
88 | 90 |
89 x509_util::ParsePrincipalValueByNID(x509_name, NID_commonName, | 91 x509_util::ParsePrincipalValueByNID( |
90 &principal->common_name); | 92 x509_name, NID_commonName, &principal->common_name); |
91 x509_util::ParsePrincipalValueByNID(x509_name, NID_localityName, | 93 x509_util::ParsePrincipalValueByNID( |
92 &principal->locality_name); | 94 x509_name, NID_localityName, &principal->locality_name); |
93 x509_util::ParsePrincipalValueByNID(x509_name, NID_stateOrProvinceName, | 95 x509_util::ParsePrincipalValueByNID( |
94 &principal->state_or_province_name); | 96 x509_name, NID_stateOrProvinceName, &principal->state_or_province_name); |
95 x509_util::ParsePrincipalValueByNID(x509_name, NID_countryName, | 97 x509_util::ParsePrincipalValueByNID( |
96 &principal->country_name); | 98 x509_name, NID_countryName, &principal->country_name); |
97 } | 99 } |
98 | 100 |
99 void ParseSubjectAltName(X509Certificate::OSCertHandle cert, | 101 void ParseSubjectAltName(X509Certificate::OSCertHandle cert, |
100 std::vector<std::string>* dns_names, | 102 std::vector<std::string>* dns_names, |
101 std::vector<std::string>* ip_addresses) { | 103 std::vector<std::string>* ip_addresses) { |
102 DCHECK(dns_names || ip_addresses); | 104 DCHECK(dns_names || ip_addresses); |
103 int index = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1); | 105 int index = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1); |
104 X509_EXTENSION* alt_name_ext = X509_get_ext(cert, index); | 106 X509_EXTENSION* alt_name_ext = X509_get_ext(cert, index); |
105 if (!alt_name_ext) | 107 if (!alt_name_ext) |
106 return; | 108 return; |
(...skipping 29 matching lines...) Expand all Loading... |
136 std::string(reinterpret_cast<const char*>(ip_addr), ip_addr_len)); | 138 std::string(reinterpret_cast<const char*>(ip_addr), ip_addr_len)); |
137 } | 139 } |
138 } | 140 } |
139 } | 141 } |
140 | 142 |
141 struct DERCache { | 143 struct DERCache { |
142 unsigned char* data; | 144 unsigned char* data; |
143 int data_length; | 145 int data_length; |
144 }; | 146 }; |
145 | 147 |
146 void DERCache_free(void* parent, void* ptr, CRYPTO_EX_DATA* ad, int idx, | 148 void DERCache_free(void* parent, |
147 long argl, void* argp) { | 149 void* ptr, |
| 150 CRYPTO_EX_DATA* ad, |
| 151 int idx, |
| 152 long argl, |
| 153 void* argp) { |
148 DERCache* der_cache = static_cast<DERCache*>(ptr); | 154 DERCache* der_cache = static_cast<DERCache*>(ptr); |
149 if (!der_cache) | 155 if (!der_cache) |
150 return; | 156 return; |
151 if (der_cache->data) | 157 if (der_cache->data) |
152 OPENSSL_free(der_cache->data); | 158 OPENSSL_free(der_cache->data); |
153 OPENSSL_free(der_cache); | 159 OPENSSL_free(der_cache); |
154 } | 160 } |
155 | 161 |
156 class X509InitSingleton { | 162 class X509InitSingleton { |
157 public: | 163 public: |
158 static X509InitSingleton* GetInstance() { | 164 static X509InitSingleton* GetInstance() { |
159 // We allow the X509 store to leak, because it is used from a non-joinable | 165 // We allow the X509 store to leak, because it is used from a non-joinable |
160 // worker that is not stopped on shutdown, hence may still be using | 166 // worker that is not stopped on shutdown, hence may still be using |
161 // OpenSSL library after the AtExit runner has completed. | 167 // OpenSSL library after the AtExit runner has completed. |
162 return Singleton<X509InitSingleton, | 168 return Singleton<X509InitSingleton, |
(...skipping 22 matching lines...) Expand all Loading... |
185 crypto::ScopedOpenSSL<X509_STORE, X509_STORE_free> store_; | 191 crypto::ScopedOpenSSL<X509_STORE, X509_STORE_free> store_; |
186 | 192 |
187 DISALLOW_COPY_AND_ASSIGN(X509InitSingleton); | 193 DISALLOW_COPY_AND_ASSIGN(X509InitSingleton); |
188 }; | 194 }; |
189 | 195 |
190 // Takes ownership of |data| (which must have been allocated by OpenSSL). | 196 // Takes ownership of |data| (which must have been allocated by OpenSSL). |
191 DERCache* SetDERCache(X509Certificate::OSCertHandle cert, | 197 DERCache* SetDERCache(X509Certificate::OSCertHandle cert, |
192 int x509_der_cache_index, | 198 int x509_der_cache_index, |
193 unsigned char* data, | 199 unsigned char* data, |
194 int data_length) { | 200 int data_length) { |
195 DERCache* internal_cache = static_cast<DERCache*>( | 201 DERCache* internal_cache = |
196 OPENSSL_malloc(sizeof(*internal_cache))); | 202 static_cast<DERCache*>(OPENSSL_malloc(sizeof(*internal_cache))); |
197 if (!internal_cache) { | 203 if (!internal_cache) { |
198 // We took ownership of |data|, so we must free if we can't add it to | 204 // We took ownership of |data|, so we must free if we can't add it to |
199 // |cert|. | 205 // |cert|. |
200 OPENSSL_free(data); | 206 OPENSSL_free(data); |
201 return NULL; | 207 return NULL; |
202 } | 208 } |
203 | 209 |
204 internal_cache->data = data; | 210 internal_cache->data = data; |
205 internal_cache->data_length = data_length; | 211 internal_cache->data_length = data_length; |
206 X509_set_ex_data(cert, x509_der_cache_index, internal_cache); | 212 X509_set_ex_data(cert, x509_der_cache_index, internal_cache); |
207 return internal_cache; | 213 return internal_cache; |
208 } | 214 } |
209 | 215 |
210 // Returns true if |der_cache| points to valid data, false otherwise. | 216 // Returns true if |der_cache| points to valid data, false otherwise. |
211 // (note: the DER-encoded data in |der_cache| is owned by |cert|, callers should | 217 // (note: the DER-encoded data in |der_cache| is owned by |cert|, callers should |
212 // not free it). | 218 // not free it). |
213 bool GetDERAndCacheIfNeeded(X509Certificate::OSCertHandle cert, | 219 bool GetDERAndCacheIfNeeded(X509Certificate::OSCertHandle cert, |
214 DERCache* der_cache) { | 220 DERCache* der_cache) { |
215 int x509_der_cache_index = | 221 int x509_der_cache_index = |
216 X509InitSingleton::GetInstance()->der_cache_ex_index(); | 222 X509InitSingleton::GetInstance()->der_cache_ex_index(); |
217 | 223 |
218 // Re-encoding the DER data via i2d_X509 is an expensive operation, but it's | 224 // Re-encoding the DER data via i2d_X509 is an expensive operation, but it's |
219 // necessary for comparing two certificates. We re-encode at most once per | 225 // necessary for comparing two certificates. We re-encode at most once per |
220 // certificate and cache the data within the X509 cert using X509_set_ex_data. | 226 // certificate and cache the data within the X509 cert using X509_set_ex_data. |
221 DERCache* internal_cache = static_cast<DERCache*>( | 227 DERCache* internal_cache = |
222 X509_get_ex_data(cert, x509_der_cache_index)); | 228 static_cast<DERCache*>(X509_get_ex_data(cert, x509_der_cache_index)); |
223 if (!internal_cache) { | 229 if (!internal_cache) { |
224 unsigned char* data = NULL; | 230 unsigned char* data = NULL; |
225 int data_length = i2d_X509(cert, &data); | 231 int data_length = i2d_X509(cert, &data); |
226 if (data_length <= 0 || !data) | 232 if (data_length <= 0 || !data) |
227 return false; | 233 return false; |
228 internal_cache = SetDERCache(cert, x509_der_cache_index, data, data_length); | 234 internal_cache = SetDERCache(cert, x509_der_cache_index, data, data_length); |
229 if (!internal_cache) | 235 if (!internal_cache) |
230 return false; | 236 return false; |
231 } | 237 } |
232 *der_cache = *internal_cache; | 238 *der_cache = *internal_cache; |
233 return true; | 239 return true; |
234 } | 240 } |
235 | 241 |
236 // Used to free a list of X509_NAMEs and the objects it points to. | 242 // Used to free a list of X509_NAMEs and the objects it points to. |
237 void sk_X509_NAME_free_all(STACK_OF(X509_NAME)* sk) { | 243 void sk_X509_NAME_free_all(STACK_OF(X509_NAME) * sk) { |
238 sk_X509_NAME_pop_free(sk, X509_NAME_free); | 244 sk_X509_NAME_pop_free(sk, X509_NAME_free); |
239 } | 245 } |
240 | 246 |
241 } // namespace | 247 } // namespace |
242 | 248 |
243 // static | 249 // static |
244 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle( | 250 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle( |
245 OSCertHandle cert_handle) { | 251 OSCertHandle cert_handle) { |
246 DCHECK(cert_handle); | 252 DCHECK(cert_handle); |
247 // Using X509_dup causes the entire certificate to be reparsed. This | 253 // Using X509_dup causes the entire certificate to be reparsed. This |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 return sha1; | 322 return sha1; |
317 SHA1_Update(&sha1_ctx, der_cache.data, der_cache.data_length); | 323 SHA1_Update(&sha1_ctx, der_cache.data, der_cache.data_length); |
318 } | 324 } |
319 SHA1_Final(sha1.data, &sha1_ctx); | 325 SHA1_Final(sha1.data, &sha1_ctx); |
320 | 326 |
321 return sha1; | 327 return sha1; |
322 } | 328 } |
323 | 329 |
324 // static | 330 // static |
325 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( | 331 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( |
326 const char* data, int length) { | 332 const char* data, |
| 333 int length) { |
327 if (length < 0) | 334 if (length < 0) |
328 return NULL; | 335 return NULL; |
329 crypto::EnsureOpenSSLInit(); | 336 crypto::EnsureOpenSSLInit(); |
330 const unsigned char* d2i_data = | 337 const unsigned char* d2i_data = reinterpret_cast<const unsigned char*>(data); |
331 reinterpret_cast<const unsigned char*>(data); | |
332 // Don't cache this data via SetDERCache as this wire format may be not be | 338 // Don't cache this data via SetDERCache as this wire format may be not be |
333 // identical from the i2d_X509 roundtrip. | 339 // identical from the i2d_X509 roundtrip. |
334 X509* cert = d2i_X509(NULL, &d2i_data, length); | 340 X509* cert = d2i_X509(NULL, &d2i_data, length); |
335 return cert; | 341 return cert; |
336 } | 342 } |
337 | 343 |
338 // static | 344 // static |
339 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( | 345 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( |
340 const char* data, int length, Format format) { | 346 const char* data, |
| 347 int length, |
| 348 Format format) { |
341 OSCertHandles results; | 349 OSCertHandles results; |
342 if (length < 0) | 350 if (length < 0) |
343 return results; | 351 return results; |
344 | 352 |
345 switch (format) { | 353 switch (format) { |
346 case FORMAT_SINGLE_CERTIFICATE: { | 354 case FORMAT_SINGLE_CERTIFICATE: { |
347 OSCertHandle handle = CreateOSCertHandleFromBytes(data, length); | 355 OSCertHandle handle = CreateOSCertHandleFromBytes(data, length); |
348 if (handle) | 356 if (handle) |
349 results.push_back(handle); | 357 results.push_back(handle); |
350 break; | 358 break; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 DCHECK(a && b); | 403 DCHECK(a && b); |
396 if (a == b) | 404 if (a == b) |
397 return true; | 405 return true; |
398 | 406 |
399 // X509_cmp only checks the fingerprint, but we want to compare the whole | 407 // X509_cmp only checks the fingerprint, but we want to compare the whole |
400 // DER data. Encoding it from OSCertHandle is an expensive operation, so we | 408 // DER data. Encoding it from OSCertHandle is an expensive operation, so we |
401 // cache the DER (if not already cached via X509_set_ex_data). | 409 // cache the DER (if not already cached via X509_set_ex_data). |
402 DERCache der_cache_a, der_cache_b; | 410 DERCache der_cache_a, der_cache_b; |
403 | 411 |
404 return GetDERAndCacheIfNeeded(a, &der_cache_a) && | 412 return GetDERAndCacheIfNeeded(a, &der_cache_a) && |
405 GetDERAndCacheIfNeeded(b, &der_cache_b) && | 413 GetDERAndCacheIfNeeded(b, &der_cache_b) && |
406 der_cache_a.data_length == der_cache_b.data_length && | 414 der_cache_a.data_length == der_cache_b.data_length && |
407 memcmp(der_cache_a.data, der_cache_b.data, der_cache_a.data_length) == 0; | 415 memcmp(der_cache_a.data, der_cache_b.data, der_cache_a.data_length) == |
| 416 0; |
408 } | 417 } |
409 | 418 |
410 // static | 419 // static |
411 X509Certificate::OSCertHandle | 420 X509Certificate::OSCertHandle X509Certificate::ReadOSCertHandleFromPickle( |
412 X509Certificate::ReadOSCertHandleFromPickle(PickleIterator* pickle_iter) { | 421 PickleIterator* pickle_iter) { |
413 const char* data; | 422 const char* data; |
414 int length; | 423 int length; |
415 if (!pickle_iter->ReadData(&data, &length)) | 424 if (!pickle_iter->ReadData(&data, &length)) |
416 return NULL; | 425 return NULL; |
417 | 426 |
418 return CreateOSCertHandleFromBytes(data, length); | 427 return CreateOSCertHandleFromBytes(data, length); |
419 } | 428 } |
420 | 429 |
421 // static | 430 // static |
422 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, | 431 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, |
423 Pickle* pickle) { | 432 Pickle* pickle) { |
424 DERCache der_cache; | 433 DERCache der_cache; |
425 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 434 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
426 return false; | 435 return false; |
427 | 436 |
428 return pickle->WriteData( | 437 return pickle->WriteData(reinterpret_cast<const char*>(der_cache.data), |
429 reinterpret_cast<const char*>(der_cache.data), | 438 der_cache.data_length); |
430 der_cache.data_length); | |
431 } | 439 } |
432 | 440 |
433 // static | 441 // static |
434 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle, | 442 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle, |
435 size_t* size_bits, | 443 size_t* size_bits, |
436 PublicKeyType* type) { | 444 PublicKeyType* type) { |
437 *type = kPublicKeyTypeUnknown; | 445 *type = kPublicKeyTypeUnknown; |
438 *size_bits = 0; | 446 *size_bits = 0; |
439 | 447 |
440 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> scoped_key( | 448 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> scoped_key( |
(...skipping 30 matching lines...) Expand all Loading... |
471 return false; | 479 return false; |
472 | 480 |
473 // Convert to a temporary list of X509_NAME objects. | 481 // Convert to a temporary list of X509_NAME objects. |
474 // It will own the objects it points to. | 482 // It will own the objects it points to. |
475 crypto::ScopedOpenSSL<STACK_OF(X509_NAME), sk_X509_NAME_free_all> | 483 crypto::ScopedOpenSSL<STACK_OF(X509_NAME), sk_X509_NAME_free_all> |
476 issuer_names(sk_X509_NAME_new_null()); | 484 issuer_names(sk_X509_NAME_new_null()); |
477 if (!issuer_names.get()) | 485 if (!issuer_names.get()) |
478 return false; | 486 return false; |
479 | 487 |
480 for (std::vector<std::string>::const_iterator it = valid_issuers.begin(); | 488 for (std::vector<std::string>::const_iterator it = valid_issuers.begin(); |
481 it != valid_issuers.end(); ++it) { | 489 it != valid_issuers.end(); |
482 const unsigned char* p = | 490 ++it) { |
483 reinterpret_cast<const unsigned char*>(it->data()); | 491 const unsigned char* p = reinterpret_cast<const unsigned char*>(it->data()); |
484 long len = static_cast<long>(it->length()); | 492 long len = static_cast<long>(it->length()); |
485 X509_NAME* ca_name = d2i_X509_NAME(NULL, &p, len); | 493 X509_NAME* ca_name = d2i_X509_NAME(NULL, &p, len); |
486 if (ca_name == NULL) | 494 if (ca_name == NULL) |
487 return false; | 495 return false; |
488 sk_X509_NAME_push(issuer_names.get(), ca_name); | 496 sk_X509_NAME_push(issuer_names.get(), ca_name); |
489 } | 497 } |
490 | 498 |
491 // Create a temporary list of X509_NAME objects corresponding | 499 // Create a temporary list of X509_NAME objects corresponding |
492 // to the certificate chain. It doesn't own the object it points to. | 500 // to the certificate chain. It doesn't own the object it points to. |
493 std::vector<X509_NAME*> cert_names; | 501 std::vector<X509_NAME*> cert_names; |
494 X509_NAME* issuer = X509_get_issuer_name(cert_handle_); | 502 X509_NAME* issuer = X509_get_issuer_name(cert_handle_); |
495 if (issuer == NULL) | 503 if (issuer == NULL) |
496 return false; | 504 return false; |
497 | 505 |
498 cert_names.push_back(issuer); | 506 cert_names.push_back(issuer); |
499 for (OSCertHandles::iterator it = intermediate_ca_certs_.begin(); | 507 for (OSCertHandles::iterator it = intermediate_ca_certs_.begin(); |
500 it != intermediate_ca_certs_.end(); ++it) { | 508 it != intermediate_ca_certs_.end(); |
| 509 ++it) { |
501 issuer = X509_get_issuer_name(*it); | 510 issuer = X509_get_issuer_name(*it); |
502 if (issuer == NULL) | 511 if (issuer == NULL) |
503 return false; | 512 return false; |
504 cert_names.push_back(issuer); | 513 cert_names.push_back(issuer); |
505 } | 514 } |
506 | 515 |
507 // and 'cert_names'. | 516 // and 'cert_names'. |
508 for (size_t n = 0; n < cert_names.size(); ++n) { | 517 for (size_t n = 0; n < cert_names.size(); ++n) { |
509 for (int m = 0; m < sk_X509_NAME_num(issuer_names.get()); ++m) { | 518 for (int m = 0; m < sk_X509_NAME_num(issuer_names.get()); ++m) { |
510 X509_NAME* issuer = sk_X509_NAME_value(issuer_names.get(), m); | 519 X509_NAME* issuer = sk_X509_NAME_value(issuer_names.get(), m); |
511 if (X509_NAME_cmp(issuer, cert_names[n]) == 0) { | 520 if (X509_NAME_cmp(issuer, cert_names[n]) == 0) { |
512 return true; | 521 return true; |
513 } | 522 } |
514 } | 523 } |
515 } | 524 } |
516 | 525 |
517 return false; | 526 return false; |
518 } | 527 } |
519 | 528 |
520 } // namespace net | 529 } // namespace net |
OLD | NEW |