1 /* Written by Matt Caswell for the OpenSSL Project */
2 /* ====================================================================
3 * Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
58 #include <openssl/opensslconf.h>
59 #include <openssl/bio.h>
60 #include <openssl/crypto.h>
61 #include <openssl/evp.h>
62 #include <openssl/ssl.h>
63 #include <openssl/err.h>
66 #define CLIENT_VERSION_LEN 2
67 #define SESSION_ID_LEN_LEN 1
68 #define CIPHERS_LEN_LEN 2
69 #define COMPRESSION_LEN_LEN 1
70 #define EXTENSIONS_LEN_LEN 2
71 #define EXTENSION_TYPE_LEN 2
72 #define EXTENSION_SIZE_LEN 2
75 #define TOTAL_NUM_TESTS 2
78 * Test that explicitly setting ticket data results in it appearing in the
79 * ClientHello for TLS1.2
81 #define TEST_SET_SESSION_TICK_DATA_TLS_1_2 0
84 * Test that explicitly setting ticket data results in it appearing in the
85 * ClientHello for a negotiated SSL/TLS version
87 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 1
89 int main(int argc
, char *argv
[])
98 unsigned char *dataend
;
99 char *dummytick
= "Hello World!";
106 err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
108 CRYPTO_set_mem_debug(1);
109 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
112 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
113 * produced when we try to connect
115 for (; currtest
< TOTAL_NUM_TESTS
; currtest
++) {
117 if (currtest
== TEST_SET_SESSION_TICK_DATA_TLS_1_2
) {
118 #ifndef OPENSSL_NO_TLS1_2
119 ctx
= SSL_CTX_new(TLSv1_2_method());
124 ctx
= SSL_CTX_new(TLS_method());
128 rbio
= BIO_new(BIO_s_mem());
129 wbio
= BIO_new(BIO_s_mem());
130 SSL_set_bio(con
, rbio
, wbio
);
131 SSL_set_connect_state(con
);
133 if (currtest
== TEST_SET_SESSION_TICK_DATA_TLS_1_2
134 || currtest
== TEST_SET_SESSION_TICK_DATA_VER_NEG
) {
135 if (!SSL_set_session_ticket_ext(con
, dummytick
, strlen(dummytick
)))
139 if (SSL_connect(con
) > 0) {
140 /* This shouldn't succeed because we don't have a server! */
144 len
= BIO_get_mem_data(wbio
, (char **)&data
);
145 dataend
= data
+ len
;
147 /* Skip the record header */
148 data
+= SSL3_RT_HEADER_LENGTH
;
149 /* Skip the handshake message header */
150 data
+= SSL3_HM_HEADER_LENGTH
;
151 /* Skip client version and random */
152 data
+= CLIENT_VERSION_LEN
+ SSL3_RANDOM_SIZE
;
153 if (data
+ SESSION_ID_LEN_LEN
> dataend
)
155 /* Skip session id */
157 data
+= SESSION_ID_LEN_LEN
+ tmplen
;
158 if (data
+ CIPHERS_LEN_LEN
> dataend
)
161 tmplen
= ((*data
) << 8) | *(data
+ 1);
162 data
+= CIPHERS_LEN_LEN
+ tmplen
;
163 if (data
+ COMPRESSION_LEN_LEN
> dataend
)
165 /* Skip compression */
167 data
+= COMPRESSION_LEN_LEN
+ tmplen
;
168 if (data
+ EXTENSIONS_LEN_LEN
> dataend
)
171 tmplen
= ((*data
) << 8) | *(data
+ 1);
172 data
+= EXTENSIONS_LEN_LEN
;
173 if (data
+ tmplen
> dataend
)
176 /* Loop through all extensions */
177 while (tmplen
> EXTENSION_TYPE_LEN
+ EXTENSION_SIZE_LEN
) {
178 type
= ((*data
) << 8) | *(data
+ 1);
179 data
+= EXTENSION_TYPE_LEN
;
180 size
= ((*data
) << 8) | *(data
+ 1);
181 data
+= EXTENSION_SIZE_LEN
;
182 if (data
+ size
> dataend
)
185 if (type
== TLSEXT_TYPE_session_ticket
) {
186 if (currtest
== TEST_SET_SESSION_TICK_DATA_TLS_1_2
187 || currtest
== TEST_SET_SESSION_TICK_DATA_VER_NEG
) {
188 if (size
== strlen(dummytick
)
189 && memcmp(data
, dummytick
, size
) == 0) {
190 /* Ticket data is as we expected */
193 printf("Received session ticket is not as expected\n");
199 tmplen
-= EXTENSION_TYPE_LEN
+ EXTENSION_SIZE_LEN
+ size
;
207 printf("ClientHello test: FAILED (Test %d)\n", currtest
);
212 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
213 if (CRYPTO_mem_leaks(err
) <= 0)
218 return testresult
?0:1;