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