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