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