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
main(int argc,char ** argv)14*e0c4386eSCy Schubert int main(int argc, char **argv)
15*e0c4386eSCy Schubert {
16*e0c4386eSCy Schubert BIO *sbio = NULL, *out = NULL;
17*e0c4386eSCy Schubert int len;
18*e0c4386eSCy Schubert char tmpbuf[1024];
19*e0c4386eSCy Schubert SSL_CTX *ctx;
20*e0c4386eSCy Schubert SSL_CONF_CTX *cctx;
21*e0c4386eSCy Schubert SSL *ssl;
22*e0c4386eSCy Schubert char **args = argv + 1;
23*e0c4386eSCy Schubert const char *connect_str = "localhost:4433";
24*e0c4386eSCy Schubert int nargs = argc - 1;
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert ctx = SSL_CTX_new(TLS_client_method());
27*e0c4386eSCy Schubert cctx = SSL_CONF_CTX_new();
28*e0c4386eSCy Schubert SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
29*e0c4386eSCy Schubert SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
30*e0c4386eSCy Schubert while (*args && **args == '-') {
31*e0c4386eSCy Schubert int rv;
32*e0c4386eSCy Schubert /* Parse standard arguments */
33*e0c4386eSCy Schubert rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
34*e0c4386eSCy Schubert if (rv == -3) {
35*e0c4386eSCy Schubert fprintf(stderr, "Missing argument for %s\n", *args);
36*e0c4386eSCy Schubert goto end;
37*e0c4386eSCy Schubert }
38*e0c4386eSCy Schubert if (rv < 0) {
39*e0c4386eSCy Schubert fprintf(stderr, "Error in command %s\n", *args);
40*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
41*e0c4386eSCy Schubert goto end;
42*e0c4386eSCy Schubert }
43*e0c4386eSCy Schubert /* If rv > 0 we processed something so proceed to next arg */
44*e0c4386eSCy Schubert if (rv > 0)
45*e0c4386eSCy Schubert continue;
46*e0c4386eSCy Schubert /* Otherwise application specific argument processing */
47*e0c4386eSCy Schubert if (strcmp(*args, "-connect") == 0) {
48*e0c4386eSCy Schubert connect_str = args[1];
49*e0c4386eSCy Schubert if (connect_str == NULL) {
50*e0c4386eSCy Schubert fprintf(stderr, "Missing -connect argument\n");
51*e0c4386eSCy Schubert goto end;
52*e0c4386eSCy Schubert }
53*e0c4386eSCy Schubert args += 2;
54*e0c4386eSCy Schubert nargs -= 2;
55*e0c4386eSCy Schubert continue;
56*e0c4386eSCy Schubert } else {
57*e0c4386eSCy Schubert fprintf(stderr, "Unknown argument %s\n", *args);
58*e0c4386eSCy Schubert goto end;
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert
62*e0c4386eSCy Schubert if (!SSL_CONF_CTX_finish(cctx)) {
63*e0c4386eSCy Schubert fprintf(stderr, "Finish error\n");
64*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
65*e0c4386eSCy Schubert goto end;
66*e0c4386eSCy Schubert }
67*e0c4386eSCy Schubert
68*e0c4386eSCy Schubert /*
69*e0c4386eSCy Schubert * We'd normally set some stuff like the verify paths and * mode here
70*e0c4386eSCy Schubert * because as things stand this will connect to * any server whose
71*e0c4386eSCy Schubert * certificate is signed by any CA.
72*e0c4386eSCy Schubert */
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert sbio = BIO_new_ssl_connect(ctx);
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert BIO_get_ssl(sbio, &ssl);
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert if (!ssl) {
79*e0c4386eSCy Schubert fprintf(stderr, "Can't locate SSL pointer\n");
80*e0c4386eSCy Schubert goto end;
81*e0c4386eSCy Schubert }
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert /* We might want to do other things with ssl here */
84*e0c4386eSCy Schubert
85*e0c4386eSCy Schubert BIO_set_conn_hostname(sbio, connect_str);
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert out = BIO_new_fp(stdout, BIO_NOCLOSE);
88*e0c4386eSCy Schubert if (BIO_do_connect(sbio) <= 0) {
89*e0c4386eSCy Schubert fprintf(stderr, "Error connecting to server\n");
90*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
91*e0c4386eSCy Schubert goto end;
92*e0c4386eSCy Schubert }
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert /* Could examine ssl here to get connection info */
95*e0c4386eSCy Schubert
96*e0c4386eSCy Schubert BIO_puts(sbio, "GET / HTTP/1.0\n\n");
97*e0c4386eSCy Schubert for (;;) {
98*e0c4386eSCy Schubert len = BIO_read(sbio, tmpbuf, 1024);
99*e0c4386eSCy Schubert if (len <= 0)
100*e0c4386eSCy Schubert break;
101*e0c4386eSCy Schubert BIO_write(out, tmpbuf, len);
102*e0c4386eSCy Schubert }
103*e0c4386eSCy Schubert end:
104*e0c4386eSCy Schubert SSL_CONF_CTX_free(cctx);
105*e0c4386eSCy Schubert BIO_free_all(sbio);
106*e0c4386eSCy Schubert BIO_free(out);
107*e0c4386eSCy Schubert return 0;
108*e0c4386eSCy Schubert }
109