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