2
0

dtlstest.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2016 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. 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(), &sctx,
  42. &cctx, cert, privkey)) {
  43. printf("Unable to create SSL_CTX pair\n");
  44. return 0;
  45. }
  46. if (!SSL_CTX_set_ecdh_auto(sctx, 1)) {
  47. printf("Failed configuring auto ECDH\n");
  48. }
  49. if (!SSL_CTX_set_cipher_list(cctx, "AES128-SHA")) {
  50. printf("Failed setting cipher list\n");
  51. }
  52. c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
  53. if (c_to_s_fbio == NULL) {
  54. printf("Failed to create filter BIO\n");
  55. goto end;
  56. }
  57. /* BIO is freed by create_ssl_connection on error */
  58. if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
  59. c_to_s_fbio)) {
  60. printf("Unable to create SSL objects\n");
  61. ERR_print_errors_fp(stdout);
  62. goto end;
  63. }
  64. if (testidx == 1)
  65. certstatus[RECORD_SEQUENCE] = 0xff;
  66. /*
  67. * Inject a dummy record from the next epoch. In test 0, this should never
  68. * get used because the message sequence number is too big. In test 1 we set
  69. * the record sequence number to be way off in the future. This should not
  70. * have an impact on the record replay protection because the record should
  71. * be dropped before it is marked as arrivedg
  72. */
  73. c_to_s_mempacket = SSL_get_wbio(clientssl1);
  74. c_to_s_mempacket = BIO_next(c_to_s_mempacket);
  75. mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
  76. sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
  77. if (!create_ssl_connection(serverssl1, clientssl1)) {
  78. printf("Unable to create SSL connection\n");
  79. ERR_print_errors_fp(stdout);
  80. goto end;
  81. }
  82. testresult = 1;
  83. end:
  84. SSL_free(serverssl1);
  85. SSL_free(clientssl1);
  86. SSL_CTX_free(sctx);
  87. SSL_CTX_free(cctx);
  88. return testresult;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. BIO *err = NULL;
  93. int testresult = 0;
  94. if (argc != 3) {
  95. printf("Invalid argument count\n");
  96. return 1;
  97. }
  98. cert = argv[1];
  99. privkey = argv[2];
  100. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  101. SSL_library_init();
  102. SSL_load_error_strings();
  103. CRYPTO_malloc_debug_init();
  104. CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
  105. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  106. if (!test_dtls_unprocessed(0) || !test_dtls_unprocessed(1))
  107. testresult = 1;
  108. ERR_free_strings();
  109. ERR_remove_thread_state(NULL);
  110. EVP_cleanup();
  111. CRYPTO_cleanup_all_ex_data();
  112. CRYPTO_mem_leaks(err);
  113. BIO_free(err);
  114. if (!testresult)
  115. printf("PASS\n");
  116. return testresult;
  117. }