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