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