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