quic_tserver.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright 2022-2024 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 "internal/quic_tserver.h"
  10. #include "internal/quic_channel.h"
  11. #include "internal/quic_statm.h"
  12. #include "internal/quic_port.h"
  13. #include "internal/quic_engine.h"
  14. #include "internal/common.h"
  15. #include "internal/time.h"
  16. #include "quic_local.h"
  17. /*
  18. * QUIC Test Server Module
  19. * =======================
  20. */
  21. struct quic_tserver_st {
  22. QUIC_TSERVER_ARGS args;
  23. /* Dummy SSL object for this QUIC connection for use by msg_callback */
  24. SSL *ssl;
  25. /*
  26. * The QUIC engine, port and channel providing the core QUIC connection
  27. * implementation.
  28. */
  29. QUIC_ENGINE *engine;
  30. QUIC_PORT *port;
  31. QUIC_CHANNEL *ch;
  32. /* The mutex we give to the QUIC channel. */
  33. CRYPTO_MUTEX *mutex;
  34. /* SSL_CTX for creating the underlying TLS connection */
  35. SSL_CTX *ctx;
  36. /* SSL for the underlying TLS connection */
  37. SSL *tls;
  38. /* The current peer L4 address. AF_UNSPEC if we do not have a peer yet. */
  39. BIO_ADDR cur_peer_addr;
  40. /* Are we connected to a peer? */
  41. unsigned int connected : 1;
  42. };
  43. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  44. unsigned char *outlen, const unsigned char *in,
  45. unsigned int inlen, void *arg)
  46. {
  47. QUIC_TSERVER *srv = arg;
  48. static const unsigned char alpndeflt[] = {
  49. 8, 'o', 's', 's', 'l', 't', 'e', 's', 't'
  50. };
  51. const unsigned char *alpn;
  52. size_t alpnlen;
  53. if (srv->args.alpn == NULL) {
  54. alpn = alpndeflt;
  55. alpnlen = sizeof(alpn);
  56. } else {
  57. alpn = srv->args.alpn;
  58. alpnlen = srv->args.alpnlen;
  59. }
  60. if (SSL_select_next_proto((unsigned char **)out, outlen, alpn, alpnlen,
  61. in, inlen) != OPENSSL_NPN_NEGOTIATED)
  62. return SSL_TLSEXT_ERR_ALERT_FATAL;
  63. return SSL_TLSEXT_ERR_OK;
  64. }
  65. QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
  66. const char *certfile, const char *keyfile)
  67. {
  68. QUIC_TSERVER *srv = NULL;
  69. QUIC_ENGINE_ARGS engine_args = {0};
  70. QUIC_PORT_ARGS port_args = {0};
  71. QUIC_CONNECTION *qc = NULL;
  72. if (args->net_rbio == NULL || args->net_wbio == NULL)
  73. goto err;
  74. if ((srv = OPENSSL_zalloc(sizeof(*srv))) == NULL)
  75. goto err;
  76. srv->args = *args;
  77. #if defined(OPENSSL_THREADS)
  78. if ((srv->mutex = ossl_crypto_mutex_new()) == NULL)
  79. goto err;
  80. #endif
  81. if (args->ctx != NULL)
  82. srv->ctx = args->ctx;
  83. else
  84. srv->ctx = SSL_CTX_new_ex(srv->args.libctx, srv->args.propq,
  85. TLS_method());
  86. if (srv->ctx == NULL)
  87. goto err;
  88. if (certfile != NULL
  89. && SSL_CTX_use_certificate_file(srv->ctx, certfile, SSL_FILETYPE_PEM) <= 0)
  90. goto err;
  91. if (keyfile != NULL
  92. && SSL_CTX_use_PrivateKey_file(srv->ctx, keyfile, SSL_FILETYPE_PEM) <= 0)
  93. goto err;
  94. SSL_CTX_set_alpn_select_cb(srv->ctx, alpn_select_cb, srv);
  95. srv->tls = SSL_new(srv->ctx);
  96. if (srv->tls == NULL)
  97. goto err;
  98. engine_args.libctx = srv->args.libctx;
  99. engine_args.propq = srv->args.propq;
  100. engine_args.mutex = srv->mutex;
  101. engine_args.now_cb = srv->args.now_cb;
  102. engine_args.now_cb_arg = srv->args.now_cb_arg;
  103. if ((srv->engine = ossl_quic_engine_new(&engine_args)) == NULL)
  104. goto err;
  105. port_args.channel_ctx = srv->ctx;
  106. port_args.is_multi_conn = 1;
  107. if ((srv->port = ossl_quic_engine_create_port(srv->engine, &port_args)) == NULL)
  108. goto err;
  109. if ((srv->ch = ossl_quic_port_create_incoming(srv->port, srv->tls)) == NULL)
  110. goto err;
  111. if (!ossl_quic_port_set_net_rbio(srv->port, srv->args.net_rbio)
  112. || !ossl_quic_port_set_net_wbio(srv->port, srv->args.net_wbio))
  113. goto err;
  114. qc = OPENSSL_zalloc(sizeof(*qc));
  115. if (qc == NULL)
  116. goto err;
  117. srv->ssl = (SSL *)qc;
  118. qc->ch = srv->ch;
  119. srv->ssl->type = SSL_TYPE_QUIC_CONNECTION;
  120. return srv;
  121. err:
  122. if (srv != NULL) {
  123. if (args->ctx == NULL)
  124. SSL_CTX_free(srv->ctx);
  125. SSL_free(srv->tls);
  126. ossl_quic_channel_free(srv->ch);
  127. ossl_quic_port_free(srv->port);
  128. ossl_quic_engine_free(srv->engine);
  129. #if defined(OPENSSL_THREADS)
  130. ossl_crypto_mutex_free(&srv->mutex);
  131. #endif
  132. OPENSSL_free(qc);
  133. }
  134. OPENSSL_free(srv);
  135. return NULL;
  136. }
  137. void ossl_quic_tserver_free(QUIC_TSERVER *srv)
  138. {
  139. if (srv == NULL)
  140. return;
  141. SSL_free(srv->tls);
  142. ossl_quic_channel_free(srv->ch);
  143. ossl_quic_port_free(srv->port);
  144. ossl_quic_engine_free(srv->engine);
  145. BIO_free_all(srv->args.net_rbio);
  146. BIO_free_all(srv->args.net_wbio);
  147. OPENSSL_free(srv->ssl);
  148. SSL_CTX_free(srv->ctx);
  149. #if defined(OPENSSL_THREADS)
  150. ossl_crypto_mutex_free(&srv->mutex);
  151. #endif
  152. OPENSSL_free(srv);
  153. }
  154. /* Set mutator callbacks for test framework support */
  155. int ossl_quic_tserver_set_plain_packet_mutator(QUIC_TSERVER *srv,
  156. ossl_mutate_packet_cb mutatecb,
  157. ossl_finish_mutate_cb finishmutatecb,
  158. void *mutatearg)
  159. {
  160. return ossl_quic_channel_set_mutator(srv->ch, mutatecb, finishmutatecb,
  161. mutatearg);
  162. }
  163. int ossl_quic_tserver_set_handshake_mutator(QUIC_TSERVER *srv,
  164. ossl_statem_mutate_handshake_cb mutate_handshake_cb,
  165. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
  166. void *mutatearg)
  167. {
  168. return ossl_statem_set_mutator(ossl_quic_channel_get0_ssl(srv->ch),
  169. mutate_handshake_cb,
  170. finish_mutate_handshake_cb,
  171. mutatearg);
  172. }
  173. int ossl_quic_tserver_tick(QUIC_TSERVER *srv)
  174. {
  175. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
  176. if (ossl_quic_channel_is_active(srv->ch))
  177. srv->connected = 1;
  178. return 1;
  179. }
  180. int ossl_quic_tserver_is_connected(QUIC_TSERVER *srv)
  181. {
  182. return ossl_quic_channel_is_active(srv->ch);
  183. }
  184. /* Returns 1 if the server is in any terminating or terminated state */
  185. int ossl_quic_tserver_is_term_any(const QUIC_TSERVER *srv)
  186. {
  187. return ossl_quic_channel_is_term_any(srv->ch);
  188. }
  189. const QUIC_TERMINATE_CAUSE *
  190. ossl_quic_tserver_get_terminate_cause(const QUIC_TSERVER *srv)
  191. {
  192. return ossl_quic_channel_get_terminate_cause(srv->ch);
  193. }
  194. /* Returns 1 if the server is in a terminated state */
  195. int ossl_quic_tserver_is_terminated(const QUIC_TSERVER *srv)
  196. {
  197. return ossl_quic_channel_is_terminated(srv->ch);
  198. }
  199. int ossl_quic_tserver_is_handshake_confirmed(const QUIC_TSERVER *srv)
  200. {
  201. return ossl_quic_channel_is_handshake_confirmed(srv->ch);
  202. }
  203. int ossl_quic_tserver_read(QUIC_TSERVER *srv,
  204. uint64_t stream_id,
  205. unsigned char *buf,
  206. size_t buf_len,
  207. size_t *bytes_read)
  208. {
  209. int is_fin = 0;
  210. QUIC_STREAM *qs;
  211. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  212. stream_id);
  213. if (qs == NULL) {
  214. int is_client_init
  215. = ((stream_id & QUIC_STREAM_INITIATOR_MASK)
  216. == QUIC_STREAM_INITIATOR_CLIENT);
  217. /*
  218. * A client-initiated stream might spontaneously come into existence, so
  219. * allow trying to read on a client-initiated stream before it exists,
  220. * assuming the connection is still active.
  221. * Otherwise, fail.
  222. */
  223. if (!is_client_init || !ossl_quic_channel_is_active(srv->ch))
  224. return 0;
  225. *bytes_read = 0;
  226. return 1;
  227. }
  228. if (qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ
  229. || !ossl_quic_stream_has_recv_buffer(qs))
  230. return 0;
  231. if (!ossl_quic_rstream_read(qs->rstream, buf, buf_len,
  232. bytes_read, &is_fin))
  233. return 0;
  234. if (*bytes_read > 0) {
  235. /*
  236. * We have read at least one byte from the stream. Inform stream-level
  237. * RXFC of the retirement of controlled bytes. Update the active stream
  238. * status (the RXFC may now want to emit a frame granting more credit to
  239. * the peer).
  240. */
  241. OSSL_RTT_INFO rtt_info;
  242. ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(srv->ch), &rtt_info);
  243. if (!ossl_quic_rxfc_on_retire(&qs->rxfc, *bytes_read,
  244. rtt_info.smoothed_rtt))
  245. return 0;
  246. }
  247. if (is_fin)
  248. ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv->ch),
  249. qs);
  250. if (*bytes_read > 0)
  251. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
  252. return 1;
  253. }
  254. int ossl_quic_tserver_has_read_ended(QUIC_TSERVER *srv, uint64_t stream_id)
  255. {
  256. QUIC_STREAM *qs;
  257. unsigned char buf[1];
  258. size_t bytes_read = 0;
  259. int is_fin = 0;
  260. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  261. stream_id);
  262. if (qs == NULL)
  263. return 0;
  264. if (qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ)
  265. return 1;
  266. if (!ossl_quic_stream_has_recv_buffer(qs))
  267. return 0;
  268. /*
  269. * If we do not have the DATA_READ, it is possible we should still return 1
  270. * if there is a lone FIN (but no more data) remaining to be retired from
  271. * the RSTREAM, for example because ossl_quic_tserver_read() has not been
  272. * called since the FIN was received.
  273. */
  274. if (!ossl_quic_rstream_peek(qs->rstream, buf, sizeof(buf),
  275. &bytes_read, &is_fin))
  276. return 0;
  277. if (is_fin && bytes_read == 0) {
  278. /* If we have a FIN awaiting retirement and no data before it... */
  279. /* Let RSTREAM know we've consumed this FIN. */
  280. if (!ossl_quic_rstream_read(qs->rstream, buf, sizeof(buf),
  281. &bytes_read, &is_fin))
  282. return 0;
  283. assert(is_fin && bytes_read == 0);
  284. assert(qs->recv_state == QUIC_RSTREAM_STATE_DATA_RECVD);
  285. ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv->ch),
  286. qs);
  287. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. int ossl_quic_tserver_write(QUIC_TSERVER *srv,
  293. uint64_t stream_id,
  294. const unsigned char *buf,
  295. size_t buf_len,
  296. size_t *bytes_written)
  297. {
  298. QUIC_STREAM *qs;
  299. if (!ossl_quic_channel_is_active(srv->ch))
  300. return 0;
  301. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  302. stream_id);
  303. if (qs == NULL || !ossl_quic_stream_has_send_buffer(qs))
  304. return 0;
  305. if (!ossl_quic_sstream_append(qs->sstream,
  306. buf, buf_len, bytes_written))
  307. return 0;
  308. if (*bytes_written > 0)
  309. /*
  310. * We have appended at least one byte to the stream. Potentially mark
  311. * the stream as active, depending on FC.
  312. */
  313. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
  314. /* Try and send. */
  315. ossl_quic_tserver_tick(srv);
  316. return 1;
  317. }
  318. int ossl_quic_tserver_conclude(QUIC_TSERVER *srv, uint64_t stream_id)
  319. {
  320. QUIC_STREAM *qs;
  321. if (!ossl_quic_channel_is_active(srv->ch))
  322. return 0;
  323. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  324. stream_id);
  325. if (qs == NULL || !ossl_quic_stream_has_send_buffer(qs))
  326. return 0;
  327. if (!ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
  328. ossl_quic_sstream_fin(qs->sstream);
  329. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
  330. }
  331. ossl_quic_tserver_tick(srv);
  332. return 1;
  333. }
  334. int ossl_quic_tserver_stream_new(QUIC_TSERVER *srv,
  335. int is_uni,
  336. uint64_t *stream_id)
  337. {
  338. QUIC_STREAM *qs;
  339. if (!ossl_quic_channel_is_active(srv->ch))
  340. return 0;
  341. if ((qs = ossl_quic_channel_new_stream_local(srv->ch, is_uni)) == NULL)
  342. return 0;
  343. *stream_id = qs->id;
  344. return 1;
  345. }
  346. BIO *ossl_quic_tserver_get0_rbio(QUIC_TSERVER *srv)
  347. {
  348. return srv->args.net_rbio;
  349. }
  350. SSL_CTX *ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER *srv)
  351. {
  352. return srv->ctx;
  353. }
  354. int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER *srv,
  355. uint64_t stream_id,
  356. uint64_t *app_error_code)
  357. {
  358. QUIC_STREAM *qs;
  359. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  360. stream_id);
  361. if (qs == NULL)
  362. return 0;
  363. if (qs->peer_stop_sending && app_error_code != NULL)
  364. *app_error_code = qs->peer_stop_sending_aec;
  365. return qs->peer_stop_sending;
  366. }
  367. int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER *srv,
  368. uint64_t stream_id,
  369. uint64_t *app_error_code)
  370. {
  371. QUIC_STREAM *qs;
  372. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  373. stream_id);
  374. if (qs == NULL)
  375. return 0;
  376. if (ossl_quic_stream_recv_is_reset(qs) && app_error_code != NULL)
  377. *app_error_code = qs->peer_reset_stream_aec;
  378. return ossl_quic_stream_recv_is_reset(qs);
  379. }
  380. int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER *srv,
  381. const QUIC_CONN_ID *conn_id)
  382. {
  383. /* Replace existing local connection ID in the QUIC_CHANNEL */
  384. return ossl_quic_channel_replace_local_cid(srv->ch, conn_id);
  385. }
  386. uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER *srv)
  387. {
  388. QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(srv->ch);
  389. QUIC_STREAM *qs = ossl_quic_stream_map_peek_accept_queue(qsm);
  390. if (qs == NULL)
  391. return UINT64_MAX;
  392. ossl_quic_stream_map_remove_from_accept_queue(qsm, qs, ossl_time_zero());
  393. return qs->id;
  394. }
  395. int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER *srv,
  396. uint64_t stream_id)
  397. {
  398. QUIC_STREAM *qs;
  399. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
  400. stream_id);
  401. if (qs == NULL)
  402. return 1;
  403. return ossl_quic_sstream_is_totally_acked(qs->sstream);
  404. }
  405. int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER *srv)
  406. {
  407. return ossl_quic_reactor_net_read_desired(
  408. ossl_quic_channel_get_reactor(srv->ch));
  409. }
  410. int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER *srv)
  411. {
  412. return ossl_quic_reactor_net_write_desired(
  413. ossl_quic_channel_get_reactor(srv->ch));
  414. }
  415. OSSL_TIME ossl_quic_tserver_get_deadline(QUIC_TSERVER *srv)
  416. {
  417. return ossl_quic_reactor_get_tick_deadline(
  418. ossl_quic_channel_get_reactor(srv->ch));
  419. }
  420. int ossl_quic_tserver_shutdown(QUIC_TSERVER *srv, uint64_t app_error_code)
  421. {
  422. ossl_quic_channel_local_close(srv->ch, app_error_code, NULL);
  423. /* TODO(QUIC SERVER): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
  424. if (ossl_quic_channel_is_terminated(srv->ch))
  425. return 1;
  426. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
  427. return ossl_quic_channel_is_terminated(srv->ch);
  428. }
  429. int ossl_quic_tserver_ping(QUIC_TSERVER *srv)
  430. {
  431. if (ossl_quic_channel_is_terminated(srv->ch))
  432. return 0;
  433. if (!ossl_quic_channel_ping(srv->ch))
  434. return 0;
  435. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
  436. return 1;
  437. }
  438. QUIC_CHANNEL *ossl_quic_tserver_get_channel(QUIC_TSERVER *srv)
  439. {
  440. return srv->ch;
  441. }
  442. void ossl_quic_tserver_set_msg_callback(QUIC_TSERVER *srv,
  443. void (*f)(int write_p, int version,
  444. int content_type,
  445. const void *buf, size_t len,
  446. SSL *ssl, void *arg),
  447. void *arg)
  448. {
  449. ossl_quic_channel_set_msg_callback(srv->ch, f, srv->ssl);
  450. ossl_quic_channel_set_msg_callback_arg(srv->ch, arg);
  451. SSL_set_msg_callback(srv->tls, f);
  452. SSL_set_msg_callback_arg(srv->tls, arg);
  453. }
  454. int ossl_quic_tserver_new_ticket(QUIC_TSERVER *srv)
  455. {
  456. return SSL_new_session_ticket(srv->tls);
  457. }
  458. int ossl_quic_tserver_set_max_early_data(QUIC_TSERVER *srv,
  459. uint32_t max_early_data)
  460. {
  461. return SSL_set_max_early_data(srv->tls, max_early_data);
  462. }
  463. void ossl_quic_tserver_set_psk_find_session_cb(QUIC_TSERVER *srv,
  464. SSL_psk_find_session_cb_func cb)
  465. {
  466. SSL_set_psk_find_session_callback(srv->tls, cb);
  467. }