Fixed buffer overflow, and also NULL pointer dereference, in ECDSA signature handling.
[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 fprintf(stderr,
156 " -noreneg prohibit renegotiations\n");
157 }
158
159 /* see brssl.h */
160 int
161 do_client(int argc, char *argv[])
162 {
163 int retcode;
164 int verbose;
165 int trace;
166 int i, bidi;
167 const char *server_name;
168 char *host;
169 char *port;
170 const char *sni;
171 anchor_list anchors = VEC_INIT;
172 unsigned vmin, vmax;
173 cipher_suite *suites;
174 size_t num_suites;
175 uint16_t *suite_ids;
176 unsigned hfuns;
177 size_t u;
178 br_ssl_client_context cc;
179 br_x509_minimal_context xc;
180 x509_noanchor_context xwc;
181 const br_hash_class *dnhash;
182 unsigned char *iobuf;
183 size_t iobuf_len;
184 size_t minhello_len;
185 int fallback;
186 uint32_t flags;
187 int fd;
188
189 retcode = 0;
190 verbose = 1;
191 trace = 0;
192 server_name = NULL;
193 host = NULL;
194 port = NULL;
195 sni = NULL;
196 bidi = 1;
197 vmin = 0;
198 vmax = 0;
199 suites = NULL;
200 num_suites = 0;
201 hfuns = 0;
202 suite_ids = NULL;
203 iobuf = NULL;
204 iobuf_len = 0;
205 minhello_len = (size_t)-1;
206 fallback = 0;
207 flags = 0;
208 fd = -1;
209 for (i = 0; i < argc; i ++) {
210 const char *arg;
211
212 arg = argv[i];
213 if (arg[0] != '-') {
214 if (server_name != NULL) {
215 fprintf(stderr,
216 "ERROR: duplicate server name\n");
217 usage_client();
218 goto client_exit_error;
219 }
220 server_name = arg;
221 continue;
222 }
223 if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
224 verbose = 1;
225 } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
226 verbose = 0;
227 } else if (eqstr(arg, "-trace")) {
228 trace = 1;
229 } else if (eqstr(arg, "-sni")) {
230 if (++ i >= argc) {
231 fprintf(stderr,
232 "ERROR: no argument for '-sni'\n");
233 usage_client();
234 goto client_exit_error;
235 }
236 if (sni != NULL) {
237 fprintf(stderr, "ERROR: duplicate SNI\n");
238 usage_client();
239 goto client_exit_error;
240 }
241 sni = argv[i];
242 } else if (eqstr(arg, "-nosni")) {
243 if (sni != NULL) {
244 fprintf(stderr, "ERROR: duplicate SNI\n");
245 usage_client();
246 goto client_exit_error;
247 }
248 sni = "";
249 } else if (eqstr(arg, "-mono")) {
250 bidi = 0;
251 } else if (eqstr(arg, "-buf")) {
252 if (++ i >= argc) {
253 fprintf(stderr,
254 "ERROR: no argument for '-buf'\n");
255 usage_client();
256 goto client_exit_error;
257 }
258 arg = argv[i];
259 if (iobuf_len != 0) {
260 fprintf(stderr,
261 "ERROR: duplicate I/O buffer length\n");
262 usage_client();
263 goto client_exit_error;
264 }
265 iobuf_len = parse_size(arg);
266 if (iobuf_len == (size_t)-1) {
267 usage_client();
268 goto client_exit_error;
269 }
270 } else if (eqstr(arg, "-CA")) {
271 if (++ i >= argc) {
272 fprintf(stderr,
273 "ERROR: no argument for '-CA'\n");
274 usage_client();
275 goto client_exit_error;
276 }
277 arg = argv[i];
278 if (read_trust_anchors(&anchors, arg) == 0) {
279 usage_client();
280 goto client_exit_error;
281 }
282 } else if (eqstr(arg, "-list")) {
283 list_names();
284 goto client_exit;
285 } else if (eqstr(arg, "-vmin")) {
286 if (++ i >= argc) {
287 fprintf(stderr,
288 "ERROR: no argument for '-vmin'\n");
289 usage_client();
290 goto client_exit_error;
291 }
292 arg = argv[i];
293 if (vmin != 0) {
294 fprintf(stderr,
295 "ERROR: duplicate minimum version\n");
296 usage_client();
297 goto client_exit_error;
298 }
299 vmin = parse_version(arg, strlen(arg));
300 if (vmin == 0) {
301 fprintf(stderr,
302 "ERROR: unrecognised version '%s'\n",
303 arg);
304 usage_client();
305 goto client_exit_error;
306 }
307 } else if (eqstr(arg, "-vmax")) {
308 if (++ i >= argc) {
309 fprintf(stderr,
310 "ERROR: no argument for '-vmax'\n");
311 usage_client();
312 goto client_exit_error;
313 }
314 arg = argv[i];
315 if (vmax != 0) {
316 fprintf(stderr,
317 "ERROR: duplicate maximum version\n");
318 usage_client();
319 goto client_exit_error;
320 }
321 vmax = parse_version(arg, strlen(arg));
322 if (vmax == 0) {
323 fprintf(stderr,
324 "ERROR: unrecognised version '%s'\n",
325 arg);
326 usage_client();
327 goto client_exit_error;
328 }
329 } else if (eqstr(arg, "-cs")) {
330 if (++ i >= argc) {
331 fprintf(stderr,
332 "ERROR: no argument for '-cs'\n");
333 usage_client();
334 goto client_exit_error;
335 }
336 arg = argv[i];
337 if (suites != NULL) {
338 fprintf(stderr, "ERROR: duplicate list"
339 " of cipher suites\n");
340 usage_client();
341 goto client_exit_error;
342 }
343 suites = parse_suites(arg, &num_suites);
344 if (suites == NULL) {
345 usage_client();
346 goto client_exit_error;
347 }
348 } else if (eqstr(arg, "-hf")) {
349 unsigned x;
350
351 if (++ i >= argc) {
352 fprintf(stderr,
353 "ERROR: no argument for '-hf'\n");
354 usage_client();
355 goto client_exit_error;
356 }
357 arg = argv[i];
358 x = parse_hash_functions(arg);
359 if (x == 0) {
360 usage_client();
361 goto client_exit_error;
362 }
363 hfuns |= x;
364 } else if (eqstr(arg, "-minhello")) {
365 if (++ i >= argc) {
366 fprintf(stderr,
367 "ERROR: no argument for '-minhello'\n");
368 usage_client();
369 goto client_exit_error;
370 }
371 arg = argv[i];
372 if (minhello_len != (size_t)-1) {
373 fprintf(stderr, "ERROR: duplicate minium"
374 " ClientHello length\n");
375 usage_client();
376 goto client_exit_error;
377 }
378 minhello_len = parse_size(arg);
379 /*
380 * Minimum ClientHello length must fit on 16 bits.
381 */
382 if (minhello_len == (size_t)-1
383 || (((minhello_len >> 12) >> 4) != 0))
384 {
385 usage_client();
386 goto client_exit_error;
387 }
388 } else if (eqstr(arg, "-fallback")) {
389 fallback = 1;
390 } else if (eqstr(arg, "-noreneg")) {
391 flags |= BR_OPT_NO_RENEGOTIATION;
392 } else {
393 fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
394 usage_client();
395 goto client_exit_error;
396 }
397 }
398 if (server_name == NULL) {
399 fprintf(stderr, "ERROR: no server name/address provided\n");
400 usage_client();
401 goto client_exit_error;
402 }
403 for (u = strlen(server_name); u > 0; u --) {
404 int c = server_name[u - 1];
405 if (c == ':') {
406 break;
407 }
408 if (c < '0' || c > '9') {
409 u = 0;
410 break;
411 }
412 }
413 if (u == 0) {
414 host = xstrdup(server_name);
415 port = "443";
416 } else {
417 port = xstrdup(server_name + u);
418 host = xmalloc(u);
419 memcpy(host, server_name, u - 1);
420 host[u - 1] = 0;
421 }
422 if (sni == NULL) {
423 sni = host;
424 }
425
426 if (vmin == 0) {
427 vmin = BR_TLS10;
428 }
429 if (vmax == 0) {
430 vmax = BR_TLS12;
431 }
432 if (vmax < vmin) {
433 fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
434 " version combination\n");
435 usage_client();
436 goto client_exit_error;
437 }
438 if (suites == NULL) {
439 num_suites = 0;
440
441 for (u = 0; cipher_suites[u].name; u ++) {
442 if ((cipher_suites[u].req & REQ_TLS12) == 0
443 || vmax >= BR_TLS12)
444 {
445 num_suites ++;
446 }
447 }
448 suites = xmalloc(num_suites * sizeof *suites);
449 num_suites = 0;
450 for (u = 0; cipher_suites[u].name; u ++) {
451 if ((cipher_suites[u].req & REQ_TLS12) == 0
452 || vmax >= BR_TLS12)
453 {
454 suites[num_suites ++] = cipher_suites[u];
455 }
456 }
457 }
458 if (hfuns == 0) {
459 hfuns = (unsigned)-1;
460 }
461 if (iobuf_len == 0) {
462 if (bidi) {
463 iobuf_len = BR_SSL_BUFSIZE_BIDI;
464 } else {
465 iobuf_len = BR_SSL_BUFSIZE_MONO;
466 }
467 }
468 iobuf = xmalloc(iobuf_len);
469
470 /*
471 * Compute implementation requirements and inject implementations.
472 */
473 suite_ids = xmalloc((num_suites + 1) * sizeof *suite_ids);
474 br_ssl_client_zero(&cc);
475 br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
476 dnhash = NULL;
477 for (u = 0; hash_functions[u].name; u ++) {
478 const br_hash_class *hc;
479 int id;
480
481 hc = hash_functions[u].hclass;
482 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
483 if ((hfuns & ((unsigned)1 << id)) != 0) {
484 dnhash = hc;
485 }
486 }
487 if (dnhash == NULL) {
488 fprintf(stderr, "ERROR: no supported hash function\n");
489 goto client_exit_error;
490 }
491 br_x509_minimal_init(&xc, dnhash,
492 &VEC_ELT(anchors, 0), VEC_LEN(anchors));
493 if (vmin <= BR_TLS11) {
494 if (!(hfuns & (1 << br_md5_ID))) {
495 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
496 goto client_exit_error;
497 }
498 if (!(hfuns & (1 << br_sha1_ID))) {
499 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
500 goto client_exit_error;
501 }
502 }
503 for (u = 0; u < num_suites; u ++) {
504 unsigned req;
505
506 req = suites[u].req;
507 suite_ids[u] = suites[u].suite;
508 if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
509 fprintf(stderr,
510 "ERROR: cipher suite %s requires TLS 1.2\n",
511 suites[u].name);
512 goto client_exit_error;
513 }
514 if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
515 fprintf(stderr,
516 "ERROR: cipher suite %s requires SHA-1\n",
517 suites[u].name);
518 goto client_exit_error;
519 }
520 if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
521 fprintf(stderr,
522 "ERROR: cipher suite %s requires SHA-256\n",
523 suites[u].name);
524 goto client_exit_error;
525 }
526 if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
527 fprintf(stderr,
528 "ERROR: cipher suite %s requires SHA-384\n",
529 suites[u].name);
530 goto client_exit_error;
531 }
532 /* TODO: algorithm implementation selection */
533 if ((req & REQ_AESCBC) != 0) {
534 br_ssl_engine_set_aes_cbc(&cc.eng,
535 &br_aes_ct_cbcenc_vtable,
536 &br_aes_ct_cbcdec_vtable);
537 br_ssl_engine_set_cbc(&cc.eng,
538 &br_sslrec_in_cbc_vtable,
539 &br_sslrec_out_cbc_vtable);
540 }
541 if ((req & REQ_AESGCM) != 0) {
542 br_ssl_engine_set_aes_ctr(&cc.eng,
543 &br_aes_ct_ctr_vtable);
544 br_ssl_engine_set_ghash(&cc.eng,
545 &br_ghash_ctmul);
546 br_ssl_engine_set_gcm(&cc.eng,
547 &br_sslrec_in_gcm_vtable,
548 &br_sslrec_out_gcm_vtable);
549 }
550 if ((req & REQ_3DESCBC) != 0) {
551 br_ssl_engine_set_des_cbc(&cc.eng,
552 &br_des_ct_cbcenc_vtable,
553 &br_des_ct_cbcdec_vtable);
554 br_ssl_engine_set_cbc(&cc.eng,
555 &br_sslrec_in_cbc_vtable,
556 &br_sslrec_out_cbc_vtable);
557 }
558 if ((req & REQ_RSAKEYX) != 0) {
559 br_ssl_client_set_rsapub(&cc, &br_rsa_i31_public);
560 }
561 if ((req & REQ_ECDHE_RSA) != 0) {
562 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
563 br_ssl_client_set_rsavrfy(&cc, &br_rsa_i31_pkcs1_vrfy);
564 }
565 if ((req & REQ_ECDHE_ECDSA) != 0) {
566 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
567 br_ssl_client_set_ecdsa(&cc, &br_ecdsa_i31_vrfy_asn1);
568 }
569 if ((req & REQ_ECDH) != 0) {
570 br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
571 }
572 }
573 if (fallback) {
574 suite_ids[num_suites ++] = 0x5600;
575 }
576 br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
577
578 for (u = 0; hash_functions[u].name; u ++) {
579 const br_hash_class *hc;
580 int id;
581
582 hc = hash_functions[u].hclass;
583 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
584 if ((hfuns & ((unsigned)1 << id)) != 0) {
585 br_ssl_engine_set_hash(&cc.eng, id, hc);
586 br_x509_minimal_set_hash(&xc, id, hc);
587 }
588 }
589 if (vmin <= BR_TLS11) {
590 br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
591 }
592 if (vmax >= BR_TLS12) {
593 if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
594 br_ssl_engine_set_prf_sha256(&cc.eng,
595 &br_tls12_sha256_prf);
596 }
597 if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
598 br_ssl_engine_set_prf_sha384(&cc.eng,
599 &br_tls12_sha384_prf);
600 }
601 }
602 br_x509_minimal_set_rsa(&xc, &br_rsa_i31_pkcs1_vrfy);
603 br_x509_minimal_set_ecdsa(&xc,
604 &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
605
606 /*
607 * If there is no provided trust anchor, then certificate validation
608 * will always fail. In that situation, we use our custom wrapper
609 * that tolerates unknown anchors.
610 */
611 if (VEC_LEN(anchors) == 0) {
612 if (verbose) {
613 fprintf(stderr,
614 "WARNING: no configured trust anchor\n");
615 }
616 x509_noanchor_init(&xwc, &xc.vtable);
617 br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
618 } else {
619 br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
620 }
621
622 if (minhello_len != (size_t)-1) {
623 br_ssl_client_set_min_clienthello_len(&cc, minhello_len);
624 }
625 br_ssl_engine_set_all_flags(&cc.eng, flags);
626
627 br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
628 br_ssl_client_reset(&cc, sni, 0);
629
630 /*
631 * We need to avoid SIGPIPE.
632 */
633 signal(SIGPIPE, SIG_IGN);
634
635 /*
636 * Connect to the peer.
637 */
638 fd = host_connect(host, port, verbose);
639 if (fd < 0) {
640 goto client_exit_error;
641 }
642
643 /*
644 * Run the engine until completion.
645 */
646 if (run_ssl_engine(&cc.eng, fd,
647 (verbose ? RUN_ENGINE_VERBOSE : 0)
648 | (trace ? RUN_ENGINE_TRACE : 0)) != 0)
649 {
650 goto client_exit_error;
651 } else {
652 goto client_exit;
653 }
654
655 /*
656 * Release allocated structures.
657 */
658 client_exit:
659 xfree(host);
660 xfree(suites);
661 xfree(suite_ids);
662 VEC_CLEAREXT(anchors, &free_ta_contents);
663 xfree(iobuf);
664 if (fd >= 0) {
665 close(fd);
666 }
667 return retcode;
668
669 client_exit_error:
670 retcode = -1;
671 goto client_exit;
672 }