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