Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #define BOOST_TEST_MODULE SecurityTest |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 21 | #include <boost/test/unit_test.hpp> |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 22 | #include <boost/bind.hpp> |
| 23 | #include <boost/filesystem.hpp> |
| 24 | #include <boost/foreach.hpp> |
| 25 | #include <boost/format.hpp> |
| 26 | #include <boost/make_shared.hpp> |
| 27 | #include <boost/shared_ptr.hpp> |
| 28 | #include <boost/thread.hpp> |
| 29 | #include <thrift/transport/TSSLServerSocket.h> |
| 30 | #include <thrift/transport/TSSLSocket.h> |
| 31 | #include <thrift/transport/TTransport.h> |
| 32 | #include "TestPortFixture.h" |
| 33 | #include <vector> |
| 34 | #ifdef linux |
| 35 | #include <signal.h> |
| 36 | #endif |
| 37 | |
| 38 | using apache::thrift::transport::TSSLServerSocket; |
| 39 | using apache::thrift::transport::TServerTransport; |
| 40 | using apache::thrift::transport::TSSLSocket; |
| 41 | using apache::thrift::transport::TSSLSocketFactory; |
| 42 | using apache::thrift::transport::TTransport; |
| 43 | using apache::thrift::transport::TTransportException; |
| 44 | using apache::thrift::transport::TTransportFactory; |
| 45 | |
| 46 | boost::filesystem::path keyDir; |
| 47 | boost::filesystem::path certFile(const std::string& filename) |
| 48 | { |
| 49 | return keyDir / filename; |
| 50 | } |
| 51 | boost::mutex gMutex; |
| 52 | |
| 53 | struct GlobalFixture |
| 54 | { |
| 55 | GlobalFixture() |
| 56 | { |
| 57 | using namespace boost::unit_test::framework; |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 58 | for (int i = 0; i < master_test_suite().argc; ++i) |
| 59 | { |
| 60 | BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % master_test_suite().argv[i]); |
| 61 | } |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 62 | |
| 63 | #ifdef linux |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 64 | // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket that has |
| 65 | // disconnected can cause a SIGPIPE signal... |
| 66 | signal(SIGPIPE, SIG_IGN); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 67 | #endif |
| 68 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 69 | TSSLSocketFactory::setManualOpenSSLInitialization(true); |
| 70 | apache::thrift::transport::initializeOpenSSL(); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 71 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 72 | keyDir = boost::filesystem::current_path().parent_path().parent_path().parent_path() / "test" / "keys"; |
| 73 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 74 | { |
| 75 | keyDir = boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]); |
| 76 | if (!boost::filesystem::exists(certFile("server.crt"))) |
| 77 | { |
| 78 | throw std::invalid_argument("The last argument to this test must be the directory containing the test certificate(s)."); |
| 79 | } |
| 80 | } |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | virtual ~GlobalFixture() |
| 84 | { |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 85 | apache::thrift::transport::cleanupOpenSSL(); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 86 | #ifdef linux |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 87 | signal(SIGPIPE, SIG_DFL); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 88 | #endif |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 92 | #if (BOOST_VERSION >= 105900) |
| 93 | BOOST_GLOBAL_FIXTURE(GlobalFixture); |
| 94 | #else |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 95 | BOOST_GLOBAL_FIXTURE(GlobalFixture) |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 96 | #endif |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 97 | |
| 98 | struct SecurityFixture : public TestPortFixture |
| 99 | { |
| 100 | void server(apache::thrift::transport::SSLProtocol protocol) |
| 101 | { |
| 102 | try |
| 103 | { |
| 104 | boost::mutex::scoped_lock lock(mMutex); |
| 105 | |
| 106 | boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory; |
| 107 | boost::shared_ptr<TSSLServerSocket> pServerSocket; |
| 108 | |
| 109 | pServerSocketFactory.reset(new TSSLSocketFactory(static_cast<apache::thrift::transport::SSLProtocol>(protocol))); |
| 110 | pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 111 | pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str()); |
| 112 | pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str()); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 113 | pServerSocketFactory->server(true); |
| 114 | pServerSocket.reset(new TSSLServerSocket("localhost", m_serverPort, pServerSocketFactory)); |
| 115 | boost::shared_ptr<TTransport> connectedClient; |
| 116 | |
| 117 | try |
| 118 | { |
| 119 | pServerSocket->listen(); |
| 120 | mCVar.notify_one(); |
| 121 | lock.unlock(); |
| 122 | |
| 123 | connectedClient = pServerSocket->accept(); |
| 124 | uint8_t buf[2]; |
| 125 | buf[0] = 'O'; |
| 126 | buf[1] = 'K'; |
| 127 | connectedClient->write(&buf[0], 2); |
| 128 | connectedClient->flush(); |
| 129 | } |
| 130 | |
| 131 | catch (apache::thrift::transport::TTransportException& ex) |
| 132 | { |
| 133 | boost::mutex::scoped_lock lock(gMutex); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 134 | BOOST_TEST_MESSAGE(boost::format("SRV %1% Exception: %2%") % boost::this_thread::get_id() % ex.what()); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (connectedClient) |
| 138 | { |
| 139 | connectedClient->close(); |
| 140 | connectedClient.reset(); |
| 141 | } |
| 142 | |
| 143 | pServerSocket->close(); |
| 144 | pServerSocket.reset(); |
| 145 | } |
| 146 | catch (std::exception& ex) |
| 147 | { |
| 148 | BOOST_FAIL(boost::format("%1%: %2%") % typeid(ex).name() % ex.what()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void client(apache::thrift::transport::SSLProtocol protocol) |
| 153 | { |
| 154 | try |
| 155 | { |
| 156 | boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory; |
| 157 | boost::shared_ptr<TSSLSocket> pClientSocket; |
| 158 | |
| 159 | try |
| 160 | { |
| 161 | pClientSocketFactory.reset(new TSSLSocketFactory(static_cast<apache::thrift::transport::SSLProtocol>(protocol))); |
| 162 | pClientSocketFactory->authenticate(true); |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 163 | pClientSocketFactory->loadCertificate(certFile("client.crt").string().c_str()); |
| 164 | pClientSocketFactory->loadPrivateKey(certFile("client.key").string().c_str()); |
| 165 | pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").string().c_str()); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 166 | pClientSocket = pClientSocketFactory->createSocket("localhost", m_serverPort); |
| 167 | pClientSocket->open(); |
| 168 | |
| 169 | uint8_t buf[3]; |
| 170 | buf[0] = 0; |
| 171 | buf[1] = 0; |
| 172 | BOOST_CHECK_EQUAL(2, pClientSocket->read(&buf[0], 2)); |
| 173 | BOOST_CHECK_EQUAL(0, memcmp(&buf[0], "OK", 2)); |
| 174 | mConnected = true; |
| 175 | } |
| 176 | catch (apache::thrift::transport::TTransportException& ex) |
| 177 | { |
| 178 | boost::mutex::scoped_lock lock(gMutex); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 179 | BOOST_TEST_MESSAGE(boost::format("CLI %1% Exception: %2%") % boost::this_thread::get_id() % ex.what()); |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (pClientSocket) |
| 183 | { |
| 184 | pClientSocket->close(); |
| 185 | pClientSocket.reset(); |
| 186 | } |
| 187 | } |
| 188 | catch (std::exception& ex) |
| 189 | { |
| 190 | BOOST_FAIL(boost::format("%1%: %2%") % typeid(ex).name() % ex.what()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static const char *protocol2str(size_t protocol) |
| 195 | { |
| 196 | static const char *strings[apache::thrift::transport::LATEST + 1] = |
| 197 | { |
| 198 | "SSLTLS", |
| 199 | "SSLv2", |
| 200 | "SSLv3", |
| 201 | "TLSv1_0", |
| 202 | "TLSv1_1", |
| 203 | "TLSv1_2" |
| 204 | }; |
| 205 | return strings[protocol]; |
| 206 | } |
| 207 | |
| 208 | boost::mutex mMutex; |
| 209 | boost::condition_variable mCVar; |
| 210 | bool mConnected; |
| 211 | }; |
| 212 | |
| 213 | BOOST_FIXTURE_TEST_SUITE(BOOST_TEST_MODULE, SecurityFixture) |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(ssl_security_matrix) |
| 216 | { |
| 217 | try |
| 218 | { |
| 219 | // matrix of connection success between client and server with different SSLProtocol selections |
| 220 | bool matrix[apache::thrift::transport::LATEST + 1][apache::thrift::transport::LATEST + 1] = |
| 221 | { |
| 222 | // server = SSLTLS SSLv2 SSLv3 TLSv1_0 TLSv1_1 TLSv1_2 |
| 223 | // client |
| 224 | /* SSLTLS */ { true, false, false, true, true, true }, |
| 225 | /* SSLv2 */ { false, false, false, false, false, false }, |
| 226 | /* SSLv3 */ { false, false, true, false, false, false }, |
| 227 | /* TLSv1_0 */ { true, false, false, true, false, false }, |
| 228 | /* TLSv1_1 */ { true, false, false, false, true, false }, |
| 229 | /* TLSv1_2 */ { true, false, false, false, false, true } |
| 230 | }; |
| 231 | |
| 232 | for (size_t si = 0; si <= apache::thrift::transport::LATEST; ++si) |
| 233 | { |
| 234 | for (size_t ci = 0; ci <= apache::thrift::transport::LATEST; ++ci) |
| 235 | { |
| 236 | if (si == 1 || ci == 1) |
| 237 | { |
| 238 | // Skip all SSLv2 cases - protocol not supported |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | boost::mutex::scoped_lock lock(mMutex); |
| 243 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 244 | BOOST_TEST_MESSAGE(boost::format("TEST: Server = %1%, Client = %2%") |
Jim King | b0b710a | 2015-07-28 13:31:27 -0400 | [diff] [blame] | 245 | % protocol2str(si) % protocol2str(ci)); |
| 246 | |
| 247 | mConnected = false; |
| 248 | boost::thread_group threads; |
| 249 | threads.create_thread(boost::bind(&SecurityFixture::server, this, static_cast<apache::thrift::transport::SSLProtocol>(si))); |
| 250 | mCVar.wait(lock); // wait for listen() to succeed |
| 251 | lock.unlock(); |
| 252 | threads.create_thread(boost::bind(&SecurityFixture::client, this, static_cast<apache::thrift::transport::SSLProtocol>(ci))); |
| 253 | threads.join_all(); |
| 254 | |
| 255 | BOOST_CHECK_MESSAGE(mConnected == matrix[ci][si], |
| 256 | boost::format(" Server = %1%, Client = %2% expected mConnected == %3% but was %4%") |
| 257 | % protocol2str(si) % protocol2str(ci) % matrix[ci][si] % mConnected); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | catch (std::exception& ex) |
| 262 | { |
| 263 | BOOST_FAIL(boost::format("%1%: %2%") % typeid(ex).name() % ex.what()); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | BOOST_AUTO_TEST_SUITE_END() |