]> git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/saccept.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / demos / bio / saccept.c
1 /* NOCW */
2 /* demos/bio/saccept.c */
3
4 /* A minimal program to server an SSL connection.
5 * It uses blocking.
6 * saccept host:port
7 * host is the interface IP to use. If any interface, use *:port
8 * The default it *:4433
9 *
10 * cc -I../../include saccept.c -L../.. -lssl -lcrypto
11 */
12
13 #include <stdio.h>
14 #include <signal.h>
15 #include "err.h"
16 #include "ssl.h"
17
18 #define CERT_FILE "server.pem"
19
20 BIO *in=NULL;
21
22 void close_up()
23 {
24 if (in != NULL)
25 BIO_free(in);
26 }
27
28 int main(argc,argv)
29 int argc;
30 char *argv[];
31 {
32 char *port=NULL;
33 BIO *ssl_bio,*tmp;
34 SSL_CTX *ctx;
35 SSL *ssl;
36 char buf[512];
37 int ret=1,i;
38
39 if (argc <= 1)
40 port="*:4433";
41 else
42 port=argv[1];
43
44 signal(SIGINT,close_up);
45
46 SSL_load_error_strings();
47
48 /* Add ciphers and message digests */
49 SSLeay_add_ssl_algorithms();
50
51 ctx=SSL_CTX_new(SSLv23_server_method());
52 if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
53 goto err;
54 if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
55 goto err;
56 if (!SSL_CTX_check_private_key(ctx))
57 goto err;
58
59 /* Setup server side SSL bio */
60 ssl=SSL_new(ctx);
61 ssl_bio=BIO_new_ssl(ctx,0);
62
63 if ((in=BIO_new_accept(port)) == NULL) goto err;
64
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);
70
71 again:
72 /* The first call will setup the accept socket, and the second
73 * will get a socket. In this loop, the first actuall accept
74 * will occur in the BIO_read() function. */
75
76 if (BIO_do_accept(in) <= 0) goto err;
77
78 for (;;)
79 {
80 i=BIO_read(in,buf,512);
81 if (i == 0)
82 {
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
86 * accept */
87 printf("Done\n");
88 tmp=BIO_pop(in);
89 BIO_free_all(tmp);
90 goto again;
91 }
92 if (i < 0) goto err;
93 fwrite(buf,1,i,stdout);
94 fflush(stdout);
95 }
96
97 ret=0;
98 err:
99 if (ret)
100 {
101 ERR_print_errors_fp(stderr);
102 }
103 if (in != NULL) BIO_free(in);
104 exit(ret);
105 return(!ret);
106 }
107