From 8b456b90e037e87c0a4e2a1f5d401bab34a81084 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Tue, 1 Jun 2021 15:45:45 +0700 Subject: [PATCH] add test case for tls export/import --- src/internal.c | 1 + tests/api.c | 301 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+) diff --git a/src/internal.c b/src/internal.c index 9c3049c76..fea5f5023 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1640,6 +1640,7 @@ int wolfSSL_session_import_internal(WOLFSSL* ssl, const unsigned char* buf, /* check if importing state only */ return wolfSSL_dtls_import_state_internal(ssl, buf, sz); #else + WOLFSSL_MSG("Invalid serialized session protocol value"); ret = BAD_FUNC_ARG; #endif } diff --git a/tests/api.c b/tests/api.c index 616ae4090..46ef4fa0f 100644 --- a/tests/api.c +++ b/tests/api.c @@ -5649,6 +5649,306 @@ static void test_wolfSSL_dtls_export(void) #endif } + +#if defined(WOLFSSL_SESSION_EXPORT) +static byte canned_client_session[] = { + 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, + 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00, + 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00, + 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, + 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D, + 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D, + 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E, + 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1, + 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE, + 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C, + 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91, + 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E, + 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02, + 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90, + 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B, + 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17, + 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D, + 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7, + 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A, + 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71, + 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, + 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20, + 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F, + 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE, + 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF, + 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54, + 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x03 +}; + + +static byte canned_server_session[] = { + 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, + 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11, + 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00, + 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, + 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D, + 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D, + 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E, + 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1, + 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE, + 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C, + 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91, + 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E, + 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02, + 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90, + 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B, + 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17, + 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D, + 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7, + 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A, + 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71, + 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, + 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20, + 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF, + 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54, + 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F, + 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE, + 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x04 +}; + + +static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args) +{ + SOCKET_T sockfd = 0; + SOCKET_T clientfd = 0; + word16 port; + + callback_functions* cbf; + WOLFSSL_CTX* ctx = 0; + WOLFSSL* ssl = 0; + + char msg[] = "I hear you fa shizzle!"; + char input[1024]; + int idx; + +#ifdef WOLFSSL_TIRTOS + fdOpenSession(Task_self()); +#endif + + ((func_args*)args)->return_code = TEST_FAIL; + cbf = ((func_args*)args)->callbacks; + + { + WOLFSSL_METHOD* method = NULL; + if (cbf != NULL && cbf->method != NULL) { + method = cbf->method(); + } + else { + method = wolfTLSv1_2_server_method(); + } + ctx = wolfSSL_CTX_new(method); + } + if (ctx == NULL) { + goto done; + } + wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"); + +#if defined(USE_WINDOWS_API) + port = ((func_args*)args)->signal->port; +#elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \ + !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS) + /* Let tcp_listen assign port */ + port = 0; +#else + /* Use default port */ + port = wolfSSLPort; +#endif + + /* do it here to detect failure */ + tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0); + CloseSocket(sockfd); + + /* call ctx setup callback */ + if (cbf != NULL && cbf->ctx_ready != NULL) { + cbf->ctx_ready(ctx); + } + + ssl = wolfSSL_new(ctx); + if (ssl == NULL) { + goto done; + } + + /* call ssl setup callback */ + if (cbf != NULL && cbf->ssl_ready != NULL) { + cbf->ssl_ready(ssl); + } + AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session, + sizeof(canned_server_session)), sizeof(canned_server_session)); + wolfSSL_set_fd(ssl, clientfd); + + idx = wolfSSL_read(ssl, input, sizeof(input)-1); + if (idx > 0) { + input[idx] = '\0'; + printf("Client message export/import: %s\n", input); + } + else { + printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx)); + goto done; + } + + if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) { + /*err_sys("SSL_write failed");*/ +#ifdef WOLFSSL_TIRTOS + return; +#else + return 0; +#endif + } + +#ifdef WOLFSSL_TIRTOS + Task_yield(); +#endif + + ((func_args*)args)->return_code = TEST_SUCCESS; + +done: + wolfSSL_shutdown(ssl); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + + CloseSocket(clientfd); + +#ifdef WOLFSSL_TIRTOS + fdCloseSession(Task_self()); +#endif + +#if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \ + && defined(HAVE_THREAD_LS) + wc_ecc_fp_free(); /* free per thread cache */ +#endif + +#if defined(HAVE_SESSION_TICKET) && \ + ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM)) +#if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM) + OpenSSLTicketCleanup(); +#elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) + TicketCleanup(); +#endif +#endif + +#ifndef WOLFSSL_TIRTOS + return 0; +#endif +} +#endif + +static void test_wolfSSL_tls_export(void) +{ +#if defined(WOLFSSL_SESSION_EXPORT) + SOCKET_T sockfd = 0; + WOLFSSL_CTX* ctx = 0; + WOLFSSL* ssl = 0; + char msg[64] = "hello wolfssl!"; + char reply[1024]; + word32 replySz; + int msgSz = (int)XSTRLEN(msg); + + tcp_ready ready; + func_args server_args; + THREAD_TYPE serverThread; + callback_functions server_cbf; + + printf(testingFmt, "wolfSSL_tls_export()"); +#ifdef WOLFSSL_TIRTOS + fdOpenSession(Task_self()); +#endif + + InitTcpReady(&ready); + +#if defined(USE_WINDOWS_API) + /* use RNG to get random port if using windows */ + ready.port = GetRandomPort(); +#endif + + XMEMSET(&server_args, 0, sizeof(func_args)); + XMEMSET(&server_cbf, 0, sizeof(callback_functions)); + server_cbf.method = wolfTLSv1_2_server_method; + server_args.callbacks = &server_cbf; + server_args.signal = &ready; + + start_thread(tls_export_server, &server_args, &serverThread); + wait_tcp_ready(&server_args); + + +#ifdef WOLFSSL_TIRTOS + fdOpenSession(Task_self()); +#endif + + AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())); + wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"); + AssertNotNull(ssl = wolfSSL_new(ctx)); + tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl); + AssertIntEQ(wolfSSL_tls_import(ssl, canned_client_session, + sizeof(canned_client_session)), sizeof(canned_client_session)); + replySz = sizeof(reply); + AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0); + AssertIntEQ(XMEMCMP(reply, canned_client_session, replySz), 0); + wolfSSL_set_fd(ssl, sockfd); + + AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz); + AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0); + + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + + CloseSocket(sockfd); + +#ifdef WOLFSSL_TIRTOS + fdCloseSession(Task_self()); +#endif + +#if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \ + && defined(HAVE_THREAD_LS) + wc_ecc_fp_free(); /* free per thread cache */ +#endif + + join_thread(serverThread); + + AssertIntEQ(server_args.return_code, TEST_SUCCESS); + FreeTcpReady(&ready); + +#ifdef WOLFSSL_TIRTOS + fdOpenSession(Task_self()); +#endif + + printf(resultFmt, passed); +#endif +} + /*----------------------------------------------------------------------------* | TLS extensions tests *----------------------------------------------------------------------------*/ @@ -49187,6 +49487,7 @@ void ApiTest(void) #endif test_wolfSSL_CTX_verifyDepth_ServerClient(); test_wolfSSL_dtls_export(); + test_wolfSSL_tls_export(); #endif AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS); AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);