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