server.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2024-2025 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. #include <openssl/err.h>
  10. #include <openssl/ssl.h>
  11. #include <openssl/quic.h>
  12. #ifdef _WIN32 /* Windows */
  13. # include <winsock2.h>
  14. #else /* Linux/Unix */
  15. # include <netinet/in.h>
  16. # include <unistd.h>
  17. #endif
  18. #include <assert.h>
  19. /*
  20. * This is a basic demo of QUIC server functionality in which one connection at
  21. * a time is accepted in a blocking loop.
  22. */
  23. /* ALPN string for TLS handshake */
  24. static const unsigned char alpn_ossltest[] = {
  25. /* "\x08ossltest" (hex for EBCDIC resilience) */
  26. 0x08, 0x6f, 0x73, 0x73, 0x6c, 0x74, 0x65, 0x73, 0x74
  27. };
  28. /* This callback validates and negotiates the desired ALPN on the server side. */
  29. static int select_alpn(SSL *ssl,
  30. const unsigned char **out, unsigned char *out_len,
  31. const unsigned char *in, unsigned int in_len,
  32. void *arg)
  33. {
  34. if (SSL_select_next_proto((unsigned char **)out, out_len,
  35. alpn_ossltest, sizeof(alpn_ossltest), in, in_len)
  36. != OPENSSL_NPN_NEGOTIATED)
  37. return SSL_TLSEXT_ERR_ALERT_FATAL;
  38. return SSL_TLSEXT_ERR_OK;
  39. }
  40. /* Create SSL_CTX. */
  41. static SSL_CTX *create_ctx(const char *cert_path, const char *key_path)
  42. {
  43. SSL_CTX *ctx;
  44. ctx = SSL_CTX_new(OSSL_QUIC_server_method());
  45. if (ctx == NULL)
  46. goto err;
  47. /* Load certificate and corresponding private key. */
  48. if (SSL_CTX_use_certificate_chain_file(ctx, cert_path) <= 0) {
  49. fprintf(stderr, "couldn't load certificate file: %s\n", cert_path);
  50. goto err;
  51. }
  52. if (SSL_CTX_use_PrivateKey_file(ctx, key_path, SSL_FILETYPE_PEM) <= 0) {
  53. fprintf(stderr, "couldn't load key file: %s\n", key_path);
  54. goto err;
  55. }
  56. if (!SSL_CTX_check_private_key(ctx)) {
  57. fprintf(stderr, "private key check failed\n");
  58. goto err;
  59. }
  60. /* Setup ALPN negotiation callback. */
  61. SSL_CTX_set_alpn_select_cb(ctx, select_alpn, NULL);
  62. return ctx;
  63. err:
  64. SSL_CTX_free(ctx);
  65. return NULL;
  66. }
  67. /* Create UDP socket using given port. */
  68. static int create_socket(uint16_t port)
  69. {
  70. int fd = -1;
  71. struct sockaddr_in sa = {0};
  72. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  73. fprintf(stderr, "cannot create socket");
  74. goto err;
  75. }
  76. sa.sin_family = AF_INET;
  77. sa.sin_port = htons(port);
  78. if (bind(fd, (const struct sockaddr *)&sa, sizeof(sa)) < 0) {
  79. fprintf(stderr, "cannot bind to %u\n", port);
  80. goto err;
  81. }
  82. return fd;
  83. err:
  84. if (fd >= 0)
  85. BIO_closesocket(fd);
  86. return -1;
  87. }
  88. /* Main loop for servicing a single incoming QUIC connection. */
  89. static int run_quic_conn(SSL *conn)
  90. {
  91. size_t written = 0;
  92. fprintf(stderr, "=> Received connection\n");
  93. /*
  94. * Write the message "hello" on the connection using a default stream
  95. * and then immediately conclude the stream (end-of-stream). This
  96. * demonstrates the use of SSL_write_ex2 for optimised FIN generation.
  97. *
  98. * Since we inherit our blocking mode from the parent QUIC SSL object (the
  99. * listener) by default, this call is also blocking.
  100. */
  101. if (!SSL_write_ex2(conn, "hello\n", 6, SSL_WRITE_FLAG_CONCLUDE, &written)
  102. || written != 6) {
  103. fprintf(stderr, "couldn't write on connection\n");
  104. ERR_print_errors_fp(stderr);
  105. return 0;
  106. }
  107. /* Shut down the connection (blocking). */
  108. if (SSL_shutdown(conn) != 1) {
  109. ERR_print_errors_fp(stderr);
  110. return 0;
  111. }
  112. fprintf(stderr, "=> Finished with connection\n");
  113. return 1;
  114. }
  115. /* Main loop for server to accept QUIC connections. */
  116. static int run_quic_server(SSL_CTX *ctx, int fd)
  117. {
  118. int ok = 0;
  119. SSL *listener = NULL, *conn = NULL;
  120. /* Create a new QUIC listener. */
  121. if ((listener = SSL_new_listener(ctx, 0)) == NULL)
  122. goto err;
  123. /* Provide the listener with our UDP socket. */
  124. if (!SSL_set_fd(listener, fd))
  125. goto err;
  126. /* Begin listening. */
  127. if (!SSL_listen(listener))
  128. goto err;
  129. /*
  130. * Listeners, and other QUIC objects, default to operating in blocking mode,
  131. * so the below call is not actually necessary. The configured behaviour is
  132. * inherited by child objects.
  133. */
  134. if (!SSL_set_blocking_mode(listener, 1))
  135. goto err;
  136. for (;;) {
  137. /* Blocking wait for an incoming connection, similar to accept(2). */
  138. conn = SSL_accept_connection(listener, 0);
  139. if (conn == NULL) {
  140. fprintf(stderr, "error while accepting connection\n");
  141. goto err;
  142. }
  143. /*
  144. * Optionally, we could disable blocking mode on the accepted connection
  145. * here by calling SSL_set_blocking_mode().
  146. */
  147. /*
  148. * Service the connection. In a real application this would be done
  149. * concurrently. In this demonstration program a single connection is
  150. * accepted and serviced at a time.
  151. */
  152. if (!run_quic_conn(conn)) {
  153. SSL_free(conn);
  154. goto err;
  155. }
  156. /* Free the connection, then loop again, accepting another connection. */
  157. SSL_free(conn);
  158. }
  159. ok = 1;
  160. err:
  161. if (!ok)
  162. ERR_print_errors_fp(stderr);
  163. SSL_free(listener);
  164. return ok;
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. int rc = 1;
  169. SSL_CTX *ctx = NULL;
  170. int fd = -1;
  171. unsigned long port;
  172. if (argc < 4) {
  173. fprintf(stderr, "usage: %s <port> <server.crt> <server.key>\n", argv[0]);
  174. goto err;
  175. }
  176. /* Create SSL_CTX. */
  177. if ((ctx = create_ctx(argv[2], argv[3])) == NULL)
  178. goto err;
  179. /* Parse port number from command line arguments. */
  180. port = strtoul(argv[1], NULL, 0);
  181. if (port == 0 || port > UINT16_MAX) {
  182. fprintf(stderr, "invalid port: %lu\n", port);
  183. goto err;
  184. }
  185. /* Create UDP socket. */
  186. if ((fd = create_socket((uint16_t)port)) < 0)
  187. goto err;
  188. /* Enter QUIC server connection acceptance loop. */
  189. if (!run_quic_server(ctx, fd))
  190. goto err;
  191. rc = 0;
  192. err:
  193. if (rc != 0)
  194. ERR_print_errors_fp(stderr);
  195. SSL_CTX_free(ctx);
  196. if (fd != -1)
  197. BIO_closesocket(fd);
  198. return rc;
  199. }