Path: blob/main/crypto/openssl/demos/bio/client-conf.c
34868 views
/*1* Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <string.h>10#include <openssl/err.h>11#include <openssl/ssl.h>12#include <openssl/conf.h>1314int main(int argc, char **argv)15{16BIO *sbio = NULL, *out = NULL;17int i, len, rv;18char tmpbuf[1024];19SSL_CTX *ctx = NULL;20SSL_CONF_CTX *cctx = NULL;21SSL *ssl = NULL;22CONF *conf = NULL;23STACK_OF(CONF_VALUE) *sect = NULL;24CONF_VALUE *cnf;25const char *connect_str = "localhost:4433";26long errline = -1;27int ret = EXIT_FAILURE;2829conf = NCONF_new(NULL);3031if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {32if (errline <= 0)33fprintf(stderr, "Error processing config file\n");34else35fprintf(stderr, "Error on line %ld\n", errline);36goto end;37}3839sect = NCONF_get_section(conf, "default");4041if (sect == NULL) {42fprintf(stderr, "Error retrieving default section\n");43goto end;44}4546ctx = SSL_CTX_new(TLS_client_method());47cctx = SSL_CONF_CTX_new();48SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);49SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);50SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);51for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {52cnf = sk_CONF_VALUE_value(sect, i);53rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);54if (rv > 0)55continue;56if (rv != -2) {57fprintf(stderr, "Error processing %s = %s\n",58cnf->name, cnf->value);59ERR_print_errors_fp(stderr);60goto end;61}62if (strcmp(cnf->name, "Connect") == 0) {63connect_str = cnf->value;64} else {65fprintf(stderr, "Unknown configuration option %s\n", cnf->name);66goto end;67}68}6970if (!SSL_CONF_CTX_finish(cctx)) {71fprintf(stderr, "Finish error\n");72ERR_print_errors_fp(stderr);73goto end;74}7576/*77* We'd normally set some stuff like the verify paths and * mode here78* because as things stand this will connect to * any server whose79* certificate is signed by any CA.80*/8182sbio = BIO_new_ssl_connect(ctx);8384BIO_get_ssl(sbio, &ssl);8586if (!ssl) {87fprintf(stderr, "Can't locate SSL pointer\n");88goto end;89}9091/* We might want to do other things with ssl here */9293BIO_set_conn_hostname(sbio, connect_str);9495out = BIO_new_fp(stdout, BIO_NOCLOSE);96if (BIO_do_connect(sbio) <= 0) {97fprintf(stderr, "Error connecting to server\n");98ERR_print_errors_fp(stderr);99goto end;100}101102/* Could examine ssl here to get connection info */103104BIO_puts(sbio, "GET / HTTP/1.0\n\n");105for (;;) {106len = BIO_read(sbio, tmpbuf, 1024);107if (len <= 0)108break;109BIO_write(out, tmpbuf, len);110}111ret = EXIT_SUCCESS;112113end:114SSL_CONF_CTX_free(cctx);115BIO_free_all(sbio);116BIO_free(out);117NCONF_free(conf);118return ret;119}120121122