server-cmod.c revision 1.1 1 /*
2 * Copyright 2015-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 TLS server it ses SSL_CTX_config and a configuration file to
12 * set most server parameters.
13 */
14
15 #include <stdio.h>
16 #include <signal.h>
17 #include <openssl/err.h>
18 #include <openssl/ssl.h>
19 #include <openssl/conf.h>
20
21 int main(int argc, char *argv[])
22 {
23 unsigned char buf[512];
24 char *port = "*:4433";
25 BIO *in = NULL;
26 BIO *ssl_bio, *tmp;
27 SSL_CTX *ctx;
28 int ret = 1, i;
29
30 ctx = SSL_CTX_new(TLS_server_method());
31
32 if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {
33 fprintf(stderr, "Error processing config file\n");
34 goto err;
35 }
36
37 if (SSL_CTX_config(ctx, "server") == 0) {
38 fprintf(stderr, "Error configuring server.\n");
39 goto err;
40 }
41
42 /* Setup server side SSL bio */
43 ssl_bio = BIO_new_ssl(ctx, 0);
44
45 if ((in = BIO_new_accept(port)) == NULL)
46 goto err;
47
48 /*
49 * This means that when a new connection is accepted on 'in', The ssl_bio
50 * will be 'duplicated' and have the new socket BIO push into it.
51 * Basically it means the SSL BIO will be automatically setup
52 */
53 BIO_set_accept_bios(in, ssl_bio);
54
55 again:
56 /*
57 * The first call will setup the accept socket, and the second will get a
58 * socket. In this loop, the first actual accept will occur in the
59 * BIO_read() function.
60 */
61
62 if (BIO_do_accept(in) <= 0)
63 goto err;
64
65 for (;;) {
66 i = BIO_read(in, buf, sizeof(buf));
67 if (i == 0) {
68 /*
69 * If we have finished, remove the underlying BIO stack so the
70 * next time we call any function for this BIO, it will attempt
71 * to do an accept
72 */
73 printf("Done\n");
74 tmp = BIO_pop(in);
75 BIO_free_all(tmp);
76 goto again;
77 }
78 if (i < 0) {
79 if (BIO_should_retry(in))
80 continue;
81 goto err;
82 }
83 fwrite(buf, 1, i, stdout);
84 fflush(stdout);
85 }
86
87 ret = 0;
88 err:
89 if (ret) {
90 ERR_print_errors_fp(stderr);
91 }
92 BIO_free(in);
93 exit(ret);
94 return (!ret);
95 }
96