Added support for ClientHello padding (RFC 7685) and fixed buffering bug.
[BearSSL] / tools / client.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <errno.h>
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "brssl.h"
40 #include "bearssl.h"
41
42 static int
43 host_connect(const char *host, const char *port, int verbose)
44 {
45 struct addrinfo hints, *si, *p;
46 int fd;
47 int err;
48
49 memset(&hints, 0, sizeof hints);
50 hints.ai_family = PF_UNSPEC;
51 hints.ai_socktype = SOCK_STREAM;
52 err = getaddrinfo(host, port, &hints, &si);
53 if (err != 0) {
54 fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
55 gai_strerror(err));
56 return -1;
57 }
58 fd = -1;
59 for (p = si; p != NULL; p = p->ai_next) {
60 if (verbose) {
61 struct sockaddr *sa;
62 void *addr;
63 char tmp[INET6_ADDRSTRLEN + 50];
64
65 sa = (struct sockaddr *)p->ai_addr;
66 if (sa->sa_family == AF_INET) {
67 addr = &((struct sockaddr_in *)sa)->sin_addr;
68 } else if (sa->sa_family == AF_INET6) {
69 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
70 } else {
71 addr = NULL;
72 }
73 if (addr != NULL) {
74 if (!inet_ntop(p->ai_family, addr,
75 tmp, sizeof tmp))
76 {
77 strcpy(tmp, "<invalid>");
78 }
79 } else {
80 sprintf(tmp, "<unknown family: %d>",
81 (int)sa->sa_family);
82 }
83 fprintf(stderr, "connecting to: %s\n", tmp);
84 }
85 fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
86 if (fd < 0) {
87 if (verbose) {
88 perror("socket()");
89 }
90 continue;
91 }
92 if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
93 if (verbose) {
94 perror("connect()");
95 }
96 close(fd);
97 continue;
98 }
99 break;
100 }
101 if (p == NULL) {
102 freeaddrinfo(si);
103 fprintf(stderr, "ERROR: failed to connect\n");
104 return -1;
105 }
106 freeaddrinfo(si);
107 if (verbose) {
108 fprintf(stderr, "connected.\n");
109 }
110
111 /*
112 * We make the socket non-blocking, since we are going to use
113 * poll() to organise I/O.
114 */
115 fcntl(fd, F_SETFL, O_NONBLOCK);
116 return fd;
117 }
118
119 static void
120 usage_client(void)
121 {
122 fprintf(stderr,
123 "usage: brssl client server[:port] [ options ]\n");
124 fprintf(stderr,
125 "options:\n");
126 fprintf(stderr,
127 " -q suppress verbose messages\n");
128 fprintf(stderr,
129 " -trace activate extra debug messages (dump of all packets)\n");
130 fprintf(stderr,
131 " -sni name use this specific name for SNI\n");
132 fprintf(stderr,
133 " -nosni do not send any SNI\n");
134 fprintf(stderr,
135 " -mono use monodirectional buffering\n");
136 fprintf(stderr,
137 " -buf length set the I/O buffer length (in bytes)\n");
138 fprintf(stderr,
139 " -CA file add certificates in 'file' to trust anchors\n");
140 fprintf(stderr,
141 " -list list supported names (protocols, algorithms...)\n");
142 fprintf(stderr,
143 " -vmin name set minimum supported version (default: TLS-1.0)\n");
144 fprintf(stderr,
145 " -vmax name set maximum supported version (default: TLS-1.2)\n");
146 fprintf(stderr,
147 " -cs names set list of supported cipher suites (comma-separated)\n");
148 fprintf(stderr,
149 " -hf names add support for some hash functions (comma-separated)\n");
150 fprintf(stderr,
151 " -minhello len set minimum ClientHello length (in bytes)\n");
152 }
153
154 /* see brssl.h */
155 int
156 do_client(int argc, char *argv[])
157 {
158 int retcode;
159 int verbose;
160 int trace;
161 int i, bidi;
162 const char *server_name;
163 char *host;
164 char *port;
165 const char *sni;
166 anchor_list anchors = VEC_INIT;
167 unsigned vmin, vmax;
168 cipher_suite *suites;
169 size_t num_suites;
170 uint16_t *suite_ids;
171 unsigned hfuns;
172 size_t u;
173 br_ssl_client_context cc;
174 br_x509_minimal_context xc;
175 x509_noanchor_context xwc;
176 const br_hash_class *dnhash;
177 unsigned char *iobuf;
178 size_t iobuf_len;
179 size_t minhello_len;
180 int fd;
181
182 retcode = 0;
183 verbose = 1;
184 trace = 0;
185 server_name = NULL;
186 host = NULL;
187 port = NULL;
188 sni = NULL;
189 bidi = 1;
190 vmin = 0;
191 vmax = 0;
192 suites = NULL;
193 num_suites = 0;
194 hfuns = 0;
195 suite_ids = NULL;
196 iobuf = NULL;
197 iobuf_len = 0;
198 minhello_len = (size_t)-1;
199 fd = -1;
200 for (i = 0; i < argc; i ++) {
201 const char *arg;
202
203 arg = argv[i];
204 if (arg[0] != '-') {
205 if (server_name != NULL) {
206 fprintf(stderr,
207 "ERROR: duplicate server name\n");
208 usage_client();
209 goto client_exit_error;
210 }
211 server_name = arg;
212 continue;
213 }
214 if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
215 verbose = 1;
216 } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
217 verbose = 0;
218 } else if (eqstr(arg, "-trace")) {
219 trace = 1;
220 } else if (eqstr(arg, "-sni")) {
221 if (++ i >= argc) {
222 fprintf(stderr,
223 "ERROR: no argument for '-sni'\n");
224 usage_client();
225 goto client_exit_error;
226 }
227 if (sni != NULL) {
228 fprintf(stderr, "ERROR: duplicate SNI\n");
229 usage_client();
230 goto client_exit_error;
231 }
232 sni = argv[i];
233 } else if (eqstr(arg, "-nosni")) {
234 if (sni != NULL) {
235 fprintf(stderr, "ERROR: duplicate SNI\n");
236 usage_client();
237 goto client_exit_error;
238 }
239 sni = "";
240 } else if (eqstr(arg, "-mono")) {
241 bidi = 0;
242 } else if (eqstr(arg, "-buf")) {
243 if (++ i >= argc) {
244 fprintf(stderr,
245 "ERROR: no argument for '-buf'\n");
246 usage_client();
247 goto client_exit_error;
248 }
249 arg = argv[i];
250 if (iobuf_len != 0) {
251 fprintf(stderr,
252 "ERROR: duplicate I/O buffer length\n");
253 usage_client();
254 goto client_exit_error;
255 }
256 iobuf_len = parse_size(arg);
257 if (iobuf_len == (size_t)-1) {
258 usage_client();
259 goto client_exit_error;
260 }
261 } else if (eqstr(arg, "-CA")) {
262 if (++ i >= argc) {
263 fprintf(stderr,
264 "ERROR: no argument for '-CA'\n");
265 usage_client();
266 goto client_exit_error;
267 }
268 arg = argv[i];
269 if (read_trust_anchors(&anchors, arg) == 0) {
270 usage_client();
271 goto client_exit_error;
272 }
273 } else if (eqstr(arg, "-list")) {
274 list_names();
275 goto client_exit;
276 } else if (eqstr(arg, "-vmin")) {
277 if (++ i >= argc) {
278 fprintf(stderr,
279 "ERROR: no argument for '-vmin'\n");
280 usage_client();
281 goto client_exit_error;
282 }
283 arg = argv[i];
284 if (vmin != 0) {
285 fprintf(stderr,
286 "ERROR: duplicate minimum version\n");
287 usage_client();
288 goto client_exit_error;
289 }
290 vmin = parse_version(arg, strlen(arg));
291 if (vmin == 0) {
292 fprintf(stderr,
293 "ERROR: unrecognised version '%s'\n",
294 arg);
295 usage_client();
296 goto client_exit_error;
297 }
298 } else if (eqstr(arg, "-vmax")) {
299 if (++ i >= argc) {
300 fprintf(stderr,
301 "ERROR: no argument for '-vmax'\n");
302 usage_client();
303 goto client_exit_error;
304 }
305 arg = argv[i];
306 if (vmax != 0) {
307 fprintf(stderr,
308 "ERROR: duplicate maximum version\n");
309 usage_client();
310 goto client_exit_error;
311 }
312 vmax = parse_version(arg, strlen(arg));
313 if (vmax == 0) {
314 fprintf(stderr,
315 "ERROR: unrecognised version '%s'\n",
316 arg);
317 usage_client();
318 goto client_exit_error;
319 }
320 } else if (eqstr(arg, "-cs")) {
321 if (++ i >= argc) {
322 fprintf(stderr,
323 "ERROR: no argument for '-cs'\n");
324 usage_client();
325 goto client_exit_error;
326 }
327 arg = argv[i];
328 if (suites != NULL) {
329 fprintf(stderr, "ERROR: duplicate list"
330 " of cipher suites\n");
331 usage_client();
332 goto client_exit_error;
333 }
334 suites = parse_suites(arg, &num_suites);
335 if (suites == NULL) {
336 usage_client();
337 goto client_exit_error;
338 }
339 } else if (eqstr(arg, "-hf")) {
340 unsigned x;
341
342 if (++ i >= argc) {
343 fprintf(stderr,
344 "ERROR: no argument for '-hf'\n");
345 usage_client();
346 goto client_exit_error;
347 }
348 arg = argv[i];
349 x = parse_hash_functions(arg);
350 if (x == 0) {
351 usage_client();
352 goto client_exit_error;
353 }
354 hfuns |= x;
355 } else if (eqstr(arg, "-minhello")) {
356 if (++ i >= argc) {
357 fprintf(stderr,
358 "ERROR: no argument for '-minhello'\n");
359 usage_client();
360 goto client_exit_error;
361 }
362 arg = argv[i];
363 if (minhello_len != (size_t)-1) {
364 fprintf(stderr, "ERROR: duplicate minium"
365 " ClientHello length\n");
366 usage_client();
367 goto client_exit_error;
368 }
369 minhello_len = parse_size(arg);
370 /*
371 * Minimum ClientHello length must fit on 16 bits.
372 */
373 if (minhello_len == (size_t)-1
374 || (((minhello_len >> 12) >> 4) != 0))
375 {
376 usage_client();
377 goto client_exit_error;
378 }
379 } else {
380 fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
381 usage_client();
382 goto client_exit_error;
383 }
384 }
385 if (server_name == NULL) {
386 fprintf(stderr, "ERROR: no server name/address provided\n");
387 usage_client();
388 goto client_exit_error;
389 }
390 for (u = strlen(server_name); u > 0; u --) {
391 int c = server_name[u - 1];
392 if (c == ':') {
393 break;
394 }
395 if (c < '0' || c > '9') {
396 u = 0;
397 break;
398 }
399 }
400 if (u == 0) {
401 host = xstrdup(server_name);
402 port = "443";
403 } else {
404 port = xstrdup(server_name + u);
405 host = xmalloc(u);
406 memcpy(host, server_name, u - 1);
407 host[u - 1] = 0;
408 }
409 if (sni == NULL) {
410 sni = host;
411 }
412
413 if (vmin == 0) {
414 vmin = BR_TLS10;
415 }
416 if (vmax == 0) {
417 vmax = BR_TLS12;
418 }
419 if (vmax < vmin) {
420 fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
421 " version combination\n");
422 usage_client();
423 goto client_exit_error;
424 }
425 if (suites == NULL) {
426 num_suites = 0;
427
428 for (u = 0; cipher_suites[u].name; u ++) {
429 if ((cipher_suites[u].req & REQ_TLS12) == 0
430 || vmax >= BR_TLS12)
431 {
432 num_suites ++;
433 }
434 }
435 suites = xmalloc(num_suites * sizeof *suites);
436 num_suites = 0;
437 for (u = 0; cipher_suites[u].name; u ++) {
438 if ((cipher_suites[u].req & REQ_TLS12) == 0
439 || vmax >= BR_TLS12)
440 {
441 suites[num_suites ++] = cipher_suites[u];
442 }
443 }
444 }
445 if (hfuns == 0) {
446 hfuns = (unsigned)-1;
447 }
448 if (iobuf_len == 0) {
449 if (bidi) {
450 iobuf_len = BR_SSL_BUFSIZE_BIDI;
451 } else {
452 iobuf_len = BR_SSL_BUFSIZE_MONO;
453 }
454 }
455 iobuf = xmalloc(iobuf_len);
456
457 /*
458 * Compute implementation requirements and inject implementations.
459 */
460 suite_ids = xmalloc(num_suites * sizeof *suite_ids);
461 br_ssl_client_zero(&cc);
462 br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
463 dnhash = NULL;
464 for (u = 0; hash_functions[u].name; u ++) {
465 const br_hash_class *hc;
466 int id;
467
468 hc = hash_functions[u].hclass;
469 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
470 if ((hfuns & ((unsigned)1 << id)) != 0) {
471 dnhash = hc;
472 }
473 }
474 if (dnhash == NULL) {
475 fprintf(stderr, "ERROR: no supported hash function\n");
476 goto client_exit_error;
477 }
478 br_x509_minimal_init(&xc, dnhash,
479 &VEC_ELT(anchors, 0), VEC_LEN(anchors));
480 if (vmin <= BR_TLS11) {
481 if (!(hfuns & (1 << br_md5_ID))) {
482 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
483 goto client_exit_error;
484 }
485 if (!(hfuns & (1 << br_sha1_ID))) {
486 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
487 goto client_exit_error;
488 }
489 }
490 for (u = 0; u < num_suites; u ++) {
491 unsigned req;
492
493 req = suites[u].req;
494 suite_ids[u] = suites[u].suite;
495 if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
496 fprintf(stderr,
497 "ERROR: cipher suite %s requires TLS 1.2\n",
498 suites[u].name);
499 goto client_exit_error;
500 }
501 if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
502 fprintf(stderr,
503 "ERROR: cipher suite %s requires SHA-1\n",
504 suites[u].name);
505 goto client_exit_error;
506 }
507 if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
508 fprintf(stderr,
509 "ERROR: cipher suite %s requires SHA-256\n",
510 suites[u].name);
511 goto client_exit_error;
512 }
513 if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
514 fprintf(stderr,
515 "ERROR: cipher suite %s requires SHA-384\n",
516 suites[u].name);
517 goto client_exit_error;
518 }
519 /* TODO: algorithm implementation selection */
520 if ((req & REQ_AESCBC) != 0) {
521 br_ssl_engine_set_aes_cbc(&cc.eng,
522 &br_aes_ct_cbcenc_vtable,
523 &br_aes_ct_cbcdec_vtable);
524 br_ssl_engine_set_cbc(&cc.eng,
525 &br_sslrec_in_cbc_vtable,
526 &br_sslrec_out_cbc_vtable);
527 }
528 if ((req & REQ_AESGCM) != 0) {
529 br_ssl_engine_set_aes_ctr(&cc.eng,
530 &br_aes_ct_ctr_vtable);
531 br_ssl_engine_set_ghash(&cc.eng,
532 &br_ghash_ctmul);
533 br_ssl_engine_set_gcm(&cc.eng,
534 &br_sslrec_in_gcm_vtable,
535 &br_sslrec_out_gcm_vtable);
536 }
537 if ((req & REQ_3DESCBC) != 0) {
538 br_ssl_engine_set_des_cbc(&cc.eng,
539 &br_des_ct_cbcenc_vtable,
540 &br_des_ct_cbcdec_vtable);
541 br_ssl_engine_set_cbc(&cc.eng,
542 &br_sslrec_in_cbc_vtable,
543 &br_sslrec_out_cbc_vtable);
544 }
545 if ((req & REQ_RSAKEYX) != 0) {
546 br_ssl_client_set_rsapub(&cc, &br_rsa_i31_public);
547 }
548 if ((req & REQ_ECDHE_RSA) != 0) {
549 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
550 br_ssl_client_set_rsavrfy(&cc, &br_rsa_i31_pkcs1_vrfy);
551 }
552 if ((req & REQ_ECDHE_ECDSA) != 0) {
553 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
554 br_ssl_client_set_ecdsa(&cc, &br_ecdsa_i31_vrfy_asn1);
555 }
556 if ((req & REQ_ECDH) != 0) {
557 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
558 }
559 }
560 br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
561
562 for (u = 0; hash_functions[u].name; u ++) {
563 const br_hash_class *hc;
564 int id;
565
566 hc = hash_functions[u].hclass;
567 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
568 if ((hfuns & ((unsigned)1 << id)) != 0) {
569 br_ssl_engine_set_hash(&cc.eng, id, hc);
570 br_x509_minimal_set_hash(&xc, id, hc);
571 }
572 }
573 if (vmin <= BR_TLS11) {
574 br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
575 }
576 if (vmax >= BR_TLS12) {
577 if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
578 br_ssl_engine_set_prf_sha256(&cc.eng,
579 &br_tls12_sha256_prf);
580 }
581 if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
582 br_ssl_engine_set_prf_sha384(&cc.eng,
583 &br_tls12_sha384_prf);
584 }
585 }
586 br_x509_minimal_set_rsa(&xc, &br_rsa_i31_pkcs1_vrfy);
587 br_x509_minimal_set_ecdsa(&xc,
588 &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
589
590 /*
591 * If there is no provided trust anchor, then certificate validation
592 * will always fail. In that situation, we use our custom wrapper
593 * that tolerates unknown anchors.
594 */
595 if (VEC_LEN(anchors) == 0) {
596 if (verbose) {
597 fprintf(stderr,
598 "WARNING: no configured trust anchor\n");
599 }
600 x509_noanchor_init(&xwc, &xc.vtable);
601 br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
602 } else {
603 br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
604 }
605
606 if (minhello_len != (size_t)-1) {
607 br_ssl_client_set_min_clienthello_len(&cc, minhello_len);
608 }
609
610 br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
611 br_ssl_client_reset(&cc, sni, 0);
612
613 /*
614 * Connect to the peer.
615 */
616 fd = host_connect(host, port, verbose);
617 if (fd < 0) {
618 goto client_exit_error;
619 }
620
621 /*
622 * Run the engine until completion.
623 */
624 if (run_ssl_engine(&cc.eng, fd,
625 (verbose ? RUN_ENGINE_VERBOSE : 0)
626 | (trace ? RUN_ENGINE_TRACE : 0)) != 0)
627 {
628 goto client_exit_error;
629 } else {
630 goto client_exit;
631 }
632
633 /*
634 * Release allocated structures.
635 */
636 client_exit:
637 xfree(host);
638 xfree(suites);
639 xfree(suite_ids);
640 VEC_CLEAREXT(anchors, &free_ta_contents);
641 xfree(iobuf);
642 if (fd >= 0) {
643 close(fd);
644 }
645 return retcode;
646
647 client_exit_error:
648 retcode = -1;
649 goto client_exit;
650 }