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> |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 | 90 |
91 // Compute a unique key string for the SSL session cache. |socket| is an | 91 // Compute a unique key string for the SSL session cache. |socket| is an |
92 // input socket object. Return a string. | 92 // input socket object. Return a string. |
93 std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) { | 93 std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) { |
94 std::string result = socket.host_and_port().ToString(); | 94 std::string result = socket.host_and_port().ToString(); |
95 result.append("/"); | 95 result.append("/"); |
96 result.append(socket.ssl_session_cache_shard()); | 96 result.append(socket.ssl_session_cache_shard()); |
97 return result; | 97 return result; |
98 } | 98 } |
99 | 99 |
100 static void FreeX509Stack(STACK_OF(X509) * ptr) { | 100 void FreeX509Stack(STACK_OF(X509) * ptr) { |
101 sk_X509_pop_free(ptr, X509_free); | 101 sk_X509_pop_free(ptr, X509_free); |
102 } | 102 } |
103 | 103 |
104 crypto::ScopedX509 OSCertHandleToOpenSSL( | |
105 X509Certificate::OSCertHandle os_handle) { | |
106 #if defined(USE_OPENSSL_CERTS) | |
107 return crypto::ScopedX509(X509Certificate::DupOSCertHandle(os_handle)); | |
108 #else // !defined(USE_OPENSSL_CERTS) | |
109 std::string der_encoded; | |
110 if (!X509Certificate::GetDEREncoded(os_handle, &der_encoded)) | |
111 return crypto::ScopedX509(); | |
112 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_encoded.data()); | |
113 return crypto::ScopedX509(d2i_X509(NULL, &bytes, der_encoded.size())); | |
114 #endif // defined(USE_OPENSSL_CERTS) | |
115 } | |
116 | |
104 } // namespace | 117 } // namespace |
105 | 118 |
106 class SSLClientSocketOpenSSL::SSLContext { | 119 class SSLClientSocketOpenSSL::SSLContext { |
107 public: | 120 public: |
108 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 121 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
109 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 122 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
110 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 123 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
111 | 124 |
112 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 125 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
113 DCHECK(ssl); | 126 DCHECK(ssl); |
(...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1342 for (size_t i = 0; i < num_client_cert_types; i++) { | 1355 for (size_t i = 0; i < num_client_cert_types; i++) { |
1343 cert_key_types_.push_back( | 1356 cert_key_types_.push_back( |
1344 static_cast<SSLClientCertType>(client_cert_types[i])); | 1357 static_cast<SSLClientCertType>(client_cert_types[i])); |
1345 } | 1358 } |
1346 | 1359 |
1347 return -1; // Suspends handshake. | 1360 return -1; // Suspends handshake. |
1348 } | 1361 } |
1349 | 1362 |
1350 // Second pass: a client certificate should have been selected. | 1363 // Second pass: a client certificate should have been selected. |
1351 if (ssl_config_.client_cert.get()) { | 1364 if (ssl_config_.client_cert.get()) { |
1365 // TODO(davidben): Configure OpenSSL to also send the intermediates. | |
1366 crypto::ScopedX509 leaf_x509 = | |
1367 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | |
1368 if (!leaf_x509 || true) { | |
1369 LOG(WARNING) << "Failed to import certificate"; | |
1370 return -1; | |
1371 } | |
1372 | |
1373 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; | |
davidben
2014/07/11 00:10:25
This silly thing will be crypto::ScopedEVP_PKEY wi
| |
1352 #if defined(USE_OPENSSL_CERTS) | 1374 #if defined(USE_OPENSSL_CERTS) |
1353 // A note about ownership: FetchClientCertPrivateKey() increments | 1375 // A note about ownership: FetchClientCertPrivateKey() increments |
1354 // the reference count of the EVP_PKEY. Ownership of this reference | 1376 // the reference count of the EVP_PKEY. Ownership of this reference |
1355 // is passed directly to OpenSSL, which will release the reference | 1377 // is passed directly to OpenSSL, which will release the reference |
1356 // using EVP_PKEY_free() when the SSL object is destroyed. | 1378 // using EVP_PKEY_free() when the SSL object is destroyed. |
1357 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; | 1379 if (!OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
1358 if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( | |
1359 ssl_config_.client_cert.get(), &privkey)) { | 1380 ssl_config_.client_cert.get(), &privkey)) { |
1360 // TODO(joth): (copied from NSS) We should wait for server certificate | 1381 // Could not find the private key. Fail the handshake and surface an |
1361 // verification before sending our credentials. See http://crbug.com/13934 | 1382 // appropriate error to the caller. |
1362 *x509 = X509Certificate::DupOSCertHandle( | 1383 LOG(WARNING) << "Client cert found without private key"; |
1363 ssl_config_.client_cert->os_cert_handle()); | 1384 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1364 *pkey = privkey.release(); | 1385 return -1; |
1365 return 1; | |
1366 } | 1386 } |
1387 #else // !defined(USE_OPENSSL_CERTS) | |
1388 // OS handling of private keys is not yet implemented. | |
1389 NOTIMPLEMENTED(); | |
1390 return 0; | |
1391 #endif // defined(USE_OPENSSL_CERTS) | |
1367 | 1392 |
1368 // Could not find the private key. Fail the handshake and surface an | 1393 // TODO(joth): (copied from NSS) We should wait for server certificate |
1369 // appropriate error to the caller. | 1394 // verification before sending our credentials. See http://crbug.com/13934 |
1370 LOG(WARNING) << "Client cert found without private key"; | 1395 *x509 = leaf_x509.release(); |
1371 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1396 *pkey = privkey.release(); |
1372 return -1; | 1397 return 1; |
1373 #else // !defined(USE_OPENSSL_CERTS) | |
1374 // OS handling of client certificates is not yet implemented. | |
1375 NOTIMPLEMENTED(); | |
1376 #endif // defined(USE_OPENSSL_CERTS) | |
1377 } | 1398 } |
1378 | 1399 |
1379 // Send no client certificate. | 1400 // Send no client certificate. |
1380 return 0; | 1401 return 0; |
1381 } | 1402 } |
1382 | 1403 |
1383 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1404 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
1384 if (!completed_handshake_) { | 1405 if (!completed_handshake_) { |
1385 // If the first handshake hasn't completed then we accept any certificates | 1406 // If the first handshake hasn't completed then we accept any certificates |
1386 // because we verify after the handshake. | 1407 // because we verify after the handshake. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1449 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1470 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
1450 return SSL_TLSEXT_ERR_OK; | 1471 return SSL_TLSEXT_ERR_OK; |
1451 } | 1472 } |
1452 | 1473 |
1453 scoped_refptr<X509Certificate> | 1474 scoped_refptr<X509Certificate> |
1454 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1475 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1455 return server_cert_; | 1476 return server_cert_; |
1456 } | 1477 } |
1457 | 1478 |
1458 } // namespace net | 1479 } // namespace net |
OLD | NEW |