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