2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/bio.h>
13 #include <openssl/x509_vfy.h>
14 #include <openssl/ssl.h>
16 #include "handshake_helper.h"
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.
23 typedef struct handshake_ex_data {
26 int session_ticket_do_not_call;
29 static int ex_data_idx;
31 static void info_callback(const SSL *s, int where, int ret)
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;
39 ex_data->alert_received = ret;
44 static int servername_callback(SSL *s, int *ad, void *arg)
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);
51 * Copy over all the SSL_CTX options - reasonable behavior
52 * allows testing of cases where the options between two
53 * contexts differ/conflict
55 SSL_clear_options(s, 0xFFFFFFFFL);
56 SSL_set_options(s, SSL_CTX_get_options(new_ctx));
58 return SSL_TLSEXT_ERR_OK;
61 static int verify_reject_callback(X509_STORE_CTX *ctx, void *arg) {
62 X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
66 static int verify_accept_callback(X509_STORE_CTX *ctx, void *arg) {
70 static int broken_session_ticket_callback(SSL* s, unsigned char* key_name, unsigned char *iv,
71 EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
76 static int do_not_call_session_ticket_callback(SSL* s, unsigned char* key_name,
79 HMAC_CTX *hctx, int enc)
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;
88 * Configure callbacks and other properties that can't be set directly
89 * in the server/client CONF.
91 static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
93 const SSL_TEST_CTX *test_ctx)
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,
100 case SSL_TEST_VERIFY_REJECT_ALL:
101 SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_callback,
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);
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
116 SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx, do_not_call_session_ticket_callback);
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);
124 * Configure callbacks and other properties that can't be set directly
125 * in the server/client CONF.
127 static void configure_handshake_ssl(SSL *server, SSL *client,
128 const SSL_TEST_CTX *test_ctx)
130 if (test_ctx->servername != SSL_TEST_SERVERNAME_NONE)
131 SSL_set_tlsext_host_name(client,
132 ssl_servername_name(test_ctx->servername));
142 static peer_status_t do_handshake_step(SSL *ssl)
146 ret = SSL_do_handshake(ssl);
150 } else if (ret == 0) {
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)
163 /* Both parties succeeded. */
165 /* Client errored. */
167 /* Server errored. */
169 /* Peers are in inconsistent state. */
171 /* One or both peers not done. */
173 } handshake_status_t;
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.
181 static handshake_status_t handshake_status(peer_status_t last_status,
182 peer_status_t previous_status,
183 int client_spoke_last)
185 switch (last_status) {
187 switch (previous_status) {
189 /* Both succeeded. */
190 return HANDSHAKE_SUCCESS;
192 /* Let the first peer finish. */
193 return HANDSHAKE_RETRY;
196 * Second peer succeeded despite the fact that the first peer
197 * already errored. This shouldn't happen.
199 return INTERNAL_ERROR;
203 if (previous_status == PEER_RETRY) {
204 /* Neither peer is done. */
205 return HANDSHAKE_RETRY;
208 * Deadlock: second peer is waiting for more input while first
209 * peer thinks they're done (no more input is coming).
211 return INTERNAL_ERROR;
214 switch (previous_status) {
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.
222 return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
224 /* We errored; let the peer finish. */
225 return HANDSHAKE_RETRY;
227 /* Both peers errored. Return the one that errored first. */
228 return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
231 /* Control should never reach here. */
232 return INTERNAL_ERROR;
235 HANDSHAKE_RESULT do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
236 SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx)
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;
243 peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
244 handshake_status_t status = HANDSHAKE_RETRY;
245 unsigned char* tick = NULL;
247 SSL_SESSION* sess = NULL;
249 configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx);
251 server = SSL_new(server_ctx);
252 client = SSL_new(client_ctx);
253 OPENSSL_assert(server != NULL && client != NULL);
255 configure_handshake_ssl(server, client, test_ctx);
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;
262 client_to_server = BIO_new(BIO_s_mem());
263 server_to_client = BIO_new(BIO_s_mem());
265 OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
267 /* Non-blocking bio. */
268 BIO_set_nbio(client_to_server, 1);
269 BIO_set_nbio(server_to_client, 1);
271 SSL_set_connect_state(client);
272 SSL_set_accept_state(server);
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);
280 ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
281 OPENSSL_assert(ex_data_idx >= 0);
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);
288 SSL_set_info_callback(server, &info_callback);
289 SSL_set_info_callback(client, &info_callback);
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).
301 client_status = do_handshake_step(client);
302 status = handshake_status(client_status, server_status,
303 1 /* client went last */);
305 server_status = do_handshake_step(server);
306 status = handshake_status(server_status, client_status,
307 0 /* server went last */);
311 case HANDSHAKE_SUCCESS:
312 ret.result = SSL_TEST_SUCCESS;
315 ret.result = SSL_TEST_CLIENT_FAIL;
318 ret.result = SSL_TEST_SERVER_FAIL;
321 ret.result = SSL_TEST_INTERNAL_ERROR;
323 case HANDSHAKE_RETRY:
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);
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;
344 ret.session_ticket = SSL_TEST_SESSION_TICKET_YES;
345 ret.session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;