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