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/bytestring.h> | |
9 #include <openssl/crypto.h> | 8 #include <openssl/crypto.h> |
10 #include <openssl/obj_mac.h> | 9 #include <openssl/obj_mac.h> |
11 #include <openssl/pem.h> | 10 #include <openssl/pem.h> |
| 11 #include <openssl/pkcs7.h> |
12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
13 #include <openssl/ssl.h> | 13 #include <openssl/ssl.h> |
14 #include <openssl/x509v3.h> | 14 #include <openssl/x509v3.h> |
15 | 15 |
16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
17 #include "base/pickle.h" | 17 #include "base/pickle.h" |
18 #include "base/sha1.h" | 18 #include "base/sha1.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "crypto/openssl_util.h" | 21 #include "crypto/openssl_util.h" |
(...skipping 11 matching lines...) Expand all Loading... |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 typedef crypto::ScopedOpenSSL<GENERAL_NAMES, GENERAL_NAMES_free>::Type | 36 typedef crypto::ScopedOpenSSL<GENERAL_NAMES, GENERAL_NAMES_free>::Type |
37 ScopedGENERAL_NAMES; | 37 ScopedGENERAL_NAMES; |
38 | 38 |
39 void CreateOSCertHandlesFromPKCS7Bytes( | 39 void CreateOSCertHandlesFromPKCS7Bytes( |
40 const char* data, int length, | 40 const char* data, int length, |
41 X509Certificate::OSCertHandles* handles) { | 41 X509Certificate::OSCertHandles* handles) { |
42 crypto::EnsureOpenSSLInit(); | 42 crypto::EnsureOpenSSLInit(); |
43 crypto::OpenSSLErrStackTracer err_cleaner(FROM_HERE); | 43 const unsigned char* der_data = reinterpret_cast<const unsigned char*>(data); |
| 44 crypto::ScopedOpenSSL<PKCS7, PKCS7_free>::Type pkcs7_cert( |
| 45 d2i_PKCS7(NULL, &der_data, length)); |
| 46 if (!pkcs7_cert.get()) |
| 47 return; |
44 | 48 |
45 CBS der_data; | 49 STACK_OF(X509)* certs = NULL; |
46 CBS_init(&der_data, reinterpret_cast<const uint8_t*>(data), length); | 50 int nid = OBJ_obj2nid(pkcs7_cert.get()->type); |
47 STACK_OF(X509)* certs = sk_X509_new_null(); | 51 if (nid == NID_pkcs7_signed) { |
| 52 certs = pkcs7_cert.get()->d.sign->cert; |
| 53 } else if (nid == NID_pkcs7_signedAndEnveloped) { |
| 54 certs = pkcs7_cert.get()->d.signed_and_enveloped->cert; |
| 55 } |
48 | 56 |
49 if (PKCS7_get_certificates(certs, &der_data)) { | 57 if (certs) { |
50 for (size_t i = 0; i < sk_X509_num(certs); ++i) { | 58 for (int i = 0; i < sk_X509_num(certs); ++i) { |
51 X509* x509_cert = | 59 X509* x509_cert = |
52 X509Certificate::DupOSCertHandle(sk_X509_value(certs, i)); | 60 X509Certificate::DupOSCertHandle(sk_X509_value(certs, i)); |
53 handles->push_back(x509_cert); | 61 handles->push_back(x509_cert); |
54 } | 62 } |
55 } | 63 } |
56 sk_X509_pop_free(certs, X509_free); | |
57 } | 64 } |
58 | 65 |
59 void ParsePrincipalValues(X509_NAME* name, | 66 void ParsePrincipalValues(X509_NAME* name, |
60 int nid, | 67 int nid, |
61 std::vector<std::string>* fields) { | 68 std::vector<std::string>* fields) { |
62 for (int index = -1; | 69 for (int index = -1; |
63 (index = X509_NAME_get_index_by_NID(name, nid, index)) != -1;) { | 70 (index = X509_NAME_get_index_by_NID(name, nid, index)) != -1;) { |
64 std::string field; | 71 std::string field; |
65 if (!x509_util::ParsePrincipalValueByIndex(name, index, &field)) | 72 if (!x509_util::ParsePrincipalValueByIndex(name, index, &field)) |
66 break; | 73 break; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 int index = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1); | 107 int index = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1); |
101 X509_EXTENSION* alt_name_ext = X509_get_ext(cert, index); | 108 X509_EXTENSION* alt_name_ext = X509_get_ext(cert, index); |
102 if (!alt_name_ext) | 109 if (!alt_name_ext) |
103 return; | 110 return; |
104 | 111 |
105 ScopedGENERAL_NAMES alt_names( | 112 ScopedGENERAL_NAMES alt_names( |
106 reinterpret_cast<GENERAL_NAMES*>(X509V3_EXT_d2i(alt_name_ext))); | 113 reinterpret_cast<GENERAL_NAMES*>(X509V3_EXT_d2i(alt_name_ext))); |
107 if (!alt_names.get()) | 114 if (!alt_names.get()) |
108 return; | 115 return; |
109 | 116 |
110 for (size_t i = 0; i < sk_GENERAL_NAME_num(alt_names.get()); ++i) { | 117 for (int i = 0; i < sk_GENERAL_NAME_num(alt_names.get()); ++i) { |
111 const GENERAL_NAME* name = sk_GENERAL_NAME_value(alt_names.get(), i); | 118 const GENERAL_NAME* name = sk_GENERAL_NAME_value(alt_names.get(), i); |
112 if (name->type == GEN_DNS && dns_names) { | 119 if (name->type == GEN_DNS && dns_names) { |
113 const unsigned char* dns_name = ASN1_STRING_data(name->d.dNSName); | 120 const unsigned char* dns_name = ASN1_STRING_data(name->d.dNSName); |
114 if (!dns_name) | 121 if (!dns_name) |
115 continue; | 122 continue; |
116 int dns_name_len = ASN1_STRING_length(name->d.dNSName); | 123 int dns_name_len = ASN1_STRING_length(name->d.dNSName); |
117 dns_names->push_back( | 124 dns_names->push_back( |
118 std::string(reinterpret_cast<const char*>(dns_name), dns_name_len)); | 125 std::string(reinterpret_cast<const char*>(dns_name), dns_name_len)); |
119 } else if (name->type == GEN_IPADD && ip_addresses) { | 126 } else if (name->type == GEN_IPADD && ip_addresses) { |
120 const unsigned char* ip_addr = name->d.iPAddress->data; | 127 const unsigned char* ip_addr = name->d.iPAddress->data; |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 for (OSCertHandles::iterator it = intermediate_ca_certs_.begin(); | 502 for (OSCertHandles::iterator it = intermediate_ca_certs_.begin(); |
496 it != intermediate_ca_certs_.end(); ++it) { | 503 it != intermediate_ca_certs_.end(); ++it) { |
497 issuer = X509_get_issuer_name(*it); | 504 issuer = X509_get_issuer_name(*it); |
498 if (issuer == NULL) | 505 if (issuer == NULL) |
499 return false; | 506 return false; |
500 cert_names.push_back(issuer); | 507 cert_names.push_back(issuer); |
501 } | 508 } |
502 | 509 |
503 // and 'cert_names'. | 510 // and 'cert_names'. |
504 for (size_t n = 0; n < cert_names.size(); ++n) { | 511 for (size_t n = 0; n < cert_names.size(); ++n) { |
505 for (size_t m = 0; m < sk_X509_NAME_num(issuer_names.get()); ++m) { | 512 for (int m = 0; m < sk_X509_NAME_num(issuer_names.get()); ++m) { |
506 X509_NAME* issuer = sk_X509_NAME_value(issuer_names.get(), m); | 513 X509_NAME* issuer = sk_X509_NAME_value(issuer_names.get(), m); |
507 if (X509_NAME_cmp(issuer, cert_names[n]) == 0) { | 514 if (X509_NAME_cmp(issuer, cert_names[n]) == 0) { |
508 return true; | 515 return true; |
509 } | 516 } |
510 } | 517 } |
511 } | 518 } |
512 | 519 |
513 return false; | 520 return false; |
514 } | 521 } |
515 | 522 |
516 } // namespace net | 523 } // namespace net |
OLD | NEW |