aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/BearSSL/tools/client.c
blob: 9838857275ec7d58f3935169a0976f225d92ee4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/*
 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <signal.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>

#define SOCKET             int
#define INVALID_SOCKET     (-1)
#endif

#include "brssl.h"

static int
host_connect(const char *host, const char *port, int verbose)
{
	struct addrinfo hints, *si, *p;
	SOCKET fd;
	int err;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	err = getaddrinfo(host, port, &hints, &si);
	if (err != 0) {
		fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
			gai_strerror(err));
		return INVALID_SOCKET;
	}
	fd = INVALID_SOCKET;
	for (p = si; p != NULL; p = p->ai_next) {
		if (verbose) {
			struct sockaddr *sa;
			void *addr;
			char tmp[INET6_ADDRSTRLEN + 50];

			sa = (struct sockaddr *)p->ai_addr;
			if (sa->sa_family == AF_INET) {
				addr = &((struct sockaddr_in *)
					(void *)sa)->sin_addr;
			} else if (sa->sa_family == AF_INET6) {
				addr = &((struct sockaddr_in6 *)
					(void *)sa)->sin6_addr;
			} else {
				addr = NULL;
			}
			if (addr != NULL) {
				if (!inet_ntop(p->ai_family, addr,
					tmp, sizeof tmp))
				{
					strcpy(tmp, "<invalid>");
				}
			} else {
				sprintf(tmp, "<unknown family: %d>",
					(int)sa->sa_family);
			}
			fprintf(stderr, "connecting to: %s\n", tmp);
		}
		fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
		if (fd == INVALID_SOCKET) {
			if (verbose) {
				perror("socket()");
			}
			continue;
		}
		if (connect(fd, p->ai_addr, p->ai_addrlen) == INVALID_SOCKET) {
			if (verbose) {
				perror("connect()");
			}
#ifdef _WIN32
			closesocket(fd);
#else
			close(fd);
#endif
			continue;
		}
		break;
	}
	if (p == NULL) {
		freeaddrinfo(si);
		fprintf(stderr, "ERROR: failed to connect\n");
		return INVALID_SOCKET;
	}
	freeaddrinfo(si);
	if (verbose) {
		fprintf(stderr, "connected.\n");
	}

	/*
	 * We make the socket non-blocking, since we are going to use
	 * poll() or select() to organise I/O.
	 */
#ifdef _WIN32
	{
		u_long arg;

		arg = 1;
		ioctlsocket(fd, FIONBIO, &arg);
	}
#else
	fcntl(fd, F_SETFL, O_NONBLOCK);
#endif
	return fd;
}

typedef struct {
	const br_ssl_client_certificate_class *vtable;
	int verbose;
	br_x509_certificate *chain;
	size_t chain_len;
	private_key *sk;
	int issuer_key_type;
} ccert_context;

static void
cc_start_name_list(const br_ssl_client_certificate_class **pctx)
{
	ccert_context *zc;

	zc = (ccert_context *)pctx;
	if (zc->verbose) {
		fprintf(stderr, "Server requests a client certificate.\n");
		fprintf(stderr, "--- anchor DN list start ---\n");
	}
}

static void
cc_start_name(const br_ssl_client_certificate_class **pctx, size_t len)
{
	ccert_context *zc;

	zc = (ccert_context *)pctx;
	if (zc->verbose) {
		fprintf(stderr, "new anchor name, length = %u\n",
			(unsigned)len);
	}
}

static void
cc_append_name(const br_ssl_client_certificate_class **pctx,
	const unsigned char *data, size_t len)
{
	ccert_context *zc;

	zc = (ccert_context *)pctx;
	if (zc->verbose) {
		size_t u;

		for (u = 0; u < len; u ++) {
			if (u == 0) {
				fprintf(stderr, "  ");
			} else if (u > 0 && u % 16 == 0) {
				fprintf(stderr, "\n  ");
			}
			fprintf(stderr, " %02x", data[u]);
		}
		if (len > 0) {
			fprintf(stderr, "\n");
		}
	}
}

static void
cc_end_name(const br_ssl_client_certificate_class **pctx)
{
	(void)pctx;
}

static void
cc_end_name_list(const br_ssl_client_certificate_class **pctx)
{
	ccert_context *zc;

	zc = (ccert_context *)pctx;
	if (zc->verbose) {
		fprintf(stderr, "--- anchor DN list end ---\n");
	}
}

static void
print_hashes(unsigned hh, unsigned hh2)
{
	int i;

	for (i = 0; i < 8; i ++) {
		const char *name;

		name = hash_function_name(i);
		if (((hh >> i) & 1) != 0) {
			fprintf(stderr, " %s", name);
		} else if (((hh2 >> i) & 1) != 0) {
			fprintf(stderr, " (%s)", name);
		}
	}
}

static int
choose_hash(unsigned hh)
{
	static const int f[] = {
		br_sha256_ID, br_sha224_ID, br_sha384_ID, br_sha512_ID,
		br_sha1_ID, br_md5sha1_ID, -1
	};

	size_t u;

	for (u = 0; f[u] >= 0; u ++) {
		if (((hh >> f[u]) & 1) != 0) {
			return f[u];
		}
	}
	return -1;
}

static void
cc_choose(const br_ssl_client_certificate_class **pctx,
	const br_ssl_client_context *cc, uint32_t auth_types,
	br_ssl_client_certificate *choices)
{
	ccert_context *zc;
	int scurve;

	zc = (ccert_context *)pctx;
	scurve = br_ssl_client_get_server_curve(cc);
	if (zc->verbose) {
		unsigned hashes;

		hashes = br_ssl_client_get_server_hashes(cc);
		if ((auth_types & 0x00FF) != 0) {
			fprintf(stderr, "supported: RSA signatures:");
			print_hashes(auth_types, hashes);
			fprintf(stderr, "\n");
		}
		if ((auth_types & 0xFF00) != 0) {
			fprintf(stderr, "supported: ECDSA signatures:");
			print_hashes(auth_types >> 8, hashes >> 8);
			fprintf(stderr, "\n");
		}
		if ((auth_types & 0x010000) != 0) {
			fprintf(stderr, "supported:"
				" fixed ECDH (cert signed with RSA)\n");
		}
		if ((auth_types & 0x020000) != 0) {
			fprintf(stderr, "supported:"
				" fixed ECDH (cert signed with ECDSA)\n");
		}
		if (scurve) {
			fprintf(stderr, "server key curve: %s (%d)\n",
				ec_curve_name(scurve), scurve);
		} else {
			fprintf(stderr, "server key is not EC\n");
		}
	}
	switch (zc->sk->key_type) {
	case BR_KEYTYPE_RSA:
		if ((choices->hash_id = choose_hash(auth_types)) >= 0) {
			if (zc->verbose) {
				fprintf(stderr, "using RSA, hash = %d (%s)\n",
					choices->hash_id,
					hash_function_name(choices->hash_id));
			}
			choices->auth_type = BR_AUTH_RSA;
			choices->chain = zc->chain;
			choices->chain_len = zc->chain_len;
			return;
		}
		break;
	case BR_KEYTYPE_EC:
		if (zc->issuer_key_type != 0
			&& scurve == zc->sk->key.ec.curve)
		{
			int x;

			x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;
			if (((auth_types >> x) & 1) != 0) {
				if (zc->verbose) {
					fprintf(stderr, "using static ECDH\n");
				}
				choices->auth_type = BR_AUTH_ECDH;
				choices->hash_id = -1;
				choices->chain = zc->chain;
				choices->chain_len = zc->chain_len;
				return;
			}
		}
		if ((choices->hash_id = choose_hash(auth_types >> 8)) >= 0) {
			if (zc->verbose) {
				fprintf(stderr, "using ECDSA, hash = %d (%s)\n",
					choices->hash_id,
					hash_function_name(choices->hash_id));
			}
			choices->auth_type = BR_AUTH_ECDSA;
			choices->chain = zc->chain;
			choices->chain_len = zc->chain_len;
			return;
		}
		break;
	}
	if (zc->verbose) {
		fprintf(stderr, "no matching client certificate\n");
	}
	choices->chain = NULL;
	choices->chain_len = 0;
}

static uint32_t
cc_do_keyx(const br_ssl_client_certificate_class **pctx,
	unsigned char *data, size_t *len)
{
	const br_ec_impl *iec;
	ccert_context *zc;
	size_t xoff, xlen;
	uint32_t r;

	zc = (ccert_context *)pctx;
	iec = br_ec_get_default();
	r = iec->mul(data, *len, zc->sk->key.ec.x,
		zc->sk->key.ec.xlen, zc->sk->key.ec.curve);
	xoff = iec->xoff(zc->sk->key.ec.curve, &xlen);
	memmove(data, data + xoff, xlen);
	*len = xlen;
	return r;
}

static size_t
cc_do_sign(const br_ssl_client_certificate_class **pctx,
	int hash_id, size_t hv_len, unsigned char *data, size_t len)
{
	ccert_context *zc;
	unsigned char hv[64];

	zc = (ccert_context *)pctx;
	memcpy(hv, data, hv_len);
	switch (zc->sk->key_type) {
		const br_hash_class *hc;
		const unsigned char *hash_oid;
		uint32_t x;
		size_t sig_len;

	case BR_KEYTYPE_RSA:
		hash_oid = get_hash_oid(hash_id);
		if (hash_oid == NULL && hash_id != 0) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: cannot RSA-sign with"
					" unknown hash function: %d\n",
					hash_id);
			}
			return 0;
		}
		sig_len = (zc->sk->key.rsa.n_bitlen + 7) >> 3;
		if (len < sig_len) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: cannot RSA-sign,"
					" buffer is too small"
					" (sig=%lu, buf=%lu)\n",
					(unsigned long)sig_len,
					(unsigned long)len);
			}
			return 0;
		}
		x = br_rsa_pkcs1_sign_get_default()(
			hash_oid, hv, hv_len, &zc->sk->key.rsa, data);
		if (!x) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: RSA-sign failure\n");
			}
			return 0;
		}
		return sig_len;

	case BR_KEYTYPE_EC:
		hc = get_hash_impl(hash_id);
		if (hc == NULL) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: cannot ECDSA-sign with"
					" unknown hash function: %d\n",
					hash_id);
			}
			return 0;
		}
		if (len < 139) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: cannot ECDSA-sign"
					" (output buffer = %lu)\n",
					(unsigned long)len);
			}
			return 0;
		}
		sig_len = br_ecdsa_sign_asn1_get_default()(
			br_ec_get_default(), hc, hv, &zc->sk->key.ec, data);
		if (sig_len == 0) {
			if (zc->verbose) {
				fprintf(stderr, "ERROR: ECDSA-sign failure\n");
			}
			return 0;
		}
		return sig_len;

	default:
		return 0;
	}
}

static const br_ssl_client_certificate_class ccert_vtable = {
	sizeof(ccert_context),
	cc_start_name_list,
	cc_start_name,
	cc_append_name,
	cc_end_name,
	cc_end_name_list,
	cc_choose,
	cc_do_keyx,
	cc_do_sign
};

static void
free_alpn(void *alpn)
{
	xfree(*(char **)alpn);
}

static void
usage_client(void)
{
	fprintf(stderr,
"usage: brssl client server[:port] [ options ]\n");
	fprintf(stderr,
"options:\n");
	fprintf(stderr,
"   -q              suppress verbose messages\n");
	fprintf(stderr,
"   -trace          activate extra debug messages (dump of all packets)\n");
	fprintf(stderr,
"   -sni name       use this specific name for SNI\n");
	fprintf(stderr,
"   -nosni          do not send any SNI\n");
	fprintf(stderr,
"   -mono           use monodirectional buffering\n");
	fprintf(stderr,
"   -buf length     set the I/O buffer length (in bytes)\n");
	fprintf(stderr,
"   -CA file        add certificates in 'file' to trust anchors\n");
	fprintf(stderr,
"   -cert file      set client certificate chain\n");
	fprintf(stderr,
"   -key file       set client private key (for certificate authentication)\n");
	fprintf(stderr,
"   -nostaticecdh   prohibit full-static ECDH (client certificate)\n");
	fprintf(stderr,
"   -list           list supported names (protocols, algorithms...)\n");
	fprintf(stderr,
"   -vmin name      set minimum supported version (default: TLS-1.0)\n");
	fprintf(stderr,
"   -vmax name      set maximum supported version (default: TLS-1.2)\n");
	fprintf(stderr,
"   -cs names       set list of supported cipher suites (comma-separated)\n");
	fprintf(stderr,
"   -hf names       add support for some hash functions (comma-separated)\n");
	fprintf(stderr,
"   -minhello len   set minimum ClientHello length (in bytes)\n");
	fprintf(stderr,
"   -fallback       send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
	fprintf(stderr,
"   -noreneg        prohibit renegotiations\n");
	fprintf(stderr,
"   -alpn name      add protocol name to list of protocols (ALPN extension)\n");
	fprintf(stderr,
"   -strictalpn     fail on ALPN mismatch\n");
}

/* see brssl.h */
int
do_client(int argc, char *argv[])
{
	int retcode;
	int verbose;
	int trace;
	int i, bidi;
	const char *server_name;
	char *host;
	char *port;
	const char *sni;
	anchor_list anchors = VEC_INIT;
	unsigned vmin, vmax;
	VECTOR(char *) alpn_names = VEC_INIT;
	cipher_suite *suites;
	size_t num_suites;
	uint16_t *suite_ids;
	unsigned hfuns;
	size_t u;
	br_ssl_client_context cc;
	br_x509_minimal_context xc;
	x509_noanchor_context xwc;
	const br_hash_class *dnhash;
	ccert_context zc;
	br_x509_certificate *chain;
	size_t chain_len;
	private_key *sk;
	int nostaticecdh;
	unsigned char *iobuf;
	size_t iobuf_len;
	size_t minhello_len;
	int fallback;
	uint32_t flags;
	SOCKET fd;

	retcode = 0;
	verbose = 1;
	trace = 0;
	server_name = NULL;
	host = NULL;
	port = NULL;
	sni = NULL;
	bidi = 1;
	vmin = 0;
	vmax = 0;
	suites = NULL;
	num_suites = 0;
	hfuns = 0;
	suite_ids = NULL;
	chain = NULL;
	chain_len = 0;
	sk = NULL;
	nostaticecdh = 0;
	iobuf = NULL;
	iobuf_len = 0;
	minhello_len = (size_t)-1;
	fallback = 0;
	flags = 0;
	fd = INVALID_SOCKET;
	for (i = 0; i < argc; i ++) {
		const char *arg;

		arg = argv[i];
		if (arg[0] != '-') {
			if (server_name != NULL) {
				fprintf(stderr,
					"ERROR: duplicate server name\n");
				usage_client();
				goto client_exit_error;
			}
			server_name = arg;
			continue;
		}
		if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
			verbose = 1;
		} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
			verbose = 0;
		} else if (eqstr(arg, "-trace")) {
			trace = 1;
		} else if (eqstr(arg, "-sni")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-sni'\n");
				usage_client();
				goto client_exit_error;
			}
			if (sni != NULL) {
				fprintf(stderr, "ERROR: duplicate SNI\n");
				usage_client();
				goto client_exit_error;
			}
			sni = argv[i];
		} else if (eqstr(arg, "-nosni")) {
			if (sni != NULL) {
				fprintf(stderr, "ERROR: duplicate SNI\n");
				usage_client();
				goto client_exit_error;
			}
			sni = "";
		} else if (eqstr(arg, "-mono")) {
			bidi = 0;
		} else if (eqstr(arg, "-buf")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-buf'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (iobuf_len != 0) {
				fprintf(stderr,
					"ERROR: duplicate I/O buffer length\n");
				usage_client();
				goto client_exit_error;
			}
			iobuf_len = parse_size(arg);
			if (iobuf_len == (size_t)-1) {
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-CA")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-CA'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (read_trust_anchors(&anchors, arg) == 0) {
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-cert")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-cert'\n");
				usage_client();
				goto client_exit_error;
			}
			if (chain != NULL) {
				fprintf(stderr,
					"ERROR: duplicate certificate chain\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			chain = read_certificates(arg, &chain_len);
			if (chain == NULL || chain_len == 0) {
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-key")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-key'\n");
				usage_client();
				goto client_exit_error;
			}
			if (sk != NULL) {
				fprintf(stderr,
					"ERROR: duplicate private key\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			sk = read_private_key(arg);
			if (sk == NULL) {
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-nostaticecdh")) {
			nostaticecdh = 1;
		} else if (eqstr(arg, "-list")) {
			list_names();
			goto client_exit;
		} else if (eqstr(arg, "-vmin")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-vmin'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (vmin != 0) {
				fprintf(stderr,
					"ERROR: duplicate minimum version\n");
				usage_client();
				goto client_exit_error;
			}
			vmin = parse_version(arg, strlen(arg));
			if (vmin == 0) {
				fprintf(stderr,
					"ERROR: unrecognised version '%s'\n",
					arg);
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-vmax")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-vmax'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (vmax != 0) {
				fprintf(stderr,
					"ERROR: duplicate maximum version\n");
				usage_client();
				goto client_exit_error;
			}
			vmax = parse_version(arg, strlen(arg));
			if (vmax == 0) {
				fprintf(stderr,
					"ERROR: unrecognised version '%s'\n",
					arg);
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-cs")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-cs'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (suites != NULL) {
				fprintf(stderr, "ERROR: duplicate list"
					" of cipher suites\n");
				usage_client();
				goto client_exit_error;
			}
			suites = parse_suites(arg, &num_suites);
			if (suites == NULL) {
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-hf")) {
			unsigned x;

			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-hf'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			x = parse_hash_functions(arg);
			if (x == 0) {
				usage_client();
				goto client_exit_error;
			}
			hfuns |= x;
		} else if (eqstr(arg, "-minhello")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-minhello'\n");
				usage_client();
				goto client_exit_error;
			}
			arg = argv[i];
			if (minhello_len != (size_t)-1) {
				fprintf(stderr, "ERROR: duplicate minimum"
					" ClientHello length\n");
				usage_client();
				goto client_exit_error;
			}
			minhello_len = parse_size(arg);
			/*
			 * Minimum ClientHello length must fit on 16 bits.
			 */
			if (minhello_len == (size_t)-1
				|| (((minhello_len >> 12) >> 4) != 0))
			{
				usage_client();
				goto client_exit_error;
			}
		} else if (eqstr(arg, "-fallback")) {
			fallback = 1;
		} else if (eqstr(arg, "-noreneg")) {
			flags |= BR_OPT_NO_RENEGOTIATION;
		} else if (eqstr(arg, "-alpn")) {
			if (++ i >= argc) {
				fprintf(stderr,
					"ERROR: no argument for '-alpn'\n");
				usage_client();
				goto client_exit_error;
			}
			VEC_ADD(alpn_names, xstrdup(argv[i]));
		} else if (eqstr(arg, "-strictalpn")) {
			flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
		} else {
			fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
			usage_client();
			goto client_exit_error;
		}
	}
	if (server_name == NULL) {
		fprintf(stderr, "ERROR: no server name/address provided\n");
		usage_client();
		goto client_exit_error;
	}
	for (u = strlen(server_name); u > 0; u --) {
		int c = server_name[u - 1];
		if (c == ':') {
			break;
		}
		if (c < '0' || c > '9') {
			u = 0;
			break;
		}
	}
	if (u == 0) {
		host = xstrdup(server_name);
		port = xstrdup("443");
	} else {
		port = xstrdup(server_name + u);
		host = xmalloc(u);
		memcpy(host, server_name, u - 1);
		host[u - 1] = 0;
	}
	if (sni == NULL) {
		sni = host;
	}

	if (chain == NULL && sk != NULL) {
		fprintf(stderr, "ERROR: private key specified, but"
			" no certificate chain\n");
		usage_client();
		goto client_exit_error;
	}
	if (chain != NULL && sk == NULL) {
		fprintf(stderr, "ERROR: certificate chain specified, but"
			" no private key\n");
		usage_client();
		goto client_exit_error;
	}

	if (vmin == 0) {
		vmin = BR_TLS10;
	}
	if (vmax == 0) {
		vmax = BR_TLS12;
	}
	if (vmax < vmin) {
		fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
			" version combination\n");
		usage_client();
		goto client_exit_error;
	}
	if (suites == NULL) {
		num_suites = 0;

		for (u = 0; cipher_suites[u].name; u ++) {
			if ((cipher_suites[u].req & REQ_TLS12) == 0
				|| vmax >= BR_TLS12)
			{
				num_suites ++;
			}
		}
		suites = xmalloc(num_suites * sizeof *suites);
		num_suites = 0;
		for (u = 0; cipher_suites[u].name; u ++) {
			if ((cipher_suites[u].req & REQ_TLS12) == 0
				|| vmax >= BR_TLS12)
			{
				suites[num_suites ++] = cipher_suites[u];
			}
		}
	}
	if (hfuns == 0) {
		hfuns = (unsigned)-1;
	}
	if (iobuf_len == 0) {
		if (bidi) {
			iobuf_len = BR_SSL_BUFSIZE_BIDI;
		} else {
			iobuf_len = BR_SSL_BUFSIZE_MONO;
		}
	}
	iobuf = xmalloc(iobuf_len);

	/*
	 * Compute implementation requirements and inject implementations.
	 */
	suite_ids = xmalloc((num_suites + 1) * sizeof *suite_ids);
	br_ssl_client_zero(&cc);
	br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
	dnhash = NULL;
	for (u = 0; hash_functions[u].name; u ++) {
		const br_hash_class *hc;
		int id;

		hc = hash_functions[u].hclass;
		id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
		if ((hfuns & ((unsigned)1 << id)) != 0) {
			dnhash = hc;
		}
	}
	if (dnhash == NULL) {
		fprintf(stderr, "ERROR: no supported hash function\n");
		goto client_exit_error;
	}
	br_x509_minimal_init(&xc, dnhash,
		&VEC_ELT(anchors, 0), VEC_LEN(anchors));
	if (vmin <= BR_TLS11) {
		if (!(hfuns & (1 << br_md5_ID))) {
			fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
			goto client_exit_error;
		}
		if (!(hfuns & (1 << br_sha1_ID))) {
			fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
			goto client_exit_error;
		}
	}
	for (u = 0; u < num_suites; u ++) {
		unsigned req;

		req = suites[u].req;
		suite_ids[u] = suites[u].suite;
		if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
			fprintf(stderr,
				"ERROR: cipher suite %s requires TLS 1.2\n",
				suites[u].name);
			goto client_exit_error;
		}
		if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
			fprintf(stderr,
				"ERROR: cipher suite %s requires SHA-1\n",
				suites[u].name);
			goto client_exit_error;
		}
		if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
			fprintf(stderr,
				"ERROR: cipher suite %s requires SHA-256\n",
				suites[u].name);
			goto client_exit_error;
		}
		if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
			fprintf(stderr,
				"ERROR: cipher suite %s requires SHA-384\n",
				suites[u].name);
			goto client_exit_error;
		}
		/* TODO: algorithm implementation selection */
		if ((req & REQ_AESCBC) != 0) {
			br_ssl_engine_set_default_aes_cbc(&cc.eng);
		}
		if ((req & REQ_AESCCM) != 0) {
			br_ssl_engine_set_default_aes_ccm(&cc.eng);
		}
		if ((req & REQ_AESGCM) != 0) {
			br_ssl_engine_set_default_aes_gcm(&cc.eng);
		}
		if ((req & REQ_CHAPOL) != 0) {
			br_ssl_engine_set_default_chapol(&cc.eng);
		}
		if ((req & REQ_3DESCBC) != 0) {
			br_ssl_engine_set_default_des_cbc(&cc.eng);
		}
		if ((req & REQ_RSAKEYX) != 0) {
			br_ssl_client_set_default_rsapub(&cc);
		}
		if ((req & REQ_ECDHE_RSA) != 0) {
			br_ssl_engine_set_default_ec(&cc.eng);
			br_ssl_engine_set_default_rsavrfy(&cc.eng);
		}
		if ((req & REQ_ECDHE_ECDSA) != 0) {
			br_ssl_engine_set_default_ecdsa(&cc.eng);
		}
		if ((req & REQ_ECDH) != 0) {
			br_ssl_engine_set_default_ec(&cc.eng);
		}
	}
	if (fallback) {
		suite_ids[num_suites ++] = 0x5600;
	}
	br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);

	for (u = 0; hash_functions[u].name; u ++) {
		const br_hash_class *hc;
		int id;

		hc = hash_functions[u].hclass;
		id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
		if ((hfuns & ((unsigned)1 << id)) != 0) {
			br_ssl_engine_set_hash(&cc.eng, id, hc);
			br_x509_minimal_set_hash(&xc, id, hc);
		}
	}
	if (vmin <= BR_TLS11) {
		br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
	}
	if (vmax >= BR_TLS12) {
		if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
			br_ssl_engine_set_prf_sha256(&cc.eng,
				&br_tls12_sha256_prf);
		}
		if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
			br_ssl_engine_set_prf_sha384(&cc.eng,
				&br_tls12_sha384_prf);
		}
	}
	br_x509_minimal_set_rsa(&xc, br_rsa_pkcs1_vrfy_get_default());
	br_x509_minimal_set_ecdsa(&xc,
		br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());

	/*
	 * If there is no provided trust anchor, then certificate validation
	 * will always fail. In that situation, we use our custom wrapper
	 * that tolerates unknown anchors.
	 */
	if (VEC_LEN(anchors) == 0) {
		if (verbose) {
			fprintf(stderr,
				"WARNING: no configured trust anchor\n");
		}
		x509_noanchor_init(&xwc, &xc.vtable);
		br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
	} else {
		br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
	}

	if (minhello_len != (size_t)-1) {
		br_ssl_client_set_min_clienthello_len(&cc, minhello_len);
	}
	br_ssl_engine_set_all_flags(&cc.eng, flags);
	if (VEC_LEN(alpn_names) != 0) {
		br_ssl_engine_set_protocol_names(&cc.eng,
			(const char **)&VEC_ELT(alpn_names, 0),
			VEC_LEN(alpn_names));
	}

	if (chain != NULL) {
		zc.vtable = &ccert_vtable;
		zc.verbose = verbose;
		zc.chain = chain;
		zc.chain_len = chain_len;
		zc.sk = sk;
		if (nostaticecdh || sk->key_type != BR_KEYTYPE_EC) {
			zc.issuer_key_type = 0;
		} else {
			zc.issuer_key_type = get_cert_signer_algo(&chain[0]);
			if (zc.issuer_key_type == 0) {
				goto client_exit_error;
			}
		}
		br_ssl_client_set_client_certificate(&cc, &zc.vtable);
	}

	br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
	br_ssl_client_reset(&cc, sni, 0);

	/*
	 * On Unix systems, we need to avoid SIGPIPE.
	 */
#ifndef _WIN32
	signal(SIGPIPE, SIG_IGN);
#endif

	/*
	 * Connect to the peer.
	 */
	fd = host_connect(host, port, verbose);
	if (fd == INVALID_SOCKET) {
		goto client_exit_error;
	}

	/*
	 * Run the engine until completion.
	 */
	if (run_ssl_engine(&cc.eng, fd,
		(verbose ? RUN_ENGINE_VERBOSE : 0)
		| (trace ? RUN_ENGINE_TRACE : 0)) != 0)
	{
		goto client_exit_error;
	} else {
		goto client_exit;
	}

	/*
	 * Release allocated structures.
	 */
client_exit:
	xfree(host);
	xfree(port);
	xfree(suites);
	xfree(suite_ids);
	VEC_CLEAREXT(anchors, &free_ta_contents);
	VEC_CLEAREXT(alpn_names, &free_alpn);
	free_certificates(chain, chain_len);
	free_private_key(sk);
	xfree(iobuf);
	if (fd != INVALID_SOCKET) {
#ifdef _WIN32
		closesocket(fd);
#else
		close(fd);
#endif
	}
	return retcode;

client_exit_error:
	retcode = -1;
	goto client_exit;
}