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>
18 #define CERT_FILE "server.pem"
44 signal(SIGINT
,close_up
);
46 SSL_load_error_strings();
53 /* Add ciphers and message digests */
54 OpenSSL_add_ssl_algorithms();
56 ctx
=SSL_CTX_new(SSLv23_server_method());
57 if (!SSL_CTX_use_certificate_file(ctx
,CERT_FILE
,SSL_FILETYPE_PEM
))
59 if (!SSL_CTX_use_PrivateKey_file(ctx
,CERT_FILE
,SSL_FILETYPE_PEM
))
61 if (!SSL_CTX_check_private_key(ctx
))
64 /* Setup server side SSL bio */
66 ssl_bio
=BIO_new_ssl(ctx
,0);
68 if ((in
=BIO_new_accept(port
)) == NULL
) goto err
;
70 /* This means that when a new connection is acceptede on 'in',
71 * The ssl_bio will be 'dupilcated' and have the new socket
72 * BIO push into it. Basically it means the SSL BIO will be
73 * automatically setup */
74 BIO_set_accept_bios(in
,ssl_bio
);
77 /* The first call will setup the accept socket, and the second
78 * will get a socket. In this loop, the first actual accept
79 * will occur in the BIO_read() function. */
81 if (BIO_do_accept(in
) <= 0) goto err
;
85 i
=BIO_read(in
,buf
,512);
88 /* If we have finished, remove the underlying
89 * BIO stack so the next time we call any function
90 * for this BIO, it will attempt to do an
98 fwrite(buf
,1,i
,stdout
);
106 ERR_print_errors_fp(stderr
);
108 if (in
!= NULL
) BIO_free(in
);