Chromium Code Reviews| 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 <openssl/err.h> | 10 #include <openssl/err.h> |
| 11 #include <openssl/opensslv.h> | 11 #include <openssl/opensslv.h> |
| 12 #include <openssl/ssl.h> | 12 #include <openssl/ssl.h> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback_helpers.h" | 15 #include "base/callback_helpers.h" |
| 16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "crypto/ec_private_key.h" | 19 #include "crypto/ec_private_key.h" |
| 20 #include "crypto/openssl_util.h" | 20 #include "crypto/openssl_util.h" |
| 21 #include "crypto/scoped_openssl_types.h" | 21 #include "crypto/scoped_openssl_types.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/cert/cert_verifier.h" | 23 #include "net/cert/cert_verifier.h" |
| 24 #include "net/cert/single_request_cert_verifier.h" | 24 #include "net/cert/single_request_cert_verifier.h" |
| 25 #include "net/cert/x509_certificate_net_log_param.h" | 25 #include "net/cert/x509_certificate_net_log_param.h" |
| 26 #include "net/socket/openssl_ssl_util.h" | |
| 27 #include "net/socket/ssl_error_params.h" | 26 #include "net/socket/ssl_error_params.h" |
| 28 #include "net/socket/ssl_session_cache_openssl.h" | 27 #include "net/socket/ssl_session_cache_openssl.h" |
| 29 #include "net/ssl/openssl_client_key_store.h" | 28 #include "net/ssl/openssl_ssl_util.h" |
| 30 #include "net/ssl/ssl_cert_request_info.h" | 29 #include "net/ssl/ssl_cert_request_info.h" |
| 31 #include "net/ssl/ssl_connection_status_flags.h" | 30 #include "net/ssl/ssl_connection_status_flags.h" |
| 32 #include "net/ssl/ssl_info.h" | 31 #include "net/ssl/ssl_info.h" |
| 33 | 32 |
| 33 #if defined(USE_OPENSSL_CERTS) | |
| 34 #include "net/ssl/openssl_client_key_store.h" | |
| 35 #else // !defined(USE_OPENSSL_CERTS) | |
| 36 #include "net/ssl/openssl_platform_key.h" | |
| 37 #endif // defined(USE_OPENSSL_CERTS) | |
|
wtc
2014/07/16 00:09:58
Nit: I would omit the comments on lines 35 and 37
davidben
2014/07/16 16:25:35
Done.
| |
| 38 | |
| 34 namespace net { | 39 namespace net { |
| 35 | 40 |
| 36 namespace { | 41 namespace { |
| 37 | 42 |
| 38 // Enable this to see logging for state machine state transitions. | 43 // Enable this to see logging for state machine state transitions. |
| 39 #if 0 | 44 #if 0 |
| 40 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ | 45 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
| 41 " jump to state " << s; \ | 46 " jump to state " << s; \ |
| 42 next_handshake_state_ = s; } while (0) | 47 next_handshake_state_ = s; } while (0) |
| 43 #else | 48 #else |
| (...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1328 return result; | 1333 return result; |
| 1329 } | 1334 } |
| 1330 | 1335 |
| 1331 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, | 1336 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, |
| 1332 X509** x509, | 1337 X509** x509, |
| 1333 EVP_PKEY** pkey) { | 1338 EVP_PKEY** pkey) { |
| 1334 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; | 1339 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1335 DCHECK(ssl == ssl_); | 1340 DCHECK(ssl == ssl_); |
| 1336 DCHECK(*x509 == NULL); | 1341 DCHECK(*x509 == NULL); |
| 1337 DCHECK(*pkey == NULL); | 1342 DCHECK(*pkey == NULL); |
| 1343 | |
| 1344 #if defined(OS_IOS) | |
| 1345 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). | |
| 1346 LOG(WARNING) << "Client auth is not supported"; | |
| 1347 #else // !defined(OS_IOS) | |
| 1338 if (!ssl_config_.send_client_cert) { | 1348 if (!ssl_config_.send_client_cert) { |
| 1339 // First pass: we know that a client certificate is needed, but we do not | 1349 // First pass: we know that a client certificate is needed, but we do not |
| 1340 // have one at hand. | 1350 // have one at hand. |
| 1341 client_auth_cert_needed_ = true; | 1351 client_auth_cert_needed_ = true; |
| 1342 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); | 1352 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
| 1343 for (int i = 0; i < sk_X509_NAME_num(authorities); i++) { | 1353 for (int i = 0; i < sk_X509_NAME_num(authorities); i++) { |
| 1344 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); | 1354 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
| 1345 unsigned char* str = NULL; | 1355 unsigned char* str = NULL; |
| 1346 int length = i2d_X509_NAME(ca_name, &str); | 1356 int length = i2d_X509_NAME(ca_name, &str); |
| 1347 cert_authorities_.push_back(std::string( | 1357 cert_authorities_.push_back(std::string( |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1366 if (ssl_config_.client_cert.get()) { | 1376 if (ssl_config_.client_cert.get()) { |
| 1367 // TODO(davidben): Configure OpenSSL to also send the intermediates. | 1377 // TODO(davidben): Configure OpenSSL to also send the intermediates. |
| 1368 ScopedX509 leaf_x509 = | 1378 ScopedX509 leaf_x509 = |
| 1369 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | 1379 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
| 1370 if (!leaf_x509) { | 1380 if (!leaf_x509) { |
| 1371 LOG(WARNING) << "Failed to import certificate"; | 1381 LOG(WARNING) << "Failed to import certificate"; |
| 1372 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1382 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1373 return -1; | 1383 return -1; |
| 1374 } | 1384 } |
| 1375 | 1385 |
| 1376 crypto::ScopedEVP_PKEY privkey; | 1386 // TODO(davidben): With Linux client auth support, this should be |
| 1387 // conditioned on OS_ANDROID and then, with https://crbug.com/394131, | |
| 1388 // removed altogether. OpenSSLClientKeyStore is mostly an artifact of the | |
| 1389 // net/ client auth API lacking a private key handle. | |
| 1377 #if defined(USE_OPENSSL_CERTS) | 1390 #if defined(USE_OPENSSL_CERTS) |
| 1378 // A note about ownership: FetchClientCertPrivateKey() increments | 1391 crypto::ScopedEVP_PKEY privkey = |
| 1379 // the reference count of the EVP_PKEY. Ownership of this reference | 1392 OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
| 1380 // is passed directly to OpenSSL, which will release the reference | 1393 ssl_config_.client_cert.get()); |
| 1381 // using EVP_PKEY_free() when the SSL object is destroyed. | 1394 #else // !defined(USE_OPENSSL_CERTS) |
| 1382 if (!OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( | 1395 crypto::ScopedEVP_PKEY privkey = |
| 1383 ssl_config_.client_cert.get(), &privkey)) { | 1396 FetchClientCertPrivateKey(ssl_config_.client_cert.get()); |
| 1397 #endif // defined(USE_OPENSSL_CERTS) | |
| 1398 if (!privkey) { | |
| 1384 // Could not find the private key. Fail the handshake and surface an | 1399 // Could not find the private key. Fail the handshake and surface an |
| 1385 // appropriate error to the caller. | 1400 // appropriate error to the caller. |
| 1386 LOG(WARNING) << "Client cert found without private key"; | 1401 LOG(WARNING) << "Client cert found without private key"; |
| 1387 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1402 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1388 return -1; | 1403 return -1; |
| 1389 } | 1404 } |
| 1390 #else // !defined(USE_OPENSSL_CERTS) | |
| 1391 // OS handling of private keys is not yet implemented. | |
| 1392 NOTIMPLEMENTED(); | |
| 1393 return 0; | |
| 1394 #endif // defined(USE_OPENSSL_CERTS) | |
| 1395 | 1405 |
| 1396 // TODO(joth): (copied from NSS) We should wait for server certificate | 1406 // TODO(joth): (copied from NSS) We should wait for server certificate |
| 1397 // verification before sending our credentials. See http://crbug.com/13934 | 1407 // verification before sending our credentials. See http://crbug.com/13934 |
| 1398 *x509 = leaf_x509.release(); | 1408 *x509 = leaf_x509.release(); |
| 1399 *pkey = privkey.release(); | 1409 *pkey = privkey.release(); |
| 1400 return 1; | 1410 return 1; |
| 1401 } | 1411 } |
| 1412 #endif // defined(OS_IOS) | |
| 1402 | 1413 |
| 1403 // Send no client certificate. | 1414 // Send no client certificate. |
| 1404 return 0; | 1415 return 0; |
| 1405 } | 1416 } |
| 1406 | 1417 |
| 1407 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1418 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
| 1408 if (!completed_handshake_) { | 1419 if (!completed_handshake_) { |
| 1409 // If the first handshake hasn't completed then we accept any certificates | 1420 // If the first handshake hasn't completed then we accept any certificates |
| 1410 // because we verify after the handshake. | 1421 // because we verify after the handshake. |
| 1411 return 1; | 1422 return 1; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1473 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1484 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
| 1474 return SSL_TLSEXT_ERR_OK; | 1485 return SSL_TLSEXT_ERR_OK; |
| 1475 } | 1486 } |
| 1476 | 1487 |
| 1477 scoped_refptr<X509Certificate> | 1488 scoped_refptr<X509Certificate> |
| 1478 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1489 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1479 return server_cert_; | 1490 return server_cert_; |
| 1480 } | 1491 } |
| 1481 | 1492 |
| 1482 } // namespace net | 1493 } // namespace net |
| OLD | NEW |