| 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 #include "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 965 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get())); | 965 new AsyncFailingChannelIDStore(), base::ThreadTaskRunnerHandle::Get())); |
| 966 context_.channel_id_service = channel_id_service_.get(); | 966 context_.channel_id_service = channel_id_service_.get(); |
| 967 } | 967 } |
| 968 | 968 |
| 969 private: | 969 private: |
| 970 scoped_ptr<ChannelIDService> channel_id_service_; | 970 scoped_ptr<ChannelIDService> channel_id_service_; |
| 971 }; | 971 }; |
| 972 | 972 |
| 973 //----------------------------------------------------------------------------- | 973 //----------------------------------------------------------------------------- |
| 974 | 974 |
| 975 // LogContainsSSLConnectEndEvent returns true if the given index in the given | |
| 976 // log is an SSL connect end event. The NSS sockets will cork in an attempt to | |
| 977 // merge the first application data record with the Finished message when false | |
| 978 // starting. However, in order to avoid the server timing out the handshake, | |
| 979 // they'll give up waiting for application data and send the Finished after a | |
| 980 // timeout. This means that an SSL connect end event may appear as a socket | |
| 981 // write. | |
| 982 static bool LogContainsSSLConnectEndEvent(const TestNetLogEntry::List& log, | |
| 983 int i) { | |
| 984 return LogContainsEndEvent(log, i, NetLog::TYPE_SSL_CONNECT) || | |
| 985 LogContainsEvent( | |
| 986 log, i, NetLog::TYPE_SOCKET_BYTES_SENT, NetLog::PHASE_NONE); | |
| 987 } | |
| 988 | |
| 989 bool SupportsAESGCM() { | 975 bool SupportsAESGCM() { |
| 990 #if defined(USE_OPENSSL) | 976 #if defined(USE_OPENSSL) |
| 991 return true; | 977 return true; |
| 992 #else | 978 #else |
| 993 crypto::EnsureNSSInit(); | 979 crypto::EnsureNSSInit(); |
| 994 return PK11_TokenExists(CKM_AES_GCM) && | 980 return PK11_TokenExists(CKM_AES_GCM) && |
| 995 PK11_TokenExists(CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256); | 981 PK11_TokenExists(CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256); |
| 996 #endif | 982 #endif |
| 997 } | 983 } |
| 998 | 984 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1024 rv = sock->Connect(callback.callback()); | 1010 rv = sock->Connect(callback.callback()); |
| 1025 | 1011 |
| 1026 TestNetLogEntry::List entries; | 1012 TestNetLogEntry::List entries; |
| 1027 log.GetEntries(&entries); | 1013 log.GetEntries(&entries); |
| 1028 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); | 1014 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); |
| 1029 if (rv == ERR_IO_PENDING) | 1015 if (rv == ERR_IO_PENDING) |
| 1030 rv = callback.WaitForResult(); | 1016 rv = callback.WaitForResult(); |
| 1031 EXPECT_EQ(OK, rv); | 1017 EXPECT_EQ(OK, rv); |
| 1032 EXPECT_TRUE(sock->IsConnected()); | 1018 EXPECT_TRUE(sock->IsConnected()); |
| 1033 log.GetEntries(&entries); | 1019 log.GetEntries(&entries); |
| 1034 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 1020 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT)); |
| 1035 | 1021 |
| 1036 sock->Disconnect(); | 1022 sock->Disconnect(); |
| 1037 EXPECT_FALSE(sock->IsConnected()); | 1023 EXPECT_FALSE(sock->IsConnected()); |
| 1038 } | 1024 } |
| 1039 | 1025 |
| 1040 TEST_F(SSLClientSocketTest, ConnectExpired) { | 1026 TEST_F(SSLClientSocketTest, ConnectExpired) { |
| 1041 SpawnedTestServer::SSLOptions ssl_options( | 1027 SpawnedTestServer::SSLOptions ssl_options( |
| 1042 SpawnedTestServer::SSLOptions::CERT_EXPIRED); | 1028 SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 1043 SpawnedTestServer test_server( | 1029 SpawnedTestServer test_server( |
| 1044 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); | 1030 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1071 if (rv == ERR_IO_PENDING) | 1057 if (rv == ERR_IO_PENDING) |
| 1072 rv = callback.WaitForResult(); | 1058 rv = callback.WaitForResult(); |
| 1073 | 1059 |
| 1074 EXPECT_EQ(ERR_CERT_DATE_INVALID, rv); | 1060 EXPECT_EQ(ERR_CERT_DATE_INVALID, rv); |
| 1075 | 1061 |
| 1076 // Rather than testing whether or not the underlying socket is connected, | 1062 // Rather than testing whether or not the underlying socket is connected, |
| 1077 // test that the handshake has finished. This is because it may be | 1063 // test that the handshake has finished. This is because it may be |
| 1078 // desirable to disconnect the socket before showing a user prompt, since | 1064 // desirable to disconnect the socket before showing a user prompt, since |
| 1079 // the user may take indefinitely long to respond. | 1065 // the user may take indefinitely long to respond. |
| 1080 log.GetEntries(&entries); | 1066 log.GetEntries(&entries); |
| 1081 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 1067 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT)); |
| 1082 } | 1068 } |
| 1083 | 1069 |
| 1084 TEST_F(SSLClientSocketTest, ConnectMismatched) { | 1070 TEST_F(SSLClientSocketTest, ConnectMismatched) { |
| 1085 SpawnedTestServer::SSLOptions ssl_options( | 1071 SpawnedTestServer::SSLOptions ssl_options( |
| 1086 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); | 1072 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 1087 SpawnedTestServer test_server( | 1073 SpawnedTestServer test_server( |
| 1088 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); | 1074 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); |
| 1089 ASSERT_TRUE(test_server.Start()); | 1075 ASSERT_TRUE(test_server.Start()); |
| 1090 | 1076 |
| 1091 cert_verifier_->set_default_result(ERR_CERT_COMMON_NAME_INVALID); | 1077 cert_verifier_->set_default_result(ERR_CERT_COMMON_NAME_INVALID); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1115 if (rv == ERR_IO_PENDING) | 1101 if (rv == ERR_IO_PENDING) |
| 1116 rv = callback.WaitForResult(); | 1102 rv = callback.WaitForResult(); |
| 1117 | 1103 |
| 1118 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID, rv); | 1104 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID, rv); |
| 1119 | 1105 |
| 1120 // Rather than testing whether or not the underlying socket is connected, | 1106 // Rather than testing whether or not the underlying socket is connected, |
| 1121 // test that the handshake has finished. This is because it may be | 1107 // test that the handshake has finished. This is because it may be |
| 1122 // desirable to disconnect the socket before showing a user prompt, since | 1108 // desirable to disconnect the socket before showing a user prompt, since |
| 1123 // the user may take indefinitely long to respond. | 1109 // the user may take indefinitely long to respond. |
| 1124 log.GetEntries(&entries); | 1110 log.GetEntries(&entries); |
| 1125 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 1111 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT)); |
| 1126 } | 1112 } |
| 1127 | 1113 |
| 1128 // Attempt to connect to a page which requests a client certificate. It should | 1114 // Attempt to connect to a page which requests a client certificate. It should |
| 1129 // return an error code on connect. | 1115 // return an error code on connect. |
| 1130 TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) { | 1116 TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) { |
| 1131 SpawnedTestServer::SSLOptions ssl_options; | 1117 SpawnedTestServer::SSLOptions ssl_options; |
| 1132 ssl_options.request_client_certificate = true; | 1118 ssl_options.request_client_certificate = true; |
| 1133 SpawnedTestServer test_server( | 1119 SpawnedTestServer test_server( |
| 1134 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); | 1120 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); |
| 1135 ASSERT_TRUE(test_server.Start()); | 1121 ASSERT_TRUE(test_server.Start()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1153 | 1139 |
| 1154 rv = sock->Connect(callback.callback()); | 1140 rv = sock->Connect(callback.callback()); |
| 1155 | 1141 |
| 1156 TestNetLogEntry::List entries; | 1142 TestNetLogEntry::List entries; |
| 1157 log.GetEntries(&entries); | 1143 log.GetEntries(&entries); |
| 1158 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); | 1144 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); |
| 1159 if (rv == ERR_IO_PENDING) | 1145 if (rv == ERR_IO_PENDING) |
| 1160 rv = callback.WaitForResult(); | 1146 rv = callback.WaitForResult(); |
| 1161 | 1147 |
| 1162 log.GetEntries(&entries); | 1148 log.GetEntries(&entries); |
| 1163 // Because we prematurely kill the handshake at CertificateRequest, | 1149 // Because the handshake is aborted during the CertificateRequest, the server |
| 1164 // the server may still send data (notably the ServerHelloDone) | 1150 // may still send the ServerHelloDone afterwards. As a result, the SSL_CONNECT |
| 1165 // after the error is returned. As a result, the SSL_CONNECT may not | 1151 // may not be the last entry. See http://crbug.com/54445. Because of this, use |
| 1166 // be the last entry. See http://crbug.com/54445. We use | 1152 // ExpectLogContainsSomewhere, rather than LogContainsEndEvent, to avoid |
| 1167 // ExpectLogContainsSomewhere instead of | 1153 // assuming particular details about the read (e.g. assuming there will only |
| 1168 // LogContainsSSLConnectEndEvent to avoid assuming, e.g., only one | 1154 // be one extra read, when there might be more). |
| 1169 // extra read instead of two. This occurs before the handshake ends, | |
| 1170 // so the corking logic of LogContainsSSLConnectEndEvent isn't | |
| 1171 // necessary. | |
| 1172 // | 1155 // |
| 1173 // TODO(davidben): When SSL_RestartHandshakeAfterCertReq in NSS is | 1156 // TODO(davidben): Is this still possible with memio_GetReadRequest? |
| 1174 // fixed and we can respond to the first CertificateRequest | |
| 1175 // without closing the socket, add a unit test for sending the | |
| 1176 // certificate. This test may still be useful as we'll want to close | |
| 1177 // the socket on a timeout if the user takes a long time to pick a | |
| 1178 // cert. Related bug: https://bugzilla.mozilla.org/show_bug.cgi?id=542832 | |
| 1179 ExpectLogContainsSomewhere( | 1157 ExpectLogContainsSomewhere( |
| 1180 entries, 0, NetLog::TYPE_SSL_CONNECT, NetLog::PHASE_END); | 1158 entries, 0, NetLog::TYPE_SSL_CONNECT, NetLog::PHASE_END); |
| 1181 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | 1159 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 1182 EXPECT_FALSE(sock->IsConnected()); | 1160 EXPECT_FALSE(sock->IsConnected()); |
| 1183 } | 1161 } |
| 1184 | 1162 |
| 1185 // Connect to a server requesting optional client authentication. Send it a | 1163 // Connect to a server requesting optional client authentication. Send it a |
| 1186 // null certificate. It should allow the connection. | 1164 // null certificate. It should allow the connection. |
| 1187 // | 1165 // |
| 1188 // TODO(davidben): Also test providing an actual certificate. | 1166 // TODO(davidben): Also test providing an actual certificate. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1220 | 1198 |
| 1221 TestNetLogEntry::List entries; | 1199 TestNetLogEntry::List entries; |
| 1222 log.GetEntries(&entries); | 1200 log.GetEntries(&entries); |
| 1223 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); | 1201 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); |
| 1224 if (rv == ERR_IO_PENDING) | 1202 if (rv == ERR_IO_PENDING) |
| 1225 rv = callback.WaitForResult(); | 1203 rv = callback.WaitForResult(); |
| 1226 | 1204 |
| 1227 EXPECT_EQ(OK, rv); | 1205 EXPECT_EQ(OK, rv); |
| 1228 EXPECT_TRUE(sock->IsConnected()); | 1206 EXPECT_TRUE(sock->IsConnected()); |
| 1229 log.GetEntries(&entries); | 1207 log.GetEntries(&entries); |
| 1230 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 1208 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT)); |
| 1231 | 1209 |
| 1232 // We responded to the server's certificate request with a Certificate | 1210 // We responded to the server's certificate request with a Certificate |
| 1233 // message with no client certificate in it. ssl_info.client_cert_sent | 1211 // message with no client certificate in it. ssl_info.client_cert_sent |
| 1234 // should be false in this case. | 1212 // should be false in this case. |
| 1235 SSLInfo ssl_info; | 1213 SSLInfo ssl_info; |
| 1236 sock->GetSSLInfo(&ssl_info); | 1214 sock->GetSSLInfo(&ssl_info); |
| 1237 EXPECT_FALSE(ssl_info.client_cert_sent); | 1215 EXPECT_FALSE(ssl_info.client_cert_sent); |
| 1238 | 1216 |
| 1239 sock->Disconnect(); | 1217 sock->Disconnect(); |
| 1240 EXPECT_FALSE(sock->IsConnected()); | 1218 EXPECT_FALSE(sock->IsConnected()); |
| (...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2274 // the socket when it encounters an error, whereas other implementations | 2252 // the socket when it encounters an error, whereas other implementations |
| 2275 // leave it connected. | 2253 // leave it connected. |
| 2276 // Because this an error that the test server is mutually aware of, as opposed | 2254 // Because this an error that the test server is mutually aware of, as opposed |
| 2277 // to being an error such as a certificate name mismatch, which is | 2255 // to being an error such as a certificate name mismatch, which is |
| 2278 // client-only, the exact index of the SSL connect end depends on how | 2256 // client-only, the exact index of the SSL connect end depends on how |
| 2279 // quickly the test server closes the underlying socket. If the test server | 2257 // quickly the test server closes the underlying socket. If the test server |
| 2280 // closes before the IO message loop pumps messages, there may be a 0-byte | 2258 // closes before the IO message loop pumps messages, there may be a 0-byte |
| 2281 // Read event in the NetLog due to TCPClientSocket picking up the EOF. As a | 2259 // Read event in the NetLog due to TCPClientSocket picking up the EOF. As a |
| 2282 // result, the SSL connect end event will be the second-to-last entry, | 2260 // result, the SSL connect end event will be the second-to-last entry, |
| 2283 // rather than the last entry. | 2261 // rather than the last entry. |
| 2284 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1) || | 2262 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT) || |
| 2285 LogContainsSSLConnectEndEvent(entries, -2)); | 2263 LogContainsEndEvent(entries, -2, NetLog::TYPE_SSL_CONNECT)); |
| 2286 } | 2264 } |
| 2287 | 2265 |
| 2288 // When creating an SSLClientSocket, it is allowed to pass in a | 2266 // When creating an SSLClientSocket, it is allowed to pass in a |
| 2289 // ClientSocketHandle that is not obtained from a client socket pool. | 2267 // ClientSocketHandle that is not obtained from a client socket pool. |
| 2290 // Here we verify that such a simple ClientSocketHandle, not associated with any | 2268 // Here we verify that such a simple ClientSocketHandle, not associated with any |
| 2291 // client socket pool, can be destroyed safely. | 2269 // client socket pool, can be destroyed safely. |
| 2292 TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { | 2270 TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { |
| 2293 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 2271 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 2294 SpawnedTestServer::kLocalhost, | 2272 SpawnedTestServer::kLocalhost, |
| 2295 base::FilePath()); | 2273 base::FilePath()); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2553 | 2531 |
| 2554 TestNetLogEntry::List entries; | 2532 TestNetLogEntry::List entries; |
| 2555 log.GetEntries(&entries); | 2533 log.GetEntries(&entries); |
| 2556 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); | 2534 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); |
| 2557 if (rv == ERR_IO_PENDING) | 2535 if (rv == ERR_IO_PENDING) |
| 2558 rv = callback.WaitForResult(); | 2536 rv = callback.WaitForResult(); |
| 2559 | 2537 |
| 2560 EXPECT_EQ(OK, rv); | 2538 EXPECT_EQ(OK, rv); |
| 2561 EXPECT_TRUE(sock->IsConnected()); | 2539 EXPECT_TRUE(sock->IsConnected()); |
| 2562 log.GetEntries(&entries); | 2540 log.GetEntries(&entries); |
| 2563 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 2541 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLog::TYPE_SSL_CONNECT)); |
| 2564 | 2542 |
| 2565 SSLInfo ssl_info; | 2543 SSLInfo ssl_info; |
| 2566 sock->GetSSLInfo(&ssl_info); | 2544 sock->GetSSLInfo(&ssl_info); |
| 2567 | 2545 |
| 2568 // Verify that SSLInfo contains the corrected re-constructed chain A -> B | 2546 // Verify that SSLInfo contains the corrected re-constructed chain A -> B |
| 2569 // -> C2. | 2547 // -> C2. |
| 2570 const X509Certificate::OSCertHandles& intermediates = | 2548 const X509Certificate::OSCertHandles& intermediates = |
| 2571 ssl_info.cert->GetIntermediateCertificates(); | 2549 ssl_info.cert->GetIntermediateCertificates(); |
| 2572 ASSERT_EQ(2U, intermediates.size()); | 2550 ASSERT_EQ(2U, intermediates.size()); |
| 2573 EXPECT_TRUE(X509Certificate::IsSameOSCert(ssl_info.cert->os_cert_handle(), | 2551 EXPECT_TRUE(X509Certificate::IsSameOSCert(ssl_info.cert->os_cert_handle(), |
| (...skipping 868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3442 ssl_config.channel_id_enabled = true; | 3420 ssl_config.channel_id_enabled = true; |
| 3443 | 3421 |
| 3444 int rv; | 3422 int rv; |
| 3445 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3423 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
| 3446 | 3424 |
| 3447 EXPECT_EQ(ERR_UNEXPECTED, rv); | 3425 EXPECT_EQ(ERR_UNEXPECTED, rv); |
| 3448 EXPECT_FALSE(sock_->IsConnected()); | 3426 EXPECT_FALSE(sock_->IsConnected()); |
| 3449 } | 3427 } |
| 3450 | 3428 |
| 3451 } // namespace net | 3429 } // namespace net |
| OLD | NEW |