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/memory/ref_counted.h" | |
7 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
8 #include "net/base/cert_test_util.h" | 9 #include "net/base/cert_test_util.h" |
9 #include "net/base/host_resolver.h" | 10 #include "net/base/host_resolver.h" |
10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
11 #include "net/base/mock_cert_verifier.h" | 12 #include "net/base/mock_cert_verifier.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
14 #include "net/base/net_log_unittest.h" | 15 #include "net/base/net_log_unittest.h" |
16 #include "net/base/ssl_cert_request_info.h" | |
15 #include "net/base/ssl_config_service.h" | 17 #include "net/base/ssl_config_service.h" |
16 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
17 #include "net/base/test_data_directory.h" | 19 #include "net/base/test_data_directory.h" |
18 #include "net/base/test_root_certs.h" | 20 #include "net/base/test_root_certs.h" |
19 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
20 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
21 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
22 #include "net/socket/tcp_client_socket.h" | 24 #include "net/socket/tcp_client_socket.h" |
23 #include "net/test/test_server.h" | 25 #include "net/test/test_server.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
929 ssl_info.cert->os_cert_handle(), certs[0]->os_cert_handle())); | 931 ssl_info.cert->os_cert_handle(), certs[0]->os_cert_handle())); |
930 EXPECT_TRUE(net::X509Certificate::IsSameOSCert( | 932 EXPECT_TRUE(net::X509Certificate::IsSameOSCert( |
931 intermediates[0], certs[1]->os_cert_handle())); | 933 intermediates[0], certs[1]->os_cert_handle())); |
932 EXPECT_TRUE(net::X509Certificate::IsSameOSCert( | 934 EXPECT_TRUE(net::X509Certificate::IsSameOSCert( |
933 intermediates[1], certs[2]->os_cert_handle())); | 935 intermediates[1], certs[2]->os_cert_handle())); |
934 | 936 |
935 sock->Disconnect(); | 937 sock->Disconnect(); |
936 EXPECT_FALSE(sock->IsConnected()); | 938 EXPECT_FALSE(sock->IsConnected()); |
937 } | 939 } |
938 | 940 |
941 // Verifies the correctness of GetSSLCertRequestInfo. | |
942 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | |
943 protected: | |
944 // Creates a test server with the given SSLOptions, connects to it and returns | |
945 // the SSLCertRequestInfo reported by the socket. | |
946 scoped_refptr<net::SSLCertRequestInfo> | |
947 GetCertRequest(net::TestServer::SSLOptions ssl_options) { | |
Ryan Sleevi
2013/01/07 18:37:58
Indent: For overly long function names, we don't i
ppi
2013/01/08 17:00:38
Fixed, thanks!
| |
948 net::TestServer test_server(net::TestServer::TYPE_HTTPS, | |
949 ssl_options, | |
950 FilePath()); | |
951 EXPECT_TRUE(test_server.Start()); | |
Ryan Sleevi
2013/01/07 18:37:58
ASSERT_TRUE
ppi
2013/01/08 17:00:38
We cannot use hard assertions in non-void returnin
| |
952 | |
953 net::AddressList addr; | |
954 EXPECT_TRUE(test_server.GetAddressList(&addr)); | |
Ryan Sleevi
2013/01/07 18:37:58
ASSERT_TRUE
| |
955 | |
956 net::TestCompletionCallback callback; | |
957 net::CapturingNetLog log; | |
958 net::StreamSocket* transport = new net::TCPClientSocket( | |
959 addr, &log, net::NetLog::Source()); | |
960 int rv = transport->Connect(callback.callback()); | |
961 if (rv == net::ERR_IO_PENDING) | |
962 rv = callback.WaitForResult(); | |
963 EXPECT_EQ(net::OK, rv); | |
964 | |
965 net::SSLConfig ssl_config = kDefaultSSLConfig; | |
966 scoped_ptr<net::SSLClientSocket> sock( | |
967 CreateSSLClientSocket( | |
968 transport, test_server.host_port_pair(), ssl_config)); | |
Ryan Sleevi
2013/01/07 18:37:58
1) You don't modify ssl_config, so why create the
ppi
2013/01/08 17:00:38
Thanks, fixed both in patch set 4.
| |
969 EXPECT_FALSE(sock->IsConnected()); | |
970 | |
971 rv = sock->Connect(callback.callback()); | |
972 if (rv == net::ERR_IO_PENDING) | |
973 rv = callback.WaitForResult(); | |
974 scoped_refptr<net::SSLCertRequestInfo> requestInfo = | |
Ryan Sleevi
2013/01/07 18:37:58
style: naming: requestInfo -> request_info
ppi
2013/01/08 17:00:38
Thanks, sorry about that!
| |
975 new net::SSLCertRequestInfo(); | |
976 sock->GetSSLCertRequestInfo(requestInfo.get()); | |
977 sock->Disconnect(); | |
978 EXPECT_FALSE(sock->IsConnected()); | |
979 | |
980 return requestInfo; | |
981 } | |
982 | |
983 // The following is needed to construct paths to certificates passed as | |
984 // |client_authorities| in server SSLOptions. Current implementation of | |
985 // RemoteTestServer (used on Android) expects relative paths, as opposed to | |
986 // LocalTestServer, which expects absolute paths (what to fix?). | |
987 FilePath CertDirectory() { | |
988 #ifdef OS_ANDROID | |
989 return net::GetTestCertsDirectoryRelative(); | |
990 #else | |
991 return net::GetTestCertsDirectory(); | |
992 #endif | |
Ryan Sleevi
2013/01/07 18:37:58
design: This sort of dependency on the implementat
ppi
2013/01/08 17:00:38
I agree - I can see no reasons for LocalTestServer
| |
993 } | |
994 }; | |
995 | |
996 TEST_F(SSLClientSocketCertRequestInfoTest, NoAuthorities) { | |
997 net::TestServer::SSLOptions ssl_options; | |
998 ssl_options.request_client_certificate = true; | |
999 scoped_refptr<net::SSLCertRequestInfo> requestInfo = | |
Ryan Sleevi
2013/01/07 18:37:58
style: requestInfo -> request_info
ppi
2013/01/08 17:00:38
Thanks, sorry about that!
| |
1000 GetCertRequest(ssl_options); | |
1001 EXPECT_EQ(0, static_cast<int>(requestInfo->cert_authorities.size())); | |
Ryan Sleevi
2013/01/07 18:37:58
nit:
EXPECT_EQ(0u, request_info->cert_authorities
ppi
2013/01/08 17:00:38
Thanks, done!
| |
1002 } | |
1003 | |
1004 TEST_F(SSLClientSocketCertRequestInfoTest, TwoAuthorities) { | |
1005 const FilePath::CharType kThawteFile[] = | |
1006 FILE_PATH_LITERAL("thawte.single.pem"); | |
1007 const unsigned char kThawteDN[] = { | |
1008 0x30, 0x4c, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, | |
1009 0x02, 0x5a, 0x41, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x0a, | |
1010 0x13, 0x1c, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x43, 0x6f, 0x6e, | |
1011 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x28, 0x50, 0x74, 0x79, | |
1012 0x29, 0x20, 0x4c, 0x74, 0x64, 0x2e, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, | |
1013 0x55, 0x04, 0x03, 0x13, 0x0d, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, | |
1014 0x53, 0x47, 0x43, 0x20, 0x43, 0x41 | |
1015 }; | |
1016 const size_t kThawteLen = 78; | |
Ryan Sleevi
2013/01/07 18:37:58
NACK:
const size_t kThawteLen = sizeof(kThawteDN)
ppi
2013/01/08 17:00:38
Thanks, done!
| |
1017 | |
1018 const FilePath::CharType kDiginotarFile[] = | |
1019 FILE_PATH_LITERAL("diginotar_root_ca.pem"); | |
1020 const unsigned char kDiginotarDN[] = { | |
1021 0x30, 0x5f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, | |
1022 0x02, 0x4e, 0x4c, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, | |
1023 0x13, 0x09, 0x44, 0x69, 0x67, 0x69, 0x4e, 0x6f, 0x74, 0x61, 0x72, 0x31, | |
1024 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x44, 0x69, | |
1025 0x67, 0x69, 0x4e, 0x6f, 0x74, 0x61, 0x72, 0x20, 0x52, 0x6f, 0x6f, 0x74, | |
1026 0x20, 0x43, 0x41, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x09, 0x2a, 0x86, 0x48, | |
1027 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x11, 0x69, 0x6e, 0x66, 0x6f, | |
1028 0x40, 0x64, 0x69, 0x67, 0x69, 0x6e, 0x6f, 0x74, 0x61, 0x72, 0x2e, 0x6e, | |
1029 0x6c | |
1030 }; | |
1031 const size_t kDiginotarLen = 97; | |
1032 | |
1033 net::TestServer::SSLOptions ssl_options; | |
1034 ssl_options.request_client_certificate = true; | |
1035 ssl_options.client_authorities.push_back(CertDirectory().Append(kThawteFile)); | |
1036 ssl_options.client_authorities.push_back( | |
1037 CertDirectory().Append(kDiginotarFile)); | |
1038 scoped_refptr<net::SSLCertRequestInfo> requestInfo = | |
1039 GetCertRequest(ssl_options); | |
1040 EXPECT_EQ(2, static_cast<int>(requestInfo->cert_authorities.size())); | |
Ryan Sleevi
2013/01/07 18:37:58
ASSERT_EQ, since you're about to de-reference.
ppi
2013/01/08 17:00:38
Thanks, done!
| |
1041 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kThawteDN), kThawteLen), | |
digit1
2013/01/07 11:24:10
I'd suggest using EXPECT_STREQ() instead, which wi
ppi
2013/01/07 18:18:48
I think that would cause troubles whenever the str
Ryan Sleevi
2013/01/07 18:37:58
Since the data here is binary, even using the std:
ppi
2013/01/08 17:00:38
The ASCII content of DN fields is readable in the
| |
1042 requestInfo->cert_authorities[0]); | |
1043 EXPECT_EQ( | |
1044 std::string(reinterpret_cast<const char*>(kDiginotarDN), kDiginotarLen), | |
1045 requestInfo->cert_authorities[1]); | |
1046 } | |
OLD | NEW |