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