2
0

asynciotest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright 2016-2018 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. #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(void)
  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. */
  191. if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC
  192. || (negversion == TLS1_3_VERSION
  193. && msgtype == SSL3_MT_SERVER_HELLO)) {
  194. fragment = 0;
  195. break;
  196. }
  197. }
  198. }
  199. /* Write any data we have left after fragmenting */
  200. ret = 0;
  201. if ((int)written < inl) {
  202. ret = BIO_write(next, in + written, inl - written);
  203. }
  204. if (ret <= 0 && BIO_should_write(next))
  205. BIO_set_retry_write(bio);
  206. else
  207. ret += written;
  208. } else {
  209. ctrs->wctr++;
  210. BIO_set_retry_write(bio);
  211. }
  212. return ret;
  213. }
  214. static long async_ctrl(BIO *bio, int cmd, long num, void *ptr)
  215. {
  216. long ret;
  217. BIO *next = BIO_next(bio);
  218. if (next == NULL)
  219. return 0;
  220. switch (cmd) {
  221. case BIO_CTRL_DUP:
  222. ret = 0L;
  223. break;
  224. default:
  225. ret = BIO_ctrl(next, cmd, num, ptr);
  226. break;
  227. }
  228. return ret;
  229. }
  230. static int async_gets(BIO *bio, char *buf, int size)
  231. {
  232. /* We don't support this - not needed anyway */
  233. return -1;
  234. }
  235. static int async_puts(BIO *bio, const char *str)
  236. {
  237. return async_write(bio, str, strlen(str));
  238. }
  239. #define MAX_ATTEMPTS 100
  240. static int test_asyncio(int test)
  241. {
  242. SSL_CTX *serverctx = NULL, *clientctx = NULL;
  243. SSL *serverssl = NULL, *clientssl = NULL;
  244. BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL;
  245. int testresult = 0, ret;
  246. size_t i, j;
  247. const char testdata[] = "Test data";
  248. char buf[sizeof(testdata)];
  249. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  250. TLS1_VERSION, 0,
  251. &serverctx, &clientctx, cert, privkey)))
  252. goto end;
  253. /*
  254. * We do 2 test runs. The first time around we just do a normal handshake
  255. * with lots of async io going on. The second time around we also break up
  256. * all records so that the content is only one byte length (up until the
  257. * CCS)
  258. */
  259. if (test == 1)
  260. fragment = 1;
  261. s_to_c_fbio = BIO_new(bio_f_async_filter());
  262. c_to_s_fbio = BIO_new(bio_f_async_filter());
  263. if (!TEST_ptr(s_to_c_fbio)
  264. || !TEST_ptr(c_to_s_fbio)) {
  265. BIO_free(s_to_c_fbio);
  266. BIO_free(c_to_s_fbio);
  267. goto end;
  268. }
  269. /* BIOs get freed on error */
  270. if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl,
  271. &clientssl, s_to_c_fbio, c_to_s_fbio))
  272. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  273. SSL_ERROR_NONE)))
  274. goto end;
  275. /*
  276. * Send and receive some test data. Do the whole thing twice to ensure
  277. * we hit at least one async event in both reading and writing
  278. */
  279. for (j = 0; j < 2; j++) {
  280. int len;
  281. /*
  282. * Write some test data. It should never take more than 2 attempts
  283. * (the first one might be a retryable fail).
  284. */
  285. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
  286. i++) {
  287. ret = SSL_write(clientssl, testdata + len,
  288. sizeof(testdata) - len);
  289. if (ret > 0) {
  290. len += ret;
  291. } else {
  292. int ssl_error = SSL_get_error(clientssl, ret);
  293. if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
  294. ssl_error == SSL_ERROR_SSL))
  295. goto end;
  296. }
  297. }
  298. if (!TEST_size_t_eq(len, sizeof(testdata)))
  299. goto end;
  300. /*
  301. * Now read the test data. It may take more attempts here because
  302. * it could fail once for each byte read, including all overhead
  303. * bytes from the record header/padding etc.
  304. */
  305. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
  306. i < MAX_ATTEMPTS; i++) {
  307. ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
  308. if (ret > 0) {
  309. len += ret;
  310. } else {
  311. int ssl_error = SSL_get_error(serverssl, ret);
  312. if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL ||
  313. ssl_error == SSL_ERROR_SSL))
  314. goto end;
  315. }
  316. }
  317. if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len))
  318. goto end;
  319. }
  320. /* Also frees the BIOs */
  321. SSL_free(clientssl);
  322. SSL_free(serverssl);
  323. clientssl = serverssl = NULL;
  324. testresult = 1;
  325. end:
  326. SSL_free(clientssl);
  327. SSL_free(serverssl);
  328. SSL_CTX_free(clientctx);
  329. SSL_CTX_free(serverctx);
  330. return testresult;
  331. }
  332. OPT_TEST_DECLARE_USAGE("certname privkey\n")
  333. int setup_tests(void)
  334. {
  335. if (!TEST_ptr(cert = test_get_argument(0))
  336. || !TEST_ptr(privkey = test_get_argument(1)))
  337. return 0;
  338. ADD_ALL_TESTS(test_asyncio, 2);
  339. return 1;
  340. }
  341. void cleanup_tests(void)
  342. {
  343. BIO_meth_free(methods_async);
  344. }