1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2013-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 program to serve an SSL connection. It uses blocking. It uses
12*e0c4386eSCy Schubert * the SSL_CONF API with a configuration file. cc -I../../include saccept.c
13*e0c4386eSCy Schubert * -L../.. -lssl -lcrypto -ldl
14*e0c4386eSCy Schubert */
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <string.h>
18*e0c4386eSCy Schubert #include <signal.h>
19*e0c4386eSCy Schubert #include <stdlib.h>
20*e0c4386eSCy Schubert #include <openssl/err.h>
21*e0c4386eSCy Schubert #include <openssl/ssl.h>
22*e0c4386eSCy Schubert #include <openssl/conf.h>
23*e0c4386eSCy Schubert
main(int argc,char * argv[])24*e0c4386eSCy Schubert int main(int argc, char *argv[])
25*e0c4386eSCy Schubert {
26*e0c4386eSCy Schubert char *port = "*:4433";
27*e0c4386eSCy Schubert BIO *in = NULL;
28*e0c4386eSCy Schubert BIO *ssl_bio, *tmp;
29*e0c4386eSCy Schubert SSL_CTX *ctx;
30*e0c4386eSCy Schubert SSL_CONF_CTX *cctx = NULL;
31*e0c4386eSCy Schubert CONF *conf = NULL;
32*e0c4386eSCy Schubert STACK_OF(CONF_VALUE) *sect = NULL;
33*e0c4386eSCy Schubert CONF_VALUE *cnf;
34*e0c4386eSCy Schubert long errline = -1;
35*e0c4386eSCy Schubert char buf[512];
36*e0c4386eSCy Schubert int ret = EXIT_FAILURE, i;
37*e0c4386eSCy Schubert
38*e0c4386eSCy Schubert ctx = SSL_CTX_new(TLS_server_method());
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert conf = NCONF_new(NULL);
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubert if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {
43*e0c4386eSCy Schubert if (errline <= 0)
44*e0c4386eSCy Schubert fprintf(stderr, "Error processing config file\n");
45*e0c4386eSCy Schubert else
46*e0c4386eSCy Schubert fprintf(stderr, "Error on line %ld\n", errline);
47*e0c4386eSCy Schubert goto err;
48*e0c4386eSCy Schubert }
49*e0c4386eSCy Schubert
50*e0c4386eSCy Schubert sect = NCONF_get_section(conf, "default");
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert if (sect == NULL) {
53*e0c4386eSCy Schubert fprintf(stderr, "Error retrieving default section\n");
54*e0c4386eSCy Schubert goto err;
55*e0c4386eSCy Schubert }
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert cctx = SSL_CONF_CTX_new();
58*e0c4386eSCy Schubert SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
59*e0c4386eSCy Schubert SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
60*e0c4386eSCy Schubert SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
61*e0c4386eSCy Schubert SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
62*e0c4386eSCy Schubert for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
63*e0c4386eSCy Schubert int rv;
64*e0c4386eSCy Schubert cnf = sk_CONF_VALUE_value(sect, i);
65*e0c4386eSCy Schubert rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
66*e0c4386eSCy Schubert if (rv > 0)
67*e0c4386eSCy Schubert continue;
68*e0c4386eSCy Schubert if (rv != -2) {
69*e0c4386eSCy Schubert fprintf(stderr, "Error processing %s = %s\n",
70*e0c4386eSCy Schubert cnf->name, cnf->value);
71*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
72*e0c4386eSCy Schubert goto err;
73*e0c4386eSCy Schubert }
74*e0c4386eSCy Schubert if (strcmp(cnf->name, "Port") == 0) {
75*e0c4386eSCy Schubert port = cnf->value;
76*e0c4386eSCy Schubert } else {
77*e0c4386eSCy Schubert fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
78*e0c4386eSCy Schubert goto err;
79*e0c4386eSCy Schubert }
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert if (!SSL_CONF_CTX_finish(cctx)) {
83*e0c4386eSCy Schubert fprintf(stderr, "Finish error\n");
84*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
85*e0c4386eSCy Schubert goto err;
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert /* Setup server side SSL bio */
89*e0c4386eSCy Schubert ssl_bio = BIO_new_ssl(ctx, 0);
90*e0c4386eSCy Schubert
91*e0c4386eSCy Schubert if ((in = BIO_new_accept(port)) == NULL)
92*e0c4386eSCy Schubert goto err;
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert /*
95*e0c4386eSCy Schubert * This means that when a new connection is accepted on 'in', The ssl_bio
96*e0c4386eSCy Schubert * will be 'duplicated' and have the new socket BIO push into it.
97*e0c4386eSCy Schubert * Basically it means the SSL BIO will be automatically setup
98*e0c4386eSCy Schubert */
99*e0c4386eSCy Schubert BIO_set_accept_bios(in, ssl_bio);
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert again:
102*e0c4386eSCy Schubert /*
103*e0c4386eSCy Schubert * The first call will setup the accept socket, and the second will get a
104*e0c4386eSCy Schubert * socket. In this loop, the first actual accept will occur in the
105*e0c4386eSCy Schubert * BIO_read() function.
106*e0c4386eSCy Schubert */
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert if (BIO_do_accept(in) <= 0)
109*e0c4386eSCy Schubert goto err;
110*e0c4386eSCy Schubert
111*e0c4386eSCy Schubert for (;;) {
112*e0c4386eSCy Schubert i = BIO_read(in, buf, 512);
113*e0c4386eSCy Schubert if (i == 0) {
114*e0c4386eSCy Schubert /*
115*e0c4386eSCy Schubert * If we have finished, remove the underlying BIO stack so the
116*e0c4386eSCy Schubert * next time we call any function for this BIO, it will attempt
117*e0c4386eSCy Schubert * to do an accept
118*e0c4386eSCy Schubert */
119*e0c4386eSCy Schubert printf("Done\n");
120*e0c4386eSCy Schubert tmp = BIO_pop(in);
121*e0c4386eSCy Schubert BIO_free_all(tmp);
122*e0c4386eSCy Schubert goto again;
123*e0c4386eSCy Schubert }
124*e0c4386eSCy Schubert if (i < 0) {
125*e0c4386eSCy Schubert if (BIO_should_retry(in))
126*e0c4386eSCy Schubert continue;
127*e0c4386eSCy Schubert goto err;
128*e0c4386eSCy Schubert }
129*e0c4386eSCy Schubert fwrite(buf, 1, i, stdout);
130*e0c4386eSCy Schubert fflush(stdout);
131*e0c4386eSCy Schubert }
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert ret = EXIT_SUCCESS;
134*e0c4386eSCy Schubert err:
135*e0c4386eSCy Schubert if (ret != EXIT_SUCCESS)
136*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
137*e0c4386eSCy Schubert BIO_free(in);
138*e0c4386eSCy Schubert return ret;
139*e0c4386eSCy Schubert }
140