Rich Salz | 9e20068 | 2016-05-18 09:16:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 8 | */ |
| 9 | |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 10 | #include <string.h> |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 11 | #include <openssl/err.h> |
| 12 | #include <openssl/ssl.h> |
| 13 | |
| 14 | int main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 15 | { |
| 16 | BIO *sbio = NULL, *out = NULL; |
| 17 | int len; |
| 18 | char tmpbuf[1024]; |
| 19 | SSL_CTX *ctx; |
| 20 | SSL_CONF_CTX *cctx; |
| 21 | SSL *ssl; |
| 22 | char **args = argv + 1; |
| 23 | const char *connect_str = "localhost:4433"; |
| 24 | int nargs = argc - 1; |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 25 | |
Matt Caswell | 13c9bb3 | 2015-03-31 00:18:31 +0100 | [diff] [blame] | 26 | ctx = SSL_CTX_new(TLS_client_method()); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 27 | cctx = SSL_CONF_CTX_new(); |
| 28 | SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); |
| 29 | SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); |
| 30 | while (*args && **args == '-') { |
| 31 | int rv; |
| 32 | /* Parse standard arguments */ |
| 33 | rv = SSL_CONF_cmd_argv(cctx, &nargs, &args); |
| 34 | if (rv == -3) { |
| 35 | fprintf(stderr, "Missing argument for %s\n", *args); |
| 36 | goto end; |
| 37 | } |
| 38 | if (rv < 0) { |
| 39 | fprintf(stderr, "Error in command %s\n", *args); |
| 40 | ERR_print_errors_fp(stderr); |
| 41 | goto end; |
| 42 | } |
| 43 | /* If rv > 0 we processed something so proceed to next arg */ |
| 44 | if (rv > 0) |
| 45 | continue; |
| 46 | /* Otherwise application specific argument processing */ |
Rich Salz | 86885c2 | 2015-05-06 14:56:14 -0400 | [diff] [blame] | 47 | if (strcmp(*args, "-connect") == 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 48 | connect_str = args[1]; |
| 49 | if (connect_str == NULL) { |
| 50 | fprintf(stderr, "Missing -connect argument\n"); |
| 51 | goto end; |
| 52 | } |
| 53 | args += 2; |
| 54 | nargs -= 2; |
| 55 | continue; |
| 56 | } else { |
| 57 | fprintf(stderr, "Unknown argument %s\n", *args); |
| 58 | goto end; |
| 59 | } |
| 60 | } |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 61 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 62 | if (!SSL_CONF_CTX_finish(cctx)) { |
| 63 | fprintf(stderr, "Finish error\n"); |
| 64 | ERR_print_errors_fp(stderr); |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 65 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 66 | } |
Dr. Stephen Henson | ebd14bf | 2013-10-18 16:28:38 +0100 | [diff] [blame] | 67 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 68 | /* |
| 69 | * We'd normally set some stuff like the verify paths and * mode here |
| 70 | * because as things stand this will connect to * any server whose |
| 71 | * certificate is signed by any CA. |
| 72 | */ |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 73 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 74 | sbio = BIO_new_ssl_connect(ctx); |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 75 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 76 | BIO_get_ssl(sbio, &ssl); |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 77 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 78 | if (!ssl) { |
| 79 | fprintf(stderr, "Can't locate SSL pointer\n"); |
| 80 | goto end; |
| 81 | } |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 82 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 83 | /* Don't want any retries */ |
| 84 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 85 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | /* We might want to do other things with ssl here */ |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 87 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 88 | BIO_set_conn_hostname(sbio, connect_str); |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 89 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | out = BIO_new_fp(stdout, BIO_NOCLOSE); |
| 91 | if (BIO_do_connect(sbio) <= 0) { |
| 92 | fprintf(stderr, "Error connecting to server\n"); |
| 93 | ERR_print_errors_fp(stderr); |
| 94 | goto end; |
| 95 | } |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 96 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 97 | if (BIO_do_handshake(sbio) <= 0) { |
| 98 | fprintf(stderr, "Error establishing SSL connection\n"); |
| 99 | ERR_print_errors_fp(stderr); |
| 100 | goto end; |
| 101 | } |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 102 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 103 | /* Could examine ssl here to get connection info */ |
Dr. Stephen Henson | 3646578 | 2013-02-26 16:33:05 +0000 | [diff] [blame] | 104 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 105 | BIO_puts(sbio, "GET / HTTP/1.0\n\n"); |
| 106 | for (;;) { |
| 107 | len = BIO_read(sbio, tmpbuf, 1024); |
| 108 | if (len <= 0) |
| 109 | break; |
| 110 | BIO_write(out, tmpbuf, len); |
| 111 | } |
| 112 | end: |
| 113 | SSL_CONF_CTX_free(cctx); |
| 114 | BIO_free_all(sbio); |
| 115 | BIO_free(out); |
| 116 | return 0; |
| 117 | } |