SSL_shutdown.3ossl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .nr rF 0
  58. .if \n(.g .if rF .nr rF 1
  59. .if (\n(rF:(\n(.g==0)) \{\
  60. . if \nF \{\
  61. . de IX
  62. . tm Index:\\$1\t\\n%\t"\\$2"
  63. ..
  64. . if !\nF==2 \{\
  65. . nr % 0
  66. . nr F 2
  67. . \}
  68. . \}
  69. .\}
  70. .rr rF
  71. .\"
  72. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  73. .\" Fear. Run. Save yourself. No user-serviceable parts.
  74. . \" fudge factors for nroff and troff
  75. .if n \{\
  76. . ds #H 0
  77. . ds #V .8m
  78. . ds #F .3m
  79. . ds #[ \f1
  80. . ds #] \fP
  81. .\}
  82. .if t \{\
  83. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  84. . ds #V .6m
  85. . ds #F 0
  86. . ds #[ \&
  87. . ds #] \&
  88. .\}
  89. . \" simple accents for nroff and troff
  90. .if n \{\
  91. . ds ' \&
  92. . ds ` \&
  93. . ds ^ \&
  94. . ds , \&
  95. . ds ~ ~
  96. . ds /
  97. .\}
  98. .if t \{\
  99. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  100. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  101. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  102. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  103. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  104. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  105. .\}
  106. . \" troff and (daisy-wheel) nroff accents
  107. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  108. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  109. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  110. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  111. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  112. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  113. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  114. .ds ae a\h'-(\w'a'u*4/10)'e
  115. .ds Ae A\h'-(\w'A'u*4/10)'E
  116. . \" corrections for vroff
  117. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  118. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  119. . \" for low resolution devices (crt and lpr)
  120. .if \n(.H>23 .if \n(.V>19 \
  121. \{\
  122. . ds : e
  123. . ds 8 ss
  124. . ds o a
  125. . ds d- d\h'-1'\(ga
  126. . ds D- D\h'-1'\(hy
  127. . ds th \o'bp'
  128. . ds Th \o'LP'
  129. . ds ae ae
  130. . ds Ae AE
  131. .\}
  132. .rm #[ #] #H #V #F C
  133. .\" ========================================================================
  134. .\"
  135. .IX Title "SSL_SHUTDOWN 3ossl"
  136. .TH SSL_SHUTDOWN 3ossl "2024-09-03" "3.3.2" "OpenSSL"
  137. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  138. .\" way too many mistakes in technical documents.
  139. .if n .ad l
  140. .nh
  141. .SH "NAME"
  142. SSL_shutdown, SSL_shutdown_ex \- shut down a TLS/SSL or QUIC connection
  143. .SH "SYNOPSIS"
  144. .IX Header "SYNOPSIS"
  145. .Vb 1
  146. \& #include <openssl/ssl.h>
  147. \&
  148. \& int SSL_shutdown(SSL *ssl);
  149. \&
  150. \& typedef struct ssl_shutdown_ex_args_st {
  151. \& uint64_t quic_error_code;
  152. \& const char *quic_reason;
  153. \& } SSL_SHUTDOWN_EX_ARGS;
  154. \&
  155. \& _\|_owur int SSL_shutdown_ex(SSL *ssl, uint64_t flags,
  156. \& const SSL_SHUTDOWN_EX_ARGS *args,
  157. \& size_t args_len);
  158. .Ve
  159. .SH "DESCRIPTION"
  160. .IX Header "DESCRIPTION"
  161. \&\fBSSL_shutdown()\fR shuts down an active connection represented by an \s-1SSL\s0 object.
  162. .PP
  163. \&\fBSSL_shutdown_ex()\fR is an extended version of \fBSSL_shutdown()\fR. If non-NULL, \fIargs\fR
  164. must point to a \fB\s-1SSL_SHUTDOWN_EX_ARGS\s0\fR structure and \fIargs_len\fR must be set to
  165. \&\f(CW\*(C`sizeof(SSL_SHUTDOWN_EX_ARGS)\*(C'\fR. The \fB\s-1SSL_SHUTDOWN_EX_ARGS\s0\fR structure must be
  166. zero-initialized. If \fIargs\fR is \s-1NULL,\s0 the behaviour is the same as passing a
  167. zero-initialised \fB\s-1SSL_SHUTDOWN_EX_ARGS\s0\fR structure. Currently, all extended
  168. arguments relate to usage with \s-1QUIC,\s0 therefore this call functions identically
  169. to \fBSSL_shutdown()\fR when not being used with \s-1QUIC.\s0
  170. .PP
  171. While the general operation of \fBSSL_shutdown()\fR is common between protocols, the
  172. exact nature of how a shutdown is performed depends on the underlying protocol
  173. being used. See the section below pertaining to each protocol for more
  174. information.
  175. .PP
  176. In general, calling \fBSSL_shutdown()\fR in nonblocking mode will initiate the
  177. shutdown process and return 0 to indicate that the shutdown process has not yet
  178. completed. Once the shutdown process has completed, subsequent calls to
  179. \&\fBSSL_shutdown()\fR will return 1. See the \s-1RETURN VALUES\s0 section for more
  180. information.
  181. .PP
  182. \&\fBSSL_shutdown()\fR should not be called if a previous fatal error has occurred on a
  183. connection; i.e., if \fBSSL_get_error\fR\|(3) has returned \fB\s-1SSL_ERROR_SYSCALL\s0\fR or
  184. \&\fB\s-1SSL_ERROR_SSL\s0\fR.
  185. .SH "TLS AND DTLS-SPECIFIC CONSIDERATIONS"
  186. .IX Header "TLS AND DTLS-SPECIFIC CONSIDERATIONS"
  187. Shutdown for \s-1SSL/TLS\s0 and \s-1DTLS\s0 is implemented in terms of the \s-1SSL/TLS/DTLS\s0
  188. close_notify alert message. The shutdown process for \s-1SSL/TLS\s0 and \s-1DTLS\s0
  189. consists of two steps:
  190. .IP "\(bu" 4
  191. A close_notify shutdown alert message is sent to the peer.
  192. .IP "\(bu" 4
  193. A close_notify shutdown alert message is received from the peer.
  194. .PP
  195. These steps can occur in either order depending on whether the connection
  196. shutdown process was first initiated by the local application or by the peer.
  197. .SS "Locally-Initiated Shutdown"
  198. .IX Subsection "Locally-Initiated Shutdown"
  199. Calling \fBSSL_shutdown()\fR on a \s-1SSL/TLS\s0 or \s-1DTLS SSL\s0 object initiates the shutdown
  200. process and causes OpenSSL to try to send a close_notify shutdown alert to the
  201. peer. The shutdown process will then be considered completed once the peer
  202. responds in turn with a close_notify shutdown alert message.
  203. .PP
  204. Calling \fBSSL_shutdown()\fR only closes the write direction of the connection; the
  205. read direction is closed by the peer. Once \fBSSL_shutdown()\fR is called,
  206. \&\fBSSL_write\fR\|(3) can no longer be used, but \fBSSL_read\fR\|(3) may still be used
  207. until the peer decides to close the connection in turn. The peer might
  208. continue sending data for some period of time before handling the local
  209. application's shutdown indication.
  210. .PP
  211. \&\fBSSL_shutdown()\fR does not affect an underlying network connection such as a \s-1TCP\s0
  212. connection, which remains open.
  213. .SS "Remotely-Initiated Shutdown"
  214. .IX Subsection "Remotely-Initiated Shutdown"
  215. If the peer was the first to initiate the shutdown process by sending a
  216. close_notify alert message, an application will be notified of this as an \s-1EOF\s0
  217. condition when calling
  218. \&\fBSSL_read\fR\|(3) (i.e., \fBSSL_read\fR\|(3) will fail and \fBSSL_get_error\fR\|(3) will
  219. return \fB\s-1SSL_ERROR_ZERO_RETURN\s0\fR), after all application data sent by the peer
  220. prior to initiating the shutdown has been read. An application should handle
  221. this condition by calling \fBSSL_shutdown()\fR to respond with a close_notify alert in
  222. turn, completing the shutdown process, though it may choose to write additional
  223. application data using \fBSSL_write\fR\|(3) before doing so. If an application does
  224. not call \fBSSL_shutdown()\fR in this case, a close_notify alert will not be sent and
  225. the behaviour will not be fully standards compliant.
  226. .SS "Shutdown Lifecycle"
  227. .IX Subsection "Shutdown Lifecycle"
  228. Regardless of whether a shutdown was initiated locally or by the peer, if the
  229. underlying \s-1BIO\s0 is blocking, a call to \fBSSL_shutdown()\fR will return firstly once a
  230. close_notify alert message is written to the peer (returning 0), and upon a
  231. second and subsequent call, once a corresponding message is received from the
  232. peer (returning 1 and completing the shutdown process). Calls to \fBSSL_shutdown()\fR
  233. with a blocking underlying \s-1BIO\s0 will also return if an error occurs.
  234. .PP
  235. If the underlying \s-1BIO\s0 is nonblocking and the shutdown process is not yet
  236. complete (for example, because a close_notify alert message has not yet been
  237. received from the peer, or because a close_notify alert message needs to be sent
  238. but would currently block), \fBSSL_shutdown()\fR returns 0 to indicate that the
  239. shutdown process is still ongoing; in this case, a call to \fBSSL_get_error\fR\|(3)
  240. will yield \fB\s-1SSL_ERROR_WANT_READ\s0\fR or \fB\s-1SSL_ERROR_WANT_WRITE\s0\fR.
  241. .PP
  242. An application can then detect completion of the shutdown process by calling
  243. \&\fBSSL_shutdown()\fR again repeatedly until it returns 1, indicating that the shutdown
  244. process is complete (with a close_notify alert having both been sent and
  245. received).
  246. .PP
  247. However, the preferred method of waiting for the shutdown to complete is to use
  248. \&\fBSSL_read\fR\|(3) until \fBSSL_get_error\fR\|(3) indicates \s-1EOF\s0 by returning
  249. \&\fB\s-1SSL_ERROR_ZERO_RETURN\s0\fR. This ensures any data received immediately before the
  250. peer's close_notify alert is still provided to the application. It also ensures
  251. any final handshake-layer messages received are processed (for example, messages
  252. issuing new session tickets).
  253. .PP
  254. If this approach is not used, the second call to \fBSSL_shutdown()\fR (to complete the
  255. shutdown by confirming receipt of the peer's close_notify message) will fail if
  256. it is called when the application has not read all pending application data
  257. sent by the peer using \fBSSL_read\fR\|(3).
  258. .PP
  259. When calling \fBSSL_shutdown()\fR, the \fB\s-1SSL_SENT_SHUTDOWN\s0\fR flag is set once an
  260. attempt is made to send a close_notify alert, regardless of whether the attempt
  261. was successful. The \fB\s-1SSL_RECEIVED_SHUTDOWN\s0\fR flag is set once a close_notify
  262. alert is received, which may occur during any call which processes incoming data
  263. from the network, such as \fBSSL_read\fR\|(3) or \fBSSL_shutdown()\fR. These flags
  264. may be checked using \fBSSL_get_shutdown\fR\|(3).
  265. .SS "Fast Shutdown"
  266. .IX Subsection "Fast Shutdown"
  267. Alternatively, it is acceptable for an application to call \fBSSL_shutdown()\fR once
  268. (such that it returns 0) and then close the underlying connection without
  269. waiting for the peer's response. This allows for a more rapid shutdown process
  270. if the application does not wish to wait for the peer.
  271. .PP
  272. This alternative \*(L"fast shutdown\*(R" approach should only be done if it is known
  273. that the peer will not send more data, otherwise there is a risk of an
  274. application exposing itself to a truncation attack. The full \fBSSL_shutdown()\fR
  275. process, in which both parties send close_notify alerts and \fBSSL_shutdown()\fR
  276. returns 1, provides a cryptographically authenticated indication of the end of a
  277. connection.
  278. .PP
  279. This approach of a single \fBSSL_shutdown()\fR call without waiting is preferable to
  280. simply calling \fBSSL_free\fR\|(3) or \fBSSL_clear\fR\|(3) as calling \fBSSL_shutdown()\fR
  281. beforehand makes an \s-1SSL\s0 session eligible for subsequent reuse and notifies the
  282. peer of connection shutdown.
  283. .PP
  284. The fast shutdown approach can only be used if there is no intention to reuse
  285. the underlying connection (e.g. a \s-1TCP\s0 connection) for further communication; in
  286. this case, the full shutdown process must be performed to ensure
  287. synchronisation.
  288. .SS "Effects on Session Reuse"
  289. .IX Subsection "Effects on Session Reuse"
  290. Calling \fBSSL_shutdown()\fR sets the \s-1SSL_SENT_SHUTDOWN\s0 flag (see
  291. \&\fBSSL_set_shutdown\fR\|(3)), regardless of whether the transmission of the
  292. close_notify alert was successful or not. This makes the \s-1SSL\s0 session eligible
  293. for reuse; the \s-1SSL\s0 session is considered properly closed and can be reused for
  294. future connections.
  295. .SS "Quiet Shutdown"
  296. .IX Subsection "Quiet Shutdown"
  297. \&\fBSSL_shutdown()\fR can be modified to set the connection to the \*(L"shutdown\*(R"
  298. state without actually sending a close_notify alert message; see
  299. \&\fBSSL_CTX_set_quiet_shutdown\fR\|(3). When \*(L"quiet shutdown\*(R" is enabled,
  300. \&\fBSSL_shutdown()\fR will always succeed and return 1 immediately.
  301. .PP
  302. This is not standards-compliant behaviour. It should only be done when the
  303. application protocol in use enables the peer to ensure that all data has been
  304. received, such that it doesn't need to wait for a close_notify alert, otherwise
  305. application data may be truncated unexpectedly.
  306. .SS "Non-Compliant Peers"
  307. .IX Subsection "Non-Compliant Peers"
  308. There are \s-1SSL/TLS\s0 implementations that never send the required close_notify
  309. alert message but simply close the underlying transport (e.g. a \s-1TCP\s0 connection)
  310. instead. This will ordinarily result in an error being generated.
  311. .PP
  312. If compatibility with such peers is desired, the option
  313. \&\fB\s-1SSL_OP_IGNORE_UNEXPECTED_EOF\s0\fR can be set. For more information, see
  314. \&\fBSSL_CTX_set_options\fR\|(3).
  315. .PP
  316. Note that use of this option means that the \s-1EOF\s0 condition for application data
  317. does not receive cryptographic protection, and therefore renders an application
  318. potentially vulnerable to truncation attacks. Thus, this option must only be
  319. used in conjunction with an application protocol which indicates unambiguously
  320. when all data has been received.
  321. .PP
  322. An alternative approach is to simply avoid calling \fBSSL_read\fR\|(3) if it is known
  323. that no more data is going to be sent. This requires an application protocol
  324. which indicates unambiguously when all data has been sent.
  325. .SS "Session Ticket Handling"
  326. .IX Subsection "Session Ticket Handling"
  327. If a client application only writes to a \s-1SSL/TLS\s0 or \s-1DTLS\s0 connection and never
  328. reads, OpenSSL may never process new \s-1SSL/TLS\s0 session tickets sent by the server.
  329. This is because OpenSSL ordinarily processes handshake messages received from a
  330. peer during calls to \fBSSL_read\fR\|(3) by the application.
  331. .PP
  332. Therefore, client applications which only write and do not read but which wish
  333. to benefit from session resumption are advised to perform a complete shutdown
  334. procedure by calling \fBSSL_shutdown()\fR until it returns 1, as described above. This
  335. will ensure there is an opportunity for \s-1SSL/TLS\s0 session ticket messages to be
  336. received and processed by OpenSSL.
  337. .SH "QUIC-SPECIFIC SHUTDOWN CONSIDERATIONS"
  338. .IX Header "QUIC-SPECIFIC SHUTDOWN CONSIDERATIONS"
  339. When used with a \s-1QUIC\s0 connection \s-1SSL\s0 object, \fBSSL_shutdown()\fR initiates a \s-1QUIC\s0
  340. immediate close using \s-1QUIC\s0 \fB\s-1CONNECTION_CLOSE\s0\fR frames.
  341. .PP
  342. \&\fBSSL_shutdown()\fR cannot be used on \s-1QUIC\s0 stream \s-1SSL\s0 objects. To conclude a stream
  343. normally, see \fBSSL_stream_conclude\fR\|(3); to perform a non-normal stream
  344. termination, see \fBSSL_stream_reset\fR\|(3).
  345. .PP
  346. \&\fBSSL_shutdown_ex()\fR may be used instead of \fBSSL_shutdown()\fR by an application to
  347. provide additional information to the peer on the reason why a connection is
  348. being shut down. The information which can be provided is as follows:
  349. .IP "\fIquic_error_code\fR" 4
  350. .IX Item "quic_error_code"
  351. An optional 62\-bit application error code to be signalled to the peer. The value
  352. must be in the range [0, 2**62\-1], else the call to \fBSSL_shutdown_ex()\fR fails. If
  353. not provided, an error code of 0 is used by default.
  354. .IP "\fIquic_reason\fR" 4
  355. .IX Item "quic_reason"
  356. An optional zero-terminated (\s-1UTF\-8\s0) reason string to be signalled to the peer.
  357. The application is responsible for providing a valid \s-1UTF\-8\s0 string and OpenSSL
  358. will not validate the string. If a reason is not provided, or \fBSSL_shutdown()\fR is
  359. used, a zero-length string is used as the reason. If provided, the reason string
  360. is copied and stored inside the \s-1QUIC\s0 connection \s-1SSL\s0 object and need not remain
  361. allocated after the call to \fBSSL_shutdown_ex()\fR returns. Reason strings are
  362. bounded by the path \s-1MTU\s0 and may be silently truncated if they are too long to
  363. fit in a \s-1QUIC\s0 packet.
  364. .Sp
  365. Reason strings are intended for human diagnostic purposes only, and should not
  366. be used for application signalling.
  367. .PP
  368. The arguments to \fBSSL_shutdown_ex()\fR are used only on the first call to
  369. \&\fBSSL_shutdown_ex()\fR (or \fBSSL_shutdown()\fR) for a given \s-1QUIC\s0 connection \s-1SSL\s0 object.
  370. These arguments are ignored on subsequent calls.
  371. .PP
  372. These functions do not affect an underlying network \s-1BIO\s0 or the resource it
  373. represents; for example, a \s-1UDP\s0 datagram provided to a \s-1QUIC\s0 connection as the
  374. network \s-1BIO\s0 will remain open.
  375. .PP
  376. Note that when using \s-1QUIC,\s0 an application must call \fBSSL_shutdown()\fR if it wants
  377. to ensure that all transmitted data was received by the peer. This is unlike a
  378. \&\s-1TLS/TCP\s0 connection, where reliable transmission of buffered data is the
  379. responsibility of the operating system. If an application calls \fBSSL_free()\fR on a
  380. \&\s-1QUIC\s0 connection \s-1SSL\s0 object or exits before completing the shutdown process using
  381. \&\fBSSL_shutdown()\fR, data which was written by the application using \fBSSL_write()\fR, but
  382. could not yet be transmitted, or which was sent but lost in the network, may not
  383. be received by the peer.
  384. .PP
  385. When using \s-1QUIC,\s0 calling \fBSSL_shutdown()\fR allows internal network event processing
  386. to be performed. It is important that this processing is performed regularly,
  387. whether during connection usage or during shutdown. If an application is not
  388. using thread assisted mode, an application conducting shutdown should either
  389. ensure that \fBSSL_shutdown()\fR is called regularly, or alternatively ensure that
  390. \&\fBSSL_handle_events()\fR is called regularly. See \fBopenssl\-quic\fR\|(7) and
  391. \&\fBSSL_handle_events\fR\|(3) for more information.
  392. .SS "Application Data Drainage Behaviour"
  393. .IX Subsection "Application Data Drainage Behaviour"
  394. When using \s-1QUIC,\s0 \fBSSL_shutdown()\fR or \fBSSL_shutdown_ex()\fR ordinarily waits until all
  395. data written to a stream by an application has been acknowledged by the peer. In
  396. other words, the shutdown process waits until all data written by the
  397. application has been sent to the peer, and until the receipt of all such data is
  398. acknowledged by the peer. Only once this process is completed is the shutdown
  399. considered complete.
  400. .PP
  401. An exception to this is streams which terminated in a non-normal fashion, for
  402. example due to a stream reset; only streams which are non-terminated at the time
  403. \&\fBSSL_shutdown()\fR is called, or which terminated in a normal fashion, have their
  404. pending send buffers flushed in this manner.
  405. .PP
  406. This behaviour of flushing streams during the shutdown process can be skipped by
  407. setting the \fB\s-1SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH\s0\fR flag in a call to
  408. \&\fBSSL_shutdown_ex()\fR; in this case, data remaining in stream send buffers may not
  409. be transmitted to the peer. This flag may be used when a non-normal application
  410. condition has occurred and the delivery of data written to streams via
  411. \&\fBSSL_write\fR\|(3) is no longer relevant.
  412. .SS "Shutdown Mode"
  413. .IX Subsection "Shutdown Mode"
  414. Aspects of how \s-1QUIC\s0 handles connection closure must be taken into account by
  415. applications. Ordinarily, \s-1QUIC\s0 expects a connection to continue to be serviced
  416. for a substantial period of time after it is nominally closed. This is necessary
  417. to ensure that any connection closure notification sent to the peer was
  418. successfully received. However, a consequence of this is that a fully
  419. RFC-compliant \s-1QUIC\s0 connection closure process could take of the order of
  420. seconds. This may be unsuitable for some applications, such as short-lived
  421. processes which need to exit immediately after completing an application-layer
  422. transaction.
  423. .PP
  424. As such, there are two shutdown modes available to users of \s-1QUIC\s0 connection \s-1SSL\s0
  425. objects:
  426. .IP "\s-1RFC\s0 compliant shutdown mode" 4
  427. .IX Item "RFC compliant shutdown mode"
  428. This is the default behaviour. The shutdown process may take a period of time up
  429. to three times the current estimated \s-1RTT\s0 to the peer. It is possible for the
  430. closure process to complete much faster in some circumstances but this cannot be
  431. relied upon.
  432. .Sp
  433. In blocking mode, the function will return once the closure process is complete.
  434. In nonblocking mode, \fBSSL_shutdown_ex()\fR should be called until it returns 1,
  435. indicating the closure process is complete and the connection is now fully shut
  436. down.
  437. .IP "Rapid shutdown mode" 4
  438. .IX Item "Rapid shutdown mode"
  439. In this mode, the peer is notified of connection closure on a best effort basis
  440. by sending a single \s-1QUIC\s0 packet. If that \s-1QUIC\s0 packet is lost, the peer will not
  441. know that the connection has terminated until the negotiated idle timeout (if
  442. any) expires.
  443. .Sp
  444. This will generally return 0 on success, indicating that the connection has not
  445. yet been fully shut down (unless it has already done so, in which case it will
  446. return 1).
  447. .PP
  448. If \fB\s-1SSL_SHUTDOWN_FLAG_RAPID\s0\fR is specified in \fIflags\fR, a rapid shutdown is
  449. performed, otherwise an RFC-compliant shutdown is performed.
  450. .PP
  451. If an application calls \fBSSL_shutdown_ex()\fR with \fB\s-1SSL_SHUTDOWN_FLAG_RAPID\s0\fR, an
  452. application can subsequently change its mind about performing a rapid shutdown
  453. by making a subsequent call to \fBSSL_shutdown_ex()\fR without the flag set.
  454. .SS "Peer-Initiated Shutdown"
  455. .IX Subsection "Peer-Initiated Shutdown"
  456. In some cases, an application may wish to wait for a shutdown initiated by the
  457. peer rather than triggered locally. To do this, call \fBSSL_shutdown_ex()\fR with
  458. \&\fI\s-1SSL_SHUTDOWN_FLAG_WAIT_PEER\s0\fR specified in \fIflags\fR. In blocking mode, this
  459. waits until the peer initiates a shutdown or the connection otherwise becomes
  460. terminated for another reason. In nonblocking mode it exits immediately with
  461. either success or failure depending on whether a shutdown has occurred.
  462. .PP
  463. If a locally initiated shutdown has already been triggered or the connection has
  464. started terminating for another reason, this flag has no effect.
  465. .PP
  466. \&\fB\s-1SSL_SHUTDOWN_FLAG_WAIT_PEER\s0\fR implies \fB\s-1SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH\s0\fR, as
  467. stream data cannot be flushed after a peer closes the connection. Stream data
  468. may still be sent to the peer in any time spent waiting before the peer closes
  469. the connection, though there is no guarantee of this.
  470. .SS "Nonblocking Mode"
  471. .IX Subsection "Nonblocking Mode"
  472. \&\fBSSL_shutdown()\fR and \fBSSL_shutdown_ex()\fR block if the connection is configured in
  473. blocking mode. This may be overridden by specifying
  474. \&\fB\s-1SSL_SHUTDOWN_FLAG_NO_BLOCK\s0\fR in \fIflags\fR when calling \fBSSL_shutdown_ex()\fR, which
  475. causes the call to operate as though in nonblocking mode.
  476. .SH "RETURN VALUES"
  477. .IX Header "RETURN VALUES"
  478. For both \fBSSL_shutdown()\fR and \fBSSL_shutdown_ex()\fR the following return values can occur:
  479. .IP "0" 4
  480. The shutdown process is ongoing and has not yet completed.
  481. .Sp
  482. For \s-1TLS\s0 and \s-1DTLS,\s0 this means that a close_notify alert has been sent but the
  483. peer has not yet replied in turn with its own close_notify.
  484. .Sp
  485. For \s-1QUIC\s0 connection \s-1SSL\s0 objects, a \s-1CONNECTION_CLOSE\s0 frame may have been
  486. sent but the connection closure process has not yet completed.
  487. .Sp
  488. Unlike most other functions, returning 0 does not indicate an error.
  489. \&\fBSSL_get_error\fR\|(3) should not be called; it may misleadingly indicate an error
  490. even though no error occurred.
  491. .IP "1" 4
  492. .IX Item "1"
  493. The shutdown was successfully completed.
  494. .Sp
  495. For \s-1TLS\s0 and \s-1DTLS,\s0 this means that a close_notify alert was sent and the peer's
  496. close_notify alert was received.
  497. .Sp
  498. For \s-1QUIC\s0 connection \s-1SSL\s0 objects, this means that the connection closure process
  499. has completed.
  500. .IP "<0" 4
  501. .IX Item "<0"
  502. The shutdown was not successful.
  503. Call \fBSSL_get_error\fR\|(3) with the return value \fBret\fR to find out the reason.
  504. It can occur if an action is needed to continue the operation for nonblocking
  505. BIOs.
  506. .Sp
  507. It can also occur when not all data was read using \fBSSL_read()\fR, or if called
  508. on a \s-1QUIC\s0 stream \s-1SSL\s0 object.
  509. .Sp
  510. This value is also returned when called on \s-1QUIC\s0 stream \s-1SSL\s0 objects.
  511. .SH "SEE ALSO"
  512. .IX Header "SEE ALSO"
  513. \&\fBSSL_get_error\fR\|(3), \fBSSL_connect\fR\|(3),
  514. \&\fBSSL_accept\fR\|(3), \fBSSL_set_shutdown\fR\|(3),
  515. \&\fBSSL_CTX_set_quiet_shutdown\fR\|(3), \fBSSL_CTX_set_options\fR\|(3)
  516. \&\fBSSL_clear\fR\|(3), \fBSSL_free\fR\|(3),
  517. \&\fBssl\fR\|(7), \fBbio\fR\|(7)
  518. .SH "HISTORY"
  519. .IX Header "HISTORY"
  520. The \fBSSL_shutdown_ex()\fR function was added in OpenSSL 3.2.
  521. .SH "COPYRIGHT"
  522. .IX Header "COPYRIGHT"
  523. Copyright 2000\-2023 The OpenSSL Project Authors. All Rights Reserved.
  524. .PP
  525. Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
  526. this file except in compliance with the License. You can obtain a copy
  527. in the file \s-1LICENSE\s0 in the source distribution or at
  528. <https://www.openssl.org/source/license.html>.