blob: dd9d1767fdfee8d6ab97f29643151d2d9c078d65 [file] [log] [blame]
Adam Langleyaacec172014-06-20 12:00:00 -07001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/base.h>
16
Adam Langleyaacec172014-06-20 12:00:00 -070017#include <openssl/err.h>
David Benjamin05709232015-03-23 19:01:33 -040018#include <openssl/pem.h>
Adam Langleyaacec172014-06-20 12:00:00 -070019#include <openssl/ssl.h>
20
David Benjamin45fb1be2015-03-22 16:31:27 -040021#include "../crypto/test/scoped_types.h"
David Benjamin05709232015-03-23 19:01:33 -040022#include "../ssl/test/scoped_types.h"
Adam Langleyaacec172014-06-20 12:00:00 -070023#include "internal.h"
Dave Tapuskab8a824d2014-12-10 19:09:52 -050024#include "transport_common.h"
Adam Langleyaacec172014-06-20 12:00:00 -070025
26
27static const struct argument kArguments[] = {
28 {
David Benjamin05709232015-03-23 19:01:33 -040029 "-connect", kRequiredArgument,
Adam Langleyaacec172014-06-20 12:00:00 -070030 "The hostname and port of the server to connect to, e.g. foo.com:443",
31 },
32 {
David Benjamin05709232015-03-23 19:01:33 -040033 "-cipher", kOptionalArgument,
Adam Langley5f51c252014-10-28 15:45:39 -070034 "An OpenSSL-style cipher suite string that configures the offered ciphers",
35 },
36 {
David Benjamin05709232015-03-23 19:01:33 -040037 "-max-version", kOptionalArgument,
38 "The maximum acceptable protocol version",
39 },
40 {
41 "-min-version", kOptionalArgument,
42 "The minimum acceptable protocol version",
43 },
44 {
45 "-server-name", kOptionalArgument,
46 "The server name to advertise",
47 },
48 {
49 "-select-next-proto", kOptionalArgument,
50 "An NPN protocol to select if the server supports NPN",
51 },
52 {
53 "-alpn-protos", kOptionalArgument,
54 "A comma-separated list of ALPN protocols to advertise",
55 },
56 {
57 "-fallback-scsv", kBooleanArgument,
58 "Enable FALLBACK_SCSV",
59 },
60 {
61 "-ocsp-stapling", kBooleanArgument,
62 "Advertise support for OCSP stabling",
63 },
64 {
65 "-signed-certificate-timestamps", kBooleanArgument,
66 "Advertise support for signed certificate timestamps",
67 },
68 {
69 "-channel-id-key", kOptionalArgument,
70 "The key to use for signing a channel ID",
71 },
72 {
David Benjamin1043ac02015-06-04 15:26:04 -040073 "-false-start", kBooleanArgument,
74 "Enable False Start",
75 },
76 {
David Benjamin05709232015-03-23 19:01:33 -040077 "", kOptionalArgument, "",
Adam Langleyaacec172014-06-20 12:00:00 -070078 },
79};
80
David Benjamin05709232015-03-23 19:01:33 -040081static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
82 ScopedBIO bio(BIO_new(BIO_s_file()));
83 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
84 return nullptr;
85 }
86 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr,
87 nullptr));
88 return pkey;
89}
90
91static bool VersionFromString(uint16_t *out_version,
92 const std::string& version) {
93 if (version == "ssl3") {
94 *out_version = SSL3_VERSION;
95 return true;
96 } else if (version == "tls1" || version == "tls1.0") {
97 *out_version = TLS1_VERSION;
98 return true;
99 } else if (version == "tls1.1") {
100 *out_version = TLS1_1_VERSION;
101 return true;
102 } else if (version == "tls1.2") {
103 *out_version = TLS1_2_VERSION;
104 return true;
105 }
106 return false;
107}
108
109static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
110 const uint8_t* in, unsigned inlen, void* arg) {
111 *out = reinterpret_cast<uint8_t *>(arg);
112 *outlen = strlen(reinterpret_cast<const char *>(arg));
113 return SSL_TLSEXT_ERR_OK;
114}
115
Adam Langleyaacec172014-06-20 12:00:00 -0700116bool Client(const std::vector<std::string> &args) {
Brian Smith33970e62015-01-27 22:32:08 -0800117 if (!InitSocketLibrary()) {
118 return false;
119 }
120
Adam Langleyaacec172014-06-20 12:00:00 -0700121 std::map<std::string, std::string> args_map;
122
123 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
124 PrintUsage(kArguments);
125 return false;
126 }
127
David Benjamin05709232015-03-23 19:01:33 -0400128 ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_client_method()));
Adam Langleyaacec172014-06-20 12:00:00 -0700129
David Benjamin859ec3c2014-09-02 16:29:36 -0400130 const char *keylog_file = getenv("SSLKEYLOGFILE");
131 if (keylog_file) {
132 BIO *keylog_bio = BIO_new_file(keylog_file, "a");
133 if (!keylog_bio) {
134 ERR_print_errors_cb(PrintErrorCallback, stderr);
135 return false;
136 }
David Benjamin05709232015-03-23 19:01:33 -0400137 SSL_CTX_set_keylog_bio(ctx.get(), keylog_bio);
David Benjamin859ec3c2014-09-02 16:29:36 -0400138 }
139
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500140 if (args_map.count("-cipher") != 0 &&
David Benjamin05709232015-03-23 19:01:33 -0400141 !SSL_CTX_set_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500142 fprintf(stderr, "Failed setting cipher list\n");
143 return false;
Adam Langley5f51c252014-10-28 15:45:39 -0700144 }
145
David Benjamin05709232015-03-23 19:01:33 -0400146 if (args_map.count("-max-version") != 0) {
147 uint16_t version;
148 if (!VersionFromString(&version, args_map["-max-version"])) {
149 fprintf(stderr, "Unknown protocol version: '%s'\n",
150 args_map["-max-version"].c_str());
151 return false;
152 }
153 SSL_CTX_set_max_version(ctx.get(), version);
154 }
155
156 if (args_map.count("-min-version") != 0) {
157 uint16_t version;
158 if (!VersionFromString(&version, args_map["-min-version"])) {
159 fprintf(stderr, "Unknown protocol version: '%s'\n",
160 args_map["-min-version"].c_str());
161 return false;
162 }
163 SSL_CTX_set_min_version(ctx.get(), version);
164 }
165
166 if (args_map.count("-select-next-proto") != 0) {
167 const std::string &proto = args_map["-select-next-proto"];
168 if (proto.size() > 255) {
169 fprintf(stderr, "Bad NPN protocol: '%s'\n", proto.c_str());
170 return false;
171 }
172 // |SSL_CTX_set_next_proto_select_cb| is not const-correct.
173 SSL_CTX_set_next_proto_select_cb(ctx.get(), NextProtoSelectCallback,
174 const_cast<char *>(proto.c_str()));
175 }
176
177 if (args_map.count("-alpn-protos") != 0) {
178 const std::string &alpn_protos = args_map["-alpn-protos"];
179 std::vector<uint8_t> wire;
180 size_t i = 0;
181 while (i <= alpn_protos.size()) {
182 size_t j = alpn_protos.find(',', i);
183 if (j == std::string::npos) {
184 j = alpn_protos.size();
185 }
186 size_t len = j - i;
187 if (len > 255) {
188 fprintf(stderr, "Invalid ALPN protocols: '%s'\n", alpn_protos.c_str());
189 return false;
190 }
191 wire.push_back(static_cast<uint8_t>(len));
192 wire.resize(wire.size() + len);
193 memcpy(wire.data() + wire.size() - len, alpn_protos.data() + i, len);
194 i = j + 1;
195 }
196 if (SSL_CTX_set_alpn_protos(ctx.get(), wire.data(), wire.size()) != 0) {
197 return false;
198 }
199 }
200
201 if (args_map.count("-fallback-scsv") != 0) {
202 SSL_CTX_set_mode(ctx.get(), SSL_MODE_SEND_FALLBACK_SCSV);
203 }
204
205 if (args_map.count("-ocsp-stapling") != 0) {
206 SSL_CTX_enable_ocsp_stapling(ctx.get());
207 }
208
209 if (args_map.count("-signed-certificate-timestamps") != 0) {
210 SSL_CTX_enable_signed_cert_timestamps(ctx.get());
211 }
212
213 if (args_map.count("-channel-id-key") != 0) {
214 ScopedEVP_PKEY pkey = LoadPrivateKey(args_map["-channel-id-key"]);
215 if (!pkey || !SSL_CTX_set1_tls_channel_id(ctx.get(), pkey.get())) {
216 return false;
217 }
David Benjamin05709232015-03-23 19:01:33 -0400218 }
219
David Benjamin1043ac02015-06-04 15:26:04 -0400220 if (args_map.count("-false-start") != 0) {
221 SSL_CTX_set_mode(ctx.get(), SSL_MODE_ENABLE_FALSE_START);
222 }
223
Adam Langleybbb42ff2014-06-23 11:25:49 -0700224 int sock = -1;
Adam Langleyaacec172014-06-20 12:00:00 -0700225 if (!Connect(&sock, args_map["-connect"])) {
226 return false;
227 }
228
David Benjamin05709232015-03-23 19:01:33 -0400229 ScopedBIO bio(BIO_new_socket(sock, BIO_CLOSE));
230 ScopedSSL ssl(SSL_new(ctx.get()));
Adam Langleyaacec172014-06-20 12:00:00 -0700231
David Benjamin05709232015-03-23 19:01:33 -0400232 if (args_map.count("-server-name") != 0) {
233 SSL_set_tlsext_host_name(ssl.get(), args_map["-server-name"].c_str());
234 }
235
236 SSL_set_bio(ssl.get(), bio.get(), bio.get());
237 bio.release();
238
239 int ret = SSL_connect(ssl.get());
Adam Langleyaacec172014-06-20 12:00:00 -0700240 if (ret != 1) {
David Benjamin05709232015-03-23 19:01:33 -0400241 int ssl_err = SSL_get_error(ssl.get(), ret);
Adam Langleyaacec172014-06-20 12:00:00 -0700242 fprintf(stderr, "Error while connecting: %d\n", ssl_err);
243 ERR_print_errors_cb(PrintErrorCallback, stderr);
244 return false;
245 }
246
247 fprintf(stderr, "Connected.\n");
David Benjamin05709232015-03-23 19:01:33 -0400248 PrintConnectionInfo(ssl.get());
Adam Langleyaacec172014-06-20 12:00:00 -0700249
David Benjamin05709232015-03-23 19:01:33 -0400250 bool ok = TransferData(ssl.get(), sock);
Adam Langleyaacec172014-06-20 12:00:00 -0700251
Adam Langleyaacec172014-06-20 12:00:00 -0700252 return ok;
253}