2 /* demos/bio/saccept.c */
5 * A minimal program to serve an SSL connection.
8 * host is the interface IP to use. If any interface, use *:port
9 * The default it *:4433
11 * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
16 #include <openssl/err.h>
17 #include <openssl/ssl.h>
19 #define CERT_FILE "server.pem"
29 int main(int argc, char *argv[])
42 signal(SIGINT,close_up);
44 SSL_load_error_strings();
46 /* Add ciphers and message digests */
47 OpenSSL_add_ssl_algorithms();
49 ctx=SSL_CTX_new(SSLv23_server_method());
50 if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
52 if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
54 if (!SSL_CTX_check_private_key(ctx))
57 /* Setup server side SSL bio */
58 ssl_bio=BIO_new_ssl(ctx,0);
60 if ((in=BIO_new_accept(port)) == NULL) goto err;
62 /* This means that when a new connection is accepted on 'in',
63 * The ssl_bio will be 'duplicated' and have the new socket
64 * BIO push into it. Basically it means the SSL BIO will be
65 * automatically setup */
66 BIO_set_accept_bios(in,ssl_bio);
69 /* The first call will setup the accept socket, and the second
70 * will get a socket. In this loop, the first actual accept
71 * will occur in the BIO_read() function. */
73 if (BIO_do_accept(in) <= 0) goto err;
77 i=BIO_read(in,buf,512);
80 /* If we have finished, remove the underlying
81 * BIO stack so the next time we call any function
82 * for this BIO, it will attempt to do an
90 fwrite(buf,1,i,stdout);
98 ERR_print_errors_fp(stderr);
100 if (in != NULL) BIO_free(in);