]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/sslapitest.c
Add an sslapitest for early callback
[thirdparty/openssl.git] / test / sslapitest.c
1 /*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11
12 #include <openssl/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/ssl.h>
16 #include <openssl/ocsp.h>
17
18 #include "ssltestlib.h"
19 #include "testutil.h"
20 #include "test_main_custom.h"
21 #include "e_os.h"
22
23 static char *cert = NULL;
24 static char *privkey = NULL;
25
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;
32
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;
37
38 static int cdummyarg = 1;
39 static X509 *ocspcert = NULL;
40 #endif
41
42 #define NUM_EXTRA_CERTS 40
43
44 /*
45 * This structure is used to validate that the correct number of log messages
46 * of various types are emitted when emitting secret logs.
47 */
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;
55 };
56
57 static void client_keylog_callback(const SSL *ssl, const char *line) {
58 int line_length = strlen(line);
59
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;
64 return;
65 }
66
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;
71
72 return;
73 }
74
75 static void server_keylog_callback(const SSL *ssl, const char *line) {
76 int line_length = strlen(line);
77
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;
82 return;
83 }
84
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;
89
90 return;
91 }
92
93 static int compare_hex_encoded_buffer(const char *hex_encoded,
94 size_t hex_length,
95 const uint8_t *raw,
96 size_t raw_length) {
97 size_t i;
98 size_t j;
99
100 /* One byte too big, just to be safe. */
101 char hexed[3] = {0};
102
103 if ((raw_length * 2) != hex_length) {
104 printf("Inconsistent hex encoded lengths.\n");
105 return 1;
106 }
107
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");
112 return 1;
113 }
114 j += 2;
115 }
116
117 return 0;
118 }
119
120 static int test_keylog_output(char *buffer, const SSL *ssl,
121 const SSL_SESSION *session,
122 struct sslapitest_log_counts *expected) {
123 char *token = NULL;
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;
134
135 token = strtok(buffer, " \n");
136 while (token) {
137 if (strcmp(token, "RSA") == 0) {
138 /*
139 * Premaster secret. Tokens should be: 16 ASCII bytes of
140 * hex-encoded encrypted secret, then the hex-encoded pre-master
141 * secret.
142 */
143 token = strtok(NULL, " \n");
144 if (!token) {
145 printf("Unexpectedly short premaster secret log.\n");
146 return 0;
147 }
148 if (strlen(token) != 16) {
149 printf("Bad value for encrypted secret: %s\n", token);
150 return 0;
151 }
152 token = strtok(NULL, " \n");
153 if (!token) {
154 printf("Unexpectedly short premaster secret log.\n");
155 return 0;
156 }
157 /*
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.
161 */
162 rsa_key_exchange_count++;
163 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
164 /*
165 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
166 * client random, then the hex-encoded master secret.
167 */
168 client_random_size = SSL_get_client_random(ssl,
169 actual_client_random,
170 SSL3_RANDOM_SIZE);
171 if (client_random_size != SSL3_RANDOM_SIZE) {
172 printf("Unexpected short client random.\n");
173 return 0;
174 }
175
176 token = strtok(NULL, " \n");
177 if (!token) {
178 printf("Unexpected short master secret log.\n");
179 return 0;
180 }
181 if (strlen(token) != 64) {
182 printf("Bad value for client random: %s\n", token);
183 return 0;
184 }
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);
188 return 0;
189 }
190
191 token = strtok(NULL, " \n");
192 if (!token) {
193 printf("Unexpectedly short master secret log.\n");
194 return 0;
195 }
196
197 master_key_size = SSL_SESSION_get_master_key(session,
198 actual_master_key,
199 master_key_size);
200 if (!master_key_size) {
201 printf("Error getting master key to compare.\n");
202 return 0;
203 }
204 if (compare_hex_encoded_buffer(token, strlen(token),
205 actual_master_key,
206 master_key_size)) {
207 printf("Bad value for master key: %s\n", token);
208 return 0;
209 }
210
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)) {
216 /*
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.
221 */
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++;
230
231 client_random_size = SSL_get_client_random(ssl,
232 actual_client_random,
233 SSL3_RANDOM_SIZE);
234 if (client_random_size != SSL3_RANDOM_SIZE) {
235 printf("Unexpected short client random.\n");
236 return 0;
237 }
238
239 token = strtok(NULL, " \n");
240 if (!token) {
241 printf("Unexpected short client handshake secret log.\n");
242 return 0;
243 }
244 if (strlen(token) != 64) {
245 printf("Bad value for client random: %s\n", token);
246 return 0;
247 }
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);
251 return 0;
252 }
253
254 token = strtok(NULL, " \n");
255 if (!token) {
256 printf("Unexpectedly short master secret log.\n");
257 return 0;
258 }
259
260 /*
261 * TODO(TLS1.3): test that application traffic secrets are what
262 * we expect */
263 } else {
264 printf("Unexpected token in buffer: %s\n", token);
265 return 0;
266 }
267
268 token = strtok(NULL, " \n");
269 }
270
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));
278 }
279
280 static int test_keylog(void) {
281 SSL_CTX *cctx = NULL, *sctx = NULL;
282 SSL *clientssl = NULL, *serverssl = NULL;
283 int testresult = 0;
284 int rc;
285 struct sslapitest_log_counts expected = {0};
286
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;
293
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");
297 return 0;
298 }
299
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);
303
304 /* We also want to ensure that we use RSA-based key exchange. */
305 rc = SSL_CTX_set_cipher_list(cctx, "RSA");
306 if (rc == 0) {
307 printf("Unable to restrict to RSA key exchange.\n");
308 goto end;
309 }
310
311 if (SSL_CTX_get_keylog_callback(cctx)) {
312 printf("Unexpected initial value for client "
313 "SSL_CTX_get_keylog_callback()\n");
314 goto end;
315 }
316 if (SSL_CTX_get_keylog_callback(sctx)) {
317 printf("Unexpected initial value for server "
318 "SSL_CTX_get_keylog_callback()\n");
319 goto end;
320 }
321
322 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
323 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
324
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");
328 }
329
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");
333 }
334
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");
338 goto end;
339 }
340
341 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
342 printf("Unable to create SSL connection\n");
343 goto end;
344 }
345
346 if (error_writing_log) {
347 printf("Error encountered while logging\n");
348 goto end;
349 }
350
351 if ((client_log_buffer_index == 0) || (server_log_buffer_index == 0)) {
352 printf("No logs written\n");
353 goto end;
354 }
355
356 /*
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.
361 */
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");
367 goto end;
368 }
369
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");
374 goto end;
375 }
376
377 testresult = 1;
378
379 end:
380 SSL_free(serverssl);
381 SSL_free(clientssl);
382 SSL_CTX_free(sctx);
383 SSL_CTX_free(cctx);
384
385 return testresult;
386 }
387
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;
392 int testresult = 0;
393 struct sslapitest_log_counts expected = {0};
394
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;
401
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");
405 return 0;
406 }
407
408 if (SSL_CTX_get_keylog_callback(cctx)) {
409 printf("Unexpected initial value for client "
410 "SSL_CTX_get_keylog_callback()\n");
411 goto end;
412 }
413 if (SSL_CTX_get_keylog_callback(sctx)) {
414 printf("Unexpected initial value for server "
415 "SSL_CTX_get_keylog_callback()\n");
416 goto end;
417 }
418
419 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
420 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
421
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");
425 }
426
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");
430 }
431
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");
435 goto end;
436 }
437
438 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
439 printf("Unable to create SSL connection\n");
440 goto end;
441 }
442
443 if (error_writing_log) {
444 printf("Error encountered while logging\n");
445 goto end;
446 }
447
448 /*
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.
452 */
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");
460 goto end;
461 }
462 if (!test_keylog_output(server_log_buffer, serverssl,
463 SSL_get_session(serverssl), &expected)) {
464 printf("Error encountered in server log buffer\n");
465 goto end;
466 }
467
468 testresult = 1;
469
470 end:
471 SSL_free(serverssl);
472 SSL_free(clientssl);
473 SSL_CTX_free(sctx);
474 SSL_CTX_free(cctx);
475
476 return testresult;
477 }
478 #endif
479
480 static int full_early_callback(SSL *s, int *al, void *arg)
481 {
482 int *ctr = arg;
483 const unsigned char *p;
484 /* We only configure two ciphers, but the SCSV is added automatically. */
485 #ifdef OPENSSL_NO_EC
486 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
487 #else
488 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
489 0x2c, 0x00, 0xff};
490 #endif
491 size_t len;
492
493 /* Make sure we can defer processing and get called back. */
494 if ((*ctr)++ == 0)
495 return -1;
496
497 len = SSL_early_get0_ciphers(s, &p);
498 if (len != sizeof(expected_ciphers) ||
499 memcmp(p, expected_ciphers, len) != 0) {
500 printf("Early callback expected ciphers mismatch\n");
501 return 0;
502 }
503 len = SSL_early_get0_compression_methods(s, &p);
504 if (len != 1 || *p != 0) {
505 printf("Early callback expected comperssion methods mismatch\n");
506 return 0;
507 }
508 return 1;
509 }
510
511 static int test_early_cb(void) {
512 SSL_CTX *cctx = NULL, *sctx = NULL;
513 SSL *clientssl = NULL, *serverssl = NULL;
514 int testctr = 0, testresult = 0;
515
516 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
517 &cctx, cert, privkey)) {
518 printf("Unable to create SSL_CTX pair\n");
519 goto end;
520 }
521
522 SSL_CTX_set_early_cb(sctx, full_early_callback, &testctr);
523 /* The gimpy cipher list we configure can't do TLS 1.3. */
524 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
525 if (!SSL_CTX_set_cipher_list(cctx,
526 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) {
527 printf("Failed to set cipher list\n");
528 goto end;
529 }
530
531 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
532 printf("Unable to create SSL objects\n");
533 goto end;
534 }
535
536 if (create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_EARLY)) {
537 printf("Creating SSL connection succeeded with async early return\n");
538 goto end;
539 }
540
541 /* Passing a -1 literal is a hack since the real value was lost. */
542 if (SSL_get_error(serverssl, -1) != SSL_ERROR_WANT_EARLY) {
543 printf("Early callback failed to make state SSL_ERROR_WANT_EARLY\n");
544 goto end;
545 }
546
547 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
548 printf("Restarting SSL connection failed\n");
549 goto end;
550 }
551
552 testresult = 1;
553
554 end:
555 SSL_free(serverssl);
556 SSL_free(clientssl);
557 SSL_CTX_free(sctx);
558 SSL_CTX_free(cctx);
559
560 return testresult;
561 }
562
563 static int execute_test_large_message(const SSL_METHOD *smeth,
564 const SSL_METHOD *cmeth, int read_ahead)
565 {
566 SSL_CTX *cctx = NULL, *sctx = NULL;
567 SSL *clientssl = NULL, *serverssl = NULL;
568 int testresult = 0;
569 int i;
570 BIO *certbio = BIO_new_file(cert, "r");
571 X509 *chaincert = NULL;
572 int certlen;
573
574 if (certbio == NULL) {
575 printf("Can't load the certficate file\n");
576 goto end;
577 }
578 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
579 BIO_free(certbio);
580 certbio = NULL;
581 if (chaincert == NULL) {
582 printf("Unable to load certificate for chain\n");
583 goto end;
584 }
585
586 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
587 &cctx, cert, privkey)) {
588 printf("Unable to create SSL_CTX pair\n");
589 goto end;
590 }
591
592 if(read_ahead) {
593 /*
594 * Test that read_ahead works correctly when dealing with large
595 * records
596 */
597 SSL_CTX_set_read_ahead(cctx, 1);
598 }
599
600 /*
601 * We assume the supplied certificate is big enough so that if we add
602 * NUM_EXTRA_CERTS it will make the overall message large enough. The
603 * default buffer size is requested to be 16k, but due to the way BUF_MEM
604 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
605 * we need to have a message larger than that.
606 */
607 certlen = i2d_X509(chaincert, NULL);
608 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
609 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
610 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
611 if (!X509_up_ref(chaincert)) {
612 printf("Unable to up ref cert\n");
613 goto end;
614 }
615 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
616 printf("Unable to add extra chain cert %d\n", i);
617 X509_free(chaincert);
618 goto end;
619 }
620 }
621
622 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
623 printf("Unable to create SSL objects\n");
624 goto end;
625 }
626
627 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
628 printf("Unable to create SSL connection\n");
629 goto end;
630 }
631
632 /*
633 * Calling SSL_clear() first is not required but this tests that SSL_clear()
634 * doesn't leak (when using enable-crypto-mdebug).
635 */
636 if (!SSL_clear(serverssl)) {
637 printf("Unexpected failure from SSL_clear()\n");
638 goto end;
639 }
640
641 testresult = 1;
642 end:
643 X509_free(chaincert);
644 SSL_free(serverssl);
645 SSL_free(clientssl);
646 SSL_CTX_free(sctx);
647 SSL_CTX_free(cctx);
648
649 return testresult;
650 }
651
652 static int test_large_message_tls(void)
653 {
654 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
655 0);
656 }
657
658 static int test_large_message_tls_read_ahead(void)
659 {
660 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
661 1);
662 }
663
664 #ifndef OPENSSL_NO_DTLS
665 static int test_large_message_dtls(void)
666 {
667 /*
668 * read_ahead is not relevant to DTLS because DTLS always acts as if
669 * read_ahead is set.
670 */
671 return execute_test_large_message(DTLS_server_method(),
672 DTLS_client_method(), 0);
673 }
674 #endif
675
676 #ifndef OPENSSL_NO_OCSP
677 static int ocsp_server_cb(SSL *s, void *arg)
678 {
679 int *argi = (int *)arg;
680 unsigned char *orespdercopy = NULL;
681 STACK_OF(OCSP_RESPID) *ids = NULL;
682 OCSP_RESPID *id = NULL;
683
684 if (*argi == 2) {
685 /* In this test we are expecting exactly 1 OCSP_RESPID */
686 SSL_get_tlsext_status_ids(s, &ids);
687 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
688 return SSL_TLSEXT_ERR_ALERT_FATAL;
689
690 id = sk_OCSP_RESPID_value(ids, 0);
691 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
692 return SSL_TLSEXT_ERR_ALERT_FATAL;
693 } else if (*argi != 1) {
694 return SSL_TLSEXT_ERR_ALERT_FATAL;
695 }
696
697
698 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
699 if (orespdercopy == NULL)
700 return SSL_TLSEXT_ERR_ALERT_FATAL;
701
702 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
703
704 ocsp_server_called = 1;
705
706 return SSL_TLSEXT_ERR_OK;
707 }
708
709 static int ocsp_client_cb(SSL *s, void *arg)
710 {
711 int *argi = (int *)arg;
712 const unsigned char *respderin;
713 size_t len;
714
715 if (*argi != 1 && *argi != 2)
716 return 0;
717
718 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
719
720 if (memcmp(orespder, respderin, len) != 0)
721 return 0;
722
723 ocsp_client_called = 1;
724
725 return 1;
726 }
727
728 static int test_tlsext_status_type(void)
729 {
730 SSL_CTX *cctx = NULL, *sctx = NULL;
731 SSL *clientssl = NULL, *serverssl = NULL;
732 int testresult = 0;
733 STACK_OF(OCSP_RESPID) *ids = NULL;
734 OCSP_RESPID *id = NULL;
735 BIO *certbio = NULL;
736
737 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
738 &cctx, cert, privkey)) {
739 printf("Unable to create SSL_CTX pair\n");
740 return 0;
741 }
742
743 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
744 printf("Unexpected initial value for "
745 "SSL_CTX_get_tlsext_status_type()\n");
746 goto end;
747 }
748
749 /* First just do various checks getting and setting tlsext_status_type */
750
751 clientssl = SSL_new(cctx);
752 if (SSL_get_tlsext_status_type(clientssl) != -1) {
753 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
754 goto end;
755 }
756
757 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
758 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
759 goto end;
760 }
761
762 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
763 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
764 goto end;
765 }
766
767 SSL_free(clientssl);
768 clientssl = NULL;
769
770 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
771 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
772 goto end;
773 }
774
775 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
776 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
777 goto end;
778 }
779
780 clientssl = SSL_new(cctx);
781
782 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
783 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
784 goto end;
785 }
786
787 SSL_free(clientssl);
788 clientssl = NULL;
789
790 /*
791 * Now actually do a handshake and check OCSP information is exchanged and
792 * the callbacks get called
793 */
794
795 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
796 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
797 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
798 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
799
800 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
801 printf("Unable to create SSL objects\n");
802 goto end;
803 }
804
805 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
806 printf("Unable to create SSL connection\n");
807 goto end;
808 }
809
810 if (!ocsp_client_called || !ocsp_server_called) {
811 printf("OCSP callbacks not called\n");
812 goto end;
813 }
814
815 SSL_free(serverssl);
816 SSL_free(clientssl);
817 serverssl = NULL;
818 clientssl = NULL;
819
820 /* Try again but this time force the server side callback to fail */
821 ocsp_client_called = 0;
822 ocsp_server_called = 0;
823 cdummyarg = 0;
824
825 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
826 printf("Unable to create SSL objects\n");
827 goto end;
828 }
829
830 /* This should fail because the callback will fail */
831 if (create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
832 printf("Unexpected success creating the connection\n");
833 goto end;
834 }
835
836 if (ocsp_client_called || ocsp_server_called) {
837 printf("OCSP callbacks successfully called unexpectedly\n");
838 goto end;
839 }
840
841 SSL_free(serverssl);
842 SSL_free(clientssl);
843 serverssl = NULL;
844 clientssl = NULL;
845
846 /*
847 * This time we'll get the client to send an OCSP_RESPID that it will
848 * accept.
849 */
850 ocsp_client_called = 0;
851 ocsp_server_called = 0;
852 cdummyarg = 2;
853
854 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
855 printf("Unable to create SSL objects\n");
856 goto end;
857 }
858
859 /*
860 * We'll just use any old cert for this test - it doesn't have to be an OCSP
861 * specifc one. We'll use the server cert.
862 */
863 certbio = BIO_new_file(cert, "r");
864 if (certbio == NULL) {
865 printf("Can't load the certficate file\n");
866 goto end;
867 }
868 id = OCSP_RESPID_new();
869 ids = sk_OCSP_RESPID_new_null();
870 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
871 if (id == NULL || ids == NULL || ocspcert == NULL
872 || !OCSP_RESPID_set_by_key(id, ocspcert)
873 || !sk_OCSP_RESPID_push(ids, id)) {
874 printf("Unable to set OCSP_RESPIDs\n");
875 goto end;
876 }
877 id = NULL;
878 SSL_set_tlsext_status_ids(clientssl, ids);
879 /* Control has been transferred */
880 ids = NULL;
881
882 BIO_free(certbio);
883 certbio = NULL;
884
885 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
886 printf("Unable to create SSL connection\n");
887 goto end;
888 }
889
890 if (!ocsp_client_called || !ocsp_server_called) {
891 printf("OCSP callbacks not called\n");
892 goto end;
893 }
894
895 testresult = 1;
896
897 end:
898 SSL_free(serverssl);
899 SSL_free(clientssl);
900 SSL_CTX_free(sctx);
901 SSL_CTX_free(cctx);
902 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
903 OCSP_RESPID_free(id);
904 BIO_free(certbio);
905 X509_free(ocspcert);
906 ocspcert = NULL;
907
908 return testresult;
909 }
910 #endif
911
912 typedef struct ssl_session_test_fixture {
913 const char *test_case_name;
914 int use_ext_cache;
915 int use_int_cache;
916 } SSL_SESSION_TEST_FIXTURE;
917
918 static int new_called = 0, remove_called = 0;
919
920 static SSL_SESSION_TEST_FIXTURE
921 ssl_session_set_up(const char *const test_case_name)
922 {
923 SSL_SESSION_TEST_FIXTURE fixture;
924
925 fixture.test_case_name = test_case_name;
926 fixture.use_ext_cache = 1;
927 fixture.use_int_cache = 1;
928
929 new_called = remove_called = 0;
930
931 return fixture;
932 }
933
934 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
935 {
936 }
937
938 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
939 {
940 new_called++;
941
942 return 1;
943 }
944
945 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
946 {
947 remove_called++;
948 }
949
950 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
951 {
952 SSL_CTX *sctx = NULL, *cctx = NULL;
953 SSL *serverssl1 = NULL, *clientssl1 = NULL;
954 SSL *serverssl2 = NULL, *clientssl2 = NULL;
955 #ifndef OPENSSL_NO_TLS1_1
956 SSL *serverssl3 = NULL, *clientssl3 = NULL;
957 #endif
958 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
959 int testresult = 0;
960
961 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
962 &cctx, cert, privkey)) {
963 printf("Unable to create SSL_CTX pair\n");
964 return 0;
965 }
966
967 #ifndef OPENSSL_NO_TLS1_2
968 /* Only allow TLS1.2 so we can force a connection failure later */
969 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
970 #endif
971
972 /* Set up session cache */
973 if (fix.use_ext_cache) {
974 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
975 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
976 }
977 if (fix.use_int_cache) {
978 /* Also covers instance where both are set */
979 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
980 } else {
981 SSL_CTX_set_session_cache_mode(cctx,
982 SSL_SESS_CACHE_CLIENT
983 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
984 }
985
986 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
987 NULL)) {
988 printf("Unable to create SSL objects\n");
989 goto end;
990 }
991
992 if (!create_ssl_connection(serverssl1, clientssl1, SSL_ERROR_NONE)) {
993 printf("Unable to create SSL connection\n");
994 goto end;
995 }
996 sess1 = SSL_get1_session(clientssl1);
997 if (sess1 == NULL) {
998 printf("Unexpected NULL session\n");
999 goto end;
1000 }
1001
1002 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
1003 /* Should have failed because it should already be in the cache */
1004 printf("Unexpected success adding session to cache\n");
1005 goto end;
1006 }
1007
1008 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
1009 printf("Session not added to cache\n");
1010 goto end;
1011 }
1012
1013 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
1014 printf("Unable to create second SSL objects\n");
1015 goto end;
1016 }
1017
1018 if (!create_ssl_connection(serverssl2, clientssl2, SSL_ERROR_NONE)) {
1019 printf("Unable to create second SSL connection\n");
1020 goto end;
1021 }
1022
1023 sess2 = SSL_get1_session(clientssl2);
1024 if (sess2 == NULL) {
1025 printf("Unexpected NULL session from clientssl2\n");
1026 goto end;
1027 }
1028
1029 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
1030 printf("Remove session callback unexpectedly called\n");
1031 goto end;
1032 }
1033
1034 /*
1035 * This should clear sess2 from the cache because it is a "bad" session. See
1036 * SSL_set_session() documentation.
1037 */
1038 if (!SSL_set_session(clientssl2, sess1)) {
1039 printf("Unexpected failure setting session\n");
1040 goto end;
1041 }
1042
1043 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
1044 printf("Failed to call callback to remove session\n");
1045 goto end;
1046 }
1047
1048
1049 if (SSL_get_session(clientssl2) != sess1) {
1050 printf("Unexpected session found\n");
1051 goto end;
1052 }
1053
1054 if (fix.use_int_cache) {
1055 if (!SSL_CTX_add_session(cctx, sess2)) {
1056 /*
1057 * Should have succeeded because it should not already be in the cache
1058 */
1059 printf("Unexpected failure adding session to cache\n");
1060 goto end;
1061 }
1062
1063 if (!SSL_CTX_remove_session(cctx, sess2)) {
1064 printf("Unexpected failure removing session from cache\n");
1065 goto end;
1066 }
1067
1068 /* This is for the purposes of internal cache testing...ignore the
1069 * counter for external cache
1070 */
1071 if (fix.use_ext_cache)
1072 remove_called--;
1073 }
1074
1075 /* This shouldn't be in the cache so should fail */
1076 if (SSL_CTX_remove_session(cctx, sess2)) {
1077 printf("Unexpected success removing session from cache\n");
1078 goto end;
1079 }
1080
1081 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
1082 printf("Failed to call callback to remove session #2\n");
1083 goto end;
1084 }
1085
1086 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
1087 /* Force a connection failure */
1088 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
1089
1090 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
1091 printf("Unable to create third SSL objects\n");
1092 goto end;
1093 }
1094
1095 if (!SSL_set_session(clientssl3, sess1)) {
1096 printf("Unable to set session for third connection\n");
1097 goto end;
1098 }
1099
1100 /* This should fail because of the mismatched protocol versions */
1101 if (create_ssl_connection(serverssl3, clientssl3, SSL_ERROR_NONE)) {
1102 printf("Unable to create third SSL connection\n");
1103 goto end;
1104 }
1105
1106
1107 /* We should have automatically removed the session from the cache */
1108 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
1109 printf("Failed to call callback to remove session #2\n");
1110 goto end;
1111 }
1112
1113 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
1114 /*
1115 * Should have succeeded because it should not already be in the cache
1116 */
1117 printf("Unexpected failure adding session to cache #2\n");
1118 goto end;
1119 }
1120 #endif
1121
1122 testresult = 1;
1123
1124 end:
1125 SSL_free(serverssl1);
1126 SSL_free(clientssl1);
1127 SSL_free(serverssl2);
1128 SSL_free(clientssl2);
1129 #ifndef OPENSSL_NO_TLS1_1
1130 SSL_free(serverssl3);
1131 SSL_free(clientssl3);
1132 #endif
1133 SSL_SESSION_free(sess1);
1134 SSL_SESSION_free(sess2);
1135 /*
1136 * Check if we need to remove any sessions up-refed for the external cache
1137 */
1138 if (new_called >= 1)
1139 SSL_SESSION_free(sess1);
1140 if (new_called >= 2)
1141 SSL_SESSION_free(sess2);
1142 SSL_CTX_free(sctx);
1143 SSL_CTX_free(cctx);
1144
1145 return testresult;
1146 }
1147
1148 static int test_session_with_only_int_cache(void)
1149 {
1150 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
1151
1152 fixture.use_ext_cache = 0;
1153
1154 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
1155 }
1156
1157 static int test_session_with_only_ext_cache(void)
1158 {
1159 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
1160
1161 fixture.use_int_cache = 0;
1162
1163 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
1164 }
1165
1166 static int test_session_with_both_cache(void)
1167 {
1168 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
1169
1170 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
1171 }
1172
1173 #define USE_NULL 0
1174 #define USE_BIO_1 1
1175 #define USE_BIO_2 2
1176
1177 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1178
1179 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1180 {
1181 switch (type) {
1182 case USE_NULL:
1183 *res = NULL;
1184 break;
1185 case USE_BIO_1:
1186 *res = bio1;
1187 break;
1188 case USE_BIO_2:
1189 *res = bio2;
1190 break;
1191 }
1192 }
1193
1194 static int test_ssl_set_bio(int idx)
1195 {
1196 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1197 BIO *bio1 = NULL;
1198 BIO *bio2 = NULL;
1199 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1200 SSL *ssl = NULL;
1201 int initrbio, initwbio, newrbio, newwbio;
1202 int testresult = 0;
1203
1204 if (ctx == NULL) {
1205 printf("Failed to allocate SSL_CTX\n");
1206 goto end;
1207 }
1208
1209 ssl = SSL_new(ctx);
1210 if (ssl == NULL) {
1211 printf("Failed to allocate SSL object\n");
1212 goto end;
1213 }
1214
1215 initrbio = idx % 3;
1216 idx /= 3;
1217 initwbio = idx % 3;
1218 idx /= 3;
1219 newrbio = idx % 3;
1220 idx /= 3;
1221 newwbio = idx;
1222 OPENSSL_assert(newwbio <= 2);
1223
1224 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
1225 || newwbio == USE_BIO_1) {
1226 bio1 = BIO_new(BIO_s_mem());
1227 if (bio1 == NULL) {
1228 printf("Failed to allocate bio1\n");
1229 goto end;
1230 }
1231 }
1232
1233 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
1234 || newwbio == USE_BIO_2) {
1235 bio2 = BIO_new(BIO_s_mem());
1236 if (bio2 == NULL) {
1237 printf("Failed to allocate bio2\n");
1238 goto end;
1239 }
1240 }
1241
1242 setupbio(&irbio, bio1, bio2, initrbio);
1243 setupbio(&iwbio, bio1, bio2, initwbio);
1244
1245 /*
1246 * We want to maintain our own refs to these BIO, so do an up ref for each
1247 * BIO that will have ownersip transferred in the SSL_set_bio() call
1248 */
1249 if (irbio != NULL)
1250 BIO_up_ref(irbio);
1251 if (iwbio != NULL && iwbio != irbio)
1252 BIO_up_ref(iwbio);
1253
1254 SSL_set_bio(ssl, irbio, iwbio);
1255
1256 setupbio(&nrbio, bio1, bio2, newrbio);
1257 setupbio(&nwbio, bio1, bio2, newwbio);
1258
1259 /*
1260 * We will (maybe) transfer ownership again so do more up refs.
1261 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1262 * already been set!
1263 */
1264 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
1265 BIO_up_ref(nrbio);
1266 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1267 BIO_up_ref(nwbio);
1268
1269 SSL_set_bio(ssl, nrbio, nwbio);
1270
1271 testresult = 1;
1272
1273 end:
1274 SSL_free(ssl);
1275 BIO_free(bio1);
1276 BIO_free(bio2);
1277 /*
1278 * This test is checking that the ref counting for SSL_set_bio is correct.
1279 * If we get here and we did too many frees then we will fail in the above
1280 * functions. If we haven't done enough then this will only be detected in
1281 * a crypto-mdebug build
1282 */
1283 SSL_CTX_free(ctx);
1284
1285 return testresult;
1286 }
1287
1288 typedef struct ssl_bio_test_fixture {
1289 const char *test_case_name;
1290 int pop_ssl;
1291 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1292 } SSL_BIO_TEST_FIXTURE;
1293
1294 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1295 {
1296 SSL_BIO_TEST_FIXTURE fixture;
1297
1298 fixture.test_case_name = test_case_name;
1299 fixture.pop_ssl = 0;
1300 fixture.change_bio = NO_BIO_CHANGE;
1301
1302 return fixture;
1303 }
1304
1305 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1306 {
1307 }
1308
1309 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1310 {
1311 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1312 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1313 SSL *ssl = NULL;
1314 int testresult = 0;
1315
1316 if (ctx == NULL) {
1317 printf("Failed to allocate SSL_CTX\n");
1318 return 0;
1319 }
1320
1321 ssl = SSL_new(ctx);
1322 if (ssl == NULL) {
1323 printf("Failed to allocate SSL object\n");
1324 goto end;
1325 }
1326
1327 sslbio = BIO_new(BIO_f_ssl());
1328 membio1 = BIO_new(BIO_s_mem());
1329
1330 if (sslbio == NULL || membio1 == NULL) {
1331 printf("Malloc failure creating BIOs\n");
1332 goto end;
1333 }
1334
1335 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1336
1337 /*
1338 * If anything goes wrong here then we could leak memory, so this will
1339 * be caught in a crypto-mdebug build
1340 */
1341 BIO_push(sslbio, membio1);
1342
1343 /* Verify chaning the rbio/wbio directly does not cause leaks */
1344 if (fix.change_bio != NO_BIO_CHANGE) {
1345 membio2 = BIO_new(BIO_s_mem());
1346 if (membio2 == NULL) {
1347 printf("Malloc failure creating membio2\n");
1348 goto end;
1349 }
1350 if (fix.change_bio == CHANGE_RBIO)
1351 SSL_set0_rbio(ssl, membio2);
1352 else
1353 SSL_set0_wbio(ssl, membio2);
1354 }
1355 ssl = NULL;
1356
1357 if (fix.pop_ssl)
1358 BIO_pop(sslbio);
1359 else
1360 BIO_pop(membio1);
1361
1362 testresult = 1;
1363 end:
1364 BIO_free(membio1);
1365 BIO_free(sslbio);
1366 SSL_free(ssl);
1367 SSL_CTX_free(ctx);
1368
1369 return testresult;
1370 }
1371
1372 static int test_ssl_bio_pop_next_bio(void)
1373 {
1374 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1375
1376 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1377 }
1378
1379 static int test_ssl_bio_pop_ssl_bio(void)
1380 {
1381 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1382
1383 fixture.pop_ssl = 1;
1384
1385 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1386 }
1387
1388 static int test_ssl_bio_change_rbio(void)
1389 {
1390 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1391
1392 fixture.change_bio = CHANGE_RBIO;
1393
1394 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1395 }
1396
1397 static int test_ssl_bio_change_wbio(void)
1398 {
1399 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1400
1401 fixture.change_bio = CHANGE_WBIO;
1402
1403 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1404 }
1405
1406 typedef struct {
1407 /* The list of sig algs */
1408 const int *list;
1409 /* The length of the list */
1410 size_t listlen;
1411 /* A sigalgs list in string format */
1412 const char *liststr;
1413 /* Whether setting the list should succeed */
1414 int valid;
1415 /* Whether creating a connection with the list should succeed */
1416 int connsuccess;
1417 } sigalgs_list;
1418
1419 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1420 #ifndef OPENSSL_NO_EC
1421 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1422 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1423 #endif
1424 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1425 static const int invalidlist2[] = {NID_sha256, NID_undef};
1426 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1427 static const int invalidlist4[] = {NID_sha256};
1428 static const sigalgs_list testsigalgs[] = {
1429 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1430 #ifndef OPENSSL_NO_EC
1431 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1432 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1433 #endif
1434 {NULL, 0, "RSA+SHA256", 1, 1},
1435 #ifndef OPENSSL_NO_EC
1436 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1437 {NULL, 0, "ECDSA+SHA512", 1, 0},
1438 #endif
1439 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1440 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1441 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1442 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1443 {NULL, 0, "RSA", 0, 0},
1444 {NULL, 0, "SHA256", 0, 0},
1445 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1446 {NULL, 0, "Invalid", 0, 0}};
1447
1448 static int test_set_sigalgs(int idx)
1449 {
1450 SSL_CTX *cctx = NULL, *sctx = NULL;
1451 SSL *clientssl = NULL, *serverssl = NULL;
1452 int testresult = 0;
1453 const sigalgs_list *curr;
1454 int testctx;
1455
1456 /* Should never happen */
1457 if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
1458 return 0;
1459
1460 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1461 curr = testctx ? &testsigalgs[idx]
1462 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1463
1464 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
1465 &cctx, cert, privkey)) {
1466 printf("Unable to create SSL_CTX pair\n");
1467 return 0;
1468 }
1469
1470 /*
1471 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1472 * for TLSv1.2 for now until we add a new API.
1473 */
1474 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1475
1476 if (testctx) {
1477 int ret;
1478 if (curr->list != NULL)
1479 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1480 else
1481 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1482
1483 if (!ret) {
1484 if (curr->valid)
1485 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1486 idx);
1487 else
1488 testresult = 1;
1489 goto end;
1490 }
1491 if (!curr->valid) {
1492 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
1493 goto end;
1494 }
1495 }
1496
1497 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
1498 printf("Unable to create SSL objects\n");
1499 goto end;
1500 }
1501
1502 if (!testctx) {
1503 int ret;
1504
1505 if (curr->list != NULL)
1506 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1507 else
1508 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1509 if (!ret) {
1510 if (curr->valid)
1511 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
1512 else
1513 testresult = 1;
1514 goto end;
1515 }
1516 if (!curr->valid) {
1517 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
1518 goto end;
1519 }
1520 }
1521
1522 if (curr->connsuccess != create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
1523 printf("Unexpected return value creating SSL connection (%d)\n", idx);
1524 goto end;
1525 }
1526
1527 testresult = 1;
1528
1529 end:
1530 SSL_free(serverssl);
1531 SSL_free(clientssl);
1532 SSL_CTX_free(sctx);
1533 SSL_CTX_free(cctx);
1534
1535 return testresult;
1536 }
1537
1538 int test_main(int argc, char *argv[])
1539 {
1540 int testresult = 1;
1541
1542 if (argc != 3) {
1543 printf("Invalid argument count\n");
1544 return 1;
1545 }
1546
1547 cert = argv[1];
1548 privkey = argv[2];
1549
1550 ADD_TEST(test_large_message_tls);
1551 ADD_TEST(test_large_message_tls_read_ahead);
1552 #ifndef OPENSSL_NO_DTLS
1553 ADD_TEST(test_large_message_dtls);
1554 #endif
1555 #ifndef OPENSSL_NO_OCSP
1556 ADD_TEST(test_tlsext_status_type);
1557 #endif
1558 ADD_TEST(test_session_with_only_int_cache);
1559 ADD_TEST(test_session_with_only_ext_cache);
1560 ADD_TEST(test_session_with_both_cache);
1561 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1562 ADD_TEST(test_ssl_bio_pop_next_bio);
1563 ADD_TEST(test_ssl_bio_pop_ssl_bio);
1564 ADD_TEST(test_ssl_bio_change_rbio);
1565 ADD_TEST(test_ssl_bio_change_wbio);
1566 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1567 ADD_TEST(test_keylog);
1568 #ifndef OPENSSL_NO_TLS1_3
1569 ADD_TEST(test_keylog_no_master_key);
1570 #endif
1571 ADD_TEST(test_early_cb);
1572
1573 testresult = run_tests(argv[0]);
1574
1575 bio_s_mempacket_test_free();
1576
1577 return testresult;
1578 }