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