2
0

sslbuffertest.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <string.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/err.h>
  14. /* We include internal headers so we can check if the buffers are allocated */
  15. #include "../ssl/ssl_local.h"
  16. #include "../ssl/record/record_local.h"
  17. #include "internal/recordmethod.h"
  18. #include "../ssl/record/methods/recmethod_local.h"
  19. #include "internal/packet.h"
  20. #include "helpers/ssltestlib.h"
  21. #include "testutil.h"
  22. struct async_ctrs {
  23. unsigned int rctr;
  24. unsigned int wctr;
  25. };
  26. static SSL_CTX *serverctx = NULL;
  27. static SSL_CTX *clientctx = NULL;
  28. #define MAX_ATTEMPTS 100
  29. static int checkbuffers(SSL *s, int isalloced)
  30. {
  31. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  32. OSSL_RECORD_LAYER *rrl = sc->rlayer.rrl;
  33. OSSL_RECORD_LAYER *wrl = sc->rlayer.wrl;
  34. if (isalloced)
  35. return rrl->rbuf.buf != NULL && wrl->wbuf[0].buf != NULL;
  36. return rrl->rbuf.buf == NULL && wrl->wbuf[0].buf == NULL;
  37. }
  38. /*
  39. * There are 9 passes in the tests
  40. * 0 = control test
  41. * tests during writes
  42. * 1 = free buffers
  43. * 2 = + allocate buffers after free
  44. * 3 = + allocate buffers again
  45. * 4 = + free buffers after allocation
  46. * tests during reads
  47. * 5 = + free buffers
  48. * 6 = + free buffers again
  49. * 7 = + allocate buffers after free
  50. * 8 = + free buffers after allocation
  51. */
  52. static int test_func(int test)
  53. {
  54. int result = 0;
  55. SSL *serverssl = NULL, *clientssl = NULL;
  56. int ret;
  57. size_t i, j;
  58. const char testdata[] = "Test data";
  59. char buf[sizeof(testdata)];
  60. if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
  61. NULL, NULL))) {
  62. TEST_error("Test %d failed: Create SSL objects failed\n", test);
  63. goto end;
  64. }
  65. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
  66. TEST_error("Test %d failed: Create SSL connection failed\n", test);
  67. goto end;
  68. }
  69. /*
  70. * Send and receive some test data. Do the whole thing twice to ensure
  71. * we hit at least one async event in both reading and writing
  72. */
  73. for (j = 0; j < 2; j++) {
  74. int len;
  75. /*
  76. * Write some test data. It should never take more than 2 attempts
  77. * (the first one might be a retryable fail).
  78. */
  79. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
  80. i++) {
  81. /* test == 0 mean to free/allocate = control */
  82. if (test >= 1 && (!TEST_true(SSL_free_buffers(clientssl))
  83. || !TEST_true(checkbuffers(clientssl, 0))))
  84. goto end;
  85. if (test >= 2 && (!TEST_true(SSL_alloc_buffers(clientssl))
  86. || !TEST_true(checkbuffers(clientssl, 1))))
  87. goto end;
  88. /* allocate a second time */
  89. if (test >= 3 && (!TEST_true(SSL_alloc_buffers(clientssl))
  90. || !TEST_true(checkbuffers(clientssl, 1))))
  91. goto end;
  92. if (test >= 4 && (!TEST_true(SSL_free_buffers(clientssl))
  93. || !TEST_true(checkbuffers(clientssl, 0))))
  94. goto end;
  95. ret = SSL_write(clientssl, testdata + len,
  96. sizeof(testdata) - len);
  97. if (ret > 0) {
  98. len += ret;
  99. } else {
  100. int ssl_error = SSL_get_error(clientssl, ret);
  101. if (ssl_error == SSL_ERROR_SYSCALL ||
  102. ssl_error == SSL_ERROR_SSL) {
  103. TEST_error("Test %d failed: Failed to write app data\n", test);
  104. goto end;
  105. }
  106. }
  107. }
  108. if (!TEST_size_t_eq(len, sizeof(testdata)))
  109. goto end;
  110. /*
  111. * Now read the test data. It may take more attempts here because
  112. * it could fail once for each byte read, including all overhead
  113. * bytes from the record header/padding etc.
  114. */
  115. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
  116. i < MAX_ATTEMPTS; i++)
  117. {
  118. if (test >= 5 && (!TEST_true(SSL_free_buffers(serverssl))
  119. || !TEST_true(checkbuffers(serverssl, 0))))
  120. goto end;
  121. /* free a second time */
  122. if (test >= 6 && (!TEST_true(SSL_free_buffers(serverssl))
  123. || !TEST_true(checkbuffers(serverssl, 0))))
  124. goto end;
  125. if (test >= 7 && (!TEST_true(SSL_alloc_buffers(serverssl))
  126. || !TEST_true(checkbuffers(serverssl, 1))))
  127. goto end;
  128. if (test >= 8 && (!TEST_true(SSL_free_buffers(serverssl))
  129. || !TEST_true(checkbuffers(serverssl, 0))))
  130. goto end;
  131. ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
  132. if (ret > 0) {
  133. len += ret;
  134. } else {
  135. int ssl_error = SSL_get_error(serverssl, ret);
  136. if (ssl_error == SSL_ERROR_SYSCALL ||
  137. ssl_error == SSL_ERROR_SSL) {
  138. TEST_error("Test %d failed: Failed to read app data\n", test);
  139. goto end;
  140. }
  141. }
  142. }
  143. if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata)))
  144. goto end;
  145. }
  146. result = 1;
  147. end:
  148. if (!result)
  149. ERR_print_errors_fp(stderr);
  150. SSL_free(clientssl);
  151. SSL_free(serverssl);
  152. return result;
  153. }
  154. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  155. int setup_tests(void)
  156. {
  157. char *cert, *pkey;
  158. if (!test_skip_common_options()) {
  159. TEST_error("Error parsing test options\n");
  160. return 0;
  161. }
  162. if (!TEST_ptr(cert = test_get_argument(0))
  163. || !TEST_ptr(pkey = test_get_argument(1)))
  164. return 0;
  165. if (!create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(),
  166. TLS1_VERSION, 0,
  167. &serverctx, &clientctx, cert, pkey)) {
  168. TEST_error("Failed to create SSL_CTX pair\n");
  169. return 0;
  170. }
  171. ADD_ALL_TESTS(test_func, 9);
  172. return 1;
  173. }
  174. void cleanup_tests(void)
  175. {
  176. SSL_CTX_free(clientctx);
  177. SSL_CTX_free(serverctx);
  178. }