OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
11 #include <openssl/pkcs7.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/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "crypto/openssl_util.h" | 21 #include "crypto/openssl_util.h" |
22 #include "net/base/asn1_util.h" | 22 #include "net/base/asn1_util.h" |
23 #include "net/base/cert_status_flags.h" | 23 #include "net/base/cert_status_flags.h" |
24 #include "net/base/cert_verify_result.h" | 24 #include "net/base/cert_verify_result.h" |
25 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
26 #include "net/base/x509_util_openssl.h" | 26 #include "net/base/x509_util_openssl.h" |
27 | 27 |
| 28 #if defined(OS_ANDROID) |
| 29 #include "base/logging.h" |
| 30 #include "net/android/network_library.h" |
| 31 #endif |
| 32 |
28 namespace net { | 33 namespace net { |
29 | 34 |
30 namespace { | 35 namespace { |
31 | 36 |
32 void CreateOSCertHandlesFromPKCS7Bytes( | 37 void CreateOSCertHandlesFromPKCS7Bytes( |
33 const char* data, int length, | 38 const char* data, int length, |
34 X509Certificate::OSCertHandles* handles) { | 39 X509Certificate::OSCertHandles* handles) { |
35 crypto::EnsureOpenSSLInit(); | 40 crypto::EnsureOpenSSLInit(); |
36 const unsigned char* der_data = reinterpret_cast<const unsigned char*>(data); | 41 const unsigned char* der_data = reinterpret_cast<const unsigned char*>(data); |
37 crypto::ScopedOpenSSL<PKCS7, PKCS7_free> pkcs7_cert( | 42 crypto::ScopedOpenSSL<PKCS7, PKCS7_free> pkcs7_cert( |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 ip_addrs->clear(); | 509 ip_addrs->clear(); |
505 | 510 |
506 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); | 511 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); |
507 } | 512 } |
508 | 513 |
509 // static | 514 // static |
510 X509_STORE* X509Certificate::cert_store() { | 515 X509_STORE* X509Certificate::cert_store() { |
511 return X509InitSingleton::GetInstance()->store(); | 516 return X509InitSingleton::GetInstance()->store(); |
512 } | 517 } |
513 | 518 |
514 #if !defined(OS_ANDROID) | 519 #if defined(OS_ANDROID) |
515 | |
516 int X509Certificate::VerifyInternal(const std::string& hostname, | 520 int X509Certificate::VerifyInternal(const std::string& hostname, |
517 int flags, | 521 int flags, |
518 CRLSet* crl_set, | 522 CRLSet* crl_set, |
| 523 CertVerifyResult* verify_result) const { |
| 524 if (!VerifyNameMatch(hostname)) |
| 525 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 526 |
| 527 std::vector<std::string> cert_bytes; |
| 528 GetChainDEREncodedBytes(&cert_bytes); |
| 529 |
| 530 // TODO(joth): Fetch the authentication type from SSL rather than hardcode. |
| 531 // TODO(jingzhao): Recover the original implementation once we support JNI. |
| 532 #if 0 |
| 533 android::VerifyResult result = |
| 534 android::VerifyX509CertChain(cert_bytes, hostname, "RSA"); |
| 535 #else |
| 536 android::VerifyResult result = android::VERIFY_INVOCATION_ERROR; |
| 537 NOTIMPLEMENTED(); |
| 538 #endif |
| 539 switch (result) { |
| 540 case android::VERIFY_OK: |
| 541 break; |
| 542 case android::VERIFY_BAD_HOSTNAME: |
| 543 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 544 break; |
| 545 case android::VERIFY_NO_TRUSTED_ROOT: |
| 546 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; |
| 547 break; |
| 548 case android::VERIFY_INVOCATION_ERROR: |
| 549 default: |
| 550 verify_result->cert_status |= ERR_CERT_INVALID; |
| 551 break; |
| 552 } |
| 553 if (IsCertStatusError(verify_result->cert_status)) |
| 554 return MapCertStatusToNetError(verify_result->cert_status); |
| 555 return OK; |
| 556 } |
| 557 |
| 558 #else |
| 559 int X509Certificate::VerifyInternal(const std::string& hostname, |
| 560 int flags, |
| 561 CRLSet* crl_set, |
519 CertVerifyResult* verify_result) const { | 562 CertVerifyResult* verify_result) const { |
520 if (!VerifyNameMatch(hostname)) | 563 if (!VerifyNameMatch(hostname)) |
521 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | 564 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
522 | 565 |
523 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( | 566 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( |
524 X509_STORE_CTX_new()); | 567 X509_STORE_CTX_new()); |
525 | 568 |
526 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( | 569 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( |
527 sk_X509_new_null()); | 570 sk_X509_new_null()); |
528 if (!intermediates.get()) | 571 if (!intermediates.get()) |
(...skipping 28 matching lines...) Expand all Loading... |
557 // Currently we only ues OpenSSL's default root CA paths, so treat all | 600 // Currently we only ues OpenSSL's default root CA paths, so treat all |
558 // correctly verified certs as being from a known root. TODO(joth): if the | 601 // correctly verified certs as being from a known root. TODO(joth): if the |
559 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev
ision=80778 | 602 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev
ision=80778 |
560 // become an issue on OpenSSL builds, we will need to embed a hardcoded list | 603 // become an issue on OpenSSL builds, we will need to embed a hardcoded list |
561 // of well known root CAs, as per the _mac and _win versions. | 604 // of well known root CAs, as per the _mac and _win versions. |
562 verify_result->is_issued_by_known_root = true; | 605 verify_result->is_issued_by_known_root = true; |
563 | 606 |
564 return OK; | 607 return OK; |
565 } | 608 } |
566 | 609 |
567 #endif // !defined(OS_ANDROID) | 610 #endif // defined(OS_ANDROID) |
568 | 611 |
569 // static | 612 // static |
570 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, | 613 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, |
571 std::string* encoded) { | 614 std::string* encoded) { |
572 DERCache der_cache; | 615 DERCache der_cache; |
573 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 616 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
574 return false; | 617 return false; |
575 encoded->assign(reinterpret_cast<const char*>(der_cache.data), | 618 encoded->assign(reinterpret_cast<const char*>(der_cache.data), |
576 der_cache.data_length); | 619 der_cache.data_length); |
577 return true; | 620 return true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 Pickle* pickle) { | 655 Pickle* pickle) { |
613 DERCache der_cache; | 656 DERCache der_cache; |
614 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 657 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
615 return false; | 658 return false; |
616 | 659 |
617 return pickle->WriteData( | 660 return pickle->WriteData( |
618 reinterpret_cast<const char*>(der_cache.data), | 661 reinterpret_cast<const char*>(der_cache.data), |
619 der_cache.data_length); | 662 der_cache.data_length); |
620 } | 663 } |
621 | 664 |
| 665 #if defined(OS_ANDROID) |
| 666 void X509Certificate::GetChainDEREncodedBytes( |
| 667 std::vector<std::string>* chain_bytes) const { |
| 668 OSCertHandles cert_handles(intermediate_ca_certs_); |
| 669 // Make sure the peer's own cert is the first in the chain, if it's not |
| 670 // already there. |
| 671 if (cert_handles.empty() || cert_handles[0] != cert_handle_) |
| 672 cert_handles.insert(cert_handles.begin(), cert_handle_); |
| 673 |
| 674 chain_bytes->reserve(cert_handles.size()); |
| 675 for (OSCertHandles::const_iterator it = cert_handles.begin(); |
| 676 it != cert_handles.end(); ++it) { |
| 677 std::string cert_bytes; |
| 678 GetDEREncoded(*it, &cert_bytes); |
| 679 chain_bytes->push_back(cert_bytes); |
| 680 } |
| 681 } |
| 682 #endif |
| 683 |
622 } // namespace net | 684 } // namespace net |
OLD | NEW |