Improve the definition of STITCHED_CALL in e_rc4_hmac_md5.c
[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 <openssl/opensslconf.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
14
15 #include "ssltestlib.h"
16 #include "testutil.h"
17
18 static char *cert = NULL;
19 static char *privkey = NULL;
20
21 static int test_tlsext_status_type(void)
22 {
23     SSL_CTX *ctx = NULL;
24     SSL *con = NULL;
25     int testresult = 0;
26
27     /* Test tlsext_status_type */
28     ctx = SSL_CTX_new(TLS_method());
29
30     if (SSL_CTX_get_tlsext_status_type(ctx) != -1) {
31         printf("Unexpected initial value for "
32                "SSL_CTX_get_tlsext_status_type()\n");
33         goto end;
34     }
35
36     con = SSL_new(ctx);
37
38     if (SSL_get_tlsext_status_type(con) != -1) {
39         printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
40         goto end;
41     }
42
43     if (!SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp)) {
44         printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
45         goto end;
46     }
47
48     if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
49         printf("Unexpected result for SSL_get_tlsext_status_type()\n");
50         goto end;
51     }
52
53     SSL_free(con);
54     con = NULL;
55
56     if (!SSL_CTX_set_tlsext_status_type(ctx, TLSEXT_STATUSTYPE_ocsp)) {
57         printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
58         goto end;
59     }
60
61     if (SSL_CTX_get_tlsext_status_type(ctx) != TLSEXT_STATUSTYPE_ocsp) {
62         printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
63         goto end;
64     }
65
66     con = SSL_new(ctx);
67
68     if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
69         printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
70         goto end;
71     }
72
73     testresult = 1;
74
75  end:
76     SSL_free(con);
77     SSL_CTX_free(ctx);
78
79     return testresult;
80 }
81
82 typedef struct ssl_session_test_fixture {
83     const char *test_case_name;
84     int use_ext_cache;
85     int use_int_cache;
86 } SSL_SESSION_TEST_FIXTURE;
87
88 static int new_called = 0, remove_called = 0;
89
90 static SSL_SESSION_TEST_FIXTURE
91 ssl_session_set_up(const char *const test_case_name)
92 {
93     SSL_SESSION_TEST_FIXTURE fixture;
94
95     fixture.test_case_name = test_case_name;
96     fixture.use_ext_cache = 1;
97     fixture.use_int_cache = 1;
98
99     new_called = remove_called = 0;
100
101     return fixture;
102 }
103
104 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
105 {
106 }
107
108 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
109 {
110     new_called++;
111
112     return 1;
113 }
114
115 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
116 {
117     remove_called++;
118 }
119
120 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
121 {
122     SSL_CTX *sctx = NULL, *cctx = NULL;
123     SSL *serverssl1 = NULL, *clientssl1 = NULL;
124     SSL *serverssl2 = NULL, *clientssl2 = NULL;
125 #ifndef OPENSSL_NO_TLS1_1
126     SSL *serverssl3 = NULL, *clientssl3 = NULL;
127 #endif
128     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
129     int testresult = 0;
130
131     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
132                              &cctx, cert, privkey)) {
133         printf("Unable to create SSL_CTX pair\n");
134         return 0;
135     }
136
137 #ifndef OPENSSL_NO_TLS1_2
138     /* Only allow TLS1.2 so we can force a connection failure later */
139     SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
140 #endif
141
142     /* Set up session cache */
143     if (fix.use_ext_cache) {
144         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
145         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
146     }
147     if (fix.use_int_cache) {
148         /* Also covers instance where both are set */
149         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
150     } else {
151         SSL_CTX_set_session_cache_mode(cctx,
152                                        SSL_SESS_CACHE_CLIENT
153                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
154     }
155
156     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
157                                NULL)) {
158         printf("Unable to create SSL objects\n");
159         goto end;
160     }
161
162     if (!create_ssl_connection(serverssl1, clientssl1)) {
163         printf("Unable to create SSL connection\n");
164         goto end;
165     }
166     sess1 = SSL_get1_session(clientssl1);
167     if (sess1 == NULL) {
168         printf("Unexpected NULL session\n");
169         goto end;
170     }
171
172     if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
173         /* Should have failed because it should already be in the cache */
174         printf("Unexpected success adding session to cache\n");
175         goto end;
176     }
177
178     if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
179         printf("Session not added to cache\n");
180         goto end;
181     }
182
183     if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
184         printf("Unable to create second SSL objects\n");
185         goto end;
186     }
187
188     if (!create_ssl_connection(serverssl2, clientssl2)) {
189         printf("Unable to create second SSL connection\n");
190         goto end;
191     }
192
193     sess2 = SSL_get1_session(clientssl2);
194     if (sess2 == NULL) {
195         printf("Unexpected NULL session from clientssl2\n");
196         goto end;
197     }
198
199     if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
200         printf("Remove session callback unexpectedly called\n");
201         goto end;
202     }
203
204     /*
205      * This should clear sess2 from the cache because it is a "bad" session. See
206      * SSL_set_session() documentation.
207      */
208     if (!SSL_set_session(clientssl2, sess1)) {
209         printf("Unexpected failure setting session\n");
210         goto end;
211     }
212
213     if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
214         printf("Failed to call callback to remove session\n");
215         goto end;
216     }
217
218
219     if (SSL_get_session(clientssl2) != sess1) {
220         printf("Unexpected session found\n");
221         goto end;
222     }
223
224     if (fix.use_int_cache) {
225         if (!SSL_CTX_add_session(cctx, sess2)) {
226             /*
227              * Should have succeeded because it should not already be in the cache
228              */
229             printf("Unexpected failure adding session to cache\n");
230             goto end;
231         }
232
233         if (!SSL_CTX_remove_session(cctx, sess2)) {
234             printf("Unexpected failure removing session from cache\n");
235             goto end;
236         }
237
238         /* This is for the purposes of internal cache testing...ignore the
239          * counter for external cache
240          */
241         if (fix.use_ext_cache)
242             remove_called--;
243     }
244
245     /* This shouldn't be in the cache so should fail */
246     if (SSL_CTX_remove_session(cctx, sess2)) {
247         printf("Unexpected success removing session from cache\n");
248         goto end;
249     }
250
251     if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
252         printf("Failed to call callback to remove session #2\n");
253         goto end;
254     }
255
256 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
257     /* Force a connection failure */
258     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
259
260     if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
261         printf("Unable to create third SSL objects\n");
262         goto end;
263     }
264
265     if (!SSL_set_session(clientssl3, sess1)) {
266         printf("Unable to set session for third connection\n");
267         goto end;
268     }
269
270     /* This should fail because of the mismatched protocol versions */
271     if (create_ssl_connection(serverssl3, clientssl3)) {
272         printf("Unable to create third SSL connection\n");
273         goto end;
274     }
275
276
277     /* We should have automatically removed the session from the cache */
278     if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
279         printf("Failed to call callback to remove session #2\n");
280         goto end;
281     }
282
283     if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
284         /*
285          * Should have succeeded because it should not already be in the cache
286          */
287         printf("Unexpected failure adding session to cache #2\n");
288         goto end;
289     }
290 #endif
291
292     testresult = 1;
293
294  end:
295     SSL_free(serverssl1);
296     SSL_free(clientssl1);
297     SSL_free(serverssl2);
298     SSL_free(clientssl2);
299 #ifndef OPENSSL_NO_TLS1_1
300     SSL_free(serverssl3);
301     SSL_free(clientssl3);
302 #endif
303     SSL_SESSION_free(sess1);
304     SSL_SESSION_free(sess2);
305     /*
306      * Check if we need to remove any sessions up-refed for the external cache
307      */
308     if (new_called >= 1)
309         SSL_SESSION_free(sess1);
310     if (new_called >= 2)
311         SSL_SESSION_free(sess2);
312     SSL_CTX_free(sctx);
313     SSL_CTX_free(cctx);
314
315     return testresult;
316 }
317
318 static int test_session_with_only_int_cache(void)
319 {
320     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
321
322     fixture.use_ext_cache = 0;
323
324     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
325 }
326
327 static int test_session_with_only_ext_cache(void)
328 {
329     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
330
331     fixture.use_int_cache = 0;
332
333     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
334 }
335
336 static int test_session_with_both_cache(void)
337 {
338     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
339
340     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
341 }
342
343 #define USE_NULL    0
344 #define USE_BIO_1   1
345 #define USE_BIO_2   2
346
347 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
348
349 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
350 {
351     switch (type) {
352     case USE_NULL:
353         *res = NULL;
354         break;
355     case USE_BIO_1:
356         *res = bio1;
357         break;
358     case USE_BIO_2:
359         *res = bio2;
360         break;
361     }
362 }
363
364 static int test_ssl_set_bio(int idx)
365 {
366     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
367     BIO *bio1 = NULL;
368     BIO *bio2 = NULL;
369     BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
370     SSL *ssl = NULL;
371     int initrbio, initwbio, newrbio, newwbio;
372     int testresult = 0;
373
374     if (ctx == NULL) {
375         printf("Failed to allocate SSL_CTX\n");
376         goto end;
377     }
378
379     ssl = SSL_new(ctx);
380     if (ssl == NULL) {
381         printf("Failed to allocate SSL object\n");
382         goto end;
383     }
384
385     initrbio = idx % 3;
386     idx /= 3;
387     initwbio = idx % 3;
388     idx /= 3;
389     newrbio = idx % 3;
390     idx /= 3;
391     newwbio = idx;
392     OPENSSL_assert(newwbio <= 2);
393
394     if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
395             || newwbio == USE_BIO_1) {
396         bio1 = BIO_new(BIO_s_mem());
397         if (bio1 == NULL) {
398             printf("Failed to allocate bio1\n");
399             goto end;
400         }
401     }
402
403     if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
404             || newwbio == USE_BIO_2) {
405         bio2 = BIO_new(BIO_s_mem());
406         if (bio2 == NULL) {
407             printf("Failed to allocate bio2\n");
408             goto end;
409         }
410     }
411
412     setupbio(&irbio, bio1, bio2, initrbio);
413     setupbio(&iwbio, bio1, bio2, initwbio);
414
415     /*
416      * We want to maintain our own refs to these BIO, so do an up ref for each
417      * BIO that will have ownersip transferred in the SSL_set_bio() call
418      */
419     if (irbio != NULL)
420         BIO_up_ref(irbio);
421     if (iwbio != NULL && iwbio != irbio)
422         BIO_up_ref(iwbio);
423
424     SSL_set_bio(ssl, irbio, iwbio);
425
426     setupbio(&nrbio, bio1, bio2, newrbio);
427     setupbio(&nwbio, bio1, bio2, newwbio);
428
429     /*
430      * We will (maybe) transfer ownership again so do more up refs.
431      * SSL_set_bio() has some really complicated ownership rules where BIOs have
432      * already been set!
433      */
434     if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
435         BIO_up_ref(nrbio);
436     if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
437         BIO_up_ref(nwbio);
438
439     SSL_set_bio(ssl, nrbio, nwbio);
440
441     testresult = 1;
442
443  end:
444     SSL_free(ssl);
445     BIO_free(bio1);
446     BIO_free(bio2);
447     /*
448      * This test is checking that the ref counting for SSL_set_bio is correct.
449      * If we get here and we did too many frees then we will fail in the above
450      * functions. If we haven't done enough then this will only be detected in
451      * a crypto-mdebug build
452      */
453     SSL_CTX_free(ctx);
454
455     return testresult;
456 }
457
458 typedef struct ssl_bio_test_fixture {
459     const char *test_case_name;
460     int pop_ssl;
461     enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
462 } SSL_BIO_TEST_FIXTURE;
463
464 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
465 {
466     SSL_BIO_TEST_FIXTURE fixture;
467
468     fixture.test_case_name = test_case_name;
469     fixture.pop_ssl = 0;
470     fixture.change_bio = NO_BIO_CHANGE;
471
472     return fixture;
473 }
474
475 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
476 {
477 }
478
479 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
480 {
481     BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
482     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
483     SSL *ssl = NULL;
484     int testresult = 0;
485
486     if (ctx == NULL) {
487         printf("Failed to allocate SSL_CTX\n");
488         return 0;
489     }
490
491     ssl = SSL_new(ctx);
492     if (ssl == NULL) {
493         printf("Failed to allocate SSL object\n");
494         goto end;
495     }
496
497     sslbio = BIO_new(BIO_f_ssl());
498     membio1 = BIO_new(BIO_s_mem());
499
500     if (sslbio == NULL || membio1 == NULL) {
501         printf("Malloc failure creating BIOs\n");
502         goto end;
503     }
504
505     BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
506
507     /*
508      * If anything goes wrong here then we could leak memory, so this will
509      * be caught in a crypto-mdebug build
510      */
511     BIO_push(sslbio, membio1);
512
513     /* Verify chaning the rbio/wbio directly does not cause leaks */
514     if (fix.change_bio != NO_BIO_CHANGE) {
515         membio2 = BIO_new(BIO_s_mem());
516         if (membio2 == NULL) {
517             printf("Malloc failure creating membio2\n");
518             goto end;
519         }
520         if (fix.change_bio == CHANGE_RBIO)
521             SSL_set0_rbio(ssl, membio2);
522         else
523             SSL_set0_wbio(ssl, membio2);
524     }
525     ssl = NULL;
526
527     if (fix.pop_ssl)
528         BIO_pop(sslbio);
529     else
530         BIO_pop(membio1);
531
532     testresult = 1;
533  end:
534     BIO_free(membio1);
535     BIO_free(sslbio);
536     SSL_free(ssl);
537     SSL_CTX_free(ctx);
538
539     return testresult;
540 }
541
542 static int test_ssl_bio_pop_next_bio(void)
543 {
544     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
545
546     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
547 }
548
549 static int test_ssl_bio_pop_ssl_bio(void)
550 {
551     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
552
553     fixture.pop_ssl = 1;
554
555     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
556 }
557
558 static int test_ssl_bio_change_rbio(void)
559 {
560     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
561
562     fixture.change_bio = CHANGE_RBIO;
563
564     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
565 }
566
567 static int test_ssl_bio_change_wbio(void)
568 {
569     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
570
571     fixture.change_bio = CHANGE_WBIO;
572
573     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
574 }
575
576 int main(int argc, char *argv[])
577 {
578     BIO *err = NULL;
579     int testresult = 1;
580
581     if (argc != 3) {
582         printf("Invalid argument count\n");
583         return 1;
584     }
585
586     cert = argv[1];
587     privkey = argv[2];
588
589     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
590
591     CRYPTO_set_mem_debug(1);
592     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
593
594     ADD_TEST(test_tlsext_status_type);
595     ADD_TEST(test_session_with_only_int_cache);
596     ADD_TEST(test_session_with_only_ext_cache);
597     ADD_TEST(test_session_with_both_cache);
598     ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
599     ADD_TEST(test_ssl_bio_pop_next_bio);
600     ADD_TEST(test_ssl_bio_pop_ssl_bio);
601     ADD_TEST(test_ssl_bio_change_rbio);
602     ADD_TEST(test_ssl_bio_change_wbio);
603
604     testresult = run_tests(argv[0]);
605
606 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
607     if (CRYPTO_mem_leaks(err) <= 0)
608         testresult = 1;
609 #endif
610     BIO_free(err);
611
612     if (!testresult)
613         printf("PASS\n");
614
615     return testresult;
616 }