|
| 1 | +/* test_tls.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 <tests/utils.h> |
| 32 | +#include <tests/api/test_tls.h> |
| 33 | + |
| 34 | + |
| 35 | +int test_tls12_unexpected_ccs(void) |
| 36 | +{ |
| 37 | + EXPECT_DECLS; |
| 38 | +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) |
| 39 | + const byte ccs[] = { |
| 40 | + 0x14, /* ccs type */ |
| 41 | + 0x03, 0x03, /* version */ |
| 42 | + 0x00, 0x01, /* length */ |
| 43 | + 0x01, /* ccs value */ |
| 44 | + }; |
| 45 | + const byte badccs[] = { |
| 46 | + 0x14, /* ccs type */ |
| 47 | + 0x03, 0x03, /* version */ |
| 48 | + 0x00, 0x01, /* length */ |
| 49 | + 0x99, /* wrong ccs value */ |
| 50 | + }; |
| 51 | + WOLFSSL_CTX *ctx_s = NULL; |
| 52 | + WOLFSSL *ssl_s = NULL; |
| 53 | + struct test_memio_ctx test_ctx; |
| 54 | + |
| 55 | + /* ccs in the wrong place */ |
| 56 | + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); |
| 57 | + /* inject SH */ |
| 58 | + ExpectIntEQ(test_memio_inject_message(&test_ctx, 0, |
| 59 | + (const char*)ccs, sizeof(ccs)), 0); |
| 60 | + ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s, |
| 61 | + NULL, wolfTLSv1_2_server_method), 0); |
| 62 | + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); |
| 63 | + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), |
| 64 | + OUT_OF_ORDER_E); |
| 65 | + wolfSSL_free(ssl_s); |
| 66 | + wolfSSL_CTX_free(ctx_s); |
| 67 | + ctx_s = NULL; |
| 68 | + ssl_s = NULL; |
| 69 | + |
| 70 | + /* malformed ccs */ |
| 71 | + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); |
| 72 | + ExpectIntEQ(test_memio_inject_message(&test_ctx, 0, |
| 73 | + (const char*)badccs, sizeof(badccs)), 0); |
| 74 | + ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s, |
| 75 | + NULL, wolfTLSv1_2_server_method), 0); |
| 76 | + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); |
| 77 | + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), |
| 78 | + LENGTH_ERROR); |
| 79 | + wolfSSL_free(ssl_s); |
| 80 | + wolfSSL_CTX_free(ctx_s); |
| 81 | +#endif |
| 82 | + return EXPECT_RESULT(); |
| 83 | +} |
| 84 | + |
| 85 | +int test_tls13_unexpected_ccs(void) |
| 86 | +{ |
| 87 | + EXPECT_DECLS; |
| 88 | +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) |
| 89 | + const byte ccs[] = { |
| 90 | + 0x14, /* ccs type */ |
| 91 | + 0x03, 0x03, /* version */ |
| 92 | + 0x00, 0x01, /* length */ |
| 93 | + 0x01, /* ccs value */ |
| 94 | + }; |
| 95 | + const byte badccs[] = { |
| 96 | + 0x14, /* ccs type */ |
| 97 | + 0x03, 0x03, /* version */ |
| 98 | + 0x00, 0x01, /* length */ |
| 99 | + 0x99, /* wrong ccs value */ |
| 100 | + }; |
| 101 | + const byte unexpectedAlert[] = { |
| 102 | + 0x15, /* alert type */ |
| 103 | + 0x03, 0x03, /* version */ |
| 104 | + 0x00, 0x02, /* length */ |
| 105 | + 0x02, /* level: fatal */ |
| 106 | + 0x0a /* protocol version */ |
| 107 | + }; |
| 108 | + WOLFSSL_CTX *ctx_s = NULL; |
| 109 | + WOLFSSL *ssl_s = NULL; |
| 110 | + struct test_memio_ctx test_ctx; |
| 111 | + |
| 112 | + /* ccs can't appear before a CH */ |
| 113 | + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); |
| 114 | + ExpectIntEQ(test_memio_inject_message(&test_ctx, 0, |
| 115 | + (const char*)ccs, sizeof(ccs)), 0); |
| 116 | + ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s, |
| 117 | + NULL, wolfTLSv1_3_server_method), 0); |
| 118 | + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); |
| 119 | + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), |
| 120 | + UNKNOWN_RECORD_TYPE); |
| 121 | + ExpectIntEQ(test_ctx.c_len, sizeof(unexpectedAlert)); |
| 122 | + ExpectBufEQ(test_ctx.c_buff, unexpectedAlert, sizeof(unexpectedAlert)); |
| 123 | + wolfSSL_free(ssl_s); |
| 124 | + wolfSSL_CTX_free(ctx_s); |
| 125 | + ctx_s = NULL; |
| 126 | + ssl_s = NULL; |
| 127 | + |
| 128 | + /* malformed ccs */ |
| 129 | + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); |
| 130 | + ExpectIntEQ(test_memio_inject_message(&test_ctx, 0, |
| 131 | + (const char*)badccs, sizeof(badccs)), 0); |
| 132 | + ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s, |
| 133 | + NULL, wolfTLSv1_3_server_method), 0); |
| 134 | + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); |
| 135 | + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), |
| 136 | + UNKNOWN_RECORD_TYPE); |
| 137 | + ExpectIntEQ(test_ctx.c_len, sizeof(unexpectedAlert)); |
| 138 | + ExpectBufEQ(test_ctx.c_buff, unexpectedAlert, sizeof(unexpectedAlert)); |
| 139 | + wolfSSL_free(ssl_s); |
| 140 | + wolfSSL_CTX_free(ctx_s); |
| 141 | +#endif |
| 142 | + return EXPECT_RESULT(); |
| 143 | +} |
0 commit comments