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