ossl-guide-quic-multi-stream.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. =pod
  2. =begin comment
  3. NB: Changes to the source code samples in this file should also be reflected in
  4. demos/guide/quic-multi-stream.c
  5. =end comment
  6. =head1 NAME
  7. ossl-guide-quic-multi-stream
  8. - OpenSSL Guide: Writing a simple multi-stream QUIC client
  9. =head1 INTRODUCTION
  10. This page will introduce some important concepts required to write a simple
  11. QUIC multi-stream application. It assumes a basic understanding of QUIC and how
  12. it is used in OpenSSL. See L<ossl-guide-quic-introduction(7)> and
  13. L<ossl-guide-quic-client-block(7)>.
  14. =head1 QUIC STREAMS
  15. In a QUIC multi-stream application we separate out the concepts of a QUIC
  16. "connection" and a QUIC "stream". A connection object represents the overarching
  17. details of the connection between a client and a server including all its
  18. negotiated and configured parameters. We use the B<SSL> object for that in an
  19. OpenSSL application (known as the connection B<SSL> object). It is created by an
  20. application calling L<SSL_new(3)>.
  21. Separately a connection can have zero or more streams associated with it
  22. (although a connection with zero streams is probably not very useful, so
  23. normally you would have at least one). A stream is used to send and receive
  24. data between the two peers. Each stream is also represented by an B<SSL>
  25. object. A stream is logically independent of all the other streams associated
  26. with the same connection. Data sent on a stream is guaranteed to be delivered
  27. in the order that it was sent within that stream. The same is not true across
  28. streams, e.g. if an application sends data on stream 1 first and then sends some
  29. more data on stream 2 second, then the remote peer may receive the data sent on
  30. stream 2 before it receives the data sent on stream 1.
  31. Once the connection B<SSL> object has completed its handshake (i.e.
  32. L<SSL_connect(3)> has returned 1), stream B<SSL> objects are created by the
  33. application calling L<SSL_new_stream(3)> or L<SSL_accept_stream(3)> (see
  34. L</CREATING NEW STREAMS> below).
  35. The same threading rules apply to B<SSL> objects as for most OpenSSL objects
  36. (see L<ossl-guide-libraries-introduction(7)>). In particular most OpenSSL
  37. functions are thread safe, but the B<SSL> object is not. This means that you can
  38. use an B<SSL> object representing one stream at the same time as another thread
  39. is using a different B<SSL> object for a different stream on the same
  40. connection. But you cannot use the same B<SSL> object on two different threads
  41. at the same time (without additional application level locking).
  42. =head1 THE DEFAULT STREAM
  43. A connection B<SSL> object may also (optionally) be associated with a stream.
  44. This stream is known as the default stream. The default stream is automatically
  45. created and associated with the B<SSL> object when the application calls
  46. L<SSL_read_ex(3)>, L<SSL_read(3)>, L<SSL_write_ex(3)> or L<SSL_write(3)> and
  47. passes the connection B<SSL> object as a parameter.
  48. If a client application calls L<SSL_write_ex(3)> or L<SSL_write(3)> first then
  49. (by default) the default stream will be a client-initiated bi-directional
  50. stream. If a client application calls L<SSL_read_ex(3)> or L<SSL_read(3)>
  51. first then the first stream initiated by the server will be used as the default
  52. stream (whether it is bi-directional or uni-directional).
  53. This behaviour can be controlled via the default stream mode. See
  54. L<SSL_set_default_stream_mode(3)> for further details.
  55. It is recommended that new multi-stream applications should not use a default
  56. stream at all and instead should use a separate stream B<SSL> object for each
  57. stream that is used. This requires calling L<SSL_set_default_stream_mode(3)>
  58. and setting the mode to B<SSL_DEFAULT_STREAM_MODE_NONE>.
  59. =head1 CREATING NEW STREAMS
  60. An endpoint can create a new stream by calling L<SSL_new_stream(3)>. This
  61. creates a locally initiated stream. In order to do so you must pass the QUIC
  62. connection B<SSL> object as a parameter. You can also specify whether you want a
  63. bi-directional or a uni-directional stream.
  64. The function returns a new QUIC stream B<SSL> object for sending and receiving
  65. data on that stream.
  66. The peer may also initiate streams. An application can use the function
  67. L<SSL_get_accept_stream_queue_len(3)> to determine the number of streams that
  68. the peer has initiated that are waiting for the application to handle. An
  69. application can call L<SSL_accept_stream(3)> to create a new B<SSL> object for
  70. a remotely initiated stream. If the peer has not initiated any then this call
  71. will block until one is available if the connection object is in blocking mode
  72. (see L<SSL_set_blocking_mode(3)>).
  73. When using a default stream OpenSSL will prevent new streams from being
  74. accepted. To override this behaviour you must call
  75. L<SSL_set_incoming_stream_policy(3)> to set the policy to
  76. B<SSL_INCOMING_STREAM_POLICY_ACCEPT>. See the man page for further details. This
  77. is not relevant if the default stream has been disabled as described in
  78. L</THE DEFAULT STREAM> above.
  79. Any stream may be bi-directional or uni-directional. If it is uni-directional
  80. then the initiator can write to it but not read from it, and vice-versa for the
  81. peer. You can determine what type of stream an B<SSL> object represents by
  82. calling L<SSL_get_stream_type(3)>. See the man page for further details.
  83. =head1 USING A STREAM TO SEND AND RECEIVE DATA
  84. Once you have a stream B<SSL> object (which includes the connection B<SSL>
  85. object if a default stream is in use) then you can send and receive data over it
  86. using the L<SSL_write_ex(3)>, L<SSL_write(3)>, L<SSL_read_ex(3)> or
  87. L<SSL_read(3)> functions. See the man pages for further details.
  88. In the event of one of these functions not returning a success code then
  89. you should call L<SSL_get_error(3)> to find out further details about the error.
  90. In blocking mode this will either be a fatal error (e.g. B<SSL_ERROR_SYSCALL>
  91. or B<SSL_ERROR_SSL>), or it will be B<SSL_ERROR_ZERO_RETURN> which can occur
  92. when attempting to read data from a stream and the peer has indicated that the
  93. stream is concluded (i.e. "FIN" has been signalled on the stream). This means
  94. that the peer will send no more data on that stream. Note that the
  95. interpretation of B<SSL_ERROR_ZERO_RETURN> is slightly different for a QUIC
  96. application compared to a TLS application. In TLS it occurs when the connection
  97. has been shutdown by the peer. In QUIC this only tells you that the current
  98. stream has been concluded by the peer. It tells you nothing about the underlying
  99. connection. If the peer has concluded the stream then no more data will be
  100. received on it, however an application can still send data to the peer until
  101. the send side of the stream has also been concluded. This can happen by the
  102. application calling L<SSL_stream_conclude(3)>. It is an error to attempt to
  103. send more data on a stream after L<SSL_stream_conclude(3)> has been called.
  104. It is also possible to abandon a stream abnormally by calling
  105. L<SSL_stream_reset(3)>.
  106. Once a stream object is no longer needed it should be freed via a call to
  107. L<SSL_free(3)>. An application should not call L<SSL_shutdown(3)> on it since
  108. this is only meaningful for connection level B<SSL> objects. Freeing the stream
  109. will automatically signal STOP_SENDING to the peer.
  110. =head1 STREAMS AND CONNECTIONS
  111. Given a stream object it is possible to get the B<SSL> object corresponding to
  112. the connection via a call to L<SSL_get0_connection(3)>. Multi-threaded
  113. restrictions apply so care should be taken when using the returned connection
  114. object. Specifically, if you are handling each of your stream objects in a
  115. different thread and call L<SSL_get0_connection(3)> from within that thread then
  116. you must be careful to not to call any function that uses the connection object
  117. at the same time as one of the other threads is also using that connection
  118. object (with the exception of L<SSL_accept_stream(3)> and
  119. L<SSL_get_accept_stream_queue_len(3)> which are thread-safe).
  120. A stream object does not inherit all its settings and values from its parent
  121. B<SSL> connection object. Therefore certain function calls that are relevant to
  122. the connection as a whole will not work on a stream. For example the function
  123. L<SSL_get_certificate(3)> can be used to obtain a handle on the peer certificate
  124. when called with a connection B<SSL> object. When called with a stream B<SSL>
  125. object it will return NULL.
  126. =head1 SIMPLE MULTI-STREAM QUIC CLIENT EXAMPLE
  127. This section will present various source code samples demonstrating how to write
  128. a simple multi-stream QUIC client application which connects to a server, send
  129. some HTTP/1.0 requests to it, and read back the responses. Note that HTTP/1.0
  130. over QUIC is non-standard and will not be supported by real world servers. This
  131. is for demonstration purposes only.
  132. We will build on the example code for the simple blocking QUIC client that is
  133. covered on the L<ossl-guide-quic-client-block(7)> page and we assume that you
  134. are familiar with it. We will only describe the differences between the simple
  135. blocking QUIC client and the multi-stream QUIC client. Although the example code
  136. uses blocking B<SSL> objects, you can equally use nonblocking B<SSL> objects.
  137. See L<ossl-guide-quic-client-non-block(7)> for more information about writing a
  138. nonblocking QUIC client.
  139. The complete source code for this example multi-stream QUIC client is available
  140. in the C<demos/guide> directory of the OpenSSL source distribution in the file
  141. C<quic-multi-stream.c>. It is also available online at
  142. L<https://github.com/openssl/openssl/blob/master/demos/guide/quic-multi-stream.c>.
  143. =head2 Disabling the default stream
  144. As discussed above in L</THE DEFAULT STREAM> we will follow the recommendation
  145. to disable the default stream for our multi-stream client. To do this we call
  146. the L<SSL_set_default_stream_mode(3)> function and pass in our connection B<SSL>
  147. object and the value B<SSL_DEFAULT_STREAM_MODE_NONE>.
  148. /*
  149. * We will use multiple streams so we will disable the default stream mode.
  150. * This is not a requirement for using multiple streams but is recommended.
  151. */
  152. if (!SSL_set_default_stream_mode(ssl, SSL_DEFAULT_STREAM_MODE_NONE)) {
  153. printf("Failed to disable the default stream mode\n");
  154. goto end;
  155. }
  156. =head2 Creating the request streams
  157. For the purposes of this example we will create two different streams to send
  158. two different HTTP requests to the server. For the purposes of demonstration the
  159. first of these will be a bi-directional stream and the second one will be a
  160. uni-directional one:
  161. /*
  162. * We create two new client initiated streams. The first will be
  163. * bi-directional, and the second will be uni-directional.
  164. */
  165. stream1 = SSL_new_stream(ssl, 0);
  166. stream2 = SSL_new_stream(ssl, SSL_STREAM_FLAG_UNI);
  167. if (stream1 == NULL || stream2 == NULL) {
  168. printf("Failed to create streams\n");
  169. goto end;
  170. }
  171. =head2 Writing data to the streams
  172. Once the streams are successfully created we can start writing data to them. In
  173. this example we will be sending a different HTTP request on each stream. To
  174. avoid repeating too much code we write a simple helper function to send an HTTP
  175. request to a stream:
  176. int write_a_request(SSL *stream, const char *request_start,
  177. const char *hostname)
  178. {
  179. const char *request_end = "\r\n\r\n";
  180. size_t written;
  181. if (!SSL_write_ex(stream, request_start, strlen(request_start), &written))
  182. return 0;
  183. if (!SSL_write_ex(stream, hostname, strlen(hostname), &written))
  184. return 0;
  185. if (!SSL_write_ex(stream, request_end, strlen(request_end), &written))
  186. return 0;
  187. return 1;
  188. }
  189. We assume the strings B<request1_start> and B<request2_start> hold the
  190. appropriate HTTP requests. We can then call our helper function above to send
  191. the requests on the two streams. For the sake of simplicity this example does
  192. this sequentially, writing to B<stream1> first and, when this is successful,
  193. writing to B<stream2> second. Remember that our client is blocking so these
  194. calls will only return once they have been successfully completed. A real
  195. application would not need to do these writes sequentially or in any particular
  196. order. For example we could start two threads (one for each stream) and write
  197. the requests to each stream simultaneously.
  198. /* Write an HTTP GET request on each of our streams to the peer */
  199. if (!write_a_request(stream1, request1_start, hostname)) {
  200. printf("Failed to write HTTP request on stream 1\n");
  201. goto end;
  202. }
  203. if (!write_a_request(stream2, request2_start, hostname)) {
  204. printf("Failed to write HTTP request on stream 2\n");
  205. goto end;
  206. }
  207. =head2 Reading data from a stream
  208. In this example B<stream1> is a bi-directional stream so, once we have sent the
  209. request on it, we can attempt to read the response from the server back. Here
  210. we just repeatedly call L<SSL_read_ex(3)> until that function fails (indicating
  211. either that there has been a problem, or that the peer has signalled the stream
  212. as concluded).
  213. printf("Stream 1 data:\n");
  214. /*
  215. * Get up to sizeof(buf) bytes of the response from stream 1 (which is a
  216. * bidirectional stream). We keep reading until the server closes the
  217. * connection.
  218. */
  219. while (SSL_read_ex(stream1, buf, sizeof(buf), &readbytes)) {
  220. /*
  221. * OpenSSL does not guarantee that the returned data is a string or
  222. * that it is NUL terminated so we use fwrite() to write the exact
  223. * number of bytes that we read. The data could be non-printable or
  224. * have NUL characters in the middle of it. For this simple example
  225. * we're going to print it to stdout anyway.
  226. */
  227. fwrite(buf, 1, readbytes, stdout);
  228. }
  229. /* In case the response didn't finish with a newline we add one now */
  230. printf("\n");
  231. In a blocking application like this one calls to L<SSL_read_ex(3)> will either
  232. succeed immediately returning data that is already available, or they will block
  233. waiting for more data to become available and return it when it is, or they will
  234. fail with a 0 response code.
  235. Once we exit the while loop above we know that the last call to
  236. L<SSL_read_ex(3)> gave a 0 response code so we call the L<SSL_get_error(3)>
  237. function to find out more details. Since this is a blocking application this
  238. will either return B<SSL_ERROR_SYSCALL> or B<SSL_ERROR_SSL> indicating a
  239. fundamental problem, or it will return B<SSL_ERROR_ZERO_RETURN> indicating that
  240. the stream is concluded and there will be no more data available to read from
  241. it. Care must be taken to distinguish between an error at the stream level (i.e.
  242. a stream reset) and an error at the connection level (i.e. a connection closed).
  243. The L<SSL_get_stream_read_state(3)> function can be used to distinguish between
  244. these different cases.
  245. /*
  246. * Check whether we finished the while loop above normally or as the
  247. * result of an error. The 0 argument to SSL_get_error() is the return
  248. * code we received from the SSL_read_ex() call. It must be 0 in order
  249. * to get here. Normal completion is indicated by SSL_ERROR_ZERO_RETURN. In
  250. * QUIC terms this means that the peer has sent FIN on the stream to
  251. * indicate that no further data will be sent.
  252. */
  253. switch (SSL_get_error(stream1, 0)) {
  254. case SSL_ERROR_ZERO_RETURN:
  255. /* Normal completion of the stream */
  256. break;
  257. case SSL_ERROR_SSL:
  258. /*
  259. * Some stream fatal error occurred. This could be because of a stream
  260. * reset - or some failure occurred on the underlying connection.
  261. */
  262. switch (SSL_get_stream_read_state(stream1)) {
  263. case SSL_STREAM_STATE_RESET_REMOTE:
  264. printf("Stream reset occurred\n");
  265. /* The stream has been reset but the connection is still healthy. */
  266. break;
  267. case SSL_STREAM_STATE_CONN_CLOSED:
  268. printf("Connection closed\n");
  269. /* Connection is already closed. Skip SSL_shutdown() */
  270. goto end;
  271. default:
  272. printf("Unknown stream failure\n");
  273. break;
  274. }
  275. break;
  276. default:
  277. /* Some other unexpected error occurred */
  278. printf ("Failed reading remaining data\n");
  279. break;
  280. }
  281. =head2 Accepting an incoming stream
  282. Our B<stream2> object that we created above was a uni-directional stream so it
  283. cannot be used to receive data from the server. In this hypothetical example
  284. we assume that the server initiates a new stream to send us back the data that
  285. we requested. To do that we call L<SSL_accept_stream(3)>. Since this is a
  286. blocking application this will wait indefinitely until the new stream has
  287. arrived and is available for us to accept. In the event of an error it will
  288. return B<NULL>.
  289. /*
  290. * In our hypothetical HTTP/1.0 over QUIC protocol that we are using we
  291. * assume that the server will respond with a server initiated stream
  292. * containing the data requested in our uni-directional stream. This doesn't
  293. * really make sense to do in a real protocol, but its just for
  294. * demonstration purposes.
  295. *
  296. * We're using blocking mode so this will block until a stream becomes
  297. * available. We could override this behaviour if we wanted to by setting
  298. * the SSL_ACCEPT_STREAM_NO_BLOCK flag in the second argument below.
  299. */
  300. stream3 = SSL_accept_stream(ssl, 0);
  301. if (stream3 == NULL) {
  302. printf("Failed to accept a new stream\n");
  303. goto end;
  304. }
  305. We can now read data from the stream in the same way that we did for B<stream1>
  306. above. We won't repeat that here.
  307. =head2 Cleaning up the streams
  308. Once we have finished using our streams we can simply free them by calling
  309. L<SSL_free(3)>. Optionally we could call L<SSL_stream_conclude(3)> on them if
  310. we want to indicate to the peer that we won't be sending them any more data, but
  311. we don't do that in this example because we assume that the HTTP application
  312. protocol supplies sufficient information for the peer to know when we have
  313. finished sending request data.
  314. We should not call L<SSL_shutdown(3)> or L<SSL_shutdown_ex(3)> on the stream
  315. objects since those calls should not be used for streams.
  316. SSL_free(stream1);
  317. SSL_free(stream2);
  318. SSL_free(stream3);
  319. =head1 SEE ALSO
  320. L<ossl-guide-introduction(7)>, L<ossl-guide-libraries-introduction(7)>,
  321. L<ossl-guide-libssl-introduction(7)> L<ossl-guide-quic-introduction(7)>,
  322. L<ossl-guide-quic-client-block(7)>
  323. =head1 COPYRIGHT
  324. Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  325. Licensed under the Apache License 2.0 (the "License"). You may not use
  326. this file except in compliance with the License. You can obtain a copy
  327. in the file LICENSE in the source distribution or at
  328. L<https://www.openssl.org/source/license.html>.
  329. =cut