xref: /minix3/crypto/external/bsd/openssl/dist/demos/bio/saccept.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* NOCW */
2ebfedea0SLionel Sambuc /* demos/bio/saccept.c */
3ebfedea0SLionel Sambuc 
4*0a6a1f1dSLionel Sambuc /*-
5*0a6a1f1dSLionel Sambuc  * A minimal program to server an SSL connection.
6ebfedea0SLionel Sambuc  * It uses blocking.
7ebfedea0SLionel Sambuc  * saccept host:port
8ebfedea0SLionel Sambuc  * host is the interface IP to use.  If any interface, use *:port
9ebfedea0SLionel Sambuc  * The default it *:4433
10ebfedea0SLionel Sambuc  *
11ebfedea0SLionel Sambuc  * cc -I../../include saccept.c -L../.. -lssl -lcrypto
12ebfedea0SLionel Sambuc  */
13ebfedea0SLionel Sambuc 
14ebfedea0SLionel Sambuc #include <stdio.h>
15ebfedea0SLionel Sambuc #include <signal.h>
16ebfedea0SLionel Sambuc #include <openssl/err.h>
17ebfedea0SLionel Sambuc #include <openssl/ssl.h>
18ebfedea0SLionel Sambuc 
19ebfedea0SLionel Sambuc #define CERT_FILE       "server.pem"
20ebfedea0SLionel Sambuc 
21ebfedea0SLionel Sambuc BIO *in = NULL;
22ebfedea0SLionel Sambuc 
close_up()23ebfedea0SLionel Sambuc void close_up()
24ebfedea0SLionel Sambuc {
25ebfedea0SLionel Sambuc     if (in != NULL)
26ebfedea0SLionel Sambuc         BIO_free(in);
27ebfedea0SLionel Sambuc }
28ebfedea0SLionel Sambuc 
main(argc,argv)29ebfedea0SLionel Sambuc int main(argc, argv)
30ebfedea0SLionel Sambuc int argc;
31ebfedea0SLionel Sambuc char *argv[];
32ebfedea0SLionel Sambuc {
33ebfedea0SLionel Sambuc     char *port = NULL;
34ebfedea0SLionel Sambuc     BIO *ssl_bio, *tmp;
35ebfedea0SLionel Sambuc     SSL_CTX *ctx;
36ebfedea0SLionel Sambuc     SSL *ssl;
37ebfedea0SLionel Sambuc     char buf[512];
38ebfedea0SLionel Sambuc     int ret = 1, i;
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc     if (argc <= 1)
41ebfedea0SLionel Sambuc         port = "*:4433";
42ebfedea0SLionel Sambuc     else
43ebfedea0SLionel Sambuc         port = argv[1];
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc     signal(SIGINT, close_up);
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc     SSL_load_error_strings();
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc #ifdef WATT32
50ebfedea0SLionel Sambuc     dbug_init();
51ebfedea0SLionel Sambuc     sock_init();
52ebfedea0SLionel Sambuc #endif
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     /* Add ciphers and message digests */
55ebfedea0SLionel Sambuc     OpenSSL_add_ssl_algorithms();
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc     ctx = SSL_CTX_new(SSLv23_server_method());
58ebfedea0SLionel Sambuc     if (!SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
59ebfedea0SLionel Sambuc         goto err;
60ebfedea0SLionel Sambuc     if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
61ebfedea0SLionel Sambuc         goto err;
62ebfedea0SLionel Sambuc     if (!SSL_CTX_check_private_key(ctx))
63ebfedea0SLionel Sambuc         goto err;
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc     /* Setup server side SSL bio */
66ebfedea0SLionel Sambuc     ssl = SSL_new(ctx);
67ebfedea0SLionel Sambuc     ssl_bio = BIO_new_ssl(ctx, 0);
68ebfedea0SLionel Sambuc 
69*0a6a1f1dSLionel Sambuc     if ((in = BIO_new_accept(port)) == NULL)
70*0a6a1f1dSLionel Sambuc         goto err;
71ebfedea0SLionel Sambuc 
72*0a6a1f1dSLionel Sambuc     /*
73*0a6a1f1dSLionel Sambuc      * This means that when a new connection is acceptede on 'in', The
74*0a6a1f1dSLionel Sambuc      * ssl_bio will be 'dupilcated' and have the new socket BIO push into it.
75*0a6a1f1dSLionel Sambuc      * Basically it means the SSL BIO will be automatically setup
76*0a6a1f1dSLionel Sambuc      */
77ebfedea0SLionel Sambuc     BIO_set_accept_bios(in, ssl_bio);
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc  again:
80*0a6a1f1dSLionel Sambuc     /*
81*0a6a1f1dSLionel Sambuc      * The first call will setup the accept socket, and the second will get a
82*0a6a1f1dSLionel Sambuc      * socket.  In this loop, the first actual accept will occur in the
83*0a6a1f1dSLionel Sambuc      * BIO_read() function.
84*0a6a1f1dSLionel Sambuc      */
85ebfedea0SLionel Sambuc 
86*0a6a1f1dSLionel Sambuc     if (BIO_do_accept(in) <= 0)
87*0a6a1f1dSLionel Sambuc         goto err;
88ebfedea0SLionel Sambuc 
89*0a6a1f1dSLionel Sambuc     for (;;) {
90ebfedea0SLionel Sambuc         i = BIO_read(in, buf, 512);
91*0a6a1f1dSLionel Sambuc         if (i == 0) {
92*0a6a1f1dSLionel Sambuc             /*
93*0a6a1f1dSLionel Sambuc              * If we have finished, remove the underlying BIO stack so the
94*0a6a1f1dSLionel Sambuc              * next time we call any function for this BIO, it will attempt
95*0a6a1f1dSLionel Sambuc              * to do an accept
96*0a6a1f1dSLionel Sambuc              */
97ebfedea0SLionel Sambuc             printf("Done\n");
98ebfedea0SLionel Sambuc             tmp = BIO_pop(in);
99ebfedea0SLionel Sambuc             BIO_free_all(tmp);
100ebfedea0SLionel Sambuc             goto again;
101ebfedea0SLionel Sambuc         }
102*0a6a1f1dSLionel Sambuc         if (i < 0)
103*0a6a1f1dSLionel Sambuc             goto err;
104ebfedea0SLionel Sambuc         fwrite(buf, 1, i, stdout);
105ebfedea0SLionel Sambuc         fflush(stdout);
106ebfedea0SLionel Sambuc     }
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc     ret = 0;
109ebfedea0SLionel Sambuc  err:
110*0a6a1f1dSLionel Sambuc     if (ret) {
111ebfedea0SLionel Sambuc         ERR_print_errors_fp(stderr);
112ebfedea0SLionel Sambuc     }
113*0a6a1f1dSLionel Sambuc     if (in != NULL)
114*0a6a1f1dSLionel Sambuc         BIO_free(in);
115ebfedea0SLionel Sambuc     exit(ret);
116ebfedea0SLionel Sambuc     return (!ret);
117ebfedea0SLionel Sambuc }
118