Issue #719:
[openssl.git] / test / handshake_helper.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <string.h>
12
13 #include <openssl/bio.h>
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  */
23 typedef struct handshake_ex_data {
24     int alert_sent;
25     int alert_received;
26 } HANDSHAKE_EX_DATA;
27
28 static int ex_data_idx;
29
30 static void info_callback(const SSL *s, int where, int ret)
31 {
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;
37         } else {
38             ex_data->alert_received = ret;
39         }
40     }
41 }
42
43 typedef enum {
44     PEER_SUCCESS,
45     PEER_RETRY,
46     PEER_ERROR
47 } peer_status_t;
48
49 static peer_status_t do_handshake_step(SSL *ssl)
50 {
51     int ret;
52
53     ret = SSL_do_handshake(ssl);
54
55     if (ret == 1) {
56         return PEER_SUCCESS;
57     } else if (ret == 0) {
58         return PEER_ERROR;
59     } else {
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)
63             return PEER_RETRY;
64         else
65             return PEER_ERROR;
66     }
67 }
68
69 typedef enum {
70     /* Both parties succeeded. */
71     HANDSHAKE_SUCCESS,
72     /* Client errored. */
73     CLIENT_ERROR,
74     /* Server errored. */
75     SERVER_ERROR,
76     /* Peers are in inconsistent state. */
77     INTERNAL_ERROR,
78     /* One or both peers not done. */
79     HANDSHAKE_RETRY
80 } handshake_status_t;
81
82 /*
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.
87  */
88 static handshake_status_t handshake_status(peer_status_t last_status,
89                                            peer_status_t previous_status,
90                                            int client_spoke_last)
91 {
92     switch (last_status) {
93     case PEER_SUCCESS:
94         switch (previous_status) {
95         case PEER_SUCCESS:
96             /* Both succeeded. */
97             return HANDSHAKE_SUCCESS;
98         case PEER_RETRY:
99             /* Let the first peer finish. */
100             return HANDSHAKE_RETRY;
101         case PEER_ERROR:
102             /*
103              * Second peer succeeded despite the fact that the first peer
104              * already errored. This shouldn't happen.
105              */
106             return INTERNAL_ERROR;
107         }
108
109     case PEER_RETRY:
110         if (previous_status == PEER_RETRY) {
111             /* Neither peer is done. */
112             return HANDSHAKE_RETRY;
113         } else {
114             /*
115              * Deadlock: second peer is waiting for more input while first
116              * peer thinks they're done (no more input is coming).
117              */
118             return INTERNAL_ERROR;
119         }
120     case PEER_ERROR:
121         switch (previous_status) {
122         case PEER_SUCCESS:
123             /*
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.
128              */
129             return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
130         case PEER_RETRY:
131             /* We errored; let the peer finish. */
132             return HANDSHAKE_RETRY;
133         case PEER_ERROR:
134             /* Both peers errored. Return the one that errored first. */
135             return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
136         }
137     }
138     /* Control should never reach here. */
139     return INTERNAL_ERROR;
140 }
141
142 HANDSHAKE_RESULT do_handshake(SSL_CTX *server_ctx, SSL_CTX *client_ctx)
143 {
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;
148     int client_turn = 1;
149     peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
150     handshake_status_t status = HANDSHAKE_RETRY;
151
152     server = SSL_new(server_ctx);
153     client = SSL_new(client_ctx);
154     OPENSSL_assert(server != NULL && client != NULL);
155
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;
160
161     client_to_server = BIO_new(BIO_s_mem());
162     server_to_client = BIO_new(BIO_s_mem());
163
164     OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
165
166     /* Non-blocking bio. */
167     BIO_set_nbio(client_to_server, 1);
168     BIO_set_nbio(server_to_client, 1);
169
170     SSL_set_connect_state(client);
171     SSL_set_accept_state(server);
172
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);
178
179     ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
180     OPENSSL_assert(ex_data_idx >= 0);
181
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);
186
187     SSL_set_info_callback(server, &info_callback);
188     SSL_set_info_callback(client, &info_callback);
189
190     /*
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).
197      */
198     for(;;) {
199         if (client_turn) {
200             client_status = do_handshake_step(client);
201             status = handshake_status(client_status, server_status,
202                                       1 /* client went last */);
203         } else {
204             server_status = do_handshake_step(server);
205             status = handshake_status(server_status, client_status,
206                                       0 /* server went last */);
207         }
208
209         switch (status) {
210         case HANDSHAKE_SUCCESS:
211             ret.result = SSL_TEST_SUCCESS;
212             goto err;
213         case CLIENT_ERROR:
214             ret.result = SSL_TEST_CLIENT_FAIL;
215             goto err;
216         case SERVER_ERROR:
217             ret.result = SSL_TEST_SERVER_FAIL;
218             goto err;
219         case INTERNAL_ERROR:
220             ret.result = SSL_TEST_INTERNAL_ERROR;
221             goto err;
222         case HANDSHAKE_RETRY:
223             /* Continue. */
224             client_turn ^= 1;
225             break;
226         }
227     }
228  err:
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);
235
236     SSL_free(server);
237     SSL_free(client);
238     return ret;
239 }