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