2
0

quic_newcid_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <string.h>
  10. #include <openssl/ssl.h>
  11. #include "helpers/quictestlib.h"
  12. #include "internal/quic_error.h"
  13. #include "testutil.h"
  14. static char *cert = NULL;
  15. static char *privkey = NULL;
  16. /*
  17. * Inject NEW_CONNECTION_ID frame
  18. */
  19. static size_t ncid_injected;
  20. static int add_ncid_frame_cb(QTEST_FAULT *fault, QUIC_PKT_HDR *hdr,
  21. unsigned char *buf, size_t len, void *cbarg)
  22. {
  23. /*
  24. * We inject NEW_CONNECTION_ID frame to trigger change of the DCID.
  25. * The connection id length must be 8, otherwise the tserver won't be
  26. * able to receive packets with this new id.
  27. */
  28. static unsigned char new_conn_id_frame[] = {
  29. 0x18, /* Type */
  30. 0x01, /* Sequence Number */
  31. 0x01, /* Retire Prior To */
  32. 0x08, /* Connection ID Length */
  33. 0x33, 0x44, 0x55, 0x66, 0xde, 0xad, 0xbe, 0xef, /* Connection ID */
  34. 0xab, 0xcd, 0xef, 0x01, 0x12, 0x32, 0x23, 0x45, /* Stateless Reset Token */
  35. 0x56, 0x06, 0x08, 0x89, 0xa1, 0xb2, 0xc3, 0xd4
  36. };
  37. /* We only ever add the unknown frame to one packet */
  38. if (ncid_injected++)
  39. return 1;
  40. return qtest_fault_prepend_frame(fault, new_conn_id_frame,
  41. sizeof(new_conn_id_frame));
  42. }
  43. static int test_ncid_frame(int fail)
  44. {
  45. int testresult = 0;
  46. SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method());
  47. QUIC_TSERVER *qtserv = NULL;
  48. SSL *cssl = NULL;
  49. char *msg = "Hello World!";
  50. size_t msglen = strlen(msg);
  51. unsigned char buf[80];
  52. size_t byteswritten;
  53. size_t bytesread;
  54. QTEST_FAULT *fault = NULL;
  55. static const QUIC_CONN_ID conn_id = {
  56. 0x08,
  57. {0x33, 0x44, 0x55, 0x66, 0xde, 0xad, 0xbe, 0xef}
  58. };
  59. ncid_injected = 0;
  60. if (!TEST_ptr(cctx))
  61. goto err;
  62. if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, 0,
  63. &qtserv, &cssl, &fault, NULL)))
  64. goto err;
  65. if (!TEST_true(qtest_create_quic_connection(qtserv, cssl)))
  66. goto err;
  67. if (!TEST_int_eq(SSL_write(cssl, msg, msglen), msglen))
  68. goto err;
  69. ossl_quic_tserver_tick(qtserv);
  70. if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
  71. &bytesread)))
  72. goto err;
  73. /*
  74. * We assume the entire message is read from the server in one go. In
  75. * theory this could get fragmented but its a small message so we assume
  76. * not.
  77. */
  78. if (!TEST_mem_eq(msg, msglen, buf, bytesread))
  79. goto err;
  80. /*
  81. * Write a message from the server to the client and add
  82. * a NEW_CONNECTION_ID frame.
  83. */
  84. if (!TEST_true(qtest_fault_set_packet_plain_listener(fault,
  85. add_ncid_frame_cb,
  86. NULL)))
  87. goto err;
  88. if (!fail && !TEST_true(ossl_quic_tserver_set_new_local_cid(qtserv, &conn_id)))
  89. goto err;
  90. if (!TEST_true(ossl_quic_tserver_write(qtserv, 0,
  91. (unsigned char *)msg, msglen,
  92. &byteswritten)))
  93. goto err;
  94. if (!TEST_true(ncid_injected))
  95. goto err;
  96. if (!TEST_size_t_eq(msglen, byteswritten))
  97. goto err;
  98. ossl_quic_tserver_tick(qtserv);
  99. if (!TEST_true(SSL_handle_events(cssl)))
  100. goto err;
  101. if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), msglen))
  102. goto err;
  103. if (!TEST_mem_eq(msg, msglen, buf, bytesread))
  104. goto err;
  105. if (!TEST_int_eq(SSL_write(cssl, msg, msglen), msglen))
  106. goto err;
  107. ossl_quic_tserver_tick(qtserv);
  108. if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
  109. &bytesread)))
  110. goto err;
  111. if (fail) {
  112. if (!TEST_size_t_eq(bytesread, 0))
  113. goto err;
  114. } else {
  115. if (!TEST_mem_eq(msg, msglen, buf, bytesread))
  116. goto err;
  117. }
  118. testresult = 1;
  119. err:
  120. qtest_fault_free(fault);
  121. SSL_free(cssl);
  122. ossl_quic_tserver_free(qtserv);
  123. SSL_CTX_free(cctx);
  124. return testresult;
  125. }
  126. OPT_TEST_DECLARE_USAGE("certsdir\n")
  127. int setup_tests(void)
  128. {
  129. char *certsdir = NULL;
  130. if (!test_skip_common_options()) {
  131. TEST_error("Error parsing test options\n");
  132. return 0;
  133. }
  134. if (!TEST_ptr(certsdir = test_get_argument(0)))
  135. return 0;
  136. cert = test_mk_file_path(certsdir, "servercert.pem");
  137. if (cert == NULL)
  138. goto err;
  139. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  140. if (privkey == NULL)
  141. goto err;
  142. ADD_ALL_TESTS(test_ncid_frame, 2);
  143. return 1;
  144. err:
  145. OPENSSL_free(cert);
  146. OPENSSL_free(privkey);
  147. return 0;
  148. }
  149. void cleanup_tests(void)
  150. {
  151. OPENSSL_free(cert);
  152. OPENSSL_free(privkey);
  153. }