dtlstest.c 11 KB

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