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