• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/impl/channel_arg_names.h>
20 #include <grpc/slice.h>
21 #include <grpc/support/time.h>
22 #include <netinet/in.h>
23 #include <openssl/crypto.h>
24 #include <openssl/evp.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 
28 #include "absl/base/thread_annotations.h"
29 #include "absl/strings/str_format.h"
30 #include "gtest/gtest.h"
31 #include "src/core/lib/iomgr/error.h"
32 #include "src/core/lib/iomgr/port.h"
33 #include "test/core/test_util/test_config.h"
34 
35 // IWYU pragma: no_include <arpa/inet.h>
36 
37 // This test won't work except with posix sockets enabled
38 #ifdef GRPC_POSIX_SOCKET_TCP
39 
40 #include <grpc/credentials.h>
41 #include <grpc/grpc.h>
42 #include <grpc/grpc_security.h>
43 #include <openssl/err.h>
44 #include <openssl/ssl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/socket.h>
48 #include <unistd.h>
49 
50 #include <string>
51 
52 #include "absl/log/log.h"
53 #include "absl/strings/str_cat.h"
54 #include "src/core/lib/debug/trace.h"
55 #include "src/core/util/crash.h"
56 #include "src/core/util/sync.h"
57 #include "src/core/util/thd.h"
58 #include "test/core/test_util/tls_utils.h"
59 
60 #define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
61 #define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
62 #define SSL_CA_PATH "src/core/tsi/test_creds/ca.pem"
63 
64 class SslLibraryInfo {
65  public:
SslLibraryInfo()66   SslLibraryInfo() {}
67 
Notify()68   void Notify() {
69     grpc_core::MutexLock lock(&mu_);
70     ready_ = true;
71     cv_.Signal();
72   }
73 
Await()74   void Await() {
75     grpc_core::MutexLock lock(&mu_);
76     while (!ready_) {
77       cv_.Wait(&mu_);
78     }
79   }
80 
81  private:
82   grpc_core::Mutex mu_;
83   grpc_core::CondVar cv_;
84   bool ready_ ABSL_GUARDED_BY(mu_) = false;
85 };
86 
87 // Arguments for TLS server thread.
88 typedef struct {
89   int socket;
90   char* alpn_preferred;
91   SslLibraryInfo* ssl_library_info;
92 } server_args;
93 
94 // Based on https://wiki.openssl.org/index.php/Simple_TLS_Server.
95 // Pick an arbitrary unused port and return it in *out_port. Return
96 // an fd>=0 on success.
create_socket(int * out_port)97 static int create_socket(int* out_port) {
98   int s;
99   struct sockaddr_in addr;
100   socklen_t addr_len;
101   *out_port = -1;
102 
103   addr.sin_family = AF_INET;
104   addr.sin_port = 0;
105   addr.sin_addr.s_addr = htonl(INADDR_ANY);
106 
107   s = socket(AF_INET, SOCK_STREAM, 0);
108   if (s < 0) {
109     perror("Unable to create socket");
110     return -1;
111   }
112 
113   if (bind(s, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
114     perror("Unable to bind");
115     LOG(ERROR) << "Unable to bind to any port";
116     close(s);
117     return -1;
118   }
119 
120   if (listen(s, 1) < 0) {
121     perror("Unable to listen");
122     close(s);
123     return -1;
124   }
125 
126   addr_len = sizeof(addr);
127   if (getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), &addr_len) !=
128           0 ||
129       addr_len > sizeof(addr)) {
130     perror("getsockname");
131     LOG(ERROR) << "Unable to get socket local address";
132     close(s);
133     return -1;
134   }
135 
136   *out_port = ntohs(addr.sin_port);
137   return s;
138 }
139 
140 // Server callback during ALPN negotiation. See man page for
141 // SSL_CTX_set_alpn_select_cb.
alpn_select_cb(SSL *,const uint8_t ** out,uint8_t * out_len,const uint8_t * in,unsigned in_len,void * arg)142 static int alpn_select_cb(SSL* /*ssl*/, const uint8_t** out, uint8_t* out_len,
143                           const uint8_t* in, unsigned in_len, void* arg) {
144   const uint8_t* alpn_preferred = static_cast<const uint8_t*>(arg);
145 
146   *out = alpn_preferred;
147   *out_len = static_cast<uint8_t>(
148       strlen(reinterpret_cast<const char*>(alpn_preferred)));
149 
150   // Validate that the ALPN list includes "h2".
151   bool h2_seen = false;
152   const char* inp = reinterpret_cast<const char*>(in);
153   const char* in_end = inp + in_len;
154   while (inp < in_end) {
155     const size_t length = static_cast<size_t>(*inp++);
156     if (length == strlen("h2") && strncmp(inp, "h2", length) == 0) {
157       h2_seen = true;
158     }
159     inp += length;
160   }
161 
162   EXPECT_EQ(inp, in_end);
163   EXPECT_TRUE(h2_seen);
164 
165   return SSL_TLSEXT_ERR_OK;
166 }
167 
ssl_log_where_info(const SSL * ssl,int where,int flag,const char * msg)168 static void ssl_log_where_info(const SSL* ssl, int where, int flag,
169                                const char* msg) {
170   if ((where & flag) != 0) {
171     GRPC_TRACE_LOG(tsi, INFO)
172         << absl::StrFormat("%20.20s - %30.30s  - %5.10s", msg,
173                            SSL_state_string_long(ssl), SSL_state_string(ssl));
174   }
175 }
176 
ssl_server_info_callback(const SSL * ssl,int where,int ret)177 static void ssl_server_info_callback(const SSL* ssl, int where, int ret) {
178   if (ret == 0) {
179     LOG(ERROR) << "ssl_server_info_callback: error occurred.\n";
180     return;
181   }
182 
183   ssl_log_where_info(ssl, where, SSL_CB_LOOP, "Server: LOOP");
184   ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_START,
185                      "Server: HANDSHAKE START");
186   ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_DONE,
187                      "Server: HANDSHAKE DONE");
188 }
189 
190 // Minimal TLS server. This is largely based on the example at
191 // https://wiki.openssl.org/index.php/Simple_TLS_Server and the gRPC core
192 // internals in src/core/tsi/ssl_transport_security.c.
server_thread(void * arg)193 static void server_thread(void* arg) {
194   const server_args* args = static_cast<server_args*>(arg);
195 
196   SSL_load_error_strings();
197   OpenSSL_add_ssl_algorithms();
198   args->ssl_library_info->Notify();
199 
200   const SSL_METHOD* method = TLSv1_2_server_method();
201   SSL_CTX* ctx = SSL_CTX_new(method);
202   if (!ctx) {
203     perror("Unable to create SSL context");
204     ERR_print_errors_fp(stderr);
205     abort();
206   }
207 
208   // Load key pair.
209   if (SSL_CTX_use_certificate_file(ctx, SSL_CERT_PATH, SSL_FILETYPE_PEM) < 0) {
210     perror("Unable to use certificate file.");
211     ERR_print_errors_fp(stderr);
212     abort();
213   }
214   if (SSL_CTX_use_PrivateKey_file(ctx, SSL_KEY_PATH, SSL_FILETYPE_PEM) < 0) {
215     perror("Unable to use private key file.");
216     ERR_print_errors_fp(stderr);
217     abort();
218   }
219   if (SSL_CTX_check_private_key(ctx) != 1) {
220     perror("Check private key failed.");
221     ERR_print_errors_fp(stderr);
222     abort();
223   }
224 
225   // Set the cipher list to match the one expressed in
226   // src/core/tsi/ssl_transport_security.cc.
227   const char* cipher_list =
228       "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-"
229       "SHA384:ECDHE-RSA-AES256-GCM-SHA384";
230   if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
231     ERR_print_errors_fp(stderr);
232     grpc_core::Crash("Couldn't set server cipher list.");
233   }
234 
235   // Enable automatic curve selection. This is a NO-OP when using OpenSSL
236   // versions > 1.0.2.
237   if (!SSL_CTX_set_ecdh_auto(ctx, /*onoff=*/1)) {
238     ERR_print_errors_fp(stderr);
239     grpc_core::Crash("Couldn't set automatic curve selection.");
240   }
241 
242   // Register the ALPN selection callback.
243   SSL_CTX_set_alpn_select_cb(ctx, alpn_select_cb, args->alpn_preferred);
244 
245   // bind/listen/accept at TCP layer.
246   const int sock = args->socket;
247   LOG(INFO) << "Server listening";
248   struct sockaddr_in addr;
249   socklen_t len = sizeof(addr);
250   const int client =
251       accept(sock, reinterpret_cast<struct sockaddr*>(&addr), &len);
252   if (client < 0) {
253     perror("Unable to accept");
254     abort();
255   }
256 
257   // Establish a SSL* and accept at SSL layer.
258   SSL* ssl = SSL_new(ctx);
259   SSL_set_info_callback(ssl, ssl_server_info_callback);
260   ASSERT_TRUE(ssl);
261   SSL_set_fd(ssl, client);
262   if (SSL_accept(ssl) <= 0) {
263     ERR_print_errors_fp(stderr);
264     LOG(ERROR) << "Handshake failed.";
265   } else {
266     LOG(INFO) << "Handshake successful.";
267   }
268 
269   // Send out the settings frame.
270   const char settings_frame[] = "\x00\x00\x00\x04\x00\x00\x00\x00\x00";
271   SSL_write(ssl, settings_frame, sizeof(settings_frame) - 1);
272 
273   // Wait until the client drops its connection.
274   char buf;
275   while (SSL_read(ssl, &buf, sizeof(buf)) > 0) {
276   }
277 
278   SSL_free(ssl);
279   close(client);
280   close(sock);
281   SSL_CTX_free(ctx);
282 }
283 
284 // This test launches a minimal TLS server on a separate thread and then
285 // establishes a TLS handshake via the core library to the server. The TLS
286 // server validates ALPN aspects of the handshake and supplies the protocol
287 // specified in the server_alpn_preferred argument to the client.
client_ssl_test(char * server_alpn_preferred)288 static bool client_ssl_test(char* server_alpn_preferred) {
289   bool success = true;
290 
291   grpc_init();
292 
293   // Find a port we can bind to. Retries added to handle flakes in port server
294   // and port picking.
295   int port = -1;
296   int server_socket = -1;
297   int socket_retries = 30;
298   while (server_socket == -1 && socket_retries-- > 0) {
299     server_socket = create_socket(&port);
300     if (server_socket == -1) {
301       sleep(1);
302     }
303   }
304   EXPECT_GT(server_socket, 0);
305   EXPECT_GT(port, 0);
306 
307   // Launch the TLS server thread.
308   SslLibraryInfo ssl_library_info;
309   server_args args = {server_socket, server_alpn_preferred, &ssl_library_info};
310   bool ok;
311   grpc_core::Thread thd("grpc_client_ssl_test", server_thread, &args, &ok);
312   EXPECT_TRUE(ok);
313   thd.Start();
314   ssl_library_info.Await();
315 
316   // Load key pair and establish client SSL credentials.
317   std::string ca_cert = grpc_core::testing::GetFileContents(SSL_CA_PATH);
318   std::string cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
319   std::string key = grpc_core::testing::GetFileContents(SSL_KEY_PATH);
320 
321   grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
322   pem_key_cert_pair.private_key = key.c_str();
323   pem_key_cert_pair.cert_chain = cert.c_str();
324   grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
325       ca_cert.c_str(), &pem_key_cert_pair, nullptr, nullptr);
326 
327   // Establish a channel pointing at the TLS server. Since the gRPC runtime is
328   // lazy, this won't necessarily establish a connection yet.
329   std::string target = absl::StrCat("127.0.0.1:", port);
330   grpc_arg ssl_name_override = {
331       GRPC_ARG_STRING,
332       const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
333       {const_cast<char*>("foo.test.google.fr")}};
334   grpc_channel_args grpc_args;
335   grpc_args.num_args = 1;
336   grpc_args.args = &ssl_name_override;
337   grpc_channel* channel =
338       grpc_channel_create(target.c_str(), ssl_creds, &grpc_args);
339   EXPECT_TRUE(channel);
340 
341   // Initially the channel will be idle, the
342   // grpc_channel_check_connectivity_state triggers an attempt to connect.
343   EXPECT_EQ(
344       grpc_channel_check_connectivity_state(channel, 1 /* try_to_connect */),
345       GRPC_CHANNEL_IDLE);
346 
347   // Wait a bounded number of times for the channel to be ready. When the
348   // channel is ready, the initial TLS handshake will have successfully
349   // completed and we know that the client's ALPN list satisfied the server.
350   int retries = 10;
351   grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
352   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
353 
354   while (state != GRPC_CHANNEL_READY && retries-- > 0) {
355     grpc_channel_watch_connectivity_state(
356         channel, state, grpc_timeout_seconds_to_deadline(3), cq, nullptr);
357     gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(5);
358     grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
359     EXPECT_EQ(ev.type, GRPC_OP_COMPLETE);
360     state =
361         grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */);
362   }
363   grpc_completion_queue_destroy(cq);
364   if (retries < 0) {
365     success = false;
366   }
367 
368   grpc_channel_destroy(channel);
369   grpc_channel_credentials_release(ssl_creds);
370 
371   thd.Join();
372 
373   grpc_shutdown();
374 
375   return success;
376 }
377 
TEST(ClientSslTest,MainTest)378 TEST(ClientSslTest, MainTest) {
379   // Handshake succeeds when the server has h2 as the ALPN preference.
380   ASSERT_TRUE(client_ssl_test(const_cast<char*>("h2")));
381 
382 // TODO(gtcooke94) Figure out why test is failing with OpenSSL and fix it.
383 #ifdef OPENSSL_IS_BORING_SSL
384   // Handshake fails when the server uses a fake protocol as its ALPN
385   // preference. This validates the client is correctly validating ALPN returns
386   // and sanity checks the client_ssl_test.
387   ASSERT_FALSE(client_ssl_test(const_cast<char*>("foo")));
388 #endif  // OPENSSL_IS_BORING_SSL
389   // Clean up the SSL libraries.
390   EVP_cleanup();
391 }
392 
393 #endif  // GRPC_POSIX_SOCKET_TCP
394 
main(int argc,char ** argv)395 int main(int argc, char** argv) {
396   grpc::testing::TestEnvironment env(&argc, argv);
397   ::testing::InitGoogleTest(&argc, argv);
398   return RUN_ALL_TESTS();
399 }
400