1 #include <openssl/err.h>
2 #include <openssl/ssl.h>
3 #include <openssl/conf.h>
5 int main(int argc, char **argv)
7 BIO *sbio = NULL, *out = NULL;
11 SSL_CONF_CTX *cctx = NULL;
14 STACK_OF(CONF_VALUE) *sect = NULL;
16 const char *connect_str = "localhost:4433";
19 ERR_load_crypto_strings();
20 ERR_load_SSL_strings();
23 conf = NCONF_new(NULL);
25 if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
27 fprintf(stderr, "Error processing config file\n");
29 fprintf(stderr, "Error on line %ld\n", errline);
33 sect = NCONF_get_section(conf, "default");
36 fprintf(stderr, "Error retrieving default section\n");
40 ctx = SSL_CTX_new(SSLv23_client_method());
41 cctx = SSL_CONF_CTX_new();
42 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
43 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
44 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
45 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
46 cnf = sk_CONF_VALUE_value(sect, i);
47 rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
51 fprintf(stderr, "Error processing %s = %s\n",
52 cnf->name, cnf->value);
53 ERR_print_errors_fp(stderr);
56 if (strcmp(cnf->name, "Connect") == 0) {
57 connect_str = cnf->value;
59 fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
64 if (!SSL_CONF_CTX_finish(cctx)) {
65 fprintf(stderr, "Finish error\n");
66 ERR_print_errors_fp(stderr);
71 * We'd normally set some stuff like the verify paths and * mode here
72 * because as things stand this will connect to * any server whose
73 * certificate is signed by any CA.
76 sbio = BIO_new_ssl_connect(ctx);
78 BIO_get_ssl(sbio, &ssl);
81 fprintf(stderr, "Can't locate SSL pointer\n");
85 /* Don't want any retries */
86 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
88 /* We might want to do other things with ssl here */
90 BIO_set_conn_hostname(sbio, connect_str);
92 out = BIO_new_fp(stdout, BIO_NOCLOSE);
93 if (BIO_do_connect(sbio) <= 0) {
94 fprintf(stderr, "Error connecting to server\n");
95 ERR_print_errors_fp(stderr);
99 if (BIO_do_handshake(sbio) <= 0) {
100 fprintf(stderr, "Error establishing SSL connection\n");
101 ERR_print_errors_fp(stderr);
105 /* Could examine ssl here to get connection info */
107 BIO_puts(sbio, "GET / HTTP/1.0\n\n");
109 len = BIO_read(sbio, tmpbuf, 1024);
112 BIO_write(out, tmpbuf, len);
115 SSL_CONF_CTX_free(cctx);