| 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/err.h> | 11 #include <openssl/err.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 |
| 36 #include "net/ssl/openssl_platform_key.h" |
| 37 #endif |
| 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 1275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1319 return result; | 1324 return result; |
| 1320 } | 1325 } |
| 1321 | 1326 |
| 1322 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, | 1327 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, |
| 1323 X509** x509, | 1328 X509** x509, |
| 1324 EVP_PKEY** pkey) { | 1329 EVP_PKEY** pkey) { |
| 1325 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; | 1330 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1326 DCHECK(ssl == ssl_); | 1331 DCHECK(ssl == ssl_); |
| 1327 DCHECK(*x509 == NULL); | 1332 DCHECK(*x509 == NULL); |
| 1328 DCHECK(*pkey == NULL); | 1333 DCHECK(*pkey == NULL); |
| 1334 |
| 1335 #if defined(OS_IOS) |
| 1336 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1337 LOG(WARNING) << "Client auth is not supported"; |
| 1338 #else // !defined(OS_IOS) |
| 1329 if (!ssl_config_.send_client_cert) { | 1339 if (!ssl_config_.send_client_cert) { |
| 1330 // First pass: we know that a client certificate is needed, but we do not | 1340 // First pass: we know that a client certificate is needed, but we do not |
| 1331 // have one at hand. | 1341 // have one at hand. |
| 1332 client_auth_cert_needed_ = true; | 1342 client_auth_cert_needed_ = true; |
| 1333 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); | 1343 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
| 1334 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { | 1344 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
| 1335 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); | 1345 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
| 1336 unsigned char* str = NULL; | 1346 unsigned char* str = NULL; |
| 1337 int length = i2d_X509_NAME(ca_name, &str); | 1347 int length = i2d_X509_NAME(ca_name, &str); |
| 1338 cert_authorities_.push_back(std::string( | 1348 cert_authorities_.push_back(std::string( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1356 if (ssl_config_.client_cert.get()) { | 1366 if (ssl_config_.client_cert.get()) { |
| 1357 // TODO(davidben): Configure OpenSSL to also send the intermediates. | 1367 // TODO(davidben): Configure OpenSSL to also send the intermediates. |
| 1358 ScopedX509 leaf_x509 = | 1368 ScopedX509 leaf_x509 = |
| 1359 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | 1369 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
| 1360 if (!leaf_x509) { | 1370 if (!leaf_x509) { |
| 1361 LOG(WARNING) << "Failed to import certificate"; | 1371 LOG(WARNING) << "Failed to import certificate"; |
| 1362 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1372 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1363 return -1; | 1373 return -1; |
| 1364 } | 1374 } |
| 1365 | 1375 |
| 1366 crypto::ScopedEVP_PKEY privkey; | 1376 // TODO(davidben): With Linux client auth support, this should be |
| 1377 // conditioned on OS_ANDROID and then, with https://crbug.com/394131, |
| 1378 // removed altogether. OpenSSLClientKeyStore is mostly an artifact of the |
| 1379 // net/ client auth API lacking a private key handle. |
| 1367 #if defined(USE_OPENSSL_CERTS) | 1380 #if defined(USE_OPENSSL_CERTS) |
| 1368 // A note about ownership: FetchClientCertPrivateKey() increments | 1381 crypto::ScopedEVP_PKEY privkey = |
| 1369 // the reference count of the EVP_PKEY. Ownership of this reference | 1382 OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
| 1370 // is passed directly to OpenSSL, which will release the reference | 1383 ssl_config_.client_cert.get()); |
| 1371 // using EVP_PKEY_free() when the SSL object is destroyed. | 1384 #else // !defined(USE_OPENSSL_CERTS) |
| 1372 if (!OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( | 1385 crypto::ScopedEVP_PKEY privkey = |
| 1373 ssl_config_.client_cert.get(), &privkey)) { | 1386 FetchClientCertPrivateKey(ssl_config_.client_cert.get()); |
| 1387 #endif // defined(USE_OPENSSL_CERTS) |
| 1388 if (!privkey) { |
| 1374 // Could not find the private key. Fail the handshake and surface an | 1389 // Could not find the private key. Fail the handshake and surface an |
| 1375 // appropriate error to the caller. | 1390 // appropriate error to the caller. |
| 1376 LOG(WARNING) << "Client cert found without private key"; | 1391 LOG(WARNING) << "Client cert found without private key"; |
| 1377 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1392 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1378 return -1; | 1393 return -1; |
| 1379 } | 1394 } |
| 1380 #else // !defined(USE_OPENSSL_CERTS) | |
| 1381 // OS handling of private keys is not yet implemented. | |
| 1382 NOTIMPLEMENTED(); | |
| 1383 return 0; | |
| 1384 #endif // defined(USE_OPENSSL_CERTS) | |
| 1385 | 1395 |
| 1386 // TODO(joth): (copied from NSS) We should wait for server certificate | 1396 // TODO(joth): (copied from NSS) We should wait for server certificate |
| 1387 // verification before sending our credentials. See http://crbug.com/13934 | 1397 // verification before sending our credentials. See http://crbug.com/13934 |
| 1388 *x509 = leaf_x509.release(); | 1398 *x509 = leaf_x509.release(); |
| 1389 *pkey = privkey.release(); | 1399 *pkey = privkey.release(); |
| 1390 return 1; | 1400 return 1; |
| 1391 } | 1401 } |
| 1402 #endif // defined(OS_IOS) |
| 1392 | 1403 |
| 1393 // Send no client certificate. | 1404 // Send no client certificate. |
| 1394 return 0; | 1405 return 0; |
| 1395 } | 1406 } |
| 1396 | 1407 |
| 1397 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1408 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
| 1398 if (!completed_handshake_) { | 1409 if (!completed_handshake_) { |
| 1399 // If the first handshake hasn't completed then we accept any certificates | 1410 // If the first handshake hasn't completed then we accept any certificates |
| 1400 // because we verify after the handshake. | 1411 // because we verify after the handshake. |
| 1401 return 1; | 1412 return 1; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1508 return socket->MaybeReplayTransportError( | 1519 return socket->MaybeReplayTransportError( |
| 1509 bio, cmd, argp, argi, argl, retvalue); | 1520 bio, cmd, argp, argi, argl, retvalue); |
| 1510 } | 1521 } |
| 1511 | 1522 |
| 1512 scoped_refptr<X509Certificate> | 1523 scoped_refptr<X509Certificate> |
| 1513 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1524 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1514 return server_cert_; | 1525 return server_cert_; |
| 1515 } | 1526 } |
| 1516 | 1527 |
| 1517 } // namespace net | 1528 } // namespace net |
| OLD | NEW |