dtlstest.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #define NUM_TESTS 2
  18. #define DUMMY_CERT_STATUS_LEN 12
  19. static unsigned char certstatus[] = {
  20. SSL3_RT_HANDSHAKE, /* Content type */
  21. 0xfe, 0xfd, /* Record version */
  22. 0, 1, /* Epoch */
  23. 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
  24. 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
  25. SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
  26. 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
  27. 0, 5, /* Message sequence */
  28. 0, 0, 0, /* Fragment offset */
  29. 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
  30. 0x80, 0x80, 0x80, 0x80, 0x80,
  31. 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
  32. };
  33. #define RECORD_SEQUENCE 10
  34. static int test_dtls_unprocessed(int testidx)
  35. {
  36. SSL_CTX *sctx = NULL, *cctx = NULL;
  37. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  38. BIO *c_to_s_fbio, *c_to_s_mempacket;
  39. int testresult = 0;
  40. printf("Starting Test %d\n", testidx);
  41. if (!create_ssl_ctx_pair(DTLS_server_method(), DTLS_client_method(),
  42. DTLS1_VERSION, DTLS_MAX_VERSION, &sctx, &cctx,
  43. cert, privkey)) {
  44. printf("Unable to create SSL_CTX pair\n");
  45. return 0;
  46. }
  47. if (!SSL_CTX_set_cipher_list(cctx, "AES128-SHA")) {
  48. printf("Failed setting cipher list\n");
  49. }
  50. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  51. if (c_to_s_fbio == NULL) {
  52. printf("Failed to create filter BIO\n");
  53. goto end;
  54. }
  55. /* BIO is freed by create_ssl_connection on error */
  56. if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
  57. c_to_s_fbio)) {
  58. printf("Unable to create SSL objects\n");
  59. ERR_print_errors_fp(stdout);
  60. goto end;
  61. }
  62. if (testidx == 1)
  63. certstatus[RECORD_SEQUENCE] = 0xff;
  64. /*
  65. * Inject a dummy record from the next epoch. In test 0, this should never
  66. * get used because the message sequence number is too big. In test 1 we set
  67. * the record sequence number to be way off in the future. This should not
  68. * have an impact on the record replay protection because the record should
  69. * be dropped before it is marked as arrived
  70. */
  71. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  72. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  73. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  74. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  75. if (!create_ssl_connection(serverssl1, clientssl1)) {
  76. printf("Unable to create SSL connection\n");
  77. ERR_print_errors_fp(stdout);
  78. goto end;
  79. }
  80. testresult = 1;
  81. end:
  82. SSL_free(serverssl1);
  83. SSL_free(clientssl1);
  84. SSL_CTX_free(sctx);
  85. SSL_CTX_free(cctx);
  86. return testresult;
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. BIO *err = NULL;
  91. int testresult = 1;
  92. if (argc != 3) {
  93. printf("Invalid argument count\n");
  94. return 1;
  95. }
  96. cert = argv[1];
  97. privkey = argv[2];
  98. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  99. CRYPTO_set_mem_debug(1);
  100. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  101. ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
  102. testresult = run_tests(argv[0]);
  103. bio_f_tls_dump_filter_free();
  104. bio_s_mempacket_test_free();
  105. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  106. if (CRYPTO_mem_leaks(err) <= 0)
  107. testresult = 1;
  108. #endif
  109. BIO_free(err);
  110. if (!testresult)
  111. printf("PASS\n");
  112. return testresult;
  113. }