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