dtlstest.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright 2016-2023 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 later records before Finished or CCS still works
  392. * Test 0: Test receiving a handshake record early from next epoch on server side
  393. * Test 1: Test receiving a handshake record early from next epoch on client side
  394. * Test 2: Test receiving an app data record early from next epoch on client side
  395. * Test 3: Test receiving an app data before Finished on client side
  396. */
  397. static int test_swap_records(int idx)
  398. {
  399. SSL_CTX *sctx = NULL, *cctx = NULL;
  400. SSL *sssl = NULL, *cssl = NULL;
  401. int testresult = 0;
  402. BIO *bio;
  403. char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  404. char buf[10];
  405. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  406. DTLS_client_method(),
  407. DTLS1_VERSION, 0,
  408. &sctx, &cctx, cert, privkey)))
  409. return 0;
  410. #ifndef OPENSSL_NO_DTLS1_2
  411. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  412. goto end;
  413. #else
  414. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  415. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
  416. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  417. "AES128-SHA:@SECLEVEL=0")))
  418. goto end;
  419. #endif
  420. if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl,
  421. NULL, NULL)))
  422. goto end;
  423. /* Send flight 1: ClientHello */
  424. if (!TEST_int_le(SSL_connect(cssl), 0))
  425. goto end;
  426. /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
  427. if (!TEST_int_le(SSL_accept(sssl), 0))
  428. goto end;
  429. /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
  430. if (!TEST_int_le(SSL_connect(cssl), 0))
  431. goto end;
  432. if (idx == 0) {
  433. /* Swap Finished and CCS within the datagram */
  434. bio = SSL_get_wbio(cssl);
  435. if (!TEST_ptr(bio)
  436. || !TEST_true(mempacket_swap_epoch(bio)))
  437. goto end;
  438. }
  439. /* Recv flight 3, send flight 4: datagram 0(NST, CCS) datagram 1(Finished) */
  440. if (!TEST_int_gt(SSL_accept(sssl), 0))
  441. goto end;
  442. /* Send flight 4 (cont'd): datagram 2(app data) */
  443. if (!TEST_int_eq(SSL_write(sssl, msg, sizeof(msg)), (int)sizeof(msg)))
  444. goto end;
  445. bio = SSL_get_wbio(sssl);
  446. if (!TEST_ptr(bio))
  447. goto end;
  448. if (idx == 1) {
  449. /* Finished comes before NST/CCS */
  450. if (!TEST_true(mempacket_move_packet(bio, 0, 1)))
  451. goto end;
  452. } else if (idx == 2) {
  453. /* App data comes before NST/CCS */
  454. if (!TEST_true(mempacket_move_packet(bio, 0, 2)))
  455. goto end;
  456. } else if (idx == 3) {
  457. /* App data comes before Finished */
  458. bio = SSL_get_wbio(sssl);
  459. if (!TEST_true(mempacket_move_packet(bio, 1, 2)))
  460. goto end;
  461. }
  462. /*
  463. * Recv flight 4 (datagram 1): NST, CCS, + flight 5: app data
  464. * + flight 4 (datagram 2): Finished
  465. */
  466. if (!TEST_int_gt(SSL_connect(cssl), 0))
  467. goto end;
  468. if (idx == 0 || idx == 1) {
  469. /* App data was not received early, so it should not be pending */
  470. if (!TEST_int_eq(SSL_pending(cssl), 0)
  471. || !TEST_false(SSL_has_pending(cssl)))
  472. goto end;
  473. } else {
  474. /* We received the app data early so it should be buffered already */
  475. if (!TEST_int_eq(SSL_pending(cssl), (int)sizeof(msg))
  476. || !TEST_true(SSL_has_pending(cssl)))
  477. goto end;
  478. }
  479. /*
  480. * Recv flight 5 (app data)
  481. */
  482. if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), (int)sizeof(msg)))
  483. goto end;
  484. testresult = 1;
  485. end:
  486. SSL_free(cssl);
  487. SSL_free(sssl);
  488. SSL_CTX_free(cctx);
  489. SSL_CTX_free(sctx);
  490. return testresult;
  491. }
  492. /* Confirm that we can create a connections using DTLSv1_listen() */
  493. static int test_listen(void)
  494. {
  495. SSL_CTX *sctx = NULL, *cctx = NULL;
  496. SSL *serverssl = NULL, *clientssl = NULL;
  497. int testresult = 0;
  498. if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
  499. DTLS_client_method(),
  500. DTLS1_VERSION, 0,
  501. &sctx, &cctx, cert, privkey)))
  502. return 0;
  503. #ifdef OPENSSL_NO_DTLS1_2
  504. /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
  505. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  506. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  507. "DEFAULT:@SECLEVEL=0")))
  508. goto end;
  509. #endif
  510. SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
  511. SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
  512. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  513. NULL, NULL)))
  514. goto end;
  515. DTLS_set_timer_cb(clientssl, timer_cb);
  516. DTLS_set_timer_cb(serverssl, timer_cb);
  517. /*
  518. * The last parameter to create_bare_ssl_connection() requests that
  519. * DTLSv1_listen() is used.
  520. */
  521. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  522. SSL_ERROR_NONE, 1, 1)))
  523. goto end;
  524. testresult = 1;
  525. end:
  526. SSL_free(serverssl);
  527. SSL_free(clientssl);
  528. SSL_CTX_free(sctx);
  529. SSL_CTX_free(cctx);
  530. return testresult;
  531. }
  532. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  533. int setup_tests(void)
  534. {
  535. if (!test_skip_common_options()) {
  536. TEST_error("Error parsing test options\n");
  537. return 0;
  538. }
  539. if (!TEST_ptr(cert = test_get_argument(0))
  540. || !TEST_ptr(privkey = test_get_argument(1)))
  541. return 0;
  542. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  543. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  544. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  545. #endif
  546. ADD_TEST(test_cookie);
  547. ADD_TEST(test_dtls_duplicate_records);
  548. ADD_TEST(test_just_finished);
  549. ADD_ALL_TESTS(test_swap_records, 4);
  550. ADD_TEST(test_listen);
  551. return 1;
  552. }
  553. void cleanup_tests(void)
  554. {
  555. bio_f_tls_dump_filter_free();
  556. bio_s_mempacket_test_free();
  557. }