Backport of 5b8fa43 and remove resolved TODO: see PR#3924.
[openssl.git] / ssl / 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 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_ecdh_auto(sctx, 1)) {
59         printf("Failed configuring auto ECDH\n");
60     }
61
62     if (!SSL_CTX_set_cipher_list(cctx, "AES128-SHA")) {
63         printf("Failed setting cipher list\n");
64     }
65
66     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
67     if (c_to_s_fbio == NULL) {
68         printf("Failed to create filter BIO\n");
69         goto end;
70     }
71
72     /* BIO is freed by create_ssl_connection on error */
73     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
74                                c_to_s_fbio)) {
75         printf("Unable to create SSL objects\n");
76         ERR_print_errors_fp(stdout);
77         goto end;
78     }
79
80     if (testidx == 1)
81         certstatus[RECORD_SEQUENCE] = 0xff;
82
83     /*
84      * Inject a dummy record from the next epoch. In test 0, this should never
85      * get used because the message sequence number is too big. In test 1 we set
86      * the record sequence number to be way off in the future. This should not
87      * have an impact on the record replay protection because the record should
88      * be dropped before it is marked as arrivedg
89      */
90     c_to_s_mempacket = SSL_get_wbio(clientssl1);
91     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
92     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
93                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
94
95     if (!create_ssl_connection(serverssl1, clientssl1)) {
96         printf("Unable to create SSL connection\n");
97         ERR_print_errors_fp(stdout);
98         goto end;
99     }
100
101     testresult = 1;
102  end:
103     SSL_free(serverssl1);
104     SSL_free(clientssl1);
105     SSL_CTX_free(sctx);
106     SSL_CTX_free(cctx);
107
108     return testresult;
109 }
110
111 int main(int argc, char *argv[])
112 {
113     BIO *err = NULL;
114     int testresult = 0;
115
116     if (argc != 3) {
117         printf("Invalid argument count\n");
118         return 1;
119     }
120
121     cert = argv[1];
122     privkey = argv[2];
123
124     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
125
126     SSL_library_init();
127     SSL_load_error_strings();
128
129     CRYPTO_malloc_debug_init();
130     CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
131     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
132
133     if (!test_dtls_unprocessed(0) || !test_dtls_unprocessed(1))
134         testresult = 1;
135
136     ERR_free_strings();
137     ERR_remove_thread_state(NULL);
138     EVP_cleanup();
139     CRYPTO_cleanup_all_ex_data();
140     CRYPTO_mem_leaks(err);
141     BIO_free(err);
142
143     if (!testresult)
144         printf("PASS\n");
145
146     return testresult;
147 }