]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/client-conf.c
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 conf
= NCONF_new(NULL
);
31 if (NCONF_load(conf
, "connect.cnf", &errline
) <= 0) {
33 fprintf(stderr
, "Error processing config file\n");
35 fprintf(stderr
, "Error on line %ld\n", errline
);
39 sect
= NCONF_get_section(conf
, "default");
42 fprintf(stderr
, "Error retrieving default section\n");
46 ctx
= SSL_CTX_new(TLS_client_method());
47 cctx
= SSL_CONF_CTX_new();
48 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_CLIENT
);
49 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_FILE
);
50 SSL_CONF_CTX_set_ssl_ctx(cctx
, ctx
);
51 for (i
= 0; i
< sk_CONF_VALUE_num(sect
); i
++) {
52 cnf
= sk_CONF_VALUE_value(sect
, i
);
53 rv
= SSL_CONF_cmd(cctx
, cnf
->name
, cnf
->value
);
57 fprintf(stderr
, "Error processing %s = %s\n",
58 cnf
->name
, cnf
->value
);
59 ERR_print_errors_fp(stderr
);
62 if (strcmp(cnf
->name
, "Connect") == 0) {
63 connect_str
= cnf
->value
;
65 fprintf(stderr
, "Unknown configuration option %s\n", cnf
->name
);
70 if (!SSL_CONF_CTX_finish(cctx
)) {
71 fprintf(stderr
, "Finish error\n");
72 ERR_print_errors_fp(stderr
);
77 * We'd normally set some stuff like the verify paths and * mode here
78 * because as things stand this will connect to * any server whose
79 * certificate is signed by any CA.
82 sbio
= BIO_new_ssl_connect(ctx
);
84 BIO_get_ssl(sbio
, &ssl
);
87 fprintf(stderr
, "Can't locate SSL pointer\n");
91 /* Don't want any retries */
92 SSL_set_mode(ssl
, SSL_MODE_AUTO_RETRY
);
94 /* We might want to do other things with ssl here */
96 BIO_set_conn_hostname(sbio
, connect_str
);
98 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
99 if (BIO_do_connect(sbio
) <= 0) {
100 fprintf(stderr
, "Error connecting to server\n");
101 ERR_print_errors_fp(stderr
);
105 if (BIO_do_handshake(sbio
) <= 0) {
106 fprintf(stderr
, "Error establishing SSL connection\n");
107 ERR_print_errors_fp(stderr
);
111 /* Could examine ssl here to get connection info */
113 BIO_puts(sbio
, "GET / HTTP/1.0\n\n");
115 len
= BIO_read(sbio
, tmpbuf
, 1024);
118 BIO_write(out
, tmpbuf
, len
);
121 SSL_CONF_CTX_free(cctx
);