aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/handshake
diff options
context:
space:
mode:
authorGravatar Harvey Tuch <htuch@google.com>2016-10-11 10:04:53 -0400
committerGravatar Harvey Tuch <htuch@google.com>2016-10-24 11:16:15 -0400
commit8cdf17a620a2d0909c1785717d5b3d8cff1248fd (patch)
treedbb3d57d4873a72ec8a4fae155e0e864c7fc487c /test/core/handshake
parent1445c479013a997a2fa41bfd09a5219298c4c3d8 (diff)
Introduce a grpc-exp ALPN protocol identifier.
This patch introduces an additional ALPN protocol, grpc-exp, intended to take preference to h2 and indicate to the server that the connection contains only gRPC traffic. This allows servers and intermediate boxes to distinguish gRPC from other HTTP/2 traffic. The choice of grpc-exp as a protocol identifier indicates that this scheme is currently experimental and should not be relied upon. The protocol is not in the IANA TLS registry. This patch also introduces client/server handshake tests that validate the preferential treatment of grpc-exp in an end-to-end manner.
Diffstat (limited to 'test/core/handshake')
-rw-r--r--test/core/handshake/client_ssl.c292
-rw-r--r--test/core/handshake/server_ssl.c264
2 files changed, 556 insertions, 0 deletions
diff --git a/test/core/handshake/client_ssl.c b/test/core/handshake/client_ssl.c
new file mode 100644
index 0000000000..7cce77fb97
--- /dev/null
+++ b/test/core/handshake/client_ssl.c
@@ -0,0 +1,292 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <arpa/inet.h>
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/thd.h>
+#include "src/core/lib/iomgr/load_file.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+#define SSL_CERT_PATH "src/core/lib/tsi/test_creds/server1.pem"
+#define SSL_KEY_PATH "src/core/lib/tsi/test_creds/server1.key"
+#define SSL_CA_PATH "src/core/lib/tsi/test_creds/ca.pem"
+
+// Arguments for TLS server thread.
+typedef struct {
+ int port;
+ char *alpn_preferred;
+} server_args;
+
+// From https://wiki.openssl.org/index.php/Simple_TLS_Server.
+int create_socket(int port) {
+ int s;
+ struct sockaddr_in addr;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons((uint16_t)port);
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s < 0) {
+ perror("Unable to create socket");
+ abort();
+ }
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ perror("Unable to bind");
+ abort();
+ }
+
+ if (listen(s, 1) < 0) {
+ perror("Unable to listen");
+ abort();
+ }
+
+ return s;
+}
+
+// Server callback during ALPN negotiation. See man page for
+// SSL_CTX_set_alpn_select_cb.
+static int alpn_select_cb(SSL *ssl, const uint8_t **out, uint8_t *out_len,
+ const uint8_t *in, unsigned in_len, void *arg) {
+ const uint8_t *alpn_preferred = (const uint8_t *)arg;
+
+ *out = alpn_preferred;
+ *out_len = (uint8_t)strlen((char *)alpn_preferred);
+
+ // Validate that the ALPN list includes "h2" and "grpc-exp", that "grpc-exp"
+ // precedes "h2".
+ bool grpc_exp_seen = false;
+ bool h2_seen = false;
+ const char *inp = (const char *)in;
+ for (int i = 0; i < (int)in_len; ++i) {
+ const size_t length = (size_t)*inp++;
+ if (length == strlen("grpc-exp") && strncmp(inp, "grpc-exp", length) == 0) {
+ grpc_exp_seen = true;
+ GPR_ASSERT(!h2_seen);
+ }
+ if (length == strlen("h2") && strncmp(inp, "h2", length) == 0) {
+ h2_seen = true;
+ GPR_ASSERT(grpc_exp_seen);
+ }
+ inp += length;
+ }
+
+ GPR_ASSERT(grpc_exp_seen);
+ GPR_ASSERT(h2_seen);
+
+ return SSL_TLSEXT_ERR_OK;
+}
+
+// Minimal TLS server. This is largely based on the example at
+// https://wiki.openssl.org/index.php/Simple_TLS_Server and the gRPC core
+// internals in src/core/lib/tsi/ssl_transport_security.c.
+static void server_thread(void *arg) {
+ const server_args *args = (server_args *)arg;
+
+ SSL_load_error_strings();
+ OpenSSL_add_ssl_algorithms();
+
+ const SSL_METHOD *method = TLSv1_2_server_method();
+ SSL_CTX *ctx = SSL_CTX_new(method);
+ if (!ctx) {
+ perror("Unable to create SSL context");
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+
+ // Load key pair.
+ if (SSL_CTX_use_certificate_file(ctx, SSL_CERT_PATH, SSL_FILETYPE_PEM) < 0) {
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+ if (SSL_CTX_use_PrivateKey_file(ctx, SSL_KEY_PATH, SSL_FILETYPE_PEM) < 0) {
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+
+ // Set the cipher list to match the one expressed in
+ // src/core/lib/tsi/ssl_transport_security.c.
+ const char *cipher_list =
+ "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-"
+ "SHA384:ECDHE-RSA-AES256-GCM-SHA384";
+ if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
+ ERR_print_errors_fp(stderr);
+ gpr_log(GPR_ERROR, "Couldn't set server cipher list.");
+ abort();
+ }
+
+ // Register the ALPN selection callback.
+ SSL_CTX_set_alpn_select_cb(ctx, alpn_select_cb, args->alpn_preferred);
+
+ // bind/list/accept at TCP layer.
+ const int sock = create_socket(args->port);
+ gpr_log(GPR_INFO, "Server listening on port %d", args->port);
+ struct sockaddr_in addr;
+ socklen_t len = sizeof(addr);
+ const int client = accept(sock, (struct sockaddr *)&addr, &len);
+ if (client < 0) {
+ perror("Unable to accept");
+ abort();
+ }
+
+ // Establish a SSL* and accept at SSL layer.
+ SSL *ssl = SSL_new(ctx);
+ GPR_ASSERT(ssl);
+ SSL_set_fd(ssl, client);
+ if (SSL_accept(ssl) <= 0) {
+ ERR_print_errors_fp(stderr);
+ gpr_log(GPR_ERROR, "Handshake failed.");
+ } else {
+ gpr_log(GPR_INFO, "Handshake successful.");
+ }
+
+ // Wait until the client drops its connection.
+ char buf;
+ while (SSL_read(ssl, &buf, sizeof(buf)) > 0);
+
+ SSL_free(ssl);
+ close(client);
+ close(sock);
+ SSL_CTX_free(ctx);
+ EVP_cleanup();
+}
+
+// This test launches a minimal TLS server on a separate thread and then
+// establishes a TLS handshake via the core library to the server. The TLS
+// server validates ALPN aspects of the handshake and supplies the protocol
+// specified in the server_alpn_preferred argument to the client.
+static bool client_ssl_test(char *server_alpn_preferred) {
+ bool success = true;
+
+ grpc_init();
+ const int port = grpc_pick_unused_port_or_die();
+
+ // Launch the TLS server thread.
+ gpr_thd_options thdopt = gpr_thd_options_default();
+ gpr_thd_id thdid;
+ gpr_thd_options_set_joinable(&thdopt);
+ server_args args = { .port = port, .alpn_preferred = server_alpn_preferred };
+ GPR_ASSERT(gpr_thd_new(&thdid, server_thread, &args, &thdopt));
+
+ // Load key pair and establish client SSL credentials.
+ grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
+ gpr_slice ca_slice, cert_slice, key_slice;
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
+ const char *ca_cert = (const char*)GPR_SLICE_START_PTR(ca_slice);
+ pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice);
+ pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice);
+ grpc_channel_credentials *ssl_creds =
+ grpc_ssl_credentials_create(ca_cert, &pem_key_cert_pair, NULL);
+
+ // Establish a channel pointing at the TLS server. Since the gRPC runtime is
+ // lazy, this won't necessarily establish a connection yet.
+ char *target;
+ gpr_asprintf(&target, "127.0.0.1:%d", port);
+ grpc_arg ssl_name_override = {GRPC_ARG_STRING,
+ GRPC_SSL_TARGET_NAME_OVERRIDE_ARG,
+ {"foo.test.google.fr"}};
+ grpc_channel_args grpc_args;
+ grpc_args.num_args = 1;
+ grpc_args.args = &ssl_name_override;
+ grpc_channel *channel = grpc_secure_channel_create(ssl_creds, target, &grpc_args, NULL);
+ GPR_ASSERT(channel);
+ gpr_free(target);
+
+ // Initially the channel will be idle, the
+ // grpc_channel_check_connectivity_state triggers an attempt to connect.
+ GPR_ASSERT(grpc_channel_check_connectivity_state(
+ channel, 1 /* try_to_connect */) == GRPC_CHANNEL_IDLE);
+
+ // Wait a bounded number of times for the channel to be ready. When the
+ // channel is ready, the initial TLS handshake will have successfully
+ // completed and we know that the client's ALPN list satisfied the server.
+ int retries = 10;
+ grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
+ grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
+ while (state != GRPC_CHANNEL_READY && retries-- > 0) {
+ grpc_channel_watch_connectivity_state(
+ channel, state, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), cq, NULL);
+ gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
+ state =
+ grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */);
+ }
+ grpc_completion_queue_destroy(cq);
+ if (retries < 0) {
+ success = false;
+ }
+
+ grpc_channel_destroy(channel);
+ grpc_channel_credentials_release(ssl_creds);
+ gpr_slice_unref(cert_slice);
+ gpr_slice_unref(key_slice);
+ gpr_slice_unref(ca_slice);
+
+ gpr_thd_join(thdid);
+
+ grpc_shutdown();
+
+ return success;
+}
+
+int main(int argc, char *argv[]) {
+ // Handshake succeeeds when the server has grpc-exp as the ALPN preference.
+ GPR_ASSERT(client_ssl_test("grpc-exp"));
+ // Handshake succeeeds when the server has h2 as the ALPN preference. This
+ // covers legacy gRPC servers which don't support grpc-exp.
+ GPR_ASSERT(client_ssl_test("h2"));
+ // Handshake fails when the server uses a fake protocol as its ALPN
+ // preference. This validates the client is correctly validating ALPN returns
+ // and sanity checks the client_ssl_test.
+ GPR_ASSERT(!client_ssl_test("foo"));
+ return 0;
+}
diff --git a/test/core/handshake/server_ssl.c b/test/core/handshake/server_ssl.c
new file mode 100644
index 0000000000..b05d168a93
--- /dev/null
+++ b/test/core/handshake/server_ssl.c
@@ -0,0 +1,264 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <arpa/inet.h>
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/thd.h>
+#include "src/core/lib/iomgr/load_file.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+#define SSL_CERT_PATH "src/core/lib/tsi/test_creds/server1.pem"
+#define SSL_KEY_PATH "src/core/lib/tsi/test_creds/server1.key"
+#define SSL_CA_PATH "src/core/lib/tsi/test_creds/ca.pem"
+
+// Handshake completed signal to server thread.
+static bool client_handshake_complete = false;
+
+static int create_socket(int port) {
+ int s;
+ struct sockaddr_in addr;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons((uint16_t)port);
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s < 0) {
+ perror("Unable to create socket");
+ return -1;
+ }
+
+ if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ perror("Unable to connect");
+ return -1;
+ }
+
+ return s;
+}
+
+// Simple gRPC server. This listens until client_handshake_complete occurs.
+static void server_thread(void *arg) {
+ const int port = *(int *)arg;
+
+ // Load key pair and establish server SSL credentials.
+ grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
+ gpr_slice ca_slice, cert_slice, key_slice;
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
+ GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
+ grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
+ const char *ca_cert = (const char *)GPR_SLICE_START_PTR(ca_slice);
+ pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice);
+ pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice);
+ grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create(
+ ca_cert, &pem_key_cert_pair, 1, 0, NULL);
+
+ // Start server listening on local port.
+ char *addr;
+ gpr_asprintf(&addr, "127.0.0.1:%d", port);
+ grpc_server *server = grpc_server_create(NULL, NULL);
+ GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
+
+ grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
+
+ grpc_server_register_completion_queue(server, cq, NULL);
+ grpc_server_start(server);
+
+ // Wait a bounded number of time until client_handshake_complete is set,
+ // sleeping between polls.
+ int retries = 10;
+ while (!client_handshake_complete && retries-- > 0) {
+ const gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
+ }
+
+ gpr_log(GPR_INFO, "Shutting down server");
+ grpc_server_shutdown_and_notify(server, cq, NULL);
+ grpc_completion_queue_shutdown(cq);
+
+ const gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
+
+ grpc_completion_queue_destroy(cq);
+ grpc_server_credentials_release(ssl_creds);
+ gpr_slice_unref(cert_slice);
+ gpr_slice_unref(key_slice);
+ gpr_slice_unref(ca_slice);
+}
+
+// This test launches a gRPC server on a separate thread and then establishes a
+// TLS handshake via a minimal TLS client. The TLS client has configurable (via
+// alpn_list) ALPN settings and can probe at the supported ALPN preferences
+// using this (via alpn_expected).
+static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
+ const char *alpn_expected) {
+ bool success = true;
+
+ grpc_init();
+ int port = grpc_pick_unused_port_or_die();
+ client_handshake_complete = false;
+
+ // Launch the gRPC server thread.
+ gpr_thd_options thdopt = gpr_thd_options_default();
+ gpr_thd_id thdid;
+ gpr_thd_options_set_joinable(&thdopt);
+ GPR_ASSERT(gpr_thd_new(&thdid, server_thread, &port, &thdopt));
+
+ SSL_load_error_strings();
+ OpenSSL_add_ssl_algorithms();
+
+ const SSL_METHOD *method = TLSv1_2_client_method();
+ SSL_CTX *ctx = SSL_CTX_new(method);
+ if (!ctx) {
+ perror("Unable to create SSL context");
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+
+ // Load key pair.
+ if (SSL_CTX_use_certificate_file(ctx, SSL_CERT_PATH, SSL_FILETYPE_PEM) < 0) {
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+ if (SSL_CTX_use_PrivateKey_file(ctx, SSL_KEY_PATH, SSL_FILETYPE_PEM) < 0) {
+ ERR_print_errors_fp(stderr);
+ abort();
+ }
+
+ // Set the cipher list to match the one expressed in
+ // src/core/lib/tsi/ssl_transport_security.c.
+ const char *cipher_list =
+ "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-"
+ "SHA384:ECDHE-RSA-AES256-GCM-SHA384";
+ if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
+ ERR_print_errors_fp(stderr);
+ gpr_log(GPR_ERROR, "Couldn't set server cipher list.");
+ abort();
+ }
+
+ // Configure ALPN list the client will send to the server. This must match the
+ // wire format, see documentation for SSL_CTX_set_alpn_protos.
+ unsigned int alpn_protos_len = alpn_list_len;
+ for (unsigned int i = 0; i < alpn_list_len; ++i) {
+ alpn_protos_len += (unsigned int)strlen(alpn_list[i]);
+ }
+ unsigned char *alpn_protos = gpr_malloc(alpn_protos_len);
+ unsigned char *p = alpn_protos;
+ for (unsigned int i = 0; i < alpn_list_len; ++i) {
+ const uint8_t len = (uint8_t)strlen(alpn_list[i]);
+ *p++ = len;
+ memcpy(p, alpn_list[i], len);
+ p += len;
+ }
+ GPR_ASSERT(SSL_CTX_set_alpn_protos(ctx, alpn_protos, alpn_protos_len) == 0);
+
+ // Try and connect to server. We allow a bounded number of retries as we might
+ // be racing with the server setup on its separate thread.
+ int retries = 10;
+ int sock = -1;
+ while (sock == -1 && retries-- > 0) {
+ sock = create_socket(port);
+ if (sock < 0) {
+ sleep(1);
+ }
+ }
+ GPR_ASSERT(sock > 0);
+ gpr_log(GPR_INFO, "Connected to server on port %d", port);
+
+ // Establish a SSL* and connect at SSL layer.
+ SSL *ssl = SSL_new(ctx);
+ GPR_ASSERT(ssl);
+ SSL_set_fd(ssl, sock);
+ if (SSL_connect(ssl) <= 0) {
+ ERR_print_errors_fp(stderr);
+ gpr_log(GPR_ERROR, "Handshake failed.");
+ success = false;
+ } else {
+ gpr_log(GPR_INFO, "Handshake successful.");
+ // Validate ALPN preferred by server matches alpn_expected.
+ const unsigned char *alpn_selected;
+ unsigned int alpn_selected_len;
+ SSL_get0_alpn_selected(ssl, &alpn_selected, &alpn_selected_len);
+ if (strlen(alpn_expected) != alpn_selected_len ||
+ strncmp((const char *)alpn_selected, alpn_expected, alpn_selected_len) != 0) {
+ gpr_log(GPR_ERROR, "Unexpected ALPN protocol preference");
+ success = false;
+ }
+ }
+ client_handshake_complete = true;
+
+ gpr_free(alpn_protos);
+ SSL_CTX_free(ctx);
+ EVP_cleanup();
+
+ gpr_thd_join(thdid);
+
+ grpc_shutdown();
+
+ return success;
+}
+
+int main(int argc, char *argv[]) {
+ // Handshake succeeeds when the client supplies the standard ALPN list.
+ const char *full_alpn_list[] = {"grpc-exp", "h2"};
+ GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
+ // Handshake succeeeds when the client supplies only h2 as the ALPN list. This
+ // covers legacy gRPC clients which don't support grpc-exp.
+ const char *h2_only_alpn_list[] = {"h2"};
+ GPR_ASSERT(server_ssl_test(h2_only_alpn_list, 1, "h2"));
+ // Handshake succeeds when the client supplies superfluous ALPN entries and
+ // also when h2 precedes gprc-exp.
+ const char *extra_alpn_list[] = {"foo", "h2", "bar", "grpc-exp"};
+ GPR_ASSERT(server_ssl_test(extra_alpn_list, 4, "h2"));
+ // Handshake fails when the client uses a fake protocol as its only ALPN
+ // preference. This validates the server is correctly validating ALPN
+ // and sanity checks the server_ssl_test.
+ const char *fake_alpn_list[] = {"foo"};
+ GPR_ASSERT(!server_ssl_test(fake_alpn_list, 1, "foo"));
+ return 0;
+}