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