2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "ssltestlib.h"
14 static int docorrupt = 0;
16 static void copy_flags(BIO *bio)
19 BIO *next = BIO_next(bio);
21 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
22 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
23 BIO_set_flags(bio, flags);
26 static int tls_corrupt_read(BIO *bio, char *out, int outl)
29 BIO *next = BIO_next(bio);
31 ret = BIO_read(next, out, outl);
37 static int tls_corrupt_write(BIO *bio, const char *in, int inl)
40 BIO *next = BIO_next(bio);
44 if (!TEST_ptr(copy = BUF_memdup(in, inl)))
46 /* corrupt last bit of application data */
48 ret = BIO_write(next, copy, inl);
51 ret = BIO_write(next, in, inl);
58 static long tls_corrupt_ctrl(BIO *bio, int cmd, long num, void *ptr)
61 BIO *next = BIO_next(bio);
71 ret = BIO_ctrl(next, cmd, num, ptr);
77 static int tls_corrupt_gets(BIO *bio, char *buf, int size)
79 /* We don't support this - not needed anyway */
83 static int tls_corrupt_puts(BIO *bio, const char *str)
85 /* We don't support this - not needed anyway */
89 static int tls_corrupt_new(BIO *bio)
96 static int tls_corrupt_free(BIO *bio)
103 #define BIO_TYPE_CUSTOM_FILTER (0x80 | BIO_TYPE_FILTER)
105 static BIO_METHOD *method_tls_corrupt = NULL;
107 /* Note: Not thread safe! */
108 static const BIO_METHOD *bio_f_tls_corrupt_filter(void)
110 if (method_tls_corrupt == NULL) {
111 method_tls_corrupt = BIO_meth_new(BIO_TYPE_CUSTOM_FILTER,
112 "TLS corrupt filter");
113 if ( method_tls_corrupt == NULL
114 || !BIO_meth_set_write(method_tls_corrupt, tls_corrupt_write)
115 || !BIO_meth_set_read(method_tls_corrupt, tls_corrupt_read)
116 || !BIO_meth_set_puts(method_tls_corrupt, tls_corrupt_puts)
117 || !BIO_meth_set_gets(method_tls_corrupt, tls_corrupt_gets)
118 || !BIO_meth_set_ctrl(method_tls_corrupt, tls_corrupt_ctrl)
119 || !BIO_meth_set_create(method_tls_corrupt, tls_corrupt_new)
120 || !BIO_meth_set_destroy(method_tls_corrupt, tls_corrupt_free))
123 return method_tls_corrupt;
126 static void bio_f_tls_corrupt_filter_free(void)
128 BIO_meth_free(method_tls_corrupt);
132 * The test is supposed to be executed with RSA key, customarily
133 * with apps/server.pem used even in other tests. For this reason
134 * |cipher_list| is initialized with RSA ciphers' names. This
135 * naturally means that if test is to be re-purposed for other
136 * type of key, then NID_auth_* filter below would need adjustment.
138 static const char **cipher_list = NULL;
140 static int setup_cipher_list(void)
144 STACK_OF(SSL_CIPHER) *sk_ciphers = NULL;
145 int i, j, numciphers = 0;
147 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method()))
148 || !TEST_ptr(ssl = SSL_new(ctx))
149 || !TEST_ptr(sk_ciphers = SSL_get1_supported_ciphers(ssl)))
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...
157 cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) *
158 sizeof(cipher_list[0]));
159 if (!TEST_ptr(cipher_list))
162 for (j = 0, i = 0; i < sk_SSL_CIPHER_num(sk_ciphers); i++) {
163 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(sk_ciphers, i);
165 if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa)
166 cipher_list[j++] = SSL_CIPHER_get_name(cipher);
168 if (TEST_int_ne(j, 0))
172 sk_SSL_CIPHER_free(sk_ciphers);
179 static char *cert = NULL;
180 static char *privkey = NULL;
182 static int test_ssl_corrupt(int testidx)
184 static unsigned char junk[16000] = { 0 };
185 SSL_CTX *sctx = NULL, *cctx = NULL;
186 SSL *server = NULL, *client = NULL;
189 STACK_OF(SSL_CIPHER) *ciphers;
190 const SSL_CIPHER *currcipher;
194 TEST_info("Starting #%d, %s", testidx, cipher_list[testidx]);
196 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
197 TLS1_VERSION, TLS_MAX_VERSION,
198 &sctx, &cctx, cert, privkey)))
201 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher_list[testidx]))
202 || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ""))
203 || !TEST_ptr(ciphers = SSL_CTX_get_ciphers(cctx))
204 || !TEST_int_eq(sk_SSL_CIPHER_num(ciphers), 1)
205 || !TEST_ptr(currcipher = sk_SSL_CIPHER_value(ciphers, 0)))
209 * No ciphers we are using are TLSv1.3 compatible so we should not attempt
210 * to negotiate TLSv1.3
212 if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)))
215 if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter())))
218 /* BIO is freed by create_ssl_connection on error */
219 if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL,
223 if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE)))
228 if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0))
231 if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0))
234 if (!TEST_int_eq(ERR_GET_REASON(ERR_peek_error()),
235 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC))
247 int setup_tests(void)
251 if (!TEST_ptr(cert = test_get_argument(0))
252 || !TEST_ptr(privkey = test_get_argument(1))) {
253 TEST_note("Usage error: require cert and private key files");
257 n = setup_cipher_list();
259 ADD_ALL_TESTS(test_ssl_corrupt, n);
263 void cleanup_tests(void)
265 bio_f_tls_corrupt_filter_free();
266 OPENSSL_free(cipher_list);