4e4d4bc8df2447dba3f92288962d8ca0e614319b
[openssl.git] / demos / bio / client-conf.c
1 #include <string.h>
2 #include <openssl/err.h>
3 #include <openssl/ssl.h>
4 #include <openssl/conf.h>
5
6 int main(int argc, char **argv)
7 {
8     BIO *sbio = NULL, *out = NULL;
9     int i, len, rv;
10     char tmpbuf[1024];
11     SSL_CTX *ctx = NULL;
12     SSL_CONF_CTX *cctx = NULL;
13     SSL *ssl = NULL;
14     CONF *conf = NULL;
15     STACK_OF(CONF_VALUE) *sect = NULL;
16     CONF_VALUE *cnf;
17     const char *connect_str = "localhost:4433";
18     long errline = -1;
19
20     ERR_load_crypto_strings();
21     ERR_load_SSL_strings();
22     SSL_library_init();
23
24     conf = NCONF_new(NULL);
25
26     if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
27         if (errline <= 0)
28             fprintf(stderr, "Error processing config file\n");
29         else
30             fprintf(stderr, "Error on line %ld\n", errline);
31         goto end;
32     }
33
34     sect = NCONF_get_section(conf, "default");
35
36     if (sect == NULL) {
37         fprintf(stderr, "Error retrieving default section\n");
38         goto end;
39     }
40
41     ctx = SSL_CTX_new(TLS_client_method());
42     cctx = SSL_CONF_CTX_new();
43     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
44     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
45     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
46     for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
47         cnf = sk_CONF_VALUE_value(sect, i);
48         rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
49         if (rv > 0)
50             continue;
51         if (rv != -2) {
52             fprintf(stderr, "Error processing %s = %s\n",
53                     cnf->name, cnf->value);
54             ERR_print_errors_fp(stderr);
55             goto end;
56         }
57         if (strcmp(cnf->name, "Connect") == 0) {
58             connect_str = cnf->value;
59         } else {
60             fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
61             goto end;
62         }
63     }
64
65     if (!SSL_CONF_CTX_finish(cctx)) {
66         fprintf(stderr, "Finish error\n");
67         ERR_print_errors_fp(stderr);
68         goto end;
69     }
70
71     /*
72      * We'd normally set some stuff like the verify paths and * mode here
73      * because as things stand this will connect to * any server whose
74      * certificate is signed by any CA.
75      */
76
77     sbio = BIO_new_ssl_connect(ctx);
78
79     BIO_get_ssl(sbio, &ssl);
80
81     if (!ssl) {
82         fprintf(stderr, "Can't locate SSL pointer\n");
83         goto end;
84     }
85
86     /* Don't want any retries */
87     SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
88
89     /* We might want to do other things with ssl here */
90
91     BIO_set_conn_hostname(sbio, connect_str);
92
93     out = BIO_new_fp(stdout, BIO_NOCLOSE);
94     if (BIO_do_connect(sbio) <= 0) {
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         fprintf(stderr, "Error establishing SSL connection\n");
102         ERR_print_errors_fp(stderr);
103         goto end;
104     }
105
106     /* Could examine ssl here to get connection info */
107
108     BIO_puts(sbio, "GET / HTTP/1.0\n\n");
109     for (;;) {
110         len = BIO_read(sbio, tmpbuf, 1024);
111         if (len <= 0)
112             break;
113         BIO_write(out, tmpbuf, len);
114     }
115  end:
116     SSL_CONF_CTX_free(cctx);
117     BIO_free_all(sbio);
118     BIO_free(out);
119     NCONF_free(conf);
120     return 0;
121 }