2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
13 #include <openssl/bio.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 {
28 static int ex_data_idx;
30 static void info_callback(const SSL *s, int where, int ret)
32 if (where & SSL_CB_ALERT) {
33 HANDSHAKE_EX_DATA *ex_data =
34 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
35 if (where & SSL_CB_WRITE) {
36 ex_data->alert_sent = ret;
38 ex_data->alert_received = ret;
49 static peer_status_t do_handshake_step(SSL *ssl)
53 ret = SSL_do_handshake(ssl);
57 } else if (ret == 0) {
60 int error = SSL_get_error(ssl, ret);
61 /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
62 if (error == SSL_ERROR_WANT_READ)
70 /* Both parties succeeded. */
76 /* Peers are in inconsistent state. */
78 /* One or both peers not done. */
83 * Determine the handshake outcome.
84 * last_status: the status of the peer to have acted last.
85 * previous_status: the status of the peer that didn't act last.
86 * client_spoke_last: 1 if the client went last.
88 static handshake_status_t handshake_status(peer_status_t last_status,
89 peer_status_t previous_status,
90 int client_spoke_last)
92 switch (last_status) {
94 switch (previous_status) {
97 return HANDSHAKE_SUCCESS;
99 /* Let the first peer finish. */
100 return HANDSHAKE_RETRY;
103 * Second peer succeeded despite the fact that the first peer
104 * already errored. This shouldn't happen.
106 return INTERNAL_ERROR;
110 if (previous_status == PEER_RETRY) {
111 /* Neither peer is done. */
112 return HANDSHAKE_RETRY;
115 * Deadlock: second peer is waiting for more input while first
116 * peer thinks they're done (no more input is coming).
118 return INTERNAL_ERROR;
121 switch (previous_status) {
124 * First peer succeeded but second peer errored.
125 * TODO(emilia): we should be able to continue here (with some
126 * application data?) to ensure the first peer receives the
127 * alert / close_notify.
129 return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
131 /* We errored; let the peer finish. */
132 return HANDSHAKE_RETRY;
134 /* Both peers errored. Return the one that errored first. */
135 return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
138 /* Control should never reach here. */
139 return INTERNAL_ERROR;
142 HANDSHAKE_RESULT do_handshake(SSL_CTX *server_ctx, SSL_CTX *client_ctx)
144 SSL *server, *client;
145 BIO *client_to_server, *server_to_client;
146 HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
147 HANDSHAKE_RESULT ret;
149 peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
150 handshake_status_t status = HANDSHAKE_RETRY;
152 server = SSL_new(server_ctx);
153 client = SSL_new(client_ctx);
154 OPENSSL_assert(server != NULL && client != NULL);
156 memset(&server_ex_data, 0, sizeof(server_ex_data));
157 memset(&client_ex_data, 0, sizeof(client_ex_data));
158 memset(&ret, 0, sizeof(ret));
159 ret.result = SSL_TEST_INTERNAL_ERROR;
161 client_to_server = BIO_new(BIO_s_mem());
162 server_to_client = BIO_new(BIO_s_mem());
164 OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
166 /* Non-blocking bio. */
167 BIO_set_nbio(client_to_server, 1);
168 BIO_set_nbio(server_to_client, 1);
170 SSL_set_connect_state(client);
171 SSL_set_accept_state(server);
173 /* The bios are now owned by the SSL object. */
174 SSL_set_bio(client, server_to_client, client_to_server);
175 OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
176 OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
177 SSL_set_bio(server, client_to_server, server_to_client);
179 ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
180 OPENSSL_assert(ex_data_idx >= 0);
182 OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
183 &server_ex_data) == 1);
184 OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
185 &client_ex_data) == 1);
187 SSL_set_info_callback(server, &info_callback);
188 SSL_set_info_callback(client, &info_callback);
191 * Half-duplex handshake loop.
192 * Client and server speak to each other synchronously in the same process.
193 * We use non-blocking BIOs, so whenever one peer blocks for read, it
194 * returns PEER_RETRY to indicate that it's the other peer's turn to write.
195 * The handshake succeeds once both peers have succeeded. If one peer
196 * errors out, we also let the other peer retry (and presumably fail).
200 client_status = do_handshake_step(client);
201 status = handshake_status(client_status, server_status,
202 1 /* client went last */);
204 server_status = do_handshake_step(server);
205 status = handshake_status(server_status, client_status,
206 0 /* server went last */);
210 case HANDSHAKE_SUCCESS:
211 ret.result = SSL_TEST_SUCCESS;
214 ret.result = SSL_TEST_CLIENT_FAIL;
217 ret.result = SSL_TEST_SERVER_FAIL;
220 ret.result = SSL_TEST_INTERNAL_ERROR;
222 case HANDSHAKE_RETRY:
229 ret.server_alert_sent = server_ex_data.alert_sent;
230 ret.server_alert_received = client_ex_data.alert_received;
231 ret.client_alert_sent = client_ex_data.alert_sent;
232 ret.client_alert_received = server_ex_data.alert_received;
233 ret.server_protocol = SSL_version(server);
234 ret.client_protocol = SSL_version(client);