Some cleanups (removed unused files, split i15 code into per-function files).
[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 typedef struct {
121 const br_ssl_client_certificate_class *vtable;
122 int verbose;
123 br_x509_certificate *chain;
124 size_t chain_len;
125 private_key *sk;
126 int issuer_key_type;
127 } ccert_context;
128
129 static void
130 cc_start_name_list(const br_ssl_client_certificate_class **pctx)
131 {
132 ccert_context *zc;
133
134 zc = (ccert_context *)pctx;
135 if (zc->verbose) {
136 fprintf(stderr, "Server requests a client certificate.\n");
137 fprintf(stderr, "--- anchor DN list start ---\n");
138 }
139 }
140
141 static void
142 cc_start_name(const br_ssl_client_certificate_class **pctx, size_t len)
143 {
144 ccert_context *zc;
145
146 zc = (ccert_context *)pctx;
147 if (zc->verbose) {
148 fprintf(stderr, "new anchor name, length = %u\n",
149 (unsigned)len);
150 }
151 }
152
153 static void
154 cc_append_name(const br_ssl_client_certificate_class **pctx,
155 const unsigned char *data, size_t len)
156 {
157 ccert_context *zc;
158
159 zc = (ccert_context *)pctx;
160 if (zc->verbose) {
161 size_t u;
162
163 for (u = 0; u < len; u ++) {
164 if (u == 0) {
165 fprintf(stderr, " ");
166 } else if (u > 0 && u % 16 == 0) {
167 fprintf(stderr, "\n ");
168 }
169 fprintf(stderr, " %02x", data[u]);
170 }
171 if (len > 0) {
172 fprintf(stderr, "\n");
173 }
174 }
175 }
176
177 static void
178 cc_end_name(const br_ssl_client_certificate_class **pctx)
179 {
180 (void)pctx;
181 }
182
183 static void
184 cc_end_name_list(const br_ssl_client_certificate_class **pctx)
185 {
186 ccert_context *zc;
187
188 zc = (ccert_context *)pctx;
189 if (zc->verbose) {
190 fprintf(stderr, "--- anchor DN list end ---\n");
191 }
192 }
193
194 static void
195 print_hashes(unsigned hh, unsigned hh2)
196 {
197 int i;
198
199 for (i = 0; i < 8; i ++) {
200 const char *name;
201
202 name = hash_function_name(i);
203 if (((hh >> i) & 1) != 0) {
204 fprintf(stderr, " %s", name);
205 } else if (((hh2 >> i) & 1) != 0) {
206 fprintf(stderr, " (%s)", name);
207 }
208 }
209 }
210
211 static int
212 choose_hash(unsigned hh)
213 {
214 static const int f[] = {
215 br_sha256_ID, br_sha224_ID, br_sha384_ID, br_sha512_ID,
216 br_sha1_ID, br_md5sha1_ID, -1
217 };
218
219 size_t u;
220
221 for (u = 0; f[u] >= 0; u ++) {
222 if (((hh >> f[u]) & 1) != 0) {
223 return f[u];
224 }
225 }
226 return -1;
227 }
228
229 static void
230 cc_choose(const br_ssl_client_certificate_class **pctx,
231 const br_ssl_client_context *cc, uint32_t auth_types,
232 br_ssl_client_certificate *choices)
233 {
234 ccert_context *zc;
235 int scurve;
236
237 zc = (ccert_context *)pctx;
238 scurve = br_ssl_client_get_server_curve(cc);
239 if (zc->verbose) {
240 unsigned hashes;
241
242 hashes = br_ssl_client_get_server_hashes(cc);
243 if ((auth_types & 0x00FF) != 0) {
244 fprintf(stderr, "supported: RSA signatures:");
245 print_hashes(auth_types, hashes);
246 fprintf(stderr, "\n");
247 }
248 if ((auth_types & 0xFF00) != 0) {
249 fprintf(stderr, "supported: ECDSA signatures:");
250 print_hashes(auth_types >> 8, hashes >> 8);
251 fprintf(stderr, "\n");
252 }
253 if ((auth_types & 0x010000) != 0) {
254 fprintf(stderr, "supported:"
255 " fixed ECDH (cert signed with RSA)\n");
256 }
257 if ((auth_types & 0x020000) != 0) {
258 fprintf(stderr, "supported:"
259 " fixed ECDH (cert signed with ECDSA)\n");
260 }
261 if (scurve) {
262 fprintf(stderr, "server key curve: %s (%d)\n",
263 ec_curve_name(scurve), scurve);
264 } else {
265 fprintf(stderr, "server key is not EC\n");
266 }
267 }
268 switch (zc->sk->key_type) {
269 case BR_KEYTYPE_RSA:
270 if ((choices->hash_id = choose_hash(auth_types)) >= 0) {
271 if (zc->verbose) {
272 fprintf(stderr, "using RSA, hash = %d (%s)\n",
273 choices->hash_id,
274 hash_function_name(choices->hash_id));
275 }
276 choices->auth_type = BR_AUTH_RSA;
277 choices->chain = zc->chain;
278 choices->chain_len = zc->chain_len;
279 return;
280 }
281 break;
282 case BR_KEYTYPE_EC:
283 if (zc->issuer_key_type != 0
284 && scurve == zc->sk->key.ec.curve)
285 {
286 int x;
287
288 x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;
289 if (((auth_types >> x) & 1) != 0) {
290 if (zc->verbose) {
291 fprintf(stderr, "using static ECDH\n");
292 }
293 choices->auth_type = BR_AUTH_ECDH;
294 choices->hash_id = -1;
295 choices->chain = zc->chain;
296 choices->chain_len = zc->chain_len;
297 return;
298 }
299 }
300 if ((choices->hash_id = choose_hash(auth_types >> 8)) >= 0) {
301 if (zc->verbose) {
302 fprintf(stderr, "using ECDSA, hash = %d (%s)\n",
303 choices->hash_id,
304 hash_function_name(choices->hash_id));
305 }
306 choices->auth_type = BR_AUTH_ECDSA;
307 choices->chain = zc->chain;
308 choices->chain_len = zc->chain_len;
309 return;
310 }
311 break;
312 }
313 if (zc->verbose) {
314 fprintf(stderr, "no matching client certificate\n");
315 }
316 choices->chain = NULL;
317 choices->chain_len = 0;
318 }
319
320 static uint32_t
321 cc_do_keyx(const br_ssl_client_certificate_class **pctx,
322 unsigned char *data, size_t *len)
323 {
324 ccert_context *zc;
325 size_t xoff, xlen;
326 uint32_t r;
327
328 zc = (ccert_context *)pctx;
329 r = br_ec_all_m15.mul(data, *len, zc->sk->key.ec.x,
330 zc->sk->key.ec.xlen, zc->sk->key.ec.curve);
331 xoff = br_ec_all_m15.xoff(zc->sk->key.ec.curve, &xlen);
332 memmove(data, data + xoff, xlen);
333 *len = xlen;
334 return r;
335 }
336
337 static size_t
338 cc_do_sign(const br_ssl_client_certificate_class **pctx,
339 int hash_id, size_t hv_len, unsigned char *data, size_t len)
340 {
341 ccert_context *zc;
342 unsigned char hv[64];
343
344 zc = (ccert_context *)pctx;
345 memcpy(hv, data, hv_len);
346 switch (zc->sk->key_type) {
347 const br_hash_class *hc;
348 const unsigned char *hash_oid;
349 uint32_t x;
350 size_t sig_len;
351
352 case BR_KEYTYPE_RSA:
353 hash_oid = get_hash_oid(hash_id);
354 if (hash_oid == NULL && hash_id != 0) {
355 if (zc->verbose) {
356 fprintf(stderr, "ERROR: cannot RSA-sign with"
357 " unknown hash function: %d\n",
358 hash_id);
359 }
360 return 0;
361 }
362 sig_len = (zc->sk->key.rsa.n_bitlen + 7) >> 3;
363 if (len < sig_len) {
364 if (zc->verbose) {
365 fprintf(stderr, "ERROR: cannot RSA-sign,"
366 " buffer is too small"
367 " (sig=%lu, buf=%lu)\n",
368 (unsigned long)sig_len,
369 (unsigned long)len);
370 }
371 return 0;
372 }
373 x = br_rsa_i31_pkcs1_sign(hash_oid, hv, hv_len,
374 &zc->sk->key.rsa, data);
375 if (!x) {
376 if (zc->verbose) {
377 fprintf(stderr, "ERROR: RSA-sign failure\n");
378 }
379 return 0;
380 }
381 return sig_len;
382
383 case BR_KEYTYPE_EC:
384 hc = get_hash_impl(hash_id);
385 if (hc == NULL) {
386 if (zc->verbose) {
387 fprintf(stderr, "ERROR: cannot ECDSA-sign with"
388 " unknown hash function: %d\n",
389 hash_id);
390 }
391 return 0;
392 }
393 if (len < 139) {
394 if (zc->verbose) {
395 fprintf(stderr, "ERROR: cannot ECDSA-sign"
396 " (output buffer = %lu)\n",
397 (unsigned long)len);
398 }
399 return 0;
400 }
401 sig_len = br_ecdsa_i31_sign_asn1(&br_ec_all_m15,
402 hc, hv, &zc->sk->key.ec, data);
403 if (sig_len == 0) {
404 if (zc->verbose) {
405 fprintf(stderr, "ERROR: ECDSA-sign failure\n");
406 }
407 return 0;
408 }
409 return sig_len;
410
411 default:
412 return 0;
413 }
414 }
415
416 static const br_ssl_client_certificate_class ccert_vtable = {
417 sizeof(ccert_context),
418 cc_start_name_list,
419 cc_start_name,
420 cc_append_name,
421 cc_end_name,
422 cc_end_name_list,
423 cc_choose,
424 cc_do_keyx,
425 cc_do_sign
426 };
427
428 static void
429 free_alpn(void *alpn)
430 {
431 xfree(*(char **)alpn);
432 }
433
434 static void
435 usage_client(void)
436 {
437 fprintf(stderr,
438 "usage: brssl client server[:port] [ options ]\n");
439 fprintf(stderr,
440 "options:\n");
441 fprintf(stderr,
442 " -q suppress verbose messages\n");
443 fprintf(stderr,
444 " -trace activate extra debug messages (dump of all packets)\n");
445 fprintf(stderr,
446 " -sni name use this specific name for SNI\n");
447 fprintf(stderr,
448 " -nosni do not send any SNI\n");
449 fprintf(stderr,
450 " -mono use monodirectional buffering\n");
451 fprintf(stderr,
452 " -buf length set the I/O buffer length (in bytes)\n");
453 fprintf(stderr,
454 " -CA file add certificates in 'file' to trust anchors\n");
455 fprintf(stderr,
456 " -cert file set client certificate chain\n");
457 fprintf(stderr,
458 " -key file set client private key (for certificate authentication)\n");
459 fprintf(stderr,
460 " -nostaticecdh prohibit full-static ECDH (client certificate)\n");
461 fprintf(stderr,
462 " -list list supported names (protocols, algorithms...)\n");
463 fprintf(stderr,
464 " -vmin name set minimum supported version (default: TLS-1.0)\n");
465 fprintf(stderr,
466 " -vmax name set maximum supported version (default: TLS-1.2)\n");
467 fprintf(stderr,
468 " -cs names set list of supported cipher suites (comma-separated)\n");
469 fprintf(stderr,
470 " -hf names add support for some hash functions (comma-separated)\n");
471 fprintf(stderr,
472 " -minhello len set minimum ClientHello length (in bytes)\n");
473 fprintf(stderr,
474 " -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
475 fprintf(stderr,
476 " -noreneg prohibit renegotiations\n");
477 fprintf(stderr,
478 " -alpn name add protocol name to list of protocols (ALPN extension)\n");
479 fprintf(stderr,
480 " -strictalpn fail on ALPN mismatch\n");
481 }
482
483 /* see brssl.h */
484 int
485 do_client(int argc, char *argv[])
486 {
487 int retcode;
488 int verbose;
489 int trace;
490 int i, bidi;
491 const char *server_name;
492 char *host;
493 char *port;
494 const char *sni;
495 anchor_list anchors = VEC_INIT;
496 unsigned vmin, vmax;
497 VECTOR(const char *) alpn_names = VEC_INIT;
498 cipher_suite *suites;
499 size_t num_suites;
500 uint16_t *suite_ids;
501 unsigned hfuns;
502 size_t u;
503 br_ssl_client_context cc;
504 br_x509_minimal_context xc;
505 x509_noanchor_context xwc;
506 const br_hash_class *dnhash;
507 ccert_context zc;
508 br_x509_certificate *chain;
509 size_t chain_len;
510 private_key *sk;
511 int nostaticecdh;
512 unsigned char *iobuf;
513 size_t iobuf_len;
514 size_t minhello_len;
515 int fallback;
516 uint32_t flags;
517 int fd;
518
519 retcode = 0;
520 verbose = 1;
521 trace = 0;
522 server_name = NULL;
523 host = NULL;
524 port = NULL;
525 sni = NULL;
526 bidi = 1;
527 vmin = 0;
528 vmax = 0;
529 suites = NULL;
530 num_suites = 0;
531 hfuns = 0;
532 suite_ids = NULL;
533 chain = NULL;
534 chain_len = 0;
535 sk = NULL;
536 nostaticecdh = 0;
537 iobuf = NULL;
538 iobuf_len = 0;
539 minhello_len = (size_t)-1;
540 fallback = 0;
541 flags = 0;
542 fd = -1;
543 for (i = 0; i < argc; i ++) {
544 const char *arg;
545
546 arg = argv[i];
547 if (arg[0] != '-') {
548 if (server_name != NULL) {
549 fprintf(stderr,
550 "ERROR: duplicate server name\n");
551 usage_client();
552 goto client_exit_error;
553 }
554 server_name = arg;
555 continue;
556 }
557 if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
558 verbose = 1;
559 } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
560 verbose = 0;
561 } else if (eqstr(arg, "-trace")) {
562 trace = 1;
563 } else if (eqstr(arg, "-sni")) {
564 if (++ i >= argc) {
565 fprintf(stderr,
566 "ERROR: no argument for '-sni'\n");
567 usage_client();
568 goto client_exit_error;
569 }
570 if (sni != NULL) {
571 fprintf(stderr, "ERROR: duplicate SNI\n");
572 usage_client();
573 goto client_exit_error;
574 }
575 sni = argv[i];
576 } else if (eqstr(arg, "-nosni")) {
577 if (sni != NULL) {
578 fprintf(stderr, "ERROR: duplicate SNI\n");
579 usage_client();
580 goto client_exit_error;
581 }
582 sni = "";
583 } else if (eqstr(arg, "-mono")) {
584 bidi = 0;
585 } else if (eqstr(arg, "-buf")) {
586 if (++ i >= argc) {
587 fprintf(stderr,
588 "ERROR: no argument for '-buf'\n");
589 usage_client();
590 goto client_exit_error;
591 }
592 arg = argv[i];
593 if (iobuf_len != 0) {
594 fprintf(stderr,
595 "ERROR: duplicate I/O buffer length\n");
596 usage_client();
597 goto client_exit_error;
598 }
599 iobuf_len = parse_size(arg);
600 if (iobuf_len == (size_t)-1) {
601 usage_client();
602 goto client_exit_error;
603 }
604 } else if (eqstr(arg, "-CA")) {
605 if (++ i >= argc) {
606 fprintf(stderr,
607 "ERROR: no argument for '-CA'\n");
608 usage_client();
609 goto client_exit_error;
610 }
611 arg = argv[i];
612 if (read_trust_anchors(&anchors, arg) == 0) {
613 usage_client();
614 goto client_exit_error;
615 }
616 } else if (eqstr(arg, "-cert")) {
617 if (++ i >= argc) {
618 fprintf(stderr,
619 "ERROR: no argument for '-cert'\n");
620 usage_client();
621 goto client_exit_error;
622 }
623 if (chain != NULL) {
624 fprintf(stderr,
625 "ERROR: duplicate certificate chain\n");
626 usage_client();
627 goto client_exit_error;
628 }
629 arg = argv[i];
630 chain = read_certificates(arg, &chain_len);
631 if (chain == NULL || chain_len == 0) {
632 goto client_exit_error;
633 }
634 } else if (eqstr(arg, "-key")) {
635 if (++ i >= argc) {
636 fprintf(stderr,
637 "ERROR: no argument for '-key'\n");
638 usage_client();
639 goto client_exit_error;
640 }
641 if (sk != NULL) {
642 fprintf(stderr,
643 "ERROR: duplicate private key\n");
644 usage_client();
645 goto client_exit_error;
646 }
647 arg = argv[i];
648 sk = read_private_key(arg);
649 if (sk == NULL) {
650 goto client_exit_error;
651 }
652 } else if (eqstr(arg, "-nostaticecdh")) {
653 nostaticecdh = 1;
654 } else if (eqstr(arg, "-list")) {
655 list_names();
656 goto client_exit;
657 } else if (eqstr(arg, "-vmin")) {
658 if (++ i >= argc) {
659 fprintf(stderr,
660 "ERROR: no argument for '-vmin'\n");
661 usage_client();
662 goto client_exit_error;
663 }
664 arg = argv[i];
665 if (vmin != 0) {
666 fprintf(stderr,
667 "ERROR: duplicate minimum version\n");
668 usage_client();
669 goto client_exit_error;
670 }
671 vmin = parse_version(arg, strlen(arg));
672 if (vmin == 0) {
673 fprintf(stderr,
674 "ERROR: unrecognised version '%s'\n",
675 arg);
676 usage_client();
677 goto client_exit_error;
678 }
679 } else if (eqstr(arg, "-vmax")) {
680 if (++ i >= argc) {
681 fprintf(stderr,
682 "ERROR: no argument for '-vmax'\n");
683 usage_client();
684 goto client_exit_error;
685 }
686 arg = argv[i];
687 if (vmax != 0) {
688 fprintf(stderr,
689 "ERROR: duplicate maximum version\n");
690 usage_client();
691 goto client_exit_error;
692 }
693 vmax = parse_version(arg, strlen(arg));
694 if (vmax == 0) {
695 fprintf(stderr,
696 "ERROR: unrecognised version '%s'\n",
697 arg);
698 usage_client();
699 goto client_exit_error;
700 }
701 } else if (eqstr(arg, "-cs")) {
702 if (++ i >= argc) {
703 fprintf(stderr,
704 "ERROR: no argument for '-cs'\n");
705 usage_client();
706 goto client_exit_error;
707 }
708 arg = argv[i];
709 if (suites != NULL) {
710 fprintf(stderr, "ERROR: duplicate list"
711 " of cipher suites\n");
712 usage_client();
713 goto client_exit_error;
714 }
715 suites = parse_suites(arg, &num_suites);
716 if (suites == NULL) {
717 usage_client();
718 goto client_exit_error;
719 }
720 } else if (eqstr(arg, "-hf")) {
721 unsigned x;
722
723 if (++ i >= argc) {
724 fprintf(stderr,
725 "ERROR: no argument for '-hf'\n");
726 usage_client();
727 goto client_exit_error;
728 }
729 arg = argv[i];
730 x = parse_hash_functions(arg);
731 if (x == 0) {
732 usage_client();
733 goto client_exit_error;
734 }
735 hfuns |= x;
736 } else if (eqstr(arg, "-minhello")) {
737 if (++ i >= argc) {
738 fprintf(stderr,
739 "ERROR: no argument for '-minhello'\n");
740 usage_client();
741 goto client_exit_error;
742 }
743 arg = argv[i];
744 if (minhello_len != (size_t)-1) {
745 fprintf(stderr, "ERROR: duplicate minium"
746 " ClientHello length\n");
747 usage_client();
748 goto client_exit_error;
749 }
750 minhello_len = parse_size(arg);
751 /*
752 * Minimum ClientHello length must fit on 16 bits.
753 */
754 if (minhello_len == (size_t)-1
755 || (((minhello_len >> 12) >> 4) != 0))
756 {
757 usage_client();
758 goto client_exit_error;
759 }
760 } else if (eqstr(arg, "-fallback")) {
761 fallback = 1;
762 } else if (eqstr(arg, "-noreneg")) {
763 flags |= BR_OPT_NO_RENEGOTIATION;
764 } else if (eqstr(arg, "-alpn")) {
765 if (++ i >= argc) {
766 fprintf(stderr,
767 "ERROR: no argument for '-alpn'\n");
768 usage_client();
769 goto client_exit_error;
770 }
771 VEC_ADD(alpn_names, xstrdup(argv[i]));
772 } else if (eqstr(arg, "-strictalpn")) {
773 flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
774 } else {
775 fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
776 usage_client();
777 goto client_exit_error;
778 }
779 }
780 if (server_name == NULL) {
781 fprintf(stderr, "ERROR: no server name/address provided\n");
782 usage_client();
783 goto client_exit_error;
784 }
785 for (u = strlen(server_name); u > 0; u --) {
786 int c = server_name[u - 1];
787 if (c == ':') {
788 break;
789 }
790 if (c < '0' || c > '9') {
791 u = 0;
792 break;
793 }
794 }
795 if (u == 0) {
796 host = xstrdup(server_name);
797 port = xstrdup("443");
798 } else {
799 port = xstrdup(server_name + u);
800 host = xmalloc(u);
801 memcpy(host, server_name, u - 1);
802 host[u - 1] = 0;
803 }
804 if (sni == NULL) {
805 sni = host;
806 }
807
808 if (chain == NULL && sk != NULL) {
809 fprintf(stderr, "ERROR: private key specified, but"
810 " no certificate chain\n");
811 usage_client();
812 goto client_exit_error;
813 }
814 if (chain != NULL && sk == NULL) {
815 fprintf(stderr, "ERROR: certificate chain specified, but"
816 " no private key\n");
817 usage_client();
818 goto client_exit_error;
819 }
820
821 if (vmin == 0) {
822 vmin = BR_TLS10;
823 }
824 if (vmax == 0) {
825 vmax = BR_TLS12;
826 }
827 if (vmax < vmin) {
828 fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
829 " version combination\n");
830 usage_client();
831 goto client_exit_error;
832 }
833 if (suites == NULL) {
834 num_suites = 0;
835
836 for (u = 0; cipher_suites[u].name; u ++) {
837 if ((cipher_suites[u].req & REQ_TLS12) == 0
838 || vmax >= BR_TLS12)
839 {
840 num_suites ++;
841 }
842 }
843 suites = xmalloc(num_suites * sizeof *suites);
844 num_suites = 0;
845 for (u = 0; cipher_suites[u].name; u ++) {
846 if ((cipher_suites[u].req & REQ_TLS12) == 0
847 || vmax >= BR_TLS12)
848 {
849 suites[num_suites ++] = cipher_suites[u];
850 }
851 }
852 }
853 if (hfuns == 0) {
854 hfuns = (unsigned)-1;
855 }
856 if (iobuf_len == 0) {
857 if (bidi) {
858 iobuf_len = BR_SSL_BUFSIZE_BIDI;
859 } else {
860 iobuf_len = BR_SSL_BUFSIZE_MONO;
861 }
862 }
863 iobuf = xmalloc(iobuf_len);
864
865 /*
866 * Compute implementation requirements and inject implementations.
867 */
868 suite_ids = xmalloc((num_suites + 1) * sizeof *suite_ids);
869 br_ssl_client_zero(&cc);
870 br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
871 dnhash = NULL;
872 for (u = 0; hash_functions[u].name; u ++) {
873 const br_hash_class *hc;
874 int id;
875
876 hc = hash_functions[u].hclass;
877 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
878 if ((hfuns & ((unsigned)1 << id)) != 0) {
879 dnhash = hc;
880 }
881 }
882 if (dnhash == NULL) {
883 fprintf(stderr, "ERROR: no supported hash function\n");
884 goto client_exit_error;
885 }
886 br_x509_minimal_init(&xc, dnhash,
887 &VEC_ELT(anchors, 0), VEC_LEN(anchors));
888 if (vmin <= BR_TLS11) {
889 if (!(hfuns & (1 << br_md5_ID))) {
890 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
891 goto client_exit_error;
892 }
893 if (!(hfuns & (1 << br_sha1_ID))) {
894 fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
895 goto client_exit_error;
896 }
897 }
898 for (u = 0; u < num_suites; u ++) {
899 unsigned req;
900
901 req = suites[u].req;
902 suite_ids[u] = suites[u].suite;
903 if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
904 fprintf(stderr,
905 "ERROR: cipher suite %s requires TLS 1.2\n",
906 suites[u].name);
907 goto client_exit_error;
908 }
909 if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
910 fprintf(stderr,
911 "ERROR: cipher suite %s requires SHA-1\n",
912 suites[u].name);
913 goto client_exit_error;
914 }
915 if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
916 fprintf(stderr,
917 "ERROR: cipher suite %s requires SHA-256\n",
918 suites[u].name);
919 goto client_exit_error;
920 }
921 if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
922 fprintf(stderr,
923 "ERROR: cipher suite %s requires SHA-384\n",
924 suites[u].name);
925 goto client_exit_error;
926 }
927 /* TODO: algorithm implementation selection */
928 if ((req & REQ_AESCBC) != 0) {
929 br_ssl_engine_set_aes_cbc(&cc.eng,
930 &br_aes_ct_cbcenc_vtable,
931 &br_aes_ct_cbcdec_vtable);
932 br_ssl_engine_set_cbc(&cc.eng,
933 &br_sslrec_in_cbc_vtable,
934 &br_sslrec_out_cbc_vtable);
935 }
936 if ((req & REQ_AESGCM) != 0) {
937 br_ssl_engine_set_aes_ctr(&cc.eng,
938 &br_aes_ct_ctr_vtable);
939 br_ssl_engine_set_ghash(&cc.eng,
940 &br_ghash_ctmul);
941 br_ssl_engine_set_gcm(&cc.eng,
942 &br_sslrec_in_gcm_vtable,
943 &br_sslrec_out_gcm_vtable);
944 }
945 if ((req & REQ_CHAPOL) != 0) {
946 br_ssl_engine_set_chacha20(&cc.eng,
947 &br_chacha20_ct_run);
948 br_ssl_engine_set_poly1305(&cc.eng,
949 &br_poly1305_ctmul_run);
950 br_ssl_engine_set_chapol(&cc.eng,
951 &br_sslrec_in_chapol_vtable,
952 &br_sslrec_out_chapol_vtable);
953 }
954 if ((req & REQ_3DESCBC) != 0) {
955 br_ssl_engine_set_des_cbc(&cc.eng,
956 &br_des_ct_cbcenc_vtable,
957 &br_des_ct_cbcdec_vtable);
958 br_ssl_engine_set_cbc(&cc.eng,
959 &br_sslrec_in_cbc_vtable,
960 &br_sslrec_out_cbc_vtable);
961 }
962 if ((req & REQ_RSAKEYX) != 0) {
963 br_ssl_client_set_rsapub(&cc, &br_rsa_i31_public);
964 }
965 if ((req & REQ_ECDHE_RSA) != 0) {
966 br_ssl_engine_set_ec(&cc.eng, &br_ec_all_m15);
967 br_ssl_engine_set_rsavrfy(&cc.eng,
968 &br_rsa_i31_pkcs1_vrfy);
969 }
970 if ((req & REQ_ECDHE_ECDSA) != 0) {
971 br_ssl_engine_set_ec(&cc.eng, &br_ec_all_m15);
972 br_ssl_engine_set_ecdsa(&cc.eng,
973 &br_ecdsa_i31_vrfy_asn1);
974 }
975 if ((req & REQ_ECDH) != 0) {
976 br_ssl_engine_set_ec(&cc.eng, &br_ec_all_m15);
977 }
978 }
979 if (fallback) {
980 suite_ids[num_suites ++] = 0x5600;
981 }
982 br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
983
984 for (u = 0; hash_functions[u].name; u ++) {
985 const br_hash_class *hc;
986 int id;
987
988 hc = hash_functions[u].hclass;
989 id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
990 if ((hfuns & ((unsigned)1 << id)) != 0) {
991 br_ssl_engine_set_hash(&cc.eng, id, hc);
992 br_x509_minimal_set_hash(&xc, id, hc);
993 }
994 }
995 if (vmin <= BR_TLS11) {
996 br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
997 }
998 if (vmax >= BR_TLS12) {
999 if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
1000 br_ssl_engine_set_prf_sha256(&cc.eng,
1001 &br_tls12_sha256_prf);
1002 }
1003 if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
1004 br_ssl_engine_set_prf_sha384(&cc.eng,
1005 &br_tls12_sha384_prf);
1006 }
1007 }
1008 br_x509_minimal_set_rsa(&xc, &br_rsa_i31_pkcs1_vrfy);
1009 br_x509_minimal_set_ecdsa(&xc,
1010 &br_ec_all_m15, &br_ecdsa_i31_vrfy_asn1);
1011
1012 /*
1013 * If there is no provided trust anchor, then certificate validation
1014 * will always fail. In that situation, we use our custom wrapper
1015 * that tolerates unknown anchors.
1016 */
1017 if (VEC_LEN(anchors) == 0) {
1018 if (verbose) {
1019 fprintf(stderr,
1020 "WARNING: no configured trust anchor\n");
1021 }
1022 x509_noanchor_init(&xwc, &xc.vtable);
1023 br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
1024 } else {
1025 br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
1026 }
1027
1028 if (minhello_len != (size_t)-1) {
1029 br_ssl_client_set_min_clienthello_len(&cc, minhello_len);
1030 }
1031 br_ssl_engine_set_all_flags(&cc.eng, flags);
1032 if (VEC_LEN(alpn_names) != 0) {
1033 br_ssl_engine_set_protocol_names(&cc.eng,
1034 &VEC_ELT(alpn_names, 0), VEC_LEN(alpn_names));
1035 }
1036
1037 if (chain != NULL) {
1038 zc.vtable = &ccert_vtable;
1039 zc.verbose = verbose;
1040 zc.chain = chain;
1041 zc.chain_len = chain_len;
1042 zc.sk = sk;
1043 if (nostaticecdh || sk->key_type != BR_KEYTYPE_EC) {
1044 zc.issuer_key_type = 0;
1045 } else {
1046 zc.issuer_key_type = get_cert_signer_algo(&chain[0]);
1047 if (zc.issuer_key_type == 0) {
1048 goto client_exit_error;
1049 }
1050 }
1051 br_ssl_client_set_client_certificate(&cc, &zc.vtable);
1052 }
1053
1054 br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
1055 br_ssl_client_reset(&cc, sni, 0);
1056
1057 /*
1058 * We need to avoid SIGPIPE.
1059 */
1060 signal(SIGPIPE, SIG_IGN);
1061
1062 /*
1063 * Connect to the peer.
1064 */
1065 fd = host_connect(host, port, verbose);
1066 if (fd < 0) {
1067 goto client_exit_error;
1068 }
1069
1070 /*
1071 * Run the engine until completion.
1072 */
1073 if (run_ssl_engine(&cc.eng, fd,
1074 (verbose ? RUN_ENGINE_VERBOSE : 0)
1075 | (trace ? RUN_ENGINE_TRACE : 0)) != 0)
1076 {
1077 goto client_exit_error;
1078 } else {
1079 goto client_exit;
1080 }
1081
1082 /*
1083 * Release allocated structures.
1084 */
1085 client_exit:
1086 xfree(host);
1087 xfree(port);
1088 xfree(suites);
1089 xfree(suite_ids);
1090 VEC_CLEAREXT(anchors, &free_ta_contents);
1091 VEC_CLEAREXT(alpn_names, &free_alpn);
1092 free_certificates(chain, chain_len);
1093 free_private_key(sk);
1094 xfree(iobuf);
1095 if (fd >= 0) {
1096 close(fd);
1097 }
1098 return retcode;
1099
1100 client_exit_error:
1101 retcode = -1;
1102 goto client_exit;
1103 }