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