4d497af1afe9e849126351f21c9bccf918528842
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/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
18 static char *cert = NULL;
19 static char *privkey = NULL;
20
21 #define NUM_TESTS   2
22
23
24 #define DUMMY_CERT_STATUS_LEN  12
25
26 static unsigned char certstatus[] = {
27     SSL3_RT_HANDSHAKE, /* Content type */
28     0xfe, 0xfd, /* Record version */
29     0, 1, /* Epoch */
30     0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
31     0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
32     SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
33     0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
34     0, 5, /* Message sequence */
35     0, 0, 0, /* Fragment offset */
36     0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
37     0x80, 0x80, 0x80, 0x80, 0x80,
38     0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
39 };
40
41 #define RECORD_SEQUENCE 10
42
43 static int test_dtls_unprocessed(int testidx)
44 {
45     SSL_CTX *sctx = NULL, *cctx = NULL;
46     SSL *serverssl1 = NULL, *clientssl1 = NULL;
47     BIO *c_to_s_fbio, *c_to_s_mempacket;
48     int testresult = 0;
49
50     printf("Starting Test %d\n", testidx);
51
52     if (!create_ssl_ctx_pair(DTLS_server_method(), DTLS_client_method(), &sctx,
53                              &cctx, cert, privkey)) {
54         printf("Unable to create SSL_CTX pair\n");
55         return 0;
56     }
57
58     if (!SSL_CTX_set_cipher_list(cctx, "ECDHE-RSA-AES256-SHA384")) {
59         printf("Failed setting cipher list\n");
60     }
61
62     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
63     if (c_to_s_fbio == NULL) {
64         printf("Failed to create filter BIO\n");
65         goto end;
66     }
67
68     /* BIO is freed by create_ssl_connection on error */
69     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
70                                c_to_s_fbio)) {
71         printf("Unable to create SSL objects\n");
72         ERR_print_errors_fp(stdout);
73         goto end;
74     }
75
76     if (testidx == 1)
77         certstatus[RECORD_SEQUENCE] = 0xff;
78
79     /*
80      * Inject a dummy record from the next epoch. In test 0, this should never
81      * get used because the message sequence number is too big. In test 1 we set
82      * the record sequence number to be way off in the future. This should not
83      * have an impact on the record replay protection because the record should
84      * be dropped before it is marked as arrived
85      */
86     c_to_s_mempacket = SSL_get_wbio(clientssl1);
87     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
88     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
89                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
90
91     if (!create_ssl_connection(serverssl1, clientssl1)) {
92         printf("Unable to create SSL connection\n");
93         ERR_print_errors_fp(stdout);
94         goto end;
95     }
96
97     testresult = 1;
98  end:
99     SSL_free(serverssl1);
100     SSL_free(clientssl1);
101     SSL_CTX_free(sctx);
102     SSL_CTX_free(cctx);
103
104     return testresult;
105 }
106
107 int main(int argc, char *argv[])
108 {
109     BIO *err = NULL;
110     int testresult = 1;
111
112     if (argc != 3) {
113         printf("Invalid argument count\n");
114         return 1;
115     }
116
117     cert = argv[1];
118     privkey = argv[2];
119
120     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
121
122     CRYPTO_set_mem_debug(1);
123     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
124
125     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
126
127     testresult = run_tests(argv[0]);
128
129     bio_f_tls_dump_filter_free();
130     bio_s_mempacket_test_free();
131
132 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
133     if (CRYPTO_mem_leaks(err) <= 0)
134         testresult = 1;
135 #endif
136     BIO_free(err);
137
138     if (!testresult)
139         printf("PASS\n");
140
141     return testresult;
142 }