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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 ip_addrs->clear(); | 450 ip_addrs->clear(); |
446 | 451 |
447 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); | 452 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); |
448 } | 453 } |
449 | 454 |
450 // static | 455 // static |
451 X509_STORE* X509Certificate::cert_store() { | 456 X509_STORE* X509Certificate::cert_store() { |
452 return X509InitSingleton::GetInstance()->store(); | 457 return X509InitSingleton::GetInstance()->store(); |
453 } | 458 } |
454 | 459 |
455 #if !defined(OS_ANDROID) | 460 #if defined(OS_ANDROID) |
456 | |
457 int X509Certificate::VerifyInternal(const std::string& hostname, | 461 int X509Certificate::VerifyInternal(const std::string& hostname, |
458 int flags, | 462 int flags, |
459 CRLSet* crl_set, | 463 CRLSet* crl_set, |
| 464 CertVerifyResult* verify_result) const { |
| 465 if (!VerifyNameMatch(hostname)) |
| 466 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 467 |
| 468 std::vector<std::string> cert_bytes; |
| 469 GetChainDEREncodedBytes(&cert_bytes); |
| 470 |
| 471 // TODO(joth): Fetch the authentication type from SSL rather than hardcode. |
| 472 // TODO(jingzhao): Recover the original implementation once we support JNI. |
| 473 #if 0 |
| 474 android::VerifyResult result = |
| 475 android::VerifyX509CertChain(cert_bytes, hostname, "RSA"); |
| 476 #else |
| 477 android::VerifyResult result = android::VERIFY_INVOCATION_ERROR; |
| 478 NOTIMPLEMENTED(); |
| 479 #endif |
| 480 switch (result) { |
| 481 case android::VERIFY_OK: |
| 482 return OK; |
| 483 case android::VERIFY_BAD_HOSTNAME: |
| 484 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 485 break; |
| 486 case android::VERIFY_NO_TRUSTED_ROOT: |
| 487 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; |
| 488 break; |
| 489 case android::VERIFY_INVOCATION_ERROR: |
| 490 default: |
| 491 verify_result->cert_status |= ERR_CERT_INVALID; |
| 492 break; |
| 493 } |
| 494 return MapCertStatusToNetError(verify_result->cert_status); |
| 495 } |
| 496 |
| 497 #else |
| 498 int X509Certificate::VerifyInternal(const std::string& hostname, |
| 499 int flags, |
| 500 CRLSet* crl_set, |
460 CertVerifyResult* verify_result) const { | 501 CertVerifyResult* verify_result) const { |
461 if (!VerifyNameMatch(hostname)) | 502 if (!VerifyNameMatch(hostname)) |
462 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | 503 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
463 | 504 |
464 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( | 505 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( |
465 X509_STORE_CTX_new()); | 506 X509_STORE_CTX_new()); |
466 | 507 |
467 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( | 508 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( |
468 sk_X509_new_null()); | 509 sk_X509_new_null()); |
469 if (!intermediates.get()) | 510 if (!intermediates.get()) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 // Currently we only ues OpenSSL's default root CA paths, so treat all | 568 // Currently we only ues OpenSSL's default root CA paths, so treat all |
528 // correctly verified certs as being from a known root. TODO(joth): if the | 569 // correctly verified certs as being from a known root. TODO(joth): if the |
529 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev
ision=80778 | 570 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev
ision=80778 |
530 // become an issue on OpenSSL builds, we will need to embed a hardcoded list | 571 // become an issue on OpenSSL builds, we will need to embed a hardcoded list |
531 // of well known root CAs, as per the _mac and _win versions. | 572 // of well known root CAs, as per the _mac and _win versions. |
532 verify_result->is_issued_by_known_root = true; | 573 verify_result->is_issued_by_known_root = true; |
533 | 574 |
534 return OK; | 575 return OK; |
535 } | 576 } |
536 | 577 |
537 #endif // !defined(OS_ANDROID) | 578 #endif // defined(OS_ANDROID) |
538 | 579 |
539 // static | 580 // static |
540 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, | 581 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, |
541 std::string* encoded) { | 582 std::string* encoded) { |
542 DERCache der_cache; | 583 DERCache der_cache; |
543 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 584 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
544 return false; | 585 return false; |
545 encoded->assign(reinterpret_cast<const char*>(der_cache.data), | 586 encoded->assign(reinterpret_cast<const char*>(der_cache.data), |
546 der_cache.data_length); | 587 der_cache.data_length); |
547 return true; | 588 return true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 Pickle* pickle) { | 623 Pickle* pickle) { |
583 DERCache der_cache; | 624 DERCache der_cache; |
584 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 625 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
585 return false; | 626 return false; |
586 | 627 |
587 return pickle->WriteData( | 628 return pickle->WriteData( |
588 reinterpret_cast<const char*>(der_cache.data), | 629 reinterpret_cast<const char*>(der_cache.data), |
589 der_cache.data_length); | 630 der_cache.data_length); |
590 } | 631 } |
591 | 632 |
| 633 #if defined(OS_ANDROID) |
| 634 void X509Certificate::GetChainDEREncodedBytes( |
| 635 std::vector<std::string>* chain_bytes) const { |
| 636 OSCertHandles cert_handles(intermediate_ca_certs_); |
| 637 // Make sure the peer's own cert is the first in the chain, if it's not |
| 638 // already there. |
| 639 if (cert_handles.empty()) |
| 640 cert_handles.insert(cert_handles.begin(), cert_handle_); |
| 641 |
| 642 chain_bytes->reserve(cert_handles.size()); |
| 643 for (OSCertHandles::const_iterator it = cert_handles.begin(); |
| 644 it != cert_handles.end(); ++it) { |
| 645 DERCache der_cache = {0}; |
| 646 GetDERAndCacheIfNeeded(*it, &der_cache); |
| 647 std::string cert_bytes ( |
| 648 reinterpret_cast<const char*>(der_cache.data), der_cache.data_length); |
| 649 chain_bytes->push_back(cert_bytes); |
| 650 } |
| 651 } |
| 652 #endif |
| 653 |
592 } // namespace net | 654 } // namespace net |
OLD | NEW |