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