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