sslcorrupttest.c 7.3 KB

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