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