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