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