Home | History | Annotate | Line # | Download | only in bio
      1 /*
      2  * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 /*
     11  * A minimal TLS server it ses SSL_CTX_config and a configuration file to
     12  * set most server parameters.
     13  */
     14 
     15 #include <stdio.h>
     16 #include <signal.h>
     17 #include <stdlib.h>
     18 #include <openssl/err.h>
     19 #include <openssl/ssl.h>
     20 #include <openssl/conf.h>
     21 
     22 int main(int argc, char *argv[])
     23 {
     24     unsigned char buf[512];
     25     char *port = "*:4433";
     26     BIO *in = NULL;
     27     BIO *ssl_bio = NULL;
     28     BIO *tmp;
     29     SSL_CTX *ctx;
     30     int ret = EXIT_FAILURE, i;
     31 
     32     ctx = SSL_CTX_new(TLS_server_method());
     33 
     34     if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {
     35         fprintf(stderr, "Error processing config file\n");
     36         goto err;
     37     }
     38 
     39     if (SSL_CTX_config(ctx, "server") == 0) {
     40         fprintf(stderr, "Error configuring server.\n");
     41         goto err;
     42     }
     43 
     44     /* Setup server side SSL bio */
     45     ssl_bio = BIO_new_ssl(ctx, 0);
     46 
     47     if ((in = BIO_new_accept(port)) == NULL)
     48         goto err;
     49 
     50     /*
     51      * This means that when a new connection is accepted on 'in', The ssl_bio
     52      * will be 'duplicated' and have the new socket BIO push into it.
     53      * Basically it means the SSL BIO will be automatically setup
     54      */
     55     BIO_set_accept_bios(in, ssl_bio);
     56     ssl_bio = NULL;
     57 
     58 again:
     59     /*
     60      * The first call will setup the accept socket, and the second will get a
     61      * socket.  In this loop, the first actual accept will occur in the
     62      * BIO_read() function.
     63      */
     64 
     65     if (BIO_do_accept(in) <= 0)
     66         goto err;
     67 
     68     for (;;) {
     69         i = BIO_read(in, buf, sizeof(buf));
     70         if (i == 0) {
     71             /*
     72              * If we have finished, remove the underlying BIO stack so the
     73              * next time we call any function for this BIO, it will attempt
     74              * to do an accept
     75              */
     76             printf("Done\n");
     77             tmp = BIO_pop(in);
     78             BIO_free_all(tmp);
     79             goto again;
     80         }
     81         if (i < 0) {
     82             if (BIO_should_retry(in))
     83                 continue;
     84             goto err;
     85         }
     86         fwrite(buf, 1, i, stdout);
     87         fflush(stdout);
     88     }
     89 
     90     ret = EXIT_SUCCESS;
     91 err:
     92     if (ret != EXIT_SUCCESS)
     93         ERR_print_errors_fp(stderr);
     94     BIO_free(in);
     95     BIO_free_all(ssl_bio);
     96     return ret;
     97 }
     98