Chromium Code Reviews| 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 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 ip_addrs->clear(); | 510 ip_addrs->clear(); |
| 506 | 511 |
| 507 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); | 512 ParseSubjectAltName(cert_handle_, dns_names, ip_addrs); |
| 508 } | 513 } |
| 509 | 514 |
| 510 // static | 515 // static |
| 511 X509_STORE* X509Certificate::cert_store() { | 516 X509_STORE* X509Certificate::cert_store() { |
| 512 return X509InitSingleton::GetInstance()->store(); | 517 return X509InitSingleton::GetInstance()->store(); |
| 513 } | 518 } |
| 514 | 519 |
| 515 #if !defined(OS_ANDROID) | 520 #if defined(OS_ANDROID) |
| 516 | |
| 517 int X509Certificate::VerifyInternal(const std::string& hostname, | 521 int X509Certificate::VerifyInternal(const std::string& hostname, |
| 518 int flags, | 522 int flags, |
| 519 CRLSet* crl_set, | 523 CRLSet* crl_set, |
| 524 CertVerifyResult* verify_result) const { | |
| 525 if (!VerifyNameMatch(hostname)) | |
| 526 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | |
| 527 | |
| 528 std::vector<std::string> cert_bytes; | |
| 529 GetChainDEREncodedBytes(&cert_bytes); | |
| 530 | |
| 531 // TODO(joth): Fetch the authentication type from SSL rather than hardcode. | |
| 532 // TODO(jingzhao): Recover the original implementation once we support JNI. | |
| 533 #if 0 | |
| 534 android::VerifyResult result = | |
| 535 android::VerifyX509CertChain(cert_bytes, hostname, "RSA"); | |
| 536 #else | |
| 537 android::VerifyResult result = android::VERIFY_INVOCATION_ERROR; | |
| 538 NOTIMPLEMENTED(); | |
| 539 #endif | |
| 540 switch (result) { | |
| 541 case android::VERIFY_OK: | |
| 542 return OK; | |
|
Ryan Sleevi
2011/11/06 02:39:48
random drive by:
What does it mean if android::Ve
Jing Zhao
2011/11/07 03:42:49
I think Joth is the original author of this functi
joth
2011/11/07 10:42:20
oh yes, good spot.
To be consistent with the !ANDR
jingzhao
2011/11/08 02:02:17
Fixed as you said.
| |
| 543 case android::VERIFY_BAD_HOSTNAME: | |
| 544 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | |
| 545 break; | |
| 546 case android::VERIFY_NO_TRUSTED_ROOT: | |
| 547 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID; | |
| 548 break; | |
| 549 case android::VERIFY_INVOCATION_ERROR: | |
| 550 default: | |
| 551 verify_result->cert_status |= ERR_CERT_INVALID; | |
| 552 break; | |
| 553 } | |
| 554 return MapCertStatusToNetError(verify_result->cert_status); | |
| 555 } | |
| 556 | |
| 557 #else | |
| 558 int X509Certificate::VerifyInternal(const std::string& hostname, | |
| 559 int flags, | |
| 560 CRLSet* crl_set, | |
| 520 CertVerifyResult* verify_result) const { | 561 CertVerifyResult* verify_result) const { |
| 521 if (!VerifyNameMatch(hostname)) | 562 if (!VerifyNameMatch(hostname)) |
| 522 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; | 563 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; |
| 523 | 564 |
| 524 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( | 565 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( |
| 525 X509_STORE_CTX_new()); | 566 X509_STORE_CTX_new()); |
| 526 | 567 |
| 527 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( | 568 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( |
| 528 sk_X509_new_null()); | 569 sk_X509_new_null()); |
| 529 if (!intermediates.get()) | 570 if (!intermediates.get()) |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 558 // Currently we only ues OpenSSL's default root CA paths, so treat all | 599 // Currently we only ues OpenSSL's default root CA paths, so treat all |
| 559 // correctly verified certs as being from a known root. TODO(joth): if the | 600 // correctly verified certs as being from a known root. TODO(joth): if the |
| 560 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev ision=80778 | 601 // motivations described in http://src.chromium.org/viewvc/chrome?view=rev&rev ision=80778 |
| 561 // become an issue on OpenSSL builds, we will need to embed a hardcoded list | 602 // become an issue on OpenSSL builds, we will need to embed a hardcoded list |
| 562 // of well known root CAs, as per the _mac and _win versions. | 603 // of well known root CAs, as per the _mac and _win versions. |
| 563 verify_result->is_issued_by_known_root = true; | 604 verify_result->is_issued_by_known_root = true; |
| 564 | 605 |
| 565 return OK; | 606 return OK; |
| 566 } | 607 } |
| 567 | 608 |
| 568 #endif // !defined(OS_ANDROID) | 609 #endif // defined(OS_ANDROID) |
| 569 | 610 |
| 570 // static | 611 // static |
| 571 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, | 612 bool X509Certificate::GetDEREncoded(X509Certificate::OSCertHandle cert_handle, |
| 572 std::string* encoded) { | 613 std::string* encoded) { |
| 573 DERCache der_cache; | 614 DERCache der_cache; |
| 574 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 615 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
| 575 return false; | 616 return false; |
| 576 encoded->assign(reinterpret_cast<const char*>(der_cache.data), | 617 encoded->assign(reinterpret_cast<const char*>(der_cache.data), |
| 577 der_cache.data_length); | 618 der_cache.data_length); |
| 578 return true; | 619 return true; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 Pickle* pickle) { | 654 Pickle* pickle) { |
| 614 DERCache der_cache; | 655 DERCache der_cache; |
| 615 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 656 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
| 616 return false; | 657 return false; |
| 617 | 658 |
| 618 return pickle->WriteData( | 659 return pickle->WriteData( |
| 619 reinterpret_cast<const char*>(der_cache.data), | 660 reinterpret_cast<const char*>(der_cache.data), |
| 620 der_cache.data_length); | 661 der_cache.data_length); |
| 621 } | 662 } |
| 622 | 663 |
| 664 #if defined(OS_ANDROID) | |
| 665 void X509Certificate::GetChainDEREncodedBytes( | |
| 666 std::vector<std::string>* chain_bytes) const { | |
| 667 OSCertHandles cert_handles(intermediate_ca_certs_); | |
|
Ryan Sleevi
2011/11/06 02:39:48
Two things:
1) Why not implement this in terms of
joth
2011/11/07 10:42:20
I think it's just GetDEREncoded didn't exist when
jingzhao
2011/11/08 02:02:17
This change seems small so I made it.. Please take
joth
2011/11/08 10:05:23
Thanks, this looks OK.
For my own record, when we
Ryan Sleevi
2011/11/08 15:03:23
Even if not unlined to the callsite, you can move
| |
| 668 // Make sure the peer's own cert is the first in the chain, if it's not | |
| 669 // already there. | |
| 670 if (cert_handles.empty()) | |
| 671 cert_handles.insert(cert_handles.begin(), cert_handle_); | |
|
Ryan Sleevi
2011/11/06 02:39:48
2) This isn't correct how X509Certificate stores i
joth
2011/11/07 10:42:20
Good spot, thank you. This was broken when it was
jingzhao
2011/11/08 02:02:17
Fixed as you said.
| |
| 672 | |
| 673 chain_bytes->reserve(cert_handles.size()); | |
| 674 for (OSCertHandles::const_iterator it = cert_handles.begin(); | |
| 675 it != cert_handles.end(); ++it) { | |
| 676 DERCache der_cache = {0}; | |
| 677 GetDERAndCacheIfNeeded(*it, &der_cache); | |
| 678 std::string cert_bytes ( | |
| 679 reinterpret_cast<const char*>(der_cache.data), der_cache.data_length); | |
| 680 chain_bytes->push_back(cert_bytes); | |
| 681 } | |
| 682 } | |
| 683 #endif | |
| 684 | |
| 623 } // namespace net | 685 } // namespace net |
| OLD | NEW |