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