]>
Commit | Line | Data |
---|---|---|
453dfd8d EK |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
440e5d80 RS |
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 | |
453dfd8d | 7 | * https://www.openssl.org/source/license.html |
453dfd8d EK |
8 | */ |
9 | ||
10 | #include <string.h> | |
11 | ||
12 | #include <openssl/bio.h> | |
a263f320 | 13 | #include <openssl/x509_vfy.h> |
453dfd8d EK |
14 | #include <openssl/ssl.h> |
15 | ||
16 | #include "handshake_helper.h" | |
d61f0078 | 17 | #include "testutil.h" |
453dfd8d | 18 | |
ce2cdac2 EK |
19 | HANDSHAKE_RESULT *HANDSHAKE_RESULT_new() |
20 | { | |
d61f0078 EK |
21 | HANDSHAKE_RESULT *ret = OPENSSL_zalloc(sizeof(*ret)); |
22 | TEST_check(ret != NULL); | |
ce2cdac2 EK |
23 | return ret; |
24 | } | |
25 | ||
26 | void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result) | |
27 | { | |
2f35e6a3 EK |
28 | if (result == NULL) |
29 | return; | |
ce2cdac2 EK |
30 | OPENSSL_free(result->client_npn_negotiated); |
31 | OPENSSL_free(result->server_npn_negotiated); | |
32 | OPENSSL_free(result->client_alpn_negotiated); | |
33 | OPENSSL_free(result->server_alpn_negotiated); | |
34 | OPENSSL_free(result); | |
35 | } | |
36 | ||
453dfd8d EK |
37 | /* |
38 | * Since there appears to be no way to extract the sent/received alert | |
39 | * from the SSL object directly, we use the info callback and stash | |
40 | * the result in ex_data. | |
41 | */ | |
e0421bd8 | 42 | typedef struct handshake_ex_data_st { |
453dfd8d | 43 | int alert_sent; |
dd8e5a57 | 44 | int num_fatal_alerts_sent; |
453dfd8d | 45 | int alert_received; |
5c753de6 | 46 | int session_ticket_do_not_call; |
d2b23cd2 | 47 | ssl_servername_t servername; |
453dfd8d EK |
48 | } HANDSHAKE_EX_DATA; |
49 | ||
e0421bd8 | 50 | typedef struct ctx_data_st { |
ce2cdac2 EK |
51 | unsigned char *npn_protocols; |
52 | size_t npn_protocols_len; | |
53 | unsigned char *alpn_protocols; | |
54 | size_t alpn_protocols_len; | |
55 | } CTX_DATA; | |
56 | ||
57 | /* |ctx_data| itself is stack-allocated. */ | |
58 | static void ctx_data_free_data(CTX_DATA *ctx_data) | |
59 | { | |
60 | OPENSSL_free(ctx_data->npn_protocols); | |
61 | ctx_data->npn_protocols = NULL; | |
62 | OPENSSL_free(ctx_data->alpn_protocols); | |
63 | ctx_data->alpn_protocols = NULL; | |
64 | } | |
65 | ||
453dfd8d EK |
66 | static int ex_data_idx; |
67 | ||
a8c82fa0 | 68 | static void info_cb(const SSL *s, int where, int ret) |
453dfd8d EK |
69 | { |
70 | if (where & SSL_CB_ALERT) { | |
71 | HANDSHAKE_EX_DATA *ex_data = | |
72 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); | |
73 | if (where & SSL_CB_WRITE) { | |
74 | ex_data->alert_sent = ret; | |
dd8e5a57 EK |
75 | if (strcmp(SSL_alert_type_string(ret), "F") == 0 |
76 | || strcmp(SSL_alert_desc_string(ret), "CN") == 0) | |
77 | ex_data->num_fatal_alerts_sent++; | |
453dfd8d EK |
78 | } else { |
79 | ex_data->alert_received = ret; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
ce2cdac2 | 84 | /* Select the appropriate server CTX. |
d2b23cd2 EK |
85 | * Returns SSL_TLSEXT_ERR_OK if a match was found. |
86 | * If |ignore| is 1, returns SSL_TLSEXT_ERR_NOACK on mismatch. | |
87 | * Otherwise, returns SSL_TLSEXT_ERR_ALERT_FATAL on mismatch. | |
88 | * An empty SNI extension also returns SSL_TSLEXT_ERR_NOACK. | |
89 | */ | |
90 | static int select_server_ctx(SSL *s, void *arg, int ignore) | |
81fc33c9 EK |
91 | { |
92 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); | |
d2b23cd2 EK |
93 | HANDSHAKE_EX_DATA *ex_data = |
94 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); | |
95 | ||
96 | if (servername == NULL) { | |
97 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; | |
98 | return SSL_TLSEXT_ERR_NOACK; | |
99 | } | |
100 | ||
101 | if (strcmp(servername, "server2") == 0) { | |
81fc33c9 EK |
102 | SSL_CTX *new_ctx = (SSL_CTX*)arg; |
103 | SSL_set_SSL_CTX(s, new_ctx); | |
104 | /* | |
105 | * Copy over all the SSL_CTX options - reasonable behavior | |
106 | * allows testing of cases where the options between two | |
107 | * contexts differ/conflict | |
108 | */ | |
109 | SSL_clear_options(s, 0xFFFFFFFFL); | |
110 | SSL_set_options(s, SSL_CTX_get_options(new_ctx)); | |
d2b23cd2 EK |
111 | |
112 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; | |
113 | return SSL_TLSEXT_ERR_OK; | |
114 | } else if (strcmp(servername, "server1") == 0) { | |
115 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; | |
116 | return SSL_TLSEXT_ERR_OK; | |
117 | } else if (ignore) { | |
118 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; | |
119 | return SSL_TLSEXT_ERR_NOACK; | |
120 | } else { | |
121 | /* Don't set an explicit alert, to test library defaults. */ | |
122 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
81fc33c9 | 123 | } |
d2b23cd2 EK |
124 | } |
125 | ||
126 | /* | |
127 | * (RFC 6066): | |
128 | * If the server understood the ClientHello extension but | |
129 | * does not recognize the server name, the server SHOULD take one of two | |
130 | * actions: either abort the handshake by sending a fatal-level | |
131 | * unrecognized_name(112) alert or continue the handshake. | |
132 | * | |
133 | * This behaviour is up to the application to configure; we test both | |
134 | * configurations to ensure the state machine propagates the result | |
135 | * correctly. | |
136 | */ | |
137 | static int servername_ignore_cb(SSL *s, int *ad, void *arg) | |
138 | { | |
139 | return select_server_ctx(s, arg, 1); | |
140 | } | |
141 | ||
142 | static int servername_reject_cb(SSL *s, int *ad, void *arg) | |
143 | { | |
144 | return select_server_ctx(s, arg, 0); | |
81fc33c9 EK |
145 | } |
146 | ||
767ccc3b MC |
147 | static unsigned char dummy_ocsp_resp_good_val = 0xff; |
148 | static unsigned char dummy_ocsp_resp_bad_val = 0xfe; | |
149 | ||
150 | static int server_ocsp_cb(SSL *s, void *arg) | |
151 | { | |
152 | unsigned char *resp; | |
153 | ||
154 | resp = OPENSSL_malloc(1); | |
155 | if (resp == NULL) | |
156 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
157 | /* | |
158 | * For the purposes of testing we just send back a dummy OCSP response | |
159 | */ | |
160 | *resp = *(unsigned char *)arg; | |
161 | if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1)) | |
162 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
163 | ||
164 | return SSL_TLSEXT_ERR_OK; | |
165 | } | |
166 | ||
167 | static int client_ocsp_cb(SSL *s, void *arg) | |
168 | { | |
169 | const unsigned char *resp; | |
170 | int len; | |
171 | ||
172 | len = SSL_get_tlsext_status_ocsp_resp(s, &resp); | |
173 | if (len != 1 || *resp != dummy_ocsp_resp_good_val) | |
174 | return 0; | |
175 | ||
176 | return 1; | |
177 | } | |
178 | ||
a8c82fa0 | 179 | static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) { |
a263f320 EK |
180 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
181 | return 0; | |
182 | } | |
183 | ||
a8c82fa0 | 184 | static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) { |
a263f320 EK |
185 | return 1; |
186 | } | |
187 | ||
ce2cdac2 | 188 | static int broken_session_ticket_cb(SSL *s, unsigned char *key_name, unsigned char *iv, |
a8c82fa0 | 189 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc) |
5c753de6 TS |
190 | { |
191 | return 0; | |
192 | } | |
193 | ||
ce2cdac2 | 194 | static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name, |
a8c82fa0 RL |
195 | unsigned char *iv, |
196 | EVP_CIPHER_CTX *ctx, | |
197 | HMAC_CTX *hctx, int enc) | |
5c753de6 TS |
198 | { |
199 | HANDSHAKE_EX_DATA *ex_data = | |
200 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); | |
201 | ex_data->session_ticket_do_not_call = 1; | |
202 | return 0; | |
203 | } | |
204 | ||
ce2cdac2 EK |
205 | /* Parse the comma-separated list into TLS format. */ |
206 | static void parse_protos(const char *protos, unsigned char **out, size_t *outlen) | |
207 | { | |
208 | size_t len, i, prefix; | |
209 | ||
210 | len = strlen(protos); | |
211 | ||
212 | /* Should never have reuse. */ | |
d61f0078 | 213 | TEST_check(*out == NULL); |
ce2cdac2 EK |
214 | |
215 | /* Test values are small, so we omit length limit checks. */ | |
216 | *out = OPENSSL_malloc(len + 1); | |
d61f0078 | 217 | TEST_check(*out != NULL); |
ce2cdac2 EK |
218 | *outlen = len + 1; |
219 | ||
220 | /* | |
221 | * foo => '3', 'f', 'o', 'o' | |
222 | * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r' | |
223 | */ | |
224 | memcpy(*out + 1, protos, len); | |
225 | ||
226 | prefix = 0; | |
227 | i = prefix + 1; | |
228 | while (i <= len) { | |
229 | if ((*out)[i] == ',') { | |
d61f0078 | 230 | TEST_check(i - 1 - prefix > 0); |
ce2cdac2 EK |
231 | (*out)[prefix] = i - 1 - prefix; |
232 | prefix = i; | |
233 | } | |
234 | i++; | |
235 | } | |
d61f0078 | 236 | TEST_check(len - prefix > 0); |
ce2cdac2 EK |
237 | (*out)[prefix] = len - prefix; |
238 | } | |
239 | ||
7b7cea6d | 240 | #ifndef OPENSSL_NO_NEXTPROTONEG |
ce2cdac2 EK |
241 | /* |
242 | * The client SHOULD select the first protocol advertised by the server that it | |
243 | * also supports. In the event that the client doesn't support any of server's | |
244 | * protocols, or the server doesn't advertise any, it SHOULD select the first | |
245 | * protocol that it supports. | |
246 | */ | |
247 | static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen, | |
248 | const unsigned char *in, unsigned int inlen, | |
249 | void *arg) | |
250 | { | |
251 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); | |
252 | int ret; | |
253 | ||
254 | ret = SSL_select_next_proto(out, outlen, in, inlen, | |
255 | ctx_data->npn_protocols, | |
256 | ctx_data->npn_protocols_len); | |
257 | /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */ | |
d61f0078 | 258 | TEST_check(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP); |
ce2cdac2 EK |
259 | return SSL_TLSEXT_ERR_OK; |
260 | } | |
261 | ||
262 | static int server_npn_cb(SSL *s, const unsigned char **data, | |
263 | unsigned int *len, void *arg) | |
264 | { | |
265 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); | |
266 | *data = ctx_data->npn_protocols; | |
267 | *len = ctx_data->npn_protocols_len; | |
268 | return SSL_TLSEXT_ERR_OK; | |
269 | } | |
7b7cea6d | 270 | #endif |
ce2cdac2 EK |
271 | |
272 | /* | |
273 | * The server SHOULD select the most highly preferred protocol that it supports | |
274 | * and that is also advertised by the client. In the event that the server | |
275 | * supports no protocols that the client advertises, then the server SHALL | |
276 | * respond with a fatal "no_application_protocol" alert. | |
277 | */ | |
278 | static int server_alpn_cb(SSL *s, const unsigned char **out, | |
279 | unsigned char *outlen, const unsigned char *in, | |
280 | unsigned int inlen, void *arg) | |
281 | { | |
282 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); | |
283 | int ret; | |
284 | ||
285 | /* SSL_select_next_proto isn't const-correct... */ | |
286 | unsigned char *tmp_out; | |
287 | ||
288 | /* | |
289 | * The result points either to |in| or to |ctx_data->alpn_protocols|. | |
290 | * The callback is allowed to point to |in| or to a long-lived buffer, | |
291 | * so we can return directly without storing a copy. | |
292 | */ | |
293 | ret = SSL_select_next_proto(&tmp_out, outlen, | |
294 | ctx_data->alpn_protocols, | |
295 | ctx_data->alpn_protocols_len, in, inlen); | |
296 | ||
297 | *out = tmp_out; | |
298 | /* Unlike NPN, we don't tolerate a mismatch. */ | |
299 | return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK | |
300 | : SSL_TLSEXT_ERR_NOACK; | |
301 | } | |
ce2cdac2 | 302 | |
a263f320 EK |
303 | /* |
304 | * Configure callbacks and other properties that can't be set directly | |
305 | * in the server/client CONF. | |
306 | */ | |
81fc33c9 EK |
307 | static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, |
308 | SSL_CTX *client_ctx, | |
6dc99745 | 309 | const SSL_TEST_CTX *test, |
9f48bbac | 310 | const SSL_TEST_EXTRA_CONF *extra, |
ce2cdac2 EK |
311 | CTX_DATA *server_ctx_data, |
312 | CTX_DATA *server2_ctx_data, | |
313 | CTX_DATA *client_ctx_data) | |
a263f320 | 314 | { |
590ed3d7 EK |
315 | unsigned char *ticket_keys; |
316 | size_t ticket_key_len; | |
317 | ||
6dc99745 EK |
318 | TEST_check(SSL_CTX_set_max_send_fragment(server_ctx, |
319 | test->max_fragment_size) == 1); | |
320 | if (server2_ctx != NULL) { | |
321 | TEST_check(SSL_CTX_set_max_send_fragment(server2_ctx, | |
322 | test->max_fragment_size) == 1); | |
323 | } | |
324 | TEST_check(SSL_CTX_set_max_send_fragment(client_ctx, | |
325 | test->max_fragment_size) == 1); | |
326 | ||
9f48bbac | 327 | switch (extra->client.verify_callback) { |
a263f320 | 328 | case SSL_TEST_VERIFY_ACCEPT_ALL: |
a8c82fa0 | 329 | SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb, |
a263f320 EK |
330 | NULL); |
331 | break; | |
332 | case SSL_TEST_VERIFY_REJECT_ALL: | |
a8c82fa0 | 333 | SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, |
a263f320 EK |
334 | NULL); |
335 | break; | |
f3b3d7f0 | 336 | case SSL_TEST_VERIFY_NONE: |
a263f320 EK |
337 | break; |
338 | } | |
81fc33c9 EK |
339 | |
340 | /* link the two contexts for SNI purposes */ | |
9f48bbac | 341 | switch (extra->server.servername_callback) { |
d2b23cd2 EK |
342 | case SSL_TEST_SERVERNAME_IGNORE_MISMATCH: |
343 | SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb); | |
344 | SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); | |
345 | break; | |
346 | case SSL_TEST_SERVERNAME_REJECT_MISMATCH: | |
347 | SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb); | |
348 | SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); | |
349 | break; | |
f3b3d7f0 | 350 | case SSL_TEST_SERVERNAME_CB_NONE: |
d2b23cd2 EK |
351 | break; |
352 | } | |
353 | ||
767ccc3b MC |
354 | if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) { |
355 | SSL_CTX_set_tlsext_status_type(client_ctx, TLSEXT_STATUSTYPE_ocsp); | |
356 | SSL_CTX_set_tlsext_status_cb(client_ctx, client_ocsp_cb); | |
357 | SSL_CTX_set_tlsext_status_arg(client_ctx, NULL); | |
358 | SSL_CTX_set_tlsext_status_cb(server_ctx, server_ocsp_cb); | |
359 | SSL_CTX_set_tlsext_status_arg(server_ctx, | |
360 | ((extra->server.cert_status == SSL_TEST_CERT_STATUS_GOOD_RESPONSE) | |
361 | ? &dummy_ocsp_resp_good_val : &dummy_ocsp_resp_bad_val)); | |
362 | } | |
363 | ||
81fc33c9 EK |
364 | /* |
365 | * The initial_ctx/session_ctx always handles the encrypt/decrypt of the | |
366 | * session ticket. This ticket_key callback is assigned to the second | |
367 | * session (assigned via SNI), and should never be invoked | |
368 | */ | |
d2b23cd2 EK |
369 | if (server2_ctx != NULL) |
370 | SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx, | |
371 | do_not_call_session_ticket_cb); | |
81fc33c9 | 372 | |
9f48bbac | 373 | if (extra->server.broken_session_ticket) { |
a8c82fa0 | 374 | SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_cb); |
5c753de6 | 375 | } |
620c6ad3 | 376 | #ifndef OPENSSL_NO_NEXTPROTONEG |
9f48bbac EK |
377 | if (extra->server.npn_protocols != NULL) { |
378 | parse_protos(extra->server.npn_protocols, | |
ce2cdac2 EK |
379 | &server_ctx_data->npn_protocols, |
380 | &server_ctx_data->npn_protocols_len); | |
381 | SSL_CTX_set_next_protos_advertised_cb(server_ctx, server_npn_cb, | |
382 | server_ctx_data); | |
383 | } | |
9f48bbac EK |
384 | if (extra->server2.npn_protocols != NULL) { |
385 | parse_protos(extra->server2.npn_protocols, | |
ce2cdac2 EK |
386 | &server2_ctx_data->npn_protocols, |
387 | &server2_ctx_data->npn_protocols_len); | |
d61f0078 | 388 | TEST_check(server2_ctx != NULL); |
ce2cdac2 EK |
389 | SSL_CTX_set_next_protos_advertised_cb(server2_ctx, server_npn_cb, |
390 | server2_ctx_data); | |
391 | } | |
9f48bbac EK |
392 | if (extra->client.npn_protocols != NULL) { |
393 | parse_protos(extra->client.npn_protocols, | |
ce2cdac2 EK |
394 | &client_ctx_data->npn_protocols, |
395 | &client_ctx_data->npn_protocols_len); | |
396 | SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb, | |
397 | client_ctx_data); | |
398 | } | |
7b7cea6d | 399 | #endif |
9f48bbac EK |
400 | if (extra->server.alpn_protocols != NULL) { |
401 | parse_protos(extra->server.alpn_protocols, | |
ce2cdac2 EK |
402 | &server_ctx_data->alpn_protocols, |
403 | &server_ctx_data->alpn_protocols_len); | |
404 | SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data); | |
405 | } | |
9f48bbac | 406 | if (extra->server2.alpn_protocols != NULL) { |
d61f0078 | 407 | TEST_check(server2_ctx != NULL); |
9f48bbac | 408 | parse_protos(extra->server2.alpn_protocols, |
ce2cdac2 EK |
409 | &server2_ctx_data->alpn_protocols, |
410 | &server2_ctx_data->alpn_protocols_len); | |
411 | SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, server2_ctx_data); | |
412 | } | |
9f48bbac | 413 | if (extra->client.alpn_protocols != NULL) { |
ce2cdac2 EK |
414 | unsigned char *alpn_protos = NULL; |
415 | size_t alpn_protos_len; | |
9f48bbac | 416 | parse_protos(extra->client.alpn_protocols, |
ce2cdac2 EK |
417 | &alpn_protos, &alpn_protos_len); |
418 | /* Reversed return value convention... */ | |
d61f0078 EK |
419 | TEST_check(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos, |
420 | alpn_protos_len) == 0); | |
ce2cdac2 EK |
421 | OPENSSL_free(alpn_protos); |
422 | } | |
7b7cea6d | 423 | |
590ed3d7 EK |
424 | /* |
425 | * Use fixed session ticket keys so that we can decrypt a ticket created with | |
426 | * one CTX in another CTX. Don't address server2 for the moment. | |
427 | */ | |
428 | ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0); | |
429 | ticket_keys = OPENSSL_zalloc(ticket_key_len); | |
d61f0078 EK |
430 | TEST_check(ticket_keys != NULL); |
431 | TEST_check(SSL_CTX_set_tlsext_ticket_keys(server_ctx, ticket_keys, | |
432 | ticket_key_len) == 1); | |
590ed3d7 | 433 | OPENSSL_free(ticket_keys); |
da085d27 | 434 | |
be82f7b3 EK |
435 | /* The default log list includes EC keys, so CT can't work without EC. */ |
436 | #if !defined(OPENSSL_NO_CT) && !defined(OPENSSL_NO_EC) | |
d61f0078 | 437 | TEST_check(SSL_CTX_set_default_ctlog_list_file(client_ctx)); |
da085d27 EK |
438 | switch (extra->client.ct_validation) { |
439 | case SSL_TEST_CT_VALIDATION_PERMISSIVE: | |
d61f0078 | 440 | TEST_check(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_PERMISSIVE)); |
da085d27 EK |
441 | break; |
442 | case SSL_TEST_CT_VALIDATION_STRICT: | |
d61f0078 | 443 | TEST_check(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT)); |
da085d27 EK |
444 | break; |
445 | case SSL_TEST_CT_VALIDATION_NONE: | |
446 | break; | |
447 | } | |
448 | #endif | |
5c753de6 TS |
449 | } |
450 | ||
ce2cdac2 | 451 | /* Configure per-SSL callbacks and other properties. */ |
5c753de6 | 452 | static void configure_handshake_ssl(SSL *server, SSL *client, |
9f48bbac | 453 | const SSL_TEST_EXTRA_CONF *extra) |
5c753de6 | 454 | { |
9f48bbac | 455 | if (extra->client.servername != SSL_TEST_SERVERNAME_NONE) |
81fc33c9 | 456 | SSL_set_tlsext_host_name(client, |
9f48bbac | 457 | ssl_servername_name(extra->client.servername)); |
a263f320 EK |
458 | } |
459 | ||
e0421bd8 | 460 | /* The status for each connection phase. */ |
453dfd8d EK |
461 | typedef enum { |
462 | PEER_SUCCESS, | |
463 | PEER_RETRY, | |
464 | PEER_ERROR | |
465 | } peer_status_t; | |
466 | ||
e0421bd8 EK |
467 | /* An SSL object and associated read-write buffers. */ |
468 | typedef struct peer_st { | |
469 | SSL *ssl; | |
470 | /* Buffer lengths are int to match the SSL read/write API. */ | |
471 | unsigned char *write_buf; | |
472 | int write_buf_len; | |
473 | unsigned char *read_buf; | |
474 | int read_buf_len; | |
475 | int bytes_to_write; | |
476 | int bytes_to_read; | |
477 | peer_status_t status; | |
478 | } PEER; | |
479 | ||
480 | static void create_peer(PEER *peer, SSL_CTX *ctx) | |
481 | { | |
482 | static const int peer_buffer_size = 64 * 1024; | |
483 | ||
484 | peer->ssl = SSL_new(ctx); | |
485 | TEST_check(peer->ssl != NULL); | |
486 | peer->write_buf = OPENSSL_zalloc(peer_buffer_size); | |
487 | TEST_check(peer->write_buf != NULL); | |
488 | peer->read_buf = OPENSSL_zalloc(peer_buffer_size); | |
489 | TEST_check(peer->read_buf != NULL); | |
490 | peer->write_buf_len = peer->read_buf_len = peer_buffer_size; | |
491 | } | |
492 | ||
493 | static void peer_free_data(PEER *peer) | |
494 | { | |
495 | SSL_free(peer->ssl); | |
496 | OPENSSL_free(peer->write_buf); | |
497 | OPENSSL_free(peer->read_buf); | |
498 | } | |
499 | ||
500 | /* | |
501 | * Note that we could do the handshake transparently under an SSL_write, | |
502 | * but separating the steps is more helpful for debugging test failures. | |
503 | */ | |
504 | static void do_handshake_step(PEER *peer) | |
505 | { | |
506 | int ret; | |
507 | ||
508 | TEST_check(peer->status == PEER_RETRY); | |
509 | ret = SSL_do_handshake(peer->ssl); | |
510 | ||
511 | if (ret == 1) { | |
512 | peer->status = PEER_SUCCESS; | |
513 | } else if (ret == 0) { | |
514 | peer->status = PEER_ERROR; | |
515 | } else { | |
516 | int error = SSL_get_error(peer->ssl, ret); | |
517 | /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */ | |
518 | if (error != SSL_ERROR_WANT_READ) | |
519 | peer->status = PEER_ERROR; | |
520 | } | |
521 | } | |
522 | ||
523 | /*- | |
524 | * Send/receive some application data. The read-write sequence is | |
525 | * Peer A: (R) W - first read will yield no data | |
526 | * Peer B: R W | |
527 | * ... | |
528 | * Peer A: R W | |
529 | * Peer B: R W | |
530 | * Peer A: R | |
531 | */ | |
532 | static void do_app_data_step(PEER *peer) | |
533 | { | |
534 | int ret = 1, write_bytes; | |
535 | ||
536 | TEST_check(peer->status == PEER_RETRY); | |
537 | ||
538 | /* We read everything available... */ | |
539 | while (ret > 0 && peer->bytes_to_read) { | |
540 | ret = SSL_read(peer->ssl, peer->read_buf, peer->read_buf_len); | |
541 | if (ret > 0) { | |
542 | TEST_check(ret <= peer->bytes_to_read); | |
543 | peer->bytes_to_read -= ret; | |
544 | } else if (ret == 0) { | |
545 | peer->status = PEER_ERROR; | |
546 | return; | |
547 | } else { | |
548 | int error = SSL_get_error(peer->ssl, ret); | |
549 | if (error != SSL_ERROR_WANT_READ) { | |
550 | peer->status = PEER_ERROR; | |
551 | return; | |
552 | } /* Else continue with write. */ | |
553 | } | |
554 | } | |
555 | ||
556 | /* ... but we only write one write-buffer-full of data. */ | |
557 | write_bytes = peer->bytes_to_write < peer->write_buf_len ? peer->bytes_to_write : | |
558 | peer->write_buf_len; | |
559 | if (write_bytes) { | |
560 | ret = SSL_write(peer->ssl, peer->write_buf, write_bytes); | |
561 | if (ret > 0) { | |
562 | /* SSL_write will only succeed with a complete write. */ | |
563 | TEST_check(ret == write_bytes); | |
564 | peer->bytes_to_write -= ret; | |
565 | } else { | |
566 | /* | |
567 | * We should perhaps check for SSL_ERROR_WANT_READ/WRITE here | |
568 | * but this doesn't yet occur with current app data sizes. | |
569 | */ | |
570 | peer->status = PEER_ERROR; | |
571 | return; | |
572 | } | |
573 | } | |
574 | ||
575 | /* | |
576 | * We could simply finish when there was nothing to read, and we have | |
577 | * nothing left to write. But keeping track of the expected number of bytes | |
578 | * to read gives us somewhat better guarantees that all data sent is in fact | |
579 | * received. | |
580 | */ | |
581 | if (!peer->bytes_to_write && !peer->bytes_to_read) { | |
582 | peer->status = PEER_SUCCESS; | |
583 | } | |
584 | } | |
585 | ||
fe7dd553 | 586 | static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer) |
e42c4544 MC |
587 | { |
588 | int ret; | |
589 | char buf; | |
590 | ||
591 | TEST_check(peer->status == PEER_RETRY); | |
fe7dd553 MC |
592 | TEST_check(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER |
593 | || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT); | |
594 | ||
595 | /* Check if we are the peer that is going to initiate */ | |
596 | if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER | |
597 | && SSL_is_server(peer->ssl)) | |
598 | || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT | |
599 | && !SSL_is_server(peer->ssl))) { | |
e42c4544 | 600 | /* |
fe7dd553 MC |
601 | * If we already asked for a renegotiation then fall through to the |
602 | * SSL_read() below. | |
e42c4544 | 603 | */ |
fe7dd553 MC |
604 | if (!SSL_renegotiate_pending(peer->ssl)) { |
605 | /* | |
606 | * If we are the client we will always attempt to resume the | |
607 | * session. The server may or may not resume dependant on the | |
608 | * setting of SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
609 | */ | |
610 | if (SSL_is_server(peer->ssl)) | |
611 | ret = SSL_renegotiate(peer->ssl); | |
612 | else | |
613 | ret = SSL_renegotiate_abbreviated(peer->ssl); | |
614 | if (!ret) { | |
615 | peer->status = PEER_ERROR; | |
616 | return; | |
617 | } | |
618 | do_handshake_step(peer); | |
619 | /* | |
620 | * If status is PEER_RETRY it means we're waiting on the peer to | |
621 | * continue the handshake. As far as setting up the renegotiation is | |
622 | * concerned that is a success. The next step will continue the | |
623 | * handshake to its conclusion. | |
624 | * | |
625 | * If status is PEER_SUCCESS then we are the server and we have | |
626 | * successfully sent the HelloRequest. We need to continue to wait | |
627 | * until the handshake arrives from the client. | |
628 | */ | |
629 | if (peer->status == PEER_RETRY) | |
630 | peer->status = PEER_SUCCESS; | |
631 | else if (peer->status == PEER_SUCCESS) | |
632 | peer->status = PEER_RETRY; | |
633 | return; | |
634 | } | |
e42c4544 MC |
635 | } |
636 | ||
637 | /* | |
638 | * The SSL object is still expecting app data, even though it's going to | |
639 | * get a handshake message. We try to read, and it should fail - after which | |
640 | * we should be in a handshake | |
641 | */ | |
642 | ret = SSL_read(peer->ssl, &buf, sizeof(buf)); | |
643 | if (ret >= 0) { | |
fe7dd553 MC |
644 | /* |
645 | * We're not actually expecting data - we're expecting a reneg to | |
646 | * start | |
647 | */ | |
e42c4544 MC |
648 | peer->status = PEER_ERROR; |
649 | return; | |
650 | } else { | |
651 | int error = SSL_get_error(peer->ssl, ret); | |
fe7dd553 | 652 | if (error != SSL_ERROR_WANT_READ) { |
e42c4544 MC |
653 | peer->status = PEER_ERROR; |
654 | return; | |
655 | } | |
fe7dd553 MC |
656 | /* If we're no in init yet then we're not done with setup yet */ |
657 | if (!SSL_in_init(peer->ssl)) | |
658 | return; | |
e42c4544 MC |
659 | } |
660 | ||
661 | peer->status = PEER_SUCCESS; | |
662 | } | |
663 | ||
664 | ||
590ed3d7 EK |
665 | /* |
666 | * RFC 5246 says: | |
667 | * | |
668 | * Note that as of TLS 1.1, | |
669 | * failure to properly close a connection no longer requires that a | |
670 | * session not be resumed. This is a change from TLS 1.0 to conform | |
671 | * with widespread implementation practice. | |
672 | * | |
673 | * However, | |
674 | * (a) OpenSSL requires that a connection be shutdown for all protocol versions. | |
675 | * (b) We test lower versions, too. | |
676 | * So we just implement shutdown. We do a full bidirectional shutdown so that we | |
677 | * can compare sent and received close_notify alerts and get some test coverage | |
678 | * for SSL_shutdown as a bonus. | |
679 | */ | |
e0421bd8 | 680 | static void do_shutdown_step(PEER *peer) |
453dfd8d EK |
681 | { |
682 | int ret; | |
683 | ||
e0421bd8 EK |
684 | TEST_check(peer->status == PEER_RETRY); |
685 | ret = SSL_shutdown(peer->ssl); | |
453dfd8d EK |
686 | |
687 | if (ret == 1) { | |
e0421bd8 EK |
688 | peer->status = PEER_SUCCESS; |
689 | } else if (ret < 0) { /* On 0, we retry. */ | |
690 | int error = SSL_get_error(peer->ssl, ret); | |
453dfd8d | 691 | /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */ |
e0421bd8 EK |
692 | if (error != SSL_ERROR_WANT_READ) |
693 | peer->status = PEER_ERROR; | |
694 | } | |
695 | } | |
696 | ||
697 | typedef enum { | |
698 | HANDSHAKE, | |
e42c4544 MC |
699 | RENEG_APPLICATION_DATA, |
700 | RENEG_SETUP, | |
701 | RENEG_HANDSHAKE, | |
e0421bd8 EK |
702 | APPLICATION_DATA, |
703 | SHUTDOWN, | |
704 | CONNECTION_DONE | |
705 | } connect_phase_t; | |
706 | ||
e42c4544 MC |
707 | static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx, |
708 | connect_phase_t phase) | |
e0421bd8 EK |
709 | { |
710 | switch (phase) { | |
711 | case HANDSHAKE: | |
fe7dd553 MC |
712 | if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER |
713 | || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT) | |
e42c4544 MC |
714 | return RENEG_APPLICATION_DATA; |
715 | return APPLICATION_DATA; | |
716 | case RENEG_APPLICATION_DATA: | |
717 | return RENEG_SETUP; | |
718 | case RENEG_SETUP: | |
719 | return RENEG_HANDSHAKE; | |
720 | case RENEG_HANDSHAKE: | |
e0421bd8 EK |
721 | return APPLICATION_DATA; |
722 | case APPLICATION_DATA: | |
723 | return SHUTDOWN; | |
724 | case SHUTDOWN: | |
725 | return CONNECTION_DONE; | |
f3b3d7f0 RS |
726 | case CONNECTION_DONE: |
727 | TEST_check(0); | |
728 | break; | |
e0421bd8 | 729 | } |
f3b3d7f0 | 730 | return -1; |
e0421bd8 EK |
731 | } |
732 | ||
fe7dd553 MC |
733 | static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer, |
734 | connect_phase_t phase) | |
e0421bd8 EK |
735 | { |
736 | switch (phase) { | |
737 | case HANDSHAKE: | |
738 | do_handshake_step(peer); | |
739 | break; | |
e42c4544 MC |
740 | case RENEG_APPLICATION_DATA: |
741 | do_app_data_step(peer); | |
742 | break; | |
743 | case RENEG_SETUP: | |
fe7dd553 | 744 | do_reneg_setup_step(test_ctx, peer); |
e42c4544 MC |
745 | break; |
746 | case RENEG_HANDSHAKE: | |
747 | do_handshake_step(peer); | |
748 | break; | |
e0421bd8 EK |
749 | case APPLICATION_DATA: |
750 | do_app_data_step(peer); | |
751 | break; | |
752 | case SHUTDOWN: | |
753 | do_shutdown_step(peer); | |
754 | break; | |
f3b3d7f0 | 755 | case CONNECTION_DONE: |
e0421bd8 | 756 | TEST_check(0); |
f3b3d7f0 | 757 | break; |
453dfd8d EK |
758 | } |
759 | } | |
760 | ||
761 | typedef enum { | |
762 | /* Both parties succeeded. */ | |
763 | HANDSHAKE_SUCCESS, | |
764 | /* Client errored. */ | |
765 | CLIENT_ERROR, | |
766 | /* Server errored. */ | |
767 | SERVER_ERROR, | |
768 | /* Peers are in inconsistent state. */ | |
769 | INTERNAL_ERROR, | |
770 | /* One or both peers not done. */ | |
771 | HANDSHAKE_RETRY | |
772 | } handshake_status_t; | |
773 | ||
774 | /* | |
775 | * Determine the handshake outcome. | |
776 | * last_status: the status of the peer to have acted last. | |
777 | * previous_status: the status of the peer that didn't act last. | |
778 | * client_spoke_last: 1 if the client went last. | |
779 | */ | |
780 | static handshake_status_t handshake_status(peer_status_t last_status, | |
781 | peer_status_t previous_status, | |
782 | int client_spoke_last) | |
783 | { | |
784 | switch (last_status) { | |
785 | case PEER_SUCCESS: | |
786 | switch (previous_status) { | |
787 | case PEER_SUCCESS: | |
788 | /* Both succeeded. */ | |
789 | return HANDSHAKE_SUCCESS; | |
790 | case PEER_RETRY: | |
791 | /* Let the first peer finish. */ | |
792 | return HANDSHAKE_RETRY; | |
793 | case PEER_ERROR: | |
794 | /* | |
795 | * Second peer succeeded despite the fact that the first peer | |
796 | * already errored. This shouldn't happen. | |
797 | */ | |
798 | return INTERNAL_ERROR; | |
799 | } | |
800 | ||
801 | case PEER_RETRY: | |
802 | if (previous_status == PEER_RETRY) { | |
803 | /* Neither peer is done. */ | |
804 | return HANDSHAKE_RETRY; | |
805 | } else { | |
806 | /* | |
807 | * Deadlock: second peer is waiting for more input while first | |
808 | * peer thinks they're done (no more input is coming). | |
809 | */ | |
810 | return INTERNAL_ERROR; | |
811 | } | |
812 | case PEER_ERROR: | |
813 | switch (previous_status) { | |
814 | case PEER_SUCCESS: | |
815 | /* | |
816 | * First peer succeeded but second peer errored. | |
817 | * TODO(emilia): we should be able to continue here (with some | |
818 | * application data?) to ensure the first peer receives the | |
819 | * alert / close_notify. | |
e0421bd8 | 820 | * (No tests currently exercise this branch.) |
453dfd8d EK |
821 | */ |
822 | return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR; | |
823 | case PEER_RETRY: | |
824 | /* We errored; let the peer finish. */ | |
825 | return HANDSHAKE_RETRY; | |
826 | case PEER_ERROR: | |
827 | /* Both peers errored. Return the one that errored first. */ | |
828 | return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR; | |
829 | } | |
830 | } | |
831 | /* Control should never reach here. */ | |
832 | return INTERNAL_ERROR; | |
833 | } | |
834 | ||
ce2cdac2 EK |
835 | /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */ |
836 | static char *dup_str(const unsigned char *in, size_t len) | |
837 | { | |
838 | char *ret; | |
839 | ||
840 | if(len == 0) | |
841 | return NULL; | |
842 | ||
843 | /* Assert that the string does not contain NUL-bytes. */ | |
d61f0078 | 844 | TEST_check(OPENSSL_strnlen((const char*)(in), len) == len); |
ce2cdac2 | 845 | ret = OPENSSL_strndup((const char*)(in), len); |
d61f0078 | 846 | TEST_check(ret != NULL); |
ce2cdac2 EK |
847 | return ret; |
848 | } | |
849 | ||
6dc99745 EK |
850 | /* |
851 | * Note that |extra| points to the correct client/server configuration | |
852 | * within |test_ctx|. When configuring the handshake, general mode settings | |
853 | * are taken from |test_ctx|, and client/server-specific settings should be | |
854 | * taken from |extra|. | |
855 | * | |
856 | * The configuration code should never reach into |test_ctx->extra| or | |
857 | * |test_ctx->resume_extra| directly. | |
858 | * | |
859 | * (We could refactor test mode settings into a substructure. This would result | |
860 | * in cleaner argument passing but would complicate the test configuration | |
861 | * parsing.) | |
862 | */ | |
590ed3d7 EK |
863 | static HANDSHAKE_RESULT *do_handshake_internal( |
864 | SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, | |
6dc99745 | 865 | const SSL_TEST_CTX *test_ctx, const SSL_TEST_EXTRA_CONF *extra, |
e0421bd8 | 866 | SSL_SESSION *session_in, SSL_SESSION **session_out) |
453dfd8d | 867 | { |
e0421bd8 | 868 | PEER server, client; |
453dfd8d EK |
869 | BIO *client_to_server, *server_to_client; |
870 | HANDSHAKE_EX_DATA server_ex_data, client_ex_data; | |
ce2cdac2 EK |
871 | CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data; |
872 | HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new(); | |
e0421bd8 EK |
873 | int client_turn = 1; |
874 | connect_phase_t phase = HANDSHAKE; | |
453dfd8d | 875 | handshake_status_t status = HANDSHAKE_RETRY; |
48593cb1 | 876 | const unsigned char* tick = NULL; |
ce2cdac2 | 877 | size_t tick_len = 0; |
5c753de6 | 878 | SSL_SESSION* sess = NULL; |
ce2cdac2 EK |
879 | const unsigned char *proto = NULL; |
880 | /* API dictates unsigned int rather than size_t. */ | |
881 | unsigned int proto_len = 0; | |
453dfd8d | 882 | |
ce2cdac2 EK |
883 | memset(&server_ctx_data, 0, sizeof(server_ctx_data)); |
884 | memset(&server2_ctx_data, 0, sizeof(server2_ctx_data)); | |
885 | memset(&client_ctx_data, 0, sizeof(client_ctx_data)); | |
e0421bd8 EK |
886 | memset(&server, 0, sizeof(server)); |
887 | memset(&client, 0, sizeof(client)); | |
ce2cdac2 | 888 | |
6dc99745 | 889 | configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx, extra, |
ce2cdac2 | 890 | &server_ctx_data, &server2_ctx_data, &client_ctx_data); |
a263f320 | 891 | |
e0421bd8 EK |
892 | /* Setup SSL and buffers; additional configuration happens below. */ |
893 | create_peer(&server, server_ctx); | |
894 | create_peer(&client, client_ctx); | |
453dfd8d | 895 | |
6dc99745 EK |
896 | server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size; |
897 | client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size; | |
e0421bd8 EK |
898 | |
899 | configure_handshake_ssl(server.ssl, client.ssl, extra); | |
590ed3d7 EK |
900 | if (session_in != NULL) { |
901 | /* In case we're testing resumption without tickets. */ | |
d61f0078 | 902 | TEST_check(SSL_CTX_add_session(server_ctx, session_in)); |
e0421bd8 | 903 | TEST_check(SSL_set_session(client.ssl, session_in)); |
590ed3d7 | 904 | } |
5c753de6 | 905 | |
453dfd8d EK |
906 | memset(&server_ex_data, 0, sizeof(server_ex_data)); |
907 | memset(&client_ex_data, 0, sizeof(client_ex_data)); | |
ce2cdac2 EK |
908 | |
909 | ret->result = SSL_TEST_INTERNAL_ERROR; | |
453dfd8d EK |
910 | |
911 | client_to_server = BIO_new(BIO_s_mem()); | |
912 | server_to_client = BIO_new(BIO_s_mem()); | |
913 | ||
e0421bd8 EK |
914 | TEST_check(client_to_server != NULL); |
915 | TEST_check(server_to_client != NULL); | |
453dfd8d EK |
916 | |
917 | /* Non-blocking bio. */ | |
918 | BIO_set_nbio(client_to_server, 1); | |
919 | BIO_set_nbio(server_to_client, 1); | |
920 | ||
e0421bd8 EK |
921 | SSL_set_connect_state(client.ssl); |
922 | SSL_set_accept_state(server.ssl); | |
453dfd8d EK |
923 | |
924 | /* The bios are now owned by the SSL object. */ | |
e0421bd8 | 925 | SSL_set_bio(client.ssl, server_to_client, client_to_server); |
d61f0078 EK |
926 | TEST_check(BIO_up_ref(server_to_client) > 0); |
927 | TEST_check(BIO_up_ref(client_to_server) > 0); | |
e0421bd8 | 928 | SSL_set_bio(server.ssl, client_to_server, server_to_client); |
453dfd8d EK |
929 | |
930 | ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL); | |
d61f0078 | 931 | TEST_check(ex_data_idx >= 0); |
453dfd8d | 932 | |
e0421bd8 EK |
933 | TEST_check(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data) == 1); |
934 | TEST_check(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data) == 1); | |
935 | ||
936 | SSL_set_info_callback(server.ssl, &info_cb); | |
937 | SSL_set_info_callback(client.ssl, &info_cb); | |
453dfd8d | 938 | |
e0421bd8 | 939 | client.status = server.status = PEER_RETRY; |
453dfd8d EK |
940 | |
941 | /* | |
942 | * Half-duplex handshake loop. | |
943 | * Client and server speak to each other synchronously in the same process. | |
944 | * We use non-blocking BIOs, so whenever one peer blocks for read, it | |
945 | * returns PEER_RETRY to indicate that it's the other peer's turn to write. | |
946 | * The handshake succeeds once both peers have succeeded. If one peer | |
947 | * errors out, we also let the other peer retry (and presumably fail). | |
948 | */ | |
949 | for(;;) { | |
950 | if (client_turn) { | |
fe7dd553 | 951 | do_connect_step(test_ctx, &client, phase); |
e0421bd8 | 952 | status = handshake_status(client.status, server.status, |
453dfd8d EK |
953 | 1 /* client went last */); |
954 | } else { | |
fe7dd553 | 955 | do_connect_step(test_ctx, &server, phase); |
e0421bd8 | 956 | status = handshake_status(server.status, client.status, |
453dfd8d EK |
957 | 0 /* server went last */); |
958 | } | |
959 | ||
960 | switch (status) { | |
961 | case HANDSHAKE_SUCCESS: | |
e42c4544 | 962 | phase = next_phase(test_ctx, phase); |
e0421bd8 | 963 | if (phase == CONNECTION_DONE) { |
590ed3d7 EK |
964 | ret->result = SSL_TEST_SUCCESS; |
965 | goto err; | |
966 | } else { | |
e0421bd8 EK |
967 | client.status = server.status = PEER_RETRY; |
968 | /* | |
969 | * For now, client starts each phase. Since each phase is | |
970 | * started separately, we can later control this more | |
971 | * precisely, for example, to test client-initiated and | |
972 | * server-initiated shutdown. | |
973 | */ | |
590ed3d7 EK |
974 | client_turn = 1; |
975 | break; | |
976 | } | |
453dfd8d | 977 | case CLIENT_ERROR: |
ce2cdac2 | 978 | ret->result = SSL_TEST_CLIENT_FAIL; |
453dfd8d EK |
979 | goto err; |
980 | case SERVER_ERROR: | |
ce2cdac2 | 981 | ret->result = SSL_TEST_SERVER_FAIL; |
453dfd8d EK |
982 | goto err; |
983 | case INTERNAL_ERROR: | |
ce2cdac2 | 984 | ret->result = SSL_TEST_INTERNAL_ERROR; |
453dfd8d EK |
985 | goto err; |
986 | case HANDSHAKE_RETRY: | |
987 | /* Continue. */ | |
988 | client_turn ^= 1; | |
989 | break; | |
990 | } | |
991 | } | |
992 | err: | |
ce2cdac2 | 993 | ret->server_alert_sent = server_ex_data.alert_sent; |
dd8e5a57 | 994 | ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent; |
ce2cdac2 EK |
995 | ret->server_alert_received = client_ex_data.alert_received; |
996 | ret->client_alert_sent = client_ex_data.alert_sent; | |
dd8e5a57 | 997 | ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent; |
ce2cdac2 | 998 | ret->client_alert_received = server_ex_data.alert_received; |
e0421bd8 EK |
999 | ret->server_protocol = SSL_version(server.ssl); |
1000 | ret->client_protocol = SSL_version(client.ssl); | |
ce2cdac2 | 1001 | ret->servername = server_ex_data.servername; |
e0421bd8 | 1002 | if ((sess = SSL_get0_session(client.ssl)) != NULL) |
ce2cdac2 EK |
1003 | SSL_SESSION_get0_ticket(sess, &tick, &tick_len); |
1004 | if (tick == NULL || tick_len == 0) | |
1005 | ret->session_ticket = SSL_TEST_SESSION_TICKET_NO; | |
5c753de6 | 1006 | else |
ce2cdac2 EK |
1007 | ret->session_ticket = SSL_TEST_SESSION_TICKET_YES; |
1008 | ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call; | |
1009 | ||
620c6ad3 | 1010 | #ifndef OPENSSL_NO_NEXTPROTONEG |
e0421bd8 | 1011 | SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len); |
ce2cdac2 EK |
1012 | ret->client_npn_negotiated = dup_str(proto, proto_len); |
1013 | ||
e0421bd8 | 1014 | SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len); |
ce2cdac2 | 1015 | ret->server_npn_negotiated = dup_str(proto, proto_len); |
7b7cea6d | 1016 | #endif |
ce2cdac2 | 1017 | |
e0421bd8 | 1018 | SSL_get0_alpn_selected(client.ssl, &proto, &proto_len); |
ce2cdac2 EK |
1019 | ret->client_alpn_negotiated = dup_str(proto, proto_len); |
1020 | ||
e0421bd8 | 1021 | SSL_get0_alpn_selected(server.ssl, &proto, &proto_len); |
ce2cdac2 | 1022 | ret->server_alpn_negotiated = dup_str(proto, proto_len); |
453dfd8d | 1023 | |
e0421bd8 EK |
1024 | ret->client_resumed = SSL_session_reused(client.ssl); |
1025 | ret->server_resumed = SSL_session_reused(server.ssl); | |
590ed3d7 EK |
1026 | |
1027 | if (session_out != NULL) | |
e0421bd8 | 1028 | *session_out = SSL_get1_session(client.ssl); |
590ed3d7 | 1029 | |
ce2cdac2 EK |
1030 | ctx_data_free_data(&server_ctx_data); |
1031 | ctx_data_free_data(&server2_ctx_data); | |
1032 | ctx_data_free_data(&client_ctx_data); | |
590ed3d7 | 1033 | |
e0421bd8 EK |
1034 | peer_free_data(&server); |
1035 | peer_free_data(&client); | |
453dfd8d EK |
1036 | return ret; |
1037 | } | |
590ed3d7 EK |
1038 | |
1039 | HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, | |
1040 | SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx, | |
11279b13 | 1041 | SSL_CTX *resume_client_ctx, |
590ed3d7 EK |
1042 | const SSL_TEST_CTX *test_ctx) |
1043 | { | |
1044 | HANDSHAKE_RESULT *result; | |
1045 | SSL_SESSION *session = NULL; | |
1046 | ||
1047 | result = do_handshake_internal(server_ctx, server2_ctx, client_ctx, | |
6dc99745 | 1048 | test_ctx, &test_ctx->extra, |
e0421bd8 | 1049 | NULL, &session); |
e42c4544 | 1050 | if (test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME) |
590ed3d7 EK |
1051 | goto end; |
1052 | ||
590ed3d7 EK |
1053 | if (result->result != SSL_TEST_SUCCESS) { |
1054 | result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED; | |
e0421bd8 | 1055 | goto end; |
590ed3d7 EK |
1056 | } |
1057 | ||
1058 | HANDSHAKE_RESULT_free(result); | |
1059 | /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */ | |
11279b13 | 1060 | result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx, |
6dc99745 | 1061 | test_ctx, &test_ctx->resume_extra, |
e0421bd8 | 1062 | session, NULL); |
590ed3d7 EK |
1063 | end: |
1064 | SSL_SESSION_free(session); | |
1065 | return result; | |
1066 | } |