]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/clienthellotest.c
Copyright consolidation 02/10
[thirdparty/openssl.git] / test / clienthellotest.c
1 /*
2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11
12 #include <openssl/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/evp.h>
16 #include <openssl/ssl.h>
17 #include <openssl/err.h>
18
19
20 #define CLIENT_VERSION_LEN 2
21 #define SESSION_ID_LEN_LEN 1
22 #define CIPHERS_LEN_LEN 2
23 #define COMPRESSION_LEN_LEN 1
24 #define EXTENSIONS_LEN_LEN 2
25 #define EXTENSION_TYPE_LEN 2
26 #define EXTENSION_SIZE_LEN 2
27
28
29 #define TOTAL_NUM_TESTS 1
30
31 /*
32 * Test that explicitly setting ticket data results in it appearing in the
33 * ClientHello for a negotiated SSL/TLS version
34 */
35 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
36
37 int main(int argc, char *argv[])
38 {
39 SSL_CTX *ctx;
40 SSL *con;
41 BIO *rbio;
42 BIO *wbio;
43 BIO *err;
44 long len;
45 unsigned char *data;
46 unsigned char *dataend;
47 char *dummytick = "Hello World!";
48 unsigned int tmplen;
49 unsigned int type;
50 unsigned int size;
51 int testresult = 0;
52 int currtest = 0;
53
54 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
55
56 CRYPTO_set_mem_debug(1);
57 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
58
59 /*
60 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
61 * produced when we try to connect
62 */
63 for (; currtest < TOTAL_NUM_TESTS; currtest++) {
64 testresult = 0;
65 ctx = SSL_CTX_new(TLS_method());
66 con = SSL_new(ctx);
67
68 rbio = BIO_new(BIO_s_mem());
69 wbio = BIO_new(BIO_s_mem());
70 SSL_set_bio(con, rbio, wbio);
71 SSL_set_connect_state(con);
72
73 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
74 if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
75 goto end;
76 }
77
78 if (SSL_connect(con) > 0) {
79 /* This shouldn't succeed because we don't have a server! */
80 goto end;
81 }
82
83 len = BIO_get_mem_data(wbio, (char **)&data);
84 dataend = data + len;
85
86 /* Skip the record header */
87 data += SSL3_RT_HEADER_LENGTH;
88 /* Skip the handshake message header */
89 data += SSL3_HM_HEADER_LENGTH;
90 /* Skip client version and random */
91 data += CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE;
92 if (data + SESSION_ID_LEN_LEN > dataend)
93 goto end;
94 /* Skip session id */
95 tmplen = *data;
96 data += SESSION_ID_LEN_LEN + tmplen;
97 if (data + CIPHERS_LEN_LEN > dataend)
98 goto end;
99 /* Skip ciphers */
100 tmplen = ((*data) << 8) | *(data + 1);
101 data += CIPHERS_LEN_LEN + tmplen;
102 if (data + COMPRESSION_LEN_LEN > dataend)
103 goto end;
104 /* Skip compression */
105 tmplen = *data;
106 data += COMPRESSION_LEN_LEN + tmplen;
107 if (data + EXTENSIONS_LEN_LEN > dataend)
108 goto end;
109 /* Extensions len */
110 tmplen = ((*data) << 8) | *(data + 1);
111 data += EXTENSIONS_LEN_LEN;
112 if (data + tmplen > dataend)
113 goto end;
114
115 /* Loop through all extensions */
116 while (tmplen > EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN) {
117 type = ((*data) << 8) | *(data + 1);
118 data += EXTENSION_TYPE_LEN;
119 size = ((*data) << 8) | *(data + 1);
120 data += EXTENSION_SIZE_LEN;
121 if (data + size > dataend)
122 goto end;
123
124 if (type == TLSEXT_TYPE_session_ticket) {
125 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
126 if (size == strlen(dummytick)
127 && memcmp(data, dummytick, size) == 0) {
128 /* Ticket data is as we expected */
129 testresult = 1;
130 } else {
131 printf("Received session ticket is not as expected\n");
132 }
133 break;
134 }
135 }
136
137 tmplen -= EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN + size;
138 data += size;
139 }
140
141 end:
142 SSL_free(con);
143 SSL_CTX_free(ctx);
144 if (!testresult) {
145 printf("ClientHello test: FAILED (Test %d)\n", currtest);
146 break;
147 }
148 }
149
150 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
151 if (CRYPTO_mem_leaks(err) <= 0)
152 testresult = 0;
153 #endif
154 BIO_free(err);
155
156 return testresult?0:1;
157 }