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