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