BIO_f_ssl.pod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. =pod
  2. =head1 NAME
  3. BIO_do_handshake,
  4. BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode,
  5. BIO_set_ssl_renegotiate_bytes,
  6. BIO_get_num_renegotiates, BIO_set_ssl_renegotiate_timeout, BIO_new_ssl,
  7. BIO_new_ssl_connect, BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id,
  8. BIO_ssl_shutdown - SSL BIO
  9. =head1 SYNOPSIS
  10. =for openssl multiple includes
  11. #include <openssl/bio.h>
  12. #include <openssl/ssl.h>
  13. const BIO_METHOD *BIO_f_ssl(void);
  14. long BIO_set_ssl(BIO *b, SSL *ssl, long c);
  15. long BIO_get_ssl(BIO *b, SSL **sslp);
  16. long BIO_set_ssl_mode(BIO *b, long client);
  17. long BIO_set_ssl_renegotiate_bytes(BIO *b, long num);
  18. long BIO_set_ssl_renegotiate_timeout(BIO *b, long seconds);
  19. long BIO_get_num_renegotiates(BIO *b);
  20. BIO *BIO_new_ssl(SSL_CTX *ctx, int client);
  21. BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
  22. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
  23. int BIO_ssl_copy_session_id(BIO *to, BIO *from);
  24. void BIO_ssl_shutdown(BIO *bio);
  25. long BIO_do_handshake(BIO *b);
  26. =head1 DESCRIPTION
  27. BIO_f_ssl() returns the SSL BIO method. This is a filter BIO which
  28. is a wrapper round the OpenSSL SSL routines adding a BIO "flavour" to
  29. SSL I/O.
  30. I/O performed on an SSL BIO communicates using the SSL protocol with
  31. the SSLs read and write BIOs. If an SSL connection is not established
  32. then an attempt is made to establish one on the first I/O call.
  33. If a BIO is appended to an SSL BIO using BIO_push() it is automatically
  34. used as the SSL BIOs read and write BIOs.
  35. Calling BIO_reset() on an SSL BIO closes down any current SSL connection
  36. by calling SSL_shutdown(). BIO_reset() is then sent to the next BIO in
  37. the chain: this will typically disconnect the underlying transport.
  38. The SSL BIO is then reset to the initial accept or connect state.
  39. If the close flag is set when an SSL BIO is freed then the internal
  40. SSL structure is also freed using SSL_free().
  41. BIO_set_ssl() sets the internal SSL pointer of SSL BIO B<b> to B<ssl> using
  42. the close flag B<c>.
  43. BIO_get_ssl() retrieves the SSL pointer of SSL BIO B<b>, it can then be
  44. manipulated using the standard SSL library functions.
  45. BIO_set_ssl_mode() sets the SSL BIO mode to B<client>. If B<client>
  46. is 1 client mode is set. If B<client> is 0 server mode is set.
  47. BIO_set_ssl_renegotiate_bytes() sets the renegotiate byte count of SSL BIO B<b>
  48. to B<num>. When set after every B<num> bytes of I/O (read and write)
  49. the SSL session is automatically renegotiated. B<num> must be at
  50. least 512 bytes.
  51. BIO_set_ssl_renegotiate_timeout() sets the renegotiate timeout of SSL BIO B<b>
  52. to B<seconds>.
  53. When the renegotiate timeout elapses the session is automatically renegotiated.
  54. BIO_get_num_renegotiates() returns the total number of session
  55. renegotiations due to I/O or timeout of SSL BIO B<b>.
  56. BIO_new_ssl() allocates an SSL BIO using SSL_CTX B<ctx> and using
  57. client mode if B<client> is non zero.
  58. BIO_new_ssl_connect() creates a new BIO chain consisting of an
  59. SSL BIO (using B<ctx>) followed by a connect BIO.
  60. BIO_new_buffer_ssl_connect() creates a new BIO chain consisting
  61. of a buffering BIO, an SSL BIO (using B<ctx>), and a connect BIO.
  62. BIO_ssl_copy_session_id() copies an SSL session id between
  63. BIO chains B<from> and B<to>. It does this by locating the
  64. SSL BIOs in each chain and calling SSL_copy_session_id() on
  65. the internal SSL pointer.
  66. BIO_ssl_shutdown() closes down an SSL connection on BIO
  67. chain B<bio>. It does this by locating the SSL BIO in the
  68. chain and calling SSL_shutdown() on its internal SSL
  69. pointer.
  70. BIO_do_handshake() attempts to complete an SSL handshake on the
  71. supplied BIO and establish the SSL connection.
  72. For non-SSL BIOs the connection is done typically at TCP level.
  73. If domain name resolution yields multiple IP addresses all of them are tried
  74. after connect() failures.
  75. The function returns 1 if the connection was established successfully.
  76. A zero or negative value is returned if the connection could not be established.
  77. The call BIO_should_retry() should be used for nonblocking connect BIOs
  78. to determine if the call should be retried.
  79. If a connection has already been established this call has no effect.
  80. =head1 NOTES
  81. SSL BIOs are exceptional in that if the underlying transport
  82. is non blocking they can still request a retry in exceptional
  83. circumstances. Specifically this will happen if a session
  84. renegotiation takes place during a BIO_read_ex() operation, one
  85. case where this happens is when step up occurs.
  86. The SSL flag SSL_AUTO_RETRY can be
  87. set to disable this behaviour. That is when this flag is set
  88. an SSL BIO using a blocking transport will never request a
  89. retry.
  90. Since unknown BIO_ctrl() operations are sent through filter
  91. BIOs the servers name and port can be set using BIO_set_host()
  92. on the BIO returned by BIO_new_ssl_connect() without having
  93. to locate the connect BIO first.
  94. Applications do not have to call BIO_do_handshake() but may wish
  95. to do so to separate the handshake process from other I/O
  96. processing.
  97. BIO_set_ssl(), BIO_get_ssl(), BIO_set_ssl_mode(),
  98. BIO_set_ssl_renegotiate_bytes(), BIO_set_ssl_renegotiate_timeout(),
  99. BIO_get_num_renegotiates(), and BIO_do_handshake() are implemented as macros.
  100. BIO_ssl_copy_session_id() is not currently supported on QUIC SSL objects and
  101. fails if called on such an object.
  102. =head1 RETURN VALUES
  103. BIO_f_ssl() returns the SSL B<BIO_METHOD> structure.
  104. BIO_set_ssl(), BIO_get_ssl(), BIO_set_ssl_mode(), BIO_set_ssl_renegotiate_bytes(),
  105. BIO_set_ssl_renegotiate_timeout() and BIO_get_num_renegotiates() return 1 on
  106. success or a value which is less than or equal to 0 if an error occurred.
  107. BIO_new_ssl(), BIO_new_ssl_connect() and BIO_new_buffer_ssl_connect() return
  108. a valid B<BIO> structure on success or B<NULL> if an error occurred.
  109. BIO_ssl_copy_session_id() returns 1 on success or 0 on error, or if called
  110. on a QUIC SSL object.
  111. BIO_do_handshake() returns 1 if the connection was established successfully.
  112. A zero or negative value is returned if the connection could not be established.
  113. =head1 EXAMPLES
  114. This SSL/TLS client example attempts to retrieve a page from an
  115. SSL/TLS web server. The I/O routines are identical to those of the
  116. unencrypted example in L<BIO_s_connect(3)>.
  117. BIO *sbio, *out;
  118. int len;
  119. char tmpbuf[1024];
  120. SSL_CTX *ctx;
  121. SSL *ssl;
  122. /* XXX Seed the PRNG if needed. */
  123. ctx = SSL_CTX_new(TLS_client_method());
  124. /* XXX Set verify paths and mode here. */
  125. sbio = BIO_new_ssl_connect(ctx);
  126. BIO_get_ssl(sbio, &ssl);
  127. if (ssl == NULL) {
  128. fprintf(stderr, "Can't locate SSL pointer\n");
  129. ERR_print_errors_fp(stderr);
  130. exit(1);
  131. }
  132. /* XXX We might want to do other things with ssl here */
  133. /* An empty host part means the loopback address */
  134. BIO_set_conn_hostname(sbio, ":https");
  135. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  136. if (BIO_do_connect(sbio) <= 0) {
  137. fprintf(stderr, "Error connecting to server\n");
  138. ERR_print_errors_fp(stderr);
  139. exit(1);
  140. }
  141. /* XXX Could examine ssl here to get connection info */
  142. BIO_puts(sbio, "GET / HTTP/1.0\n\n");
  143. for (;;) {
  144. len = BIO_read(sbio, tmpbuf, 1024);
  145. if (len <= 0)
  146. break;
  147. BIO_write(out, tmpbuf, len);
  148. }
  149. BIO_free_all(sbio);
  150. BIO_free(out);
  151. Here is a simple server example. It makes use of a buffering
  152. BIO to allow lines to be read from the SSL BIO using BIO_gets.
  153. It creates a pseudo web page containing the actual request from
  154. a client and also echoes the request to standard output.
  155. BIO *sbio, *bbio, *acpt, *out;
  156. int len;
  157. char tmpbuf[1024];
  158. SSL_CTX *ctx;
  159. SSL *ssl;
  160. /* XXX Seed the PRNG if needed. */
  161. ctx = SSL_CTX_new(TLS_server_method());
  162. if (!SSL_CTX_use_certificate_file(ctx, "server.pem", SSL_FILETYPE_PEM)
  163. || !SSL_CTX_use_PrivateKey_file(ctx, "server.pem", SSL_FILETYPE_PEM)
  164. || !SSL_CTX_check_private_key(ctx)) {
  165. fprintf(stderr, "Error setting up SSL_CTX\n");
  166. ERR_print_errors_fp(stderr);
  167. exit(1);
  168. }
  169. /* XXX Other things like set verify locations, EDH temp callbacks. */
  170. /* New SSL BIO setup as server */
  171. sbio = BIO_new_ssl(ctx, 0);
  172. BIO_get_ssl(sbio, &ssl);
  173. if (ssl == NULL) {
  174. fprintf(stderr, "Can't locate SSL pointer\n");
  175. ERR_print_errors_fp(stderr);
  176. exit(1);
  177. }
  178. bbio = BIO_new(BIO_f_buffer());
  179. sbio = BIO_push(bbio, sbio);
  180. acpt = BIO_new_accept("4433");
  181. /*
  182. * By doing this when a new connection is established
  183. * we automatically have sbio inserted into it. The
  184. * BIO chain is now 'swallowed' by the accept BIO and
  185. * will be freed when the accept BIO is freed.
  186. */
  187. BIO_set_accept_bios(acpt, sbio);
  188. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  189. /* First call to BIO_do_accept() sets up accept BIO */
  190. if (BIO_do_accept(acpt) <= 0) {
  191. fprintf(stderr, "Error setting up accept BIO\n");
  192. ERR_print_errors_fp(stderr);
  193. exit(1);
  194. }
  195. /* Second call to BIO_do_accept() waits for incoming connection */
  196. if (BIO_do_accept(acpt) <= 0) {
  197. fprintf(stderr, "Error accepting connection\n");
  198. ERR_print_errors_fp(stderr);
  199. exit(1);
  200. }
  201. /* We only want one connection so remove and free accept BIO */
  202. sbio = BIO_pop(acpt);
  203. BIO_free_all(acpt);
  204. if (BIO_do_handshake(sbio) <= 0) {
  205. fprintf(stderr, "Error in SSL handshake\n");
  206. ERR_print_errors_fp(stderr);
  207. exit(1);
  208. }
  209. BIO_puts(sbio, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n");
  210. BIO_puts(sbio, "\r\nConnection Established\r\nRequest headers:\r\n");
  211. BIO_puts(sbio, "--------------------------------------------------\r\n");
  212. for (;;) {
  213. len = BIO_gets(sbio, tmpbuf, 1024);
  214. if (len <= 0)
  215. break;
  216. BIO_write(sbio, tmpbuf, len);
  217. BIO_write(out, tmpbuf, len);
  218. /* Look for blank line signifying end of headers*/
  219. if (tmpbuf[0] == '\r' || tmpbuf[0] == '\n')
  220. break;
  221. }
  222. BIO_puts(sbio, "--------------------------------------------------\r\n");
  223. BIO_puts(sbio, "\r\n");
  224. BIO_flush(sbio);
  225. BIO_free_all(sbio);
  226. =head1 HISTORY
  227. In OpenSSL before 1.0.0 the BIO_pop() call was handled incorrectly,
  228. the I/O BIO reference count was incorrectly incremented (instead of
  229. decremented) and dissociated with the SSL BIO even if the SSL BIO was not
  230. explicitly being popped (e.g. a pop higher up the chain). Applications which
  231. included workarounds for this bug (e.g. freeing BIOs more than once) should
  232. be modified to handle this fix or they may free up an already freed BIO.
  233. =head1 COPYRIGHT
  234. Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
  235. Licensed under the Apache License 2.0 (the "License"). You may not use
  236. this file except in compliance with the License. You can obtain a copy
  237. in the file LICENSE in the source distribution or at
  238. L<https://www.openssl.org/source/license.html>.
  239. =cut