]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/client-conf.c
66b5cac47ce5594de68e13370634ed6e17d0718a
2 * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/err.h>
12 #include <openssl/ssl.h>
13 #include <openssl/conf.h>
15 int main(int argc
, char **argv
)
17 BIO
*sbio
= NULL
, *out
= NULL
;
21 SSL_CONF_CTX
*cctx
= NULL
;
24 STACK_OF(CONF_VALUE
) *sect
= NULL
;
26 const char *connect_str
= "localhost:4433";
29 ERR_load_crypto_strings();
30 ERR_load_SSL_strings();
33 conf
= NCONF_new(NULL
);
35 if (NCONF_load(conf
, "connect.cnf", &errline
) <= 0) {
37 fprintf(stderr
, "Error processing config file\n");
39 fprintf(stderr
, "Error on line %ld\n", errline
);
43 sect
= NCONF_get_section(conf
, "default");
46 fprintf(stderr
, "Error retrieving default section\n");
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
);
61 fprintf(stderr
, "Error processing %s = %s\n",
62 cnf
->name
, cnf
->value
);
63 ERR_print_errors_fp(stderr
);
66 if (strcmp(cnf
->name
, "Connect") == 0) {
67 connect_str
= cnf
->value
;
69 fprintf(stderr
, "Unknown configuration option %s\n", cnf
->name
);
74 if (!SSL_CONF_CTX_finish(cctx
)) {
75 fprintf(stderr
, "Finish error\n");
76 ERR_print_errors_fp(stderr
);
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.
86 sbio
= BIO_new_ssl_connect(ctx
);
88 BIO_get_ssl(sbio
, &ssl
);
91 fprintf(stderr
, "Can't locate SSL pointer\n");
95 /* Don't want any retries */
96 SSL_set_mode(ssl
, SSL_MODE_AUTO_RETRY
);
98 /* We might want to do other things with ssl here */
100 BIO_set_conn_hostname(sbio
, connect_str
);
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
);
109 if (BIO_do_handshake(sbio
) <= 0) {
110 fprintf(stderr
, "Error establishing SSL connection\n");
111 ERR_print_errors_fp(stderr
);
115 /* Could examine ssl here to get connection info */
117 BIO_puts(sbio
, "GET / HTTP/1.0\n\n");
119 len
= BIO_read(sbio
, tmpbuf
, 1024);
122 BIO_write(out
, tmpbuf
, len
);
125 SSL_CONF_CTX_free(cctx
);