]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/handshake_helper.c
Clean up following new SNI tests
[thirdparty/openssl.git] / test / handshake_helper.c
CommitLineData
453dfd8d
EK
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
440e5d80
RS
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
453dfd8d 7 * https://www.openssl.org/source/license.html
453dfd8d
EK
8 */
9
10#include <string.h>
11
12#include <openssl/bio.h>
a263f320 13#include <openssl/x509_vfy.h>
453dfd8d
EK
14#include <openssl/ssl.h>
15
16#include "handshake_helper.h"
17
18/*
19 * Since there appears to be no way to extract the sent/received alert
20 * from the SSL object directly, we use the info callback and stash
21 * the result in ex_data.
22 */
23typedef struct handshake_ex_data {
24 int alert_sent;
25 int alert_received;
5c753de6 26 int session_ticket_do_not_call;
453dfd8d
EK
27} HANDSHAKE_EX_DATA;
28
29static int ex_data_idx;
30
31static void info_callback(const SSL *s, int where, int ret)
32{
33 if (where & SSL_CB_ALERT) {
34 HANDSHAKE_EX_DATA *ex_data =
35 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
36 if (where & SSL_CB_WRITE) {
37 ex_data->alert_sent = ret;
38 } else {
39 ex_data->alert_received = ret;
40 }
41 }
42}
43
81fc33c9
EK
44static int servername_callback(SSL *s, int *ad, void *arg)
45{
46 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
47 if (servername != NULL && !strcmp(servername, "server2")) {
48 SSL_CTX *new_ctx = (SSL_CTX*)arg;
49 SSL_set_SSL_CTX(s, new_ctx);
50 /*
51 * Copy over all the SSL_CTX options - reasonable behavior
52 * allows testing of cases where the options between two
53 * contexts differ/conflict
54 */
55 SSL_clear_options(s, 0xFFFFFFFFL);
56 SSL_set_options(s, SSL_CTX_get_options(new_ctx));
57 }
58 return SSL_TLSEXT_ERR_OK;
59}
60
a263f320
EK
61static int verify_reject_callback(X509_STORE_CTX *ctx, void *arg) {
62 X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
63 return 0;
64}
65
66static int verify_accept_callback(X509_STORE_CTX *ctx, void *arg) {
67 return 1;
68}
69
5c753de6
TS
70static int broken_session_ticket_callback(SSL* s, unsigned char* key_name, unsigned char *iv,
71 EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
72{
73 return 0;
74}
75
81fc33c9
EK
76static int do_not_call_session_ticket_callback(SSL* s, unsigned char* key_name,
77 unsigned char *iv,
78 EVP_CIPHER_CTX *ctx,
79 HMAC_CTX *hctx, int enc)
5c753de6
TS
80{
81 HANDSHAKE_EX_DATA *ex_data =
82 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
83 ex_data->session_ticket_do_not_call = 1;
84 return 0;
85}
86
a263f320
EK
87/*
88 * Configure callbacks and other properties that can't be set directly
89 * in the server/client CONF.
90 */
81fc33c9
EK
91static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
92 SSL_CTX *client_ctx,
5c753de6 93 const SSL_TEST_CTX *test_ctx)
a263f320
EK
94{
95 switch (test_ctx->client_verify_callback) {
96 case SSL_TEST_VERIFY_ACCEPT_ALL:
97 SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_callback,
98 NULL);
99 break;
100 case SSL_TEST_VERIFY_REJECT_ALL:
101 SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_callback,
102 NULL);
103 break;
104 default:
105 break;
106 }
81fc33c9
EK
107
108 /* link the two contexts for SNI purposes */
109 SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_callback);
110 SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
111 /*
112 * The initial_ctx/session_ctx always handles the encrypt/decrypt of the
113 * session ticket. This ticket_key callback is assigned to the second
114 * session (assigned via SNI), and should never be invoked
115 */
116 SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx, do_not_call_session_ticket_callback);
117
5c753de6
TS
118 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_BROKEN) {
119 SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_callback);
120 }
121}
122
123/*
124 * Configure callbacks and other properties that can't be set directly
125 * in the server/client CONF.
126 */
127static void configure_handshake_ssl(SSL *server, SSL *client,
128 const SSL_TEST_CTX *test_ctx)
129{
81fc33c9
EK
130 if (test_ctx->servername != SSL_TEST_SERVERNAME_NONE)
131 SSL_set_tlsext_host_name(client,
132 ssl_servername_name(test_ctx->servername));
a263f320
EK
133}
134
135
453dfd8d
EK
136typedef enum {
137 PEER_SUCCESS,
138 PEER_RETRY,
139 PEER_ERROR
140} peer_status_t;
141
142static peer_status_t do_handshake_step(SSL *ssl)
143{
144 int ret;
145
146 ret = SSL_do_handshake(ssl);
147
148 if (ret == 1) {
149 return PEER_SUCCESS;
150 } else if (ret == 0) {
151 return PEER_ERROR;
152 } else {
153 int error = SSL_get_error(ssl, ret);
154 /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
155 if (error == SSL_ERROR_WANT_READ)
156 return PEER_RETRY;
157 else
158 return PEER_ERROR;
159 }
160}
161
162typedef enum {
163 /* Both parties succeeded. */
164 HANDSHAKE_SUCCESS,
165 /* Client errored. */
166 CLIENT_ERROR,
167 /* Server errored. */
168 SERVER_ERROR,
169 /* Peers are in inconsistent state. */
170 INTERNAL_ERROR,
171 /* One or both peers not done. */
172 HANDSHAKE_RETRY
173} handshake_status_t;
174
175/*
176 * Determine the handshake outcome.
177 * last_status: the status of the peer to have acted last.
178 * previous_status: the status of the peer that didn't act last.
179 * client_spoke_last: 1 if the client went last.
180 */
181static handshake_status_t handshake_status(peer_status_t last_status,
182 peer_status_t previous_status,
183 int client_spoke_last)
184{
185 switch (last_status) {
186 case PEER_SUCCESS:
187 switch (previous_status) {
188 case PEER_SUCCESS:
189 /* Both succeeded. */
190 return HANDSHAKE_SUCCESS;
191 case PEER_RETRY:
192 /* Let the first peer finish. */
193 return HANDSHAKE_RETRY;
194 case PEER_ERROR:
195 /*
196 * Second peer succeeded despite the fact that the first peer
197 * already errored. This shouldn't happen.
198 */
199 return INTERNAL_ERROR;
200 }
201
202 case PEER_RETRY:
203 if (previous_status == PEER_RETRY) {
204 /* Neither peer is done. */
205 return HANDSHAKE_RETRY;
206 } else {
207 /*
208 * Deadlock: second peer is waiting for more input while first
209 * peer thinks they're done (no more input is coming).
210 */
211 return INTERNAL_ERROR;
212 }
213 case PEER_ERROR:
214 switch (previous_status) {
215 case PEER_SUCCESS:
216 /*
217 * First peer succeeded but second peer errored.
218 * TODO(emilia): we should be able to continue here (with some
219 * application data?) to ensure the first peer receives the
220 * alert / close_notify.
221 */
222 return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
223 case PEER_RETRY:
224 /* We errored; let the peer finish. */
225 return HANDSHAKE_RETRY;
226 case PEER_ERROR:
227 /* Both peers errored. Return the one that errored first. */
228 return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
229 }
230 }
231 /* Control should never reach here. */
232 return INTERNAL_ERROR;
233}
234
81fc33c9
EK
235HANDSHAKE_RESULT do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
236 SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx)
453dfd8d
EK
237{
238 SSL *server, *client;
239 BIO *client_to_server, *server_to_client;
240 HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
241 HANDSHAKE_RESULT ret;
242 int client_turn = 1;
243 peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
244 handshake_status_t status = HANDSHAKE_RETRY;
5c753de6
TS
245 unsigned char* tick = NULL;
246 size_t len = 0;
247 SSL_SESSION* sess = NULL;
453dfd8d 248
81fc33c9 249 configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx);
a263f320 250
453dfd8d
EK
251 server = SSL_new(server_ctx);
252 client = SSL_new(client_ctx);
253 OPENSSL_assert(server != NULL && client != NULL);
254
5c753de6
TS
255 configure_handshake_ssl(server, client, test_ctx);
256
453dfd8d
EK
257 memset(&server_ex_data, 0, sizeof(server_ex_data));
258 memset(&client_ex_data, 0, sizeof(client_ex_data));
259 memset(&ret, 0, sizeof(ret));
260 ret.result = SSL_TEST_INTERNAL_ERROR;
261
262 client_to_server = BIO_new(BIO_s_mem());
263 server_to_client = BIO_new(BIO_s_mem());
264
265 OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
266
267 /* Non-blocking bio. */
268 BIO_set_nbio(client_to_server, 1);
269 BIO_set_nbio(server_to_client, 1);
270
271 SSL_set_connect_state(client);
272 SSL_set_accept_state(server);
273
274 /* The bios are now owned by the SSL object. */
275 SSL_set_bio(client, server_to_client, client_to_server);
276 OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
277 OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
278 SSL_set_bio(server, client_to_server, server_to_client);
279
280 ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
281 OPENSSL_assert(ex_data_idx >= 0);
282
283 OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
284 &server_ex_data) == 1);
285 OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
286 &client_ex_data) == 1);
287
288 SSL_set_info_callback(server, &info_callback);
289 SSL_set_info_callback(client, &info_callback);
290
291 /*
292 * Half-duplex handshake loop.
293 * Client and server speak to each other synchronously in the same process.
294 * We use non-blocking BIOs, so whenever one peer blocks for read, it
295 * returns PEER_RETRY to indicate that it's the other peer's turn to write.
296 * The handshake succeeds once both peers have succeeded. If one peer
297 * errors out, we also let the other peer retry (and presumably fail).
298 */
299 for(;;) {
300 if (client_turn) {
301 client_status = do_handshake_step(client);
302 status = handshake_status(client_status, server_status,
303 1 /* client went last */);
304 } else {
305 server_status = do_handshake_step(server);
306 status = handshake_status(server_status, client_status,
307 0 /* server went last */);
308 }
309
310 switch (status) {
311 case HANDSHAKE_SUCCESS:
312 ret.result = SSL_TEST_SUCCESS;
313 goto err;
314 case CLIENT_ERROR:
315 ret.result = SSL_TEST_CLIENT_FAIL;
316 goto err;
317 case SERVER_ERROR:
318 ret.result = SSL_TEST_SERVER_FAIL;
319 goto err;
320 case INTERNAL_ERROR:
321 ret.result = SSL_TEST_INTERNAL_ERROR;
322 goto err;
323 case HANDSHAKE_RETRY:
324 /* Continue. */
325 client_turn ^= 1;
326 break;
327 }
328 }
329 err:
330 ret.server_alert_sent = server_ex_data.alert_sent;
331 ret.server_alert_received = client_ex_data.alert_received;
332 ret.client_alert_sent = client_ex_data.alert_sent;
333 ret.client_alert_received = server_ex_data.alert_received;
334 ret.server_protocol = SSL_version(server);
335 ret.client_protocol = SSL_version(client);
5c753de6
TS
336 ret.servername = ((SSL_get_SSL_CTX(server) == server_ctx)
337 ? SSL_TEST_SERVERNAME_SERVER1
338 : SSL_TEST_SERVERNAME_SERVER2);
339 if ((sess = SSL_get0_session(client)) != NULL)
340 SSL_SESSION_get0_ticket(sess, &tick, &len);
341 if (tick == NULL || len == 0)
342 ret.session_ticket = SSL_TEST_SESSION_TICKET_NO;
343 else
344 ret.session_ticket = SSL_TEST_SESSION_TICKET_YES;
345 ret.session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
453dfd8d
EK
346
347 SSL_free(server);
348 SSL_free(client);
349 return ret;
350}