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>
14 int main(int argc, char **argv)
16 BIO *sbio = NULL, *out = NULL;
22 char **args = argv + 1;
23 const char *connect_str = "localhost:4433";
26 ERR_load_crypto_strings();
27 ERR_load_SSL_strings();
30 ctx = SSL_CTX_new(TLS_client_method());
31 cctx = SSL_CONF_CTX_new();
32 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
33 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
34 while (*args && **args == '-') {
36 /* Parse standard arguments */
37 rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
39 fprintf(stderr, "Missing argument for %s\n", *args);
43 fprintf(stderr, "Error in command %s\n", *args);
44 ERR_print_errors_fp(stderr);
47 /* If rv > 0 we processed something so proceed to next arg */
50 /* Otherwise application specific argument processing */
51 if (strcmp(*args, "-connect") == 0) {
52 connect_str = args[1];
53 if (connect_str == NULL) {
54 fprintf(stderr, "Missing -connect argument\n");
61 fprintf(stderr, "Unknown argument %s\n", *args);
66 if (!SSL_CONF_CTX_finish(cctx)) {
67 fprintf(stderr, "Finish error\n");
68 ERR_print_errors_fp(stderr);
73 * We'd normally set some stuff like the verify paths and * mode here
74 * because as things stand this will connect to * any server whose
75 * certificate is signed by any CA.
78 sbio = BIO_new_ssl_connect(ctx);
80 BIO_get_ssl(sbio, &ssl);
83 fprintf(stderr, "Can't locate SSL pointer\n");
87 /* Don't want any retries */
88 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
90 /* We might want to do other things with ssl here */
92 BIO_set_conn_hostname(sbio, connect_str);
94 out = BIO_new_fp(stdout, BIO_NOCLOSE);
95 if (BIO_do_connect(sbio) <= 0) {
96 fprintf(stderr, "Error connecting to server\n");
97 ERR_print_errors_fp(stderr);
101 if (BIO_do_handshake(sbio) <= 0) {
102 fprintf(stderr, "Error establishing SSL connection\n");
103 ERR_print_errors_fp(stderr);
107 /* Could examine ssl here to get connection info */
109 BIO_puts(sbio, "GET / HTTP/1.0\n\n");
111 len = BIO_read(sbio, tmpbuf, 1024);
114 BIO_write(out, tmpbuf, len);
117 SSL_CONF_CTX_free(cctx);