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"
28 int main(int argc, char *argv[])
41 signal(SIGINT, close_up);
43 SSL_load_error_strings();
45 /* Add ciphers and message digests */
46 OpenSSL_add_ssl_algorithms();
48 ctx = SSL_CTX_new(TLS_server_method());
49 if (!SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
51 if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
53 if (!SSL_CTX_check_private_key(ctx))
56 /* Setup server side SSL bio */
57 ssl_bio = BIO_new_ssl(ctx, 0);
59 if ((in = BIO_new_accept(port)) == NULL)
63 * This means that when a new connection is accepted on 'in', The ssl_bio
64 * will be 'duplicated' and have the new socket BIO push into it.
65 * Basically it means the SSL BIO will be automatically setup
67 BIO_set_accept_bios(in, ssl_bio);
71 * The first call will setup the accept socket, and the second will get a
72 * socket. In this loop, the first actual accept will occur in the
73 * BIO_read() function.
76 if (BIO_do_accept(in) <= 0)
80 i = BIO_read(in, buf, 512);
83 * If we have finished, remove the underlying BIO stack so the
84 * next time we call any function for this BIO, it will attempt
94 fwrite(buf, 1, i, stdout);
101 ERR_print_errors_fp(stderr);