dtlstest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <openssl/bio.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include "ssltestlib.h"
  15. #include "testutil.h"
  16. static char *cert = NULL;
  17. static char *privkey = NULL;
  18. static unsigned int timer_cb_count;
  19. #define NUM_TESTS 2
  20. #define DUMMY_CERT_STATUS_LEN 12
  21. static unsigned char certstatus[] = {
  22. SSL3_RT_HANDSHAKE, /* Content type */
  23. 0xfe, 0xfd, /* Record version */
  24. 0, 1, /* Epoch */
  25. 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
  26. 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
  27. SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
  28. 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
  29. 0, 5, /* Message sequence */
  30. 0, 0, 0, /* Fragment offset */
  31. 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
  32. 0x80, 0x80, 0x80, 0x80, 0x80,
  33. 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
  34. };
  35. #define RECORD_SEQUENCE 10
  36. static unsigned int timer_cb(SSL *s, unsigned int timer_us)
  37. {
  38. ++timer_cb_count;
  39. if (timer_us == 0)
  40. return 50000;
  41. else
  42. return 2 * timer_us;
  43. }
  44. static int test_dtls_unprocessed(int testidx)
  45. {
  46. SSL_CTX *sctx = NULL, *cctx = NULL;
  47. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  48. BIO *c_to_s_fbio, *c_to_s_mempacket;
  49. int testresult = 0;
  50. timer_cb_count = 0;
  51. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  52. DTLS_client_method(),
  53. DTLS1_VERSION, 0,
  54. &sctx, &cctx, cert, privkey)))
  55. return 0;
  56. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  57. goto end;
  58. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  59. if (!TEST_ptr(c_to_s_fbio))
  60. goto end;
  61. /* BIO is freed by create_ssl_connection on error */
  62. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  63. NULL, c_to_s_fbio)))
  64. goto end;
  65. DTLS_set_timer_cb(clientssl1, timer_cb);
  66. if (testidx == 1)
  67. certstatus[RECORD_SEQUENCE] = 0xff;
  68. /*
  69. * Inject a dummy record from the next epoch. In test 0, this should never
  70. * get used because the message sequence number is too big. In test 1 we set
  71. * the record sequence number to be way off in the future.
  72. */
  73. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  74. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  75. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  76. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  77. /*
  78. * Create the connection. We use "create_bare_ssl_connection" here so that
  79. * we can force the connection to not do "SSL_read" once partly connected.
  80. * We don't want to accidentally read the dummy records we injected because
  81. * they will fail to decrypt.
  82. */
  83. if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
  84. SSL_ERROR_NONE, 0)))
  85. goto end;
  86. if (timer_cb_count == 0) {
  87. printf("timer_callback was not called.\n");
  88. goto end;
  89. }
  90. testresult = 1;
  91. end:
  92. SSL_free(serverssl1);
  93. SSL_free(clientssl1);
  94. SSL_CTX_free(sctx);
  95. SSL_CTX_free(cctx);
  96. return testresult;
  97. }
  98. #define CLI_TO_SRV_EPOCH_0_RECS 3
  99. #define CLI_TO_SRV_EPOCH_1_RECS 1
  100. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  101. # define SRV_TO_CLI_EPOCH_0_RECS 10
  102. #else
  103. /*
  104. * In this case we have no ServerKeyExchange message, because we don't have
  105. * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
  106. * test.
  107. */
  108. # define SRV_TO_CLI_EPOCH_0_RECS 9
  109. #endif
  110. #define SRV_TO_CLI_EPOCH_1_RECS 1
  111. #define TOTAL_FULL_HAND_RECORDS \
  112. (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
  113. SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
  114. #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
  115. #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
  116. #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
  117. #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
  118. #define TOTAL_RESUME_HAND_RECORDS \
  119. (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
  120. SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
  121. #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
  122. static int test_dtls_drop_records(int idx)
  123. {
  124. SSL_CTX *sctx = NULL, *cctx = NULL;
  125. SSL *serverssl = NULL, *clientssl = NULL;
  126. BIO *c_to_s_fbio, *mempackbio;
  127. int testresult = 0;
  128. int epoch = 0;
  129. SSL_SESSION *sess = NULL;
  130. int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
  131. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  132. DTLS_client_method(),
  133. DTLS1_VERSION, 0,
  134. &sctx, &cctx, cert, privkey)))
  135. return 0;
  136. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  137. goto end;
  138. if (idx >= TOTAL_FULL_HAND_RECORDS) {
  139. /* We're going to do a resumption handshake. Get a session first. */
  140. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  141. NULL, NULL))
  142. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  143. SSL_ERROR_NONE))
  144. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  145. goto end;
  146. SSL_shutdown(clientssl);
  147. SSL_shutdown(serverssl);
  148. SSL_free(serverssl);
  149. SSL_free(clientssl);
  150. serverssl = clientssl = NULL;
  151. cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
  152. cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
  153. srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
  154. idx -= TOTAL_FULL_HAND_RECORDS;
  155. } else {
  156. cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
  157. cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
  158. srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
  159. }
  160. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  161. if (!TEST_ptr(c_to_s_fbio))
  162. goto end;
  163. /* BIO is freed by create_ssl_connection on error */
  164. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  165. NULL, c_to_s_fbio)))
  166. goto end;
  167. if (sess != NULL) {
  168. if (!TEST_true(SSL_set_session(clientssl, sess)))
  169. goto end;
  170. }
  171. DTLS_set_timer_cb(clientssl, timer_cb);
  172. DTLS_set_timer_cb(serverssl, timer_cb);
  173. /* Work out which record to drop based on the test number */
  174. if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
  175. mempackbio = SSL_get_wbio(serverssl);
  176. idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
  177. if (idx >= srv_to_cli_epoch0) {
  178. epoch = 1;
  179. idx -= srv_to_cli_epoch0;
  180. }
  181. } else {
  182. mempackbio = SSL_get_wbio(clientssl);
  183. if (idx >= cli_to_srv_epoch0) {
  184. epoch = 1;
  185. idx -= cli_to_srv_epoch0;
  186. }
  187. mempackbio = BIO_next(mempackbio);
  188. }
  189. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
  190. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
  191. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  192. goto end;
  193. if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
  194. goto end;
  195. /* If the test did what we planned then it should have dropped a record */
  196. if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
  197. NULL), -1))
  198. goto end;
  199. testresult = 1;
  200. end:
  201. SSL_SESSION_free(sess);
  202. SSL_free(serverssl);
  203. SSL_free(clientssl);
  204. SSL_CTX_free(sctx);
  205. SSL_CTX_free(cctx);
  206. return testresult;
  207. }
  208. static const char dummy_cookie[] = "0123456";
  209. static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
  210. unsigned int *cookie_len)
  211. {
  212. memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
  213. *cookie_len = sizeof(dummy_cookie);
  214. return 1;
  215. }
  216. static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
  217. unsigned int cookie_len)
  218. {
  219. return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
  220. }
  221. static int test_cookie(void)
  222. {
  223. SSL_CTX *sctx = NULL, *cctx = NULL;
  224. SSL *serverssl = NULL, *clientssl = NULL;
  225. int testresult = 0;
  226. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  227. DTLS_client_method(),
  228. DTLS1_VERSION, 0,
  229. &sctx, &cctx, cert, privkey)))
  230. return 0;
  231. SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
  232. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  233. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  234. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  235. NULL, NULL))
  236. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  237. SSL_ERROR_NONE)))
  238. goto end;
  239. testresult = 1;
  240. end:
  241. SSL_free(serverssl);
  242. SSL_free(clientssl);
  243. SSL_CTX_free(sctx);
  244. SSL_CTX_free(cctx);
  245. return testresult;
  246. }
  247. static int test_dtls_duplicate_records(void)
  248. {
  249. SSL_CTX *sctx = NULL, *cctx = NULL;
  250. SSL *serverssl = NULL, *clientssl = NULL;
  251. int testresult = 0;
  252. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  253. DTLS_client_method(),
  254. DTLS1_VERSION, 0,
  255. &sctx, &cctx, cert, privkey)))
  256. return 0;
  257. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  258. NULL, NULL)))
  259. goto end;
  260. DTLS_set_timer_cb(clientssl, timer_cb);
  261. DTLS_set_timer_cb(serverssl, timer_cb);
  262. BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  263. BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  264. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  265. goto end;
  266. testresult = 1;
  267. end:
  268. SSL_free(serverssl);
  269. SSL_free(clientssl);
  270. SSL_CTX_free(sctx);
  271. SSL_CTX_free(cctx);
  272. return testresult;
  273. }
  274. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  275. int setup_tests(void)
  276. {
  277. if (!test_skip_common_options()) {
  278. TEST_error("Error parsing test options\n");
  279. return 0;
  280. }
  281. if (!TEST_ptr(cert = test_get_argument(0))
  282. || !TEST_ptr(privkey = test_get_argument(1)))
  283. return 0;
  284. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  285. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  286. ADD_TEST(test_cookie);
  287. ADD_TEST(test_dtls_duplicate_records);
  288. return 1;
  289. }
  290. void cleanup_tests(void)
  291. {
  292. bio_f_tls_dump_filter_free();
  293. bio_s_mempacket_test_free();
  294. }