dtlstest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/bio.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/err.h>
  13. #include "ssltestlib.h"
  14. #include "testutil.h"
  15. static char *cert = NULL;
  16. static char *privkey = NULL;
  17. static unsigned int timer_cb_count;
  18. #define NUM_TESTS 2
  19. #define DUMMY_CERT_STATUS_LEN 12
  20. static unsigned char certstatus[] = {
  21. SSL3_RT_HANDSHAKE, /* Content type */
  22. 0xfe, 0xfd, /* Record version */
  23. 0, 1, /* Epoch */
  24. 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
  25. 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
  26. SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
  27. 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
  28. 0, 5, /* Message sequence */
  29. 0, 0, 0, /* Fragment offset */
  30. 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
  31. 0x80, 0x80, 0x80, 0x80, 0x80,
  32. 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
  33. };
  34. #define RECORD_SEQUENCE 10
  35. static unsigned int timer_cb(SSL *s, unsigned int timer_us)
  36. {
  37. ++timer_cb_count;
  38. if (timer_us == 0)
  39. return 50000;
  40. else
  41. return 2 * timer_us;
  42. }
  43. static int test_dtls_unprocessed(int testidx)
  44. {
  45. SSL_CTX *sctx = NULL, *cctx = NULL;
  46. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  47. BIO *c_to_s_fbio, *c_to_s_mempacket;
  48. int testresult = 0;
  49. timer_cb_count = 0;
  50. if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
  51. DTLS_client_method(),
  52. DTLS1_VERSION, DTLS_MAX_VERSION,
  53. &sctx, &cctx, cert, privkey)))
  54. return 0;
  55. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
  56. goto end;
  57. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  58. if (!TEST_ptr(c_to_s_fbio))
  59. goto end;
  60. /* BIO is freed by create_ssl_connection on error */
  61. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  62. NULL, c_to_s_fbio)))
  63. goto end;
  64. DTLS_set_timer_cb(clientssl1, timer_cb);
  65. if (testidx == 1)
  66. certstatus[RECORD_SEQUENCE] = 0xff;
  67. /*
  68. * Inject a dummy record from the next epoch. In test 0, this should never
  69. * get used because the message sequence number is too big. In test 1 we set
  70. * the record sequence number to be way off in the future. This should not
  71. * have an impact on the record replay protection because the record should
  72. * be dropped before it is marked as arrived
  73. */
  74. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  75. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  76. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  77. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  78. if (!TEST_true(create_ssl_connection(serverssl1, clientssl1,
  79. SSL_ERROR_NONE)))
  80. goto end;
  81. if (timer_cb_count == 0) {
  82. printf("timer_callback was not called.\n");
  83. goto end;
  84. }
  85. testresult = 1;
  86. end:
  87. SSL_free(serverssl1);
  88. SSL_free(clientssl1);
  89. SSL_CTX_free(sctx);
  90. SSL_CTX_free(cctx);
  91. return testresult;
  92. }
  93. #define CLI_TO_SRV_EPOCH_0_RECS 3
  94. #define CLI_TO_SRV_EPOCH_1_RECS 1
  95. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  96. # define SRV_TO_CLI_EPOCH_0_RECS 12
  97. #else
  98. /*
  99. * In this case we have no ServerKeyExchange message, because we don't have
  100. * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
  101. * test.
  102. */
  103. # define SRV_TO_CLI_EPOCH_0_RECS 9
  104. #endif
  105. #define SRV_TO_CLI_EPOCH_1_RECS 1
  106. #define TOTAL_FULL_HAND_RECORDS \
  107. (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
  108. SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
  109. #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
  110. #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
  111. #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
  112. #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
  113. #define TOTAL_RESUME_HAND_RECORDS \
  114. (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
  115. SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
  116. #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
  117. static int test_dtls_drop_records(int idx)
  118. {
  119. SSL_CTX *sctx = NULL, *cctx = NULL;
  120. SSL *serverssl = NULL, *clientssl = NULL;
  121. BIO *c_to_s_fbio, *mempackbio;
  122. int testresult = 0;
  123. int epoch = 0;
  124. SSL_SESSION *sess = NULL;
  125. int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
  126. if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
  127. DTLS_client_method(),
  128. DTLS1_VERSION, DTLS_MAX_VERSION,
  129. &sctx, &cctx, cert, privkey)))
  130. return 0;
  131. if (idx >= TOTAL_FULL_HAND_RECORDS) {
  132. /* We're going to do a resumption handshake. Get a session first. */
  133. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  134. NULL, NULL))
  135. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  136. SSL_ERROR_NONE))
  137. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  138. goto end;
  139. SSL_shutdown(clientssl);
  140. SSL_shutdown(serverssl);
  141. SSL_free(serverssl);
  142. SSL_free(clientssl);
  143. serverssl = clientssl = NULL;
  144. cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
  145. cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
  146. srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
  147. idx -= TOTAL_FULL_HAND_RECORDS;
  148. } else {
  149. cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
  150. cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
  151. srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
  152. }
  153. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  154. if (!TEST_ptr(c_to_s_fbio))
  155. goto end;
  156. /* BIO is freed by create_ssl_connection on error */
  157. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  158. NULL, c_to_s_fbio)))
  159. goto end;
  160. if (sess != NULL) {
  161. if (!TEST_true(SSL_set_session(clientssl, sess)))
  162. goto end;
  163. }
  164. DTLS_set_timer_cb(clientssl, timer_cb);
  165. DTLS_set_timer_cb(serverssl, timer_cb);
  166. /* Work out which record to drop based on the test number */
  167. if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
  168. mempackbio = SSL_get_wbio(serverssl);
  169. idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
  170. if (idx >= srv_to_cli_epoch0) {
  171. epoch = 1;
  172. idx -= srv_to_cli_epoch0;
  173. }
  174. } else {
  175. mempackbio = SSL_get_wbio(clientssl);
  176. if (idx >= cli_to_srv_epoch0) {
  177. epoch = 1;
  178. idx -= cli_to_srv_epoch0;
  179. }
  180. mempackbio = BIO_next(mempackbio);
  181. }
  182. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
  183. BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
  184. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  185. goto end;
  186. if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
  187. goto end;
  188. /* If the test did what we planned then it should have dropped a record */
  189. if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
  190. NULL), -1))
  191. goto end;
  192. testresult = 1;
  193. end:
  194. SSL_SESSION_free(sess);
  195. SSL_free(serverssl);
  196. SSL_free(clientssl);
  197. SSL_CTX_free(sctx);
  198. SSL_CTX_free(cctx);
  199. return testresult;
  200. }
  201. int setup_tests(void)
  202. {
  203. if (!TEST_ptr(cert = test_get_argument(0))
  204. || !TEST_ptr(privkey = test_get_argument(1)))
  205. return 0;
  206. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  207. ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
  208. return 1;
  209. }
  210. void cleanup_tests(void)
  211. {
  212. bio_f_tls_dump_filter_free();
  213. bio_s_mempacket_test_free();
  214. }