2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "ssltestlib.h"
14 DEFINE_STACK_OF(SSL_CIPHER)
16 static int docorrupt = 0;
18 static void copy_flags(BIO *bio)
21 BIO *next = BIO_next(bio);
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);
28 static int tls_corrupt_read(BIO *bio, char *out, int outl)
31 BIO *next = BIO_next(bio);
33 ret = BIO_read(next, out, outl);
39 static int tls_corrupt_write(BIO *bio, const char *in, int inl)
42 BIO *next = BIO_next(bio);
46 if (!TEST_ptr(copy = OPENSSL_memdup(in, inl)))
48 /* corrupt last bit of application data */
50 ret = BIO_write(next, copy, inl);
53 ret = BIO_write(next, in, inl);
60 static long tls_corrupt_ctrl(BIO *bio, int cmd, long num, void *ptr)
63 BIO *next = BIO_next(bio);
73 ret = BIO_ctrl(next, cmd, num, ptr);
79 static int tls_corrupt_gets(BIO *bio, char *buf, int size)
81 /* We don't support this - not needed anyway */
85 static int tls_corrupt_puts(BIO *bio, const char *str)
87 /* We don't support this - not needed anyway */
91 static int tls_corrupt_new(BIO *bio)
98 static int tls_corrupt_free(BIO *bio)
100 BIO_set_init(bio, 0);
105 #define BIO_TYPE_CUSTOM_FILTER (0x80 | BIO_TYPE_FILTER)
107 static BIO_METHOD *method_tls_corrupt = NULL;
109 /* Note: Not thread safe! */
110 static const BIO_METHOD *bio_f_tls_corrupt_filter(void)
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))
125 return method_tls_corrupt;
128 static void bio_f_tls_corrupt_filter_free(void)
130 BIO_meth_free(method_tls_corrupt);
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.
140 static const char **cipher_list = NULL;
142 static int setup_cipher_list(void)
146 STACK_OF(SSL_CIPHER) *sk_ciphers = NULL;
147 int i, j, numciphers = 0;
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)))
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...
159 cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) *
160 sizeof(cipher_list[0]));
161 if (!TEST_ptr(cipher_list))
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);
167 if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa)
168 cipher_list[j++] = SSL_CIPHER_get_name(cipher);
170 if (TEST_int_ne(j, 0))
174 sk_SSL_CIPHER_free(sk_ciphers);
181 static char *cert = NULL;
182 static char *privkey = NULL;
184 static int test_ssl_corrupt(int testidx)
186 static unsigned char junk[16000] = { 0 };
187 SSL_CTX *sctx = NULL, *cctx = NULL;
188 SSL *server = NULL, *client = NULL;
191 STACK_OF(SSL_CIPHER) *ciphers;
192 const SSL_CIPHER *currcipher;
196 TEST_info("Starting #%d, %s", testidx, cipher_list[testidx]);
198 if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(),
201 &sctx, &cctx, cert, privkey)))
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)))
212 * No ciphers we are using are TLSv1.3 compatible so we should not attempt
213 * to negotiate TLSv1.3
215 if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)))
218 if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter())))
221 /* BIO is freed by create_ssl_connection on error */
222 if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL,
226 if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE)))
231 if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0))
234 if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0))
237 if (!TEST_int_eq(ERR_GET_REASON(ERR_peek_error()),
238 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC))
250 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
252 int setup_tests(void)
256 if (!test_skip_common_options()) {
257 TEST_error("Error parsing test options\n");
261 if (!TEST_ptr(cert = test_get_argument(0))
262 || !TEST_ptr(privkey = test_get_argument(1)))
265 n = setup_cipher_list();
267 ADD_ALL_TESTS(test_ssl_corrupt, n);
271 void cleanup_tests(void)
273 bio_f_tls_corrupt_filter_free();
274 OPENSSL_free(cipher_list);