2
0

asynciotest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #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, sessionid, extensions;
  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. if (!PACKET_forward(&wholebody,
  134. SSL3_HM_HEADER_LENGTH - 1)
  135. || !PACKET_get_net_2(&wholebody, &negversion)
  136. /* Skip random (32 bytes) */
  137. || !PACKET_forward(&wholebody, 32)
  138. /* Skip session id */
  139. || !PACKET_get_length_prefixed_1(&wholebody,
  140. &sessionid)
  141. /*
  142. * Skip ciphersuite (2 bytes) and compression
  143. * method (1 byte)
  144. */
  145. || !PACKET_forward(&wholebody, 2 + 1)
  146. || !PACKET_get_length_prefixed_2(&wholebody,
  147. &extensions))
  148. return -1;
  149. /*
  150. * Find the negotiated version in supported_versions
  151. * extension, if present.
  152. */
  153. while (PACKET_remaining(&extensions)) {
  154. unsigned int type;
  155. PACKET extbody;
  156. if (!PACKET_get_net_2(&extensions, &type)
  157. || !PACKET_get_length_prefixed_2(&extensions,
  158. &extbody))
  159. return -1;
  160. if (type == TLSEXT_TYPE_supported_versions
  161. && (!PACKET_get_net_2(&extbody, &negversion)
  162. || PACKET_remaining(&extbody) != 0))
  163. return -1;
  164. }
  165. }
  166. while (PACKET_get_1(&payload, &data)) {
  167. /* Create a new one byte long record for each byte in the
  168. * record in the input buffer
  169. */
  170. char smallrec[MIN_RECORD_LEN] = {
  171. 0, /* Content type */
  172. 0, /* Version hi */
  173. 0, /* Version lo */
  174. 0, /* Length hi */
  175. 1, /* Length lo */
  176. 0 /* Data */
  177. };
  178. smallrec[CONTENTTYPEPOS] = contenttype;
  179. smallrec[VERSIONHIPOS] = versionhi;
  180. smallrec[VERSIONLOPOS] = versionlo;
  181. smallrec[DATAPOS] = data;
  182. ret = BIO_write(next, smallrec, MIN_RECORD_LEN);
  183. if (ret <= 0)
  184. return -1;
  185. written++;
  186. }
  187. /*
  188. * We can't fragment anything after the ServerHello (or CCS <=
  189. * TLS1.2), otherwise we get a bad record MAC
  190. * TODO(TLS1.3): Change TLS1_3_VERSION_DRAFT to TLS1_3_VERSION
  191. * before release
  192. */
  193. if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC
  194. || (negversion == TLS1_3_VERSION_DRAFT
  195. && msgtype == SSL3_MT_SERVER_HELLO)) {
  196. fragment = 0;
  197. break;
  198. }
  199. }
  200. }
  201. /* Write any data we have left after fragmenting */
  202. ret = 0;
  203. if ((int)written < inl) {
  204. ret = BIO_write(next, in + written, inl - written);
  205. }
  206. if (ret <= 0 && BIO_should_write(next))
  207. BIO_set_retry_write(bio);
  208. else
  209. ret += written;
  210. } else {
  211. ctrs->wctr++;
  212. BIO_set_retry_write(bio);
  213. }
  214. return ret;
  215. }
  216. static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
  217. {
  218. long ret;
  219. BIO *next = BIO_next(bio);
  220. if (next == NULL)
  221. return 0;
  222. switch (cmd) {
  223. case BIO_CTRL_DUP:
  224. ret = 0L;
  225. break;
  226. default:
  227. ret = BIO_ctrl(next, cmd, num, ptr);
  228. break;
  229. }
  230. return ret;
  231. }
  232. static int async_gets(BIO *bio, char *buf, int size)
  233. {
  234. /* We don't support this - not needed anyway */
  235. return -1;
  236. }
  237. static int async_puts(BIO *bio, const char *str)
  238. {
  239. return async_write(bio, str, strlen(str));
  240. }
  241. #define MAX_ATTEMPTS 100
  242. static int test_asyncio(int test)
  243. {
  244. SSL_CTX *serverctx = NULL, *clientctx = NULL;
  245. SSL *serverssl = NULL, *clientssl = NULL;
  246. BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
  247. int testresult = 0, ret;
  248. size_t i, j;
  249. const char testdata[] = "Test data";
  250. char buf[sizeof(testdata)];
  251. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  252. TLS1_VERSION, TLS_MAX_VERSION,
  253. &serverctx, &clientctx, cert, privkey)))
  254. goto end;
  255. /*
  256. * We do 2 test runs. The first time around we just do a normal handshake
  257. * with lots of async io going on. The second time around we also break up
  258. * all records so that the content is only one byte length (up until the
  259. * CCS)
  260. */
  261. if (test == 1)
  262. fragment = 1;
  263. s_to_c_fbio = BIO_new(bio_f_async_filter());
  264. c_to_s_fbio = BIO_new(bio_f_async_filter());
  265. if (!TEST_ptr(s_to_c_fbio)
  266. || !TEST_ptr(c_to_s_fbio)) {
  267. BIO_free(s_to_c_fbio);
  268. BIO_free(c_to_s_fbio);
  269. goto end;
  270. }
  271. /* BIOs get freed on error */
  272. if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl,
  273. &clientssl, s_to_c_fbio, c_to_s_fbio))
  274. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  275. SSL_ERROR_NONE)))
  276. goto end;
  277. /*
  278. * Send and receive some test data. Do the whole thing twice to ensure
  279. * we hit at least one async event in both reading and writing
  280. */
  281. for (j = 0; j < 2; j++) {
  282. int len;
  283. /*
  284. * Write some test data. It should never take more than 2 attempts
  285. * (the first one might be a retryable fail).
  286. */
  287. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
  288. i++) {
  289. ret = SSL_write(clientssl, testdata + len,
  290. sizeof(testdata) - len);
  291. if (ret > 0) {
  292. len += ret;
  293. } else {
  294. int ssl_error = SSL_get_error(clientssl, ret);
  295. if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
  296. ssl_error == SSL_ERROR_SSL))
  297. goto end;
  298. }
  299. }
  300. if (!TEST_size_t_eq(len, sizeof(testdata)))
  301. goto end;
  302. /*
  303. * Now read the test data. It may take more attempts here because
  304. * it could fail once for each byte read, including all overhead
  305. * bytes from the record header/padding etc.
  306. */
  307. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
  308. i < MAX_ATTEMPTS; i++) {
  309. ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
  310. if (ret > 0) {
  311. len += ret;
  312. } else {
  313. int ssl_error = SSL_get_error(serverssl, ret);
  314. if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
  315. ssl_error == SSL_ERROR_SSL))
  316. goto end;
  317. }
  318. }
  319. if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len))
  320. goto end;
  321. }
  322. /* Also frees the BIOs */
  323. SSL_free(clientssl);
  324. SSL_free(serverssl);
  325. clientssl = serverssl = NULL;
  326. testresult = 1;
  327. end:
  328. SSL_free(clientssl);
  329. SSL_free(serverssl);
  330. SSL_CTX_free(clientctx);
  331. SSL_CTX_free(serverctx);
  332. return testresult;
  333. }
  334. int setup_tests(void)
  335. {
  336. if (!TEST_ptr(cert = test_get_argument(0))
  337. || !TEST_ptr(privkey = test_get_argument(1)))
  338. return 0;
  339. ADD_ALL_TESTS(test_asyncio, 2);
  340. return 1;
  341. }
  342. void cleanup_tests(void)
  343. {
  344. BIO_meth_free(methods_async);
  345. }