Cleanup cert config files for tests
[openssl.git] / test / sslcorrupttest.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include "ssltestlib.h"
12 #include "testutil.h"
13
14 DEFINE_STACK_OF(SSL_CIPHER)
15
16 static int docorrupt = 0;
17
18 static void copy_flags(BIO *bio)
19 {
20     int flags;
21     BIO *next = BIO_next(bio);
22
23     flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
24     BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
25     BIO_set_flags(bio, flags);
26 }
27
28 static int tls_corrupt_read(BIO *bio, char *out, int outl)
29 {
30     int ret;
31     BIO *next = BIO_next(bio);
32
33     ret = BIO_read(next, out, outl);
34     copy_flags(bio);
35
36     return ret;
37 }
38
39 static int tls_corrupt_write(BIO *bio, const char *in, int inl)
40 {
41     int ret;
42     BIO *next = BIO_next(bio);
43     char *copy;
44
45     if (docorrupt) {
46         if (!TEST_ptr(copy = OPENSSL_memdup(in, inl)))
47             return 0;
48         /* corrupt last bit of application data */
49         copy[inl-1] ^= 1;
50         ret = BIO_write(next, copy, inl);
51         OPENSSL_free(copy);
52     } else {
53         ret = BIO_write(next, in, inl);
54     }
55     copy_flags(bio);
56
57     return ret;
58 }
59
60 static long tls_corrupt_ctrl(BIO *bio, int cmd, long num, void *ptr)
61 {
62     long ret;
63     BIO *next = BIO_next(bio);
64
65     if (next == NULL)
66         return 0;
67
68     switch (cmd) {
69     case BIO_CTRL_DUP:
70         ret = 0L;
71         break;
72     default:
73         ret = BIO_ctrl(next, cmd, num, ptr);
74         break;
75     }
76     return ret;
77 }
78
79 static int tls_corrupt_gets(BIO *bio, char *buf, int size)
80 {
81     /* We don't support this - not needed anyway */
82     return -1;
83 }
84
85 static int tls_corrupt_puts(BIO *bio, const char *str)
86 {
87     /* We don't support this - not needed anyway */
88     return -1;
89 }
90
91 static int tls_corrupt_new(BIO *bio)
92 {
93     BIO_set_init(bio, 1);
94
95     return 1;
96 }
97
98 static int tls_corrupt_free(BIO *bio)
99 {
100     BIO_set_init(bio, 0);
101
102     return 1;
103 }
104
105 #define BIO_TYPE_CUSTOM_FILTER  (0x80 | BIO_TYPE_FILTER)
106
107 static BIO_METHOD *method_tls_corrupt = NULL;
108
109 /* Note: Not thread safe! */
110 static const BIO_METHOD *bio_f_tls_corrupt_filter(void)
111 {
112     if (method_tls_corrupt == NULL) {
113         method_tls_corrupt = BIO_meth_new(BIO_TYPE_CUSTOM_FILTER,
114                                           "TLS corrupt filter");
115         if (   method_tls_corrupt == NULL
116             || !BIO_meth_set_write(method_tls_corrupt, tls_corrupt_write)
117             || !BIO_meth_set_read(method_tls_corrupt, tls_corrupt_read)
118             || !BIO_meth_set_puts(method_tls_corrupt, tls_corrupt_puts)
119             || !BIO_meth_set_gets(method_tls_corrupt, tls_corrupt_gets)
120             || !BIO_meth_set_ctrl(method_tls_corrupt, tls_corrupt_ctrl)
121             || !BIO_meth_set_create(method_tls_corrupt, tls_corrupt_new)
122             || !BIO_meth_set_destroy(method_tls_corrupt, tls_corrupt_free))
123             return NULL;
124     }
125     return method_tls_corrupt;
126 }
127
128 static void bio_f_tls_corrupt_filter_free(void)
129 {
130     BIO_meth_free(method_tls_corrupt);
131 }
132
133 /*
134  * The test is supposed to be executed with RSA key, customarily
135  * with apps/server.pem used even in other tests. For this reason
136  * |cipher_list| is initialized with RSA ciphers' names. This
137  * naturally means that if test is to be re-purposed for other
138  * type of key, then NID_auth_* filter below would need adjustment.
139  */
140 static const char **cipher_list = NULL;
141
142 static int setup_cipher_list(void)
143 {
144     SSL_CTX *ctx = NULL;
145     SSL *ssl = NULL;
146     STACK_OF(SSL_CIPHER) *sk_ciphers = NULL;
147     int i, j, numciphers = 0;
148
149     if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method()))
150             || !TEST_ptr(ssl = SSL_new(ctx))
151             || !TEST_ptr(sk_ciphers = SSL_get1_supported_ciphers(ssl)))
152         goto err;
153
154     /*
155      * The |cipher_list| will be filled only with names of RSA ciphers,
156      * so that some of the allocated space will be wasted, but the loss
157      * is deemed acceptable...
158      */
159     cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) *
160                                  sizeof(cipher_list[0]));
161     if (!TEST_ptr(cipher_list))
162         goto err;
163
164     for (j = 0, i = 0; i < sk_SSL_CIPHER_num(sk_ciphers); i++) {
165         const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(sk_ciphers, i);
166
167         if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa)
168             cipher_list[j++] = SSL_CIPHER_get_name(cipher);
169     }
170     if (TEST_int_ne(j, 0))
171         numciphers = j;
172
173 err:
174     sk_SSL_CIPHER_free(sk_ciphers);
175     SSL_free(ssl);
176     SSL_CTX_free(ctx);
177
178     return numciphers;
179 }
180
181 static char *cert = NULL;
182 static char *privkey = NULL;
183
184 static int test_ssl_corrupt(int testidx)
185 {
186     static unsigned char junk[16000] = { 0 };
187     SSL_CTX *sctx = NULL, *cctx = NULL;
188     SSL *server = NULL, *client = NULL;
189     BIO *c_to_s_fbio;
190     int testresult = 0;
191     STACK_OF(SSL_CIPHER) *ciphers;
192     const SSL_CIPHER *currcipher;
193
194     docorrupt = 0;
195
196     TEST_info("Starting #%d, %s", testidx, cipher_list[testidx]);
197
198     if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(),
199                                        TLS_client_method(),
200                                        TLS1_VERSION, 0,
201                                        &sctx, &cctx, cert, privkey)))
202         return 0;
203
204     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher_list[testidx]))
205             || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ""))
206             || !TEST_ptr(ciphers = SSL_CTX_get_ciphers(cctx))
207             || !TEST_int_eq(sk_SSL_CIPHER_num(ciphers), 1)
208             || !TEST_ptr(currcipher = sk_SSL_CIPHER_value(ciphers, 0)))
209         goto end;
210
211     /*
212      * No ciphers we are using are TLSv1.3 compatible so we should not attempt
213      * to negotiate TLSv1.3
214      */
215     if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)))
216         goto end;
217
218     if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter())))
219         goto end;
220
221     /* BIO is freed by create_ssl_connection on error */
222     if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL,
223                                       c_to_s_fbio)))
224         goto end;
225
226     if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE)))
227         goto end;
228
229     docorrupt = 1;
230
231     if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0))
232         goto end;
233
234     if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0))
235         goto end;
236
237     if (!TEST_int_eq(ERR_GET_REASON(ERR_peek_error()),
238                      SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC))
239         goto end;
240
241     testresult = 1;
242  end:
243     SSL_free(server);
244     SSL_free(client);
245     SSL_CTX_free(sctx);
246     SSL_CTX_free(cctx);
247     return testresult;
248 }
249
250 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
251
252 int setup_tests(void)
253 {
254     int n;
255
256     if (!test_skip_common_options()) {
257         TEST_error("Error parsing test options\n");
258         return 0;
259     }
260
261     if (!TEST_ptr(cert = test_get_argument(0))
262             || !TEST_ptr(privkey = test_get_argument(1)))
263         return 0;
264
265     n = setup_cipher_list();
266     if (n > 0)
267         ADD_ALL_TESTS(test_ssl_corrupt, n);
268     return 1;
269 }
270
271 void cleanup_tests(void)
272 {
273     bio_f_tls_corrupt_filter_free();
274     OPENSSL_free(cipher_list);
275 }