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