Don't run the sigalgs tests over a TLSv1.3 connection
[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 static const unsigned char orespder[] = "Dummy OCSP Response";
27 static int ocsp_server_called = 0;
28 static int ocsp_client_called = 0;
29
30 static int cdummyarg = 1;
31 static X509 *ocspcert = NULL;
32
33 #define NUM_EXTRA_CERTS 40
34
35 static int execute_test_large_message(const SSL_METHOD *smeth,
36                                       const SSL_METHOD *cmeth, int read_ahead)
37 {
38     SSL_CTX *cctx = NULL, *sctx = NULL;
39     SSL *clientssl = NULL, *serverssl = NULL;
40     int testresult = 0;
41     int i;
42     BIO *certbio = BIO_new_file(cert, "r");
43     X509 *chaincert = NULL;
44     int certlen;
45
46     if (certbio == NULL) {
47         printf("Can't load the certficate file\n");
48         goto end;
49     }
50     chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
51     BIO_free(certbio);
52     certbio = NULL;
53     if (chaincert == NULL) {
54         printf("Unable to load certificate for chain\n");
55         goto end;
56     }
57
58     if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
59                              &cctx, cert, privkey)) {
60         printf("Unable to create SSL_CTX pair\n");
61         goto end;
62     }
63
64     if(read_ahead) {
65         /*
66          * Test that read_ahead works correctly when dealing with large
67          * records
68          */
69         SSL_CTX_set_read_ahead(cctx, 1);
70     }
71
72     /*
73      * We assume the supplied certificate is big enough so that if we add
74      * NUM_EXTRA_CERTS it will make the overall message large enough. The
75      * default buffer size is requested to be 16k, but due to the way BUF_MEM
76      * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
77      * we need to have a message larger than that.
78      */
79     certlen = i2d_X509(chaincert, NULL);
80     OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
81                    > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
82     for (i = 0; i < NUM_EXTRA_CERTS; i++) {
83         if (!X509_up_ref(chaincert)) {
84             printf("Unable to up ref cert\n");
85             goto end;
86         }
87         if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
88             printf("Unable to add extra chain cert %d\n", i);
89             X509_free(chaincert);
90             goto end;
91         }
92     }
93
94     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
95         printf("Unable to create SSL objects\n");
96         goto end;
97     }
98
99     if (!create_ssl_connection(serverssl, clientssl)) {
100         printf("Unable to create SSL connection\n");
101         goto end;
102     }
103
104     /*
105      * Calling SSL_clear() first is not required but this tests that SSL_clear()
106      * doesn't leak (when using enable-crypto-mdebug).
107      */
108     if (!SSL_clear(serverssl)) {
109         printf("Unexpected failure from SSL_clear()\n");
110         goto end;
111     }
112
113     testresult = 1;
114  end:
115     X509_free(chaincert);
116     SSL_free(serverssl);
117     SSL_free(clientssl);
118     SSL_CTX_free(sctx);
119     SSL_CTX_free(cctx);
120
121     return testresult;
122 }
123
124 static int test_large_message_tls(void)
125 {
126     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
127                                       0);
128 }
129
130 static int test_large_message_tls_read_ahead(void)
131 {
132     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
133                                       1);
134 }
135
136 #ifndef OPENSSL_NO_DTLS
137 static int test_large_message_dtls(void)
138 {
139     /*
140      * read_ahead is not relevant to DTLS because DTLS always acts as if
141      * read_ahead is set.
142      */
143     return execute_test_large_message(DTLS_server_method(),
144                                       DTLS_client_method(), 0);
145 }
146 #endif
147
148 static int ocsp_server_cb(SSL *s, void *arg)
149 {
150     int *argi = (int *)arg;
151     unsigned char *orespdercopy = NULL;
152     STACK_OF(OCSP_RESPID) *ids = NULL;
153     OCSP_RESPID *id = NULL;
154
155     if (*argi == 2) {
156         /* In this test we are expecting exactly 1 OCSP_RESPID */
157         SSL_get_tlsext_status_ids(s, &ids);
158         if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
159             return SSL_TLSEXT_ERR_ALERT_FATAL;
160
161         id = sk_OCSP_RESPID_value(ids, 0);
162         if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
163             return SSL_TLSEXT_ERR_ALERT_FATAL;
164     } else if (*argi != 1) {
165         return SSL_TLSEXT_ERR_ALERT_FATAL;
166     }
167
168
169     orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
170     if (orespdercopy == NULL)
171         return SSL_TLSEXT_ERR_ALERT_FATAL;
172
173     SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
174
175     ocsp_server_called = 1;
176
177     return SSL_TLSEXT_ERR_OK;
178 }
179
180 static int ocsp_client_cb(SSL *s, void *arg)
181 {
182     int *argi = (int *)arg;
183     const unsigned char *respderin;
184     size_t len;
185
186     if (*argi != 1 && *argi != 2)
187         return 0;
188
189     len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
190
191     if (memcmp(orespder, respderin, len) != 0)
192         return 0;
193
194     ocsp_client_called = 1;
195
196     return 1;
197 }
198
199 static int test_tlsext_status_type(void)
200 {
201     SSL_CTX *cctx = NULL, *sctx = NULL;
202     SSL *clientssl = NULL, *serverssl = NULL;
203     int testresult = 0;
204     STACK_OF(OCSP_RESPID) *ids = NULL;
205     OCSP_RESPID *id = NULL;
206     BIO *certbio = NULL;
207
208     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
209                              &cctx, cert, privkey)) {
210         printf("Unable to create SSL_CTX pair\n");
211         return 0;
212     }
213
214     if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
215         printf("Unexpected initial value for "
216                "SSL_CTX_get_tlsext_status_type()\n");
217         goto end;
218     }
219
220     /* First just do various checks getting and setting tlsext_status_type */
221
222     clientssl = SSL_new(cctx);
223     if (SSL_get_tlsext_status_type(clientssl) != -1) {
224         printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
225         goto end;
226     }
227
228     if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
229         printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
230         goto end;
231     }
232
233     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
234         printf("Unexpected result for SSL_get_tlsext_status_type()\n");
235         goto end;
236     }
237
238     SSL_free(clientssl);
239     clientssl = NULL;
240
241     if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
242         printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
243         goto end;
244     }
245
246     if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
247         printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
248         goto end;
249     }
250
251     clientssl = SSL_new(cctx);
252
253     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
254         printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
255         goto end;
256     }
257
258     SSL_free(clientssl);
259     clientssl = NULL;
260
261     /*
262      * Now actually do a handshake and check OCSP information is exchanged and
263      * the callbacks get called
264      */
265
266     SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
267     SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
268     SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
269     SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
270
271     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
272         printf("Unable to create SSL objects\n");
273         goto end;
274     }
275
276     if (!create_ssl_connection(serverssl, clientssl)) {
277         printf("Unable to create SSL connection\n");
278         goto end;
279     }
280
281     if (!ocsp_client_called || !ocsp_server_called) {
282         printf("OCSP callbacks not called\n");
283         goto end;
284     }
285
286     SSL_free(serverssl);
287     SSL_free(clientssl);
288     serverssl = NULL;
289     clientssl = NULL;
290
291     /* Try again but this time force the server side callback to fail */
292     ocsp_client_called = 0;
293     ocsp_server_called = 0;
294     cdummyarg = 0;
295
296     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
297         printf("Unable to create SSL objects\n");
298         goto end;
299     }
300
301     /* This should fail because the callback will fail */
302     if (create_ssl_connection(serverssl, clientssl)) {
303         printf("Unexpected success creating the connection\n");
304         goto end;
305     }
306
307     if (ocsp_client_called || ocsp_server_called) {
308         printf("OCSP callbacks successfully called unexpectedly\n");
309         goto end;
310     }
311
312     SSL_free(serverssl);
313     SSL_free(clientssl);
314     serverssl = NULL;
315     clientssl = NULL;
316
317     /*
318      * This time we'll get the client to send an OCSP_RESPID that it will
319      * accept.
320      */
321     ocsp_client_called = 0;
322     ocsp_server_called = 0;
323     cdummyarg = 2;
324
325     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
326         printf("Unable to create SSL objects\n");
327         goto end;
328     }
329
330     /*
331      * We'll just use any old cert for this test - it doesn't have to be an OCSP
332      * specifc one. We'll use the server cert.
333      */
334     certbio = BIO_new_file(cert, "r");
335     if (certbio == NULL) {
336         printf("Can't load the certficate file\n");
337         goto end;
338     }
339     id = OCSP_RESPID_new();
340     ids = sk_OCSP_RESPID_new_null();
341     ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
342     if (id == NULL || ids == NULL || ocspcert == NULL
343             || !OCSP_RESPID_set_by_key(id, ocspcert)
344             || !sk_OCSP_RESPID_push(ids, id)) {
345         printf("Unable to set OCSP_RESPIDs\n");
346         goto end;
347     }
348     id = NULL;
349     SSL_set_tlsext_status_ids(clientssl, ids);
350     /* Control has been transferred */
351     ids = NULL;
352
353     BIO_free(certbio);
354     certbio = NULL;
355
356     if (!create_ssl_connection(serverssl, clientssl)) {
357         printf("Unable to create SSL connection\n");
358         goto end;
359     }
360
361     if (!ocsp_client_called || !ocsp_server_called) {
362         printf("OCSP callbacks not called\n");
363         goto end;
364     }
365
366     testresult = 1;
367
368  end:
369     SSL_free(serverssl);
370     SSL_free(clientssl);
371     SSL_CTX_free(sctx);
372     SSL_CTX_free(cctx);
373     sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
374     OCSP_RESPID_free(id);
375     BIO_free(certbio);
376     X509_free(ocspcert);
377     ocspcert = NULL;
378
379     return testresult;
380 }
381
382 typedef struct ssl_session_test_fixture {
383     const char *test_case_name;
384     int use_ext_cache;
385     int use_int_cache;
386 } SSL_SESSION_TEST_FIXTURE;
387
388 static int new_called = 0, remove_called = 0;
389
390 static SSL_SESSION_TEST_FIXTURE
391 ssl_session_set_up(const char *const test_case_name)
392 {
393     SSL_SESSION_TEST_FIXTURE fixture;
394
395     fixture.test_case_name = test_case_name;
396     fixture.use_ext_cache = 1;
397     fixture.use_int_cache = 1;
398
399     new_called = remove_called = 0;
400
401     return fixture;
402 }
403
404 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
405 {
406 }
407
408 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
409 {
410     new_called++;
411
412     return 1;
413 }
414
415 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
416 {
417     remove_called++;
418 }
419
420 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
421 {
422     SSL_CTX *sctx = NULL, *cctx = NULL;
423     SSL *serverssl1 = NULL, *clientssl1 = NULL;
424     SSL *serverssl2 = NULL, *clientssl2 = NULL;
425 #ifndef OPENSSL_NO_TLS1_1
426     SSL *serverssl3 = NULL, *clientssl3 = NULL;
427 #endif
428     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
429     int testresult = 0;
430
431     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
432                              &cctx, cert, privkey)) {
433         printf("Unable to create SSL_CTX pair\n");
434         return 0;
435     }
436
437 #ifndef OPENSSL_NO_TLS1_2
438     /* Only allow TLS1.2 so we can force a connection failure later */
439     SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
440 #endif
441
442     /*
443      * TODO(TLS1.3): Test temporarily disabled for TLS1.3 until we've
444      * implemented session resumption.
445      */
446     SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
447
448     /* Set up session cache */
449     if (fix.use_ext_cache) {
450         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
451         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
452     }
453     if (fix.use_int_cache) {
454         /* Also covers instance where both are set */
455         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
456     } else {
457         SSL_CTX_set_session_cache_mode(cctx,
458                                        SSL_SESS_CACHE_CLIENT
459                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
460     }
461
462     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
463                                NULL)) {
464         printf("Unable to create SSL objects\n");
465         goto end;
466     }
467
468     if (!create_ssl_connection(serverssl1, clientssl1)) {
469         printf("Unable to create SSL connection\n");
470         goto end;
471     }
472     sess1 = SSL_get1_session(clientssl1);
473     if (sess1 == NULL) {
474         printf("Unexpected NULL session\n");
475         goto end;
476     }
477
478     if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
479         /* Should have failed because it should already be in the cache */
480         printf("Unexpected success adding session to cache\n");
481         goto end;
482     }
483
484     if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
485         printf("Session not added to cache\n");
486         goto end;
487     }
488
489     if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
490         printf("Unable to create second SSL objects\n");
491         goto end;
492     }
493
494     if (!create_ssl_connection(serverssl2, clientssl2)) {
495         printf("Unable to create second SSL connection\n");
496         goto end;
497     }
498
499     sess2 = SSL_get1_session(clientssl2);
500     if (sess2 == NULL) {
501         printf("Unexpected NULL session from clientssl2\n");
502         goto end;
503     }
504
505     if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
506         printf("Remove session callback unexpectedly called\n");
507         goto end;
508     }
509
510     /*
511      * This should clear sess2 from the cache because it is a "bad" session. See
512      * SSL_set_session() documentation.
513      */
514     if (!SSL_set_session(clientssl2, sess1)) {
515         printf("Unexpected failure setting session\n");
516         goto end;
517     }
518
519     if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
520         printf("Failed to call callback to remove session\n");
521         goto end;
522     }
523
524
525     if (SSL_get_session(clientssl2) != sess1) {
526         printf("Unexpected session found\n");
527         goto end;
528     }
529
530     if (fix.use_int_cache) {
531         if (!SSL_CTX_add_session(cctx, sess2)) {
532             /*
533              * Should have succeeded because it should not already be in the cache
534              */
535             printf("Unexpected failure adding session to cache\n");
536             goto end;
537         }
538
539         if (!SSL_CTX_remove_session(cctx, sess2)) {
540             printf("Unexpected failure removing session from cache\n");
541             goto end;
542         }
543
544         /* This is for the purposes of internal cache testing...ignore the
545          * counter for external cache
546          */
547         if (fix.use_ext_cache)
548             remove_called--;
549     }
550
551     /* This shouldn't be in the cache so should fail */
552     if (SSL_CTX_remove_session(cctx, sess2)) {
553         printf("Unexpected success removing session from cache\n");
554         goto end;
555     }
556
557     if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
558         printf("Failed to call callback to remove session #2\n");
559         goto end;
560     }
561
562 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
563     /* Force a connection failure */
564     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
565
566     if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
567         printf("Unable to create third SSL objects\n");
568         goto end;
569     }
570
571     if (!SSL_set_session(clientssl3, sess1)) {
572         printf("Unable to set session for third connection\n");
573         goto end;
574     }
575
576     /* This should fail because of the mismatched protocol versions */
577     if (create_ssl_connection(serverssl3, clientssl3)) {
578         printf("Unable to create third SSL connection\n");
579         goto end;
580     }
581
582
583     /* We should have automatically removed the session from the cache */
584     if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
585         printf("Failed to call callback to remove session #2\n");
586         goto end;
587     }
588
589     if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
590         /*
591          * Should have succeeded because it should not already be in the cache
592          */
593         printf("Unexpected failure adding session to cache #2\n");
594         goto end;
595     }
596 #endif
597
598     testresult = 1;
599
600  end:
601     SSL_free(serverssl1);
602     SSL_free(clientssl1);
603     SSL_free(serverssl2);
604     SSL_free(clientssl2);
605 #ifndef OPENSSL_NO_TLS1_1
606     SSL_free(serverssl3);
607     SSL_free(clientssl3);
608 #endif
609     SSL_SESSION_free(sess1);
610     SSL_SESSION_free(sess2);
611     /*
612      * Check if we need to remove any sessions up-refed for the external cache
613      */
614     if (new_called >= 1)
615         SSL_SESSION_free(sess1);
616     if (new_called >= 2)
617         SSL_SESSION_free(sess2);
618     SSL_CTX_free(sctx);
619     SSL_CTX_free(cctx);
620
621     return testresult;
622 }
623
624 static int test_session_with_only_int_cache(void)
625 {
626     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
627
628     fixture.use_ext_cache = 0;
629
630     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
631 }
632
633 static int test_session_with_only_ext_cache(void)
634 {
635     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
636
637     fixture.use_int_cache = 0;
638
639     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
640 }
641
642 static int test_session_with_both_cache(void)
643 {
644     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
645
646     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
647 }
648
649 #define USE_NULL    0
650 #define USE_BIO_1   1
651 #define USE_BIO_2   2
652
653 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
654
655 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
656 {
657     switch (type) {
658     case USE_NULL:
659         *res = NULL;
660         break;
661     case USE_BIO_1:
662         *res = bio1;
663         break;
664     case USE_BIO_2:
665         *res = bio2;
666         break;
667     }
668 }
669
670 static int test_ssl_set_bio(int idx)
671 {
672     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
673     BIO *bio1 = NULL;
674     BIO *bio2 = NULL;
675     BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
676     SSL *ssl = NULL;
677     int initrbio, initwbio, newrbio, newwbio;
678     int testresult = 0;
679
680     if (ctx == NULL) {
681         printf("Failed to allocate SSL_CTX\n");
682         goto end;
683     }
684
685     ssl = SSL_new(ctx);
686     if (ssl == NULL) {
687         printf("Failed to allocate SSL object\n");
688         goto end;
689     }
690
691     initrbio = idx % 3;
692     idx /= 3;
693     initwbio = idx % 3;
694     idx /= 3;
695     newrbio = idx % 3;
696     idx /= 3;
697     newwbio = idx;
698     OPENSSL_assert(newwbio <= 2);
699
700     if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
701             || newwbio == USE_BIO_1) {
702         bio1 = BIO_new(BIO_s_mem());
703         if (bio1 == NULL) {
704             printf("Failed to allocate bio1\n");
705             goto end;
706         }
707     }
708
709     if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
710             || newwbio == USE_BIO_2) {
711         bio2 = BIO_new(BIO_s_mem());
712         if (bio2 == NULL) {
713             printf("Failed to allocate bio2\n");
714             goto end;
715         }
716     }
717
718     setupbio(&irbio, bio1, bio2, initrbio);
719     setupbio(&iwbio, bio1, bio2, initwbio);
720
721     /*
722      * We want to maintain our own refs to these BIO, so do an up ref for each
723      * BIO that will have ownersip transferred in the SSL_set_bio() call
724      */
725     if (irbio != NULL)
726         BIO_up_ref(irbio);
727     if (iwbio != NULL && iwbio != irbio)
728         BIO_up_ref(iwbio);
729
730     SSL_set_bio(ssl, irbio, iwbio);
731
732     setupbio(&nrbio, bio1, bio2, newrbio);
733     setupbio(&nwbio, bio1, bio2, newwbio);
734
735     /*
736      * We will (maybe) transfer ownership again so do more up refs.
737      * SSL_set_bio() has some really complicated ownership rules where BIOs have
738      * already been set!
739      */
740     if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
741         BIO_up_ref(nrbio);
742     if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
743         BIO_up_ref(nwbio);
744
745     SSL_set_bio(ssl, nrbio, nwbio);
746
747     testresult = 1;
748
749  end:
750     SSL_free(ssl);
751     BIO_free(bio1);
752     BIO_free(bio2);
753     /*
754      * This test is checking that the ref counting for SSL_set_bio is correct.
755      * If we get here and we did too many frees then we will fail in the above
756      * functions. If we haven't done enough then this will only be detected in
757      * a crypto-mdebug build
758      */
759     SSL_CTX_free(ctx);
760
761     return testresult;
762 }
763
764 typedef struct ssl_bio_test_fixture {
765     const char *test_case_name;
766     int pop_ssl;
767     enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
768 } SSL_BIO_TEST_FIXTURE;
769
770 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
771 {
772     SSL_BIO_TEST_FIXTURE fixture;
773
774     fixture.test_case_name = test_case_name;
775     fixture.pop_ssl = 0;
776     fixture.change_bio = NO_BIO_CHANGE;
777
778     return fixture;
779 }
780
781 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
782 {
783 }
784
785 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
786 {
787     BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
788     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
789     SSL *ssl = NULL;
790     int testresult = 0;
791
792     if (ctx == NULL) {
793         printf("Failed to allocate SSL_CTX\n");
794         return 0;
795     }
796
797     ssl = SSL_new(ctx);
798     if (ssl == NULL) {
799         printf("Failed to allocate SSL object\n");
800         goto end;
801     }
802
803     sslbio = BIO_new(BIO_f_ssl());
804     membio1 = BIO_new(BIO_s_mem());
805
806     if (sslbio == NULL || membio1 == NULL) {
807         printf("Malloc failure creating BIOs\n");
808         goto end;
809     }
810
811     BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
812
813     /*
814      * If anything goes wrong here then we could leak memory, so this will
815      * be caught in a crypto-mdebug build
816      */
817     BIO_push(sslbio, membio1);
818
819     /* Verify chaning the rbio/wbio directly does not cause leaks */
820     if (fix.change_bio != NO_BIO_CHANGE) {
821         membio2 = BIO_new(BIO_s_mem());
822         if (membio2 == NULL) {
823             printf("Malloc failure creating membio2\n");
824             goto end;
825         }
826         if (fix.change_bio == CHANGE_RBIO)
827             SSL_set0_rbio(ssl, membio2);
828         else
829             SSL_set0_wbio(ssl, membio2);
830     }
831     ssl = NULL;
832
833     if (fix.pop_ssl)
834         BIO_pop(sslbio);
835     else
836         BIO_pop(membio1);
837
838     testresult = 1;
839  end:
840     BIO_free(membio1);
841     BIO_free(sslbio);
842     SSL_free(ssl);
843     SSL_CTX_free(ctx);
844
845     return testresult;
846 }
847
848 static int test_ssl_bio_pop_next_bio(void)
849 {
850     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
851
852     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
853 }
854
855 static int test_ssl_bio_pop_ssl_bio(void)
856 {
857     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
858
859     fixture.pop_ssl = 1;
860
861     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
862 }
863
864 static int test_ssl_bio_change_rbio(void)
865 {
866     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
867
868     fixture.change_bio = CHANGE_RBIO;
869
870     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
871 }
872
873 static int test_ssl_bio_change_wbio(void)
874 {
875     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
876
877     fixture.change_bio = CHANGE_WBIO;
878
879     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
880 }
881
882 typedef struct {
883     /* The list of sig algs */
884     const int *list;
885     /* The length of the list */
886     size_t listlen;
887     /* A sigalgs list in string format */
888     const char *liststr;
889     /* Whether setting the list should succeed */
890     int valid;
891     /* Whether creating a connection with the list should succeed */
892     int connsuccess;
893 } sigalgs_list;
894
895 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
896 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
897 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
898 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
899 static const int invalidlist2[] = {NID_sha256, NID_undef};
900 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
901 static const int invalidlist4[] = {NID_sha256};
902 static const sigalgs_list testsigalgs[] = {
903     {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
904     {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
905     {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
906     {NULL, 0, "RSA+SHA256", 1, 1},
907     {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
908     {NULL, 0, "ECDSA+SHA512", 1, 0},
909     {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
910     {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
911     {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
912     {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
913     {NULL, 0, "RSA", 0, 0},
914     {NULL, 0, "SHA256", 0, 0},
915     {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
916     {NULL, 0, "Invalid", 0, 0}};
917
918 static int test_set_sigalgs(int idx)
919 {
920     SSL_CTX *cctx = NULL, *sctx = NULL;
921     SSL *clientssl = NULL, *serverssl = NULL;
922     int testresult = 0;
923     const sigalgs_list *curr;
924     int testctx;
925
926     /* Should never happen */
927     if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
928         return 0;
929
930     testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
931     curr = testctx ? &testsigalgs[idx]
932                    : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
933
934     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
935                              &cctx, cert, privkey)) {
936         printf("Unable to create SSL_CTX pair\n");
937         return 0;
938     }
939
940     /*
941      * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
942      * for TLSv1.2 for now until we add a new API.
943      */
944     SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
945
946     if (testctx) {
947         int ret;
948         if (curr->list != NULL)
949             ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
950         else
951             ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
952
953         if (!ret) {
954             if (curr->valid)
955                 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
956                        idx);
957             else
958                 testresult = 1;
959             goto end;
960         }
961         if (!curr->valid) {
962             printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
963             goto end;
964         }
965     }
966
967     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
968         printf("Unable to create SSL objects\n");
969         goto end;
970     }
971
972     if (!testctx) {
973         int ret;
974
975         if (curr->list != NULL)
976             ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
977         else
978             ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
979         if (!ret) {
980             if (curr->valid)
981                 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
982             else
983                 testresult = 1;
984             goto end;
985         }
986         if (!curr->valid) {
987             printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
988             goto end;
989         }
990     }
991
992     if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
993         printf("Unexpected return value creating SSL connection (%d)\n", idx);
994         goto end;
995     }
996
997     testresult = 1;
998
999  end:
1000     SSL_free(serverssl);
1001     SSL_free(clientssl);
1002     SSL_CTX_free(sctx);
1003     SSL_CTX_free(cctx);
1004
1005     return testresult;
1006 }
1007
1008 int test_main(int argc, char *argv[])
1009 {
1010     int testresult = 1;
1011
1012     if (argc != 3) {
1013         printf("Invalid argument count\n");
1014         return 1;
1015     }
1016
1017     cert = argv[1];
1018     privkey = argv[2];
1019
1020     ADD_TEST(test_large_message_tls);
1021     ADD_TEST(test_large_message_tls_read_ahead);
1022 #ifndef OPENSSL_NO_DTLS
1023     ADD_TEST(test_large_message_dtls);
1024 #endif
1025     ADD_TEST(test_tlsext_status_type);
1026     ADD_TEST(test_session_with_only_int_cache);
1027     ADD_TEST(test_session_with_only_ext_cache);
1028     ADD_TEST(test_session_with_both_cache);
1029     ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1030     ADD_TEST(test_ssl_bio_pop_next_bio);
1031     ADD_TEST(test_ssl_bio_pop_ssl_bio);
1032     ADD_TEST(test_ssl_bio_change_rbio);
1033     ADD_TEST(test_ssl_bio_change_wbio);
1034     ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1035
1036     testresult = run_tests(argv[0]);
1037
1038     bio_s_mempacket_test_free();
1039
1040     return testresult;
1041 }