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