Fix test/recipes/95-test_external_krb5.t
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016-2017 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/bio.h>
11 #include <openssl/crypto.h>
12 #include <openssl/ssl.h>
13 #include <openssl/err.h>
14
15 #include "ssltestlib.h"
16 #include "testutil.h"
17 #include "test_main_custom.h"
18
19 static char *cert = NULL;
20 static char *privkey = NULL;
21
22 #define NUM_TESTS   2
23
24
25 #define DUMMY_CERT_STATUS_LEN  12
26
27 static unsigned char certstatus[] = {
28     SSL3_RT_HANDSHAKE, /* Content type */
29     0xfe, 0xfd, /* Record version */
30     0, 1, /* Epoch */
31     0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
32     0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
33     SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
34     0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
35     0, 5, /* Message sequence */
36     0, 0, 0, /* Fragment offset */
37     0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
38     0x80, 0x80, 0x80, 0x80, 0x80,
39     0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
40 };
41
42 #define RECORD_SEQUENCE 10
43
44 static int test_dtls_unprocessed(int testidx)
45 {
46     SSL_CTX *sctx = NULL, *cctx = NULL;
47     SSL *serverssl1 = NULL, *clientssl1 = NULL;
48     BIO *c_to_s_fbio, *c_to_s_mempacket;
49     int testresult = 0;
50
51     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
52                                        DTLS_client_method(), &sctx,
53                                        &cctx, cert, privkey)))
54         return 0;
55
56     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
57         goto end;
58
59     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
60     if (!TEST_ptr(c_to_s_fbio))
61         goto end;
62
63     /* BIO is freed by create_ssl_connection on error */
64     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
65                                       NULL, c_to_s_fbio)))
66         goto end;
67
68     if (testidx == 1)
69         certstatus[RECORD_SEQUENCE] = 0xff;
70
71     /*
72      * Inject a dummy record from the next epoch. In test 0, this should never
73      * get used because the message sequence number is too big. In test 1 we set
74      * the record sequence number to be way off in the future. This should not
75      * have an impact on the record replay protection because the record should
76      * be dropped before it is marked as arrived
77      */
78     c_to_s_mempacket = SSL_get_wbio(clientssl1);
79     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
80     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
81                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
82
83     if (!TEST_true(create_ssl_connection(serverssl1, clientssl1,
84                                          SSL_ERROR_NONE)))
85         goto end;
86
87     testresult = 1;
88  end:
89     SSL_free(serverssl1);
90     SSL_free(clientssl1);
91     SSL_CTX_free(sctx);
92     SSL_CTX_free(cctx);
93
94     return testresult;
95 }
96
97 int test_main(int argc, char *argv[])
98 {
99     int testresult = 1;
100
101     if (!TEST_int_eq(argc, 3))
102         return 1;
103
104     cert = argv[1];
105     privkey = argv[2];
106
107     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
108
109     testresult = run_tests(argv[0]);
110
111     bio_f_tls_dump_filter_free();
112     bio_s_mempacket_test_free();
113
114     return testresult;
115 }