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 // OpenSSL binding for SSLClientSocket. The class layout and general principle | 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle |
6 // of operation is derived from SSLClientSocketNSS. | 6 // of operation is derived from SSLClientSocketNSS. |
7 | 7 |
8 #include "net/socket/ssl_client_socket_openssl.h" | 8 #include "net/socket/ssl_client_socket_openssl.h" |
9 | 9 |
10 #include <errno.h> | 10 #include <errno.h> |
11 #include <openssl/bio.h> | 11 #include <openssl/bio.h> |
12 #include <openssl/err.h> | 12 #include <openssl/err.h> |
13 #include <openssl/mem.h> | 13 #include <openssl/mem.h> |
14 #include <openssl/ssl.h> | 14 #include <openssl/ssl.h> |
15 #include <string.h> | 15 #include <string.h> |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/callback_helpers.h" | 18 #include "base/callback_helpers.h" |
19 #include "base/environment.h" | |
20 #include "base/lazy_instance.h" | 19 #include "base/lazy_instance.h" |
21 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
22 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
23 #include "base/profiler/scoped_tracker.h" | 22 #include "base/profiler/scoped_tracker.h" |
24 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
25 #include "base/strings/string_piece.h" | 24 #include "base/strings/string_piece.h" |
26 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
27 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
28 #include "base/threading/thread_local.h" | 27 #include "base/threading/thread_local.h" |
29 #include "base/values.h" | 28 #include "base/values.h" |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 // It would be better if the callback were not a global setting, | 237 // It would be better if the callback were not a global setting, |
239 // but that is an OpenSSL issue. | 238 // but that is an OpenSSL issue. |
240 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, | 239 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
241 NULL); | 240 NULL); |
242 | 241 |
243 // Disable the internal session cache. Session caching is handled | 242 // Disable the internal session cache. Session caching is handled |
244 // externally (i.e. by SSLClientSessionCacheOpenSSL). | 243 // externally (i.e. by SSLClientSessionCacheOpenSSL). |
245 SSL_CTX_set_session_cache_mode( | 244 SSL_CTX_set_session_cache_mode( |
246 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); | 245 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
247 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); | 246 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
248 | |
249 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
250 std::string ssl_keylog_file; | |
251 if (env->GetVar("SSLKEYLOGFILE", &ssl_keylog_file) && | |
252 !ssl_keylog_file.empty()) { | |
253 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
254 BIO* bio = BIO_new_file(ssl_keylog_file.c_str(), "a"); | |
255 if (!bio) { | |
256 LOG(ERROR) << "Failed to open " << ssl_keylog_file; | |
257 ERR_print_errors_cb(&LogErrorCallback, NULL); | |
258 } else { | |
259 SSL_CTX_set_keylog_bio(ssl_ctx_.get(), bio); | |
260 } | |
261 } | |
262 } | 247 } |
263 | 248 |
264 static int ClientCertRequestCallback(SSL* ssl, void* arg) { | 249 static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
265 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 250 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
266 DCHECK(socket); | 251 DCHECK(socket); |
267 return socket->ClientCertRequestCallback(ssl); | 252 return socket->ClientCertRequestCallback(ssl); |
268 } | 253 } |
269 | 254 |
270 static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { | 255 static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
271 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( | 256 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 policy_enforcer_(context.cert_policy_enforcer), | 450 policy_enforcer_(context.cert_policy_enforcer), |
466 net_log_(transport_->socket()->NetLog()), | 451 net_log_(transport_->socket()->NetLog()), |
467 weak_factory_(this) { | 452 weak_factory_(this) { |
468 DCHECK(cert_verifier_); | 453 DCHECK(cert_verifier_); |
469 } | 454 } |
470 | 455 |
471 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 456 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
472 Disconnect(); | 457 Disconnect(); |
473 } | 458 } |
474 | 459 |
| 460 void SSLClientSocketOpenSSL::SetSSLKeyLogFile( |
| 461 const std::string& ssl_keylog_file) { |
| 462 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 463 BIO* bio = BIO_new_file(ssl_keylog_file.c_str(), "a"); |
| 464 if (!bio) { |
| 465 LOG(ERROR) << "Failed to open " << ssl_keylog_file; |
| 466 ERR_print_errors_cb(&LogErrorCallback, NULL); |
| 467 } else { |
| 468 SSL_CTX_set_keylog_bio(SSLContext::GetInstance()->ssl_ctx(), bio); |
| 469 } |
| 470 } |
| 471 |
475 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 472 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
476 SSLCertRequestInfo* cert_request_info) { | 473 SSLCertRequestInfo* cert_request_info) { |
477 cert_request_info->host_and_port = host_and_port_; | 474 cert_request_info->host_and_port = host_and_port_; |
478 cert_request_info->cert_authorities = cert_authorities_; | 475 cert_request_info->cert_authorities = cert_authorities_; |
479 cert_request_info->cert_key_types = cert_key_types_; | 476 cert_request_info->cert_key_types = cert_key_types_; |
480 } | 477 } |
481 | 478 |
482 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( | 479 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
483 std::string* proto) const { | 480 std::string* proto) const { |
484 *proto = npn_proto_; | 481 *proto = npn_proto_; |
(...skipping 1682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2167 OnHandshakeIOComplete(signature_result_); | 2164 OnHandshakeIOComplete(signature_result_); |
2168 return; | 2165 return; |
2169 } | 2166 } |
2170 | 2167 |
2171 // During a renegotiation, either Read or Write calls may be blocked on an | 2168 // During a renegotiation, either Read or Write calls may be blocked on an |
2172 // asynchronous private key operation. | 2169 // asynchronous private key operation. |
2173 PumpReadWriteEvents(); | 2170 PumpReadWriteEvents(); |
2174 } | 2171 } |
2175 | 2172 |
2176 } // namespace net | 2173 } // namespace net |
OLD | NEW |