106a089dafa7a0abee335dcfa287e68c76391c82
[openssl.git] / demos / bio / saccept.c
1 /*
2  * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*-
11  * A minimal program to serve an SSL connection.
12  * It uses blocking.
13  * saccept host:port
14  * host is the interface IP to use.  If any interface, use *:port
15  * The default it *:4433
16  *
17  * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
18  */
19
20 #include <stdio.h>
21 #include <signal.h>
22 #include <openssl/err.h>
23 #include <openssl/ssl.h>
24
25 #define CERT_FILE       "server.pem"
26
27 static int done = 0;
28
29 void interrupt()
30 {
31     done = 1;
32 }
33
34 void sigsetup(void)
35 {
36     struct sigaction sa;
37
38     /*
39      * Catch at most once, and don't restart the accept system call.
40      */
41     sa.sa_flags = SA_RESETHAND;
42     sa.sa_handler = interrupt;
43     sigemptyset(&sa.sa_mask);
44     sigaction(SIGINT, &sa, NULL);
45 }
46
47 int main(int argc, char *argv[])
48 {
49     char *port = NULL;
50     BIO *in = NULL;
51     BIO *ssl_bio, *tmp;
52     SSL_CTX *ctx;
53     char buf[512];
54     int ret = 1, i;
55
56     if (argc <= 1)
57         port = "*:4433";
58     else
59         port = argv[1];
60
61     SSL_load_error_strings();
62
63     /* Add ciphers and message digests */
64     OpenSSL_add_ssl_algorithms();
65
66     ctx = SSL_CTX_new(TLS_server_method());
67     if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
68         goto err;
69     if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
70         goto err;
71     if (!SSL_CTX_check_private_key(ctx))
72         goto err;
73
74     /* Setup server side SSL bio */
75     ssl_bio = BIO_new_ssl(ctx, 0);
76
77     if ((in = BIO_new_accept(port)) == NULL)
78         goto err;
79
80     /*
81      * This means that when a new connection is accepted on 'in', The ssl_bio
82      * will be 'duplicated' and have the new socket BIO push into it.
83      * Basically it means the SSL BIO will be automatically setup
84      */
85     BIO_set_accept_bios(in, ssl_bio);
86
87     /* Arrange to leave server loop on interrupt */
88     sigsetup();
89
90  again:
91     /*
92      * The first call will setup the accept socket, and the second will get a
93      * socket.  In this loop, the first actual accept will occur in the
94      * BIO_read() function.
95      */
96
97     if (BIO_do_accept(in) <= 0)
98         goto err;
99
100     while (!done) {
101         i = BIO_read(in, buf, 512);
102         if (i == 0) {
103             /*
104              * If we have finished, remove the underlying BIO stack so the
105              * next time we call any function for this BIO, it will attempt
106              * to do an accept
107              */
108             printf("Done\n");
109             tmp = BIO_pop(in);
110             BIO_free_all(tmp);
111             goto again;
112         }
113         if (i < 0)
114             goto err;
115         fwrite(buf, 1, i, stdout);
116         fflush(stdout);
117     }
118
119     ret = 0;
120  err:
121     if (ret) {
122         ERR_print_errors_fp(stderr);
123     }
124     BIO_free(in);
125     exit(ret);
126     return (!ret);
127 }