quicserver.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * This is a temporary test server for QUIC. It will eventually be replaced
  11. * by s_server and removed once we have full QUIC server support.
  12. */
  13. #include <stdio.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/ssl.h>
  16. #include <openssl/err.h>
  17. #include "internal/e_os.h"
  18. #include "internal/sockets.h"
  19. #include "internal/quic_tserver.h"
  20. #include "internal/time.h"
  21. static BIO *bio_err = NULL;
  22. static void wait_for_activity(QUIC_TSERVER *qtserv)
  23. {
  24. fd_set readfds, writefds;
  25. fd_set *readfdsp = NULL, *writefdsp = NULL;
  26. struct timeval timeout, *timeoutp = NULL;
  27. int width;
  28. int sock;
  29. BIO *bio = ossl_quic_tserver_get0_rbio(qtserv);
  30. OSSL_TIME deadline;
  31. BIO_get_fd(bio, &sock);
  32. if (ossl_quic_tserver_get_net_read_desired(qtserv)) {
  33. readfdsp = &readfds;
  34. FD_ZERO(readfdsp);
  35. openssl_fdset(sock, readfdsp);
  36. }
  37. if (ossl_quic_tserver_get_net_write_desired(qtserv)) {
  38. writefdsp = &writefds;
  39. FD_ZERO(writefdsp);
  40. openssl_fdset(sock, writefdsp);
  41. }
  42. deadline = ossl_quic_tserver_get_deadline(qtserv);
  43. if (!ossl_time_is_infinite(deadline)) {
  44. timeout = ossl_time_to_timeval(ossl_time_subtract(deadline,
  45. ossl_time_now()));
  46. timeoutp = &timeout;
  47. }
  48. width = sock + 1;
  49. if (readfdsp == NULL && writefdsp == NULL && timeoutp == NULL)
  50. return;
  51. select(width, readfdsp, writefdsp, NULL, timeoutp);
  52. }
  53. /* Helper function to create a BIO connected to the server */
  54. static BIO *create_dgram_bio(int family, const char *hostname, const char *port)
  55. {
  56. int sock = -1;
  57. BIO_ADDRINFO *res;
  58. const BIO_ADDRINFO *ai = NULL;
  59. BIO *bio;
  60. if (BIO_sock_init() != 1)
  61. return NULL;
  62. /*
  63. * Lookup IP address info for the server.
  64. */
  65. if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_SERVER, family, SOCK_DGRAM,
  66. 0, &res))
  67. return NULL;
  68. /*
  69. * Loop through all the possible addresses for the server and find one
  70. * we can create and start listening on
  71. */
  72. for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
  73. /* Create the UDP socket */
  74. sock = BIO_socket(BIO_ADDRINFO_family(ai), SOCK_DGRAM, 0, 0);
  75. if (sock == -1)
  76. continue;
  77. /* Start listening on the socket */
  78. if (!BIO_listen(sock, BIO_ADDRINFO_address(ai), 0)) {
  79. BIO_closesocket(sock);
  80. continue;
  81. }
  82. /* Set to non-blocking mode */
  83. if (!BIO_socket_nbio(sock, 1)) {
  84. BIO_closesocket(sock);
  85. continue;
  86. }
  87. break; /* stop searching if we found an addr */
  88. }
  89. /* Free the address information resources we allocated earlier */
  90. BIO_ADDRINFO_free(res);
  91. /* If we didn't bind any sockets, fail */
  92. if (ai == NULL)
  93. return NULL;
  94. /* Create a BIO to wrap the socket */
  95. bio = BIO_new(BIO_s_datagram());
  96. if (bio == NULL) {
  97. BIO_closesocket(sock);
  98. return NULL;
  99. }
  100. /*
  101. * Associate the newly created BIO with the underlying socket. By
  102. * passing BIO_CLOSE here the socket will be automatically closed when
  103. * the BIO is freed. Alternatively you can use BIO_NOCLOSE, in which
  104. * case you must close the socket explicitly when it is no longer
  105. * needed.
  106. */
  107. BIO_set_fd(bio, sock, BIO_CLOSE);
  108. return bio;
  109. }
  110. static void usage(void)
  111. {
  112. BIO_printf(bio_err, "quicserver [-6][-trace] hostname port certfile keyfile\n");
  113. }
  114. int main(int argc, char *argv[])
  115. {
  116. QUIC_TSERVER_ARGS tserver_args = {0};
  117. QUIC_TSERVER *qtserv = NULL;
  118. int ipv6 = 0, trace = 0;
  119. int argnext = 1;
  120. BIO *bio = NULL;
  121. char *hostname, *port, *certfile, *keyfile;
  122. int ret = EXIT_FAILURE;
  123. unsigned char reqbuf[1024];
  124. size_t numbytes, reqbytes = 0;
  125. const char reqterm[] = {
  126. '\r', '\n', '\r', '\n'
  127. };
  128. const char *response[] = {
  129. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Hello world</body>\n</html>\n",
  130. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Hello again</body>\n</html>\n",
  131. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>Another response</body>\n</html>\n",
  132. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n<!DOCTYPE html>\n<html>\n<body>A message</body>\n</html>\n",
  133. };
  134. unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
  135. int first = 1;
  136. uint64_t streamid;
  137. size_t respnum = 0;
  138. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  139. if (argc == 0 || bio_err == NULL)
  140. goto end2;
  141. while (argnext < argc) {
  142. if (argv[argnext][0] != '-')
  143. break;
  144. if (strcmp(argv[argnext], "-6") == 0) {
  145. ipv6 = 1;
  146. } else if(strcmp(argv[argnext], "-trace") == 0) {
  147. trace = 1;
  148. } else {
  149. BIO_printf(bio_err, "Unrecognised argument %s\n", argv[argnext]);
  150. usage();
  151. goto end2;
  152. }
  153. argnext++;
  154. }
  155. if (argc - argnext != 4) {
  156. usage();
  157. goto end2;
  158. }
  159. hostname = argv[argnext++];
  160. port = argv[argnext++];
  161. certfile = argv[argnext++];
  162. keyfile = argv[argnext++];
  163. bio = create_dgram_bio(ipv6 ? AF_INET6 : AF_INET, hostname, port);
  164. if (bio == NULL || !BIO_up_ref(bio)) {
  165. BIO_printf(bio_err, "Unable to create server socket\n");
  166. goto end2;
  167. }
  168. tserver_args.libctx = NULL;
  169. tserver_args.net_rbio = bio;
  170. tserver_args.net_wbio = bio;
  171. tserver_args.alpn = alpn;
  172. tserver_args.alpnlen = sizeof(alpn);
  173. tserver_args.ctx = NULL;
  174. qtserv = ossl_quic_tserver_new(&tserver_args, certfile, keyfile);
  175. if (qtserv == NULL) {
  176. BIO_printf(bio_err, "Failed to create the QUIC_TSERVER\n");
  177. goto end;
  178. }
  179. BIO_printf(bio_err, "Starting quicserver\n");
  180. BIO_printf(bio_err,
  181. "Note that this utility will be removed in a future OpenSSL version.\n");
  182. BIO_printf(bio_err,
  183. "For test purposes only. Not for use in a production environment.\n");
  184. /* Ownership of the BIO is passed to qtserv */
  185. bio = NULL;
  186. if (trace)
  187. #ifndef OPENSSL_NO_SSL_TRACE
  188. ossl_quic_tserver_set_msg_callback(qtserv, SSL_trace, bio_err);
  189. #else
  190. BIO_printf(bio_err,
  191. "Warning: -trace specified but no SSL tracing support present\n");
  192. #endif
  193. /* Wait for handshake to complete */
  194. ossl_quic_tserver_tick(qtserv);
  195. while(!ossl_quic_tserver_is_handshake_confirmed(qtserv)) {
  196. wait_for_activity(qtserv);
  197. ossl_quic_tserver_tick(qtserv);
  198. if (ossl_quic_tserver_is_terminated(qtserv)) {
  199. BIO_printf(bio_err, "Failed waiting for handshake completion\n");
  200. ret = EXIT_FAILURE;
  201. goto end;
  202. }
  203. }
  204. for (;; respnum++) {
  205. if (respnum >= OSSL_NELEM(response))
  206. goto end;
  207. /* Wait for an incoming stream */
  208. do {
  209. streamid = ossl_quic_tserver_pop_incoming_stream(qtserv);
  210. if (streamid == UINT64_MAX)
  211. wait_for_activity(qtserv);
  212. ossl_quic_tserver_tick(qtserv);
  213. if (ossl_quic_tserver_is_terminated(qtserv)) {
  214. /* Assume we finished everything the clients wants from us */
  215. ret = EXIT_SUCCESS;
  216. goto end;
  217. }
  218. } while(streamid == UINT64_MAX);
  219. /* Read the request */
  220. do {
  221. if (first)
  222. first = 0;
  223. else
  224. wait_for_activity(qtserv);
  225. ossl_quic_tserver_tick(qtserv);
  226. if (ossl_quic_tserver_is_terminated(qtserv)) {
  227. BIO_printf(bio_err, "Failed reading request\n");
  228. ret = EXIT_FAILURE;
  229. goto end;
  230. }
  231. if (ossl_quic_tserver_read(qtserv, streamid, reqbuf + reqbytes,
  232. sizeof(reqbuf) - reqbytes,
  233. &numbytes)) {
  234. if (numbytes > 0)
  235. fwrite(reqbuf + reqbytes, 1, numbytes, stdout);
  236. reqbytes += numbytes;
  237. }
  238. } while (reqbytes < sizeof(reqterm)
  239. || memcmp(reqbuf + reqbytes - sizeof(reqterm), reqterm,
  240. sizeof(reqterm)) != 0);
  241. if ((streamid & QUIC_STREAM_DIR_UNI) != 0) {
  242. /*
  243. * Incoming stream was uni-directional. Create a server initiated
  244. * uni-directional stream for the response.
  245. */
  246. if (!ossl_quic_tserver_stream_new(qtserv, 1, &streamid)) {
  247. BIO_printf(bio_err, "Failed creating response stream\n");
  248. goto end;
  249. }
  250. }
  251. /* Send the response */
  252. ossl_quic_tserver_tick(qtserv);
  253. if (!ossl_quic_tserver_write(qtserv, streamid,
  254. (unsigned char *)response[respnum],
  255. strlen(response[respnum]), &numbytes))
  256. goto end;
  257. if (!ossl_quic_tserver_conclude(qtserv, streamid))
  258. goto end;
  259. }
  260. end:
  261. /* Free twice because we did an up-ref */
  262. BIO_free(bio);
  263. end2:
  264. BIO_free(bio);
  265. ossl_quic_tserver_free(qtserv);
  266. BIO_free(bio_err);
  267. return ret;
  268. }