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