Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: net/socket/ssl_client_socket_openssl.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <openssl/err.h> 10 #include <openssl/err.h>
(...skipping 18 matching lines...) Expand all
29 #include "net/ssl/ssl_cert_request_info.h" 29 #include "net/ssl/ssl_cert_request_info.h"
30 #include "net/ssl/ssl_connection_status_flags.h" 30 #include "net/ssl/ssl_connection_status_flags.h"
31 #include "net/ssl/ssl_info.h" 31 #include "net/ssl/ssl_info.h"
32 32
33 namespace net { 33 namespace net {
34 34
35 namespace { 35 namespace {
36 36
37 // Enable this to see logging for state machine state transitions. 37 // Enable this to see logging for state machine state transitions.
38 #if 0 38 #if 0
39 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ 39 #define GotoState(s) \
40 " jump to state " << s; \ 40 do { \
41 next_handshake_state_ = s; } while (0) 41 DVLOG(2) << (void*) this << " " << __FUNCTION__ << " jump to state " << s; \
42 next_handshake_state_ = s; \
43 } while (0)
42 #else 44 #else
43 #define GotoState(s) next_handshake_state_ = s 45 #define GotoState(s) next_handshake_state_ = s
44 #endif 46 #endif
45 47
46 // This constant can be any non-negative/non-zero value (eg: it does not 48 // This constant can be any non-negative/non-zero value (eg: it does not
47 // overlap with any value of the net::Error range, including net::OK). 49 // overlap with any value of the net::Error range, including net::OK).
48 const int kNoPendingReadResult = 1; 50 const int kNoPendingReadResult = 1;
49 51
50 // If a client doesn't have a list of protocols that it supports, but 52 // If a client doesn't have a list of protocols that it supports, but
51 // the server supports NPN, choosing "http/1.1" is the best answer. 53 // the server supports NPN, choosing "http/1.1" is the best answer.
52 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; 54 const char kDefaultSupportedNPNProtocol[] = "http/1.1";
53 55
54 #if OPENSSL_VERSION_NUMBER < 0x1000103fL 56 #if OPENSSL_VERSION_NUMBER < 0x1000103fL
55 // This method doesn't seem to have made it into the OpenSSL headers. 57 // This method doesn't seem to have made it into the OpenSSL headers.
56 unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } 58 unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) {
59 return cipher->id;
60 }
57 #endif 61 #endif
58 62
59 // Used for encoding the |connection_status| field of an SSLInfo object. 63 // Used for encoding the |connection_status| field of an SSLInfo object.
60 int EncodeSSLConnectionStatus(int cipher_suite, 64 int EncodeSSLConnectionStatus(int cipher_suite, int compression, int version) {
61 int compression, 65 return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK)
62 int version) { 66 << SSL_CONNECTION_CIPHERSUITE_SHIFT) |
63 return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK) << 67 ((compression & SSL_CONNECTION_COMPRESSION_MASK)
64 SSL_CONNECTION_CIPHERSUITE_SHIFT) | 68 << SSL_CONNECTION_COMPRESSION_SHIFT) |
65 ((compression & SSL_CONNECTION_COMPRESSION_MASK) << 69 ((version & SSL_CONNECTION_VERSION_MASK)
66 SSL_CONNECTION_COMPRESSION_SHIFT) | 70 << SSL_CONNECTION_VERSION_SHIFT);
67 ((version & SSL_CONNECTION_VERSION_MASK) <<
68 SSL_CONNECTION_VERSION_SHIFT);
69 } 71 }
70 72
71 // Returns the net SSL version number (see ssl_connection_status_flags.h) for 73 // Returns the net SSL version number (see ssl_connection_status_flags.h) for
72 // this SSL connection. 74 // this SSL connection.
73 int GetNetSSLVersion(SSL* ssl) { 75 int GetNetSSLVersion(SSL* ssl) {
74 switch (SSL_version(ssl)) { 76 switch (SSL_version(ssl)) {
75 case SSL2_VERSION: 77 case SSL2_VERSION:
76 return SSL_CONNECTION_VERSION_SSL2; 78 return SSL_CONNECTION_VERSION_SSL2;
77 case SSL3_VERSION: 79 case SSL3_VERSION:
78 return SSL_CONNECTION_VERSION_SSL3; 80 return SSL_CONNECTION_VERSION_SSL3;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 // error stack if needed. Note that |tracer| is not currently used in the 199 // error stack if needed. Note that |tracer| is not currently used in the
198 // implementation, but is passed in anyway as this ensures the caller will clear 200 // implementation, but is passed in anyway as this ensures the caller will clear
199 // any residual codes left on the error stack. 201 // any residual codes left on the error stack.
200 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) { 202 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) {
201 switch (err) { 203 switch (err) {
202 case SSL_ERROR_WANT_READ: 204 case SSL_ERROR_WANT_READ:
203 case SSL_ERROR_WANT_WRITE: 205 case SSL_ERROR_WANT_WRITE:
204 return ERR_IO_PENDING; 206 return ERR_IO_PENDING;
205 case SSL_ERROR_SYSCALL: 207 case SSL_ERROR_SYSCALL:
206 LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in " 208 LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in "
207 "error queue: " << ERR_peek_error() << ", errno: " 209 "error queue: " << ERR_peek_error() << ", errno: " << errno;
208 << errno;
209 return ERR_SSL_PROTOCOL_ERROR; 210 return ERR_SSL_PROTOCOL_ERROR;
210 case SSL_ERROR_SSL: 211 case SSL_ERROR_SSL:
211 return MapOpenSSLErrorSSL(); 212 return MapOpenSSLErrorSSL();
212 default: 213 default:
213 // TODO(joth): Implement full mapping. 214 // TODO(joth): Implement full mapping.
214 LOG(WARNING) << "Unknown OpenSSL error " << err; 215 LOG(WARNING) << "Unknown OpenSSL error " << err;
215 return ERR_SSL_PROTOCOL_ERROR; 216 return ERR_SSL_PROTOCOL_ERROR;
216 } 217 }
217 } 218 }
218 219
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 DCHECK_NE(ssl_socket_data_index_, -1); 268 DCHECK_NE(ssl_socket_data_index_, -1);
268 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); 269 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method()));
269 session_cache_.Reset(ssl_ctx_.get(), kDefaultSessionCacheConfig); 270 session_cache_.Reset(ssl_ctx_.get(), kDefaultSessionCacheConfig);
270 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); 271 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL);
271 SSL_CTX_set_client_cert_cb(ssl_ctx_.get(), ClientCertCallback); 272 SSL_CTX_set_client_cert_cb(ssl_ctx_.get(), ClientCertCallback);
272 SSL_CTX_set_channel_id_cb(ssl_ctx_.get(), ChannelIDCallback); 273 SSL_CTX_set_channel_id_cb(ssl_ctx_.get(), ChannelIDCallback);
273 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); 274 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL);
274 // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty. 275 // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty.
275 // It would be better if the callback were not a global setting, 276 // It would be better if the callback were not a global setting,
276 // but that is an OpenSSL issue. 277 // but that is an OpenSSL issue.
277 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, 278 SSL_CTX_set_next_proto_select_cb(
278 NULL); 279 ssl_ctx_.get(), SelectNextProtoCallback, NULL);
279 } 280 }
280 281
281 static std::string GetSessionCacheKey(const SSL* ssl) { 282 static std::string GetSessionCacheKey(const SSL* ssl) {
282 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 283 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
283 DCHECK(socket); 284 DCHECK(socket);
284 return GetSocketSessionCacheKey(*socket); 285 return GetSocketSessionCacheKey(*socket);
285 } 286 }
286 287
287 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; 288 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig;
288 289
289 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { 290 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) {
290 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 291 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
291 CHECK(socket); 292 CHECK(socket);
292 return socket->ClientCertRequestCallback(ssl, x509, pkey); 293 return socket->ClientCertRequestCallback(ssl, x509, pkey);
293 } 294 }
294 295
295 static void ChannelIDCallback(SSL* ssl, EVP_PKEY** pkey) { 296 static void ChannelIDCallback(SSL* ssl, EVP_PKEY** pkey) {
296 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 297 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
297 CHECK(socket); 298 CHECK(socket);
298 socket->ChannelIDRequestCallback(ssl, pkey); 299 socket->ChannelIDRequestCallback(ssl, pkey);
299 } 300 }
300 301
301 static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { 302 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg) {
302 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( 303 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(
303 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); 304 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx()));
304 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 305 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
305 CHECK(socket); 306 CHECK(socket);
306 307
307 return socket->CertVerifyCallback(store_ctx); 308 return socket->CertVerifyCallback(store_ctx);
308 } 309 }
309 310
310 static int SelectNextProtoCallback(SSL* ssl, 311 static int SelectNextProtoCallback(SSL* ssl,
311 unsigned char** out, unsigned char* outlen, 312 unsigned char** out,
313 unsigned char* outlen,
312 const unsigned char* in, 314 const unsigned char* in,
313 unsigned int inlen, void* arg) { 315 unsigned int inlen,
316 void* arg) {
314 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 317 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
315 return socket->SelectNextProtoCallback(out, outlen, in, inlen); 318 return socket->SelectNextProtoCallback(out, outlen, in, inlen);
316 } 319 }
317 320
318 // This is the index used with SSL_get_ex_data to retrieve the owner 321 // This is the index used with SSL_get_ex_data to retrieve the owner
319 // SSLClientSocketOpenSSL object from an SSL instance. 322 // SSLClientSocketOpenSSL object from an SSL instance.
320 int ssl_socket_data_index_; 323 int ssl_socket_data_index_;
321 324
322 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; 325 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_;
323 // |session_cache_| must be destroyed before |ssl_ctx_|. 326 // |session_cache_| must be destroyed before |ssl_ctx_|.
324 SSLSessionCacheOpenSSL session_cache_; 327 SSLSessionCacheOpenSSL session_cache_;
325 }; 328 };
326 329
327 // PeerCertificateChain is a helper object which extracts the certificate 330 // PeerCertificateChain is a helper object which extracts the certificate
328 // chain, as given by the server, from an OpenSSL socket and performs the needed 331 // chain, as given by the server, from an OpenSSL socket and performs the needed
329 // resource management. The first element of the chain is the leaf certificate 332 // resource management. The first element of the chain is the leaf certificate
330 // and the other elements are in the order given by the server. 333 // and the other elements are in the order given by the server.
331 class SSLClientSocketOpenSSL::PeerCertificateChain { 334 class SSLClientSocketOpenSSL::PeerCertificateChain {
332 public: 335 public:
333 explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } 336 explicit PeerCertificateChain(STACK_OF(X509) * chain) { Reset(chain); }
334 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } 337 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; }
335 ~PeerCertificateChain() {} 338 ~PeerCertificateChain() {}
336 PeerCertificateChain& operator=(const PeerCertificateChain& other); 339 PeerCertificateChain& operator=(const PeerCertificateChain& other);
337 340
338 // Resets the PeerCertificateChain to the set of certificates in|chain|, 341 // Resets the PeerCertificateChain to the set of certificates in|chain|,
339 // which may be NULL, indicating to empty the store certificates. 342 // which may be NULL, indicating to empty the store certificates.
340 // Note: If an error occurs, such as being unable to parse the certificates, 343 // Note: If an error occurs, such as being unable to parse the certificates,
341 // this will behave as if Reset(NULL) was called. 344 // this will behave as if Reset(NULL) was called.
342 void Reset(STACK_OF(X509)* chain); 345 void Reset(STACK_OF(X509) * chain);
343 346
344 // Note that when USE_OPENSSL is defined, OSCertHandle is X509* 347 // Note that when USE_OPENSSL is defined, OSCertHandle is X509*
345 const scoped_refptr<X509Certificate>& AsOSChain() const { return os_chain_; } 348 const scoped_refptr<X509Certificate>& AsOSChain() const { return os_chain_; }
346 349
347 size_t size() const { 350 size_t size() const {
348 if (!openssl_chain_.get()) 351 if (!openssl_chain_.get())
349 return 0; 352 return 0;
350 return sk_X509_num(openssl_chain_.get()); 353 return sk_X509_num(openssl_chain_.get());
351 } 354 }
352 355
353 X509* operator[](size_t index) const { 356 X509* operator[](size_t index) const {
354 DCHECK_LT(index, size()); 357 DCHECK_LT(index, size());
355 return sk_X509_value(openssl_chain_.get(), index); 358 return sk_X509_value(openssl_chain_.get(), index);
356 } 359 }
357 360
358 bool IsValid() { return os_chain_.get() && openssl_chain_.get(); } 361 bool IsValid() { return os_chain_.get() && openssl_chain_.get(); }
359 362
360 private: 363 private:
361 static void FreeX509Stack(STACK_OF(X509)* cert_chain) { 364 static void FreeX509Stack(STACK_OF(X509) * cert_chain) {
362 sk_X509_pop_free(cert_chain, X509_free); 365 sk_X509_pop_free(cert_chain, X509_free);
363 } 366 }
364 367
365 friend class crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>; 368 friend class crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>;
366 369
367 crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_; 370 crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_;
368 371
369 scoped_refptr<X509Certificate> os_chain_; 372 scoped_refptr<X509Certificate> os_chain_;
370 }; 373 };
371 374
372 SSLClientSocketOpenSSL::PeerCertificateChain& 375 SSLClientSocketOpenSSL::PeerCertificateChain&
373 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( 376 SSLClientSocketOpenSSL::PeerCertificateChain::
374 const PeerCertificateChain& other) { 377 operator=(const PeerCertificateChain& other) {
375 if (this == &other) 378 if (this == &other)
376 return *this; 379 return *this;
377 380
378 // os_chain_ is reference counted by scoped_refptr; 381 // os_chain_ is reference counted by scoped_refptr;
379 os_chain_ = other.os_chain_; 382 os_chain_ = other.os_chain_;
380 383
381 // Must increase the reference count manually for sk_X509_dup 384 // Must increase the reference count manually for sk_X509_dup
382 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); 385 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get()));
383 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { 386 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) {
384 X509* x = sk_X509_value(openssl_chain_.get(), i); 387 X509* x = sk_X509_value(openssl_chain_.get(), i);
385 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 388 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
386 } 389 }
387 return *this; 390 return *this;
388 } 391 }
389 392
390 #if defined(USE_OPENSSL_CERTS) 393 #if defined(USE_OPENSSL_CERTS)
391 // When OSCertHandle is typedef'ed to X509, this implementation does a short cut 394 // When OSCertHandle is typedef'ed to X509, this implementation does a short cut
392 // to avoid converting back and forth between der and X509 struct. 395 // to avoid converting back and forth between der and X509 struct.
393 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( 396 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset(STACK_OF(X509) *
394 STACK_OF(X509)* chain) { 397 chain) {
395 openssl_chain_.reset(NULL); 398 openssl_chain_.reset(NULL);
396 os_chain_ = NULL; 399 os_chain_ = NULL;
397 400
398 if (!chain) 401 if (!chain)
399 return; 402 return;
400 403
401 X509Certificate::OSCertHandles intermediates; 404 X509Certificate::OSCertHandles intermediates;
402 for (int i = 1; i < sk_X509_num(chain); ++i) 405 for (int i = 1; i < sk_X509_num(chain); ++i)
403 intermediates.push_back(sk_X509_value(chain, i)); 406 intermediates.push_back(sk_X509_value(chain, i));
404 407
405 os_chain_ = 408 os_chain_ =
406 X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates); 409 X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates);
407 410
408 // sk_X509_dup does not increase reference count on the certs in the stack. 411 // sk_X509_dup does not increase reference count on the certs in the stack.
409 openssl_chain_.reset(sk_X509_dup(chain)); 412 openssl_chain_.reset(sk_X509_dup(chain));
410 413
411 std::vector<base::StringPiece> der_chain; 414 std::vector<base::StringPiece> der_chain;
412 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { 415 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) {
413 X509* x = sk_X509_value(openssl_chain_.get(), i); 416 X509* x = sk_X509_value(openssl_chain_.get(), i);
414 // Increase the reference count for the certs in openssl_chain_. 417 // Increase the reference count for the certs in openssl_chain_.
415 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 418 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
416 } 419 }
417 } 420 }
418 #else // !defined(USE_OPENSSL_CERTS) 421 #else // !defined(USE_OPENSSL_CERTS)
419 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( 422 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset(STACK_OF(X509) *
420 STACK_OF(X509)* chain) { 423 chain) {
421 openssl_chain_.reset(NULL); 424 openssl_chain_.reset(NULL);
422 os_chain_ = NULL; 425 os_chain_ = NULL;
423 426
424 if (!chain) 427 if (!chain)
425 return; 428 return;
426 429
427 // sk_X509_dup does not increase reference count on the certs in the stack. 430 // sk_X509_dup does not increase reference count on the certs in the stack.
428 openssl_chain_.reset(sk_X509_dup(chain)); 431 openssl_chain_.reset(sk_X509_dup(chain));
429 432
430 std::vector<base::StringPiece> der_chain; 433 std::vector<base::StringPiece> der_chain;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 transport_bio_(NULL), 498 transport_bio_(NULL),
496 transport_(transport_socket.Pass()), 499 transport_(transport_socket.Pass()),
497 host_and_port_(host_and_port), 500 host_and_port_(host_and_port),
498 ssl_config_(ssl_config), 501 ssl_config_(ssl_config),
499 ssl_session_cache_shard_(context.ssl_session_cache_shard), 502 ssl_session_cache_shard_(context.ssl_session_cache_shard),
500 trying_cached_session_(false), 503 trying_cached_session_(false),
501 next_handshake_state_(STATE_NONE), 504 next_handshake_state_(STATE_NONE),
502 npn_status_(kNextProtoUnsupported), 505 npn_status_(kNextProtoUnsupported),
503 channel_id_request_return_value_(ERR_UNEXPECTED), 506 channel_id_request_return_value_(ERR_UNEXPECTED),
504 channel_id_xtn_negotiated_(false), 507 channel_id_xtn_negotiated_(false),
505 net_log_(transport_->socket()->NetLog()) {} 508 net_log_(transport_->socket()->NetLog()) {
509 }
506 510
507 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { 511 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() {
508 Disconnect(); 512 Disconnect();
509 } 513 }
510 514
511 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( 515 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo(
512 SSLCertRequestInfo* cert_request_info) { 516 SSLCertRequestInfo* cert_request_info) {
513 cert_request_info->host_and_port = host_and_port_; 517 cert_request_info->host_and_port = host_and_port_;
514 cert_request_info->cert_authorities = cert_authorities_; 518 cert_request_info->cert_authorities = cert_authorities_;
515 } 519 }
516 520
517 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( 521 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto(
518 std::string* proto, std::string* server_protos) { 522 std::string* proto,
523 std::string* server_protos) {
519 *proto = npn_proto_; 524 *proto = npn_proto_;
520 *server_protos = server_protos_; 525 *server_protos = server_protos_;
521 return npn_status_; 526 return npn_status_;
522 } 527 }
523 528
524 ServerBoundCertService* 529 ServerBoundCertService* SSLClientSocketOpenSSL::GetServerBoundCertService()
525 SSLClientSocketOpenSSL::GetServerBoundCertService() const { 530 const {
526 return server_bound_cert_service_; 531 return server_bound_cert_service_;
527 } 532 }
528 533
529 int SSLClientSocketOpenSSL::ExportKeyingMaterial( 534 int SSLClientSocketOpenSSL::ExportKeyingMaterial(
530 const base::StringPiece& label, 535 const base::StringPiece& label,
531 bool has_context, const base::StringPiece& context, 536 bool has_context,
532 unsigned char* out, unsigned int outlen) { 537 const base::StringPiece& context,
538 unsigned char* out,
539 unsigned int outlen) {
533 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 540 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
534 541
535 int rv = SSL_export_keying_material( 542 int rv = SSL_export_keying_material(
536 ssl_, out, outlen, const_cast<char*>(label.data()), 543 ssl_,
544 out,
545 outlen,
546 const_cast<char*>(label.data()),
537 label.size(), 547 label.size(),
538 reinterpret_cast<unsigned char*>(const_cast<char*>(context.data())), 548 reinterpret_cast<unsigned char*>(const_cast<char*>(context.data())),
539 context.length(), 549 context.length(),
540 context.length() > 0); 550 context.length() > 0);
541 551
542 if (rv != 1) { 552 if (rv != 1) {
543 int ssl_error = SSL_get_error(ssl_, rv); 553 int ssl_error = SSL_get_error(ssl_, rv);
544 LOG(ERROR) << "Failed to export keying material;" 554 LOG(ERROR) << "Failed to export keying material;"
545 << " returned " << rv 555 << " returned " << rv << ", SSL error code " << ssl_error;
546 << ", SSL error code " << ssl_error;
547 return MapOpenSSLError(ssl_error, err_tracer); 556 return MapOpenSSLError(ssl_error, err_tracer);
548 } 557 }
549 return OK; 558 return OK;
550 } 559 }
551 560
552 int SSLClientSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { 561 int SSLClientSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) {
553 return ERR_NOT_IMPLEMENTED; 562 return ERR_NOT_IMPLEMENTED;
554 } 563 }
555 564
556 int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { 565 int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 // Null all callbacks, delete all buffers. 606 // Null all callbacks, delete all buffers.
598 transport_send_busy_ = false; 607 transport_send_busy_ = false;
599 send_buffer_ = NULL; 608 send_buffer_ = NULL;
600 transport_recv_busy_ = false; 609 transport_recv_busy_ = false;
601 transport_recv_eof_ = false; 610 transport_recv_eof_ = false;
602 recv_buffer_ = NULL; 611 recv_buffer_ = NULL;
603 612
604 user_connect_callback_.Reset(); 613 user_connect_callback_.Reset();
605 user_read_callback_.Reset(); 614 user_read_callback_.Reset();
606 user_write_callback_.Reset(); 615 user_write_callback_.Reset();
607 user_read_buf_ = NULL; 616 user_read_buf_ = NULL;
608 user_read_buf_len_ = 0; 617 user_read_buf_len_ = 0;
609 user_write_buf_ = NULL; 618 user_write_buf_ = NULL;
610 user_write_buf_len_ = 0; 619 user_write_buf_len_ = 0;
611 620
612 pending_read_error_ = kNoPendingReadResult; 621 pending_read_error_ = kNoPendingReadResult;
613 transport_write_error_ = OK; 622 transport_write_error_ = OK;
614 623
615 server_cert_verify_result_.Reset(); 624 server_cert_verify_result_.Reset();
616 completed_handshake_ = false; 625 completed_handshake_ = false;
617 626
618 cert_authorities_.clear(); 627 cert_authorities_.clear();
619 client_auth_cert_needed_ = false; 628 client_auth_cert_needed_ = false;
620 } 629 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 698
690 bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { 699 bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) {
691 ssl_info->Reset(); 700 ssl_info->Reset();
692 if (!server_cert_.get()) 701 if (!server_cert_.get())
693 return false; 702 return false;
694 703
695 ssl_info->cert = server_cert_verify_result_.verified_cert; 704 ssl_info->cert = server_cert_verify_result_.verified_cert;
696 ssl_info->cert_status = server_cert_verify_result_.cert_status; 705 ssl_info->cert_status = server_cert_verify_result_.cert_status;
697 ssl_info->is_issued_by_known_root = 706 ssl_info->is_issued_by_known_root =
698 server_cert_verify_result_.is_issued_by_known_root; 707 server_cert_verify_result_.is_issued_by_known_root;
699 ssl_info->public_key_hashes = 708 ssl_info->public_key_hashes = server_cert_verify_result_.public_key_hashes;
700 server_cert_verify_result_.public_key_hashes;
701 ssl_info->client_cert_sent = 709 ssl_info->client_cert_sent =
702 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); 710 ssl_config_.send_client_cert && ssl_config_.client_cert.get();
703 ssl_info->channel_id_sent = WasChannelIDSent(); 711 ssl_info->channel_id_sent = WasChannelIDSent();
704 712
705 RecordChannelIDSupport(server_bound_cert_service_, 713 RecordChannelIDSupport(server_bound_cert_service_,
706 channel_id_xtn_negotiated_, 714 channel_id_xtn_negotiated_,
707 ssl_config_.channel_id_enabled, 715 ssl_config_.channel_id_enabled,
708 crypto::ECPrivateKey::IsSupported()); 716 crypto::ECPrivateKey::IsSupported());
709 717
710 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); 718 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_);
711 CHECK(cipher); 719 CHECK(cipher);
712 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); 720 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL);
713 const COMP_METHOD* compression = SSL_get_current_compression(ssl_); 721 const COMP_METHOD* compression = SSL_get_current_compression(ssl_);
714 722
715 ssl_info->connection_status = EncodeSSLConnectionStatus( 723 ssl_info->connection_status =
716 SSL_CIPHER_get_id(cipher), 724 EncodeSSLConnectionStatus(SSL_CIPHER_get_id(cipher),
717 compression ? compression->type : 0, 725 compression ? compression->type : 0,
718 GetNetSSLVersion(ssl_)); 726 GetNetSSLVersion(ssl_));
719 727
720 bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_); 728 bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_);
721 if (!peer_supports_renego_ext) 729 if (!peer_supports_renego_ext)
722 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; 730 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION;
723 UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported", 731 UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported",
724 implicit_cast<int>(peer_supports_renego_ext), 2); 732 implicit_cast<int>(peer_supports_renego_ext),
733 2);
725 734
726 if (ssl_config_.version_fallback) 735 if (ssl_config_.version_fallback)
727 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; 736 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK;
728 737
729 ssl_info->handshake_type = SSL_session_reused(ssl_) ? 738 ssl_info->handshake_type = SSL_session_reused(ssl_)
730 SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; 739 ? SSLInfo::HANDSHAKE_RESUME
740 : SSLInfo::HANDSHAKE_FULL;
731 741
732 DVLOG(3) << "Encoded connection status: cipher suite = " 742 DVLOG(3) << "Encoded connection status: cipher suite = "
733 << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) 743 << SSLConnectionStatusToCipherSuite(ssl_info->connection_status)
734 << " version = " 744 << " version = "
735 << SSLConnectionStatusToVersion(ssl_info->connection_status); 745 << SSLConnectionStatusToVersion(ssl_info->connection_status);
736 return true; 746 return true;
737 } 747 }
738 748
739 int SSLClientSocketOpenSSL::Read(IOBuffer* buf, 749 int SSLClientSocketOpenSSL::Read(IOBuffer* buf,
740 int buf_len, 750 int buf_len,
741 const CompletionCallback& callback) { 751 const CompletionCallback& callback) {
742 user_read_buf_ = buf; 752 user_read_buf_ = buf;
743 user_read_buf_len_ = buf_len; 753 user_read_buf_len_ = buf_len;
744 754
745 int rv = DoReadLoop(OK); 755 int rv = DoReadLoop(OK);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 859
850 // Removing ciphers by ID from OpenSSL is a bit involved as we must use the 860 // Removing ciphers by ID from OpenSSL is a bit involved as we must use the
851 // textual name with SSL_set_cipher_list because there is no public API to 861 // textual name with SSL_set_cipher_list because there is no public API to
852 // directly remove a cipher by ID. 862 // directly remove a cipher by ID.
853 STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); 863 STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_);
854 DCHECK(ciphers); 864 DCHECK(ciphers);
855 // See SSLConfig::disabled_cipher_suites for description of the suites 865 // See SSLConfig::disabled_cipher_suites for description of the suites
856 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 866 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256
857 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 867 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384
858 // as the handshake hash. 868 // as the handshake hash.
859 std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:" 869 std::string command(
860 "!aECDH:!AESGCM+AES256"); 870 "DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:"
871 "!aECDH:!AESGCM+AES256");
861 // Walk through all the installed ciphers, seeing if any need to be 872 // Walk through all the installed ciphers, seeing if any need to be
862 // appended to the cipher removal |command|. 873 // appended to the cipher removal |command|.
863 for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { 874 for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
864 const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); 875 const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
865 const uint16 id = SSL_CIPHER_get_id(cipher); 876 const uint16 id = SSL_CIPHER_get_id(cipher);
866 // Remove any ciphers with a strength of less than 80 bits. Note the NSS 877 // Remove any ciphers with a strength of less than 80 bits. Note the NSS
867 // implementation uses "effective" bits here but OpenSSL does not provide 878 // implementation uses "effective" bits here but OpenSSL does not provide
868 // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, 879 // this detail. This only impacts Triple DES: reports 112 vs. 168 bits,
869 // both of which are greater than 80 anyway. 880 // both of which are greater than 80 anyway.
870 bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; 881 bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80;
871 if (!disable) { 882 if (!disable) {
872 disable = std::find(ssl_config_.disabled_cipher_suites.begin(), 883 disable = std::find(ssl_config_.disabled_cipher_suites.begin(),
873 ssl_config_.disabled_cipher_suites.end(), id) != 884 ssl_config_.disabled_cipher_suites.end(),
874 ssl_config_.disabled_cipher_suites.end(); 885 id) != ssl_config_.disabled_cipher_suites.end();
875 } 886 }
876 if (disable) { 887 if (disable) {
877 const char* name = SSL_CIPHER_get_name(cipher); 888 const char* name = SSL_CIPHER_get_name(cipher);
878 DVLOG(3) << "Found cipher to remove: '" << name << "', ID: " << id 889 DVLOG(3) << "Found cipher to remove: '" << name << "', ID: " << id
879 << " strength: " << SSL_CIPHER_get_bits(cipher, NULL); 890 << " strength: " << SSL_CIPHER_get_bits(cipher, NULL);
880 command.append(":!"); 891 command.append(":!");
881 command.append(name); 892 command.append(name);
882 } 893 }
883 } 894 }
884 int rv = SSL_set_cipher_list(ssl_, command.c_str()); 895 int rv = SSL_set_cipher_list(ssl_, command.c_str());
885 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. 896 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL.
886 // This will almost certainly result in the socket failing to complete the 897 // This will almost certainly result in the socket failing to complete the
887 // handshake at which point the appropriate error is bubbled up to the client. 898 // handshake at which point the appropriate error is bubbled up to the client.
888 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " 899 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') "
889 "returned " << rv; 900 "returned "
901 << rv;
890 902
891 // TLS channel ids. 903 // TLS channel ids.
892 if (IsChannelIDEnabled(ssl_config_, server_bound_cert_service_)) { 904 if (IsChannelIDEnabled(ssl_config_, server_bound_cert_service_)) {
893 SSL_enable_tls_channel_id(ssl_); 905 SSL_enable_tls_channel_id(ssl_);
894 } 906 }
895 907
896 return true; 908 return true;
897 } 909 }
898 910
899 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { 911 void SSLClientSocketOpenSSL::DoReadCallback(int rv) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 } 964 }
953 } 965 }
954 } else if (rv == 1) { 966 } else if (rv == 1) {
955 if (trying_cached_session_ && logging::DEBUG_MODE) { 967 if (trying_cached_session_ && logging::DEBUG_MODE) {
956 DVLOG(2) << "Result of session reuse for " << host_and_port_.ToString() 968 DVLOG(2) << "Result of session reuse for " << host_and_port_.ToString()
957 << " is: " << (SSL_session_reused(ssl_) ? "Success" : "Fail"); 969 << " is: " << (SSL_session_reused(ssl_) ? "Success" : "Fail");
958 } 970 }
959 // SSL handshake is completed. Let's verify the certificate. 971 // SSL handshake is completed. Let's verify the certificate.
960 const bool got_cert = !!UpdateServerCert(); 972 const bool got_cert = !!UpdateServerCert();
961 DCHECK(got_cert); 973 DCHECK(got_cert);
962 net_log_.AddEvent( 974 net_log_.AddEvent(NetLog::TYPE_SSL_CERTIFICATES_RECEIVED,
963 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, 975 base::Bind(&NetLogX509CertificateCallback,
964 base::Bind(&NetLogX509CertificateCallback, 976 base::Unretained(server_cert_.get())));
965 base::Unretained(server_cert_.get())));
966 GotoState(STATE_VERIFY_CERT); 977 GotoState(STATE_VERIFY_CERT);
967 } else { 978 } else {
968 int ssl_error = SSL_get_error(ssl_, rv); 979 int ssl_error = SSL_get_error(ssl_, rv);
969 980
970 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { 981 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) {
971 // The server supports TLS channel id and the lookup is asynchronous. 982 // The server supports TLS channel id and the lookup is asynchronous.
972 // Retrieve the error from the call to |server_bound_cert_service_|. 983 // Retrieve the error from the call to |server_bound_cert_service_|.
973 net_error = channel_id_request_return_value_; 984 net_error = channel_id_request_return_value_;
974 } else { 985 } else {
975 net_error = MapOpenSSLError(ssl_error, err_tracer); 986 net_error = MapOpenSSLError(ssl_error, err_tracer);
976 } 987 }
977 988
978 // If not done, stay in this state 989 // If not done, stay in this state
979 if (net_error == ERR_IO_PENDING) { 990 if (net_error == ERR_IO_PENDING) {
980 GotoState(STATE_HANDSHAKE); 991 GotoState(STATE_HANDSHAKE);
981 } else { 992 } else {
982 LOG(ERROR) << "handshake failed; returned " << rv 993 LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code "
983 << ", SSL error code " << ssl_error 994 << ssl_error << ", net_error " << net_error;
984 << ", net_error " << net_error; 995 net_log_.AddEvent(NetLog::TYPE_SSL_HANDSHAKE_ERROR,
985 net_log_.AddEvent( 996 CreateNetLogSSLErrorCallback(net_error, ssl_error));
986 NetLog::TYPE_SSL_HANDSHAKE_ERROR,
987 CreateNetLogSSLErrorCallback(net_error, ssl_error));
988 } 997 }
989 } 998 }
990 return net_error; 999 return net_error;
991 } 1000 }
992 1001
993 int SSLClientSocketOpenSSL::DoVerifyCert(int result) { 1002 int SSLClientSocketOpenSSL::DoVerifyCert(int result) {
994 DCHECK(server_cert_.get()); 1003 DCHECK(server_cert_.get());
995 GotoState(STATE_VERIFY_CERT_COMPLETE); 1004 GotoState(STATE_VERIFY_CERT_COMPLETE);
996 1005
997 CertStatus cert_status; 1006 CertStatus cert_status;
(...skipping 27 matching lines...) Expand all
1025 } 1034 }
1026 1035
1027 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { 1036 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) {
1028 verifier_.reset(); 1037 verifier_.reset();
1029 1038
1030 if (result == OK) { 1039 if (result == OK) {
1031 // TODO(joth): Work out if we need to remember the intermediate CA certs 1040 // TODO(joth): Work out if we need to remember the intermediate CA certs
1032 // when the server sends them to us, and do so here. 1041 // when the server sends them to us, and do so here.
1033 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); 1042 SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_);
1034 } else { 1043 } else {
1035 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) 1044 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) << " ("
1036 << " (" << result << ")"; 1045 << result << ")";
1037 } 1046 }
1038 1047
1039 completed_handshake_ = true; 1048 completed_handshake_ = true;
1040 // Exit DoHandshakeLoop and return the result to the caller to Connect. 1049 // Exit DoHandshakeLoop and return the result to the caller to Connect.
1041 DCHECK_EQ(STATE_NONE, next_handshake_state_); 1050 DCHECK_EQ(STATE_NONE, next_handshake_state_);
1042 return result; 1051 return result;
1043 } 1052 }
1044 1053
1045 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { 1054 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) {
1046 if (!user_connect_callback_.is_null()) { 1055 if (!user_connect_callback_.is_null()) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 // to stay in the current state. 1139 // to stay in the current state.
1131 State state = next_handshake_state_; 1140 State state = next_handshake_state_;
1132 GotoState(STATE_NONE); 1141 GotoState(STATE_NONE);
1133 switch (state) { 1142 switch (state) {
1134 case STATE_HANDSHAKE: 1143 case STATE_HANDSHAKE:
1135 rv = DoHandshake(); 1144 rv = DoHandshake();
1136 break; 1145 break;
1137 case STATE_VERIFY_CERT: 1146 case STATE_VERIFY_CERT:
1138 DCHECK(rv == OK); 1147 DCHECK(rv == OK);
1139 rv = DoVerifyCert(rv); 1148 rv = DoVerifyCert(rv);
1140 break; 1149 break;
1141 case STATE_VERIFY_CERT_COMPLETE: 1150 case STATE_VERIFY_CERT_COMPLETE:
1142 rv = DoVerifyCertComplete(rv); 1151 rv = DoVerifyCertComplete(rv);
1143 break; 1152 break;
1144 case STATE_NONE: 1153 case STATE_NONE:
1145 default: 1154 default:
1146 rv = ERR_UNEXPECTED; 1155 rv = ERR_UNEXPECTED;
1147 NOTREACHED() << "unexpected state" << state; 1156 NOTREACHED() << "unexpected state" << state;
1148 break; 1157 break;
1149 } 1158 }
1150 1159
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 } 1197 }
1189 1198
1190 int SSLClientSocketOpenSSL::DoPayloadRead() { 1199 int SSLClientSocketOpenSSL::DoPayloadRead() {
1191 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1200 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1192 1201
1193 int rv; 1202 int rv;
1194 if (pending_read_error_ != kNoPendingReadResult) { 1203 if (pending_read_error_ != kNoPendingReadResult) {
1195 rv = pending_read_error_; 1204 rv = pending_read_error_;
1196 pending_read_error_ = kNoPendingReadResult; 1205 pending_read_error_ = kNoPendingReadResult;
1197 if (rv == 0) { 1206 if (rv == 0) {
1198 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, 1207 net_log_.AddByteTransferEvent(
1199 rv, user_read_buf_->data()); 1208 NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, user_read_buf_->data());
1200 } 1209 }
1201 return rv; 1210 return rv;
1202 } 1211 }
1203 1212
1204 int total_bytes_read = 0; 1213 int total_bytes_read = 0;
1205 do { 1214 do {
1206 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, 1215 rv = SSL_read(ssl_,
1216 user_read_buf_->data() + total_bytes_read,
1207 user_read_buf_len_ - total_bytes_read); 1217 user_read_buf_len_ - total_bytes_read);
1208 if (rv > 0) 1218 if (rv > 0)
1209 total_bytes_read += rv; 1219 total_bytes_read += rv;
1210 } while (total_bytes_read < user_read_buf_len_ && rv > 0); 1220 } while (total_bytes_read < user_read_buf_len_ && rv > 0);
1211 1221
1212 if (total_bytes_read == user_read_buf_len_) { 1222 if (total_bytes_read == user_read_buf_len_) {
1213 rv = total_bytes_read; 1223 rv = total_bytes_read;
1214 } else { 1224 } else {
1215 // Otherwise, an error occurred (rv <= 0). The error needs to be handled 1225 // Otherwise, an error occurred (rv <= 0). The error needs to be handled
1216 // immediately, while the OpenSSL errors are still available in 1226 // immediately, while the OpenSSL errors are still available in
1217 // thread-local storage. However, the handled/remapped error code should 1227 // thread-local storage. However, the handled/remapped error code should
1218 // only be returned if no application data was already read; if it was, the 1228 // only be returned if no application data was already read; if it was, the
1219 // error code should be deferred until the next call of DoPayloadRead. 1229 // error code should be deferred until the next call of DoPayloadRead.
1220 // 1230 //
1221 // If no data was read, |*next_result| will point to the return value of 1231 // If no data was read, |*next_result| will point to the return value of
1222 // this function. If at least some data was read, |*next_result| will point 1232 // this function. If at least some data was read, |*next_result| will point
1223 // to |pending_read_error_|, to be returned in a future call to 1233 // to |pending_read_error_|, to be returned in a future call to
1224 // DoPayloadRead() (e.g.: after the current data is handled). 1234 // DoPayloadRead() (e.g.: after the current data is handled).
1225 int *next_result = &rv; 1235 int* next_result = &rv;
1226 if (total_bytes_read > 0) { 1236 if (total_bytes_read > 0) {
1227 pending_read_error_ = rv; 1237 pending_read_error_ = rv;
1228 rv = total_bytes_read; 1238 rv = total_bytes_read;
1229 next_result = &pending_read_error_; 1239 next_result = &pending_read_error_;
1230 } 1240 }
1231 1241
1232 if (client_auth_cert_needed_) { 1242 if (client_auth_cert_needed_) {
1233 *next_result = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; 1243 *next_result = ERR_SSL_CLIENT_AUTH_CERT_NEEDED;
1234 } else if (*next_result < 0) { 1244 } else if (*next_result < 0) {
1235 int err = SSL_get_error(ssl_, *next_result); 1245 int err = SSL_get_error(ssl_, *next_result);
1236 *next_result = MapOpenSSLError(err, err_tracer); 1246 *next_result = MapOpenSSLError(err, err_tracer);
1237 if (rv > 0 && *next_result == ERR_IO_PENDING) { 1247 if (rv > 0 && *next_result == ERR_IO_PENDING) {
1238 // If at least some data was read from SSL_read(), do not treat 1248 // If at least some data was read from SSL_read(), do not treat
1239 // insufficient data as an error to return in the next call to 1249 // insufficient data as an error to return in the next call to
1240 // DoPayloadRead() - instead, let the call fall through to check 1250 // DoPayloadRead() - instead, let the call fall through to check
1241 // SSL_read() again. This is because DoTransportIO() may complete 1251 // SSL_read() again. This is because DoTransportIO() may complete
1242 // in between the next call to DoPayloadRead(), and thus it is 1252 // in between the next call to DoPayloadRead(), and thus it is
1243 // important to check SSL_read() on subsequent invocations to see 1253 // important to check SSL_read() on subsequent invocations to see
1244 // if a complete record may now be read. 1254 // if a complete record may now be read.
1245 *next_result = kNoPendingReadResult; 1255 *next_result = kNoPendingReadResult;
1246 } 1256 }
1247 } 1257 }
1248 } 1258 }
1249 1259
1250 if (rv >= 0) { 1260 if (rv >= 0) {
1251 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, 1261 net_log_.AddByteTransferEvent(
1252 user_read_buf_->data()); 1262 NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, user_read_buf_->data());
1253 } 1263 }
1254 return rv; 1264 return rv;
1255 } 1265 }
1256 1266
1257 int SSLClientSocketOpenSSL::DoPayloadWrite() { 1267 int SSLClientSocketOpenSSL::DoPayloadWrite() {
1258 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1268 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1259 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); 1269 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_);
1260 1270
1261 if (rv >= 0) { 1271 if (rv >= 0) {
1262 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, 1272 net_log_.AddByteTransferEvent(
1263 user_write_buf_->data()); 1273 NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, user_write_buf_->data());
1264 return rv; 1274 return rv;
1265 } 1275 }
1266 1276
1267 int err = SSL_get_error(ssl_, rv); 1277 int err = SSL_get_error(ssl_, rv);
1268 return MapOpenSSLError(err, err_tracer); 1278 return MapOpenSSLError(err, err_tracer);
1269 } 1279 }
1270 1280
1271 int SSLClientSocketOpenSSL::BufferSend(void) { 1281 int SSLClientSocketOpenSSL::BufferSend(void) {
1272 if (transport_send_busy_) 1282 if (transport_send_busy_)
1273 return ERR_IO_PENDING; 1283 return ERR_IO_PENDING;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 EVP_PKEY** pkey) { 1418 EVP_PKEY** pkey) {
1409 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; 1419 DVLOG(3) << "OpenSSL ClientCertRequestCallback called";
1410 DCHECK(ssl == ssl_); 1420 DCHECK(ssl == ssl_);
1411 DCHECK(*x509 == NULL); 1421 DCHECK(*x509 == NULL);
1412 DCHECK(*pkey == NULL); 1422 DCHECK(*pkey == NULL);
1413 #if defined(USE_OPENSSL_CERTS) 1423 #if defined(USE_OPENSSL_CERTS)
1414 if (!ssl_config_.send_client_cert) { 1424 if (!ssl_config_.send_client_cert) {
1415 // First pass: we know that a client certificate is needed, but we do not 1425 // First pass: we know that a client certificate is needed, but we do not
1416 // have one at hand. 1426 // have one at hand.
1417 client_auth_cert_needed_ = true; 1427 client_auth_cert_needed_ = true;
1418 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); 1428 STACK_OF(X509_NAME)* authorities = SSL_get_client_CA_list(ssl);
1419 for (int i = 0; i < sk_X509_NAME_num(authorities); i++) { 1429 for (int i = 0; i < sk_X509_NAME_num(authorities); i++) {
1420 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); 1430 X509_NAME* ca_name = (X509_NAME*)sk_X509_NAME_value(authorities, i);
1421 unsigned char* str = NULL; 1431 unsigned char* str = NULL;
1422 int length = i2d_X509_NAME(ca_name, &str); 1432 int length = i2d_X509_NAME(ca_name, &str);
1423 cert_authorities_.push_back(std::string( 1433 cert_authorities_.push_back(std::string(
1424 reinterpret_cast<const char*>(str), 1434 reinterpret_cast<const char*>(str), static_cast<size_t>(length)));
1425 static_cast<size_t>(length)));
1426 OPENSSL_free(str); 1435 OPENSSL_free(str);
1427 } 1436 }
1428 1437
1429 return -1; // Suspends handshake. 1438 return -1; // Suspends handshake.
1430 } 1439 }
1431 1440
1432 // Second pass: a client certificate should have been selected. 1441 // Second pass: a client certificate should have been selected.
1433 if (ssl_config_.client_cert.get()) { 1442 if (ssl_config_.client_cert.get()) {
1434 // A note about ownership: FetchClientCertPrivateKey() increments 1443 // A note about ownership: FetchClientCertPrivateKey() increments
1435 // the reference count of the EVP_PKEY. Ownership of this reference 1444 // the reference count of the EVP_PKEY. Ownership of this reference
1436 // is passed directly to OpenSSL, which will release the reference 1445 // is passed directly to OpenSSL, which will release the reference
1437 // using EVP_PKEY_free() when the SSL object is destroyed. 1446 // using EVP_PKEY_free() when the SSL object is destroyed.
1438 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; 1447 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey;
1439 if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( 1448 if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey(
1440 ssl_config_.client_cert.get(), &privkey)) { 1449 ssl_config_.client_cert.get(), &privkey)) {
1441 // TODO(joth): (copied from NSS) We should wait for server certificate 1450 // TODO(joth): (copied from NSS) We should wait for server certificate
1442 // verification before sending our credentials. See http://crbug.com/13934 1451 // verification before sending our credentials. See http://crbug.com/13934
1443 *x509 = X509Certificate::DupOSCertHandle( 1452 *x509 = X509Certificate::DupOSCertHandle(
1444 ssl_config_.client_cert->os_cert_handle()); 1453 ssl_config_.client_cert->os_cert_handle());
1445 *pkey = privkey.release(); 1454 *pkey = privkey.release();
1446 return 1; 1455 return 1;
1447 } 1456 }
1448 LOG(WARNING) << "Client cert found without private key"; 1457 LOG(WARNING) << "Client cert found without private key";
1449 } 1458 }
1450 #else // !defined(USE_OPENSSL_CERTS) 1459 #else // !defined(USE_OPENSSL_CERTS)
1451 // OS handling of client certificates is not yet implemented. 1460 // OS handling of client certificates is not yet implemented.
1452 NOTIMPLEMENTED(); 1461 NOTIMPLEMENTED();
1453 #endif // defined(USE_OPENSSL_CERTS) 1462 #endif // defined(USE_OPENSSL_CERTS)
1454 1463
1455 // Send no client certificate. 1464 // Send no client certificate.
1456 return 0; 1465 return 0;
1457 } 1466 }
1458 1467
1459 void SSLClientSocketOpenSSL::ChannelIDRequestCallback(SSL* ssl, 1468 void SSLClientSocketOpenSSL::ChannelIDRequestCallback(SSL* ssl,
1460 EVP_PKEY** pkey) { 1469 EVP_PKEY** pkey) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1529 const_cast<char*>(kDefaultSupportedNPNProtocol)); 1538 const_cast<char*>(kDefaultSupportedNPNProtocol));
1530 *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; 1539 *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1;
1531 npn_status_ = kNextProtoUnsupported; 1540 npn_status_ = kNextProtoUnsupported;
1532 return SSL_TLSEXT_ERR_OK; 1541 return SSL_TLSEXT_ERR_OK;
1533 } 1542 }
1534 1543
1535 // Assume there's no overlap between our protocols and the server's list. 1544 // Assume there's no overlap between our protocols and the server's list.
1536 npn_status_ = kNextProtoNoOverlap; 1545 npn_status_ = kNextProtoNoOverlap;
1537 1546
1538 // For each protocol in server preference order, see if we support it. 1547 // For each protocol in server preference order, see if we support it.
1539 for (unsigned int i = 0; i < inlen; i += in[i] + 1) { 1548 for (unsigned int i = 0; i < inlen; i += in [i] + 1) {
1540 for (std::vector<std::string>::const_iterator 1549 for (std::vector<std::string>::const_iterator j =
1541 j = ssl_config_.next_protos.begin(); 1550 ssl_config_.next_protos.begin();
1542 j != ssl_config_.next_protos.end(); ++j) { 1551 j != ssl_config_.next_protos.end();
1543 if (in[i] == j->size() && 1552 ++j) {
1544 memcmp(&in[i + 1], j->data(), in[i]) == 0) { 1553 if (in[i] == j->size() && memcmp(&in[i + 1], j->data(), in[i]) == 0) {
1545 // We found a match. 1554 // We found a match.
1546 *out = const_cast<unsigned char*>(in) + i + 1; 1555 *out = const_cast<unsigned char*>(in) + i + 1;
1547 *outlen = in[i]; 1556 *outlen = in[i];
1548 npn_status_ = kNextProtoNegotiated; 1557 npn_status_ = kNextProtoNegotiated;
1549 break; 1558 break;
1550 } 1559 }
1551 } 1560 }
1552 if (npn_status_ == kNextProtoNegotiated) 1561 if (npn_status_ == kNextProtoNegotiated)
1553 break; 1562 break;
1554 } 1563 }
1555 1564
1556 // If we didn't find a protocol, we select the first one from our list. 1565 // If we didn't find a protocol, we select the first one from our list.
1557 if (npn_status_ == kNextProtoNoOverlap) { 1566 if (npn_status_ == kNextProtoNoOverlap) {
1558 *out = reinterpret_cast<uint8*>(const_cast<char*>( 1567 *out = reinterpret_cast<uint8*>(
1559 ssl_config_.next_protos[0].data())); 1568 const_cast<char*>(ssl_config_.next_protos[0].data()));
1560 *outlen = ssl_config_.next_protos[0].size(); 1569 *outlen = ssl_config_.next_protos[0].size();
1561 } 1570 }
1562 1571
1563 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); 1572 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen);
1564 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); 1573 server_protos_.assign(reinterpret_cast<const char*>(in), inlen);
1565 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; 1574 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_;
1566 return SSL_TLSEXT_ERR_OK; 1575 return SSL_TLSEXT_ERR_OK;
1567 } 1576 }
1568 1577
1569 scoped_refptr<X509Certificate> 1578 scoped_refptr<X509Certificate>
1570 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { 1579 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const {
1571 return server_cert_; 1580 return server_cert_;
1572 } 1581 }
1573 1582
1574 } // namespace net 1583 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698