blob: c09f4576656bdede50f2238cbae6dd942ce23057 [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
David Benjamind28f59c2015-11-17 22:32:50 -050017#include <stdio.h>
18
Adam Langleyaacec172014-06-20 12:00:00 -070019#include <openssl/err.h>
David Benjamin05709232015-03-23 19:01:33 -040020#include <openssl/pem.h>
Adam Langleyaacec172014-06-20 12:00:00 -070021#include <openssl/ssl.h>
22
David Benjamin45fb1be2015-03-22 16:31:27 -040023#include "../crypto/test/scoped_types.h"
David Benjamin05709232015-03-23 19:01:33 -040024#include "../ssl/test/scoped_types.h"
Adam Langleyaacec172014-06-20 12:00:00 -070025#include "internal.h"
Dave Tapuskab8a824d2014-12-10 19:09:52 -050026#include "transport_common.h"
Adam Langleyaacec172014-06-20 12:00:00 -070027
28
29static const struct argument kArguments[] = {
30 {
David Benjamin05709232015-03-23 19:01:33 -040031 "-connect", kRequiredArgument,
Adam Langleyaacec172014-06-20 12:00:00 -070032 "The hostname and port of the server to connect to, e.g. foo.com:443",
33 },
34 {
David Benjamin05709232015-03-23 19:01:33 -040035 "-cipher", kOptionalArgument,
Adam Langley5f51c252014-10-28 15:45:39 -070036 "An OpenSSL-style cipher suite string that configures the offered ciphers",
37 },
38 {
David Benjamin05709232015-03-23 19:01:33 -040039 "-max-version", kOptionalArgument,
40 "The maximum acceptable protocol version",
41 },
42 {
43 "-min-version", kOptionalArgument,
44 "The minimum acceptable protocol version",
45 },
46 {
47 "-server-name", kOptionalArgument,
48 "The server name to advertise",
49 },
50 {
51 "-select-next-proto", kOptionalArgument,
52 "An NPN protocol to select if the server supports NPN",
53 },
54 {
55 "-alpn-protos", kOptionalArgument,
56 "A comma-separated list of ALPN protocols to advertise",
57 },
58 {
59 "-fallback-scsv", kBooleanArgument,
60 "Enable FALLBACK_SCSV",
61 },
62 {
63 "-ocsp-stapling", kBooleanArgument,
64 "Advertise support for OCSP stabling",
65 },
66 {
67 "-signed-certificate-timestamps", kBooleanArgument,
68 "Advertise support for signed certificate timestamps",
69 },
70 {
71 "-channel-id-key", kOptionalArgument,
72 "The key to use for signing a channel ID",
73 },
74 {
David Benjamin1043ac02015-06-04 15:26:04 -040075 "-false-start", kBooleanArgument,
76 "Enable False Start",
77 },
David Benjamin621f95a2015-08-28 15:08:34 -040078 { "-session-in", kOptionalArgument,
79 "A file containing a session to resume.",
80 },
81 { "-session-out", kOptionalArgument,
82 "A file to write the negotiated session to.",
83 },
David Benjamin1043ac02015-06-04 15:26:04 -040084 {
David Benjamin05709232015-03-23 19:01:33 -040085 "", kOptionalArgument, "",
Adam Langleyaacec172014-06-20 12:00:00 -070086 },
87};
88
David Benjamin05709232015-03-23 19:01:33 -040089static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
90 ScopedBIO bio(BIO_new(BIO_s_file()));
91 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
92 return nullptr;
93 }
94 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr,
95 nullptr));
96 return pkey;
97}
98
99static bool VersionFromString(uint16_t *out_version,
100 const std::string& version) {
101 if (version == "ssl3") {
102 *out_version = SSL3_VERSION;
103 return true;
104 } else if (version == "tls1" || version == "tls1.0") {
105 *out_version = TLS1_VERSION;
106 return true;
107 } else if (version == "tls1.1") {
108 *out_version = TLS1_1_VERSION;
109 return true;
110 } else if (version == "tls1.2") {
111 *out_version = TLS1_2_VERSION;
112 return true;
113 }
114 return false;
115}
116
117static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
118 const uint8_t* in, unsigned inlen, void* arg) {
119 *out = reinterpret_cast<uint8_t *>(arg);
120 *outlen = strlen(reinterpret_cast<const char *>(arg));
121 return SSL_TLSEXT_ERR_OK;
122}
123
David Benjamind28f59c2015-11-17 22:32:50 -0500124static FILE *g_keylog_file = nullptr;
125
126static void KeyLogCallback(const SSL *ssl, const char *line) {
127 fprintf(g_keylog_file, "%s\n", line);
128 fflush(g_keylog_file);
129}
130
Adam Langleyaacec172014-06-20 12:00:00 -0700131bool Client(const std::vector<std::string> &args) {
Brian Smith33970e62015-01-27 22:32:08 -0800132 if (!InitSocketLibrary()) {
133 return false;
134 }
135
Adam Langleyaacec172014-06-20 12:00:00 -0700136 std::map<std::string, std::string> args_map;
137
138 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
139 PrintUsage(kArguments);
140 return false;
141 }
142
David Benjamin05709232015-03-23 19:01:33 -0400143 ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_client_method()));
Adam Langleyaacec172014-06-20 12:00:00 -0700144
David Benjamin859ec3c2014-09-02 16:29:36 -0400145 const char *keylog_file = getenv("SSLKEYLOGFILE");
146 if (keylog_file) {
David Benjamind28f59c2015-11-17 22:32:50 -0500147 g_keylog_file = fopen(keylog_file, "a");
148 if (g_keylog_file == nullptr) {
149 perror("fopen");
David Benjamin859ec3c2014-09-02 16:29:36 -0400150 return false;
151 }
David Benjamind28f59c2015-11-17 22:32:50 -0500152 SSL_CTX_set_keylog_callback(ctx.get(), KeyLogCallback);
David Benjamin859ec3c2014-09-02 16:29:36 -0400153 }
154
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500155 if (args_map.count("-cipher") != 0 &&
David Benjamin05709232015-03-23 19:01:33 -0400156 !SSL_CTX_set_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500157 fprintf(stderr, "Failed setting cipher list\n");
158 return false;
Adam Langley5f51c252014-10-28 15:45:39 -0700159 }
160
David Benjamin05709232015-03-23 19:01:33 -0400161 if (args_map.count("-max-version") != 0) {
162 uint16_t version;
163 if (!VersionFromString(&version, args_map["-max-version"])) {
164 fprintf(stderr, "Unknown protocol version: '%s'\n",
165 args_map["-max-version"].c_str());
166 return false;
167 }
168 SSL_CTX_set_max_version(ctx.get(), version);
169 }
170
171 if (args_map.count("-min-version") != 0) {
172 uint16_t version;
173 if (!VersionFromString(&version, args_map["-min-version"])) {
174 fprintf(stderr, "Unknown protocol version: '%s'\n",
175 args_map["-min-version"].c_str());
176 return false;
177 }
178 SSL_CTX_set_min_version(ctx.get(), version);
179 }
180
181 if (args_map.count("-select-next-proto") != 0) {
182 const std::string &proto = args_map["-select-next-proto"];
183 if (proto.size() > 255) {
184 fprintf(stderr, "Bad NPN protocol: '%s'\n", proto.c_str());
185 return false;
186 }
187 // |SSL_CTX_set_next_proto_select_cb| is not const-correct.
188 SSL_CTX_set_next_proto_select_cb(ctx.get(), NextProtoSelectCallback,
189 const_cast<char *>(proto.c_str()));
190 }
191
192 if (args_map.count("-alpn-protos") != 0) {
193 const std::string &alpn_protos = args_map["-alpn-protos"];
194 std::vector<uint8_t> wire;
195 size_t i = 0;
196 while (i <= alpn_protos.size()) {
197 size_t j = alpn_protos.find(',', i);
198 if (j == std::string::npos) {
199 j = alpn_protos.size();
200 }
201 size_t len = j - i;
202 if (len > 255) {
203 fprintf(stderr, "Invalid ALPN protocols: '%s'\n", alpn_protos.c_str());
204 return false;
205 }
206 wire.push_back(static_cast<uint8_t>(len));
207 wire.resize(wire.size() + len);
208 memcpy(wire.data() + wire.size() - len, alpn_protos.data() + i, len);
209 i = j + 1;
210 }
211 if (SSL_CTX_set_alpn_protos(ctx.get(), wire.data(), wire.size()) != 0) {
212 return false;
213 }
214 }
215
216 if (args_map.count("-fallback-scsv") != 0) {
217 SSL_CTX_set_mode(ctx.get(), SSL_MODE_SEND_FALLBACK_SCSV);
218 }
219
220 if (args_map.count("-ocsp-stapling") != 0) {
221 SSL_CTX_enable_ocsp_stapling(ctx.get());
222 }
223
224 if (args_map.count("-signed-certificate-timestamps") != 0) {
225 SSL_CTX_enable_signed_cert_timestamps(ctx.get());
226 }
227
228 if (args_map.count("-channel-id-key") != 0) {
229 ScopedEVP_PKEY pkey = LoadPrivateKey(args_map["-channel-id-key"]);
230 if (!pkey || !SSL_CTX_set1_tls_channel_id(ctx.get(), pkey.get())) {
231 return false;
232 }
David Benjamin05709232015-03-23 19:01:33 -0400233 }
234
David Benjamin1043ac02015-06-04 15:26:04 -0400235 if (args_map.count("-false-start") != 0) {
236 SSL_CTX_set_mode(ctx.get(), SSL_MODE_ENABLE_FALSE_START);
237 }
238
Adam Langleybbb42ff2014-06-23 11:25:49 -0700239 int sock = -1;
Adam Langleyaacec172014-06-20 12:00:00 -0700240 if (!Connect(&sock, args_map["-connect"])) {
241 return false;
242 }
243
David Benjamin05709232015-03-23 19:01:33 -0400244 ScopedBIO bio(BIO_new_socket(sock, BIO_CLOSE));
245 ScopedSSL ssl(SSL_new(ctx.get()));
Adam Langleyaacec172014-06-20 12:00:00 -0700246
David Benjamin05709232015-03-23 19:01:33 -0400247 if (args_map.count("-server-name") != 0) {
248 SSL_set_tlsext_host_name(ssl.get(), args_map["-server-name"].c_str());
249 }
250
David Benjamin621f95a2015-08-28 15:08:34 -0400251 if (args_map.count("-session-in") != 0) {
252 ScopedBIO in(BIO_new_file(args_map["-session-in"].c_str(), "rb"));
253 if (!in) {
254 fprintf(stderr, "Error reading session\n");
255 ERR_print_errors_cb(PrintErrorCallback, stderr);
256 return false;
257 }
258 ScopedSSL_SESSION session(PEM_read_bio_SSL_SESSION(in.get(), nullptr,
259 nullptr, nullptr));
260 if (!session) {
261 fprintf(stderr, "Error reading session\n");
262 ERR_print_errors_cb(PrintErrorCallback, stderr);
263 return false;
264 }
265 SSL_set_session(ssl.get(), session.get());
266 }
267
David Benjamin05709232015-03-23 19:01:33 -0400268 SSL_set_bio(ssl.get(), bio.get(), bio.get());
269 bio.release();
270
271 int ret = SSL_connect(ssl.get());
Adam Langleyaacec172014-06-20 12:00:00 -0700272 if (ret != 1) {
David Benjamin05709232015-03-23 19:01:33 -0400273 int ssl_err = SSL_get_error(ssl.get(), ret);
Adam Langleyaacec172014-06-20 12:00:00 -0700274 fprintf(stderr, "Error while connecting: %d\n", ssl_err);
275 ERR_print_errors_cb(PrintErrorCallback, stderr);
276 return false;
277 }
278
279 fprintf(stderr, "Connected.\n");
David Benjamin05709232015-03-23 19:01:33 -0400280 PrintConnectionInfo(ssl.get());
Adam Langleyaacec172014-06-20 12:00:00 -0700281
David Benjamin621f95a2015-08-28 15:08:34 -0400282 if (args_map.count("-session-out") != 0) {
283 ScopedBIO out(BIO_new_file(args_map["-session-out"].c_str(), "wb"));
284 if (!out ||
285 !PEM_write_bio_SSL_SESSION(out.get(), SSL_get0_session(ssl.get()))) {
286 fprintf(stderr, "Error while saving session:\n");
287 ERR_print_errors_cb(PrintErrorCallback, stderr);
288 return false;
289 }
290 }
291
David Benjamin05709232015-03-23 19:01:33 -0400292 bool ok = TransferData(ssl.get(), sock);
Adam Langleyaacec172014-06-20 12:00:00 -0700293
Adam Langleyaacec172014-06-20 12:00:00 -0700294 return ok;
295}