|
| 1 | +/* test_tls_ext.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2025 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <tests/unit.h> |
| 23 | + |
| 24 | +#ifdef NO_INLINE |
| 25 | + #include <wolfssl/wolfcrypt/misc.h> |
| 26 | +#else |
| 27 | + #define WOLFSSL_MISC_INCLUDED |
| 28 | + #include <wolfcrypt/src/misc.c> |
| 29 | +#endif |
| 30 | + |
| 31 | +#include <wolfssl/internal.h> |
| 32 | +#include <tests/utils.h> |
| 33 | +#include <tests/api/test_tls_ext.h> |
| 34 | + |
| 35 | +int test_tls_ems_downgrade(void) |
| 36 | +{ |
| 37 | + EXPECT_DECLS; |
| 38 | +#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
| 39 | + defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ |
| 40 | + defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) |
| 41 | + struct test_memio_ctx test_ctx; |
| 42 | + WOLFSSL_CTX *ctx_c = NULL; |
| 43 | + WOLFSSL_CTX *ctx_s = NULL; |
| 44 | + WOLFSSL *ssl_c = NULL; |
| 45 | + WOLFSSL *ssl_s = NULL; |
| 46 | + WOLFSSL_SESSION* session = NULL; |
| 47 | + /* TLS EMS extension in binary form */ |
| 48 | + const char ems_ext[] = { 0x00, 0x17, 0x00, 0x00 }; |
| 49 | + char data = 0; |
| 50 | + |
| 51 | + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); |
| 52 | + |
| 53 | + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, |
| 54 | + wolfTLS_client_method, wolfTLS_server_method), 0); |
| 55 | + |
| 56 | + ExpectIntEQ(wolfSSL_connect(ssl_c), -1); |
| 57 | + ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ); |
| 58 | + |
| 59 | + /* Verify that the EMS extension is present in Client's message */ |
| 60 | + ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len, |
| 61 | + ems_ext, sizeof(ems_ext))); |
| 62 | + |
| 63 | + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); |
| 64 | + ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION); |
| 65 | + |
| 66 | + /* Do a round of reads to exchange the ticket message */ |
| 67 | + ExpectIntEQ(wolfSSL_read(ssl_s, &data, sizeof(data)), -1); |
| 68 | + ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ); |
| 69 | + ExpectIntEQ(wolfSSL_read(ssl_c, &data, sizeof(data)), -1); |
| 70 | + ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ); |
| 71 | + |
| 72 | + ExpectNotNull(session = wolfSSL_get1_session(ssl_c)); |
| 73 | + ExpectTrue(session->haveEMS); |
| 74 | + |
| 75 | + wolfSSL_free(ssl_c); |
| 76 | + ssl_c = NULL; |
| 77 | + wolfSSL_free(ssl_s); |
| 78 | + ssl_s = NULL; |
| 79 | + |
| 80 | + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, |
| 81 | + wolfTLS_client_method, wolfTLS_server_method), 0); |
| 82 | + |
| 83 | + /* Resuming the connection */ |
| 84 | + ExpectIntEQ(wolfSSL_set_session(ssl_c, session), WOLFSSL_SUCCESS); |
| 85 | + ExpectIntEQ(wolfSSL_connect(ssl_c), -1); |
| 86 | + ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ); |
| 87 | + |
| 88 | + /* Verify that the EMS extension is still present in the resumption CH |
| 89 | + * even though we used TLS 1.3 */ |
| 90 | + ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len, |
| 91 | + ems_ext, sizeof(ems_ext))); |
| 92 | + |
| 93 | + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); |
| 94 | + ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION); |
| 95 | + |
| 96 | + wolfSSL_SESSION_free(session); |
| 97 | + wolfSSL_free(ssl_c); |
| 98 | + wolfSSL_free(ssl_s); |
| 99 | + wolfSSL_CTX_free(ctx_c); |
| 100 | + wolfSSL_CTX_free(ctx_s); |
| 101 | +#endif |
| 102 | + return EXPECT_RESULT(); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +int test_wolfSSL_DisableExtendedMasterSecret(void) |
| 107 | +{ |
| 108 | + EXPECT_DECLS; |
| 109 | +#if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT) && \ |
| 110 | + !defined(NO_TLS) |
| 111 | + WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()); |
| 112 | + WOLFSSL *ssl = wolfSSL_new(ctx); |
| 113 | + |
| 114 | + ExpectNotNull(ctx); |
| 115 | + ExpectNotNull(ssl); |
| 116 | + |
| 117 | + /* error cases */ |
| 118 | + ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL)); |
| 119 | + ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL)); |
| 120 | + |
| 121 | + /* success cases */ |
| 122 | + ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx)); |
| 123 | + ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl)); |
| 124 | + |
| 125 | + wolfSSL_free(ssl); |
| 126 | + wolfSSL_CTX_free(ctx); |
| 127 | +#endif |
| 128 | + return EXPECT_RESULT(); |
| 129 | +} |
0 commit comments