Gracefully free a NULL HANDSHAKE_RESULT
[openssl.git] / test / handshake_helper.c
index 77852ad586a642f272029450e1cbd6e85bfc0b0f..3e7f12948e478bc6bb0540ead0fd1a68a67685a5 100644 (file)
 #include <openssl/ssl.h>
 
 #include "handshake_helper.h"
+#include "testutil.h"
 
 HANDSHAKE_RESULT *HANDSHAKE_RESULT_new()
 {
-    HANDSHAKE_RESULT *ret;
-    ret = OPENSSL_zalloc(sizeof(*ret));
-    OPENSSL_assert(ret != NULL);
+    HANDSHAKE_RESULT *ret = OPENSSL_zalloc(sizeof(*ret));
+    TEST_check(ret != NULL);
     return ret;
 }
 
 void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result)
 {
+    if (result == NULL)
+        return;
     OPENSSL_free(result->client_npn_negotiated);
     OPENSSL_free(result->server_npn_negotiated);
     OPENSSL_free(result->client_alpn_negotiated);
@@ -172,11 +174,11 @@ static void parse_protos(const char *protos, unsigned char **out, size_t *outlen
     len = strlen(protos);
 
     /* Should never have reuse. */
-    OPENSSL_assert(*out == NULL);
+    TEST_check(*out == NULL);
 
     /* Test values are small, so we omit length limit checks. */
     *out = OPENSSL_malloc(len + 1);
-    OPENSSL_assert(*out != NULL);
+    TEST_check(*out != NULL);
     *outlen = len + 1;
 
     /*
@@ -189,16 +191,17 @@ static void parse_protos(const char *protos, unsigned char **out, size_t *outlen
     i = prefix + 1;
     while (i <= len) {
         if ((*out)[i] == ',') {
-            OPENSSL_assert(i - 1 - prefix > 0);
+            TEST_check(i - 1 - prefix > 0);
             (*out)[prefix] = i - 1 - prefix;
             prefix = i;
         }
         i++;
     }
-    OPENSSL_assert(len - prefix > 0);
+    TEST_check(len - prefix > 0);
     (*out)[prefix] = len - prefix;
 }
 
+#ifndef OPENSSL_NO_NEXTPROTONEG
 /*
  * The client SHOULD select the first protocol advertised by the server that it
  * also supports.  In the event that the client doesn't support any of server's
@@ -216,8 +219,7 @@ static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen,
                                 ctx_data->npn_protocols,
                                 ctx_data->npn_protocols_len);
     /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */
-    OPENSSL_assert(ret == OPENSSL_NPN_NEGOTIATED
-                   || ret == OPENSSL_NPN_NO_OVERLAP);
+    TEST_check(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP);
     return SSL_TLSEXT_ERR_OK;
 }
 
@@ -229,6 +231,7 @@ static int server_npn_cb(SSL *s, const unsigned char **data,
     *len = ctx_data->npn_protocols_len;
     return SSL_TLSEXT_ERR_OK;
 }
+#endif
 
 /*
  * The server SHOULD select the most highly preferred protocol that it supports
@@ -261,19 +264,21 @@ static int server_alpn_cb(SSL *s, const unsigned char **out,
         : SSL_TLSEXT_ERR_NOACK;
 }
 
-
 /*
  * Configure callbacks and other properties that can't be set directly
  * in the server/client CONF.
  */
 static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
                                     SSL_CTX *client_ctx,
-                                    const SSL_TEST_CTX *test_ctx,
+                                    const SSL_TEST_EXTRA_CONF *extra,
                                     CTX_DATA *server_ctx_data,
                                     CTX_DATA *server2_ctx_data,
                                     CTX_DATA *client_ctx_data)
 {
-    switch (test_ctx->client_verify_callback) {
+    unsigned char *ticket_keys;
+    size_t ticket_key_len;
+
+    switch (extra->client.verify_callback) {
     case SSL_TEST_VERIFY_ACCEPT_ALL:
         SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb,
                                          NULL);
@@ -287,7 +292,7 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
     }
 
     /* link the two contexts for SNI purposes */
-    switch (test_ctx->servername_callback) {
+    switch (extra->server.servername_callback) {
     case SSL_TEST_SERVERNAME_IGNORE_MISMATCH:
         SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb);
         SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
@@ -309,64 +314,90 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
         SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx,
                                          do_not_call_session_ticket_cb);
 
-    if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_BROKEN) {
+    if (extra->server.broken_session_ticket) {
         SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_cb);
     }
-
-    if (test_ctx->server_npn_protocols != NULL) {
-        parse_protos(test_ctx->server_npn_protocols,
+#ifndef OPENSSL_NO_NEXTPROTONEG
+    if (extra->server.npn_protocols != NULL) {
+        parse_protos(extra->server.npn_protocols,
                      &server_ctx_data->npn_protocols,
                      &server_ctx_data->npn_protocols_len);
         SSL_CTX_set_next_protos_advertised_cb(server_ctx, server_npn_cb,
                                               server_ctx_data);
     }
-    if (test_ctx->server2_npn_protocols != NULL) {
-        parse_protos(test_ctx->server2_npn_protocols,
+    if (extra->server2.npn_protocols != NULL) {
+        parse_protos(extra->server2.npn_protocols,
                      &server2_ctx_data->npn_protocols,
                      &server2_ctx_data->npn_protocols_len);
-        OPENSSL_assert(server2_ctx != NULL);
+        TEST_check(server2_ctx != NULL);
         SSL_CTX_set_next_protos_advertised_cb(server2_ctx, server_npn_cb,
                                               server2_ctx_data);
     }
-    if (test_ctx->client_npn_protocols != NULL) {
-        parse_protos(test_ctx->client_npn_protocols,
+    if (extra->client.npn_protocols != NULL) {
+        parse_protos(extra->client.npn_protocols,
                      &client_ctx_data->npn_protocols,
                      &client_ctx_data->npn_protocols_len);
         SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb,
                                          client_ctx_data);
     }
-    if (test_ctx->server_alpn_protocols != NULL) {
-        parse_protos(test_ctx->server_alpn_protocols,
+#endif
+    if (extra->server.alpn_protocols != NULL) {
+        parse_protos(extra->server.alpn_protocols,
                      &server_ctx_data->alpn_protocols,
                      &server_ctx_data->alpn_protocols_len);
         SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data);
     }
-    if (test_ctx->server2_alpn_protocols != NULL) {
-        OPENSSL_assert(server2_ctx != NULL);
-        parse_protos(test_ctx->server2_alpn_protocols,
+    if (extra->server2.alpn_protocols != NULL) {
+        TEST_check(server2_ctx != NULL);
+        parse_protos(extra->server2.alpn_protocols,
                      &server2_ctx_data->alpn_protocols,
                      &server2_ctx_data->alpn_protocols_len);
         SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, server2_ctx_data);
     }
-    if (test_ctx->client_alpn_protocols != NULL) {
+    if (extra->client.alpn_protocols != NULL) {
         unsigned char *alpn_protos = NULL;
         size_t alpn_protos_len;
-        parse_protos(test_ctx->client_alpn_protocols,
+        parse_protos(extra->client.alpn_protocols,
                      &alpn_protos, &alpn_protos_len);
         /* Reversed return value convention... */
-        OPENSSL_assert(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos,
-                                               alpn_protos_len) == 0);
+        TEST_check(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos,
+                                           alpn_protos_len) == 0);
         OPENSSL_free(alpn_protos);
     }
+
+    /*
+     * Use fixed session ticket keys so that we can decrypt a ticket created with
+     * one CTX in another CTX. Don't address server2 for the moment.
+     */
+    ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0);
+    ticket_keys = OPENSSL_zalloc(ticket_key_len);
+    TEST_check(ticket_keys != NULL);
+    TEST_check(SSL_CTX_set_tlsext_ticket_keys(server_ctx, ticket_keys,
+                                              ticket_key_len) == 1);
+    OPENSSL_free(ticket_keys);
+
+#ifndef OPENSSL_NO_CT
+    TEST_check(SSL_CTX_set_default_ctlog_list_file(client_ctx));
+    switch (extra->client.ct_validation) {
+    case SSL_TEST_CT_VALIDATION_PERMISSIVE:
+        TEST_check(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_PERMISSIVE));
+        break;
+    case SSL_TEST_CT_VALIDATION_STRICT:
+        TEST_check(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT));
+        break;
+    case SSL_TEST_CT_VALIDATION_NONE:
+        break;
+    }
+#endif
 }
 
 /* Configure per-SSL callbacks and other properties. */
 static void configure_handshake_ssl(SSL *server, SSL *client,
-                                    const SSL_TEST_CTX *test_ctx)
+                                    const SSL_TEST_EXTRA_CONF *extra)
 {
-    if (test_ctx->servername != SSL_TEST_SERVERNAME_NONE)
+    if (extra->client.servername != SSL_TEST_SERVERNAME_NONE)
         SSL_set_tlsext_host_name(client,
-                                 ssl_servername_name(test_ctx->servername));
+                                 ssl_servername_name(extra->client.servername));
 }
 
 
@@ -376,16 +407,31 @@ typedef enum {
     PEER_ERROR
 } peer_status_t;
 
-static peer_status_t do_handshake_step(SSL *ssl)
+/*
+ * RFC 5246 says:
+ *
+ * Note that as of TLS 1.1,
+ *     failure to properly close a connection no longer requires that a
+ *     session not be resumed.  This is a change from TLS 1.0 to conform
+ *     with widespread implementation practice.
+ *
+ * However,
+ * (a) OpenSSL requires that a connection be shutdown for all protocol versions.
+ * (b) We test lower versions, too.
+ * So we just implement shutdown. We do a full bidirectional shutdown so that we
+ * can compare sent and received close_notify alerts and get some test coverage
+ * for SSL_shutdown as a bonus.
+ */
+static peer_status_t do_handshake_step(SSL *ssl, int shutdown)
 {
     int ret;
 
-    ret = SSL_do_handshake(ssl);
+    ret = shutdown ? SSL_shutdown(ssl) : SSL_do_handshake(ssl);
 
     if (ret == 1) {
         return PEER_SUCCESS;
     } else if (ret == 0) {
-        return PEER_ERROR;
+        return shutdown ? PEER_RETRY : PEER_ERROR;
     } else {
         int error = SSL_get_error(ssl, ret);
         /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
@@ -478,21 +524,23 @@ static char *dup_str(const unsigned char *in, size_t len)
         return NULL;
 
     /* Assert that the string does not contain NUL-bytes. */
-    OPENSSL_assert(OPENSSL_strnlen((const char*)(in), len) == len);
+    TEST_check(OPENSSL_strnlen((const char*)(in), len) == len);
     ret = OPENSSL_strndup((const char*)(in), len);
-    OPENSSL_assert(ret != NULL);
+    TEST_check(ret != NULL);
     return ret;
 }
 
-HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
-                               SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx)
+static HANDSHAKE_RESULT *do_handshake_internal(
+    SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx,
+    const SSL_TEST_EXTRA_CONF *extra, SSL_SESSION *session_in,
+    SSL_SESSION **session_out)
 {
     SSL *server, *client;
     BIO *client_to_server, *server_to_client;
     HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
     CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;
     HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();
-    int client_turn = 1;
+    int client_turn = 1, shutdown = 0;
     peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
     handshake_status_t status = HANDSHAKE_RETRY;
     unsigned char* tick = NULL;
@@ -506,14 +554,20 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
     memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
     memset(&client_ctx_data, 0, sizeof(client_ctx_data));
 
-    configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx,
+    configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, extra,
                             &server_ctx_data, &server2_ctx_data, &client_ctx_data);
 
     server = SSL_new(server_ctx);
     client = SSL_new(client_ctx);
-    OPENSSL_assert(server != NULL && client != NULL);
-
-    configure_handshake_ssl(server, client, test_ctx);
+    TEST_check(server != NULL);
+    TEST_check(client != NULL);
+
+    configure_handshake_ssl(server, client, extra);
+    if (session_in != NULL) {
+        /* In case we're testing resumption without tickets. */
+        TEST_check(SSL_CTX_add_session(server_ctx, session_in));
+        TEST_check(SSL_set_session(client, session_in));
+    }
 
     memset(&server_ex_data, 0, sizeof(server_ex_data));
     memset(&client_ex_data, 0, sizeof(client_ex_data));
@@ -523,7 +577,7 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
     client_to_server = BIO_new(BIO_s_mem());
     server_to_client = BIO_new(BIO_s_mem());
 
-    OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
+    TEST_check(client_to_server != NULL && server_to_client != NULL);
 
     /* Non-blocking bio. */
     BIO_set_nbio(client_to_server, 1);
@@ -534,16 +588,16 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
 
     /* The bios are now owned by the SSL object. */
     SSL_set_bio(client, server_to_client, client_to_server);
-    OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
-    OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
+    TEST_check(BIO_up_ref(server_to_client) > 0);
+    TEST_check(BIO_up_ref(client_to_server) > 0);
     SSL_set_bio(server, client_to_server, server_to_client);
 
     ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
-    OPENSSL_assert(ex_data_idx >= 0);
+    TEST_check(ex_data_idx >= 0);
 
-    OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
+    TEST_check(SSL_set_ex_data(server, ex_data_idx,
                                    &server_ex_data) == 1);
-    OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
+    TEST_check(SSL_set_ex_data(client, ex_data_idx,
                                    &client_ex_data) == 1);
 
     SSL_set_info_callback(server, &info_cb);
@@ -559,19 +613,26 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
      */
     for(;;) {
         if (client_turn) {
-            client_status = do_handshake_step(client);
+            client_status = do_handshake_step(client, shutdown);
             status = handshake_status(client_status, server_status,
                                       1 /* client went last */);
         } else {
-            server_status = do_handshake_step(server);
+            server_status = do_handshake_step(server, shutdown);
             status = handshake_status(server_status, client_status,
                                       0 /* server went last */);
         }
 
         switch (status) {
         case HANDSHAKE_SUCCESS:
-            ret->result = SSL_TEST_SUCCESS;
-            goto err;
+            if (shutdown) {
+                ret->result = SSL_TEST_SUCCESS;
+                goto err;
+            } else {
+                client_status = server_status = PEER_RETRY;
+                shutdown = 1;
+                client_turn = 1;
+                break;
+            }
         case CLIENT_ERROR:
             ret->result = SSL_TEST_CLIENT_FAIL;
             goto err;
@@ -603,11 +664,13 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
         ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
     ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
 
+#ifndef OPENSSL_NO_NEXTPROTONEG
     SSL_get0_next_proto_negotiated(client, &proto, &proto_len);
     ret->client_npn_negotiated = dup_str(proto, proto_len);
 
     SSL_get0_next_proto_negotiated(server, &proto, &proto_len);
     ret->server_npn_negotiated = dup_str(proto, proto_len);
+#endif
 
     SSL_get0_alpn_selected(client, &proto, &proto_len);
     ret->client_alpn_negotiated = dup_str(proto, proto_len);
@@ -615,10 +678,46 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
     SSL_get0_alpn_selected(server, &proto, &proto_len);
     ret->server_alpn_negotiated = dup_str(proto, proto_len);
 
+    ret->client_resumed = SSL_session_reused(client);
+    ret->server_resumed = SSL_session_reused(server);
+
+    if (session_out != NULL)
+        *session_out = SSL_get1_session(client);
+
     ctx_data_free_data(&server_ctx_data);
     ctx_data_free_data(&server2_ctx_data);
     ctx_data_free_data(&client_ctx_data);
+
     SSL_free(server);
     SSL_free(client);
     return ret;
 }
+
+HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
+                               SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx,
+                               SSL_CTX *resume_client_ctx,
+                               const SSL_TEST_CTX *test_ctx)
+{
+    HANDSHAKE_RESULT *result;
+    SSL_SESSION *session = NULL;
+
+    result = do_handshake_internal(server_ctx, server2_ctx, client_ctx,
+                                   &test_ctx->extra, NULL, &session);
+    if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_SIMPLE)
+        goto end;
+
+    TEST_check(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME);
+
+    if (result->result != SSL_TEST_SUCCESS) {
+        result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED;
+        return result;
+    }
+
+    HANDSHAKE_RESULT_free(result);
+    /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
+    result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx,
+                                   &test_ctx->resume_extra, session, NULL);
+ end:
+    SSL_SESSION_free(session);
+    return result;
+}