Add PKEY_METHOD macro tests
[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
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     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
51                                        DTLS_client_method(), &sctx,
52                                        &cctx, cert, privkey)))
53         return 0;
54
55     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
56         goto end;
57
58     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
59     if (!TEST_ptr(c_to_s_fbio))
60         goto end;
61
62     /* BIO is freed by create_ssl_connection on error */
63     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
64                                       NULL, c_to_s_fbio)))
65         goto end;
66
67     if (testidx == 1)
68         certstatus[RECORD_SEQUENCE] = 0xff;
69
70     /*
71      * Inject a dummy record from the next epoch. In test 0, this should never
72      * get used because the message sequence number is too big. In test 1 we set
73      * the record sequence number to be way off in the future. This should not
74      * have an impact on the record replay protection because the record should
75      * be dropped before it is marked as arrived
76      */
77     c_to_s_mempacket = SSL_get_wbio(clientssl1);
78     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
79     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
80                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
81
82     if (!TEST_true(create_ssl_connection(serverssl1, clientssl1,
83                                          SSL_ERROR_NONE)))
84         goto end;
85
86     testresult = 1;
87  end:
88     SSL_free(serverssl1);
89     SSL_free(clientssl1);
90     SSL_CTX_free(sctx);
91     SSL_CTX_free(cctx);
92
93     return testresult;
94 }
95
96 int setup_tests(void)
97 {
98     if (!TEST_ptr(cert = test_get_argument(0))
99             || !TEST_ptr(privkey = test_get_argument(1)))
100         return 0;
101
102     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
103     return 1;
104 }
105
106 void cleanup_tests(void)
107 {
108     bio_f_tls_dump_filter_free();
109     bio_s_mempacket_test_free();
110 }