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