sslcorrupttest.c 7.0 KB

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