dtls_mtu_test.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2016-2022 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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/dtls1.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include "helpers/ssltestlib.h"
  15. #include "testutil.h"
  16. /* for SSL_READ_ETM() */
  17. #include "../ssl/ssl_local.h"
  18. static int debug = 0;
  19. static unsigned int clnt_psk_callback(SSL *ssl, const char *hint,
  20. char *ident, unsigned int max_ident_len,
  21. unsigned char *psk,
  22. unsigned int max_psk_len)
  23. {
  24. BIO_snprintf(ident, max_ident_len, "psk");
  25. if (max_psk_len > 20)
  26. max_psk_len = 20;
  27. memset(psk, 0x5a, max_psk_len);
  28. return max_psk_len;
  29. }
  30. static unsigned int srvr_psk_callback(SSL *ssl, const char *identity,
  31. unsigned char *psk,
  32. unsigned int max_psk_len)
  33. {
  34. if (max_psk_len > 20)
  35. max_psk_len = 20;
  36. memset(psk, 0x5a, max_psk_len);
  37. return max_psk_len;
  38. }
  39. static int mtu_test(SSL_CTX *ctx, const char *cs, int no_etm)
  40. {
  41. SSL *srvr_ssl = NULL, *clnt_ssl = NULL;
  42. BIO *sc_bio = NULL;
  43. int i;
  44. size_t s;
  45. size_t mtus[30];
  46. unsigned char buf[600];
  47. int rv = 0;
  48. SSL_CONNECTION *clnt_sc;
  49. memset(buf, 0x5a, sizeof(buf));
  50. if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl,
  51. NULL, NULL)))
  52. goto end;
  53. if (no_etm)
  54. SSL_set_options(srvr_ssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
  55. if (!TEST_true(SSL_set_cipher_list(srvr_ssl, cs))
  56. || !TEST_true(SSL_set_cipher_list(clnt_ssl, cs))
  57. || !TEST_ptr(sc_bio = SSL_get_rbio(srvr_ssl))
  58. || !TEST_true(create_ssl_connection(clnt_ssl, srvr_ssl,
  59. SSL_ERROR_NONE)))
  60. goto end;
  61. if (debug)
  62. TEST_info("Channel established");
  63. /* For record MTU values between 500 and 539, call DTLS_get_data_mtu()
  64. * to query the payload MTU which will fit. */
  65. for (i = 0; i < 30; i++) {
  66. SSL_set_mtu(clnt_ssl, 500 + i);
  67. mtus[i] = DTLS_get_data_mtu(clnt_ssl);
  68. if (debug)
  69. TEST_info("%s%s MTU for record mtu %d = %lu",
  70. cs, no_etm ? "-noEtM" : "",
  71. 500 + i, (unsigned long)mtus[i]);
  72. if (!TEST_size_t_ne(mtus[i], 0)) {
  73. TEST_info("Cipher %s MTU %d", cs, 500 + i);
  74. goto end;
  75. }
  76. }
  77. /* Now get out of the way */
  78. SSL_set_mtu(clnt_ssl, 1000);
  79. /*
  80. * Now for all values in the range of payload MTUs, send a payload of
  81. * that size and see what actual record size we end up with.
  82. */
  83. for (s = mtus[0]; s <= mtus[29]; s++) {
  84. size_t reclen;
  85. if (!TEST_int_eq(SSL_write(clnt_ssl, buf, s), (int)s))
  86. goto end;
  87. reclen = BIO_read(sc_bio, buf, sizeof(buf));
  88. if (debug)
  89. TEST_info("record %zu for payload %zu", reclen, s);
  90. for (i = 0; i < 30; i++) {
  91. /* DTLS_get_data_mtu() with record MTU 500+i returned mtus[i] ... */
  92. if (!TEST_false(s <= mtus[i] && reclen > (size_t)(500 + i))) {
  93. /*
  94. * We sent a packet smaller than or equal to mtus[j] and
  95. * that made a record *larger* than the record MTU 500+j!
  96. */
  97. TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",
  98. cs, (unsigned long)s, (unsigned long)mtus[i],
  99. (unsigned long)reclen, 500 + i);
  100. goto end;
  101. }
  102. if (!TEST_false(s > mtus[i] && reclen <= (size_t)(500 + i))) {
  103. /*
  104. * We sent a *larger* packet than mtus[i] and that *still*
  105. * fits within the record MTU 500+i, so DTLS_get_data_mtu()
  106. * was overly pessimistic.
  107. */
  108. TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",
  109. cs, (unsigned long)s, (unsigned long)mtus[i],
  110. (unsigned long)reclen, 500 + i);
  111. goto end;
  112. }
  113. }
  114. }
  115. if (!TEST_ptr(clnt_sc = SSL_CONNECTION_FROM_SSL_ONLY(clnt_ssl)))
  116. goto end;
  117. rv = 1;
  118. if (SSL_READ_ETM(clnt_sc))
  119. rv = 2;
  120. end:
  121. SSL_free(clnt_ssl);
  122. SSL_free(srvr_ssl);
  123. return rv;
  124. }
  125. static int run_mtu_tests(void)
  126. {
  127. SSL_CTX *ctx = NULL;
  128. STACK_OF(SSL_CIPHER) *ciphers;
  129. int i, ret = 0;
  130. if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method())))
  131. goto end;
  132. SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback);
  133. SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback);
  134. SSL_CTX_set_security_level(ctx, 0);
  135. /*
  136. * We only care about iterating over each enc/mac; we don't want to
  137. * repeat the test for each auth/kx variant. So keep life simple and
  138. * only do (non-DH) PSK.
  139. */
  140. if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "PSK")))
  141. goto end;
  142. ciphers = SSL_CTX_get_ciphers(ctx);
  143. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  144. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  145. const char *cipher_name = SSL_CIPHER_get_name(cipher);
  146. /* As noted above, only one test for each enc/mac variant. */
  147. if (!HAS_PREFIX(cipher_name, "PSK-"))
  148. continue;
  149. if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 0), 0))
  150. break;
  151. TEST_info("%s OK", cipher_name);
  152. if (ret == 1)
  153. continue;
  154. /* mtu_test() returns 2 if it used Encrypt-then-MAC */
  155. if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 1), 0))
  156. break;
  157. TEST_info("%s without EtM OK", cipher_name);
  158. }
  159. end:
  160. SSL_CTX_free(ctx);
  161. return ret;
  162. }
  163. static int test_server_mtu_larger_than_max_fragment_length(void)
  164. {
  165. SSL_CTX *ctx = NULL;
  166. SSL *srvr_ssl = NULL, *clnt_ssl = NULL;
  167. int rv = 0;
  168. if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method())))
  169. goto end;
  170. SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback);
  171. SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback);
  172. #ifndef OPENSSL_NO_DH
  173. if (!TEST_true(SSL_CTX_set_dh_auto(ctx, 1)))
  174. goto end;
  175. #endif
  176. if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl,
  177. NULL, NULL)))
  178. goto end;
  179. SSL_set_options(srvr_ssl, SSL_OP_NO_QUERY_MTU);
  180. if (!TEST_true(DTLS_set_link_mtu(srvr_ssl, 1500)))
  181. goto end;
  182. SSL_set_tlsext_max_fragment_length(clnt_ssl,
  183. TLSEXT_max_fragment_length_512);
  184. if (!TEST_true(create_ssl_connection(srvr_ssl, clnt_ssl,
  185. SSL_ERROR_NONE)))
  186. goto end;
  187. rv = 1;
  188. end:
  189. SSL_free(clnt_ssl);
  190. SSL_free(srvr_ssl);
  191. SSL_CTX_free(ctx);
  192. return rv;
  193. }
  194. int setup_tests(void)
  195. {
  196. ADD_TEST(run_mtu_tests);
  197. ADD_TEST(test_server_mtu_larger_than_max_fragment_length);
  198. return 1;
  199. }
  200. void cleanup_tests(void)
  201. {
  202. bio_s_mempacket_test_free();
  203. }