From cb4b57c5c714ede45b1c4a008c9aa26d2dd9eab0 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 30 Sep 2021 10:08:47 -0600 Subject: [PATCH] add tls 1.3 test case --- doc/dox_comments/header_files/ssl.h | 9 +- src/internal.c | 24 +++-- src/ssl.c | 12 +-- tests/api.c | 158 +++++++++++++++++++++++++--- wolfssl/internal.h | 4 +- 5 files changed, 171 insertions(+), 36 deletions(-) diff --git a/doc/dox_comments/header_files/ssl.h b/doc/dox_comments/header_files/ssl.h index f5b8a8512..4fceb5033 100644 --- a/doc/dox_comments/header_files/ssl.h +++ b/doc/dox_comments/header_files/ssl.h @@ -702,11 +702,14 @@ WOLFSSL_API int wolfSSL_dtls_export(WOLFSSL* ssl, unsigned char* buf, unsigned int* sz); /*! - \brief Used to export a serialized TLS session. In most cases - wolfSSL_set_session should be used instead, this function is for importing a - serialized state of the connection. + \brief Used to export a serialized TLS session. This function is for + importing a serialized state of the connection. + In most cases wolfSSL_get_session should be used instead of + wolfSSL_tls_export. Additional debug info can be displayed with the macro WOLFSSL_SESSION_EXPORT_DEBUG defined. + WARNING: buf contains sensitive information about the state and is best to + be encrypted before storing if stored. \return the number of bytes written into buffer 'buf' diff --git a/src/internal.c b/src/internal.c index 7447d904a..9b5e2def2 100644 --- a/src/internal.c +++ b/src/internal.c @@ -601,7 +601,7 @@ static int ExportCipherSpecState(WOLFSSL* ssl, byte* exp, word32 len, byte ver, return BAD_FUNC_ARG; } - specs= &ssl->specs; + specs = &ssl->specs; if (WOLFSSL_EXPORT_SPC_SZ > len) { return BUFFER_E; } @@ -1186,10 +1186,10 @@ static int ExportOptions(WOLFSSL* ssl, byte* exp, word32 len, byte ver, exp[idx++] = options->asyncState; if (type == WOLFSSL_EXPORT_TLS) { - exp[idx++] = ssl->options.disallowEncThenMac; - exp[idx++] = ssl->options.encThenMac; - exp[idx++] = ssl->options.startedETMRead; - exp[idx++] = ssl->options.startedETMWrite; + exp[idx++] = options->disallowEncThenMac; + exp[idx++] = options->encThenMac; + exp[idx++] = options->startedETMRead; + exp[idx++] = options->startedETMWrite; } /* version of connection */ @@ -1362,10 +1362,10 @@ static int ImportOptions(WOLFSSL* ssl, const byte* exp, word32 len, byte ver, options->asyncState = exp[idx++]; if (type == WOLFSSL_EXPORT_TLS) { - ssl->options.disallowEncThenMac = exp[idx++]; - ssl->options.encThenMac = exp[idx++]; - ssl->options.startedETMRead = exp[idx++]; - ssl->options.startedETMWrite = exp[idx++]; + options->disallowEncThenMac = exp[idx++]; + options->encThenMac = exp[idx++]; + options->startedETMRead = exp[idx++]; + options->startedETMWrite = exp[idx++]; } /* version of connection */ @@ -1374,6 +1374,12 @@ static int ImportOptions(WOLFSSL* ssl, const byte* exp, word32 len, byte ver, return VERSION_ERROR; } + /* set TLS 1.3 flag in options if this was a TLS 1.3 connection */ + if (ssl->version.major == SSLv3_MAJOR && + ssl->version.minor == TLSv1_3_MINOR) { + options->tls1_3 = 1; + } + return idx; } diff --git a/src/ssl.c b/src/ssl.c index 9391974f8..1f3d292e2 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -239,10 +239,7 @@ byte tsip_rootCAverified( ); #endif #ifdef WOLFSSL_SESSION_EXPORT -/** - * Used to import a serialized TLS session. In most cases wolfSSL_get_session - * should be used instead, this function is for exporting the state of the - * connection and when imported a resumption is not needed. +/* Used to import a serialized TLS session. * WARNING: buf contains sensitive information about the state and is best to be * encrypted before storing if stored. * @@ -260,10 +257,9 @@ int wolfSSL_tls_import(WOLFSSL* ssl, const unsigned char* buf, unsigned int sz) } -/** - * Used to export a serialized TLS session. In most cases wolfSSL_set_session - * should be used instead, this function is for importing a serialized state of - * the connection. +/* Used to export a serialized TLS session. + * WARNING: buf contains sensitive information about the state and is best to be + * encrypted before storing if stored. * * @param ssl WOLFSSL structure to export the session from * @param buf output of serialized session diff --git a/tests/api.c b/tests/api.c index 2bc9a5d16..4fd1d9c56 100644 --- a/tests/api.c +++ b/tests/api.c @@ -5651,6 +5651,86 @@ static void test_wolfSSL_dtls_export(void) #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12) +#ifdef WOLFSSL_TLS13 +static const byte canned_client_tls13_session[] = { + 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01, + 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, + 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00, + 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, + 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, + 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18, + 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31, + 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4, + 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B, + 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7, + 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01, + 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2, + 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48, + 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C, + 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73, + 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E, + 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77, + 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35, + 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F, + 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, + 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, + 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, + 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, + 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, + 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10, + 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20, + 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x03 +}; + +static const byte canned_server_tls13_session[] = { + 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00, + 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, + 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F, + 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, + 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, + 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18, + 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31, + 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4, + 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B, + 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7, + 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01, + 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2, + 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48, + 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C, + 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73, + 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E, + 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77, + 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35, + 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F, + 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, + 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, + 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D, + 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3, + 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1, + 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10, + 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20, + 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x04 +}; +#endif /* WOLFSSL_TLS13 */ + static const byte canned_client_session[] = { 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, @@ -5801,15 +5881,12 @@ static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args) if (ssl == NULL) { goto done; } + wolfSSL_set_fd(ssl, clientfd); /* 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'; @@ -5864,11 +5941,32 @@ done: return 0; #endif } + + +static void load_tls12_canned_server(WOLFSSL* ssl) +{ + int clientfd = wolfSSL_get_fd(ssl); + AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session, + sizeof(canned_server_session)), sizeof(canned_server_session)); + wolfSSL_set_fd(ssl, clientfd); +} + + +#ifdef WOLFSSL_TLS13 +static void load_tls13_canned_server(WOLFSSL* ssl) +{ + int clientfd = wolfSSL_get_fd(ssl); + AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session, + sizeof(canned_server_tls13_session)), + sizeof(canned_server_tls13_session)); + wolfSSL_set_fd(ssl, clientfd); +} #endif -static void test_wolfSSL_tls_export(void) + +/* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */ +static void test_wolfSSL_tls_export_run(int v) { -#if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12) SOCKET_T sockfd = 0; WOLFSSL_CTX* ctx = 0; WOLFSSL* ssl = 0; @@ -5876,13 +5974,14 @@ static void test_wolfSSL_tls_export(void) char reply[1024]; word32 replySz; int msgSz = (int)XSTRLEN(msg); + const byte* clientSession = NULL; + int clientSessionSz = 0; 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 @@ -5896,9 +5995,31 @@ static void test_wolfSSL_tls_export(void) XMEMSET(&server_args, 0, sizeof(func_args)); XMEMSET(&server_cbf, 0, sizeof(callback_functions)); - server_cbf.method = wolfTLSv1_2_server_method; + switch (v) { + case WOLFSSL_TLSV1_2: + server_cbf.method = wolfTLSv1_2_server_method; + server_cbf.ssl_ready = load_tls12_canned_server; + + /* setup the client side */ + AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())); + wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"); + clientSession = canned_client_session; + clientSessionSz = sizeof(canned_client_session); + break; + #ifdef WOLFSSL_TLS13 + case WOLFSSL_TLSV1_3: + server_cbf.method = wolfTLSv1_3_server_method; + server_cbf.ssl_ready = load_tls13_canned_server; + + /* setup the client side */ + AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())); + clientSession = canned_client_tls13_session; + clientSessionSz = sizeof(canned_client_tls13_session); + break; + #endif + } server_args.callbacks = &server_cbf; - server_args.signal = &ready; + server_args.signal = &ready; start_thread(tls_export_server, &server_args, &serverThread); wait_tcp_ready(&server_args); @@ -5908,15 +6029,13 @@ static void test_wolfSSL_tls_export(void) 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)); + AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz), + clientSessionSz); replySz = sizeof(reply); AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0); - AssertIntEQ(XMEMCMP(reply, canned_client_session, replySz), 0); + AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0); wolfSSL_set_fd(ssl, sockfd); AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz); @@ -5945,6 +6064,17 @@ static void test_wolfSSL_tls_export(void) fdOpenSession(Task_self()); #endif +} +#endif + +static void test_wolfSSL_tls_export(void) +{ +#if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12) + printf(testingFmt, "wolfSSL_tls_export()"); + test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2); + #ifdef WOLFSSL_TLS13 + test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3); + #endif printf(resultFmt, passed); #endif } diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 9bac559d8..d9ee87778 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -1666,9 +1666,9 @@ WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_3(void); #endif #ifdef WOLFSSL_SESSION_EXPORT WOLFSSL_LOCAL int wolfSSL_session_export_internal(WOLFSSL* ssl, byte* buf, - word32* sz, int isTLS); + word32* sz, int type); WOLFSSL_LOCAL int wolfSSL_session_import_internal(WOLFSSL* ssl, const byte* buf, - word32 sz, int isTLS); + word32 sz, int type); #ifdef WOLFSSL_DTLS WOLFSSL_LOCAL int wolfSSL_dtls_export_state_internal(WOLFSSL* ssl, byte* buf, word32 sz);