2 /* demos/bio/saccept-conf.c */
4 /* A minimal program to serve an SSL connection.
6 * It uses the SSL_CONF API with a configuration file.
8 * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
15 #include <openssl/conf.h>
17 int main(int argc, char *argv[])
19 char *port = "*:4433";
23 SSL_CONF_CTX *cctx = NULL;
25 STACK_OF(CONF_VALUE) *sect = NULL;
31 SSL_load_error_strings();
33 /* Add ciphers and message digests */
34 OpenSSL_add_ssl_algorithms();
36 conf = NCONF_new(NULL);
38 if (NCONF_load(conf, "accept.cnf", &errline) <= 0)
41 fprintf(stderr, "Error processing config file\n");
43 fprintf(stderr, "Error on line %ld\n", errline);
47 sect = NCONF_get_section(conf, "default");
51 fprintf(stderr, "Error retrieving default section\n");
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++)
64 cnf = sk_CONF_VALUE_value(sect, i);
65 rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
70 fprintf(stderr, "Error processing %s = %s\n",
71 cnf->name, cnf->value);
72 ERR_print_errors_fp(stderr);
75 if (!strcmp(cnf->name, "Port"))
81 fprintf(stderr, "Unknown configuration option %s\n",
87 if (!SSL_CONF_CTX_finish(cctx))
89 fprintf(stderr, "Finish error\n");
90 ERR_print_errors_fp(stderr);
94 /* Setup server side SSL bio */
95 ssl_bio=BIO_new_ssl(ctx,0);
97 if ((in=BIO_new_accept(port)) == NULL) goto err;
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);
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. */
110 if (BIO_do_accept(in) <= 0) goto err;
114 i=BIO_read(in,buf,512);
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
127 fwrite(buf,1,i,stdout);
135 ERR_print_errors_fp(stderr);
137 if (in != NULL) BIO_free(in);