Document -trusted_first option in man pages and help.
[openssl.git] / demos / bio / client-arg.c
1 #include <openssl/err.h>
2 #include <openssl/ssl.h>
3
4 int main(int argc, char **argv)
5         {
6         BIO *sbio = NULL, *out = NULL;
7         int len;
8         char tmpbuf[1024];
9         SSL_CTX *ctx;
10         SSL_CONF_CTX *cctx;
11         SSL *ssl;
12         char **args = argv + 1;
13         const char *connect_str = "localhost:4433";
14         int nargs = argc - 1;
15
16         ERR_load_crypto_strings();
17         ERR_load_SSL_strings();
18         SSL_library_init();
19
20         ctx = SSL_CTX_new(SSLv23_client_method());
21         cctx = SSL_CONF_CTX_new();
22         SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
23         SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
24         while(*args && **args == '-')
25                 {
26                 int rv;
27                 /* Parse standard arguments */
28                 rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
29                 if (rv == -3)
30                         {
31                         fprintf(stderr, "Missing argument for %s\n", *args);
32                         goto end;
33                         }
34                 if (rv < 0)
35                         {
36                         fprintf(stderr, "Error in command %s\n", *args);
37                         ERR_print_errors_fp(stderr);
38                         goto end;
39                         }
40                 /* If rv > 0 we processed something so proceed to next arg */
41                 if (rv > 0)
42                         continue;
43                 /* Otherwise application specific argument processing */
44                 if (!strcmp(*args, "-connect"))
45                         {
46                         connect_str = args[1];
47                         if (connect_str == NULL)
48                                 {
49                                 fprintf(stderr, "Missing -connect argument\n");
50                                 goto end;
51                                 }
52                         args += 2;
53                         nargs -= 2;
54                         continue;
55                         }
56                 else
57                         {
58                         fprintf(stderr, "Unknown argument %s\n", *args);
59                         goto end;
60                         }
61                 }
62
63         if (!SSL_CONF_CTX_finish(cctx))
64                 {
65                 fprintf(stderr, "Finish error\n");
66                 ERR_print_errors_fp(stderr);
67                 goto err;
68                 }
69
70         /* We'd normally set some stuff like the verify paths and
71         * mode here because as things stand this will connect to
72         * any server whose certificate is signed by any CA.
73          */
74
75         sbio = BIO_new_ssl_connect(ctx);
76
77         BIO_get_ssl(sbio, &ssl);
78
79         if(!ssl)
80                 {
81                 fprintf(stderr, "Can't locate SSL pointer\n");
82                 goto end;
83                 }
84
85         /* Don't want any retries */
86         SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
87
88         /* We might want to do other things with ssl here */
89
90         BIO_set_conn_hostname(sbio, connect_str);
91
92         out = BIO_new_fp(stdout, BIO_NOCLOSE);
93         if(BIO_do_connect(sbio) <= 0)
94                 {
95                 fprintf(stderr, "Error connecting to server\n");
96                 ERR_print_errors_fp(stderr);
97                 goto end;
98                 }
99
100         if(BIO_do_handshake(sbio) <= 0)
101                 {
102                 fprintf(stderr, "Error establishing SSL connection\n");
103                 ERR_print_errors_fp(stderr);
104                 goto end;
105                 }
106
107         /* Could examine ssl here to get connection info */
108
109         BIO_puts(sbio, "GET / HTTP/1.0\n\n");
110         for(;;)
111                 {       
112                 len = BIO_read(sbio, tmpbuf, 1024);
113                 if(len <= 0) break;
114                 BIO_write(out, tmpbuf, len);
115                 }
116         end:
117         SSL_CONF_CTX_free(cctx);
118         BIO_free_all(sbio);
119         BIO_free(out);
120         return 0;
121         }
122