asynciotest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL licenses, (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. #include "../ssl/packet_locl.h"
  15. #include "ssltestlib.h"
  16. /* Should we fragment records or not? 0 = no, !0 = yes*/
  17. static int fragment = 0;
  18. static int async_new(BIO *bi);
  19. static int async_free(BIO *a);
  20. static int async_read(BIO *b, char *out, int outl);
  21. static int async_write(BIO *b, const char *in, int inl);
  22. static long async_ctrl(BIO *b, int cmd, long num, void *ptr);
  23. static int async_gets(BIO *bp, char *buf, int size);
  24. static int async_puts(BIO *bp, const char *str);
  25. /* Choose a sufficiently large type likely to be unused for this custom BIO */
  26. # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER)
  27. static BIO_METHOD *methods_async = NULL;
  28. struct async_ctrs {
  29. unsigned int rctr;
  30. unsigned int wctr;
  31. };
  32. static const BIO_METHOD *bio_f_async_filter()
  33. {
  34. if (methods_async == NULL) {
  35. methods_async = BIO_meth_new(BIO_TYPE_ASYNC_FILTER, "Async filter");
  36. if ( methods_async == NULL
  37. || !BIO_meth_set_write(methods_async, async_write)
  38. || !BIO_meth_set_read(methods_async, async_read)
  39. || !BIO_meth_set_puts(methods_async, async_puts)
  40. || !BIO_meth_set_gets(methods_async, async_gets)
  41. || !BIO_meth_set_ctrl(methods_async, async_ctrl)
  42. || !BIO_meth_set_create(methods_async, async_new)
  43. || !BIO_meth_set_destroy(methods_async, async_free))
  44. return NULL;
  45. }
  46. return methods_async;
  47. }
  48. static int async_new(BIO *bio)
  49. {
  50. struct async_ctrs *ctrs;
  51. ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs));
  52. if (ctrs == NULL)
  53. return 0;
  54. BIO_set_data(bio, ctrs);
  55. BIO_set_init(bio, 1);
  56. return 1;
  57. }
  58. static int async_free(BIO *bio)
  59. {
  60. struct async_ctrs *ctrs;
  61. if (bio == NULL)
  62. return 0;
  63. ctrs = BIO_get_data(bio);
  64. OPENSSL_free(ctrs);
  65. BIO_set_data(bio, NULL);
  66. BIO_set_init(bio, 0);
  67. return 1;
  68. }
  69. static int async_read(BIO *bio, char *out, int outl)
  70. {
  71. struct async_ctrs *ctrs;
  72. int ret = -1;
  73. BIO *next = BIO_next(bio);
  74. if (outl <= 0)
  75. return 0;
  76. if (next == NULL)
  77. return 0;
  78. ctrs = BIO_get_data(bio);
  79. BIO_clear_retry_flags(bio);
  80. if (ctrs->rctr > 0) {
  81. ret = BIO_read(next, out, 1);
  82. if (ret <= 0 && BIO_should_read(next))
  83. BIO_set_retry_read(bio);
  84. ctrs->rctr = 0;
  85. } else {
  86. ctrs->rctr++;
  87. BIO_set_retry_read(bio);
  88. }
  89. return ret;
  90. }
  91. #define MIN_RECORD_LEN 6
  92. #define CONTENTTYPEPOS 0
  93. #define VERSIONHIPOS 1
  94. #define VERSIONLOPOS 2
  95. #define DATAPOS 5
  96. static int async_write(BIO *bio, const char *in, int inl)
  97. {
  98. struct async_ctrs *ctrs;
  99. int ret = -1;
  100. size_t written = 0;
  101. BIO *next = BIO_next(bio);
  102. if (inl <= 0)
  103. return 0;
  104. if (next == NULL)
  105. return 0;
  106. ctrs = BIO_get_data(bio);
  107. BIO_clear_retry_flags(bio);
  108. if (ctrs->wctr > 0) {
  109. ctrs->wctr = 0;
  110. if (fragment) {
  111. PACKET pkt;
  112. if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl))
  113. abort();
  114. while (PACKET_remaining(&pkt) > 0) {
  115. PACKET payload;
  116. unsigned int contenttype, versionhi, versionlo, data;
  117. if ( !PACKET_get_1(&pkt, &contenttype)
  118. || !PACKET_get_1(&pkt, &versionhi)
  119. || !PACKET_get_1(&pkt, &versionlo)
  120. || !PACKET_get_length_prefixed_2(&pkt, &payload))
  121. abort();
  122. /* Pretend we wrote out the record header */
  123. written += SSL3_RT_HEADER_LENGTH;
  124. while (PACKET_get_1(&payload, &data)) {
  125. /* Create a new one byte long record for each byte in the
  126. * record in the input buffer
  127. */
  128. char smallrec[MIN_RECORD_LEN] = {
  129. 0, /* Content type */
  130. 0, /* Version hi */
  131. 0, /* Version lo */
  132. 0, /* Length hi */
  133. 1, /* Length lo */
  134. 0 /* Data */
  135. };
  136. smallrec[CONTENTTYPEPOS] = contenttype;
  137. smallrec[VERSIONHIPOS] = versionhi;
  138. smallrec[VERSIONLOPOS] = versionlo;
  139. smallrec[DATAPOS] = data;
  140. ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
  141. if (ret <= 0)
  142. abort();
  143. written++;
  144. }
  145. /*
  146. * We can't fragment anything after the CCS, otherwise we
  147. * get a bad record MAC
  148. */
  149. if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC) {
  150. fragment = 0;
  151. break;
  152. }
  153. }
  154. }
  155. /* Write any data we have left after fragmenting */
  156. ret = 0;
  157. if ((int)written < inl) {
  158. ret = BIO_write(next, in + written , inl - written);
  159. }
  160. if (ret <= 0 && BIO_should_write(next))
  161. BIO_set_retry_write(bio);
  162. else
  163. ret += written;
  164. } else {
  165. ctrs->wctr++;
  166. BIO_set_retry_write(bio);
  167. }
  168. return ret;
  169. }
  170. static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
  171. {
  172. long ret;
  173. BIO *next = BIO_next(bio);
  174. if (next == NULL)
  175. return 0;
  176. switch (cmd) {
  177. case BIO_CTRL_DUP:
  178. ret = 0L;
  179. break;
  180. default:
  181. ret = BIO_ctrl(next, cmd, num, ptr);
  182. break;
  183. }
  184. return ret;
  185. }
  186. static int async_gets(BIO *bio, char *buf, int size)
  187. {
  188. /* We don't support this - not needed anyway */
  189. return -1;
  190. }
  191. static int async_puts(BIO *bio, const char *str)
  192. {
  193. return async_write(bio, str, strlen(str));
  194. }
  195. #define MAX_ATTEMPTS 100
  196. int main(int argc, char *argv[])
  197. {
  198. SSL_CTX *serverctx = NULL, *clientctx = NULL;
  199. SSL *serverssl = NULL, *clientssl = NULL;
  200. BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
  201. int test, err = 1, ret;
  202. size_t i, j;
  203. const char testdata[] = "Test data";
  204. char buf[sizeof(testdata)];
  205. CRYPTO_set_mem_debug(1);
  206. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  207. if (argc != 3) {
  208. printf("Invalid argument count\n");
  209. goto end;
  210. }
  211. if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  212. TLS1_VERSION, TLS_MAX_VERSION,
  213. &serverctx, &clientctx, argv[1], argv[2])) {
  214. printf("Failed to create SSL_CTX pair\n");
  215. goto end;
  216. }
  217. /*
  218. * We do 2 test runs. The first time around we just do a normal handshake
  219. * with lots of async io going on. The second time around we also break up
  220. * all records so that the content is only one byte length (up until the
  221. * CCS)
  222. */
  223. for (test = 1; test < 3; test++) {
  224. if (test == 2)
  225. fragment = 1;
  226. s_to_c_fbio = BIO_new(bio_f_async_filter());
  227. c_to_s_fbio = BIO_new(bio_f_async_filter());
  228. if (s_to_c_fbio == NULL || c_to_s_fbio == NULL) {
  229. printf("Failed to create filter BIOs\n");
  230. BIO_free(s_to_c_fbio);
  231. BIO_free(c_to_s_fbio);
  232. goto end;
  233. }
  234. /* BIOs get freed on error */
  235. if (!create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
  236. s_to_c_fbio, c_to_s_fbio)) {
  237. printf("Test %d failed: Create SSL objects failed\n", test);
  238. goto end;
  239. }
  240. if (!create_ssl_connection(serverssl, clientssl)) {
  241. printf("Test %d failed: Create SSL connection failed\n", test);
  242. goto end;
  243. }
  244. /*
  245. * Send and receive some test data. Do the whole thing twice to ensure
  246. * we hit at least one async event in both reading and writing
  247. */
  248. for (j = 0; j < 2; j++) {
  249. int len;
  250. /*
  251. * Write some test data. It should never take more than 2 attempts
  252. * (the first one might be a retryable fail).
  253. */
  254. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
  255. i++) {
  256. ret = SSL_write(clientssl, testdata + len,
  257. sizeof(testdata) - len);
  258. if (ret > 0) {
  259. len += ret;
  260. } else {
  261. int ssl_error = SSL_get_error(clientssl, ret);
  262. if (ssl_error == SSL_ERROR_SYSCALL ||
  263. ssl_error == SSL_ERROR_SSL) {
  264. printf("Test %d failed: Failed to write app data\n", test);
  265. err = -1;
  266. goto end;
  267. }
  268. }
  269. }
  270. if (len != sizeof(testdata)) {
  271. err = -1;
  272. printf("Test %d failed: Failed to write all app data\n", test);
  273. goto end;
  274. }
  275. /*
  276. * Now read the test data. It may take more attempts here because
  277. * it could fail once for each byte read, including all overhead
  278. * bytes from the record header/padding etc.
  279. */
  280. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
  281. i < MAX_ATTEMPTS; i++)
  282. {
  283. ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
  284. if (ret > 0) {
  285. len += ret;
  286. } else {
  287. int ssl_error = SSL_get_error(serverssl, ret);
  288. if (ssl_error == SSL_ERROR_SYSCALL ||
  289. ssl_error == SSL_ERROR_SSL) {
  290. printf("Test %d failed: Failed to read app data\n", test);
  291. err = -1;
  292. goto end;
  293. }
  294. }
  295. }
  296. if (len != sizeof(testdata)
  297. || memcmp(buf, testdata, sizeof(testdata)) != 0) {
  298. err = -1;
  299. printf("Test %d failed: Unexpected app data received\n", test);
  300. goto end;
  301. }
  302. }
  303. /* Also frees the BIOs */
  304. SSL_free(clientssl);
  305. SSL_free(serverssl);
  306. clientssl = serverssl = NULL;
  307. }
  308. printf("Test success\n");
  309. err = 0;
  310. end:
  311. if (err)
  312. ERR_print_errors_fp(stderr);
  313. SSL_free(clientssl);
  314. SSL_free(serverssl);
  315. SSL_CTX_free(clientctx);
  316. SSL_CTX_free(serverctx);
  317. # ifndef OPENSSL_NO_CRYPTO_MDEBUG
  318. CRYPTO_mem_leaks_fp(stderr);
  319. # endif
  320. return err;
  321. }