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