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