|
| 1 | +// Copyright Blaze 2021. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE or copy at |
| 4 | +// https://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include "cCHello.h" |
| 13 | + |
| 14 | +static uint8_t ch_1_3[] = { |
| 15 | + 0x16, 0x03, 0x01, 0x00, 0xca, 0x01, 0x00, 0x00, 0xc6, 0x03, 0x03, 0x00, |
| 16 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, |
| 17 | + 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 18 | + 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0xe0, 0xe1, 0xe2, 0xe3, |
| 19 | + 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 20 | + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, |
| 21 | + 0xfc, 0xfd, 0xfe, 0xff, 0x00, 0x06, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, |
| 22 | + 0x01, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x18, 0x00, 0x16, 0x00, 0x00, |
| 23 | + 0x13, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x75, 0x6c, 0x66, |
| 24 | + 0x68, 0x65, 0x69, 0x6d, 0x2e, 0x6e, 0x65, 0x74, 0x00, 0x0a, 0x00, 0x08, |
| 25 | + 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, 0x0d, 0x00, 0x14, |
| 26 | + 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, |
| 27 | + 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x00, 0x33, 0x00, 0x26, |
| 28 | + 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x35, 0x80, 0x72, 0xd6, 0x36, 0x58, |
| 29 | + 0x80, 0xd1, 0xae, 0xea, 0x32, 0x9a, 0xdf, 0x91, 0x21, 0x38, 0x38, 0x51, |
| 30 | + 0xed, 0x21, 0xa2, 0x8e, 0x3b, 0x75, 0xe9, 0x65, 0xd0, 0xd2, 0xcd, 0x16, |
| 31 | + 0x62, 0x54, 0x00, 0x2d, 0x00, 0x02, 0x01, 0x01, 0x00, 0x2b, 0x00, 0x03, |
| 32 | + 0x02, 0x03, 0x04}; |
| 33 | + |
| 34 | +static uint8_t ch_1_2[] = { |
| 35 | + 0x16, 0x03, 0x01, 0x00, 0xa5, 0x01, 0x00, 0x00, 0xa1, 0x03, 0x03, 0x00, |
| 36 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, |
| 37 | + 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 38 | + 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x00, 0x20, 0xcc, 0xa8, |
| 39 | + 0xcc, 0xa9, 0xc0, 0x2f, 0xc0, 0x30, 0xc0, 0x2b, 0xc0, 0x2c, 0xc0, 0x13, |
| 40 | + 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x0a, 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, |
| 41 | + 0x00, 0x35, 0xc0, 0x12, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x58, 0x00, 0x00, |
| 42 | + 0x00, 0x18, 0x00, 0x16, 0x00, 0x00, 0x13, 0x65, 0x78, 0x61, 0x6d, 0x70, |
| 43 | + 0x6c, 0x65, 0x2e, 0x75, 0x6c, 0x66, 0x68, 0x65, 0x69, 0x6d, 0x2e, 0x6e, |
| 44 | + 0x65, 0x74, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 | + 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 46 | + 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, |
| 47 | + 0x10, 0x04, 0x01, 0x04, 0x03, 0x05, 0x01, 0x05, 0x03, 0x06, 0x01, 0x06, |
| 48 | + 0x03, 0x02, 0x01, 0x02, 0x03, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x12, |
| 49 | + 0x00, 0x00}; |
| 50 | + |
| 51 | +static void assert_array(uint8_t *x, uint8_t *y, size_t len) { |
| 52 | + assert(memcmp(x, y, sizeof(uint8_t) * len) == 0); |
| 53 | +} |
| 54 | + |
| 55 | +static void print_hex(uint8_t *data, uint32_t data_len) { |
| 56 | + printf("{"); |
| 57 | + |
| 58 | + for (size_t i = 0; i < data_len; i++) |
| 59 | + if (i != (data_len - 1)) |
| 60 | + printf("0x%x, ", data[i]); |
| 61 | + else |
| 62 | + printf("0x%x", data[i]); |
| 63 | + |
| 64 | + printf("}\n"); |
| 65 | +} |
| 66 | + |
| 67 | +static client_hello_t *ch_init() { |
| 68 | + client_hello_t *ch = cchello_client_hello_init(); |
| 69 | + if (ch == NULL) { |
| 70 | + fprintf(stderr, "out of memory\n"); |
| 71 | + exit(EXIT_FAILURE); |
| 72 | + } |
| 73 | + |
| 74 | + return ch; |
| 75 | +} |
| 76 | + |
| 77 | +static void test_ch_1_3() { |
| 78 | + client_hello_t *ch = ch_init(); |
| 79 | + |
| 80 | + uint8_t c_session_id[] = {0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 81 | + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 82 | + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 83 | + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}; |
| 84 | + uint8_t c_random[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, |
| 85 | + 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, |
| 86 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 87 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}; |
| 88 | + uint8_t c_ciphers[] = {0x13, 0x1, 0x13, 0x2, 0x13, 0x3}; |
| 89 | + uint8_t c_extensions[] = { |
| 90 | + 0x0, 0x0, 0x0, 0x18, 0x0, 0x16, 0x0, 0x0, 0x13, 0x65, 0x78, 0x61, |
| 91 | + 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x75, 0x6c, 0x66, 0x68, 0x65, 0x69, 0x6d, |
| 92 | + 0x2e, 0x6e, 0x65, 0x74, 0x0, 0xa, 0x0, 0x8, 0x0, 0x6, 0x0, 0x1d, |
| 93 | + 0x0, 0x17, 0x0, 0x18, 0x0, 0xd, 0x0, 0x14, 0x0, 0x12, 0x4, 0x3, |
| 94 | + 0x8, 0x4, 0x4, 0x1, 0x5, 0x3, 0x8, 0x5, 0x5, 0x1, 0x8, 0x6, |
| 95 | + 0x6, 0x1, 0x2, 0x1, 0x0, 0x33, 0x0, 0x26, 0x0, 0x24, 0x0, 0x1d, |
| 96 | + 0x0, 0x20, 0x35, 0x80, 0x72, 0xd6, 0x36, 0x58, 0x80, 0xd1, 0xae, 0xea, |
| 97 | + 0x32, 0x9a, 0xdf, 0x91, 0x21, 0x38, 0x38, 0x51, 0xed, 0x21, 0xa2, 0x8e, |
| 98 | + 0x3b, 0x75, 0xe9, 0x65, 0xd0, 0xd2, 0xcd, 0x16, 0x62, 0x54, 0x0, 0x2d, |
| 99 | + 0x0, 0x2, 0x1, 0x1, 0x0, 0x2b, 0x0, 0x3, 0x2, 0x3, 0x4}; |
| 100 | + |
| 101 | + printf("TEST: parsing tls1.3 client hello: START.\n"); |
| 102 | + int ret = cchello_parse(ch, ch_1_3, sizeof(ch_1_3)); |
| 103 | + assert(ret == sizeof(ch_1_3)); |
| 104 | + |
| 105 | + printf("tls version: %d\n", ch->version); |
| 106 | + assert(ch->version == TLS_1_0); |
| 107 | + |
| 108 | + printf("session id: "); |
| 109 | + print_hex(ch->session_id, ch->session_id_len); |
| 110 | + assert_array(ch->session_id, c_session_id, sizeof(c_session_id)); |
| 111 | + |
| 112 | + printf("random: "); |
| 113 | + print_hex(ch->random, sizeof(ch->random)); |
| 114 | + assert_array(ch->random, c_random, sizeof(c_random)); |
| 115 | + |
| 116 | + printf("compression methods: "); |
| 117 | + print_hex(ch->compression_methods, ch->compression_methods_len); |
| 118 | + assert(ch->compression_methods[0] == 0x0); |
| 119 | + |
| 120 | + printf("cipher suites: "); |
| 121 | + print_hex(ch->cipher_suites, ch->cipher_suites_len); |
| 122 | + assert_array(ch->cipher_suites, c_ciphers, sizeof(c_ciphers)); |
| 123 | + |
| 124 | + printf("extensions: "); |
| 125 | + print_hex(ch->extensions, ch->extensions_len); |
| 126 | + assert_array(ch->extensions, c_extensions, sizeof(c_extensions)); |
| 127 | + |
| 128 | + printf("TEST: parsing tls1.3 client hello: PASSED.\n\n"); |
| 129 | + cchello_client_hello_free(ch); |
| 130 | +} |
| 131 | + |
| 132 | +static void test_ch_1_2() { |
| 133 | + client_hello_t *ch = ch_init(); |
| 134 | + |
| 135 | + uint8_t c_session_id[] = {}; |
| 136 | + uint8_t c_random[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, |
| 137 | + 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, |
| 138 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 139 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}; |
| 140 | + uint8_t c_ciphers[] = {0xcc, 0xa8, 0xcc, 0xa9, 0xc0, 0x2f, 0xc0, 0x30, |
| 141 | + 0xc0, 0x2b, 0xc0, 0x2c, 0xc0, 0x13, 0xc0, 0x9, |
| 142 | + 0xc0, 0x14, 0xc0, 0xa, 0x0, 0x9c, 0x0, 0x9d, |
| 143 | + 0x0, 0x2f, 0x0, 0x35, 0xc0, 0x12, 0x0, 0xa}; |
| 144 | + uint8_t c_extensions[] = { |
| 145 | + 0x0, 0x0, 0x0, 0x18, 0x0, 0x16, 0x0, 0x0, 0x13, 0x65, 0x78, |
| 146 | + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x75, 0x6c, 0x66, 0x68, 0x65, |
| 147 | + 0x69, 0x6d, 0x2e, 0x6e, 0x65, 0x74, 0x0, 0x5, 0x0, 0x5, 0x1, |
| 148 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0xa, 0x0, 0x8, 0x0, |
| 149 | + 0x1d, 0x0, 0x17, 0x0, 0x18, 0x0, 0x19, 0x0, 0xb, 0x0, 0x2, |
| 150 | + 0x1, 0x0, 0x0, 0xd, 0x0, 0x12, 0x0, 0x10, 0x4, 0x1, 0x4, |
| 151 | + 0x3, 0x5, 0x1, 0x5, 0x3, 0x6, 0x1, 0x6, 0x3, 0x2, 0x1, |
| 152 | + 0x2, 0x3, 0xff, 0x1, 0x0, 0x1, 0x0, 0x0, 0x12, 0x0, 0x0}; |
| 153 | + |
| 154 | + printf("TEST: parsing tls1.2 client hello: START.\n"); |
| 155 | + int ret = cchello_parse(ch, ch_1_2, sizeof(ch_1_2)); |
| 156 | + assert(ret == sizeof(ch_1_2)); |
| 157 | + |
| 158 | + printf("tls version: %d\n", ch->version); |
| 159 | + assert(ch->version == TLS_1_0); |
| 160 | + |
| 161 | + printf("session id: "); |
| 162 | + print_hex(ch->session_id, ch->session_id_len); |
| 163 | + assert_array(ch->session_id, c_session_id, sizeof(c_session_id)); |
| 164 | + |
| 165 | + printf("random: "); |
| 166 | + print_hex(ch->random, sizeof(ch->random)); |
| 167 | + assert_array(ch->random, c_random, sizeof(c_random)); |
| 168 | + |
| 169 | + printf("compression methods: "); |
| 170 | + print_hex(ch->compression_methods, ch->compression_methods_len); |
| 171 | + assert(ch->compression_methods[0] == 0x0); |
| 172 | + |
| 173 | + printf("cipher suites: "); |
| 174 | + print_hex(ch->cipher_suites, ch->cipher_suites_len); |
| 175 | + assert_array(ch->cipher_suites, c_ciphers, sizeof(c_ciphers)); |
| 176 | + |
| 177 | + printf("extensions: "); |
| 178 | + print_hex(ch->extensions, ch->extensions_len); |
| 179 | + assert_array(ch->extensions, c_extensions, sizeof(c_extensions)); |
| 180 | + |
| 181 | + printf("TEST: parsing tls1.2 client hello: PASSED.\n\n"); |
| 182 | + cchello_client_hello_free(ch); |
| 183 | +} |
| 184 | + |
| 185 | +int main(void) { |
| 186 | + test_ch_1_2(); |
| 187 | + test_ch_1_3(); |
| 188 | + |
| 189 | + printf("ALL TESTS PASSED.\n"); |
| 190 | + |
| 191 | + return 0; |
| 192 | +} |
0 commit comments