Add more session tests
[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     SSL *serverssl3 = NULL, *clientssl3 = NULL;
126     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
127     int testresult = 0;
128
129     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
130                              &cctx, cert, privkey)) {
131         printf("Unable to create SSL_CTX pair\n");
132         return 0;
133     }
134
135 #ifndef OPENSSL_NO_TLS1_2
136     /* Only allow TLS1.2 so we can force a connection failure later */
137     SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
138 #endif
139
140     /* Set up session cache */
141     if (fix.use_ext_cache) {
142         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
143         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
144     }
145     if (fix.use_int_cache) {
146         /* Also covers instance where both are set */
147         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
148     } else {
149         SSL_CTX_set_session_cache_mode(cctx,
150                                        SSL_SESS_CACHE_CLIENT
151                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
152     }
153
154     if (!create_ssl_connection(sctx, cctx, &serverssl1, &clientssl1, NULL,
155                                NULL)) {
156         printf("Unable to create SSL connection\n");
157         goto end;
158     }
159     sess1 = SSL_get1_session(clientssl1);
160     if (sess1 == NULL) {
161         printf("Unexpected NULL session\n");
162         goto end;
163     }
164
165     if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
166         /* Should have failed because it should already be in the cache */
167         printf("Unexpected success adding session to cache\n");
168         goto end;
169     }
170
171     if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
172         printf("Session not added to cache\n");
173         goto end;
174     }
175
176     if (!create_ssl_connection(sctx, cctx, &serverssl2, &clientssl2, NULL,
177                                NULL)) {
178         printf("Unable to create second SSL connection\n");
179         goto end;
180     }
181
182     sess2 = SSL_get1_session(clientssl2);
183     if (sess2 == NULL) {
184         printf("Unexpected NULL session from clientssl2\n");
185         goto end;
186     }
187
188     if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
189         printf("Remove session callback unexpectedly called\n");
190         goto end;
191     }
192
193     /*
194      * This should clear sess2 from the cache because it is a "bad" session. See
195      * SSL_set_session() documentation.
196      */
197     if (!SSL_set_session(clientssl2, sess1)) {
198         printf("Unexpected failure setting session\n");
199         goto end;
200     }
201
202     if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
203         printf("Failed to call callback to remove session\n");
204         goto end;
205     }
206
207
208     if (SSL_get_session(clientssl2) != sess1) {
209         printf("Unexpected session found\n");
210         goto end;
211     }
212
213     if (fix.use_int_cache) {
214         if (!SSL_CTX_add_session(cctx, sess2)) {
215             /*
216              * Should have succeeded because it should not already be in the cache
217              */
218             printf("Unexpected failure adding session to cache\n");
219             goto end;
220         }
221
222         if (!SSL_CTX_remove_session(cctx, sess2)) {
223             printf("Unexpected failure removing session from cache\n");
224             goto end;
225         }
226
227         /* This is for the purposes of internal cache testing...ignore the
228          * counter for external cache
229          */
230         if (fix.use_ext_cache)
231             remove_called--;
232     }
233
234     /* This shouldn't be in the cache so should fail */
235     if (SSL_CTX_remove_session(cctx, sess2)) {
236         printf("Unexpected success removing session from cache\n");
237         goto end;
238     }
239
240     if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
241         printf("Failed to call callback to remove session #2\n");
242         goto end;
243     }
244
245 #ifndef OPENSSL_NO_TLS1_1
246     /* Force a connection failure */
247     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
248     clientssl3 = SSL_new(cctx);
249     if (clientssl3 == NULL) {
250         printf("Malloc failure\n");
251         goto end;
252     }
253     if (!SSL_set_session(clientssl3, sess1)) {
254         printf("Unable to set session for third connection\n");
255         goto end;
256     }
257
258     /* This should fail because of the mismatched protocol versions */
259     if (create_ssl_connection(sctx, cctx, &serverssl3, &clientssl3, NULL,
260                                NULL)) {
261         printf("Unexpected success creating SSL connection\n");
262         goto end;
263     }
264
265     /* We should have automatically removed the session from the cache */
266     if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
267         printf("Failed to call callback to remove session #2\n");
268         goto end;
269     }
270
271     if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
272         /*
273          * Should have succeeded because it should not already be in the cache
274          */
275         printf("Unexpected failure adding session to cache #2\n");
276         goto end;
277     }
278 #endif
279
280     testresult = 1;
281
282  end:
283     SSL_free(serverssl1);
284     SSL_free(clientssl1);
285     SSL_free(serverssl2);
286     SSL_free(clientssl2);
287     SSL_free(serverssl3);
288     SSL_free(clientssl3);
289     SSL_SESSION_free(sess1);
290     SSL_SESSION_free(sess2);
291     /*
292      * Check if we need to remove any sessions up-refed for the external cache
293      */
294     if (new_called >= 1)
295         SSL_SESSION_free(sess1);
296     if (new_called >= 2)
297         SSL_SESSION_free(sess2);
298     SSL_CTX_free(sctx);
299     SSL_CTX_free(cctx);
300
301     return testresult;
302 }
303
304 static int test_session_with_only_int_cache(void) {
305     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
306
307     fixture.use_ext_cache = 0;
308
309     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
310 }
311
312 static int test_session_with_only_ext_cache(void) {
313     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
314
315     fixture.use_int_cache = 0;
316
317     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
318 }
319
320 static int test_session_with_both_cache(void) {
321     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
322
323     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
324 }
325
326 int main(int argc, char *argv[])
327 {
328     BIO *err = NULL;
329     int testresult = 1;
330
331     if (argc != 3) {
332         printf("Invalid argument count\n");
333         return 1;
334     }
335
336     cert = argv[1];
337     privkey = argv[2];
338
339     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
340
341     CRYPTO_set_mem_debug(1);
342     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
343
344     ADD_TEST(test_tlsext_status_type);
345     ADD_TEST(test_session_with_only_int_cache);
346     ADD_TEST(test_session_with_only_ext_cache);
347     ADD_TEST(test_session_with_both_cache);
348
349     testresult = run_tests(argv[0]);
350
351 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
352     if (CRYPTO_mem_leaks(err) <= 0)
353         testresult = 1;
354 #endif
355     BIO_free(err);
356
357     if (!testresult)
358         printf("PASS\n");
359
360     return testresult;
361 }