xref: /netbsd-src/crypto/external/bsd/openssl/dist/demos/bio/server-conf.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
213d40330Schristos  * Copyright 2013-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  */
9a6054fbfSspz 
10a6054fbfSspz /*
11a6054fbfSspz  * A minimal program to serve an SSL connection. It uses blocking. It uses
12a6054fbfSspz  * the SSL_CONF API with a configuration file. cc -I../../include saccept.c
13a6054fbfSspz  * -L../.. -lssl -lcrypto -ldl
14a6054fbfSspz  */
15a6054fbfSspz 
16a6054fbfSspz #include <stdio.h>
17c7da899bSchristos #include <string.h>
18a6054fbfSspz #include <signal.h>
1913d40330Schristos #include <stdlib.h>
20a6054fbfSspz #include <openssl/err.h>
21a6054fbfSspz #include <openssl/ssl.h>
22a6054fbfSspz #include <openssl/conf.h>
23a6054fbfSspz 
main(int argc,char * argv[])24a6054fbfSspz int main(int argc, char *argv[])
25a6054fbfSspz {
26a6054fbfSspz     char *port = "*:4433";
27a6054fbfSspz     BIO *in = NULL;
28a6054fbfSspz     BIO *ssl_bio, *tmp;
29a6054fbfSspz     SSL_CTX *ctx;
30a6054fbfSspz     SSL_CONF_CTX *cctx = NULL;
31a6054fbfSspz     CONF *conf = NULL;
32a6054fbfSspz     STACK_OF(CONF_VALUE) *sect = NULL;
33a6054fbfSspz     CONF_VALUE *cnf;
34a6054fbfSspz     long errline = -1;
35a6054fbfSspz     char buf[512];
3613d40330Schristos     int ret = EXIT_FAILURE, i;
37a6054fbfSspz 
38c7da899bSchristos     ctx = SSL_CTX_new(TLS_server_method());
39a6054fbfSspz 
40a6054fbfSspz     conf = NCONF_new(NULL);
41a6054fbfSspz 
42a6054fbfSspz     if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {
43a6054fbfSspz         if (errline <= 0)
44a6054fbfSspz             fprintf(stderr, "Error processing config file\n");
45a6054fbfSspz         else
46a6054fbfSspz             fprintf(stderr, "Error on line %ld\n", errline);
47a6054fbfSspz         goto err;
48a6054fbfSspz     }
49a6054fbfSspz 
50a6054fbfSspz     sect = NCONF_get_section(conf, "default");
51a6054fbfSspz 
52a6054fbfSspz     if (sect == NULL) {
53a6054fbfSspz         fprintf(stderr, "Error retrieving default section\n");
54a6054fbfSspz         goto err;
55a6054fbfSspz     }
56a6054fbfSspz 
57a6054fbfSspz     cctx = SSL_CONF_CTX_new();
58a6054fbfSspz     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
59a6054fbfSspz     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
60a6054fbfSspz     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
61a6054fbfSspz     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
62a6054fbfSspz     for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
63a6054fbfSspz         int rv;
64a6054fbfSspz         cnf = sk_CONF_VALUE_value(sect, i);
65a6054fbfSspz         rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
66a6054fbfSspz         if (rv > 0)
67a6054fbfSspz             continue;
68a6054fbfSspz         if (rv != -2) {
69a6054fbfSspz             fprintf(stderr, "Error processing %s = %s\n",
70a6054fbfSspz                     cnf->name, cnf->value);
71a6054fbfSspz             ERR_print_errors_fp(stderr);
72a6054fbfSspz             goto err;
73a6054fbfSspz         }
74c7da899bSchristos         if (strcmp(cnf->name, "Port") == 0) {
75a6054fbfSspz             port = cnf->value;
76a6054fbfSspz         } else {
77a6054fbfSspz             fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
78a6054fbfSspz             goto err;
79a6054fbfSspz         }
80a6054fbfSspz     }
81a6054fbfSspz 
82a6054fbfSspz     if (!SSL_CONF_CTX_finish(cctx)) {
83a6054fbfSspz         fprintf(stderr, "Finish error\n");
84a6054fbfSspz         ERR_print_errors_fp(stderr);
85a6054fbfSspz         goto err;
86a6054fbfSspz     }
87a6054fbfSspz 
88a6054fbfSspz     /* Setup server side SSL bio */
89a6054fbfSspz     ssl_bio = BIO_new_ssl(ctx, 0);
90a6054fbfSspz 
91a6054fbfSspz     if ((in = BIO_new_accept(port)) == NULL)
92a6054fbfSspz         goto err;
93a6054fbfSspz 
94a6054fbfSspz     /*
95a6054fbfSspz      * This means that when a new connection is accepted on 'in', The ssl_bio
96a6054fbfSspz      * will be 'duplicated' and have the new socket BIO push into it.
97a6054fbfSspz      * Basically it means the SSL BIO will be automatically setup
98a6054fbfSspz      */
99a6054fbfSspz     BIO_set_accept_bios(in, ssl_bio);
100a6054fbfSspz 
101a6054fbfSspz  again:
102a6054fbfSspz     /*
103a6054fbfSspz      * The first call will setup the accept socket, and the second will get a
104a6054fbfSspz      * socket.  In this loop, the first actual accept will occur in the
105a6054fbfSspz      * BIO_read() function.
106a6054fbfSspz      */
107a6054fbfSspz 
108a6054fbfSspz     if (BIO_do_accept(in) <= 0)
109a6054fbfSspz         goto err;
110a6054fbfSspz 
111a6054fbfSspz     for (;;) {
112a6054fbfSspz         i = BIO_read(in, buf, 512);
113a6054fbfSspz         if (i == 0) {
114a6054fbfSspz             /*
115a6054fbfSspz              * If we have finished, remove the underlying BIO stack so the
116a6054fbfSspz              * next time we call any function for this BIO, it will attempt
117a6054fbfSspz              * to do an accept
118a6054fbfSspz              */
119a6054fbfSspz             printf("Done\n");
120a6054fbfSspz             tmp = BIO_pop(in);
121a6054fbfSspz             BIO_free_all(tmp);
122a6054fbfSspz             goto again;
123a6054fbfSspz         }
124a6054fbfSspz         if (i < 0) {
125a6054fbfSspz             if (BIO_should_retry(in))
126a6054fbfSspz                 continue;
127a6054fbfSspz             goto err;
128a6054fbfSspz         }
129a6054fbfSspz         fwrite(buf, 1, i, stdout);
130a6054fbfSspz         fflush(stdout);
131a6054fbfSspz     }
132a6054fbfSspz 
13313d40330Schristos     ret = EXIT_SUCCESS;
134a6054fbfSspz  err:
13513d40330Schristos     if (ret != EXIT_SUCCESS)
136a6054fbfSspz         ERR_print_errors_fp(stderr);
137a6054fbfSspz     BIO_free(in);
13813d40330Schristos     return ret;
139a6054fbfSspz }
140