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