| 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/socket/ssl_client_socket_impl.h" | 5 #include "net/socket/ssl_client_socket_impl.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); | 474 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
| 475 return *this; | 475 return *this; |
| 476 } | 476 } |
| 477 | 477 |
| 478 void SSLClientSocketImpl::PeerCertificateChain::Reset(STACK_OF(X509) * chain) { | 478 void SSLClientSocketImpl::PeerCertificateChain::Reset(STACK_OF(X509) * chain) { |
| 479 openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); | 479 openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); |
| 480 } | 480 } |
| 481 | 481 |
| 482 scoped_refptr<X509Certificate> | 482 scoped_refptr<X509Certificate> |
| 483 SSLClientSocketImpl::PeerCertificateChain::AsOSChain() const { | 483 SSLClientSocketImpl::PeerCertificateChain::AsOSChain() const { |
| 484 // DER-encode the chain and convert to a platform certificate handle. | 484 #if defined(USE_OPENSSL_CERTS) |
| 485 std::vector<std::string> chain; | 485 // When OSCertHandle is typedef'ed to X509, this implementation does a short |
| 486 chain.reserve(sk_X509_num(openssl_chain_.get())); | 486 // cut to avoid converting back and forth between DER and the X509 struct. |
| 487 X509Certificate::OSCertHandles intermediates; |
| 488 for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 489 X509* cert = sk_X509_value(openssl_chain_.get(), i); |
| 490 DCHECK(cert->buf); |
| 491 intermediates.push_back(cert); |
| 492 } |
| 493 |
| 494 X509* leaf = sk_X509_value(openssl_chain_.get(), 0); |
| 495 DCHECK(leaf->buf); |
| 496 return X509Certificate::CreateFromHandle(leaf, intermediates); |
| 497 #else |
| 498 // Convert the certificate chains to a platform certificate handle. |
| 499 std::vector<base::StringPiece> der_chain; |
| 500 der_chain.reserve(sk_X509_num(openssl_chain_.get())); |
| 487 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | 501 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 488 X509* x = sk_X509_value(openssl_chain_.get(), i); | 502 X509* cert = sk_X509_value(openssl_chain_.get(), i); |
| 489 // Note: This intentionally avoids using x509_util::GetDER(), which may | 503 DCHECK(cert->buf); |
| 490 // cache the encoded DER on |x|, as |x| is shared with the underlying | 504 base::StringPiece der; |
| 491 // socket (SSL*) this chain belongs to. As the DER will only be used | 505 if (!x509_util::GetDER(cert, &der)) |
| 492 // once in //net, within this code, this avoids needlessly caching | |
| 493 // additional data. See https://crbug.com/642082 | |
| 494 int len = i2d_X509(x, nullptr); | |
| 495 if (len < 0) | |
| 496 return nullptr; | 506 return nullptr; |
| 497 std::string cert; | 507 der_chain.push_back(der); |
| 498 uint8_t* ptr = reinterpret_cast<uint8_t*>(base::WriteInto(&cert, len + 1)); | |
| 499 len = i2d_X509(x, &ptr); | |
| 500 if (len < 0) { | |
| 501 NOTREACHED(); | |
| 502 return nullptr; | |
| 503 } | |
| 504 chain.push_back(std::move(cert)); | |
| 505 } | 508 } |
| 506 std::vector<base::StringPiece> stringpiece_chain; | 509 return X509Certificate::CreateFromDERCertChain(der_chain); |
| 507 for (const auto& cert : chain) | 510 #endif |
| 508 stringpiece_chain.push_back(cert); | |
| 509 | |
| 510 return X509Certificate::CreateFromDERCertChain(stringpiece_chain); | |
| 511 } | 511 } |
| 512 | 512 |
| 513 // static | 513 // static |
| 514 void SSLClientSocket::ClearSessionCache() { | 514 void SSLClientSocket::ClearSessionCache() { |
| 515 SSLClientSocketImpl::SSLContext* context = | 515 SSLClientSocketImpl::SSLContext* context = |
| 516 SSLClientSocketImpl::SSLContext::GetInstance(); | 516 SSLClientSocketImpl::SSLContext::GetInstance(); |
| 517 context->session_cache()->Flush(); | 517 context->session_cache()->Flush(); |
| 518 } | 518 } |
| 519 | 519 |
| 520 SSLClientSocketImpl::SSLClientSocketImpl( | 520 SSLClientSocketImpl::SSLClientSocketImpl( |
| (...skipping 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2051 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED && | 2051 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED && |
| 2052 !certificate_requested_) { | 2052 !certificate_requested_) { |
| 2053 net_error = ERR_SSL_PROTOCOL_ERROR; | 2053 net_error = ERR_SSL_PROTOCOL_ERROR; |
| 2054 } | 2054 } |
| 2055 } | 2055 } |
| 2056 | 2056 |
| 2057 return net_error; | 2057 return net_error; |
| 2058 } | 2058 } |
| 2059 | 2059 |
| 2060 } // namespace net | 2060 } // namespace net |
| OLD | NEW |