2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/bio.h>
13 #include <openssl/x509_vfy.h>
14 #include <openssl/ssl.h>
16 #include "handshake_helper.h"
19 HANDSHAKE_RESULT
*HANDSHAKE_RESULT_new()
21 HANDSHAKE_RESULT
*ret
= OPENSSL_zalloc(sizeof(*ret
));
22 TEST_check(ret
!= NULL
);
26 void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT
*result
)
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
);
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.
42 typedef struct handshake_ex_data_st
{
44 int num_fatal_alerts_sent
;
46 int session_ticket_do_not_call
;
47 ssl_servername_t servername
;
50 typedef struct ctx_data_st
{
51 unsigned char *npn_protocols
;
52 size_t npn_protocols_len
;
53 unsigned char *alpn_protocols
;
54 size_t alpn_protocols_len
;
57 /* |ctx_data| itself is stack-allocated. */
58 static void ctx_data_free_data(CTX_DATA
*ctx_data
)
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
;
66 static int ex_data_idx
;
68 static void info_cb(const SSL
*s
, int where
, int ret
)
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
;
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
++;
79 ex_data
->alert_received
= ret
;
84 /* Select the appropriate server CTX.
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.
90 static int select_server_ctx(SSL
*s
, void *arg
, int ignore
)
92 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
93 HANDSHAKE_EX_DATA
*ex_data
=
94 (HANDSHAKE_EX_DATA
*)(SSL_get_ex_data(s
, ex_data_idx
));
96 if (servername
== NULL
) {
97 ex_data
->servername
= SSL_TEST_SERVERNAME_SERVER1
;
98 return SSL_TLSEXT_ERR_NOACK
;
101 if (strcmp(servername
, "server2") == 0) {
102 SSL_CTX
*new_ctx
= (SSL_CTX
*)arg
;
103 SSL_set_SSL_CTX(s
, new_ctx
);
105 * Copy over all the SSL_CTX options - reasonable behavior
106 * allows testing of cases where the options between two
107 * contexts differ/conflict
109 SSL_clear_options(s
, 0xFFFFFFFFL
);
110 SSL_set_options(s
, SSL_CTX_get_options(new_ctx
));
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
;
118 ex_data
->servername
= SSL_TEST_SERVERNAME_SERVER1
;
119 return SSL_TLSEXT_ERR_NOACK
;
121 /* Don't set an explicit alert, to test library defaults. */
122 return SSL_TLSEXT_ERR_ALERT_FATAL
;
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.
133 * This behaviour is up to the application to configure; we test both
134 * configurations to ensure the state machine propagates the result
137 static int servername_ignore_cb(SSL
*s
, int *ad
, void *arg
)
139 return select_server_ctx(s
, arg
, 1);
142 static int servername_reject_cb(SSL
*s
, int *ad
, void *arg
)
144 return select_server_ctx(s
, arg
, 0);
147 static unsigned char dummy_ocsp_resp_good_val
= 0xff;
148 static unsigned char dummy_ocsp_resp_bad_val
= 0xfe;
150 static int server_ocsp_cb(SSL
*s
, void *arg
)
154 resp
= OPENSSL_malloc(1);
156 return SSL_TLSEXT_ERR_ALERT_FATAL
;
158 * For the purposes of testing we just send back a dummy OCSP response
160 *resp
= *(unsigned char *)arg
;
161 if (!SSL_set_tlsext_status_ocsp_resp(s
, resp
, 1))
162 return SSL_TLSEXT_ERR_ALERT_FATAL
;
164 return SSL_TLSEXT_ERR_OK
;
167 static int client_ocsp_cb(SSL
*s
, void *arg
)
169 const unsigned char *resp
;
172 len
= SSL_get_tlsext_status_ocsp_resp(s
, &resp
);
173 if (len
!= 1 || *resp
!= dummy_ocsp_resp_good_val
)
179 static int verify_reject_cb(X509_STORE_CTX
*ctx
, void *arg
) {
180 X509_STORE_CTX_set_error(ctx
, X509_V_ERR_APPLICATION_VERIFICATION
);
184 static int verify_accept_cb(X509_STORE_CTX
*ctx
, void *arg
) {
188 static int broken_session_ticket_cb(SSL
*s
, unsigned char *key_name
, unsigned char *iv
,
189 EVP_CIPHER_CTX
*ctx
, HMAC_CTX
*hctx
, int enc
)
194 static int do_not_call_session_ticket_cb(SSL
*s
, unsigned char *key_name
,
197 HMAC_CTX
*hctx
, int enc
)
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;
205 /* Parse the comma-separated list into TLS format. */
206 static void parse_protos(const char *protos
, unsigned char **out
, size_t *outlen
)
208 size_t len
, i
, prefix
;
210 len
= strlen(protos
);
212 /* Should never have reuse. */
213 TEST_check(*out
== NULL
);
215 /* Test values are small, so we omit length limit checks. */
216 *out
= OPENSSL_malloc(len
+ 1);
217 TEST_check(*out
!= NULL
);
221 * foo => '3', 'f', 'o', 'o'
222 * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r'
224 memcpy(*out
+ 1, protos
, len
);
229 if ((*out
)[i
] == ',') {
230 TEST_check(i
- 1 - prefix
> 0);
231 (*out
)[prefix
] = i
- 1 - prefix
;
236 TEST_check(len
- prefix
> 0);
237 (*out
)[prefix
] = len
- prefix
;
240 #ifndef OPENSSL_NO_NEXTPROTONEG
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.
247 static int client_npn_cb(SSL
*s
, unsigned char **out
, unsigned char *outlen
,
248 const unsigned char *in
, unsigned int inlen
,
251 CTX_DATA
*ctx_data
= (CTX_DATA
*)(arg
);
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. */
258 TEST_check(ret
== OPENSSL_NPN_NEGOTIATED
|| ret
== OPENSSL_NPN_NO_OVERLAP
);
259 return SSL_TLSEXT_ERR_OK
;
262 static int server_npn_cb(SSL
*s
, const unsigned char **data
,
263 unsigned int *len
, void *arg
)
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
;
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.
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
)
282 CTX_DATA
*ctx_data
= (CTX_DATA
*)(arg
);
285 /* SSL_select_next_proto isn't const-correct... */
286 unsigned char *tmp_out
;
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.
293 ret
= SSL_select_next_proto(&tmp_out
, outlen
,
294 ctx_data
->alpn_protocols
,
295 ctx_data
->alpn_protocols_len
, in
, inlen
);
298 /* Unlike NPN, we don't tolerate a mismatch. */
299 return ret
== OPENSSL_NPN_NEGOTIATED
? SSL_TLSEXT_ERR_OK
300 : SSL_TLSEXT_ERR_NOACK
;
304 * Configure callbacks and other properties that can't be set directly
305 * in the server/client CONF.
307 static void configure_handshake_ctx(SSL_CTX
*server_ctx
, SSL_CTX
*server2_ctx
,
309 const SSL_TEST_CTX
*test
,
310 const SSL_TEST_EXTRA_CONF
*extra
,
311 CTX_DATA
*server_ctx_data
,
312 CTX_DATA
*server2_ctx_data
,
313 CTX_DATA
*client_ctx_data
)
315 unsigned char *ticket_keys
;
316 size_t ticket_key_len
;
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);
324 TEST_check(SSL_CTX_set_max_send_fragment(client_ctx
,
325 test
->max_fragment_size
) == 1);
327 switch (extra
->client
.verify_callback
) {
328 case SSL_TEST_VERIFY_ACCEPT_ALL
:
329 SSL_CTX_set_cert_verify_callback(client_ctx
, &verify_accept_cb
,
332 case SSL_TEST_VERIFY_REJECT_ALL
:
333 SSL_CTX_set_cert_verify_callback(client_ctx
, &verify_reject_cb
,
336 case SSL_TEST_VERIFY_NONE
:
340 /* link the two contexts for SNI purposes */
341 switch (extra
->server
.servername_callback
) {
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
);
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
);
350 case SSL_TEST_SERVERNAME_CB_NONE
:
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
));
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
369 if (server2_ctx
!= NULL
)
370 SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx
,
371 do_not_call_session_ticket_cb
);
373 if (extra
->server
.broken_session_ticket
) {
374 SSL_CTX_set_tlsext_ticket_key_cb(server_ctx
, broken_session_ticket_cb
);
376 #ifndef OPENSSL_NO_NEXTPROTONEG
377 if (extra
->server
.npn_protocols
!= NULL
) {
378 parse_protos(extra
->server
.npn_protocols
,
379 &server_ctx_data
->npn_protocols
,
380 &server_ctx_data
->npn_protocols_len
);
381 SSL_CTX_set_npn_advertised_cb(server_ctx
, server_npn_cb
,
384 if (extra
->server2
.npn_protocols
!= NULL
) {
385 parse_protos(extra
->server2
.npn_protocols
,
386 &server2_ctx_data
->npn_protocols
,
387 &server2_ctx_data
->npn_protocols_len
);
388 TEST_check(server2_ctx
!= NULL
);
389 SSL_CTX_set_npn_advertised_cb(server2_ctx
, server_npn_cb
,
392 if (extra
->client
.npn_protocols
!= NULL
) {
393 parse_protos(extra
->client
.npn_protocols
,
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
,
400 if (extra
->server
.alpn_protocols
!= NULL
) {
401 parse_protos(extra
->server
.alpn_protocols
,
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
);
406 if (extra
->server2
.alpn_protocols
!= NULL
) {
407 TEST_check(server2_ctx
!= NULL
);
408 parse_protos(extra
->server2
.alpn_protocols
,
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
);
413 if (extra
->client
.alpn_protocols
!= NULL
) {
414 unsigned char *alpn_protos
= NULL
;
415 size_t alpn_protos_len
;
416 parse_protos(extra
->client
.alpn_protocols
,
417 &alpn_protos
, &alpn_protos_len
);
418 /* Reversed return value convention... */
419 TEST_check(SSL_CTX_set_alpn_protos(client_ctx
, alpn_protos
,
420 alpn_protos_len
) == 0);
421 OPENSSL_free(alpn_protos
);
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.
428 ticket_key_len
= SSL_CTX_set_tlsext_ticket_keys(server_ctx
, NULL
, 0);
429 ticket_keys
= OPENSSL_zalloc(ticket_key_len
);
430 TEST_check(ticket_keys
!= NULL
);
431 TEST_check(SSL_CTX_set_tlsext_ticket_keys(server_ctx
, ticket_keys
,
432 ticket_key_len
) == 1);
433 OPENSSL_free(ticket_keys
);
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)
437 TEST_check(SSL_CTX_set_default_ctlog_list_file(client_ctx
));
438 switch (extra
->client
.ct_validation
) {
439 case SSL_TEST_CT_VALIDATION_PERMISSIVE
:
440 TEST_check(SSL_CTX_enable_ct(client_ctx
, SSL_CT_VALIDATION_PERMISSIVE
));
442 case SSL_TEST_CT_VALIDATION_STRICT
:
443 TEST_check(SSL_CTX_enable_ct(client_ctx
, SSL_CT_VALIDATION_STRICT
));
445 case SSL_TEST_CT_VALIDATION_NONE
:
451 /* Configure per-SSL callbacks and other properties. */
452 static void configure_handshake_ssl(SSL
*server
, SSL
*client
,
453 const SSL_TEST_EXTRA_CONF
*extra
)
455 if (extra
->client
.servername
!= SSL_TEST_SERVERNAME_NONE
)
456 SSL_set_tlsext_host_name(client
,
457 ssl_servername_name(extra
->client
.servername
));
460 /* The status for each connection phase. */
467 /* An SSL object and associated read-write buffers. */
468 typedef struct peer_st
{
470 /* Buffer lengths are int to match the SSL read/write API. */
471 unsigned char *write_buf
;
473 unsigned char *read_buf
;
477 peer_status_t status
;
480 static void create_peer(PEER
*peer
, SSL_CTX
*ctx
)
482 static const int peer_buffer_size
= 64 * 1024;
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
;
493 static void peer_free_data(PEER
*peer
)
496 OPENSSL_free(peer
->write_buf
);
497 OPENSSL_free(peer
->read_buf
);
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.
504 static void do_handshake_step(PEER
*peer
)
508 TEST_check(peer
->status
== PEER_RETRY
);
509 ret
= SSL_do_handshake(peer
->ssl
);
512 peer
->status
= PEER_SUCCESS
;
513 } else if (ret
== 0) {
514 peer
->status
= PEER_ERROR
;
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
;
524 * Send/receive some application data. The read-write sequence is
525 * Peer A: (R) W - first read will yield no data
532 static void do_app_data_step(PEER
*peer
)
534 int ret
= 1, write_bytes
;
536 TEST_check(peer
->status
== PEER_RETRY
);
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
);
542 TEST_check(ret
<= peer
->bytes_to_read
);
543 peer
->bytes_to_read
-= ret
;
544 } else if (ret
== 0) {
545 peer
->status
= PEER_ERROR
;
548 int error
= SSL_get_error(peer
->ssl
, ret
);
549 if (error
!= SSL_ERROR_WANT_READ
) {
550 peer
->status
= PEER_ERROR
;
552 } /* Else continue with write. */
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
:
560 ret
= SSL_write(peer
->ssl
, peer
->write_buf
, write_bytes
);
562 /* SSL_write will only succeed with a complete write. */
563 TEST_check(ret
== write_bytes
);
564 peer
->bytes_to_write
-= ret
;
567 * We should perhaps check for SSL_ERROR_WANT_READ/WRITE here
568 * but this doesn't yet occur with current app data sizes.
570 peer
->status
= PEER_ERROR
;
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
581 if (!peer
->bytes_to_write
&& !peer
->bytes_to_read
) {
582 peer
->status
= PEER_SUCCESS
;
586 static void do_reneg_setup_step(const SSL_TEST_CTX
*test_ctx
, PEER
*peer
)
591 TEST_check(peer
->status
== PEER_RETRY
);
592 TEST_check(test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RENEG_SERVER
593 || test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RENEG_CLIENT
);
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
))) {
601 * If we already asked for a renegotiation then fall through to the
604 if (!SSL_renegotiate_pending(peer
->ssl
)) {
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
610 if (SSL_is_server(peer
->ssl
)) {
611 ret
= SSL_renegotiate(peer
->ssl
);
613 if (test_ctx
->extra
.client
.reneg_ciphers
!= NULL
) {
614 if (!SSL_set_cipher_list(peer
->ssl
,
615 test_ctx
->extra
.client
.reneg_ciphers
)) {
616 peer
->status
= PEER_ERROR
;
619 ret
= SSL_renegotiate(peer
->ssl
);
621 ret
= SSL_renegotiate_abbreviated(peer
->ssl
);
625 peer
->status
= PEER_ERROR
;
628 do_handshake_step(peer
);
630 * If status is PEER_RETRY it means we're waiting on the peer to
631 * continue the handshake. As far as setting up the renegotiation is
632 * concerned that is a success. The next step will continue the
633 * handshake to its conclusion.
635 * If status is PEER_SUCCESS then we are the server and we have
636 * successfully sent the HelloRequest. We need to continue to wait
637 * until the handshake arrives from the client.
639 if (peer
->status
== PEER_RETRY
)
640 peer
->status
= PEER_SUCCESS
;
641 else if (peer
->status
== PEER_SUCCESS
)
642 peer
->status
= PEER_RETRY
;
648 * The SSL object is still expecting app data, even though it's going to
649 * get a handshake message. We try to read, and it should fail - after which
650 * we should be in a handshake
652 ret
= SSL_read(peer
->ssl
, &buf
, sizeof(buf
));
655 * We're not actually expecting data - we're expecting a reneg to
658 peer
->status
= PEER_ERROR
;
661 int error
= SSL_get_error(peer
->ssl
, ret
);
662 if (error
!= SSL_ERROR_WANT_READ
) {
663 peer
->status
= PEER_ERROR
;
666 /* If we're no in init yet then we're not done with setup yet */
667 if (!SSL_in_init(peer
->ssl
))
671 peer
->status
= PEER_SUCCESS
;
678 * Note that as of TLS 1.1,
679 * failure to properly close a connection no longer requires that a
680 * session not be resumed. This is a change from TLS 1.0 to conform
681 * with widespread implementation practice.
684 * (a) OpenSSL requires that a connection be shutdown for all protocol versions.
685 * (b) We test lower versions, too.
686 * So we just implement shutdown. We do a full bidirectional shutdown so that we
687 * can compare sent and received close_notify alerts and get some test coverage
688 * for SSL_shutdown as a bonus.
690 static void do_shutdown_step(PEER
*peer
)
694 TEST_check(peer
->status
== PEER_RETRY
);
695 ret
= SSL_shutdown(peer
->ssl
);
698 peer
->status
= PEER_SUCCESS
;
699 } else if (ret
< 0) { /* On 0, we retry. */
700 int error
= SSL_get_error(peer
->ssl
, ret
);
701 /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
702 if (error
!= SSL_ERROR_WANT_READ
)
703 peer
->status
= PEER_ERROR
;
709 RENEG_APPLICATION_DATA
,
717 static connect_phase_t
next_phase(const SSL_TEST_CTX
*test_ctx
,
718 connect_phase_t phase
)
722 if (test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RENEG_SERVER
723 || test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RENEG_CLIENT
)
724 return RENEG_APPLICATION_DATA
;
725 return APPLICATION_DATA
;
726 case RENEG_APPLICATION_DATA
:
729 return RENEG_HANDSHAKE
;
730 case RENEG_HANDSHAKE
:
731 return APPLICATION_DATA
;
732 case APPLICATION_DATA
:
735 return CONNECTION_DONE
;
736 case CONNECTION_DONE
:
743 static void do_connect_step(const SSL_TEST_CTX
*test_ctx
, PEER
*peer
,
744 connect_phase_t phase
)
748 do_handshake_step(peer
);
750 case RENEG_APPLICATION_DATA
:
751 do_app_data_step(peer
);
754 do_reneg_setup_step(test_ctx
, peer
);
756 case RENEG_HANDSHAKE
:
757 do_handshake_step(peer
);
759 case APPLICATION_DATA
:
760 do_app_data_step(peer
);
763 do_shutdown_step(peer
);
765 case CONNECTION_DONE
:
772 /* Both parties succeeded. */
774 /* Client errored. */
776 /* Server errored. */
778 /* Peers are in inconsistent state. */
780 /* One or both peers not done. */
782 } handshake_status_t
;
785 * Determine the handshake outcome.
786 * last_status: the status of the peer to have acted last.
787 * previous_status: the status of the peer that didn't act last.
788 * client_spoke_last: 1 if the client went last.
790 static handshake_status_t
handshake_status(peer_status_t last_status
,
791 peer_status_t previous_status
,
792 int client_spoke_last
)
794 switch (last_status
) {
796 switch (previous_status
) {
798 /* Both succeeded. */
799 return HANDSHAKE_SUCCESS
;
801 /* Let the first peer finish. */
802 return HANDSHAKE_RETRY
;
805 * Second peer succeeded despite the fact that the first peer
806 * already errored. This shouldn't happen.
808 return INTERNAL_ERROR
;
812 if (previous_status
== PEER_RETRY
) {
813 /* Neither peer is done. */
814 return HANDSHAKE_RETRY
;
817 * Deadlock: second peer is waiting for more input while first
818 * peer thinks they're done (no more input is coming).
820 return INTERNAL_ERROR
;
823 switch (previous_status
) {
826 * First peer succeeded but second peer errored.
827 * TODO(emilia): we should be able to continue here (with some
828 * application data?) to ensure the first peer receives the
829 * alert / close_notify.
830 * (No tests currently exercise this branch.)
832 return client_spoke_last
? CLIENT_ERROR
: SERVER_ERROR
;
834 /* We errored; let the peer finish. */
835 return HANDSHAKE_RETRY
;
837 /* Both peers errored. Return the one that errored first. */
838 return client_spoke_last
? SERVER_ERROR
: CLIENT_ERROR
;
841 /* Control should never reach here. */
842 return INTERNAL_ERROR
;
845 /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */
846 static char *dup_str(const unsigned char *in
, size_t len
)
853 /* Assert that the string does not contain NUL-bytes. */
854 TEST_check(OPENSSL_strnlen((const char*)(in
), len
) == len
);
855 ret
= OPENSSL_strndup((const char*)(in
), len
);
856 TEST_check(ret
!= NULL
);
860 static int pkey_type(EVP_PKEY
*pkey
)
862 int nid
= EVP_PKEY_id(pkey
);
864 #ifndef OPENSSL_NO_EC
865 if (nid
== EVP_PKEY_EC
) {
866 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
867 return EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
873 static int peer_pkey_type(SSL
*s
)
875 X509
*x
= SSL_get_peer_certificate(s
);
878 int nid
= pkey_type(X509_get0_pubkey(x
));
887 * Note that |extra| points to the correct client/server configuration
888 * within |test_ctx|. When configuring the handshake, general mode settings
889 * are taken from |test_ctx|, and client/server-specific settings should be
890 * taken from |extra|.
892 * The configuration code should never reach into |test_ctx->extra| or
893 * |test_ctx->resume_extra| directly.
895 * (We could refactor test mode settings into a substructure. This would result
896 * in cleaner argument passing but would complicate the test configuration
899 static HANDSHAKE_RESULT
*do_handshake_internal(
900 SSL_CTX
*server_ctx
, SSL_CTX
*server2_ctx
, SSL_CTX
*client_ctx
,
901 const SSL_TEST_CTX
*test_ctx
, const SSL_TEST_EXTRA_CONF
*extra
,
902 SSL_SESSION
*session_in
, SSL_SESSION
**session_out
)
905 BIO
*client_to_server
, *server_to_client
;
906 HANDSHAKE_EX_DATA server_ex_data
, client_ex_data
;
907 CTX_DATA client_ctx_data
, server_ctx_data
, server2_ctx_data
;
908 HANDSHAKE_RESULT
*ret
= HANDSHAKE_RESULT_new();
909 int client_turn
= 1, client_turn_count
= 0;
910 connect_phase_t phase
= HANDSHAKE
;
911 handshake_status_t status
= HANDSHAKE_RETRY
;
912 const unsigned char* tick
= NULL
;
914 SSL_SESSION
* sess
= NULL
;
915 const unsigned char *proto
= NULL
;
916 /* API dictates unsigned int rather than size_t. */
917 unsigned int proto_len
= 0;
920 memset(&server_ctx_data
, 0, sizeof(server_ctx_data
));
921 memset(&server2_ctx_data
, 0, sizeof(server2_ctx_data
));
922 memset(&client_ctx_data
, 0, sizeof(client_ctx_data
));
923 memset(&server
, 0, sizeof(server
));
924 memset(&client
, 0, sizeof(client
));
926 configure_handshake_ctx(server_ctx
, server2_ctx
, client_ctx
, test_ctx
, extra
,
927 &server_ctx_data
, &server2_ctx_data
, &client_ctx_data
);
929 /* Setup SSL and buffers; additional configuration happens below. */
930 create_peer(&server
, server_ctx
);
931 create_peer(&client
, client_ctx
);
933 server
.bytes_to_write
= client
.bytes_to_read
= test_ctx
->app_data_size
;
934 client
.bytes_to_write
= server
.bytes_to_read
= test_ctx
->app_data_size
;
936 configure_handshake_ssl(server
.ssl
, client
.ssl
, extra
);
937 if (session_in
!= NULL
) {
938 /* In case we're testing resumption without tickets. */
939 TEST_check(SSL_CTX_add_session(server_ctx
, session_in
));
940 TEST_check(SSL_set_session(client
.ssl
, session_in
));
943 memset(&server_ex_data
, 0, sizeof(server_ex_data
));
944 memset(&client_ex_data
, 0, sizeof(client_ex_data
));
946 ret
->result
= SSL_TEST_INTERNAL_ERROR
;
948 client_to_server
= BIO_new(BIO_s_mem());
949 server_to_client
= BIO_new(BIO_s_mem());
951 TEST_check(client_to_server
!= NULL
);
952 TEST_check(server_to_client
!= NULL
);
954 /* Non-blocking bio. */
955 BIO_set_nbio(client_to_server
, 1);
956 BIO_set_nbio(server_to_client
, 1);
958 SSL_set_connect_state(client
.ssl
);
959 SSL_set_accept_state(server
.ssl
);
961 /* The bios are now owned by the SSL object. */
962 SSL_set_bio(client
.ssl
, server_to_client
, client_to_server
);
963 TEST_check(BIO_up_ref(server_to_client
) > 0);
964 TEST_check(BIO_up_ref(client_to_server
) > 0);
965 SSL_set_bio(server
.ssl
, client_to_server
, server_to_client
);
967 ex_data_idx
= SSL_get_ex_new_index(0, "ex data", NULL
, NULL
, NULL
);
968 TEST_check(ex_data_idx
>= 0);
970 TEST_check(SSL_set_ex_data(server
.ssl
, ex_data_idx
, &server_ex_data
) == 1);
971 TEST_check(SSL_set_ex_data(client
.ssl
, ex_data_idx
, &client_ex_data
) == 1);
973 SSL_set_info_callback(server
.ssl
, &info_cb
);
974 SSL_set_info_callback(client
.ssl
, &info_cb
);
976 client
.status
= server
.status
= PEER_RETRY
;
979 * Half-duplex handshake loop.
980 * Client and server speak to each other synchronously in the same process.
981 * We use non-blocking BIOs, so whenever one peer blocks for read, it
982 * returns PEER_RETRY to indicate that it's the other peer's turn to write.
983 * The handshake succeeds once both peers have succeeded. If one peer
984 * errors out, we also let the other peer retry (and presumably fail).
988 do_connect_step(test_ctx
, &client
, phase
);
989 status
= handshake_status(client
.status
, server
.status
,
990 1 /* client went last */);
992 do_connect_step(test_ctx
, &server
, phase
);
993 status
= handshake_status(server
.status
, client
.status
,
994 0 /* server went last */);
998 case HANDSHAKE_SUCCESS
:
999 client_turn_count
= 0;
1000 phase
= next_phase(test_ctx
, phase
);
1001 if (phase
== CONNECTION_DONE
) {
1002 ret
->result
= SSL_TEST_SUCCESS
;
1005 client
.status
= server
.status
= PEER_RETRY
;
1007 * For now, client starts each phase. Since each phase is
1008 * started separately, we can later control this more
1009 * precisely, for example, to test client-initiated and
1010 * server-initiated shutdown.
1016 ret
->result
= SSL_TEST_CLIENT_FAIL
;
1019 ret
->result
= SSL_TEST_SERVER_FAIL
;
1021 case INTERNAL_ERROR
:
1022 ret
->result
= SSL_TEST_INTERNAL_ERROR
;
1024 case HANDSHAKE_RETRY
:
1025 if (client_turn_count
++ >= 2000) {
1027 * At this point, there's been so many PEER_RETRY in a row
1028 * that it's likely both sides are stuck waiting for a read.
1029 * It's time to give up.
1031 ret
->result
= SSL_TEST_INTERNAL_ERROR
;
1041 ret
->server_alert_sent
= server_ex_data
.alert_sent
;
1042 ret
->server_num_fatal_alerts_sent
= server_ex_data
.num_fatal_alerts_sent
;
1043 ret
->server_alert_received
= client_ex_data
.alert_received
;
1044 ret
->client_alert_sent
= client_ex_data
.alert_sent
;
1045 ret
->client_num_fatal_alerts_sent
= client_ex_data
.num_fatal_alerts_sent
;
1046 ret
->client_alert_received
= server_ex_data
.alert_received
;
1047 ret
->server_protocol
= SSL_version(server
.ssl
);
1048 ret
->client_protocol
= SSL_version(client
.ssl
);
1049 ret
->servername
= server_ex_data
.servername
;
1050 if ((sess
= SSL_get0_session(client
.ssl
)) != NULL
)
1051 SSL_SESSION_get0_ticket(sess
, &tick
, &tick_len
);
1052 if (tick
== NULL
|| tick_len
== 0)
1053 ret
->session_ticket
= SSL_TEST_SESSION_TICKET_NO
;
1055 ret
->session_ticket
= SSL_TEST_SESSION_TICKET_YES
;
1056 ret
->session_ticket_do_not_call
= server_ex_data
.session_ticket_do_not_call
;
1058 #ifndef OPENSSL_NO_NEXTPROTONEG
1059 SSL_get0_next_proto_negotiated(client
.ssl
, &proto
, &proto_len
);
1060 ret
->client_npn_negotiated
= dup_str(proto
, proto_len
);
1062 SSL_get0_next_proto_negotiated(server
.ssl
, &proto
, &proto_len
);
1063 ret
->server_npn_negotiated
= dup_str(proto
, proto_len
);
1066 SSL_get0_alpn_selected(client
.ssl
, &proto
, &proto_len
);
1067 ret
->client_alpn_negotiated
= dup_str(proto
, proto_len
);
1069 SSL_get0_alpn_selected(server
.ssl
, &proto
, &proto_len
);
1070 ret
->server_alpn_negotiated
= dup_str(proto
, proto_len
);
1072 ret
->client_resumed
= SSL_session_reused(client
.ssl
);
1073 ret
->server_resumed
= SSL_session_reused(server
.ssl
);
1075 if (session_out
!= NULL
)
1076 *session_out
= SSL_get1_session(client
.ssl
);
1078 if (SSL_get_server_tmp_key(client
.ssl
, &tmp_key
)) {
1079 ret
->tmp_key_type
= pkey_type(tmp_key
);
1080 EVP_PKEY_free(tmp_key
);
1083 SSL_get_peer_signature_nid(client
.ssl
, &ret
->server_sign_hash
);
1084 SSL_get_peer_signature_nid(server
.ssl
, &ret
->client_sign_hash
);
1086 SSL_get_peer_signature_type_nid(client
.ssl
, &ret
->server_sign_type
);
1087 SSL_get_peer_signature_type_nid(server
.ssl
, &ret
->client_sign_type
);
1089 ret
->server_cert_type
= peer_pkey_type(client
.ssl
);
1090 ret
->client_cert_type
= peer_pkey_type(server
.ssl
);
1092 ctx_data_free_data(&server_ctx_data
);
1093 ctx_data_free_data(&server2_ctx_data
);
1094 ctx_data_free_data(&client_ctx_data
);
1096 peer_free_data(&server
);
1097 peer_free_data(&client
);
1101 HANDSHAKE_RESULT
*do_handshake(SSL_CTX
*server_ctx
, SSL_CTX
*server2_ctx
,
1102 SSL_CTX
*client_ctx
, SSL_CTX
*resume_server_ctx
,
1103 SSL_CTX
*resume_client_ctx
,
1104 const SSL_TEST_CTX
*test_ctx
)
1106 HANDSHAKE_RESULT
*result
;
1107 SSL_SESSION
*session
= NULL
;
1109 result
= do_handshake_internal(server_ctx
, server2_ctx
, client_ctx
,
1110 test_ctx
, &test_ctx
->extra
,
1112 if (test_ctx
->handshake_mode
!= SSL_TEST_HANDSHAKE_RESUME
)
1115 if (result
->result
!= SSL_TEST_SUCCESS
) {
1116 result
->result
= SSL_TEST_FIRST_HANDSHAKE_FAILED
;
1120 HANDSHAKE_RESULT_free(result
);
1121 /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
1122 result
= do_handshake_internal(resume_server_ctx
, NULL
, resume_client_ctx
,
1123 test_ctx
, &test_ctx
->resume_extra
,
1126 SSL_SESSION_free(session
);