Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: net/base/x509_certificate_openssl.cc

Issue 11579002: Add X509Certificate::IsIssuedByEncoded() (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: add X509CertificateTest.IsIssuedByEncoded unit test Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base/x509_certificate.h" 5 #include "net/base/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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 if (data_length <= 0 || !data) 226 if (data_length <= 0 || !data)
227 return false; 227 return false;
228 internal_cache = SetDERCache(cert, x509_der_cache_index, data, data_length); 228 internal_cache = SetDERCache(cert, x509_der_cache_index, data, data_length);
229 if (!internal_cache) 229 if (!internal_cache)
230 return false; 230 return false;
231 } 231 }
232 *der_cache = *internal_cache; 232 *der_cache = *internal_cache;
233 return true; 233 return true;
234 } 234 }
235 235
236 // Used to free a list of X509_NAMEs without touching its objts.
237 // sk_X509_NAME_free is a macro and can't be used as function
238 // template parameter.
239 void sk_X509_NAME_free_list(STACK_OF(X509_NAME)* sk) {
240 sk_X509_NAME_free(sk);
241 }
242
243 // Used to free a list of X509_NAMEs and the objects it points to.
244 void sk_X509_NAME_free_all(STACK_OF(X509_NAME)* sk) {
245 sk_X509_NAME_pop_free(sk, X509_NAME_free);
246 }
247
236 } // namespace 248 } // namespace
237 249
238 // static 250 // static
239 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle( 251 X509Certificate::OSCertHandle X509Certificate::DupOSCertHandle(
240 OSCertHandle cert_handle) { 252 OSCertHandle cert_handle) {
241 DCHECK(cert_handle); 253 DCHECK(cert_handle);
242 // Using X509_dup causes the entire certificate to be reparsed. This 254 // Using X509_dup causes the entire certificate to be reparsed. This
243 // conversion, besides being non-trivial, drops any associated 255 // conversion, besides being non-trivial, drops any associated
244 // application-specific data set by X509_set_ex_data. Using CRYPTO_add 256 // application-specific data set by X509_set_ex_data. Using CRYPTO_add
245 // just bumps up the ref-count for the cert, without causing any allocations 257 // just bumps up the ref-count for the cert, without causing any allocations
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 *type = kPublicKeyTypeECDSA; 476 *type = kPublicKeyTypeECDSA;
465 *size_bits = EVP_PKEY_size(key); 477 *size_bits = EVP_PKEY_size(key);
466 break; 478 break;
467 case EVP_PKEY_DH: 479 case EVP_PKEY_DH:
468 *type = kPublicKeyTypeDH; 480 *type = kPublicKeyTypeDH;
469 *size_bits = EVP_PKEY_size(key) * 8; 481 *size_bits = EVP_PKEY_size(key) * 8;
470 break; 482 break;
471 } 483 }
472 } 484 }
473 485
486 bool X509Certificate::IsIssuedByEncoded(
487 const std::vector<std::string>& valid_issuers) {
488 if (valid_issuers.empty())
489 return false;
490
491 // Convert to a temporary list of X509_NAME objects.
492 // It will own the objects it points to.
493 crypto::ScopedOpenSSL<STACK_OF(X509_NAME), sk_X509_NAME_free_all>
494 issuer_names(sk_X509_NAME_new_null());
495 if (!issuer_names.get())
496 return false;
497
498 for (std::vector<std::string>::const_iterator it = valid_issuers.begin();
499 it != valid_issuers.end(); ++it) {
500 const unsigned char* p =
501 reinterpret_cast<const unsigned char*>(it->data());
502 long len = static_cast<long>(it->length());
503 X509_NAME* ca_name = d2i_X509_NAME(NULL, &p, len);
504 if (ca_name == NULL)
505 return false;
506 sk_X509_NAME_push(issuer_names.get(), ca_name);
507 }
508
509 // Create a temporary list of X509_NAME objects corresponding
510 // to the certificate chain. It doesn't own the object it points to.
511 std::vector<X509_NAME*> cert_names;
512 X509_NAME* issuer = X509_get_issuer_name(cert_handle_);
513 if (issuer == NULL)
514 return false;
515
516 cert_names.push_back(issuer);
517 for (OSCertHandles::iterator it = intermediate_ca_certs_.begin();
518 it != intermediate_ca_certs_.end(); ++it) {
519 issuer = X509_get_issuer_name(*it);
520 if (issuer == NULL)
521 return false;
522 cert_names.push_back(issuer);
523 }
524
525 // and 'cert_names'.
526 for (size_t n = 0; n < cert_names.size(); ++n) {
527 for (int m = 0; m < sk_X509_NAME_num(issuer_names.get()); ++m) {
528 X509_NAME* issuer = sk_X509_NAME_value(issuer_names.get(), m);
529 if (X509_NAME_cmp(issuer, cert_names[n]) == 0) {
530 return true;
531 }
532 }
533 }
534
535 return false;
536 }
537
474 } // namespace net 538 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698