quicserver.c 9.5 KB

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