2 /* demos/bio/saccept.c */
4 /* A minimal program to server an SSL connection.
7 * host is the interface IP to use. If any interface, use *:port
8 * The default it *:4433
10 * cc -I../../include saccept.c -L../.. -lssl -lcrypto
15 #include <openssl/err.h>
16 #include <openssl/ssl.h>
19 int main(int argc
, char *argv
[])
21 char *port
= "*:4433";
28 char **args
= argv
+ 1;
31 SSL_load_error_strings();
33 /* Add ciphers and message digests */
34 OpenSSL_add_ssl_algorithms();
36 ctx
=SSL_CTX_new(SSLv23_server_method());
38 cctx
= SSL_CONF_CTX_new();
39 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_SERVER
);
40 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_CERTIFICATE
);
41 SSL_CONF_CTX_set_ssl_ctx(cctx
, ctx
);
42 while(*args
&& **args
== '-')
45 /* Parse standard arguments */
46 rv
= SSL_CONF_cmd_argv(cctx
, &nargs
, &args
);
49 fprintf(stderr
, "Missing argument for %s\n", *args
);
54 fprintf(stderr
, "Error in command %s\n", *args
);
55 ERR_print_errors_fp(stderr
);
58 /* If rv > 0 we processed something so proceed to next arg */
61 /* Otherwise application specific argument processing */
62 if (!strcmp(*args
, "-port"))
67 fprintf(stderr
, "Missing -port argument\n");
76 fprintf(stderr
, "Unknown argument %s\n", *args
);
81 if (!SSL_CONF_CTX_finish(cctx
))
83 fprintf(stderr
, "Finish error\n");
84 ERR_print_errors_fp(stderr
);
88 /* Setup server side SSL bio */
89 ssl_bio
=BIO_new_ssl(ctx
,0);
91 if ((in
=BIO_new_accept(port
)) == NULL
) goto err
;
93 /* This means that when a new connection is acceptede on 'in',
94 * The ssl_bio will be 'dupilcated' and have the new socket
95 * BIO push into it. Basically it means the SSL BIO will be
96 * automatically setup */
97 BIO_set_accept_bios(in
,ssl_bio
);
100 /* The first call will setup the accept socket, and the second
101 * will get a socket. In this loop, the first actual accept
102 * will occur in the BIO_read() function. */
104 if (BIO_do_accept(in
) <= 0) goto err
;
108 i
=BIO_read(in
,buf
,512);
111 /* If we have finished, remove the underlying
112 * BIO stack so the next time we call any function
113 * for this BIO, it will attempt to do an
121 fwrite(buf
,1,i
,stdout
);
129 ERR_print_errors_fp(stderr
);
131 if (in
!= NULL
) BIO_free(in
);