dtlstest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 1000000;
  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. int setup_tests(void)
  94. {
  95. if (!TEST_ptr(cert = test_get_argument(0))
  96. || !TEST_ptr(privkey = test_get_argument(1)))
  97. return 0;
  98. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  99. return 1;
  100. }
  101. void cleanup_tests(void)
  102. {
  103. bio_f_tls_dump_filter_free();
  104. bio_s_mempacket_test_free();
  105. }