dtlstest.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 const char dummy_cookie[] = "0123456";
  37. static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
  38. unsigned int *cookie_len)
  39. {
  40. memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
  41. *cookie_len = sizeof(dummy_cookie);
  42. return 1;
  43. }
  44. static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
  45. unsigned int cookie_len)
  46. {
  47. return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
  48. }
  49. static unsigned int timer_cb(SSL *s, unsigned int timer_us)
  50. {
  51. ++timer_cb_count;
  52. if (timer_us == 0)
  53. return 50000;
  54. else
  55. return 2 * timer_us;
  56. }
  57. static int test_dtls_unprocessed(int testidx)
  58. {
  59. SSL_CTX *sctx = NULL, *cctx = NULL;
  60. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  61. BIO *c_to_s_fbio, *c_to_s_mempacket;
  62. int testresult = 0;
  63. timer_cb_count = 0;
  64. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  65. DTLS_client_method(),
  66. DTLS1_VERSION, 0,
  67. &sctx, &cctx, cert, privkey)))
  68. return 0;
  69. #ifndef OPENSSL_NO_DTLS1_2
  70. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  71. goto end;
  72. #else
  73. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  74. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
  75. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  76. "AES128-SHA:@SECLEVEL=0")))
  77. goto end;
  78. #endif
  79. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  80. if (!TEST_ptr(c_to_s_fbio))
  81. goto end;
  82. /* BIO is freed by create_ssl_connection on error */
  83. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  84. NULL, c_to_s_fbio)))
  85. goto end;
  86. DTLS_set_timer_cb(clientssl1, timer_cb);
  87. if (testidx == 1)
  88. certstatus[RECORD_SEQUENCE] = 0xff;
  89. /*
  90. * Inject a dummy record from the next epoch. In test 0, this should never
  91. * get used because the message sequence number is too big. In test 1 we set
  92. * the record sequence number to be way off in the future.
  93. */
  94. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  95. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  96. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  97. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  98. /*
  99. * Create the connection. We use "create_bare_ssl_connection" here so that
  100. * we can force the connection to not do "SSL_read" once partly connected.
  101. * We don't want to accidentally read the dummy records we injected because
  102. * they will fail to decrypt.
  103. */
  104. if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
  105. SSL_ERROR_NONE, 0, 0)))
  106. goto end;
  107. if (timer_cb_count == 0) {
  108. printf("timer_callback was not called.\n");
  109. goto end;
  110. }
  111. testresult = 1;
  112. end:
  113. SSL_free(serverssl1);
  114. SSL_free(clientssl1);
  115. SSL_CTX_free(sctx);
  116. SSL_CTX_free(cctx);
  117. return testresult;
  118. }
  119. /* One record for the cookieless initial ClientHello */
  120. #define CLI_TO_SRV_COOKIE_EXCH 1
  121. /*
  122. * In a resumption handshake we use 2 records for the initial ClientHello in
  123. * this test because we are using a very small MTU and the ClientHello is
  124. * bigger than in the non resumption case.
  125. */
  126. #define CLI_TO_SRV_RESUME_COOKIE_EXCH 2
  127. #define SRV_TO_CLI_COOKIE_EXCH 1
  128. #define CLI_TO_SRV_EPOCH_0_RECS 3
  129. #define CLI_TO_SRV_EPOCH_1_RECS 1
  130. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  131. # define SRV_TO_CLI_EPOCH_0_RECS 10
  132. #else
  133. /*
  134. * In this case we have no ServerKeyExchange message, because we don't have
  135. * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
  136. * test.
  137. */
  138. # define SRV_TO_CLI_EPOCH_0_RECS 9
  139. #endif
  140. #define SRV_TO_CLI_EPOCH_1_RECS 1
  141. #define TOTAL_FULL_HAND_RECORDS \
  142. (CLI_TO_SRV_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \
  143. CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
  144. SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
  145. #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
  146. #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
  147. #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
  148. #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
  149. #define TOTAL_RESUME_HAND_RECORDS \
  150. (CLI_TO_SRV_RESUME_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \
  151. CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
  152. SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
  153. #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
  154. /*
  155. * We are assuming a ServerKeyExchange message is sent in this test. If we don't
  156. * have either DH or EC, then it won't be
  157. */
  158. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  159. static int test_dtls_drop_records(int idx)
  160. {
  161. SSL_CTX *sctx = NULL, *cctx = NULL;
  162. SSL *serverssl = NULL, *clientssl = NULL;
  163. BIO *c_to_s_fbio, *mempackbio;
  164. int testresult = 0;
  165. int epoch = 0;
  166. SSL_SESSION *sess = NULL;
  167. int cli_to_srv_cookie, cli_to_srv_epoch0, cli_to_srv_epoch1;
  168. int srv_to_cli_epoch0;
  169. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  170. DTLS_client_method(),
  171. DTLS1_VERSION, 0,
  172. &sctx, &cctx, cert, privkey)))
  173. return 0;
  174. #ifdef OPENSSL_NO_DTLS1_2
  175. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  176. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  177. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  178. "DEFAULT:@SECLEVEL=0")))
  179. goto end;
  180. #endif
  181. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  182. goto end;
  183. SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
  184. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  185. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  186. if (idx >= TOTAL_FULL_HAND_RECORDS) {
  187. /* We're going to do a resumption handshake. Get a session first. */
  188. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  189. NULL, NULL))
  190. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  191. SSL_ERROR_NONE))
  192. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  193. goto end;
  194. SSL_shutdown(clientssl);
  195. SSL_shutdown(serverssl);
  196. SSL_free(serverssl);
  197. SSL_free(clientssl);
  198. serverssl = clientssl = NULL;
  199. cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
  200. cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
  201. srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
  202. cli_to_srv_cookie = CLI_TO_SRV_RESUME_COOKIE_EXCH;
  203. idx -= TOTAL_FULL_HAND_RECORDS;
  204. } else {
  205. cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
  206. cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
  207. srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
  208. cli_to_srv_cookie = CLI_TO_SRV_COOKIE_EXCH;
  209. }
  210. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  211. if (!TEST_ptr(c_to_s_fbio))
  212. goto end;
  213. /* BIO is freed by create_ssl_connection on error */
  214. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  215. NULL, c_to_s_fbio)))
  216. goto end;
  217. if (sess != NULL) {
  218. if (!TEST_true(SSL_set_session(clientssl, sess)))
  219. goto end;
  220. }
  221. DTLS_set_timer_cb(clientssl, timer_cb);
  222. DTLS_set_timer_cb(serverssl, timer_cb);
  223. /* Work out which record to drop based on the test number */
  224. if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1) {
  225. mempackbio = SSL_get_wbio(serverssl);
  226. idx -= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1;
  227. if (idx >= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0) {
  228. epoch = 1;
  229. idx -= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0;
  230. }
  231. } else {
  232. mempackbio = SSL_get_wbio(clientssl);
  233. if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0) {
  234. epoch = 1;
  235. idx -= cli_to_srv_cookie + cli_to_srv_epoch0;
  236. }
  237. mempackbio = BIO_next(mempackbio);
  238. }
  239. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
  240. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
  241. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  242. goto end;
  243. if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
  244. goto end;
  245. /* If the test did what we planned then it should have dropped a record */
  246. if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
  247. NULL), -1))
  248. goto end;
  249. testresult = 1;
  250. end:
  251. SSL_SESSION_free(sess);
  252. SSL_free(serverssl);
  253. SSL_free(clientssl);
  254. SSL_CTX_free(sctx);
  255. SSL_CTX_free(cctx);
  256. return testresult;
  257. }
  258. #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
  259. static int test_cookie(void)
  260. {
  261. SSL_CTX *sctx = NULL, *cctx = NULL;
  262. SSL *serverssl = NULL, *clientssl = NULL;
  263. int testresult = 0;
  264. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  265. DTLS_client_method(),
  266. DTLS1_VERSION, 0,
  267. &sctx, &cctx, cert, privkey)))
  268. return 0;
  269. SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
  270. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  271. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  272. #ifdef OPENSSL_NO_DTLS1_2
  273. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  274. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  275. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  276. "DEFAULT:@SECLEVEL=0")))
  277. goto end;
  278. #endif
  279. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  280. NULL, NULL))
  281. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  282. SSL_ERROR_NONE)))
  283. goto end;
  284. testresult = 1;
  285. end:
  286. SSL_free(serverssl);
  287. SSL_free(clientssl);
  288. SSL_CTX_free(sctx);
  289. SSL_CTX_free(cctx);
  290. return testresult;
  291. }
  292. static int test_dtls_duplicate_records(void)
  293. {
  294. SSL_CTX *sctx = NULL, *cctx = NULL;
  295. SSL *serverssl = NULL, *clientssl = NULL;
  296. int testresult = 0;
  297. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  298. DTLS_client_method(),
  299. DTLS1_VERSION, 0,
  300. &sctx, &cctx, cert, privkey)))
  301. return 0;
  302. #ifdef OPENSSL_NO_DTLS1_2
  303. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  304. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  305. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  306. "DEFAULT:@SECLEVEL=0")))
  307. goto end;
  308. #endif
  309. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  310. NULL, NULL)))
  311. goto end;
  312. DTLS_set_timer_cb(clientssl, timer_cb);
  313. DTLS_set_timer_cb(serverssl, timer_cb);
  314. BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  315. BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
  316. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  317. goto end;
  318. testresult = 1;
  319. end:
  320. SSL_free(serverssl);
  321. SSL_free(clientssl);
  322. SSL_CTX_free(sctx);
  323. SSL_CTX_free(cctx);
  324. return testresult;
  325. }
  326. /*
  327. * Test just sending a Finished message as the first message. Should fail due
  328. * to an unexpected message.
  329. */
  330. static int test_just_finished(void)
  331. {
  332. int testresult = 0, ret;
  333. SSL_CTX *sctx = NULL;
  334. SSL *serverssl = NULL;
  335. BIO *rbio = NULL, *wbio = NULL, *sbio = NULL;
  336. unsigned char buf[] = {
  337. /* Record header */
  338. SSL3_RT_HANDSHAKE, /* content type */
  339. (DTLS1_2_VERSION >> 8) & 0xff, /* protocol version hi byte */
  340. DTLS1_2_VERSION & 0xff, /* protocol version lo byte */
  341. 0, 0, /* epoch */
  342. 0, 0, 0, 0, 0, 0, /* record sequence */
  343. 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, /* record length */
  344. /* Message header */
  345. SSL3_MT_FINISHED, /* message type */
  346. 0, 0, SHA_DIGEST_LENGTH, /* message length */
  347. 0, 0, /* message sequence */
  348. 0, 0, 0, /* fragment offset */
  349. 0, 0, SHA_DIGEST_LENGTH, /* fragment length */
  350. /* Message body */
  351. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  352. };
  353. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  354. NULL, 0, 0,
  355. &sctx, NULL, cert, privkey)))
  356. return 0;
  357. #ifdef OPENSSL_NO_DTLS1_2
  358. /* DTLSv1 is not allowed at the default security level */
  359. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
  360. goto end;
  361. #endif
  362. serverssl = SSL_new(sctx);
  363. rbio = BIO_new(BIO_s_mem());
  364. wbio = BIO_new(BIO_s_mem());
  365. if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio))
  366. goto end;
  367. sbio = rbio;
  368. SSL_set0_rbio(serverssl, rbio);
  369. SSL_set0_wbio(serverssl, wbio);
  370. rbio = wbio = NULL;
  371. DTLS_set_timer_cb(serverssl, timer_cb);
  372. if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf)))
  373. goto end;
  374. /* We expect the attempt to process the message to fail */
  375. if (!TEST_int_le(ret = SSL_accept(serverssl), 0))
  376. goto end;
  377. /* Check that we got the error we were expecting */
  378. if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL))
  379. goto end;
  380. if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE))
  381. goto end;
  382. testresult = 1;
  383. end:
  384. BIO_free(rbio);
  385. BIO_free(wbio);
  386. SSL_free(serverssl);
  387. SSL_CTX_free(sctx);
  388. return testresult;
  389. }
  390. /*
  391. * Test that swapping a record from the next epoch into the current epoch still
  392. * works. Libssl should buffer the record until it needs it.
  393. */
  394. static int test_swap_epoch(void)
  395. {
  396. SSL_CTX *sctx = NULL, *cctx = NULL;
  397. SSL *sssl = NULL, *cssl = NULL;
  398. int testresult = 0;
  399. BIO *bio;
  400. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  401. DTLS_client_method(),
  402. DTLS1_VERSION, 0,
  403. &sctx, &cctx, cert, privkey)))
  404. return 0;
  405. #ifndef OPENSSL_NO_DTLS1_2
  406. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  407. goto end;
  408. #else
  409. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  410. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
  411. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  412. "AES128-SHA:@SECLEVEL=0")))
  413. goto end;
  414. #endif
  415. /* BIO is freed by create_ssl_connection on error */
  416. if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl,
  417. NULL, NULL)))
  418. goto end;
  419. /* Send flight 1: ClientHello */
  420. if (!TEST_int_le(SSL_connect(cssl), 0))
  421. goto end;
  422. /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
  423. if (!TEST_int_le(SSL_accept(sssl), 0))
  424. goto end;
  425. /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
  426. if (!TEST_int_le(SSL_connect(cssl), 0))
  427. goto end;
  428. bio = SSL_get_wbio(cssl);
  429. if (!TEST_ptr(bio)
  430. || !TEST_true(mempacket_swap_epoch(bio)))
  431. goto end;
  432. /*
  433. * Recv flight 3, send flight 4: CCS, Finished.
  434. * This should work in a single call because we have all the messages we
  435. * need. Even though we had out of order packets, the record for the next
  436. * epoch that we read early should be buffered and used later.
  437. */
  438. if (!TEST_int_gt(SSL_accept(sssl), 0))
  439. goto end;
  440. /* Recv flight 4 */
  441. if (!TEST_int_gt(SSL_connect(cssl), 0))
  442. goto end;
  443. testresult = 1;
  444. end:
  445. SSL_free(cssl);
  446. SSL_free(sssl);
  447. SSL_CTX_free(cctx);
  448. SSL_CTX_free(sctx);
  449. return testresult;
  450. }
  451. /*
  452. * Test that swapping an app data record so that it is received before the
  453. * Finished message still works.
  454. */
  455. static int test_swap_app_data(void)
  456. {
  457. SSL_CTX *sctx = NULL, *cctx = NULL;
  458. SSL *sssl = NULL, *cssl = NULL;
  459. int testresult = 0;
  460. BIO *bio;
  461. char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  462. char buf[10];
  463. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  464. DTLS_client_method(),
  465. DTLS1_VERSION, 0,
  466. &sctx, &cctx, cert, privkey)))
  467. return 0;
  468. #ifndef OPENSSL_NO_DTLS1_2
  469. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  470. goto end;
  471. #else
  472. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  473. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
  474. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  475. "AES128-SHA:@SECLEVEL=0")))
  476. goto end;
  477. #endif
  478. if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl,
  479. NULL, NULL)))
  480. goto end;
  481. /* Send flight 1: ClientHello */
  482. if (!TEST_int_le(SSL_connect(cssl), 0))
  483. goto end;
  484. /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
  485. if (!TEST_int_le(SSL_accept(sssl), 0))
  486. goto end;
  487. /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
  488. if (!TEST_int_le(SSL_connect(cssl), 0))
  489. goto end;
  490. /* Recv flight 3, send flight 4: datagram 1(NST, CCS) datagram 2(Finished) */
  491. if (!TEST_int_gt(SSL_accept(sssl), 0))
  492. goto end;
  493. /* Send flight 5: app data */
  494. if (!TEST_int_eq(SSL_write(sssl, msg, sizeof(msg)), (int)sizeof(msg)))
  495. goto end;
  496. bio = SSL_get_wbio(sssl);
  497. if (!TEST_ptr(bio)
  498. || !TEST_true(mempacket_swap_recent(bio)))
  499. goto end;
  500. /*
  501. * Recv flight 4 (datagram 1): NST, CCS, + flight 5: app data
  502. * + flight 4 (datagram 2): Finished
  503. */
  504. if (!TEST_int_gt(SSL_connect(cssl), 0))
  505. goto end;
  506. /* The app data should be buffered already */
  507. if (!TEST_int_eq(SSL_pending(cssl), (int)sizeof(msg))
  508. || !TEST_true(SSL_has_pending(cssl)))
  509. goto end;
  510. /*
  511. * Recv flight 5 (app data)
  512. * We already buffered this so it should be available.
  513. */
  514. if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), (int)sizeof(msg)))
  515. goto end;
  516. testresult = 1;
  517. end:
  518. SSL_free(cssl);
  519. SSL_free(sssl);
  520. SSL_CTX_free(cctx);
  521. SSL_CTX_free(sctx);
  522. return testresult;
  523. }
  524. /* Confirm that we can create a connections using DTLSv1_listen() */
  525. static int test_listen(void)
  526. {
  527. SSL_CTX *sctx = NULL, *cctx = NULL;
  528. SSL *serverssl = NULL, *clientssl = NULL;
  529. int testresult = 0;
  530. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  531. DTLS_client_method(),
  532. DTLS1_VERSION, 0,
  533. &sctx, &cctx, cert, privkey)))
  534. return 0;
  535. #ifdef OPENSSL_NO_DTLS1_2
  536. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  537. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  538. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  539. "DEFAULT:@SECLEVEL=0")))
  540. goto end;
  541. #endif
  542. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  543. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  544. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  545. NULL, NULL)))
  546. goto end;
  547. DTLS_set_timer_cb(clientssl, timer_cb);
  548. DTLS_set_timer_cb(serverssl, timer_cb);
  549. /*
  550. * The last parameter to create_bare_ssl_connection() requests that
  551. * DLTSv1_listen() is used.
  552. */
  553. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  554. SSL_ERROR_NONE, 1, 1)))
  555. goto end;
  556. testresult = 1;
  557. end:
  558. SSL_free(serverssl);
  559. SSL_free(clientssl);
  560. SSL_CTX_free(sctx);
  561. SSL_CTX_free(cctx);
  562. return testresult;
  563. }
  564. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  565. int setup_tests(void)
  566. {
  567. if (!test_skip_common_options()) {
  568. TEST_error("Error parsing test options\n");
  569. return 0;
  570. }
  571. if (!TEST_ptr(cert = test_get_argument(0))
  572. || !TEST_ptr(privkey = test_get_argument(1)))
  573. return 0;
  574. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  575. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  576. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  577. #endif
  578. ADD_TEST(test_cookie);
  579. ADD_TEST(test_dtls_duplicate_records);
  580. ADD_TEST(test_just_finished);
  581. ADD_TEST(test_swap_epoch);
  582. ADD_TEST(test_swap_app_data);
  583. ADD_TEST(test_listen);
  584. return 1;
  585. }
  586. void cleanup_tests(void)
  587. {
  588. bio_f_tls_dump_filter_free();
  589. bio_s_mempacket_test_free();
  590. }