Split out CKE construction PSK pre-amble and RSA into a separate function
[openssl.git] / demos / bio / server-cmod.c
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     SSL_load_error_strings();
31
32     /* Add ciphers and message digests */
33     OpenSSL_add_ssl_algorithms();
34
35     if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {
36         fprintf(stderr, "Error processing config file\n");
37         goto err;
38     }
39
40     ctx = SSL_CTX_new(TLS_server_method());
41
42     if (SSL_CTX_config(ctx, "server") == 0) {
43         fprintf(stderr, "Error configuring server.\n");
44         goto err;
45     }
46
47     /* Setup server side SSL bio */
48     ssl_bio = BIO_new_ssl(ctx, 0);
49
50     if ((in = BIO_new_accept(port)) == NULL)
51         goto err;
52
53     /*
54      * This means that when a new connection is accepted on 'in', The ssl_bio
55      * will be 'duplicated' and have the new socket BIO push into it.
56      * Basically it means the SSL BIO will be automatically setup
57      */
58     BIO_set_accept_bios(in, ssl_bio);
59
60  again:
61     /*
62      * The first call will setup the accept socket, and the second will get a
63      * socket.  In this loop, the first actual accept will occur in the
64      * BIO_read() function.
65      */
66
67     if (BIO_do_accept(in) <= 0)
68         goto err;
69
70     for (;;) {
71         i = BIO_read(in, buf, sizeof(buf));
72         if (i == 0) {
73             /*
74              * If we have finished, remove the underlying BIO stack so the
75              * next time we call any function for this BIO, it will attempt
76              * to do an accept
77              */
78             printf("Done\n");
79             tmp = BIO_pop(in);
80             BIO_free_all(tmp);
81             goto again;
82         }
83         if (i < 0) {
84             if (BIO_should_retry(in))
85                 continue;
86             goto err;
87         }
88         fwrite(buf, 1, i, stdout);
89         fflush(stdout);
90     }
91
92     ret = 0;
93  err:
94     if (ret) {
95         ERR_print_errors_fp(stderr);
96     }
97     BIO_free(in);
98     exit(ret);
99     return (!ret);
100 }