SSL_write.3ossl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "SSL_WRITE 3ossl"
  58. .TH SSL_WRITE 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. SSL_write_ex2, SSL_write_ex, SSL_write, SSL_sendfile, SSL_WRITE_FLAG_CONCLUDE \-
  65. write bytes to a TLS/SSL connection
  66. .SH SYNOPSIS
  67. .IX Header "SYNOPSIS"
  68. .Vb 1
  69. \& #include <openssl/ssl.h>
  70. \&
  71. \& #define SSL_WRITE_FLAG_CONCLUDE
  72. \&
  73. \& ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags);
  74. \& int SSL_write_ex2(SSL *s, const void *buf, size_t num,
  75. \& uint64_t flags,
  76. \& size_t *written);
  77. \& int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written);
  78. \& int SSL_write(SSL *ssl, const void *buf, int num);
  79. .Ve
  80. .SH DESCRIPTION
  81. .IX Header "DESCRIPTION"
  82. \&\fBSSL_write_ex()\fR and \fBSSL_write()\fR write \fBnum\fR bytes from the buffer \fBbuf\fR into
  83. the specified \fBssl\fR connection. On success \fBSSL_write_ex()\fR will store the number
  84. of bytes written in \fB*written\fR.
  85. .PP
  86. \&\fBSSL_write_ex2()\fR functions similarly to \fBSSL_write_ex()\fR but can also accept
  87. optional flags which modify its behaviour. Calling \fBSSL_write_ex2()\fR with a
  88. \&\fIflags\fR argument of 0 is exactly equivalent to calling \fBSSL_write_ex()\fR.
  89. .PP
  90. \&\fBSSL_sendfile()\fR writes \fBsize\fR bytes from offset \fBoffset\fR in the file
  91. descriptor \fBfd\fR to the specified SSL connection \fBs\fR. This function provides
  92. efficient zero-copy semantics. \fBSSL_sendfile()\fR is available only when
  93. Kernel TLS is enabled, which can be checked by calling \fBBIO_get_ktls_send()\fR.
  94. It is provided here to allow users to maintain the same interface.
  95. The meaning of \fBflags\fR is platform dependent.
  96. Currently, under Linux it is ignored.
  97. .PP
  98. The \fIflags\fR argument to \fBSSL_write_ex2()\fR can accept zero or more of the
  99. following flags. Note that which flags are supported will depend on the kind of
  100. SSL object and underlying protocol being used:
  101. .IP \fBSSL_WRITE_FLAG_CONCLUDE\fR 4
  102. .IX Item "SSL_WRITE_FLAG_CONCLUDE"
  103. This flag is only supported on QUIC stream SSL objects (or QUIC connection SSL
  104. objects with a default stream attached).
  105. .Sp
  106. If this flag is set, and the call to \fBSSL_write_ex2()\fR succeeds, and all of the
  107. data passed to the call is written (meaning that \f(CW\*(C`*written == num\*(C'\fR), the
  108. relevant QUIC stream's send part is concluded automatically as though
  109. \&\fBSSL_stream_conclude\fR\|(3) was called (causing transmission of a FIN for the
  110. stream).
  111. .Sp
  112. While using this flag is semantically equivalent to calling
  113. \&\fBSSL_stream_conclude\fR\|(3) after a successful call to this function, using this
  114. flag enables greater efficiency than making these two API calls separately, as
  115. it enables the written stream data and the FIN flag indicating the end of the
  116. stream to be scheduled as part of the same QUIC STREAM frame and QUIC packet.
  117. .Sp
  118. Setting this flag does not cause a stream's send part to be concluded if not all
  119. of the data passed to the call was consumed.
  120. .PP
  121. A call to \fBSSL_write_ex2()\fR fails if a flag is passed which is not supported or
  122. understood by the given SSL object. An application should determine if a flag is
  123. supported (for example, for \fBSSL_WRITE_FLAG_CONCLUDE\fR, that a QUIC stream SSL
  124. object is being used) before attempting to use it.
  125. .SH NOTES
  126. .IX Header "NOTES"
  127. In the paragraphs below a "write function" is defined as one of either
  128. \&\fBSSL_write_ex()\fR, or \fBSSL_write()\fR.
  129. .PP
  130. If necessary, a write function will negotiate a TLS/SSL session, if not already
  131. explicitly performed by \fBSSL_connect\fR\|(3) or \fBSSL_accept\fR\|(3). If the peer
  132. requests a re-negotiation, it will be performed transparently during
  133. the write function operation. The behaviour of the write functions depends on the
  134. underlying BIO.
  135. .PP
  136. For the transparent negotiation to succeed, the \fBssl\fR must have been
  137. initialized to client or server mode. This is being done by calling
  138. \&\fBSSL_set_connect_state\fR\|(3) or \fBSSL_set_accept_state()\fR
  139. before the first call to a write function.
  140. .PP
  141. If the underlying BIO is \fBblocking\fR, the write functions will only return, once
  142. the write operation has been finished or an error occurred.
  143. .PP
  144. If the underlying BIO is \fBnonblocking\fR the write functions will also return
  145. when the underlying BIO could not satisfy the needs of the function to continue
  146. the operation. In this case a call to \fBSSL_get_error\fR\|(3) with the
  147. return value of the write function will yield \fBSSL_ERROR_WANT_READ\fR
  148. or \fBSSL_ERROR_WANT_WRITE\fR. As at any time a re-negotiation is possible, a
  149. call to a write function can also cause read operations! The calling process
  150. then must repeat the call after taking appropriate action to satisfy the needs
  151. of the write function. The action depends on the underlying BIO. When using a
  152. nonblocking socket, nothing is to be done, but \fBselect()\fR can be used to check
  153. for the required condition. When using a buffering BIO, like a BIO pair, data
  154. must be written into or retrieved out of the BIO before being able to continue.
  155. .PP
  156. The write functions will only return with success when the complete contents of
  157. \&\fBbuf\fR of length \fBnum\fR has been written. This default behaviour can be changed
  158. with the SSL_MODE_ENABLE_PARTIAL_WRITE option of \fBSSL_CTX_set_mode\fR\|(3). When
  159. this flag is set the write functions will also return with success when a
  160. partial write has been successfully completed. In this case the write function
  161. operation is considered completed. The bytes are sent and a new write call with
  162. a new buffer (with the already sent bytes removed) must be started. A partial
  163. write is performed with the size of a message block, which is 16kB.
  164. .PP
  165. When used with a QUIC SSL object, calling an I/O function such as \fBSSL_write()\fR
  166. allows internal network event processing to be performed. It is important that
  167. this processing is performed regularly. If an application is not using thread
  168. assisted mode, an application should ensure that an I/O function such as
  169. \&\fBSSL_write()\fR is called regularly, or alternatively ensure that \fBSSL_handle_events()\fR
  170. is called regularly. See \fBopenssl\-quic\fR\|(7) and \fBSSL_handle_events\fR\|(3) for more
  171. information.
  172. .SH WARNINGS
  173. .IX Header "WARNINGS"
  174. When a write function call has to be repeated because \fBSSL_get_error\fR\|(3)
  175. returned \fBSSL_ERROR_WANT_READ\fR or \fBSSL_ERROR_WANT_WRITE\fR, it must be repeated
  176. with the same arguments.
  177. The data that was passed might have been partially processed.
  178. When \fBSSL_MODE_ACCEPT_MOVING_WRITE_BUFFER\fR was set using \fBSSL_CTX_set_mode\fR\|(3)
  179. the pointer can be different, but the data and length should still be the same.
  180. .PP
  181. You should not call \fBSSL_write()\fR with num=0, it will return an error.
  182. \&\fBSSL_write_ex()\fR can be called with num=0, but will not send application data to
  183. the peer.
  184. .SH "RETURN VALUES"
  185. .IX Header "RETURN VALUES"
  186. \&\fBSSL_write_ex()\fR and \fBSSL_write_ex2()\fR return 1 for success or 0 for failure.
  187. Success means that all requested application data bytes have been written to the
  188. SSL connection or, if SSL_MODE_ENABLE_PARTIAL_WRITE is in use, at least 1
  189. application data byte has been written to the SSL connection. Failure means that
  190. not all the requested bytes have been written yet (if
  191. SSL_MODE_ENABLE_PARTIAL_WRITE is not in use) or no bytes could be written to the
  192. SSL connection (if SSL_MODE_ENABLE_PARTIAL_WRITE is in use). Failures can be
  193. retryable (e.g. the network write buffer has temporarily filled up) or
  194. non-retryable (e.g. a fatal network error). In the event of a failure call
  195. \&\fBSSL_get_error\fR\|(3) to find out the reason which indicates whether the call is
  196. retryable or not.
  197. .PP
  198. For \fBSSL_write()\fR the following return values can occur:
  199. .IP "> 0" 4
  200. .IX Item "> 0"
  201. The write operation was successful, the return value is the number of
  202. bytes actually written to the TLS/SSL connection.
  203. .IP "<= 0" 4
  204. .IX Item "<= 0"
  205. The write operation was not successful, because either the connection was
  206. closed, an error occurred or action must be taken by the calling process.
  207. Call \fBSSL_get_error()\fR with the return value \fBret\fR to find out the reason.
  208. .Sp
  209. Old documentation indicated a difference between 0 and \-1, and that \-1 was
  210. retryable.
  211. You should instead call \fBSSL_get_error()\fR to find out if it's retryable.
  212. .PP
  213. For \fBSSL_sendfile()\fR, the following return values can occur:
  214. .IP ">= 0" 4
  215. .IX Item ">= 0"
  216. The write operation was successful, the return value is the number
  217. of bytes of the file written to the TLS/SSL connection. The return
  218. value can be less than \fBsize\fR for a partial write.
  219. .IP "< 0" 4
  220. .IX Item "< 0"
  221. The write operation was not successful, because either the connection was
  222. closed, an error occurred or action must be taken by the calling process.
  223. Call \fBSSL_get_error()\fR with the return value to find out the reason.
  224. .SH "SEE ALSO"
  225. .IX Header "SEE ALSO"
  226. \&\fBSSL_get_error\fR\|(3), \fBSSL_read_ex\fR\|(3), \fBSSL_read\fR\|(3)
  227. \&\fBSSL_CTX_set_mode\fR\|(3), \fBSSL_CTX_new\fR\|(3),
  228. \&\fBSSL_connect\fR\|(3), \fBSSL_accept\fR\|(3)
  229. \&\fBSSL_set_connect_state\fR\|(3), \fBBIO_ctrl\fR\|(3),
  230. \&\fBssl\fR\|(7), \fBbio\fR\|(7)
  231. .SH HISTORY
  232. .IX Header "HISTORY"
  233. The \fBSSL_write_ex()\fR function was added in OpenSSL 1.1.1.
  234. The \fBSSL_sendfile()\fR function was added in OpenSSL 3.0.
  235. .SH COPYRIGHT
  236. .IX Header "COPYRIGHT"
  237. Copyright 2000\-2024 The OpenSSL Project Authors. All Rights Reserved.
  238. .PP
  239. Licensed under the Apache License 2.0 (the "License"). You may not use
  240. this file except in compliance with the License. You can obtain a copy
  241. in the file LICENSE in the source distribution or at
  242. <https://www.openssl.org/source/license.html>.