Re-enable TLSv1.3 session resumption related tests in sslapitest
[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 #include "test_main_custom.h"
21 #include "e_os.h"
22
23 static char *cert = NULL;
24 static char *privkey = NULL;
25
26 #define LOG_BUFFER_SIZE 1024
27 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
28 static int server_log_buffer_index = 0;
29 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
30 static int client_log_buffer_index = 0;
31 static int error_writing_log = 0;
32
33 #ifndef OPENSSL_NO_OCSP
34 static const unsigned char orespder[] = "Dummy OCSP Response";
35 static int ocsp_server_called = 0;
36 static int ocsp_client_called = 0;
37
38 static int cdummyarg = 1;
39 static X509 *ocspcert = NULL;
40 #endif
41
42 #define NUM_EXTRA_CERTS 40
43
44 static void client_keylog_callback(const SSL *ssl, const char *line) {
45     int line_length = strlen(line);
46
47     /* If the log doesn't fit, error out. */
48     if ((client_log_buffer_index + line_length) > LOG_BUFFER_SIZE) {
49         printf("No room in client log\n");
50         error_writing_log = 1;
51         return;
52     }
53
54     strcat(client_log_buffer, line);
55     client_log_buffer_index += line_length;
56     client_log_buffer[client_log_buffer_index] = '\n';
57     client_log_buffer_index += 1;
58
59     return;
60 }
61
62 static void server_keylog_callback(const SSL *ssl, const char *line) {
63     int line_length = strlen(line);
64
65     /* If the log doesn't fit, error out. */
66     if ((server_log_buffer_index + line_length) > LOG_BUFFER_SIZE) {
67         printf("No room in server log\n");
68         error_writing_log = 1;
69         return;
70     }
71
72     strcat(server_log_buffer, line);
73     server_log_buffer_index += line_length;
74     server_log_buffer[server_log_buffer_index] = '\n';
75     server_log_buffer_index += 1;
76
77     return;
78 }
79
80 static int compare_hex_encoded_buffer(const char *hex_encoded,
81                                       size_t hex_length,
82                                       const uint8_t *raw,
83                                       size_t raw_length) {
84     size_t i;
85     size_t j;
86
87     /* One byte too big, just to be safe. */
88     char hexed[3] = {0};
89
90     if ((raw_length * 2) != hex_length) {
91         printf("Inconsistent hex encoded lengths.\n");
92         return 1;
93     }
94
95     for (i = j = 0; (i < raw_length) && ((j + 1) < hex_length); i++) {
96         sprintf(hexed, "%02x", raw[i]);
97         if ((hexed[0] != hex_encoded[j]) || (hexed[1] != hex_encoded[j + 1])) {
98             printf("Hex output does not match.\n");
99             return 1;
100         }
101         j += 2;
102     }
103
104     return 0;
105 }
106
107 static int test_keylog_output(char *buffer, const SSL *ssl,
108                               const SSL_SESSION *session) {
109     int saw_client_random = 0;
110     char *token = NULL;
111     unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
112     size_t client_random_size = SSL3_RANDOM_SIZE;
113     unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
114     size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
115
116     token = strtok(buffer, " \n");
117     while (token) {
118         if (strcmp(token, "RSA") == 0) {
119             /* Premaster secret. Tokens should be: 16 ASCII bytes of
120              * hex-encoded encrypted secret, then the hex-encoded pre-master
121              * secret.
122              */
123             token = strtok(NULL, " \n");
124             if (!token) {
125                 printf("Unexpectedly short premaster secret log.\n");
126                 return -1;
127             }
128             if (strlen(token) != 16) {
129                 printf("Bad value for encrypted secret: %s\n", token);
130                 return -1;
131             }
132             token = strtok(NULL, " \n");
133             if (!token) {
134                 printf("Unexpectedly short premaster secret log.\n");
135                 return -1;
136             }
137             /* TODO: Can I check this sensibly? */
138         } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
139             /* Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
140              * client random, then the hex-encoded master secret.
141              */
142             client_random_size = SSL_get_client_random(ssl,
143                                                        actual_client_random,
144                                                        SSL3_RANDOM_SIZE);
145             if (client_random_size != SSL3_RANDOM_SIZE) {
146                 printf("Unexpected short client random.\n");
147                 return -1;
148             }
149
150             token = strtok(NULL, " \n");
151             if (!token) {
152                 printf("Unexpected short master secret log.\n");
153                 return -1;
154             }
155             if (strlen(token) != 64) {
156                 printf("Bad value for client random: %s\n", token);
157                 return -1;
158             }
159             if (compare_hex_encoded_buffer(token, 64, actual_client_random,
160                                            client_random_size)) {
161                 printf("Bad value for client random: %s\n", token);
162                 return -1;
163             }
164
165             token = strtok(NULL, " \n");
166             if (!token) {
167                 printf("Unexpectedly short master secret log.\n");
168                 return -1;
169             }
170
171             master_key_size = SSL_SESSION_get_master_key(session,
172                                                          actual_master_key,
173                                                          master_key_size);
174             if (!master_key_size) {
175                 printf("Error getting master key to compare.\n");
176                 return -1;
177             }
178             if (compare_hex_encoded_buffer(token, strlen(token),
179                                            actual_master_key,
180                                            master_key_size)) {
181                 printf("Bad value for master key: %s\n", token);
182                 return -1;
183             }
184
185             saw_client_random = 1;
186         } else {
187             printf("Unexpected token in buffer: %s\n", token);
188             return -1;
189         }
190
191         token = strtok(NULL, " \n");
192     }
193
194     return saw_client_random;
195 }
196
197 static int test_keylog(void) {
198     SSL_CTX *cctx = NULL, *sctx = NULL;
199     SSL *clientssl = NULL, *serverssl = NULL;
200     int testresult = 0;
201     int rc;
202
203     /* Clean up logging space */
204     memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
205     memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
206     client_log_buffer_index = 0;
207     server_log_buffer_index = 0;
208     error_writing_log = 0;
209
210     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
211                              &cctx, cert, privkey)) {
212         printf("Unable to create SSL_CTX pair\n");
213         return 0;
214     }
215
216     /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
217     SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
218     SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
219
220     /* We also want to ensure that we use RSA-based key exchange. */
221     rc = SSL_CTX_set_cipher_list(cctx, "RSA");
222     if (rc == 0) {
223         printf("Unable to restrict to RSA key exchange.\n");
224         goto end;
225     }
226
227     if (SSL_CTX_get_keylog_callback(cctx)) {
228         printf("Unexpected initial value for client "
229                "SSL_CTX_get_keylog_callback()\n");
230         goto end;
231     }
232     if (SSL_CTX_get_keylog_callback(sctx)) {
233         printf("Unexpected initial value for server "
234                "SSL_CTX_get_keylog_callback()\n");
235         goto end;
236     }
237
238     SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
239     SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
240
241     if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
242         printf("Unexpected set value for client "
243                "SSL_CTX_get_keylog_callback()\n");
244     }
245
246     if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
247         printf("Unexpected set value for server "
248                "SSL_CTX_get_keylog_callback()\n");
249     }
250
251     /* Now do a handshake and check that the logs have been written to. */
252     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
253         printf("Unable to create SSL objects\n");
254         goto end;
255     }
256
257     if (!create_ssl_connection(serverssl, clientssl)) {
258         printf("Unable to create SSL connection\n");
259         goto end;
260     }
261
262     if (error_writing_log) {
263         printf("Error encountered while logging\n");
264         goto end;
265     }
266
267     if ((client_log_buffer_index == 0) || (server_log_buffer_index == 0)) {
268         printf("No logs written\n");
269         goto end;
270     }
271
272     /* Now we want to test that our output data was vaguely sensible. We
273      * do that by using strtok and confirming that we have more or less the
274      * data we expect.
275      */
276     if (test_keylog_output(client_log_buffer, clientssl,
277                            SSL_get_session(clientssl)) != 1) {
278         printf("Error encountered in client log buffer\n");
279         goto end;
280     }
281     if (test_keylog_output(server_log_buffer, serverssl,
282                            SSL_get_session(serverssl)) != 1) {
283         printf("Error encountered in server log buffer\n");
284         goto end;
285     }
286
287     testresult = 1;
288
289 end:
290     SSL_free(serverssl);
291     SSL_free(clientssl);
292     SSL_CTX_free(sctx);
293     SSL_CTX_free(cctx);
294
295     return testresult;
296 }
297
298 #ifndef OPENSSL_NO_TLS1_3
299 static int test_keylog_no_master_key(void) {
300     SSL_CTX *cctx = NULL, *sctx = NULL;
301     SSL *clientssl = NULL, *serverssl = NULL;
302     int testresult = 0;
303
304     /* Clean up logging space */
305     memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
306     memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
307     client_log_buffer_index = 0;
308     server_log_buffer_index = 0;
309     error_writing_log = 0;
310
311     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
312                              &cctx, cert, privkey)) {
313         printf("Unable to create SSL_CTX pair\n");
314         return 0;
315     }
316
317     if (SSL_CTX_get_keylog_callback(cctx)) {
318         printf("Unexpected initial value for client "
319                "SSL_CTX_get_keylog_callback()\n");
320         goto end;
321     }
322     if (SSL_CTX_get_keylog_callback(sctx)) {
323         printf("Unexpected initial value for server "
324                "SSL_CTX_get_keylog_callback()\n");
325         goto end;
326     }
327
328     SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
329     SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
330
331     if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
332         printf("Unexpected set value for client "
333                "SSL_CTX_get_keylog_callback()\n");
334     }
335
336     if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
337         printf("Unexpected set value for server "
338                "SSL_CTX_get_keylog_callback()\n");
339     }
340
341     /* Now do a handshake and check that the logs have been written to. */
342     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
343         printf("Unable to create SSL objects\n");
344         goto end;
345     }
346
347     if (!create_ssl_connection(serverssl, clientssl)) {
348         printf("Unable to create SSL connection\n");
349         goto end;
350     }
351
352     if (error_writing_log) {
353         printf("Error encountered while logging\n");
354         goto end;
355     }
356
357     /* Now we want to test that our output data was vaguely sensible. For this
358      * test, we expect no CLIENT_RANDOM entry.
359      */
360     if (test_keylog_output(client_log_buffer, clientssl,
361                            SSL_get_session(clientssl)) != 0) {
362         printf("Error encountered in client log buffer\n");
363         goto end;
364     }
365     if (test_keylog_output(server_log_buffer, serverssl,
366                            SSL_get_session(serverssl)) != 0) {
367         printf("Error encountered in server log buffer\n");
368         goto end;
369     }
370
371     testresult = 1;
372
373 end:
374     SSL_free(serverssl);
375     SSL_free(clientssl);
376     SSL_CTX_free(sctx);
377     SSL_CTX_free(cctx);
378
379     return testresult;
380 }
381 #endif
382
383 static int execute_test_large_message(const SSL_METHOD *smeth,
384                                       const SSL_METHOD *cmeth, int read_ahead)
385 {
386     SSL_CTX *cctx = NULL, *sctx = NULL;
387     SSL *clientssl = NULL, *serverssl = NULL;
388     int testresult = 0;
389     int i;
390     BIO *certbio = BIO_new_file(cert, "r");
391     X509 *chaincert = NULL;
392     int certlen;
393
394     if (certbio == NULL) {
395         printf("Can't load the certficate file\n");
396         goto end;
397     }
398     chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
399     BIO_free(certbio);
400     certbio = NULL;
401     if (chaincert == NULL) {
402         printf("Unable to load certificate for chain\n");
403         goto end;
404     }
405
406     if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
407                              &cctx, cert, privkey)) {
408         printf("Unable to create SSL_CTX pair\n");
409         goto end;
410     }
411
412     if(read_ahead) {
413         /*
414          * Test that read_ahead works correctly when dealing with large
415          * records
416          */
417         SSL_CTX_set_read_ahead(cctx, 1);
418     }
419
420     /*
421      * We assume the supplied certificate is big enough so that if we add
422      * NUM_EXTRA_CERTS it will make the overall message large enough. The
423      * default buffer size is requested to be 16k, but due to the way BUF_MEM
424      * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
425      * we need to have a message larger than that.
426      */
427     certlen = i2d_X509(chaincert, NULL);
428     OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
429                    > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
430     for (i = 0; i < NUM_EXTRA_CERTS; i++) {
431         if (!X509_up_ref(chaincert)) {
432             printf("Unable to up ref cert\n");
433             goto end;
434         }
435         if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
436             printf("Unable to add extra chain cert %d\n", i);
437             X509_free(chaincert);
438             goto end;
439         }
440     }
441
442     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
443         printf("Unable to create SSL objects\n");
444         goto end;
445     }
446
447     if (!create_ssl_connection(serverssl, clientssl)) {
448         printf("Unable to create SSL connection\n");
449         goto end;
450     }
451
452     /*
453      * Calling SSL_clear() first is not required but this tests that SSL_clear()
454      * doesn't leak (when using enable-crypto-mdebug).
455      */
456     if (!SSL_clear(serverssl)) {
457         printf("Unexpected failure from SSL_clear()\n");
458         goto end;
459     }
460
461     testresult = 1;
462  end:
463     X509_free(chaincert);
464     SSL_free(serverssl);
465     SSL_free(clientssl);
466     SSL_CTX_free(sctx);
467     SSL_CTX_free(cctx);
468
469     return testresult;
470 }
471
472 static int test_large_message_tls(void)
473 {
474     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
475                                       0);
476 }
477
478 static int test_large_message_tls_read_ahead(void)
479 {
480     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
481                                       1);
482 }
483
484 #ifndef OPENSSL_NO_DTLS
485 static int test_large_message_dtls(void)
486 {
487     /*
488      * read_ahead is not relevant to DTLS because DTLS always acts as if
489      * read_ahead is set.
490      */
491     return execute_test_large_message(DTLS_server_method(),
492                                       DTLS_client_method(), 0);
493 }
494 #endif
495
496 #ifndef OPENSSL_NO_OCSP
497 static int ocsp_server_cb(SSL *s, void *arg)
498 {
499     int *argi = (int *)arg;
500     unsigned char *orespdercopy = NULL;
501     STACK_OF(OCSP_RESPID) *ids = NULL;
502     OCSP_RESPID *id = NULL;
503
504     if (*argi == 2) {
505         /* In this test we are expecting exactly 1 OCSP_RESPID */
506         SSL_get_tlsext_status_ids(s, &ids);
507         if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
508             return SSL_TLSEXT_ERR_ALERT_FATAL;
509
510         id = sk_OCSP_RESPID_value(ids, 0);
511         if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
512             return SSL_TLSEXT_ERR_ALERT_FATAL;
513     } else if (*argi != 1) {
514         return SSL_TLSEXT_ERR_ALERT_FATAL;
515     }
516
517
518     orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
519     if (orespdercopy == NULL)
520         return SSL_TLSEXT_ERR_ALERT_FATAL;
521
522     SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
523
524     ocsp_server_called = 1;
525
526     return SSL_TLSEXT_ERR_OK;
527 }
528
529 static int ocsp_client_cb(SSL *s, void *arg)
530 {
531     int *argi = (int *)arg;
532     const unsigned char *respderin;
533     size_t len;
534
535     if (*argi != 1 && *argi != 2)
536         return 0;
537
538     len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
539
540     if (memcmp(orespder, respderin, len) != 0)
541         return 0;
542
543     ocsp_client_called = 1;
544
545     return 1;
546 }
547
548 static int test_tlsext_status_type(void)
549 {
550     SSL_CTX *cctx = NULL, *sctx = NULL;
551     SSL *clientssl = NULL, *serverssl = NULL;
552     int testresult = 0;
553     STACK_OF(OCSP_RESPID) *ids = NULL;
554     OCSP_RESPID *id = NULL;
555     BIO *certbio = NULL;
556
557     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
558                              &cctx, cert, privkey)) {
559         printf("Unable to create SSL_CTX pair\n");
560         return 0;
561     }
562
563     if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
564         printf("Unexpected initial value for "
565                "SSL_CTX_get_tlsext_status_type()\n");
566         goto end;
567     }
568
569     /* First just do various checks getting and setting tlsext_status_type */
570
571     clientssl = SSL_new(cctx);
572     if (SSL_get_tlsext_status_type(clientssl) != -1) {
573         printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
574         goto end;
575     }
576
577     if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
578         printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
579         goto end;
580     }
581
582     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
583         printf("Unexpected result for SSL_get_tlsext_status_type()\n");
584         goto end;
585     }
586
587     SSL_free(clientssl);
588     clientssl = NULL;
589
590     if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
591         printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
592         goto end;
593     }
594
595     if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
596         printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
597         goto end;
598     }
599
600     clientssl = SSL_new(cctx);
601
602     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
603         printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
604         goto end;
605     }
606
607     SSL_free(clientssl);
608     clientssl = NULL;
609
610     /*
611      * Now actually do a handshake and check OCSP information is exchanged and
612      * the callbacks get called
613      */
614
615     SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
616     SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
617     SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
618     SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
619
620     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
621         printf("Unable to create SSL objects\n");
622         goto end;
623     }
624
625     if (!create_ssl_connection(serverssl, clientssl)) {
626         printf("Unable to create SSL connection\n");
627         goto end;
628     }
629
630     if (!ocsp_client_called || !ocsp_server_called) {
631         printf("OCSP callbacks not called\n");
632         goto end;
633     }
634
635     SSL_free(serverssl);
636     SSL_free(clientssl);
637     serverssl = NULL;
638     clientssl = NULL;
639
640     /* Try again but this time force the server side callback to fail */
641     ocsp_client_called = 0;
642     ocsp_server_called = 0;
643     cdummyarg = 0;
644
645     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
646         printf("Unable to create SSL objects\n");
647         goto end;
648     }
649
650     /* This should fail because the callback will fail */
651     if (create_ssl_connection(serverssl, clientssl)) {
652         printf("Unexpected success creating the connection\n");
653         goto end;
654     }
655
656     if (ocsp_client_called || ocsp_server_called) {
657         printf("OCSP callbacks successfully called unexpectedly\n");
658         goto end;
659     }
660
661     SSL_free(serverssl);
662     SSL_free(clientssl);
663     serverssl = NULL;
664     clientssl = NULL;
665
666     /*
667      * This time we'll get the client to send an OCSP_RESPID that it will
668      * accept.
669      */
670     ocsp_client_called = 0;
671     ocsp_server_called = 0;
672     cdummyarg = 2;
673
674     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
675         printf("Unable to create SSL objects\n");
676         goto end;
677     }
678
679     /*
680      * We'll just use any old cert for this test - it doesn't have to be an OCSP
681      * specifc one. We'll use the server cert.
682      */
683     certbio = BIO_new_file(cert, "r");
684     if (certbio == NULL) {
685         printf("Can't load the certficate file\n");
686         goto end;
687     }
688     id = OCSP_RESPID_new();
689     ids = sk_OCSP_RESPID_new_null();
690     ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
691     if (id == NULL || ids == NULL || ocspcert == NULL
692             || !OCSP_RESPID_set_by_key(id, ocspcert)
693             || !sk_OCSP_RESPID_push(ids, id)) {
694         printf("Unable to set OCSP_RESPIDs\n");
695         goto end;
696     }
697     id = NULL;
698     SSL_set_tlsext_status_ids(clientssl, ids);
699     /* Control has been transferred */
700     ids = NULL;
701
702     BIO_free(certbio);
703     certbio = NULL;
704
705     if (!create_ssl_connection(serverssl, clientssl)) {
706         printf("Unable to create SSL connection\n");
707         goto end;
708     }
709
710     if (!ocsp_client_called || !ocsp_server_called) {
711         printf("OCSP callbacks not called\n");
712         goto end;
713     }
714
715     testresult = 1;
716
717  end:
718     SSL_free(serverssl);
719     SSL_free(clientssl);
720     SSL_CTX_free(sctx);
721     SSL_CTX_free(cctx);
722     sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
723     OCSP_RESPID_free(id);
724     BIO_free(certbio);
725     X509_free(ocspcert);
726     ocspcert = NULL;
727
728     return testresult;
729 }
730 #endif
731
732 typedef struct ssl_session_test_fixture {
733     const char *test_case_name;
734     int use_ext_cache;
735     int use_int_cache;
736 } SSL_SESSION_TEST_FIXTURE;
737
738 static int new_called = 0, remove_called = 0;
739
740 static SSL_SESSION_TEST_FIXTURE
741 ssl_session_set_up(const char *const test_case_name)
742 {
743     SSL_SESSION_TEST_FIXTURE fixture;
744
745     fixture.test_case_name = test_case_name;
746     fixture.use_ext_cache = 1;
747     fixture.use_int_cache = 1;
748
749     new_called = remove_called = 0;
750
751     return fixture;
752 }
753
754 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
755 {
756 }
757
758 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
759 {
760     new_called++;
761
762     return 1;
763 }
764
765 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
766 {
767     remove_called++;
768 }
769
770 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
771 {
772     SSL_CTX *sctx = NULL, *cctx = NULL;
773     SSL *serverssl1 = NULL, *clientssl1 = NULL;
774     SSL *serverssl2 = NULL, *clientssl2 = NULL;
775 #ifndef OPENSSL_NO_TLS1_1
776     SSL *serverssl3 = NULL, *clientssl3 = NULL;
777 #endif
778     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
779     int testresult = 0;
780
781     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
782                              &cctx, cert, privkey)) {
783         printf("Unable to create SSL_CTX pair\n");
784         return 0;
785     }
786
787 #ifndef OPENSSL_NO_TLS1_2
788     /* Only allow TLS1.2 so we can force a connection failure later */
789     SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
790 #endif
791
792     /* Set up session cache */
793     if (fix.use_ext_cache) {
794         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
795         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
796     }
797     if (fix.use_int_cache) {
798         /* Also covers instance where both are set */
799         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
800     } else {
801         SSL_CTX_set_session_cache_mode(cctx,
802                                        SSL_SESS_CACHE_CLIENT
803                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
804     }
805
806     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
807                                NULL)) {
808         printf("Unable to create SSL objects\n");
809         goto end;
810     }
811
812     if (!create_ssl_connection(serverssl1, clientssl1)) {
813         printf("Unable to create SSL connection\n");
814         goto end;
815     }
816     sess1 = SSL_get1_session(clientssl1);
817     if (sess1 == NULL) {
818         printf("Unexpected NULL session\n");
819         goto end;
820     }
821
822     if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
823         /* Should have failed because it should already be in the cache */
824         printf("Unexpected success adding session to cache\n");
825         goto end;
826     }
827
828     if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
829         printf("Session not added to cache\n");
830         goto end;
831     }
832
833     if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
834         printf("Unable to create second SSL objects\n");
835         goto end;
836     }
837
838     if (!create_ssl_connection(serverssl2, clientssl2)) {
839         printf("Unable to create second SSL connection\n");
840         goto end;
841     }
842
843     sess2 = SSL_get1_session(clientssl2);
844     if (sess2 == NULL) {
845         printf("Unexpected NULL session from clientssl2\n");
846         goto end;
847     }
848
849     if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
850         printf("Remove session callback unexpectedly called\n");
851         goto end;
852     }
853
854     /*
855      * This should clear sess2 from the cache because it is a "bad" session. See
856      * SSL_set_session() documentation.
857      */
858     if (!SSL_set_session(clientssl2, sess1)) {
859         printf("Unexpected failure setting session\n");
860         goto end;
861     }
862
863     if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
864         printf("Failed to call callback to remove session\n");
865         goto end;
866     }
867
868
869     if (SSL_get_session(clientssl2) != sess1) {
870         printf("Unexpected session found\n");
871         goto end;
872     }
873
874     if (fix.use_int_cache) {
875         if (!SSL_CTX_add_session(cctx, sess2)) {
876             /*
877              * Should have succeeded because it should not already be in the cache
878              */
879             printf("Unexpected failure adding session to cache\n");
880             goto end;
881         }
882
883         if (!SSL_CTX_remove_session(cctx, sess2)) {
884             printf("Unexpected failure removing session from cache\n");
885             goto end;
886         }
887
888         /* This is for the purposes of internal cache testing...ignore the
889          * counter for external cache
890          */
891         if (fix.use_ext_cache)
892             remove_called--;
893     }
894
895     /* This shouldn't be in the cache so should fail */
896     if (SSL_CTX_remove_session(cctx, sess2)) {
897         printf("Unexpected success removing session from cache\n");
898         goto end;
899     }
900
901     if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
902         printf("Failed to call callback to remove session #2\n");
903         goto end;
904     }
905
906 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
907     /* Force a connection failure */
908     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
909
910     if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
911         printf("Unable to create third SSL objects\n");
912         goto end;
913     }
914
915     if (!SSL_set_session(clientssl3, sess1)) {
916         printf("Unable to set session for third connection\n");
917         goto end;
918     }
919
920     /* This should fail because of the mismatched protocol versions */
921     if (create_ssl_connection(serverssl3, clientssl3)) {
922         printf("Unable to create third SSL connection\n");
923         goto end;
924     }
925
926
927     /* We should have automatically removed the session from the cache */
928     if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
929         printf("Failed to call callback to remove session #2\n");
930         goto end;
931     }
932
933     if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
934         /*
935          * Should have succeeded because it should not already be in the cache
936          */
937         printf("Unexpected failure adding session to cache #2\n");
938         goto end;
939     }
940 #endif
941
942     testresult = 1;
943
944  end:
945     SSL_free(serverssl1);
946     SSL_free(clientssl1);
947     SSL_free(serverssl2);
948     SSL_free(clientssl2);
949 #ifndef OPENSSL_NO_TLS1_1
950     SSL_free(serverssl3);
951     SSL_free(clientssl3);
952 #endif
953     SSL_SESSION_free(sess1);
954     SSL_SESSION_free(sess2);
955     /*
956      * Check if we need to remove any sessions up-refed for the external cache
957      */
958     if (new_called >= 1)
959         SSL_SESSION_free(sess1);
960     if (new_called >= 2)
961         SSL_SESSION_free(sess2);
962     SSL_CTX_free(sctx);
963     SSL_CTX_free(cctx);
964
965     return testresult;
966 }
967
968 static int test_session_with_only_int_cache(void)
969 {
970     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
971
972     fixture.use_ext_cache = 0;
973
974     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
975 }
976
977 static int test_session_with_only_ext_cache(void)
978 {
979     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
980
981     fixture.use_int_cache = 0;
982
983     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
984 }
985
986 static int test_session_with_both_cache(void)
987 {
988     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
989
990     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
991 }
992
993 #define USE_NULL    0
994 #define USE_BIO_1   1
995 #define USE_BIO_2   2
996
997 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
998
999 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1000 {
1001     switch (type) {
1002     case USE_NULL:
1003         *res = NULL;
1004         break;
1005     case USE_BIO_1:
1006         *res = bio1;
1007         break;
1008     case USE_BIO_2:
1009         *res = bio2;
1010         break;
1011     }
1012 }
1013
1014 static int test_ssl_set_bio(int idx)
1015 {
1016     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1017     BIO *bio1 = NULL;
1018     BIO *bio2 = NULL;
1019     BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1020     SSL *ssl = NULL;
1021     int initrbio, initwbio, newrbio, newwbio;
1022     int testresult = 0;
1023
1024     if (ctx == NULL) {
1025         printf("Failed to allocate SSL_CTX\n");
1026         goto end;
1027     }
1028
1029     ssl = SSL_new(ctx);
1030     if (ssl == NULL) {
1031         printf("Failed to allocate SSL object\n");
1032         goto end;
1033     }
1034
1035     initrbio = idx % 3;
1036     idx /= 3;
1037     initwbio = idx % 3;
1038     idx /= 3;
1039     newrbio = idx % 3;
1040     idx /= 3;
1041     newwbio = idx;
1042     OPENSSL_assert(newwbio <= 2);
1043
1044     if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
1045             || newwbio == USE_BIO_1) {
1046         bio1 = BIO_new(BIO_s_mem());
1047         if (bio1 == NULL) {
1048             printf("Failed to allocate bio1\n");
1049             goto end;
1050         }
1051     }
1052
1053     if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
1054             || newwbio == USE_BIO_2) {
1055         bio2 = BIO_new(BIO_s_mem());
1056         if (bio2 == NULL) {
1057             printf("Failed to allocate bio2\n");
1058             goto end;
1059         }
1060     }
1061
1062     setupbio(&irbio, bio1, bio2, initrbio);
1063     setupbio(&iwbio, bio1, bio2, initwbio);
1064
1065     /*
1066      * We want to maintain our own refs to these BIO, so do an up ref for each
1067      * BIO that will have ownersip transferred in the SSL_set_bio() call
1068      */
1069     if (irbio != NULL)
1070         BIO_up_ref(irbio);
1071     if (iwbio != NULL && iwbio != irbio)
1072         BIO_up_ref(iwbio);
1073
1074     SSL_set_bio(ssl, irbio, iwbio);
1075
1076     setupbio(&nrbio, bio1, bio2, newrbio);
1077     setupbio(&nwbio, bio1, bio2, newwbio);
1078
1079     /*
1080      * We will (maybe) transfer ownership again so do more up refs.
1081      * SSL_set_bio() has some really complicated ownership rules where BIOs have
1082      * already been set!
1083      */
1084     if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
1085         BIO_up_ref(nrbio);
1086     if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1087         BIO_up_ref(nwbio);
1088
1089     SSL_set_bio(ssl, nrbio, nwbio);
1090
1091     testresult = 1;
1092
1093  end:
1094     SSL_free(ssl);
1095     BIO_free(bio1);
1096     BIO_free(bio2);
1097     /*
1098      * This test is checking that the ref counting for SSL_set_bio is correct.
1099      * If we get here and we did too many frees then we will fail in the above
1100      * functions. If we haven't done enough then this will only be detected in
1101      * a crypto-mdebug build
1102      */
1103     SSL_CTX_free(ctx);
1104
1105     return testresult;
1106 }
1107
1108 typedef struct ssl_bio_test_fixture {
1109     const char *test_case_name;
1110     int pop_ssl;
1111     enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1112 } SSL_BIO_TEST_FIXTURE;
1113
1114 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1115 {
1116     SSL_BIO_TEST_FIXTURE fixture;
1117
1118     fixture.test_case_name = test_case_name;
1119     fixture.pop_ssl = 0;
1120     fixture.change_bio = NO_BIO_CHANGE;
1121
1122     return fixture;
1123 }
1124
1125 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1126 {
1127 }
1128
1129 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1130 {
1131     BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1132     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1133     SSL *ssl = NULL;
1134     int testresult = 0;
1135
1136     if (ctx == NULL) {
1137         printf("Failed to allocate SSL_CTX\n");
1138         return 0;
1139     }
1140
1141     ssl = SSL_new(ctx);
1142     if (ssl == NULL) {
1143         printf("Failed to allocate SSL object\n");
1144         goto end;
1145     }
1146
1147     sslbio = BIO_new(BIO_f_ssl());
1148     membio1 = BIO_new(BIO_s_mem());
1149
1150     if (sslbio == NULL || membio1 == NULL) {
1151         printf("Malloc failure creating BIOs\n");
1152         goto end;
1153     }
1154
1155     BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1156
1157     /*
1158      * If anything goes wrong here then we could leak memory, so this will
1159      * be caught in a crypto-mdebug build
1160      */
1161     BIO_push(sslbio, membio1);
1162
1163     /* Verify chaning the rbio/wbio directly does not cause leaks */
1164     if (fix.change_bio != NO_BIO_CHANGE) {
1165         membio2 = BIO_new(BIO_s_mem());
1166         if (membio2 == NULL) {
1167             printf("Malloc failure creating membio2\n");
1168             goto end;
1169         }
1170         if (fix.change_bio == CHANGE_RBIO)
1171             SSL_set0_rbio(ssl, membio2);
1172         else
1173             SSL_set0_wbio(ssl, membio2);
1174     }
1175     ssl = NULL;
1176
1177     if (fix.pop_ssl)
1178         BIO_pop(sslbio);
1179     else
1180         BIO_pop(membio1);
1181
1182     testresult = 1;
1183  end:
1184     BIO_free(membio1);
1185     BIO_free(sslbio);
1186     SSL_free(ssl);
1187     SSL_CTX_free(ctx);
1188
1189     return testresult;
1190 }
1191
1192 static int test_ssl_bio_pop_next_bio(void)
1193 {
1194     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1195
1196     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1197 }
1198
1199 static int test_ssl_bio_pop_ssl_bio(void)
1200 {
1201     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1202
1203     fixture.pop_ssl = 1;
1204
1205     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1206 }
1207
1208 static int test_ssl_bio_change_rbio(void)
1209 {
1210     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1211
1212     fixture.change_bio = CHANGE_RBIO;
1213
1214     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1215 }
1216
1217 static int test_ssl_bio_change_wbio(void)
1218 {
1219     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1220
1221     fixture.change_bio = CHANGE_WBIO;
1222
1223     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1224 }
1225
1226 typedef struct {
1227     /* The list of sig algs */
1228     const int *list;
1229     /* The length of the list */
1230     size_t listlen;
1231     /* A sigalgs list in string format */
1232     const char *liststr;
1233     /* Whether setting the list should succeed */
1234     int valid;
1235     /* Whether creating a connection with the list should succeed */
1236     int connsuccess;
1237 } sigalgs_list;
1238
1239 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1240 #ifndef OPENSSL_NO_EC
1241 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1242 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1243 #endif
1244 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1245 static const int invalidlist2[] = {NID_sha256, NID_undef};
1246 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1247 static const int invalidlist4[] = {NID_sha256};
1248 static const sigalgs_list testsigalgs[] = {
1249     {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1250 #ifndef OPENSSL_NO_EC
1251     {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1252     {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1253 #endif
1254     {NULL, 0, "RSA+SHA256", 1, 1},
1255 #ifndef OPENSSL_NO_EC
1256     {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1257     {NULL, 0, "ECDSA+SHA512", 1, 0},
1258 #endif
1259     {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1260     {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1261     {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1262     {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1263     {NULL, 0, "RSA", 0, 0},
1264     {NULL, 0, "SHA256", 0, 0},
1265     {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1266     {NULL, 0, "Invalid", 0, 0}};
1267
1268 static int test_set_sigalgs(int idx)
1269 {
1270     SSL_CTX *cctx = NULL, *sctx = NULL;
1271     SSL *clientssl = NULL, *serverssl = NULL;
1272     int testresult = 0;
1273     const sigalgs_list *curr;
1274     int testctx;
1275
1276     /* Should never happen */
1277     if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
1278         return 0;
1279
1280     testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1281     curr = testctx ? &testsigalgs[idx]
1282                    : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1283
1284     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
1285                              &cctx, cert, privkey)) {
1286         printf("Unable to create SSL_CTX pair\n");
1287         return 0;
1288     }
1289
1290     /*
1291      * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1292      * for TLSv1.2 for now until we add a new API.
1293      */
1294     SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1295
1296     if (testctx) {
1297         int ret;
1298         if (curr->list != NULL)
1299             ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1300         else
1301             ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1302
1303         if (!ret) {
1304             if (curr->valid)
1305                 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1306                        idx);
1307             else
1308                 testresult = 1;
1309             goto end;
1310         }
1311         if (!curr->valid) {
1312             printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
1313             goto end;
1314         }
1315     }
1316
1317     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
1318         printf("Unable to create SSL objects\n");
1319         goto end;
1320     }
1321
1322     if (!testctx) {
1323         int ret;
1324
1325         if (curr->list != NULL)
1326             ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1327         else
1328             ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1329         if (!ret) {
1330             if (curr->valid)
1331                 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
1332             else
1333                 testresult = 1;
1334             goto end;
1335         }
1336         if (!curr->valid) {
1337             printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
1338             goto end;
1339         }
1340     }
1341
1342     if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
1343         printf("Unexpected return value creating SSL connection (%d)\n", idx);
1344         goto end;
1345     }
1346
1347     testresult = 1;
1348
1349  end:
1350     SSL_free(serverssl);
1351     SSL_free(clientssl);
1352     SSL_CTX_free(sctx);
1353     SSL_CTX_free(cctx);
1354
1355     return testresult;
1356 }
1357
1358 int test_main(int argc, char *argv[])
1359 {
1360     int testresult = 1;
1361
1362     if (argc != 3) {
1363         printf("Invalid argument count\n");
1364         return 1;
1365     }
1366
1367     cert = argv[1];
1368     privkey = argv[2];
1369
1370     ADD_TEST(test_large_message_tls);
1371     ADD_TEST(test_large_message_tls_read_ahead);
1372 #ifndef OPENSSL_NO_DTLS
1373     ADD_TEST(test_large_message_dtls);
1374 #endif
1375 #ifndef OPENSSL_NO_OCSP
1376     ADD_TEST(test_tlsext_status_type);
1377 #endif
1378     ADD_TEST(test_session_with_only_int_cache);
1379     ADD_TEST(test_session_with_only_ext_cache);
1380     ADD_TEST(test_session_with_both_cache);
1381     ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1382     ADD_TEST(test_ssl_bio_pop_next_bio);
1383     ADD_TEST(test_ssl_bio_pop_ssl_bio);
1384     ADD_TEST(test_ssl_bio_change_rbio);
1385     ADD_TEST(test_ssl_bio_change_wbio);
1386     ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1387     ADD_TEST(test_keylog);
1388 #ifndef OPENSSL_NO_TLS1_3
1389     ADD_TEST(test_keylog_no_master_key);
1390 #endif
1391
1392     testresult = run_tests(argv[0]);
1393
1394     bio_s_mempacket_test_free();
1395
1396     return testresult;
1397 }