]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/bio/saccept.c
Fixup BIO demos for OpenSSL 1.1.x
[thirdparty/openssl.git] / demos / bio / saccept.c
CommitLineData
9e200689
RS
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
23a22b4c
MC
10/*-
11 * A minimal program to serve an SSL connection.
d02b48c6
RE
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 *
f3efeaad 17 * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
d02b48c6
RE
18 */
19
20#include <stdio.h>
21#include <signal.h>
ec577822
BM
22#include <openssl/err.h>
23#include <openssl/ssl.h>
d02b48c6 24
0f113f3e 25#define CERT_FILE "server.pem"
d02b48c6 26
919ba009 27static int done = 0;
d02b48c6 28
16c6deed 29void interrupt(int sig)
0f113f3e 30{
919ba009
VD
31 done = 1;
32}
33
34void 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);
0f113f3e 45}
d02b48c6 46
24f77b34 47int main(int argc, char *argv[])
0f113f3e
MC
48{
49 char *port = NULL;
919ba009 50 BIO *in = NULL;
0f113f3e
MC
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
32ec4153 61 ctx = SSL_CTX_new(TLS_server_method());
919ba009 62 if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
0f113f3e
MC
63 goto err;
64 if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
65 goto err;
66 if (!SSL_CTX_check_private_key(ctx))
67 goto err;
68
69 /* Setup server side SSL bio */
70 ssl_bio = BIO_new_ssl(ctx, 0);
71
72 if ((in = BIO_new_accept(port)) == NULL)
73 goto err;
74
75 /*
76 * This means that when a new connection is accepted on 'in', The ssl_bio
77 * will be 'duplicated' and have the new socket BIO push into it.
78 * Basically it means the SSL BIO will be automatically setup
79 */
80 BIO_set_accept_bios(in, ssl_bio);
81
919ba009
VD
82 /* Arrange to leave server loop on interrupt */
83 sigsetup();
84
0f113f3e
MC
85 again:
86 /*
87 * The first call will setup the accept socket, and the second will get a
88 * socket. In this loop, the first actual accept will occur in the
89 * BIO_read() function.
90 */
91
92 if (BIO_do_accept(in) <= 0)
93 goto err;
94
919ba009 95 while (!done) {
0f113f3e
MC
96 i = BIO_read(in, buf, 512);
97 if (i == 0) {
98 /*
99 * If we have finished, remove the underlying BIO stack so the
100 * next time we call any function for this BIO, it will attempt
101 * to do an accept
102 */
103 printf("Done\n");
104 tmp = BIO_pop(in);
105 BIO_free_all(tmp);
106 goto again;
107 }
108 if (i < 0)
109 goto err;
110 fwrite(buf, 1, i, stdout);
111 fflush(stdout);
112 }
113
114 ret = 0;
115 err:
116 if (ret) {
117 ERR_print_errors_fp(stderr);
118 }
ca3a82c3 119 BIO_free(in);
0f113f3e
MC
120 exit(ret);
121 return (!ret);
122}