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