dtlstest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 2016-2021 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. #ifndef OPENSSL_NO_DTLS1_2
  57. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  58. goto end;
  59. #else
  60. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  61. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
  62. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  63. "AES128-SHA:@SECLEVEL=0")))
  64. goto end;
  65. #endif
  66. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  67. if (!TEST_ptr(c_to_s_fbio))
  68. goto end;
  69. /* BIO is freed by create_ssl_connection on error */
  70. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  71. NULL, c_to_s_fbio)))
  72. goto end;
  73. DTLS_set_timer_cb(clientssl1, timer_cb);
  74. if (testidx == 1)
  75. certstatus[RECORD_SEQUENCE] = 0xff;
  76. /*
  77. * Inject a dummy record from the next epoch. In test 0, this should never
  78. * get used because the message sequence number is too big. In test 1 we set
  79. * the record sequence number to be way off in the future.
  80. */
  81. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  82. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  83. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  84. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  85. /*
  86. * Create the connection. We use "create_bare_ssl_connection" here so that
  87. * we can force the connection to not do "SSL_read" once partly connected.
  88. * We don't want to accidentally read the dummy records we injected because
  89. * they will fail to decrypt.
  90. */
  91. if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
  92. SSL_ERROR_NONE, 0)))
  93. goto end;
  94. if (timer_cb_count == 0) {
  95. printf("timer_callback was not called.\n");
  96. goto end;
  97. }
  98. testresult = 1;
  99. end:
  100. SSL_free(serverssl1);
  101. SSL_free(clientssl1);
  102. SSL_CTX_free(sctx);
  103. SSL_CTX_free(cctx);
  104. return testresult;
  105. }
  106. #define CLI_TO_SRV_EPOCH_0_RECS 3
  107. #define CLI_TO_SRV_EPOCH_1_RECS 1
  108. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  109. # define SRV_TO_CLI_EPOCH_0_RECS 10
  110. #else
  111. /*
  112. * In this case we have no ServerKeyExchange message, because we don't have
  113. * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
  114. * test.
  115. */
  116. # define SRV_TO_CLI_EPOCH_0_RECS 9
  117. #endif
  118. #define SRV_TO_CLI_EPOCH_1_RECS 1
  119. #define TOTAL_FULL_HAND_RECORDS \
  120. (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
  121. SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
  122. #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
  123. #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
  124. #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
  125. #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
  126. #define TOTAL_RESUME_HAND_RECORDS \
  127. (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
  128. SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
  129. #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
  130. /*
  131. * We are assuming a ServerKeyExchange message is sent in this test. If we don't
  132. * have either DH or EC, then it won't be
  133. */
  134. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  135. static int test_dtls_drop_records(int idx)
  136. {
  137. SSL_CTX *sctx = NULL, *cctx = NULL;
  138. SSL *serverssl = NULL, *clientssl = NULL;
  139. BIO *c_to_s_fbio, *mempackbio;
  140. int testresult = 0;
  141. int epoch = 0;
  142. SSL_SESSION *sess = NULL;
  143. int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
  144. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  145. DTLS_client_method(),
  146. DTLS1_VERSION, 0,
  147. &sctx, &cctx, cert, privkey)))
  148. return 0;
  149. #ifdef OPENSSL_NO_DTLS1_2
  150. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  151. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  152. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  153. "DEFAULT:@SECLEVEL=0")))
  154. goto end;
  155. #endif
  156. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  157. goto end;
  158. if (idx >= TOTAL_FULL_HAND_RECORDS) {
  159. /* We're going to do a resumption handshake. Get a session first. */
  160. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  161. NULL, NULL))
  162. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  163. SSL_ERROR_NONE))
  164. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  165. goto end;
  166. SSL_shutdown(clientssl);
  167. SSL_shutdown(serverssl);
  168. SSL_free(serverssl);
  169. SSL_free(clientssl);
  170. serverssl = clientssl = NULL;
  171. cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
  172. cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
  173. srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
  174. idx -= TOTAL_FULL_HAND_RECORDS;
  175. } else {
  176. cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
  177. cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
  178. srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
  179. }
  180. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  181. if (!TEST_ptr(c_to_s_fbio))
  182. goto end;
  183. /* BIO is freed by create_ssl_connection on error */
  184. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  185. NULL, c_to_s_fbio)))
  186. goto end;
  187. if (sess != NULL) {
  188. if (!TEST_true(SSL_set_session(clientssl, sess)))
  189. goto end;
  190. }
  191. DTLS_set_timer_cb(clientssl, timer_cb);
  192. DTLS_set_timer_cb(serverssl, timer_cb);
  193. /* Work out which record to drop based on the test number */
  194. if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
  195. mempackbio = SSL_get_wbio(serverssl);
  196. idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
  197. if (idx >= srv_to_cli_epoch0) {
  198. epoch = 1;
  199. idx -= srv_to_cli_epoch0;
  200. }
  201. } else {
  202. mempackbio = SSL_get_wbio(clientssl);
  203. if (idx >= cli_to_srv_epoch0) {
  204. epoch = 1;
  205. idx -= cli_to_srv_epoch0;
  206. }
  207. mempackbio = BIO_next(mempackbio);
  208. }
  209. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
  210. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
  211. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  212. goto end;
  213. if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
  214. goto end;
  215. /* If the test did what we planned then it should have dropped a record */
  216. if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
  217. NULL), -1))
  218. goto end;
  219. testresult = 1;
  220. end:
  221. SSL_SESSION_free(sess);
  222. SSL_free(serverssl);
  223. SSL_free(clientssl);
  224. SSL_CTX_free(sctx);
  225. SSL_CTX_free(cctx);
  226. return testresult;
  227. }
  228. #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
  229. static const char dummy_cookie[] = "0123456";
  230. static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
  231. unsigned int *cookie_len)
  232. {
  233. memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
  234. *cookie_len = sizeof(dummy_cookie);
  235. return 1;
  236. }
  237. static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
  238. unsigned int cookie_len)
  239. {
  240. return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
  241. }
  242. static int test_cookie(void)
  243. {
  244. SSL_CTX *sctx = NULL, *cctx = NULL;
  245. SSL *serverssl = NULL, *clientssl = NULL;
  246. int testresult = 0;
  247. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  248. DTLS_client_method(),
  249. DTLS1_VERSION, 0,
  250. &sctx, &cctx, cert, privkey)))
  251. return 0;
  252. SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
  253. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  254. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  255. #ifdef OPENSSL_NO_DTLS1_2
  256. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  257. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  258. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  259. "DEFAULT:@SECLEVEL=0")))
  260. goto end;
  261. #endif
  262. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  263. NULL, NULL))
  264. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  265. SSL_ERROR_NONE)))
  266. goto end;
  267. testresult = 1;
  268. end:
  269. SSL_free(serverssl);
  270. SSL_free(clientssl);
  271. SSL_CTX_free(sctx);
  272. SSL_CTX_free(cctx);
  273. return testresult;
  274. }
  275. static int test_dtls_duplicate_records(void)
  276. {
  277. SSL_CTX *sctx = NULL, *cctx = NULL;
  278. SSL *serverssl = NULL, *clientssl = NULL;
  279. int testresult = 0;
  280. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  281. DTLS_client_method(),
  282. DTLS1_VERSION, 0,
  283. &sctx, &cctx, cert, privkey)))
  284. return 0;
  285. #ifdef OPENSSL_NO_DTLS1_2
  286. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  287. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  288. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  289. "DEFAULT:@SECLEVEL=0")))
  290. goto end;
  291. #endif
  292. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  293. NULL, NULL)))
  294. goto end;
  295. DTLS_set_timer_cb(clientssl, timer_cb);
  296. DTLS_set_timer_cb(serverssl, timer_cb);
  297. BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  298. BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  299. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  300. goto end;
  301. testresult = 1;
  302. end:
  303. SSL_free(serverssl);
  304. SSL_free(clientssl);
  305. SSL_CTX_free(sctx);
  306. SSL_CTX_free(cctx);
  307. return testresult;
  308. }
  309. /*
  310. * Test just sending a Finished message as the first message. Should fail due
  311. * to an unexpected message.
  312. */
  313. static int test_just_finished(void)
  314. {
  315. int testresult = 0, ret;
  316. SSL_CTX *sctx = NULL;
  317. SSL *serverssl = NULL;
  318. BIO *rbio = NULL, *wbio = NULL, *sbio = NULL;
  319. unsigned char buf[] = {
  320. /* Record header */
  321. SSL3_RT_HANDSHAKE, /* content type */
  322. (DTLS1_2_VERSION >> 8) & 0xff, /* protocol version hi byte */
  323. DTLS1_2_VERSION & 0xff, /* protocol version lo byte */
  324. 0, 0, /* epoch */
  325. 0, 0, 0, 0, 0, 0, /* record sequence */
  326. 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, /* record length */
  327. /* Message header */
  328. SSL3_MT_FINISHED, /* message type */
  329. 0, 0, SHA_DIGEST_LENGTH, /* message length */
  330. 0, 0, /* message sequence */
  331. 0, 0, 0, /* fragment offset */
  332. 0, 0, SHA_DIGEST_LENGTH, /* fragment length */
  333. /* Message body */
  334. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  335. };
  336. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  337. NULL, 0, 0,
  338. &sctx, NULL, cert, privkey)))
  339. return 0;
  340. serverssl = SSL_new(sctx);
  341. rbio = BIO_new(BIO_s_mem());
  342. wbio = BIO_new(BIO_s_mem());
  343. if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio))
  344. goto end;
  345. sbio = rbio;
  346. SSL_set0_rbio(serverssl, rbio);
  347. SSL_set0_wbio(serverssl, wbio);
  348. rbio = wbio = NULL;
  349. DTLS_set_timer_cb(serverssl, timer_cb);
  350. if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf)))
  351. goto end;
  352. /* We expect the attempt to process the message to fail */
  353. if (!TEST_int_le(ret = SSL_accept(serverssl), 0))
  354. goto end;
  355. /* Check that we got the error we were expecting */
  356. if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL))
  357. goto end;
  358. if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE))
  359. goto end;
  360. testresult = 1;
  361. end:
  362. BIO_free(rbio);
  363. BIO_free(wbio);
  364. SSL_free(serverssl);
  365. SSL_CTX_free(sctx);
  366. return testresult;
  367. }
  368. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  369. int setup_tests(void)
  370. {
  371. if (!test_skip_common_options()) {
  372. TEST_error("Error parsing test options\n");
  373. return 0;
  374. }
  375. if (!TEST_ptr(cert = test_get_argument(0))
  376. || !TEST_ptr(privkey = test_get_argument(1)))
  377. return 0;
  378. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  379. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  380. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  381. #endif
  382. ADD_TEST(test_cookie);
  383. ADD_TEST(test_dtls_duplicate_records);
  384. ADD_TEST(test_just_finished);
  385. return 1;
  386. }
  387. void cleanup_tests(void)
  388. {
  389. bio_f_tls_dump_filter_free();
  390. bio_s_mempacket_test_free();
  391. }