blob: 136907753ef18dff9240dfea3cbf206bf7a78723 [file] [log] [blame]
Jim Kingb0b710a2015-07-28 13:31:27 -04001/*
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 Grochowskie9bdb412015-09-25 20:17:36 +020021#include <boost/test/unit_test.hpp>
Jim Kingb0b710a2015-07-28 13:31:27 -040022#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
38using apache::thrift::transport::TSSLServerSocket;
39using apache::thrift::transport::TServerTransport;
40using apache::thrift::transport::TSSLSocket;
41using apache::thrift::transport::TSSLSocketFactory;
42using apache::thrift::transport::TTransport;
43using apache::thrift::transport::TTransportException;
44using apache::thrift::transport::TTransportFactory;
45
46boost::filesystem::path keyDir;
47boost::filesystem::path certFile(const std::string& filename)
48{
49 return keyDir / filename;
50}
51boost::mutex gMutex;
52
53struct GlobalFixture
54{
55 GlobalFixture()
56 {
57 using namespace boost::unit_test::framework;
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020058 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 Kingb0b710a2015-07-28 13:31:27 -040062
63 #ifdef linux
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020064 // 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 Kingb0b710a2015-07-28 13:31:27 -040067 #endif
68
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020069 TSSLSocketFactory::setManualOpenSSLInitialization(true);
70 apache::thrift::transport::initializeOpenSSL();
Jim Kingb0b710a2015-07-28 13:31:27 -040071
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020072 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 Kingb0b710a2015-07-28 13:31:27 -040081 }
82
83 virtual ~GlobalFixture()
84 {
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020085 apache::thrift::transport::cleanupOpenSSL();
Jim Kingb0b710a2015-07-28 13:31:27 -040086#ifdef linux
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020087 signal(SIGPIPE, SIG_DFL);
Jim Kingb0b710a2015-07-28 13:31:27 -040088#endif
Jim Kingb0b710a2015-07-28 13:31:27 -040089 }
90};
91
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020092#if (BOOST_VERSION >= 105900)
93BOOST_GLOBAL_FIXTURE(GlobalFixture);
94#else
Jim Kingb0b710a2015-07-28 13:31:27 -040095BOOST_GLOBAL_FIXTURE(GlobalFixture)
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020096#endif
Jim Kingb0b710a2015-07-28 13:31:27 -040097
98struct 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 Monaco796667b2016-01-04 23:05:19 +0100111 pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str());
112 pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str());
Jim Kingb0b710a2015-07-28 13:31:27 -0400113 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 Grochowskie9bdb412015-09-25 20:17:36 +0200134 BOOST_TEST_MESSAGE(boost::format("SRV %1% Exception: %2%") % boost::this_thread::get_id() % ex.what());
Jim Kingb0b710a2015-07-28 13:31:27 -0400135 }
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 Monaco796667b2016-01-04 23:05:19 +0100163 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 Kingb0b710a2015-07-28 13:31:27 -0400166 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 Grochowskie9bdb412015-09-25 20:17:36 +0200179 BOOST_TEST_MESSAGE(boost::format("CLI %1% Exception: %2%") % boost::this_thread::get_id() % ex.what());
Jim Kingb0b710a2015-07-28 13:31:27 -0400180 }
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
213BOOST_FIXTURE_TEST_SUITE(BOOST_TEST_MODULE, SecurityFixture)
214
215BOOST_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 Grochowskie9bdb412015-09-25 20:17:36 +0200244 BOOST_TEST_MESSAGE(boost::format("TEST: Server = %1%, Client = %2%")
Jim Kingb0b710a2015-07-28 13:31:27 -0400245 % 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
267BOOST_AUTO_TEST_SUITE_END()