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