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