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