DJGPP adjustments
[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/x509_vfy.h>
15 #include <openssl/ssl.h>
16
17 #include "handshake_helper.h"
18
19 /*
20  * Since there appears to be no way to extract the sent/received alert
21  * from the SSL object directly, we use the info callback and stash
22  * the result in ex_data.
23  */
24 typedef struct handshake_ex_data {
25     int alert_sent;
26     int alert_received;
27 } HANDSHAKE_EX_DATA;
28
29 static int ex_data_idx;
30
31 static 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
44 static int verify_reject_callback(X509_STORE_CTX *ctx, void *arg) {
45     X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
46     return 0;
47 }
48
49 static int verify_accept_callback(X509_STORE_CTX *ctx, void *arg) {
50     return 1;
51 }
52
53 /*
54  * Configure callbacks and other properties that can't be set directly
55  * in the server/client CONF.
56  */
57 static void configure_handshake(SSL_CTX *server_ctx, SSL_CTX *client_ctx,
58                                 const SSL_TEST_CTX *test_ctx)
59 {
60     switch (test_ctx->client_verify_callback) {
61     case SSL_TEST_VERIFY_ACCEPT_ALL:
62         SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_callback,
63                                          NULL);
64         break;
65     case SSL_TEST_VERIFY_REJECT_ALL:
66         SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_callback,
67                                          NULL);
68         break;
69     default:
70         break;
71     }
72 }
73
74
75 typedef enum {
76     PEER_SUCCESS,
77     PEER_RETRY,
78     PEER_ERROR
79 } peer_status_t;
80
81 static peer_status_t do_handshake_step(SSL *ssl)
82 {
83     int ret;
84
85     ret = SSL_do_handshake(ssl);
86
87     if (ret == 1) {
88         return PEER_SUCCESS;
89     } else if (ret == 0) {
90         return PEER_ERROR;
91     } else {
92         int error = SSL_get_error(ssl, ret);
93         /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
94         if (error == SSL_ERROR_WANT_READ)
95             return PEER_RETRY;
96         else
97             return PEER_ERROR;
98     }
99 }
100
101 typedef enum {
102     /* Both parties succeeded. */
103     HANDSHAKE_SUCCESS,
104     /* Client errored. */
105     CLIENT_ERROR,
106     /* Server errored. */
107     SERVER_ERROR,
108     /* Peers are in inconsistent state. */
109     INTERNAL_ERROR,
110     /* One or both peers not done. */
111     HANDSHAKE_RETRY
112 } handshake_status_t;
113
114 /*
115  * Determine the handshake outcome.
116  * last_status: the status of the peer to have acted last.
117  * previous_status: the status of the peer that didn't act last.
118  * client_spoke_last: 1 if the client went last.
119  */
120 static handshake_status_t handshake_status(peer_status_t last_status,
121                                            peer_status_t previous_status,
122                                            int client_spoke_last)
123 {
124     switch (last_status) {
125     case PEER_SUCCESS:
126         switch (previous_status) {
127         case PEER_SUCCESS:
128             /* Both succeeded. */
129             return HANDSHAKE_SUCCESS;
130         case PEER_RETRY:
131             /* Let the first peer finish. */
132             return HANDSHAKE_RETRY;
133         case PEER_ERROR:
134             /*
135              * Second peer succeeded despite the fact that the first peer
136              * already errored. This shouldn't happen.
137              */
138             return INTERNAL_ERROR;
139         }
140
141     case PEER_RETRY:
142         if (previous_status == PEER_RETRY) {
143             /* Neither peer is done. */
144             return HANDSHAKE_RETRY;
145         } else {
146             /*
147              * Deadlock: second peer is waiting for more input while first
148              * peer thinks they're done (no more input is coming).
149              */
150             return INTERNAL_ERROR;
151         }
152     case PEER_ERROR:
153         switch (previous_status) {
154         case PEER_SUCCESS:
155             /*
156              * First peer succeeded but second peer errored.
157              * TODO(emilia): we should be able to continue here (with some
158              * application data?) to ensure the first peer receives the
159              * alert / close_notify.
160              */
161             return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
162         case PEER_RETRY:
163             /* We errored; let the peer finish. */
164             return HANDSHAKE_RETRY;
165         case PEER_ERROR:
166             /* Both peers errored. Return the one that errored first. */
167             return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
168         }
169     }
170     /* Control should never reach here. */
171     return INTERNAL_ERROR;
172 }
173
174 HANDSHAKE_RESULT do_handshake(SSL_CTX *server_ctx, SSL_CTX *client_ctx,
175                               const SSL_TEST_CTX *test_ctx)
176 {
177     SSL *server, *client;
178     BIO *client_to_server, *server_to_client;
179     HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
180     HANDSHAKE_RESULT ret;
181     int client_turn = 1;
182     peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
183     handshake_status_t status = HANDSHAKE_RETRY;
184
185     configure_handshake(server_ctx, client_ctx, test_ctx);
186
187     server = SSL_new(server_ctx);
188     client = SSL_new(client_ctx);
189     OPENSSL_assert(server != NULL && client != NULL);
190
191     memset(&server_ex_data, 0, sizeof(server_ex_data));
192     memset(&client_ex_data, 0, sizeof(client_ex_data));
193     memset(&ret, 0, sizeof(ret));
194     ret.result = SSL_TEST_INTERNAL_ERROR;
195
196     client_to_server = BIO_new(BIO_s_mem());
197     server_to_client = BIO_new(BIO_s_mem());
198
199     OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
200
201     /* Non-blocking bio. */
202     BIO_set_nbio(client_to_server, 1);
203     BIO_set_nbio(server_to_client, 1);
204
205     SSL_set_connect_state(client);
206     SSL_set_accept_state(server);
207
208     /* The bios are now owned by the SSL object. */
209     SSL_set_bio(client, server_to_client, client_to_server);
210     OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
211     OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
212     SSL_set_bio(server, client_to_server, server_to_client);
213
214     ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
215     OPENSSL_assert(ex_data_idx >= 0);
216
217     OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
218                                    &server_ex_data) == 1);
219     OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
220                                    &client_ex_data) == 1);
221
222     SSL_set_info_callback(server, &info_callback);
223     SSL_set_info_callback(client, &info_callback);
224
225     /*
226      * Half-duplex handshake loop.
227      * Client and server speak to each other synchronously in the same process.
228      * We use non-blocking BIOs, so whenever one peer blocks for read, it
229      * returns PEER_RETRY to indicate that it's the other peer's turn to write.
230      * The handshake succeeds once both peers have succeeded. If one peer
231      * errors out, we also let the other peer retry (and presumably fail).
232      */
233     for(;;) {
234         if (client_turn) {
235             client_status = do_handshake_step(client);
236             status = handshake_status(client_status, server_status,
237                                       1 /* client went last */);
238         } else {
239             server_status = do_handshake_step(server);
240             status = handshake_status(server_status, client_status,
241                                       0 /* server went last */);
242         }
243
244         switch (status) {
245         case HANDSHAKE_SUCCESS:
246             ret.result = SSL_TEST_SUCCESS;
247             goto err;
248         case CLIENT_ERROR:
249             ret.result = SSL_TEST_CLIENT_FAIL;
250             goto err;
251         case SERVER_ERROR:
252             ret.result = SSL_TEST_SERVER_FAIL;
253             goto err;
254         case INTERNAL_ERROR:
255             ret.result = SSL_TEST_INTERNAL_ERROR;
256             goto err;
257         case HANDSHAKE_RETRY:
258             /* Continue. */
259             client_turn ^= 1;
260             break;
261         }
262     }
263  err:
264     ret.server_alert_sent = server_ex_data.alert_sent;
265     ret.server_alert_received = client_ex_data.alert_received;
266     ret.client_alert_sent = client_ex_data.alert_sent;
267     ret.client_alert_received = server_ex_data.alert_received;
268     ret.server_protocol = SSL_version(server);
269     ret.client_protocol = SSL_version(client);
270
271     SSL_free(server);
272     SSL_free(client);
273     return ret;
274 }