asynciotest.c 11 KB

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