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