dtlstest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 (idx >= TOTAL_FULL_HAND_RECORDS) {
  137. /* We're going to do a resumption handshake. Get a session first. */
  138. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  139. NULL, NULL))
  140. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  141. SSL_ERROR_NONE))
  142. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  143. goto end;
  144. SSL_shutdown(clientssl);
  145. SSL_shutdown(serverssl);
  146. SSL_free(serverssl);
  147. SSL_free(clientssl);
  148. serverssl = clientssl = NULL;
  149. cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
  150. cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
  151. srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
  152. idx -= TOTAL_FULL_HAND_RECORDS;
  153. } else {
  154. cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
  155. cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
  156. srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
  157. }
  158. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  159. if (!TEST_ptr(c_to_s_fbio))
  160. goto end;
  161. /* BIO is freed by create_ssl_connection on error */
  162. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  163. NULL, c_to_s_fbio)))
  164. goto end;
  165. if (sess != NULL) {
  166. if (!TEST_true(SSL_set_session(clientssl, sess)))
  167. goto end;
  168. }
  169. DTLS_set_timer_cb(clientssl, timer_cb);
  170. DTLS_set_timer_cb(serverssl, timer_cb);
  171. /* Work out which record to drop based on the test number */
  172. if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
  173. mempackbio = SSL_get_wbio(serverssl);
  174. idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
  175. if (idx >= srv_to_cli_epoch0) {
  176. epoch = 1;
  177. idx -= srv_to_cli_epoch0;
  178. }
  179. } else {
  180. mempackbio = SSL_get_wbio(clientssl);
  181. if (idx >= cli_to_srv_epoch0) {
  182. epoch = 1;
  183. idx -= cli_to_srv_epoch0;
  184. }
  185. mempackbio = BIO_next(mempackbio);
  186. }
  187. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
  188. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
  189. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  190. goto end;
  191. if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
  192. goto end;
  193. /* If the test did what we planned then it should have dropped a record */
  194. if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
  195. NULL), -1))
  196. goto end;
  197. testresult = 1;
  198. end:
  199. SSL_SESSION_free(sess);
  200. SSL_free(serverssl);
  201. SSL_free(clientssl);
  202. SSL_CTX_free(sctx);
  203. SSL_CTX_free(cctx);
  204. return testresult;
  205. }
  206. static const char dummy_cookie[] = "0123456";
  207. static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
  208. unsigned int *cookie_len)
  209. {
  210. memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
  211. *cookie_len = sizeof(dummy_cookie);
  212. return 1;
  213. }
  214. static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
  215. unsigned int cookie_len)
  216. {
  217. return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
  218. }
  219. static int test_cookie(void)
  220. {
  221. SSL_CTX *sctx = NULL, *cctx = NULL;
  222. SSL *serverssl = NULL, *clientssl = NULL;
  223. int testresult = 0;
  224. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  225. DTLS_client_method(),
  226. DTLS1_VERSION, 0,
  227. &sctx, &cctx, cert, privkey)))
  228. return 0;
  229. SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
  230. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  231. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  232. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  233. NULL, NULL))
  234. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  235. SSL_ERROR_NONE)))
  236. goto end;
  237. testresult = 1;
  238. end:
  239. SSL_free(serverssl);
  240. SSL_free(clientssl);
  241. SSL_CTX_free(sctx);
  242. SSL_CTX_free(cctx);
  243. return testresult;
  244. }
  245. static int test_dtls_duplicate_records(void)
  246. {
  247. SSL_CTX *sctx = NULL, *cctx = NULL;
  248. SSL *serverssl = NULL, *clientssl = NULL;
  249. int testresult = 0;
  250. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  251. DTLS_client_method(),
  252. DTLS1_VERSION, 0,
  253. &sctx, &cctx, cert, privkey)))
  254. return 0;
  255. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  256. NULL, NULL)))
  257. goto end;
  258. DTLS_set_timer_cb(clientssl, timer_cb);
  259. DTLS_set_timer_cb(serverssl, timer_cb);
  260. BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  261. BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  262. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  263. goto end;
  264. testresult = 1;
  265. end:
  266. SSL_free(serverssl);
  267. SSL_free(clientssl);
  268. SSL_CTX_free(sctx);
  269. SSL_CTX_free(cctx);
  270. return testresult;
  271. }
  272. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  273. int setup_tests(void)
  274. {
  275. if (!test_skip_common_options()) {
  276. TEST_error("Error parsing test options\n");
  277. return 0;
  278. }
  279. if (!TEST_ptr(cert = test_get_argument(0))
  280. || !TEST_ptr(privkey = test_get_argument(1)))
  281. return 0;
  282. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  283. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  284. ADD_TEST(test_cookie);
  285. ADD_TEST(test_dtls_duplicate_records);
  286. return 1;
  287. }
  288. void cleanup_tests(void)
  289. {
  290. bio_f_tls_dump_filter_free();
  291. bio_s_mempacket_test_free();
  292. }