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