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> |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 | 88 |
89 // TLS extension number use for Token Binding. | 89 // TLS extension number use for Token Binding. |
90 const unsigned int kTbExtNum = 30033; | 90 const unsigned int kTbExtNum = 30033; |
91 | 91 |
92 // Token Binding ProtocolVersions supported. | 92 // Token Binding ProtocolVersions supported. |
93 const uint8_t kTbProtocolVersionMajor = 0; | 93 const uint8_t kTbProtocolVersionMajor = 0; |
94 const uint8_t kTbProtocolVersionMinor = 3; | 94 const uint8_t kTbProtocolVersionMinor = 3; |
95 const uint8_t kTbMinProtocolVersionMajor = 0; | 95 const uint8_t kTbMinProtocolVersionMajor = 0; |
96 const uint8_t kTbMinProtocolVersionMinor = 2; | 96 const uint8_t kTbMinProtocolVersionMinor = 2; |
97 | 97 |
98 void FreeX509Stack(STACK_OF(X509)* ptr) { | |
99 sk_X509_pop_free(ptr, X509_free); | |
100 } | |
101 | |
102 using ScopedX509Stack = crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>; | |
103 | |
104 // Used for encoding the |connection_status| field of an SSLInfo object. | |
105 int EncodeSSLConnectionStatus(uint16_t cipher_suite, | |
106 int compression, | |
107 int version) { | |
108 return cipher_suite | | |
109 ((compression & SSL_CONNECTION_COMPRESSION_MASK) << | |
110 SSL_CONNECTION_COMPRESSION_SHIFT) | | |
111 ((version & SSL_CONNECTION_VERSION_MASK) << | |
112 SSL_CONNECTION_VERSION_SHIFT); | |
113 } | |
114 | |
115 // Returns the net SSL version number (see ssl_connection_status_flags.h) for | |
116 // this SSL connection. | |
117 int GetNetSSLVersion(SSL* ssl) { | |
118 switch (SSL_version(ssl)) { | |
119 case TLS1_VERSION: | |
120 return SSL_CONNECTION_VERSION_TLS1; | |
121 case TLS1_1_VERSION: | |
122 return SSL_CONNECTION_VERSION_TLS1_1; | |
123 case TLS1_2_VERSION: | |
124 return SSL_CONNECTION_VERSION_TLS1_2; | |
125 default: | |
126 NOTREACHED(); | |
127 return SSL_CONNECTION_VERSION_UNKNOWN; | |
128 } | |
129 } | |
130 | |
131 ScopedX509 OSCertHandleToOpenSSL( | |
132 X509Certificate::OSCertHandle os_handle) { | |
133 #if defined(USE_OPENSSL_CERTS) | |
134 return ScopedX509(X509Certificate::DupOSCertHandle(os_handle)); | |
135 #else // !defined(USE_OPENSSL_CERTS) | |
136 std::string der_encoded; | |
137 if (!X509Certificate::GetDEREncoded(os_handle, &der_encoded)) | |
138 return ScopedX509(); | |
139 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_encoded.data()); | |
140 return ScopedX509(d2i_X509(NULL, &bytes, der_encoded.size())); | |
141 #endif // defined(USE_OPENSSL_CERTS) | |
142 } | |
143 | |
144 ScopedX509Stack OSCertHandlesToOpenSSL( | |
145 const X509Certificate::OSCertHandles& os_handles) { | |
146 ScopedX509Stack stack(sk_X509_new_null()); | |
147 for (size_t i = 0; i < os_handles.size(); i++) { | |
148 ScopedX509 x509 = OSCertHandleToOpenSSL(os_handles[i]); | |
149 if (!x509) | |
150 return ScopedX509Stack(); | |
151 sk_X509_push(stack.get(), x509.release()); | |
152 } | |
153 return stack; | |
154 } | |
155 | |
156 bool EVP_MDToPrivateKeyHash(const EVP_MD* md, SSLPrivateKey::Hash* hash) { | 98 bool EVP_MDToPrivateKeyHash(const EVP_MD* md, SSLPrivateKey::Hash* hash) { |
157 switch (EVP_MD_type(md)) { | 99 switch (EVP_MD_type(md)) { |
158 case NID_md5_sha1: | 100 case NID_md5_sha1: |
159 *hash = SSLPrivateKey::Hash::MD5_SHA1; | 101 *hash = SSLPrivateKey::Hash::MD5_SHA1; |
160 return true; | 102 return true; |
161 case NID_sha1: | 103 case NID_sha1: |
162 *hash = SSLPrivateKey::Hash::SHA1; | 104 *hash = SSLPrivateKey::Hash::SHA1; |
163 return true; | 105 return true; |
164 case NID_sha256: | 106 case NID_sha256: |
165 *hash = SSLPrivateKey::Hash::SHA256; | 107 *hash = SSLPrivateKey::Hash::SHA256; |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 bool empty() const { | 390 bool empty() const { |
449 return size() == 0; | 391 return size() == 0; |
450 } | 392 } |
451 | 393 |
452 X509* Get(size_t index) const { | 394 X509* Get(size_t index) const { |
453 DCHECK_LT(index, size()); | 395 DCHECK_LT(index, size()); |
454 return sk_X509_value(openssl_chain_.get(), index); | 396 return sk_X509_value(openssl_chain_.get(), index); |
455 } | 397 } |
456 | 398 |
457 private: | 399 private: |
458 ScopedX509Stack openssl_chain_; | 400 ScopedX509_STACK openssl_chain_; |
459 }; | 401 }; |
460 | 402 |
461 SSLClientSocketOpenSSL::PeerCertificateChain& | 403 SSLClientSocketOpenSSL::PeerCertificateChain& |
462 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( | 404 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( |
463 const PeerCertificateChain& other) { | 405 const PeerCertificateChain& other) { |
464 if (this == &other) | 406 if (this == &other) |
465 return *this; | 407 return *this; |
466 | 408 |
467 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); | 409 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
468 return *this; | 410 return *this; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
800 ssl_info->pinning_failure_log = pinning_failure_log_; | 742 ssl_info->pinning_failure_log = pinning_failure_log_; |
801 | 743 |
802 AddSCTInfoToSSLInfo(ssl_info); | 744 AddSCTInfoToSSLInfo(ssl_info); |
803 | 745 |
804 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); | 746 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
805 CHECK(cipher); | 747 CHECK(cipher); |
806 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); | 748 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
807 ssl_info->key_exchange_info = | 749 ssl_info->key_exchange_info = |
808 SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl_)); | 750 SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl_)); |
809 | 751 |
810 ssl_info->connection_status = EncodeSSLConnectionStatus( | 752 SSLConnectionStatusSetCipherSuite( |
811 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), 0 /* no compression */, | 753 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), |
812 GetNetSSLVersion(ssl_)); | 754 &ssl_info->connection_status); |
755 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), | |
756 &ssl_info->connection_status); | |
davidben
2016/01/25 20:56:10
Why did this change?
ryanchung
2016/01/29 23:22:12
You mentioned compression doesn't exists anymore a
davidben
2016/02/04 00:40:11
Derp! Sorry, my bad. I wasn't paying attention and
| |
813 | 757 |
814 if (!SSL_get_secure_renegotiation_support(ssl_)) | 758 if (!SSL_get_secure_renegotiation_support(ssl_)) |
815 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; | 759 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
816 | 760 |
817 if (ssl_config_.version_fallback) | 761 if (ssl_config_.version_fallback) |
818 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; | 762 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
819 | 763 |
820 ssl_info->handshake_type = SSL_session_reused(ssl_) ? | 764 ssl_info->handshake_type = SSL_session_reused(ssl_) ? |
821 SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; | 765 SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; |
822 | 766 |
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1868 // Second pass: a client certificate should have been selected. | 1812 // Second pass: a client certificate should have been selected. |
1869 if (ssl_config_.client_cert.get()) { | 1813 if (ssl_config_.client_cert.get()) { |
1870 ScopedX509 leaf_x509 = | 1814 ScopedX509 leaf_x509 = |
1871 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); | 1815 OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
1872 if (!leaf_x509) { | 1816 if (!leaf_x509) { |
1873 LOG(WARNING) << "Failed to import certificate"; | 1817 LOG(WARNING) << "Failed to import certificate"; |
1874 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1818 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
1875 return -1; | 1819 return -1; |
1876 } | 1820 } |
1877 | 1821 |
1878 ScopedX509Stack chain = OSCertHandlesToOpenSSL( | 1822 ScopedX509_STACK chain = OSCertHandlesToOpenSSL( |
1879 ssl_config_.client_cert->GetIntermediateCertificates()); | 1823 ssl_config_.client_cert->GetIntermediateCertificates()); |
1880 if (!chain) { | 1824 if (!chain) { |
1881 LOG(WARNING) << "Failed to import intermediate certificates"; | 1825 LOG(WARNING) << "Failed to import intermediate certificates"; |
1882 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1826 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
1883 return -1; | 1827 return -1; |
1884 } | 1828 } |
1885 | 1829 |
1886 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || | 1830 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
1887 !SSL_set1_chain(ssl_, chain.get())) { | 1831 !SSL_set1_chain(ssl_, chain.get())) { |
1888 LOG(WARNING) << "Failed to set client certificate"; | 1832 LOG(WARNING) << "Failed to set client certificate"; |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2304 tb_was_negotiated_ = true; | 2248 tb_was_negotiated_ = true; |
2305 return 1; | 2249 return 1; |
2306 } | 2250 } |
2307 } | 2251 } |
2308 | 2252 |
2309 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; | 2253 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
2310 return 0; | 2254 return 0; |
2311 } | 2255 } |
2312 | 2256 |
2313 } // namespace net | 2257 } // namespace net |
OLD | NEW |