]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/sslapitest.c
Add a basic test for BN_bn2dec.
[thirdparty/openssl.git] / test / sslapitest.c
CommitLineData
2cb4b5f6
MC
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
ba881d3b
MC
10#include <string.h>
11
2cb4b5f6
MC
12#include <openssl/opensslconf.h>
13#include <openssl/bio.h>
14#include <openssl/crypto.h>
15#include <openssl/ssl.h>
ba881d3b 16#include <openssl/ocsp.h>
2cb4b5f6
MC
17
18#include "ssltestlib.h"
c887104f 19#include "testutil.h"
2cb4b5f6
MC
20
21static char *cert = NULL;
22static char *privkey = NULL;
23
ba881d3b
MC
24static const unsigned char orespder[] = "Dummy OCSP Response";
25static int ocsp_server_called = 0;
26static int ocsp_client_called = 0;
27
28static int cdummyarg = 1;
29static X509 *ocspcert = NULL;
30
84d5549e
MC
31#define NUM_EXTRA_CERTS 40
32
33static 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
102static int test_large_message_tls(void)
103{
104 return execute_test_large_message(TLS_server_method(), TLS_client_method());
105}
106
107static int test_large_message_dtls(void)
108{
109 return execute_test_large_message(DTLS_server_method(),
110 DTLS_client_method());
111}
112
ba881d3b
MC
113static 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
145static 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
2cb4b5f6
MC
164static int test_tlsext_status_type(void)
165{
ba881d3b
MC
166 SSL_CTX *cctx = NULL, *sctx = NULL;
167 SSL *clientssl = NULL, *serverssl = NULL;
2cb4b5f6 168 int testresult = 0;
ba881d3b
MC
169 STACK_OF(OCSP_RESPID) *ids = NULL;
170 OCSP_RESPID *id = NULL;
171 BIO *certbio = NULL;
2cb4b5f6 172
ba881d3b
MC
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 }
2cb4b5f6 178
ba881d3b 179 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
2cb4b5f6
MC
180 printf("Unexpected initial value for "
181 "SSL_CTX_get_tlsext_status_type()\n");
182 goto end;
183 }
184
ba881d3b 185 /* First just do various checks getting and setting tlsext_status_type */
2cb4b5f6 186
ba881d3b
MC
187 clientssl = SSL_new(cctx);
188 if (SSL_get_tlsext_status_type(clientssl) != -1) {
2cb4b5f6
MC
189 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
190 goto end;
191 }
192
ba881d3b 193 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
2cb4b5f6
MC
194 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
195 goto end;
196 }
197
ba881d3b 198 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
2cb4b5f6
MC
199 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
200 goto end;
201 }
202
ba881d3b
MC
203 SSL_free(clientssl);
204 clientssl = NULL;
2cb4b5f6 205
ba881d3b 206 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
2cb4b5f6
MC
207 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
208 goto end;
209 }
210
ba881d3b 211 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
2cb4b5f6
MC
212 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
213 goto end;
214 }
215
ba881d3b 216 clientssl = SSL_new(cctx);
2cb4b5f6 217
ba881d3b 218 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
2cb4b5f6
MC
219 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
220 goto end;
221 }
222
ba881d3b
MC
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
2cb4b5f6
MC
331 testresult = 1;
332
333 end:
ba881d3b
MC
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;
2cb4b5f6
MC
343
344 return testresult;
345}
346
eaa776da
MC
347typedef 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
353static int new_called = 0, remove_called = 0;
354
355static SSL_SESSION_TEST_FIXTURE
356ssl_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
369static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
370{
371}
372
373static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
374{
375 new_called++;
376
377 return 1;
378}
379
380static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
381{
382 remove_called++;
383}
384
385static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
2cb4b5f6
MC
386{
387 SSL_CTX *sctx = NULL, *cctx = NULL;
388 SSL *serverssl1 = NULL, *clientssl1 = NULL;
389 SSL *serverssl2 = NULL, *clientssl2 = NULL;
b4982125 390#ifndef OPENSSL_NO_TLS1_1
eaa776da 391 SSL *serverssl3 = NULL, *clientssl3 = NULL;
b4982125 392#endif
2cb4b5f6
MC
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
eaa776da
MC
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 }
2cb4b5f6 420
b4982125 421 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
2cb4b5f6 422 NULL)) {
b4982125
MC
423 printf("Unable to create SSL objects\n");
424 goto end;
425 }
426
427 if (!create_ssl_connection(serverssl1, clientssl1)) {
2cb4b5f6
MC
428 printf("Unable to create SSL connection\n");
429 goto end;
430 }
2cb4b5f6
MC
431 sess1 = SSL_get1_session(clientssl1);
432 if (sess1 == NULL) {
433 printf("Unexpected NULL session\n");
434 goto end;
435 }
436
eaa776da 437 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
2cb4b5f6
MC
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
eaa776da
MC
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
b4982125
MC
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)) {
2cb4b5f6
MC
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
eaa776da
MC
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
2cb4b5f6
MC
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
eaa776da
MC
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
2cb4b5f6
MC
484 if (SSL_get_session(clientssl2) != sess1) {
485 printf("Unexpected session found\n");
486 goto end;
487 }
488
eaa776da
MC
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
2cb4b5f6 505 */
eaa776da
MC
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");
2cb4b5f6
MC
513 goto end;
514 }
515
eaa776da
MC
516 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
517 printf("Failed to call callback to remove session #2\n");
2cb4b5f6
MC
518 goto end;
519 }
520
80f397e2 521#if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
eaa776da
MC
522 /* Force a connection failure */
523 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
b4982125
MC
524
525 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
526 printf("Unable to create third SSL objects\n");
eaa776da
MC
527 goto end;
528 }
b4982125 529
eaa776da
MC
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 */
b4982125
MC
536 if (create_ssl_connection(serverssl3, clientssl3)) {
537 printf("Unable to create third SSL connection\n");
eaa776da
MC
538 goto end;
539 }
540
b4982125 541
eaa776da
MC
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");
2cb4b5f6
MC
545 goto end;
546 }
547
eaa776da
MC
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
2cb4b5f6 557 testresult = 1;
eaa776da 558
2cb4b5f6
MC
559 end:
560 SSL_free(serverssl1);
561 SSL_free(clientssl1);
562 SSL_free(serverssl2);
563 SSL_free(clientssl2);
b4982125 564#ifndef OPENSSL_NO_TLS1_1
eaa776da
MC
565 SSL_free(serverssl3);
566 SSL_free(clientssl3);
b4982125 567#endif
2cb4b5f6
MC
568 SSL_SESSION_free(sess1);
569 SSL_SESSION_free(sess2);
eaa776da
MC
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);
2cb4b5f6
MC
577 SSL_CTX_free(sctx);
578 SSL_CTX_free(cctx);
579
580 return testresult;
581}
582
7fb4c820
MC
583static int test_session_with_only_int_cache(void)
584{
eaa776da
MC
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
7fb4c820
MC
592static int test_session_with_only_ext_cache(void)
593{
eaa776da
MC
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
7fb4c820
MC
601static int test_session_with_both_cache(void)
602{
eaa776da
MC
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
7fb4c820
MC
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
614static 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
629static 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;
0fae8150 634 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
7fb4c820
MC
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
9a716987
MC
723typedef 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
729static 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;
bee5ee5f 735 fixture.change_bio = NO_BIO_CHANGE;
9a716987
MC
736
737 return fixture;
738}
739
740static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
741{
742}
743
744static 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)
65e2d672 786 SSL_set0_rbio(ssl, membio2);
9a716987 787 else
65e2d672 788 SSL_set0_wbio(ssl, membio2);
9a716987
MC
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
807static 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
814static 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
823static 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
832static 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
2cb4b5f6
MC
841int main(int argc, char *argv[])
842{
c887104f
MC
843 BIO *err = NULL;
844 int testresult = 1;
2cb4b5f6
MC
845
846 if (argc != 3) {
847 printf("Invalid argument count\n");
c887104f 848 return 1;
2cb4b5f6
MC
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
84d5549e
MC
859 ADD_TEST(test_large_message_tls);
860 ADD_TEST(test_large_message_dtls);
c887104f 861 ADD_TEST(test_tlsext_status_type);
eaa776da
MC
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);
7fb4c820 865 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
9a716987
MC
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);
2cb4b5f6 870
c887104f 871 testresult = run_tests(argv[0]);
2cb4b5f6
MC
872
873#ifndef OPENSSL_NO_CRYPTO_MDEBUG
874 if (CRYPTO_mem_leaks(err) <= 0)
c887104f 875 testresult = 1;
2cb4b5f6
MC
876#endif
877 BIO_free(err);
878
c887104f 879 if (!testresult)
2cb4b5f6
MC
880 printf("PASS\n");
881
c887104f 882 return testresult;
2cb4b5f6 883}