2 /* demos/bio/saccept.c */
4 /* A minimal program to serve 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 -ldl
15 #include <openssl/err.h>
16 #include <openssl/ssl.h>
18 #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(SSLv23_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) goto err;
61 /* This means that when a new connection is accepted on 'in',
62 * The ssl_bio will be 'duplicated' and have the new socket
63 * BIO push into it. Basically it means the SSL BIO will be
64 * automatically setup */
65 BIO_set_accept_bios(in,ssl_bio);
68 /* The first call will setup the accept socket, and the second
69 * will get a socket. In this loop, the first actual accept
70 * will occur in the BIO_read() function. */
72 if (BIO_do_accept(in) <= 0) goto err;
76 i=BIO_read(in,buf,512);
79 /* If we have finished, remove the underlying
80 * BIO stack so the next time we call any function
81 * for this BIO, it will attempt to do an
89 fwrite(buf,1,i,stdout);
97 ERR_print_errors_fp(stderr);
99 if (in != NULL) BIO_free(in);