|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
#include <openssl/err.h>
|
|
Packit Service |
084de1 |
#include <openssl/ssl.h>
|
|
Packit Service |
084de1 |
#include <openssl/conf.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int main(int argc, char **argv)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *sbio = NULL, *out = NULL;
|
|
Packit Service |
084de1 |
int i, len, rv;
|
|
Packit Service |
084de1 |
char tmpbuf[1024];
|
|
Packit Service |
084de1 |
SSL_CTX *ctx = NULL;
|
|
Packit Service |
084de1 |
SSL_CONF_CTX *cctx = NULL;
|
|
Packit Service |
084de1 |
SSL *ssl = NULL;
|
|
Packit Service |
084de1 |
CONF *conf = NULL;
|
|
Packit Service |
084de1 |
STACK_OF(CONF_VALUE) *sect = NULL;
|
|
Packit Service |
084de1 |
CONF_VALUE *cnf;
|
|
Packit Service |
084de1 |
const char *connect_str = "localhost:4433";
|
|
Packit Service |
084de1 |
long errline = -1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
conf = NCONF_new(NULL);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
|
|
Packit Service |
084de1 |
if (errline <= 0)
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error processing config file\n");
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error on line %ld\n", errline);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sect = NCONF_get_section(conf, "default");
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (sect == NULL) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error retrieving default section\n");
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ctx = SSL_CTX_new(TLS_client_method());
|
|
Packit Service |
084de1 |
cctx = SSL_CONF_CTX_new();
|
|
Packit Service |
084de1 |
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
|
|
Packit Service |
084de1 |
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
|
|
Packit Service |
084de1 |
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
|
|
Packit Service |
084de1 |
for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
|
|
Packit Service |
084de1 |
cnf = sk_CONF_VALUE_value(sect, i);
|
|
Packit Service |
084de1 |
rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
|
|
Packit Service |
084de1 |
if (rv > 0)
|
|
Packit Service |
084de1 |
continue;
|
|
Packit Service |
084de1 |
if (rv != -2) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error processing %s = %s\n",
|
|
Packit Service |
084de1 |
cnf->name, cnf->value);
|
|
Packit Service |
084de1 |
ERR_print_errors_fp(stderr);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (strcmp(cnf->name, "Connect") == 0) {
|
|
Packit Service |
084de1 |
connect_str = cnf->value;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!SSL_CONF_CTX_finish(cctx)) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Finish error\n");
|
|
Packit Service |
084de1 |
ERR_print_errors_fp(stderr);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* We'd normally set some stuff like the verify paths and * mode here
|
|
Packit Service |
084de1 |
* because as things stand this will connect to * any server whose
|
|
Packit Service |
084de1 |
* certificate is signed by any CA.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sbio = BIO_new_ssl_connect(ctx);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIO_get_ssl(sbio, &ssl;;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!ssl) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Can't locate SSL pointer\n");
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Don't want any retries */
|
|
Packit Service |
084de1 |
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* We might want to do other things with ssl here */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIO_set_conn_hostname(sbio, connect_str);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
if (BIO_do_connect(sbio) <= 0) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error connecting to server\n");
|
|
Packit Service |
084de1 |
ERR_print_errors_fp(stderr);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (BIO_do_handshake(sbio) <= 0) {
|
|
Packit Service |
084de1 |
fprintf(stderr, "Error establishing SSL connection\n");
|
|
Packit Service |
084de1 |
ERR_print_errors_fp(stderr);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Could examine ssl here to get connection info */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIO_puts(sbio, "GET / HTTP/1.0\n\n");
|
|
Packit Service |
084de1 |
for (;;) {
|
|
Packit Service |
084de1 |
len = BIO_read(sbio, tmpbuf, 1024);
|
|
Packit Service |
084de1 |
if (len <= 0)
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
BIO_write(out, tmpbuf, len);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
end:
|
|
Packit Service |
084de1 |
SSL_CONF_CTX_free(cctx);
|
|
Packit Service |
084de1 |
BIO_free_all(sbio);
|
|
Packit Service |
084de1 |
BIO_free(out);
|
|
Packit Service |
084de1 |
NCONF_free(conf);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|