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