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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 #endif | 45 #endif |
46 | 46 |
47 // This constant can be any non-negative/non-zero value (eg: it does not | 47 // This constant can be any non-negative/non-zero value (eg: it does not |
48 // overlap with any value of the net::Error range, including net::OK). | 48 // overlap with any value of the net::Error range, including net::OK). |
49 const int kNoPendingReadResult = 1; | 49 const int kNoPendingReadResult = 1; |
50 | 50 |
51 // If a client doesn't have a list of protocols that it supports, but | 51 // If a client doesn't have a list of protocols that it supports, but |
52 // the server supports NPN, choosing "http/1.1" is the best answer. | 52 // the server supports NPN, choosing "http/1.1" is the best answer. |
53 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; | 53 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
54 | 54 |
55 typedef crypto::ScopedOpenSSL<X509, X509_free>::Type ScopedX509; | |
56 | |
55 #if OPENSSL_VERSION_NUMBER < 0x1000103fL | 57 #if OPENSSL_VERSION_NUMBER < 0x1000103fL |
56 // This method doesn't seem to have made it into the OpenSSL headers. | 58 // This method doesn't seem to have made it into the OpenSSL headers. |
57 unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } | 59 unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } |
58 #endif | 60 #endif |
59 | 61 |
60 // Used for encoding the |connection_status| field of an SSLInfo object. | 62 // Used for encoding the |connection_status| field of an SSLInfo object. |
61 int EncodeSSLConnectionStatus(int cipher_suite, | 63 int EncodeSSLConnectionStatus(int cipher_suite, |
62 int compression, | 64 int compression, |
63 int version) { | 65 int version) { |
64 return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK) << | 66 return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK) << |
(...skipping 25 matching lines...) Expand all Loading... | |
90 | 92 |
91 // Compute a unique key string for the SSL session cache. |socket| is an | 93 // Compute a unique key string for the SSL session cache. |socket| is an |
92 // input socket object. Return a string. | 94 // input socket object. Return a string. |
93 std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) { | 95 std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) { |
94 std::string result = socket.host_and_port().ToString(); | 96 std::string result = socket.host_and_port().ToString(); |
95 result.append("/"); | 97 result.append("/"); |
96 result.append(socket.ssl_session_cache_shard()); | 98 result.append(socket.ssl_session_cache_shard()); |
97 return result; | 99 return result; |
98 } | 100 } |
99 | 101 |
100 static void FreeX509Stack(STACK_OF(X509) * ptr) { | 102 void FreeX509Stack(STACK_OF(X509) * ptr) { |
101 sk_X509_pop_free(ptr, X509_free); | 103 sk_X509_pop_free(ptr, X509_free); |
102 } | 104 } |
103 | 105 |
106 ScopedX509 OSCertHandleToOpenSSL( | |
107 X509Certificate::OSCertHandle os_handle) { | |
108 #if defined(USE_OPENSSL_CERTS) | |
109 return ScopedX509(X509Certificate::DupOSCertHandle(os_handle)); | |
110 #else // !defined(USE_OPENSSL_CERTS) | |
111 std::string der_encoded; | |
112 if (!X509Certificate::GetDEREncoded(os_handle, &der_encoded)) | |
113 return ScopedX509(); | |
114 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_encoded.data()); | |
115 return ScopedX509(d2i_X509(NULL, &bytes, der_encoded.size())); | |
116 #endif // defined(USE_OPENSSL_CERTS) | |
117 } | |
118 | |
104 } // namespace | 119 } // namespace |
105 | 120 |
106 class SSLClientSocketOpenSSL::SSLContext { | 121 class SSLClientSocketOpenSSL::SSLContext { |
107 public: | 122 public: |
108 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 123 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
109 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 124 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
110 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 125 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
111 | 126 |
112 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 127 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
113 DCHECK(ssl); | 128 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++) { | 1357 for (size_t i = 0; i < num_client_cert_types; i++) { |
1343 cert_key_types_.push_back( | 1358 cert_key_types_.push_back( |
1344 static_cast<SSLClientCertType>(client_cert_types[i])); | 1359 static_cast<SSLClientCertType>(client_cert_types[i])); |
1345 } | 1360 } |
1346 | 1361 |
1347 return -1; // Suspends handshake. | 1362 return -1; // Suspends handshake. |
1348 } | 1363 } |
1349 | 1364 |
1350 // Second pass: a client certificate should have been selected. | 1365 // Second pass: a client certificate should have been selected. |
1351 if (ssl_config_.client_cert.get()) { | 1366 if (ssl_config_.client_cert.get()) { |
1367 // TODO(davidben): Configure OpenSSL to also send the intermediates. | |
1368 ScopedX509 leaf_x509 = | |
1369 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | |
1370 if (!leaf_x509) { | |
1371 LOG(WARNING) << "Failed to import certificate"; | |
1372 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | |
Ryan Sleevi
2014/07/11 18:20:14
Update NSS to use this code too?
davidben
2014/07/11 18:32:20
Hrm. So it doesn't look like NSS actually gives yo
| |
1373 return -1; | |
1374 } | |
1375 | |
1376 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; | |
1352 #if defined(USE_OPENSSL_CERTS) | 1377 #if defined(USE_OPENSSL_CERTS) |
1353 // A note about ownership: FetchClientCertPrivateKey() increments | 1378 // A note about ownership: FetchClientCertPrivateKey() increments |
1354 // the reference count of the EVP_PKEY. Ownership of this reference | 1379 // the reference count of the EVP_PKEY. Ownership of this reference |
1355 // is passed directly to OpenSSL, which will release the reference | 1380 // is passed directly to OpenSSL, which will release the reference |
1356 // using EVP_PKEY_free() when the SSL object is destroyed. | 1381 // using EVP_PKEY_free() when the SSL object is destroyed. |
1357 OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; | 1382 if (!OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
1358 if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( | |
1359 ssl_config_.client_cert.get(), &privkey)) { | 1383 ssl_config_.client_cert.get(), &privkey)) { |
1360 // TODO(joth): (copied from NSS) We should wait for server certificate | 1384 // Could not find the private key. Fail the handshake and surface an |
1361 // verification before sending our credentials. See http://crbug.com/13934 | 1385 // appropriate error to the caller. |
1362 *x509 = X509Certificate::DupOSCertHandle( | 1386 LOG(WARNING) << "Client cert found without private key"; |
1363 ssl_config_.client_cert->os_cert_handle()); | 1387 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1364 *pkey = privkey.release(); | 1388 return -1; |
1365 return 1; | |
1366 } | 1389 } |
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) | |
1367 | 1395 |
1368 // Could not find the private key. Fail the handshake and surface an | 1396 // TODO(joth): (copied from NSS) We should wait for server certificate |
1369 // appropriate error to the caller. | 1397 // verification before sending our credentials. See http://crbug.com/13934 |
1370 LOG(WARNING) << "Client cert found without private key"; | 1398 *x509 = leaf_x509.release(); |
1371 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1399 *pkey = privkey.release(); |
1372 return -1; | 1400 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 } | 1401 } |
1378 | 1402 |
1379 // Send no client certificate. | 1403 // Send no client certificate. |
1380 return 0; | 1404 return 0; |
1381 } | 1405 } |
1382 | 1406 |
1383 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1407 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
1384 if (!completed_handshake_) { | 1408 if (!completed_handshake_) { |
1385 // If the first handshake hasn't completed then we accept any certificates | 1409 // If the first handshake hasn't completed then we accept any certificates |
1386 // because we verify after the handshake. | 1410 // 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_; | 1473 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
1450 return SSL_TLSEXT_ERR_OK; | 1474 return SSL_TLSEXT_ERR_OK; |
1451 } | 1475 } |
1452 | 1476 |
1453 scoped_refptr<X509Certificate> | 1477 scoped_refptr<X509Certificate> |
1454 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1478 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1455 return server_cert_; | 1479 return server_cert_; |
1456 } | 1480 } |
1457 | 1481 |
1458 } // namespace net | 1482 } // namespace net |
OLD | NEW |