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