2
0

ssl_handshake_rtt_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 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. /*
  10. * We need access to the deprecated low level HMAC APIs for legacy purposes
  11. * when the deprecated calls are not hidden
  12. */
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. # define OPENSSL_SUPPRESS_DEPRECATED
  15. #endif
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <openssl/opensslconf.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/ssl.h>
  22. #include <openssl/engine.h>
  23. #include "helpers/ssltestlib.h"
  24. #include "testutil.h"
  25. #include "testutil/output.h"
  26. #include "internal/ktls.h"
  27. #include "../ssl/ssl_local.h"
  28. #include "../ssl/statem/statem_local.h"
  29. static OSSL_LIB_CTX *libctx = NULL;
  30. static char *cert = NULL;
  31. static char *privkey = NULL;
  32. /*
  33. * Test 0: Clientside handshake RTT (TLSv1.2)
  34. * Test 1: Serverside handshake RTT (TLSv1.2)
  35. * Test 2: Clientside handshake RTT (TLSv1.3)
  36. * Test 3: Serverside handshake RTT (TLSv1.3)
  37. * Test 4: Clientside handshake RTT with Early Data (TLSv1.3)
  38. */
  39. static int test_handshake_rtt(int tst)
  40. {
  41. SSL_CTX *cctx = NULL, *sctx = NULL;
  42. SSL *clientssl = NULL, *serverssl = NULL;
  43. int testresult = 0;
  44. SSL_CONNECTION *s = NULL;
  45. OSSL_STATEM *st = NULL;
  46. uint64_t rtt;
  47. #ifdef OPENSSL_NO_TLS1_2
  48. if (tst <= 1)
  49. return 1;
  50. #endif
  51. #ifdef OSSL_NO_USABLE_TLS1_3
  52. if (tst >= 2)
  53. return 1;
  54. #endif
  55. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  56. TLS_client_method(),
  57. TLS1_VERSION,
  58. (tst <= 1) ? TLS1_2_VERSION
  59. : TLS1_3_VERSION,
  60. &sctx, &cctx, cert, privkey))
  61. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  62. NULL, NULL)))
  63. goto end;
  64. s = SSL_CONNECTION_FROM_SSL(tst % 2 == 0 ? clientssl : serverssl);
  65. if (!TEST_ptr(s) || !TEST_ptr(st = &s->statem))
  66. return 0;
  67. /* implicitly set handshake rtt with a delay */
  68. switch (tst) {
  69. case 0:
  70. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  71. ossl_statem_client_write_transition(s);
  72. OSSL_sleep(1);
  73. st->hand_state = TLS_ST_CR_SRVR_DONE;
  74. ossl_statem_client_write_transition(s);
  75. break;
  76. case 1:
  77. st->hand_state = TLS_ST_SW_SRVR_DONE;
  78. ossl_statem_server_write_transition(s);
  79. OSSL_sleep(1);
  80. st->hand_state = TLS_ST_SR_FINISHED;
  81. ossl_statem_server_write_transition(s);
  82. break;
  83. case 2:
  84. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  85. ossl_statem_client_write_transition(s);
  86. OSSL_sleep(1);
  87. st->hand_state = TLS_ST_CR_SRVR_DONE;
  88. ossl_statem_client_write_transition(s);
  89. break;
  90. case 3:
  91. st->hand_state = TLS_ST_SW_SRVR_DONE;
  92. ossl_statem_server_write_transition(s);
  93. OSSL_sleep(1);
  94. st->hand_state = TLS_ST_SR_FINISHED;
  95. ossl_statem_server_write_transition(s);
  96. break;
  97. case 4:
  98. st->hand_state = TLS_ST_EARLY_DATA;
  99. ossl_statem_client_write_transition(s);
  100. OSSL_sleep(1);
  101. st->hand_state = TLS_ST_CR_SRVR_DONE;
  102. ossl_statem_client_write_transition(s);
  103. break;
  104. }
  105. if (!TEST_int_gt(SSL_get_handshake_rtt(SSL_CONNECTION_GET_SSL(s), &rtt), 0))
  106. goto end;
  107. /* 1 millisec is the absolute minimum it could be given the delay */
  108. if (!TEST_uint64_t_ge(rtt, 1000))
  109. goto end;
  110. testresult = 1;
  111. end:
  112. SSL_free(serverssl);
  113. SSL_free(clientssl);
  114. SSL_CTX_free(sctx);
  115. SSL_CTX_free(cctx);
  116. return testresult;
  117. }
  118. int setup_tests(void)
  119. {
  120. ADD_ALL_TESTS(test_handshake_rtt, 5);
  121. return 1;
  122. }