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
9 #include <openssl/err.h>
10 #include <openssl/ssl.h>
11 #include <openssl/conf.h>
13 int main(int argc
, char *argv
[])
15 char *port
= "*:4433";
19 SSL_CONF_CTX
*cctx
= NULL
;
21 STACK_OF(CONF_VALUE
) *sect
= NULL
;
27 SSL_load_error_strings();
29 /* Add ciphers and message digests */
30 OpenSSL_add_ssl_algorithms();
32 conf
= NCONF_new(NULL
);
34 if (NCONF_load(conf
, "accept.cnf", &errline
) <= 0) {
36 fprintf(stderr
, "Error processing config file\n");
38 fprintf(stderr
, "Error on line %ld\n", errline
);
42 sect
= NCONF_get_section(conf
, "default");
45 fprintf(stderr
, "Error retrieving default section\n");
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
++) {
57 cnf
= sk_CONF_VALUE_value(sect
, i
);
58 rv
= SSL_CONF_cmd(cctx
, cnf
->name
, cnf
->value
);
62 fprintf(stderr
, "Error processing %s = %s\n",
63 cnf
->name
, cnf
->value
);
64 ERR_print_errors_fp(stderr
);
67 if (strcmp(cnf
->name
, "Port") == 0) {
70 fprintf(stderr
, "Unknown configuration option %s\n", cnf
->name
);
75 if (!SSL_CONF_CTX_finish(cctx
)) {
76 fprintf(stderr
, "Finish error\n");
77 ERR_print_errors_fp(stderr
);
81 /* Setup server side SSL bio */
82 ssl_bio
= BIO_new_ssl(ctx
, 0);
84 if ((in
= BIO_new_accept(port
)) == NULL
)
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
92 BIO_set_accept_bios(in
, ssl_bio
);
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.
101 if (BIO_do_accept(in
) <= 0)
105 i
= BIO_read(in
, buf
, 512);
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
118 if (BIO_should_retry(in
))
122 fwrite(buf
, 1, i
, stdout
);
129 ERR_print_errors_fp(stderr
);