2 * A minimal program to serve an SSL connection.
5 * host is the interface IP to use. If any interface, use *:port
6 * The default it *:4433
8 * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
16 #define CERT_FILE "server.pem"
30 * Catch at most once, and don't restart the accept system call.
32 sa.sa_flags = SA_RESETHAND;
33 sa.sa_handler = interrupt;
34 sigemptyset(&sa.sa_mask);
35 sigaction(SIGINT, &sa, NULL);
38 int main(int argc, char *argv[])
52 SSL_load_error_strings();
54 /* Add ciphers and message digests */
55 OpenSSL_add_ssl_algorithms();
57 ctx = SSL_CTX_new(TLS_server_method());
58 if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
60 if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
62 if (!SSL_CTX_check_private_key(ctx))
65 /* Setup server side SSL bio */
66 ssl_bio = BIO_new_ssl(ctx, 0);
68 if ((in = BIO_new_accept(port)) == NULL)
72 * This means that when a new connection is accepted on 'in', The ssl_bio
73 * will be 'duplicated' and have the new socket BIO push into it.
74 * Basically it means the SSL BIO will be automatically setup
76 BIO_set_accept_bios(in, ssl_bio);
78 /* Arrange to leave server loop on interrupt */
83 * The first call will setup the accept socket, and the second will get a
84 * socket. In this loop, the first actual accept will occur in the
85 * BIO_read() function.
88 if (BIO_do_accept(in) <= 0)
92 i = BIO_read(in, buf, 512);
95 * If we have finished, remove the underlying BIO stack so the
96 * next time we call any function for this BIO, it will attempt
106 fwrite(buf, 1, i, stdout);
113 ERR_print_errors_fp(stderr);