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/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/ssl.h>
16 #include <openssl/ocsp.h>
18 #include "ssltestlib.h"
20 #include "test_main_custom.h"
23 static char *cert
= NULL
;
24 static char *privkey
= NULL
;
26 #define LOG_BUFFER_SIZE 1024
27 static char server_log_buffer
[LOG_BUFFER_SIZE
+ 1] = {0};
28 static int server_log_buffer_index
= 0;
29 static char client_log_buffer
[LOG_BUFFER_SIZE
+ 1] = {0};
30 static int client_log_buffer_index
= 0;
31 static int error_writing_log
= 0;
33 #ifndef OPENSSL_NO_OCSP
34 static const unsigned char orespder
[] = "Dummy OCSP Response";
35 static int ocsp_server_called
= 0;
36 static int ocsp_client_called
= 0;
38 static int cdummyarg
= 1;
39 static X509
*ocspcert
= NULL
;
42 #define NUM_EXTRA_CERTS 40
45 * This structure is used to validate that the correct number of log messages
46 * of various types are emitted when emitting secret logs.
48 struct sslapitest_log_counts
{
49 unsigned int rsa_key_exchange_count
;
50 unsigned int master_secret_count
;
51 unsigned int client_handshake_secret_count
;
52 unsigned int server_handshake_secret_count
;
53 unsigned int client_application_secret_count
;
54 unsigned int server_application_secret_count
;
57 static void client_keylog_callback(const SSL
*ssl
, const char *line
) {
58 int line_length
= strlen(line
);
60 /* If the log doesn't fit, error out. */
61 if ((client_log_buffer_index
+ line_length
) > LOG_BUFFER_SIZE
) {
62 printf("No room in client log\n");
63 error_writing_log
= 1;
67 strcat(client_log_buffer
, line
);
68 client_log_buffer_index
+= line_length
;
69 client_log_buffer
[client_log_buffer_index
] = '\n';
70 client_log_buffer_index
+= 1;
75 static void server_keylog_callback(const SSL
*ssl
, const char *line
) {
76 int line_length
= strlen(line
);
78 /* If the log doesn't fit, error out. */
79 if ((server_log_buffer_index
+ line_length
) > LOG_BUFFER_SIZE
) {
80 printf("No room in server log\n");
81 error_writing_log
= 1;
85 strcat(server_log_buffer
, line
);
86 server_log_buffer_index
+= line_length
;
87 server_log_buffer
[server_log_buffer_index
] = '\n';
88 server_log_buffer_index
+= 1;
93 static int compare_hex_encoded_buffer(const char *hex_encoded
,
100 /* One byte too big, just to be safe. */
103 if ((raw_length
* 2) != hex_length
) {
104 printf("Inconsistent hex encoded lengths.\n");
108 for (i
= j
= 0; (i
< raw_length
) && ((j
+ 1) < hex_length
); i
++) {
109 sprintf(hexed
, "%02x", raw
[i
]);
110 if ((hexed
[0] != hex_encoded
[j
]) || (hexed
[1] != hex_encoded
[j
+ 1])) {
111 printf("Hex output does not match.\n");
120 static int test_keylog_output(char *buffer
, const SSL
*ssl
,
121 const SSL_SESSION
*session
,
122 struct sslapitest_log_counts
*expected
) {
124 unsigned char actual_client_random
[SSL3_RANDOM_SIZE
] = {0};
125 size_t client_random_size
= SSL3_RANDOM_SIZE
;
126 unsigned char actual_master_key
[SSL_MAX_MASTER_KEY_LENGTH
] = {0};
127 size_t master_key_size
= SSL_MAX_MASTER_KEY_LENGTH
;
128 unsigned int rsa_key_exchange_count
= 0;
129 unsigned int master_secret_count
= 0;
130 unsigned int client_handshake_secret_count
= 0;
131 unsigned int server_handshake_secret_count
= 0;
132 unsigned int client_application_secret_count
= 0;
133 unsigned int server_application_secret_count
= 0;
135 token
= strtok(buffer
, " \n");
137 if (strcmp(token
, "RSA") == 0) {
139 * Premaster secret. Tokens should be: 16 ASCII bytes of
140 * hex-encoded encrypted secret, then the hex-encoded pre-master
143 token
= strtok(NULL
, " \n");
145 printf("Unexpectedly short premaster secret log.\n");
148 if (strlen(token
) != 16) {
149 printf("Bad value for encrypted secret: %s\n", token
);
152 token
= strtok(NULL
, " \n");
154 printf("Unexpectedly short premaster secret log.\n");
158 * We can't sensibly check the log because the premaster secret is
159 * transient, and OpenSSL doesn't keep hold of it once the master
160 * secret is generated.
162 rsa_key_exchange_count
++;
163 } else if (strcmp(token
, "CLIENT_RANDOM") == 0) {
165 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
166 * client random, then the hex-encoded master secret.
168 client_random_size
= SSL_get_client_random(ssl
,
169 actual_client_random
,
171 if (client_random_size
!= SSL3_RANDOM_SIZE
) {
172 printf("Unexpected short client random.\n");
176 token
= strtok(NULL
, " \n");
178 printf("Unexpected short master secret log.\n");
181 if (strlen(token
) != 64) {
182 printf("Bad value for client random: %s\n", token
);
185 if (compare_hex_encoded_buffer(token
, 64, actual_client_random
,
186 client_random_size
)) {
187 printf("Bad value for client random: %s\n", token
);
191 token
= strtok(NULL
, " \n");
193 printf("Unexpectedly short master secret log.\n");
197 master_key_size
= SSL_SESSION_get_master_key(session
,
200 if (!master_key_size
) {
201 printf("Error getting master key to compare.\n");
204 if (compare_hex_encoded_buffer(token
, strlen(token
),
207 printf("Bad value for master key: %s\n", token
);
211 master_secret_count
++;
212 } else if ((strcmp(token
, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0) ||
213 (strcmp(token
, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0) ||
214 (strcmp(token
, "CLIENT_TRAFFIC_SECRET_0") == 0) ||
215 (strcmp(token
, "SERVER_TRAFFIC_SECRET_0") == 0)) {
217 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
218 * client random, and then the hex-encoded secret. In this case,
219 * we treat all of these secrets identically and then just
220 * distinguish between them when counting what we saw.
222 if (strcmp(token
, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
223 client_handshake_secret_count
++;
224 else if (strcmp(token
, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
225 server_handshake_secret_count
++;
226 else if (strcmp(token
, "CLIENT_TRAFFIC_SECRET_0") == 0)
227 client_application_secret_count
++;
228 else if (strcmp(token
, "SERVER_TRAFFIC_SECRET_0") == 0)
229 server_application_secret_count
++;
231 client_random_size
= SSL_get_client_random(ssl
,
232 actual_client_random
,
234 if (client_random_size
!= SSL3_RANDOM_SIZE
) {
235 printf("Unexpected short client random.\n");
239 token
= strtok(NULL
, " \n");
241 printf("Unexpected short client handshake secret log.\n");
244 if (strlen(token
) != 64) {
245 printf("Bad value for client random: %s\n", token
);
248 if (compare_hex_encoded_buffer(token
, 64, actual_client_random
,
249 client_random_size
)) {
250 printf("Bad value for client random: %s\n", token
);
254 token
= strtok(NULL
, " \n");
256 printf("Unexpectedly short master secret log.\n");
261 * TODO(TLS1.3): test that application traffic secrets are what
264 printf("Unexpected token in buffer: %s\n", token
);
268 token
= strtok(NULL
, " \n");
271 /* Return whether we got what we expected. */
272 return ((rsa_key_exchange_count
== expected
->rsa_key_exchange_count
) &&
273 (master_secret_count
== expected
->master_secret_count
) &&
274 (client_handshake_secret_count
== expected
->client_handshake_secret_count
) &&
275 (server_handshake_secret_count
== expected
->server_handshake_secret_count
) &&
276 (client_application_secret_count
== expected
->client_application_secret_count
) &&
277 (server_application_secret_count
== expected
->server_application_secret_count
));
280 static int test_keylog(void) {
281 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
282 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
285 struct sslapitest_log_counts expected
= {0};
287 /* Clean up logging space */
288 memset(client_log_buffer
, 0, LOG_BUFFER_SIZE
+ 1);
289 memset(server_log_buffer
, 0, LOG_BUFFER_SIZE
+ 1);
290 client_log_buffer_index
= 0;
291 server_log_buffer_index
= 0;
292 error_writing_log
= 0;
294 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
295 &cctx
, cert
, privkey
)) {
296 printf("Unable to create SSL_CTX pair\n");
300 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
301 SSL_CTX_set_options(cctx
, SSL_OP_NO_TLSv1_3
);
302 SSL_CTX_set_options(sctx
, SSL_OP_NO_TLSv1_3
);
304 /* We also want to ensure that we use RSA-based key exchange. */
305 rc
= SSL_CTX_set_cipher_list(cctx
, "RSA");
307 printf("Unable to restrict to RSA key exchange.\n");
311 if (SSL_CTX_get_keylog_callback(cctx
)) {
312 printf("Unexpected initial value for client "
313 "SSL_CTX_get_keylog_callback()\n");
316 if (SSL_CTX_get_keylog_callback(sctx
)) {
317 printf("Unexpected initial value for server "
318 "SSL_CTX_get_keylog_callback()\n");
322 SSL_CTX_set_keylog_callback(cctx
, client_keylog_callback
);
323 SSL_CTX_set_keylog_callback(sctx
, server_keylog_callback
);
325 if (SSL_CTX_get_keylog_callback(cctx
) != client_keylog_callback
) {
326 printf("Unexpected set value for client "
327 "SSL_CTX_get_keylog_callback()\n");
330 if (SSL_CTX_get_keylog_callback(sctx
) != server_keylog_callback
) {
331 printf("Unexpected set value for server "
332 "SSL_CTX_get_keylog_callback()\n");
335 /* Now do a handshake and check that the logs have been written to. */
336 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
337 printf("Unable to create SSL objects\n");
341 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
342 printf("Unable to create SSL connection\n");
346 if (error_writing_log
) {
347 printf("Error encountered while logging\n");
351 if ((client_log_buffer_index
== 0) || (server_log_buffer_index
== 0)) {
352 printf("No logs written\n");
357 * Now we want to test that our output data was vaguely sensible. We
358 * do that by using strtok and confirming that we have more or less the
359 * data we expect. For both client and server, we expect to see one master
360 * secret. The client should also see a RSA key exchange.
362 expected
.rsa_key_exchange_count
= 1;
363 expected
.master_secret_count
= 1;
364 if (!test_keylog_output(client_log_buffer
, clientssl
,
365 SSL_get_session(clientssl
), &expected
)) {
366 printf("Error encountered in client log buffer\n");
370 expected
.rsa_key_exchange_count
= 0;
371 if (!test_keylog_output(server_log_buffer
, serverssl
,
372 SSL_get_session(serverssl
), &expected
)) {
373 printf("Error encountered in server log buffer\n");
388 #ifndef OPENSSL_NO_TLS1_3
389 static int test_keylog_no_master_key(void) {
390 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
391 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
393 struct sslapitest_log_counts expected
= {0};
395 /* Clean up logging space */
396 memset(client_log_buffer
, 0, LOG_BUFFER_SIZE
+ 1);
397 memset(server_log_buffer
, 0, LOG_BUFFER_SIZE
+ 1);
398 client_log_buffer_index
= 0;
399 server_log_buffer_index
= 0;
400 error_writing_log
= 0;
402 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
403 &cctx
, cert
, privkey
)) {
404 printf("Unable to create SSL_CTX pair\n");
408 if (SSL_CTX_get_keylog_callback(cctx
)) {
409 printf("Unexpected initial value for client "
410 "SSL_CTX_get_keylog_callback()\n");
413 if (SSL_CTX_get_keylog_callback(sctx
)) {
414 printf("Unexpected initial value for server "
415 "SSL_CTX_get_keylog_callback()\n");
419 SSL_CTX_set_keylog_callback(cctx
, client_keylog_callback
);
420 SSL_CTX_set_keylog_callback(sctx
, server_keylog_callback
);
422 if (SSL_CTX_get_keylog_callback(cctx
) != client_keylog_callback
) {
423 printf("Unexpected set value for client "
424 "SSL_CTX_get_keylog_callback()\n");
427 if (SSL_CTX_get_keylog_callback(sctx
) != server_keylog_callback
) {
428 printf("Unexpected set value for server "
429 "SSL_CTX_get_keylog_callback()\n");
432 /* Now do a handshake and check that the logs have been written to. */
433 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
434 printf("Unable to create SSL objects\n");
438 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
439 printf("Unable to create SSL connection\n");
443 if (error_writing_log
) {
444 printf("Error encountered while logging\n");
449 * Now we want to test that our output data was vaguely sensible. For this
450 * test, we expect no CLIENT_RANDOM entry becuase it doesn't make sense for
451 * TLSv1.3, but we do expect both client and server to emit keys.
453 expected
.client_handshake_secret_count
= 1;
454 expected
.server_handshake_secret_count
= 1;
455 expected
.client_application_secret_count
= 1;
456 expected
.server_application_secret_count
= 1;
457 if (!test_keylog_output(client_log_buffer
, clientssl
,
458 SSL_get_session(clientssl
), &expected
)) {
459 printf("Error encountered in client log buffer\n");
462 if (!test_keylog_output(server_log_buffer
, serverssl
,
463 SSL_get_session(serverssl
), &expected
)) {
464 printf("Error encountered in server log buffer\n");
480 #ifndef OPENSSL_NO_TLS1_2
481 static int full_early_callback(SSL
*s
, int *al
, void *arg
)
484 const unsigned char *p
;
485 /* We only configure two ciphers, but the SCSV is added automatically. */
487 const unsigned char expected_ciphers
[] = {0x00, 0x9d, 0x00, 0xff};
489 const unsigned char expected_ciphers
[] = {0x00, 0x9d, 0xc0,
494 /* Make sure we can defer processing and get called back. */
498 len
= SSL_early_get0_ciphers(s
, &p
);
499 if (len
!= sizeof(expected_ciphers
) ||
500 memcmp(p
, expected_ciphers
, len
) != 0) {
501 printf("Early callback expected ciphers mismatch\n");
504 len
= SSL_early_get0_compression_methods(s
, &p
);
505 if (len
!= 1 || *p
!= 0) {
506 printf("Early callback expected comperssion methods mismatch\n");
512 static int test_early_cb(void) {
513 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
514 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
515 int testctr
= 0, testresult
= 0;
517 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
518 &cctx
, cert
, privkey
)) {
519 printf("Unable to create SSL_CTX pair\n");
523 SSL_CTX_set_early_cb(sctx
, full_early_callback
, &testctr
);
524 /* The gimpy cipher list we configure can't do TLS 1.3. */
525 SSL_CTX_set_max_proto_version(cctx
, TLS1_2_VERSION
);
526 if (!SSL_CTX_set_cipher_list(cctx
,
527 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) {
528 printf("Failed to set cipher list\n");
532 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
533 printf("Unable to create SSL objects\n");
537 if (create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_WANT_EARLY
)) {
538 printf("Creating SSL connection succeeded with async early return\n");
542 /* Passing a -1 literal is a hack since the real value was lost. */
543 if (SSL_get_error(serverssl
, -1) != SSL_ERROR_WANT_EARLY
) {
544 printf("Early callback failed to make state SSL_ERROR_WANT_EARLY\n");
548 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
549 printf("Restarting SSL connection failed\n");
565 static int execute_test_large_message(const SSL_METHOD
*smeth
,
566 const SSL_METHOD
*cmeth
, int read_ahead
)
568 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
569 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
572 BIO
*certbio
= BIO_new_file(cert
, "r");
573 X509
*chaincert
= NULL
;
576 if (certbio
== NULL
) {
577 printf("Can't load the certficate file\n");
580 chaincert
= PEM_read_bio_X509(certbio
, NULL
, NULL
, NULL
);
583 if (chaincert
== NULL
) {
584 printf("Unable to load certificate for chain\n");
588 if (!create_ssl_ctx_pair(smeth
, cmeth
, &sctx
,
589 &cctx
, cert
, privkey
)) {
590 printf("Unable to create SSL_CTX pair\n");
596 * Test that read_ahead works correctly when dealing with large
599 SSL_CTX_set_read_ahead(cctx
, 1);
603 * We assume the supplied certificate is big enough so that if we add
604 * NUM_EXTRA_CERTS it will make the overall message large enough. The
605 * default buffer size is requested to be 16k, but due to the way BUF_MEM
606 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
607 * we need to have a message larger than that.
609 certlen
= i2d_X509(chaincert
, NULL
);
610 OPENSSL_assert((certlen
* NUM_EXTRA_CERTS
)
611 > ((SSL3_RT_MAX_PLAIN_LENGTH
* 4) / 3));
612 for (i
= 0; i
< NUM_EXTRA_CERTS
; i
++) {
613 if (!X509_up_ref(chaincert
)) {
614 printf("Unable to up ref cert\n");
617 if (!SSL_CTX_add_extra_chain_cert(sctx
, chaincert
)) {
618 printf("Unable to add extra chain cert %d\n", i
);
619 X509_free(chaincert
);
624 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
625 printf("Unable to create SSL objects\n");
629 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
630 printf("Unable to create SSL connection\n");
635 * Calling SSL_clear() first is not required but this tests that SSL_clear()
636 * doesn't leak (when using enable-crypto-mdebug).
638 if (!SSL_clear(serverssl
)) {
639 printf("Unexpected failure from SSL_clear()\n");
645 X509_free(chaincert
);
654 static int test_large_message_tls(void)
656 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
660 static int test_large_message_tls_read_ahead(void)
662 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
666 #ifndef OPENSSL_NO_DTLS
667 static int test_large_message_dtls(void)
670 * read_ahead is not relevant to DTLS because DTLS always acts as if
673 return execute_test_large_message(DTLS_server_method(),
674 DTLS_client_method(), 0);
678 #ifndef OPENSSL_NO_OCSP
679 static int ocsp_server_cb(SSL
*s
, void *arg
)
681 int *argi
= (int *)arg
;
682 unsigned char *orespdercopy
= NULL
;
683 STACK_OF(OCSP_RESPID
) *ids
= NULL
;
684 OCSP_RESPID
*id
= NULL
;
687 /* In this test we are expecting exactly 1 OCSP_RESPID */
688 SSL_get_tlsext_status_ids(s
, &ids
);
689 if (ids
== NULL
|| sk_OCSP_RESPID_num(ids
) != 1)
690 return SSL_TLSEXT_ERR_ALERT_FATAL
;
692 id
= sk_OCSP_RESPID_value(ids
, 0);
693 if (id
== NULL
|| !OCSP_RESPID_match(id
, ocspcert
))
694 return SSL_TLSEXT_ERR_ALERT_FATAL
;
695 } else if (*argi
!= 1) {
696 return SSL_TLSEXT_ERR_ALERT_FATAL
;
700 orespdercopy
= OPENSSL_memdup(orespder
, sizeof(orespder
));
701 if (orespdercopy
== NULL
)
702 return SSL_TLSEXT_ERR_ALERT_FATAL
;
704 SSL_set_tlsext_status_ocsp_resp(s
, orespdercopy
, sizeof(orespder
));
706 ocsp_server_called
= 1;
708 return SSL_TLSEXT_ERR_OK
;
711 static int ocsp_client_cb(SSL
*s
, void *arg
)
713 int *argi
= (int *)arg
;
714 const unsigned char *respderin
;
717 if (*argi
!= 1 && *argi
!= 2)
720 len
= SSL_get_tlsext_status_ocsp_resp(s
, &respderin
);
722 if (memcmp(orespder
, respderin
, len
) != 0)
725 ocsp_client_called
= 1;
730 static int test_tlsext_status_type(void)
732 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
733 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
735 STACK_OF(OCSP_RESPID
) *ids
= NULL
;
736 OCSP_RESPID
*id
= NULL
;
739 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
740 &cctx
, cert
, privkey
)) {
741 printf("Unable to create SSL_CTX pair\n");
745 if (SSL_CTX_get_tlsext_status_type(cctx
) != -1) {
746 printf("Unexpected initial value for "
747 "SSL_CTX_get_tlsext_status_type()\n");
751 /* First just do various checks getting and setting tlsext_status_type */
753 clientssl
= SSL_new(cctx
);
754 if (SSL_get_tlsext_status_type(clientssl
) != -1) {
755 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
759 if (!SSL_set_tlsext_status_type(clientssl
, TLSEXT_STATUSTYPE_ocsp
)) {
760 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
764 if (SSL_get_tlsext_status_type(clientssl
) != TLSEXT_STATUSTYPE_ocsp
) {
765 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
772 if (!SSL_CTX_set_tlsext_status_type(cctx
, TLSEXT_STATUSTYPE_ocsp
)) {
773 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
777 if (SSL_CTX_get_tlsext_status_type(cctx
) != TLSEXT_STATUSTYPE_ocsp
) {
778 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
782 clientssl
= SSL_new(cctx
);
784 if (SSL_get_tlsext_status_type(clientssl
) != TLSEXT_STATUSTYPE_ocsp
) {
785 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
793 * Now actually do a handshake and check OCSP information is exchanged and
794 * the callbacks get called
797 SSL_CTX_set_tlsext_status_cb(cctx
, ocsp_client_cb
);
798 SSL_CTX_set_tlsext_status_arg(cctx
, &cdummyarg
);
799 SSL_CTX_set_tlsext_status_cb(sctx
, ocsp_server_cb
);
800 SSL_CTX_set_tlsext_status_arg(sctx
, &cdummyarg
);
802 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
803 printf("Unable to create SSL objects\n");
807 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
808 printf("Unable to create SSL connection\n");
812 if (!ocsp_client_called
|| !ocsp_server_called
) {
813 printf("OCSP callbacks not called\n");
822 /* Try again but this time force the server side callback to fail */
823 ocsp_client_called
= 0;
824 ocsp_server_called
= 0;
827 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
828 printf("Unable to create SSL objects\n");
832 /* This should fail because the callback will fail */
833 if (create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
834 printf("Unexpected success creating the connection\n");
838 if (ocsp_client_called
|| ocsp_server_called
) {
839 printf("OCSP callbacks successfully called unexpectedly\n");
849 * This time we'll get the client to send an OCSP_RESPID that it will
852 ocsp_client_called
= 0;
853 ocsp_server_called
= 0;
856 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
857 printf("Unable to create SSL objects\n");
862 * We'll just use any old cert for this test - it doesn't have to be an OCSP
863 * specifc one. We'll use the server cert.
865 certbio
= BIO_new_file(cert
, "r");
866 if (certbio
== NULL
) {
867 printf("Can't load the certficate file\n");
870 id
= OCSP_RESPID_new();
871 ids
= sk_OCSP_RESPID_new_null();
872 ocspcert
= PEM_read_bio_X509(certbio
, NULL
, NULL
, NULL
);
873 if (id
== NULL
|| ids
== NULL
|| ocspcert
== NULL
874 || !OCSP_RESPID_set_by_key(id
, ocspcert
)
875 || !sk_OCSP_RESPID_push(ids
, id
)) {
876 printf("Unable to set OCSP_RESPIDs\n");
880 SSL_set_tlsext_status_ids(clientssl
, ids
);
881 /* Control has been transferred */
887 if (!create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
888 printf("Unable to create SSL connection\n");
892 if (!ocsp_client_called
|| !ocsp_server_called
) {
893 printf("OCSP callbacks not called\n");
904 sk_OCSP_RESPID_pop_free(ids
, OCSP_RESPID_free
);
905 OCSP_RESPID_free(id
);
914 typedef struct ssl_session_test_fixture
{
915 const char *test_case_name
;
918 } SSL_SESSION_TEST_FIXTURE
;
920 static int new_called
= 0, remove_called
= 0;
922 static SSL_SESSION_TEST_FIXTURE
923 ssl_session_set_up(const char *const test_case_name
)
925 SSL_SESSION_TEST_FIXTURE fixture
;
927 fixture
.test_case_name
= test_case_name
;
928 fixture
.use_ext_cache
= 1;
929 fixture
.use_int_cache
= 1;
931 new_called
= remove_called
= 0;
936 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture
)
940 static int new_session_cb(SSL
*ssl
, SSL_SESSION
*sess
)
947 static void remove_session_cb(SSL_CTX
*ctx
, SSL_SESSION
*sess
)
952 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix
)
954 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
955 SSL
*serverssl1
= NULL
, *clientssl1
= NULL
;
956 SSL
*serverssl2
= NULL
, *clientssl2
= NULL
;
957 #ifndef OPENSSL_NO_TLS1_1
958 SSL
*serverssl3
= NULL
, *clientssl3
= NULL
;
960 SSL_SESSION
*sess1
= NULL
, *sess2
= NULL
;
963 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
964 &cctx
, cert
, privkey
)) {
965 printf("Unable to create SSL_CTX pair\n");
969 #ifndef OPENSSL_NO_TLS1_2
970 /* Only allow TLS1.2 so we can force a connection failure later */
971 SSL_CTX_set_min_proto_version(cctx
, TLS1_2_VERSION
);
974 /* Set up session cache */
975 if (fix
.use_ext_cache
) {
976 SSL_CTX_sess_set_new_cb(cctx
, new_session_cb
);
977 SSL_CTX_sess_set_remove_cb(cctx
, remove_session_cb
);
979 if (fix
.use_int_cache
) {
980 /* Also covers instance where both are set */
981 SSL_CTX_set_session_cache_mode(cctx
, SSL_SESS_CACHE_CLIENT
);
983 SSL_CTX_set_session_cache_mode(cctx
,
984 SSL_SESS_CACHE_CLIENT
985 | SSL_SESS_CACHE_NO_INTERNAL_STORE
);
988 if (!create_ssl_objects(sctx
, cctx
, &serverssl1
, &clientssl1
, NULL
,
990 printf("Unable to create SSL objects\n");
994 if (!create_ssl_connection(serverssl1
, clientssl1
, SSL_ERROR_NONE
)) {
995 printf("Unable to create SSL connection\n");
998 sess1
= SSL_get1_session(clientssl1
);
1000 printf("Unexpected NULL session\n");
1004 if (fix
.use_int_cache
&& SSL_CTX_add_session(cctx
, sess1
)) {
1005 /* Should have failed because it should already be in the cache */
1006 printf("Unexpected success adding session to cache\n");
1010 if (fix
.use_ext_cache
&& (new_called
!= 1 || remove_called
!= 0)) {
1011 printf("Session not added to cache\n");
1015 if (!create_ssl_objects(sctx
, cctx
, &serverssl2
, &clientssl2
, NULL
, NULL
)) {
1016 printf("Unable to create second SSL objects\n");
1020 if (!create_ssl_connection(serverssl2
, clientssl2
, SSL_ERROR_NONE
)) {
1021 printf("Unable to create second SSL connection\n");
1025 sess2
= SSL_get1_session(clientssl2
);
1026 if (sess2
== NULL
) {
1027 printf("Unexpected NULL session from clientssl2\n");
1031 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 0)) {
1032 printf("Remove session callback unexpectedly called\n");
1037 * This should clear sess2 from the cache because it is a "bad" session. See
1038 * SSL_set_session() documentation.
1040 if (!SSL_set_session(clientssl2
, sess1
)) {
1041 printf("Unexpected failure setting session\n");
1045 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 1)) {
1046 printf("Failed to call callback to remove session\n");
1051 if (SSL_get_session(clientssl2
) != sess1
) {
1052 printf("Unexpected session found\n");
1056 if (fix
.use_int_cache
) {
1057 if (!SSL_CTX_add_session(cctx
, sess2
)) {
1059 * Should have succeeded because it should not already be in the cache
1061 printf("Unexpected failure adding session to cache\n");
1065 if (!SSL_CTX_remove_session(cctx
, sess2
)) {
1066 printf("Unexpected failure removing session from cache\n");
1070 /* This is for the purposes of internal cache testing...ignore the
1071 * counter for external cache
1073 if (fix
.use_ext_cache
)
1077 /* This shouldn't be in the cache so should fail */
1078 if (SSL_CTX_remove_session(cctx
, sess2
)) {
1079 printf("Unexpected success removing session from cache\n");
1083 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 2)) {
1084 printf("Failed to call callback to remove session #2\n");
1088 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
1089 /* Force a connection failure */
1090 SSL_CTX_set_max_proto_version(sctx
, TLS1_1_VERSION
);
1092 if (!create_ssl_objects(sctx
, cctx
, &serverssl3
, &clientssl3
, NULL
, NULL
)) {
1093 printf("Unable to create third SSL objects\n");
1097 if (!SSL_set_session(clientssl3
, sess1
)) {
1098 printf("Unable to set session for third connection\n");
1102 /* This should fail because of the mismatched protocol versions */
1103 if (create_ssl_connection(serverssl3
, clientssl3
, SSL_ERROR_NONE
)) {
1104 printf("Unable to create third SSL connection\n");
1109 /* We should have automatically removed the session from the cache */
1110 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 3)) {
1111 printf("Failed to call callback to remove session #2\n");
1115 if (fix
.use_int_cache
&& !SSL_CTX_add_session(cctx
, sess2
)) {
1117 * Should have succeeded because it should not already be in the cache
1119 printf("Unexpected failure adding session to cache #2\n");
1127 SSL_free(serverssl1
);
1128 SSL_free(clientssl1
);
1129 SSL_free(serverssl2
);
1130 SSL_free(clientssl2
);
1131 #ifndef OPENSSL_NO_TLS1_1
1132 SSL_free(serverssl3
);
1133 SSL_free(clientssl3
);
1135 SSL_SESSION_free(sess1
);
1136 SSL_SESSION_free(sess2
);
1138 * Check if we need to remove any sessions up-refed for the external cache
1140 if (new_called
>= 1)
1141 SSL_SESSION_free(sess1
);
1142 if (new_called
>= 2)
1143 SSL_SESSION_free(sess2
);
1150 static int test_session_with_only_int_cache(void)
1152 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
1154 fixture
.use_ext_cache
= 0;
1156 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
1159 static int test_session_with_only_ext_cache(void)
1161 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
1163 fixture
.use_int_cache
= 0;
1165 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
1168 static int test_session_with_both_cache(void)
1170 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
1172 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
1179 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1181 static void setupbio(BIO
**res
, BIO
*bio1
, BIO
*bio2
, int type
)
1196 static int test_ssl_set_bio(int idx
)
1198 SSL_CTX
*ctx
= SSL_CTX_new(TLS_method());
1201 BIO
*irbio
= NULL
, *iwbio
= NULL
, *nrbio
= NULL
, *nwbio
= NULL
;
1203 int initrbio
, initwbio
, newrbio
, newwbio
;
1207 printf("Failed to allocate SSL_CTX\n");
1213 printf("Failed to allocate SSL object\n");
1224 OPENSSL_assert(newwbio
<= 2);
1226 if (initrbio
== USE_BIO_1
|| initwbio
== USE_BIO_1
|| newrbio
== USE_BIO_1
1227 || newwbio
== USE_BIO_1
) {
1228 bio1
= BIO_new(BIO_s_mem());
1230 printf("Failed to allocate bio1\n");
1235 if (initrbio
== USE_BIO_2
|| initwbio
== USE_BIO_2
|| newrbio
== USE_BIO_2
1236 || newwbio
== USE_BIO_2
) {
1237 bio2
= BIO_new(BIO_s_mem());
1239 printf("Failed to allocate bio2\n");
1244 setupbio(&irbio
, bio1
, bio2
, initrbio
);
1245 setupbio(&iwbio
, bio1
, bio2
, initwbio
);
1248 * We want to maintain our own refs to these BIO, so do an up ref for each
1249 * BIO that will have ownersip transferred in the SSL_set_bio() call
1253 if (iwbio
!= NULL
&& iwbio
!= irbio
)
1256 SSL_set_bio(ssl
, irbio
, iwbio
);
1258 setupbio(&nrbio
, bio1
, bio2
, newrbio
);
1259 setupbio(&nwbio
, bio1
, bio2
, newwbio
);
1262 * We will (maybe) transfer ownership again so do more up refs.
1263 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1266 if (nrbio
!= NULL
&& nrbio
!= irbio
&& (nwbio
!= iwbio
|| nrbio
!= nwbio
))
1268 if (nwbio
!= NULL
&& nwbio
!= nrbio
&& (nwbio
!= iwbio
|| (nwbio
== iwbio
&& irbio
== iwbio
)))
1271 SSL_set_bio(ssl
, nrbio
, nwbio
);
1280 * This test is checking that the ref counting for SSL_set_bio is correct.
1281 * If we get here and we did too many frees then we will fail in the above
1282 * functions. If we haven't done enough then this will only be detected in
1283 * a crypto-mdebug build
1290 typedef struct ssl_bio_test_fixture
{
1291 const char *test_case_name
;
1293 enum { NO_BIO_CHANGE
, CHANGE_RBIO
, CHANGE_WBIO
} change_bio
;
1294 } SSL_BIO_TEST_FIXTURE
;
1296 static SSL_BIO_TEST_FIXTURE
ssl_bio_set_up(const char *const test_case_name
)
1298 SSL_BIO_TEST_FIXTURE fixture
;
1300 fixture
.test_case_name
= test_case_name
;
1301 fixture
.pop_ssl
= 0;
1302 fixture
.change_bio
= NO_BIO_CHANGE
;
1307 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture
)
1311 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix
)
1313 BIO
*sslbio
= NULL
, *membio1
= NULL
, *membio2
= NULL
;
1314 SSL_CTX
*ctx
= SSL_CTX_new(TLS_method());
1319 printf("Failed to allocate SSL_CTX\n");
1325 printf("Failed to allocate SSL object\n");
1329 sslbio
= BIO_new(BIO_f_ssl());
1330 membio1
= BIO_new(BIO_s_mem());
1332 if (sslbio
== NULL
|| membio1
== NULL
) {
1333 printf("Malloc failure creating BIOs\n");
1337 BIO_set_ssl(sslbio
, ssl
, BIO_CLOSE
);
1340 * If anything goes wrong here then we could leak memory, so this will
1341 * be caught in a crypto-mdebug build
1343 BIO_push(sslbio
, membio1
);
1345 /* Verify chaning the rbio/wbio directly does not cause leaks */
1346 if (fix
.change_bio
!= NO_BIO_CHANGE
) {
1347 membio2
= BIO_new(BIO_s_mem());
1348 if (membio2
== NULL
) {
1349 printf("Malloc failure creating membio2\n");
1352 if (fix
.change_bio
== CHANGE_RBIO
)
1353 SSL_set0_rbio(ssl
, membio2
);
1355 SSL_set0_wbio(ssl
, membio2
);
1374 static int test_ssl_bio_pop_next_bio(void)
1376 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
1378 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
1381 static int test_ssl_bio_pop_ssl_bio(void)
1383 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
1385 fixture
.pop_ssl
= 1;
1387 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
1390 static int test_ssl_bio_change_rbio(void)
1392 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
1394 fixture
.change_bio
= CHANGE_RBIO
;
1396 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
1399 static int test_ssl_bio_change_wbio(void)
1401 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
1403 fixture
.change_bio
= CHANGE_WBIO
;
1405 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
1409 /* The list of sig algs */
1411 /* The length of the list */
1413 /* A sigalgs list in string format */
1414 const char *liststr
;
1415 /* Whether setting the list should succeed */
1417 /* Whether creating a connection with the list should succeed */
1421 static const int validlist1
[] = {NID_sha256
, EVP_PKEY_RSA
};
1422 #ifndef OPENSSL_NO_EC
1423 static const int validlist2
[] = {NID_sha256
, EVP_PKEY_RSA
, NID_sha512
, EVP_PKEY_EC
};
1424 static const int validlist3
[] = {NID_sha512
, EVP_PKEY_EC
};
1426 static const int invalidlist1
[] = {NID_undef
, EVP_PKEY_RSA
};
1427 static const int invalidlist2
[] = {NID_sha256
, NID_undef
};
1428 static const int invalidlist3
[] = {NID_sha256
, EVP_PKEY_RSA
, NID_sha256
};
1429 static const int invalidlist4
[] = {NID_sha256
};
1430 static const sigalgs_list testsigalgs
[] = {
1431 {validlist1
, OSSL_NELEM(validlist1
), NULL
, 1, 1},
1432 #ifndef OPENSSL_NO_EC
1433 {validlist2
, OSSL_NELEM(validlist2
), NULL
, 1, 1},
1434 {validlist3
, OSSL_NELEM(validlist3
), NULL
, 1, 0},
1436 {NULL
, 0, "RSA+SHA256", 1, 1},
1437 #ifndef OPENSSL_NO_EC
1438 {NULL
, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1439 {NULL
, 0, "ECDSA+SHA512", 1, 0},
1441 {invalidlist1
, OSSL_NELEM(invalidlist1
), NULL
, 0, 0},
1442 {invalidlist2
, OSSL_NELEM(invalidlist2
), NULL
, 0, 0},
1443 {invalidlist3
, OSSL_NELEM(invalidlist3
), NULL
, 0, 0},
1444 {invalidlist4
, OSSL_NELEM(invalidlist4
), NULL
, 0, 0},
1445 {NULL
, 0, "RSA", 0, 0},
1446 {NULL
, 0, "SHA256", 0, 0},
1447 {NULL
, 0, "RSA+SHA256:SHA256", 0, 0},
1448 {NULL
, 0, "Invalid", 0, 0}};
1450 static int test_set_sigalgs(int idx
)
1452 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
1453 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
1455 const sigalgs_list
*curr
;
1458 /* Should never happen */
1459 if ((size_t)idx
>= OSSL_NELEM(testsigalgs
) * 2)
1462 testctx
= ((size_t)idx
< OSSL_NELEM(testsigalgs
));
1463 curr
= testctx
? &testsigalgs
[idx
]
1464 : &testsigalgs
[idx
- OSSL_NELEM(testsigalgs
)];
1466 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
1467 &cctx
, cert
, privkey
)) {
1468 printf("Unable to create SSL_CTX pair\n");
1473 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1474 * for TLSv1.2 for now until we add a new API.
1476 SSL_CTX_set_max_proto_version(cctx
, TLS1_2_VERSION
);
1480 if (curr
->list
!= NULL
)
1481 ret
= SSL_CTX_set1_sigalgs(cctx
, curr
->list
, curr
->listlen
);
1483 ret
= SSL_CTX_set1_sigalgs_list(cctx
, curr
->liststr
);
1487 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1494 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx
);
1499 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
1500 printf("Unable to create SSL objects\n");
1507 if (curr
->list
!= NULL
)
1508 ret
= SSL_set1_sigalgs(clientssl
, curr
->list
, curr
->listlen
);
1510 ret
= SSL_set1_sigalgs_list(clientssl
, curr
->liststr
);
1513 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx
);
1519 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx
);
1524 if (curr
->connsuccess
!= create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)) {
1525 printf("Unexpected return value creating SSL connection (%d)\n", idx
);
1532 SSL_free(serverssl
);
1533 SSL_free(clientssl
);
1540 #ifndef OPENSSL_NO_TLS1_3
1542 #define MSG1 "Hello"
1543 #define MSG2 "World."
1547 #define MSG6 "test."
1550 * Helper method to setup objects for early data test. Caller frees objects on
1553 static int setupearly_data_test(SSL_CTX
**cctx
, SSL_CTX
**sctx
, SSL
**clientssl
,
1554 SSL
**serverssl
, SSL_SESSION
**sess
)
1556 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), sctx
,
1557 cctx
, cert
, privkey
)) {
1558 printf("Unable to create SSL_CTX pair\n");
1562 if (!create_ssl_objects(*sctx
, *cctx
, serverssl
, clientssl
, NULL
, NULL
)) {
1563 printf("Unable to create SSL objects\n");
1567 if (!create_ssl_connection(*serverssl
, *clientssl
, SSL_ERROR_NONE
)) {
1568 printf("Unable to create SSL connection\n");
1572 *sess
= SSL_get1_session(*clientssl
);
1574 SSL_shutdown(*clientssl
);
1575 SSL_shutdown(*serverssl
);
1577 SSL_free(*serverssl
);
1578 SSL_free(*clientssl
);
1579 *serverssl
= *clientssl
= NULL
;
1581 if (!create_ssl_objects(*sctx
, *cctx
, serverssl
, clientssl
, NULL
, NULL
)) {
1582 printf("Unable to create SSL objects (2)\n");
1586 if (!SSL_set_session(*clientssl
, *sess
)) {
1587 printf("Failed setting session\n");
1594 static int test_early_data_read_write(void)
1596 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
1597 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
1599 SSL_SESSION
*sess
= NULL
;
1600 unsigned char buf
[20];
1601 size_t readbytes
, written
;
1603 if (!setupearly_data_test(&cctx
, &sctx
, &clientssl
, &serverssl
, &sess
))
1606 /* Write and read some early data */
1607 if (!SSL_write_early_data(clientssl
, MSG1
, strlen(MSG1
), &written
)
1608 || written
!= strlen(MSG1
)) {
1609 printf("Failed writing early data message 1\n");
1613 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1614 != SSL_READ_EARLY_DATA_SUCCESS
1615 || readbytes
!= strlen(MSG1
)
1616 || memcmp(MSG1
, buf
, strlen(MSG1
))) {
1617 printf("Failed reading early data message 1\n");
1621 if (SSL_get_early_data_status(serverssl
) != SSL_EARLY_DATA_ACCEPTED
) {
1622 printf("Unexpected early data status\n");
1627 * Server should be able to write data, and client should be able to
1630 if (!SSL_write_early_data(serverssl
, MSG2
, strlen(MSG2
), &written
)
1631 || written
!= strlen(MSG2
)) {
1632 printf("Failed writing message 2\n");
1636 if (!SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)
1637 || readbytes
!= strlen(MSG2
)
1638 || memcmp(MSG2
, buf
, strlen(MSG2
))) {
1639 printf("Failed reading message 2\n");
1643 /* Even after reading normal data, client should be able write early data */
1644 if (!SSL_write_early_data(clientssl
, MSG3
, strlen(MSG3
), &written
)
1645 || written
!= strlen(MSG3
)) {
1646 printf("Failed writing early data message 3\n");
1650 /* Server should still be able read early data after writing data */
1651 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1652 != SSL_READ_EARLY_DATA_SUCCESS
1653 || readbytes
!= strlen(MSG3
)
1654 || memcmp(MSG3
, buf
, strlen(MSG3
))) {
1655 printf("Failed reading early data message 3\n");
1659 /* Write more data from server and read it from client */
1660 if (!SSL_write_early_data(serverssl
, MSG4
, strlen(MSG4
), &written
)
1661 || written
!= strlen(MSG4
)) {
1662 printf("Failed writing message 4\n");
1666 if (!SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)
1667 || readbytes
!= strlen(MSG4
)
1668 || memcmp(MSG4
, buf
, strlen(MSG4
))) {
1669 printf("Failed reading message 4\n");
1674 * If client writes normal data it should mean writing early data is no
1677 if (!SSL_write_ex(clientssl
, MSG5
, strlen(MSG5
), &written
)
1678 || written
!= strlen(MSG5
)) {
1679 printf("Failed writing message 5\n");
1683 if (SSL_get_early_data_status(clientssl
) != SSL_EARLY_DATA_ACCEPTED
) {
1684 printf("Unexpected early data status(2)\n");
1688 /* Server should be told that there is no more early data */
1689 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1690 != SSL_READ_EARLY_DATA_FINISH
1691 || readbytes
!= 0) {
1692 printf("Failed finishing read of early data\n");
1696 /* Server should be able to read normal data */
1697 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
1698 || readbytes
!= strlen(MSG5
)) {
1699 printf("Failed reading message 5\n");
1703 /* Client and server should not be able to write/read early data now */
1704 if (SSL_write_early_data(clientssl
, MSG6
, strlen(MSG6
), &written
)) {
1705 printf("Unexpected success writing early data\n");
1710 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1711 != SSL_READ_EARLY_DATA_ERROR
) {
1712 printf("Unexpected success reading early data\n");
1718 * Make sure we process the NewSessionTicket. This arrives post-handshake
1719 * so we must make sure we attempt a read - even though we don't expect to
1720 * actually get any application data.
1722 if (SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)) {
1723 printf("Unexpected success doing final client read\n");
1727 SSL_SESSION_free(sess
);
1728 sess
= SSL_get1_session(clientssl
);
1730 SSL_shutdown(clientssl
);
1731 SSL_shutdown(serverssl
);
1733 SSL_free(serverssl
);
1734 SSL_free(clientssl
);
1735 serverssl
= clientssl
= NULL
;
1737 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
1738 printf("Unable to create SSL objects (3)\n");
1742 if (!SSL_set_session(clientssl
, sess
)) {
1743 printf("Failed setting session (2)\n");
1747 /* Write and read some early data */
1748 if (!SSL_write_early_data(clientssl
, MSG1
, strlen(MSG1
), &written
)
1749 || written
!= strlen(MSG1
)) {
1750 printf("Failed writing early data message 1\n");
1754 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1755 != SSL_READ_EARLY_DATA_SUCCESS
1756 || readbytes
!= strlen(MSG1
)
1757 || memcmp(MSG1
, buf
, strlen(MSG1
))) {
1758 printf("Failed reading early data message 1\n");
1762 if (SSL_connect(clientssl
) <= 0) {
1763 printf("Unable to complete client handshake\n");
1767 if (SSL_accept(serverssl
) <= 0) {
1768 printf("Unable to complete server handshake\n");
1772 /* Client and server should not be able to write/read early data now */
1773 if (SSL_write_early_data(clientssl
, MSG6
, strlen(MSG6
), &written
)) {
1774 printf("Unexpected success writing early data (2)\n");
1779 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1780 != SSL_READ_EARLY_DATA_ERROR
) {
1781 printf("Unexpected success reading early data (2)\n");
1786 /* Client and server should be able to write/read normal data */
1787 if (!SSL_write_ex(clientssl
, MSG5
, strlen(MSG5
), &written
)
1788 || written
!= strlen(MSG5
)) {
1789 printf("Failed writing message 5 (2)\n");
1793 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
1794 || readbytes
!= strlen(MSG5
)) {
1795 printf("Failed reading message 5 (2)\n");
1803 ERR_print_errors_fp(stdout
);
1804 SSL_SESSION_free(sess
);
1805 SSL_free(serverssl
);
1806 SSL_free(clientssl
);
1813 static int test_early_data_skip(void)
1815 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
1816 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
1819 unsigned char buf
[20];
1820 size_t readbytes
, written
;
1823 * Test that a server attempting to read early data can handle a connection
1824 * from a client where the early data is not acceptable.
1827 if (!setupearly_data_test(&cctx
, &sctx
, &clientssl
, &serverssl
, &sess
))
1831 * Deliberately corrupt the creation time. We take 20 seconds off the time.
1832 * It could be any value as long as it is not within tolerance. This should
1833 * mean the ticket is rejected.
1835 if (!SSL_SESSION_set_time(sess
, time(NULL
) - 20)) {
1836 printf("Unexpected failure setting session creation time\n");
1840 /* Write some early data */
1841 if (!SSL_write_early_data(clientssl
, MSG1
, strlen(MSG1
), &written
)
1842 || written
!= strlen(MSG1
)) {
1843 printf("Failed writing early data message 1\n");
1847 /* Server should reject the early data and skip over it */
1848 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1849 != SSL_READ_EARLY_DATA_FINISH
1850 || readbytes
!= 0) {
1851 printf("Failed reading early data\n");
1855 if (SSL_get_early_data_status(serverssl
) != SSL_EARLY_DATA_REJECTED
) {
1856 printf("Unexpected early data status\n");
1861 * We should be able to send normal data despite rejection of early data
1863 if (!SSL_write_ex(clientssl
, MSG2
, strlen(MSG2
), &written
)
1864 || written
!= strlen(MSG2
)) {
1865 printf("Failed writing message 2\n");
1869 if (SSL_get_early_data_status(clientssl
) != SSL_EARLY_DATA_REJECTED
) {
1870 printf("Unexpected early data status (2)\n");
1874 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
1875 || readbytes
!= strlen(MSG2
)
1876 || memcmp(MSG2
, buf
, strlen(MSG2
))) {
1877 printf("Failed reading message 2\n");
1885 ERR_print_errors_fp(stdout
);
1886 SSL_SESSION_free(sess
);
1887 SSL_free(serverssl
);
1888 SSL_free(clientssl
);
1895 static int test_early_data_not_sent(void)
1897 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
1898 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
1901 unsigned char buf
[20];
1902 size_t readbytes
, written
;
1905 * Test that a server attempting to read early data can handle a connection
1906 * from a client that doesn't send any.
1909 if (!setupearly_data_test(&cctx
, &sctx
, &clientssl
, &serverssl
, &sess
))
1912 /* Write some data - should block due to handshake with server */
1913 SSL_set_connect_state(clientssl
);
1914 if (SSL_write_ex(clientssl
, MSG1
, strlen(MSG1
), &written
)) {
1915 printf("Unexpected success writing message 1\n");
1919 /* Server should detect that early data has not been sent */
1920 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
1921 != SSL_READ_EARLY_DATA_FINISH
1922 || readbytes
!= 0) {
1923 printf("Failed reading early data\n");
1927 if (SSL_get_early_data_status(serverssl
) != SSL_EARLY_DATA_NOT_SENT
) {
1928 printf("Unexpected early data status\n");
1932 if (SSL_get_early_data_status(clientssl
) != SSL_EARLY_DATA_NOT_SENT
) {
1933 printf("Unexpected early data status (2)\n");
1937 /* Continue writing the message we started earlier */
1938 if (!SSL_write_ex(clientssl
, MSG1
, strlen(MSG1
), &written
)
1939 || written
!= strlen(MSG1
)) {
1940 printf("Failed writing message 1\n");
1944 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
1945 || readbytes
!= strlen(MSG1
)
1946 || memcmp(MSG1
, buf
, strlen(MSG1
))) {
1947 printf("Failed reading message 1\n");
1951 if (!SSL_write_ex(serverssl
, MSG2
, strlen(MSG2
), &written
)
1952 || written
!= strlen(MSG2
)) {
1953 printf("Failed writing message 2\n");
1957 /* Should block due to the NewSessionTicket arrival */
1958 if (SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)) {
1959 printf("Unexpected success reading message 2\n");
1963 if (!SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)
1964 || readbytes
!= strlen(MSG2
)
1965 || memcmp(MSG2
, buf
, strlen(MSG2
))) {
1966 printf("Failed reading message 2\n");
1974 ERR_print_errors_fp(stdout
);
1975 SSL_SESSION_free(sess
);
1976 SSL_free(serverssl
);
1977 SSL_free(clientssl
);
1984 static int test_early_data_not_expected(void)
1986 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
1987 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
1990 unsigned char buf
[20];
1991 size_t readbytes
, written
;
1994 * Test that a server that doesn't try to read early data can handle a
1995 * client sending some.
1998 if (!setupearly_data_test(&cctx
, &sctx
, &clientssl
, &serverssl
, &sess
))
2001 /* Write some early data */
2002 if (!SSL_write_early_data(clientssl
, MSG1
, strlen(MSG1
), &written
)) {
2003 printf("Unexpected failure writing message 1\n");
2008 * Server should skip over early data and then block waiting for client to
2009 * continue handshake
2011 if (SSL_accept(serverssl
) > 0) {
2012 printf("Unexpected success setting up server connection\n");
2016 if (SSL_connect(clientssl
) <= 0) {
2017 printf("Failed setting up client connection\n");
2021 if (SSL_get_early_data_status(serverssl
) != SSL_EARLY_DATA_REJECTED
) {
2022 printf("Unexpected early data status\n");
2026 if (SSL_accept(serverssl
) <= 0) {
2027 printf("Failed setting up server connection\n");
2031 if (SSL_get_early_data_status(clientssl
) != SSL_EARLY_DATA_REJECTED
) {
2032 printf("Unexpected early data status (2)\n");
2036 /* Send some normal data from client to server */
2037 if (!SSL_write_ex(clientssl
, MSG2
, strlen(MSG2
), &written
)
2038 || written
!= strlen(MSG2
)) {
2039 printf("Failed writing message 2\n");
2043 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
2044 || readbytes
!= strlen(MSG2
)
2045 || memcmp(MSG2
, buf
, strlen(MSG2
))) {
2046 printf("Failed reading message 2\n");
2054 ERR_print_errors_fp(stdout
);
2055 SSL_SESSION_free(sess
);
2056 SSL_free(serverssl
);
2057 SSL_free(clientssl
);
2065 # ifndef OPENSSL_NO_TLS1_2
2066 static int test_early_data_tls1_2(void)
2068 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
2069 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
2071 unsigned char buf
[20];
2072 size_t readbytes
, written
;
2075 * Test that a server attempting to read early data can handle a connection
2076 * from a TLSv1.2 client.
2079 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
2080 &cctx
, cert
, privkey
)) {
2081 printf("Unable to create SSL_CTX pair\n");
2085 if (!create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
, NULL
, NULL
)) {
2086 printf("Unable to create SSL objects\n");
2090 /* Write some data - should block due to handshake with server */
2091 SSL_set_max_proto_version(clientssl
, TLS1_2_VERSION
);
2092 SSL_set_connect_state(clientssl
);
2093 if (SSL_write_ex(clientssl
, MSG1
, strlen(MSG1
), &written
)) {
2094 printf("Unexpected success writing message 1\n");
2099 * Server should do TLSv1.2 handshake. First it will block waiting for more
2100 * messages from client after ServerDone. Then SSL_read_early_data should
2101 * finish and detect that early data has not been sent
2103 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
2104 != SSL_READ_EARLY_DATA_ERROR
) {
2105 printf("Unexpected success reading early data\n");
2110 * Continue writing the message we started earlier. Will still block waiting
2111 * for the CCS/Finished from server
2113 if (SSL_write_ex(clientssl
, MSG1
, strlen(MSG1
), &written
)) {
2114 printf("Unexpected success writing message 1\n");
2118 if (SSL_read_early_data(serverssl
, buf
, sizeof(buf
), &readbytes
)
2119 != SSL_READ_EARLY_DATA_FINISH
2120 || readbytes
!= 0) {
2121 printf("Failed reading early data\n");
2125 if (SSL_get_early_data_status(serverssl
) != SSL_EARLY_DATA_NOT_SENT
) {
2126 printf("Unexpected early data status\n");
2130 /* Continue writing the message we started earlier */
2131 if (!SSL_write_ex(clientssl
, MSG1
, strlen(MSG1
), &written
)
2132 || written
!= strlen(MSG1
)) {
2133 printf("Failed writing message 1\n");
2137 if (SSL_get_early_data_status(clientssl
) != SSL_EARLY_DATA_NOT_SENT
) {
2138 printf("Unexpected early data status (2)\n");
2142 if (!SSL_read_ex(serverssl
, buf
, sizeof(buf
), &readbytes
)
2143 || readbytes
!= strlen(MSG1
)
2144 || memcmp(MSG1
, buf
, strlen(MSG1
))) {
2145 printf("Failed reading message 1\n");
2149 if (!SSL_write_ex(serverssl
, MSG2
, strlen(MSG2
), &written
)
2150 || written
!= strlen(MSG2
)) {
2151 printf("Failed writing message 2\n");
2155 if (!SSL_read_ex(clientssl
, buf
, sizeof(buf
), &readbytes
)
2156 || readbytes
!= strlen(MSG2
)
2157 || memcmp(MSG2
, buf
, strlen(MSG2
))) {
2158 printf("Failed reading message 2\n");
2166 ERR_print_errors_fp(stdout
);
2167 SSL_free(serverssl
);
2168 SSL_free(clientssl
);
2177 int test_main(int argc
, char *argv
[])
2182 printf("Invalid argument count\n");
2189 ADD_TEST(test_large_message_tls
);
2190 ADD_TEST(test_large_message_tls_read_ahead
);
2191 #ifndef OPENSSL_NO_DTLS
2192 ADD_TEST(test_large_message_dtls
);
2194 #ifndef OPENSSL_NO_OCSP
2195 ADD_TEST(test_tlsext_status_type
);
2197 ADD_TEST(test_session_with_only_int_cache
);
2198 ADD_TEST(test_session_with_only_ext_cache
);
2199 ADD_TEST(test_session_with_both_cache
);
2200 ADD_ALL_TESTS(test_ssl_set_bio
, TOTAL_SSL_SET_BIO_TESTS
);
2201 ADD_TEST(test_ssl_bio_pop_next_bio
);
2202 ADD_TEST(test_ssl_bio_pop_ssl_bio
);
2203 ADD_TEST(test_ssl_bio_change_rbio
);
2204 ADD_TEST(test_ssl_bio_change_wbio
);
2205 ADD_ALL_TESTS(test_set_sigalgs
, OSSL_NELEM(testsigalgs
) * 2);
2206 ADD_TEST(test_keylog
);
2207 #ifndef OPENSSL_NO_TLS1_3
2208 ADD_TEST(test_keylog_no_master_key
);
2210 #ifndef OPENSSL_NO_TLS1_2
2211 ADD_TEST(test_early_cb
);
2213 #ifndef OPENSSL_NO_TLS1_3
2214 ADD_TEST(test_early_data_read_write
);
2215 ADD_TEST(test_early_data_skip
);
2216 ADD_TEST(test_early_data_not_sent
);
2217 ADD_TEST(test_early_data_not_expected
);
2218 # ifndef OPENSSL_NO_TLS1_2
2219 ADD_TEST(test_early_data_tls1_2
);
2223 testresult
= run_tests(argv
[0]);
2225 bio_s_mempacket_test_free();