2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #define INVALID_SOCKET (-1)
51 host_connect(const char *host
, const char *port
, int verbose
)
53 struct addrinfo hints
, *si
, *p
;
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
);
62 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
64 return INVALID_SOCKET
;
67 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
71 char tmp
[INET6_ADDRSTRLEN
+ 50];
73 sa
= (struct sockaddr
*)p
->ai_addr
;
74 if (sa
->sa_family
== AF_INET
) {
75 addr
= &((struct sockaddr_in
*)
76 (void *)sa
)->sin_addr
;
77 } else if (sa
->sa_family
== AF_INET6
) {
78 addr
= &((struct sockaddr_in6
*)
79 (void *)sa
)->sin6_addr
;
84 if (!inet_ntop(p
->ai_family
, addr
,
87 strcpy(tmp
, "<invalid>");
90 sprintf(tmp
, "<unknown family: %d>",
93 fprintf(stderr
, "connecting to: %s\n", tmp
);
95 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
96 if (fd
== INVALID_SOCKET
) {
102 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) == INVALID_SOCKET
) {
117 fprintf(stderr
, "ERROR: failed to connect\n");
118 return INVALID_SOCKET
;
122 fprintf(stderr
, "connected.\n");
126 * We make the socket non-blocking, since we are going to use
127 * poll() or select() to organise I/O.
134 ioctlsocket(fd
, FIONBIO
, &arg
);
137 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
143 const br_ssl_client_certificate_class
*vtable
;
145 br_x509_certificate
*chain
;
152 cc_start_name_list(const br_ssl_client_certificate_class
**pctx
)
156 zc
= (ccert_context
*)pctx
;
158 fprintf(stderr
, "Server requests a client certificate.\n");
159 fprintf(stderr
, "--- anchor DN list start ---\n");
164 cc_start_name(const br_ssl_client_certificate_class
**pctx
, size_t len
)
168 zc
= (ccert_context
*)pctx
;
170 fprintf(stderr
, "new anchor name, length = %u\n",
176 cc_append_name(const br_ssl_client_certificate_class
**pctx
,
177 const unsigned char *data
, size_t len
)
181 zc
= (ccert_context
*)pctx
;
185 for (u
= 0; u
< len
; u
++) {
187 fprintf(stderr
, " ");
188 } else if (u
> 0 && u
% 16 == 0) {
189 fprintf(stderr
, "\n ");
191 fprintf(stderr
, " %02x", data
[u
]);
194 fprintf(stderr
, "\n");
200 cc_end_name(const br_ssl_client_certificate_class
**pctx
)
206 cc_end_name_list(const br_ssl_client_certificate_class
**pctx
)
210 zc
= (ccert_context
*)pctx
;
212 fprintf(stderr
, "--- anchor DN list end ---\n");
217 print_hashes(unsigned hh
, unsigned hh2
)
221 for (i
= 0; i
< 8; i
++) {
224 name
= hash_function_name(i
);
225 if (((hh
>> i
) & 1) != 0) {
226 fprintf(stderr
, " %s", name
);
227 } else if (((hh2
>> i
) & 1) != 0) {
228 fprintf(stderr
, " (%s)", name
);
234 choose_hash(unsigned hh
)
236 static const int f
[] = {
237 br_sha256_ID
, br_sha224_ID
, br_sha384_ID
, br_sha512_ID
,
238 br_sha1_ID
, br_md5sha1_ID
, -1
243 for (u
= 0; f
[u
] >= 0; u
++) {
244 if (((hh
>> f
[u
]) & 1) != 0) {
252 cc_choose(const br_ssl_client_certificate_class
**pctx
,
253 const br_ssl_client_context
*cc
, uint32_t auth_types
,
254 br_ssl_client_certificate
*choices
)
259 zc
= (ccert_context
*)pctx
;
260 scurve
= br_ssl_client_get_server_curve(cc
);
264 hashes
= br_ssl_client_get_server_hashes(cc
);
265 if ((auth_types
& 0x00FF) != 0) {
266 fprintf(stderr
, "supported: RSA signatures:");
267 print_hashes(auth_types
, hashes
);
268 fprintf(stderr
, "\n");
270 if ((auth_types
& 0xFF00) != 0) {
271 fprintf(stderr
, "supported: ECDSA signatures:");
272 print_hashes(auth_types
>> 8, hashes
>> 8);
273 fprintf(stderr
, "\n");
275 if ((auth_types
& 0x010000) != 0) {
276 fprintf(stderr
, "supported:"
277 " fixed ECDH (cert signed with RSA)\n");
279 if ((auth_types
& 0x020000) != 0) {
280 fprintf(stderr
, "supported:"
281 " fixed ECDH (cert signed with ECDSA)\n");
284 fprintf(stderr
, "server key curve: %s (%d)\n",
285 ec_curve_name(scurve
), scurve
);
287 fprintf(stderr
, "server key is not EC\n");
290 switch (zc
->sk
->key_type
) {
292 if ((choices
->hash_id
= choose_hash(auth_types
)) >= 0) {
294 fprintf(stderr
, "using RSA, hash = %d (%s)\n",
296 hash_function_name(choices
->hash_id
));
298 choices
->auth_type
= BR_AUTH_RSA
;
299 choices
->chain
= zc
->chain
;
300 choices
->chain_len
= zc
->chain_len
;
305 if (zc
->issuer_key_type
!= 0
306 && scurve
== zc
->sk
->key
.ec
.curve
)
310 x
= (zc
->issuer_key_type
== BR_KEYTYPE_RSA
) ? 16 : 17;
311 if (((auth_types
>> x
) & 1) != 0) {
313 fprintf(stderr
, "using static ECDH\n");
315 choices
->auth_type
= BR_AUTH_ECDH
;
316 choices
->hash_id
= -1;
317 choices
->chain
= zc
->chain
;
318 choices
->chain_len
= zc
->chain_len
;
322 if ((choices
->hash_id
= choose_hash(auth_types
>> 8)) >= 0) {
324 fprintf(stderr
, "using ECDSA, hash = %d (%s)\n",
326 hash_function_name(choices
->hash_id
));
328 choices
->auth_type
= BR_AUTH_ECDSA
;
329 choices
->chain
= zc
->chain
;
330 choices
->chain_len
= zc
->chain_len
;
336 fprintf(stderr
, "no matching client certificate\n");
338 choices
->chain
= NULL
;
339 choices
->chain_len
= 0;
343 cc_do_keyx(const br_ssl_client_certificate_class
**pctx
,
344 unsigned char *data
, size_t *len
)
346 const br_ec_impl
*iec
;
351 zc
= (ccert_context
*)pctx
;
352 iec
= br_ec_get_default();
353 r
= iec
->mul(data
, *len
, zc
->sk
->key
.ec
.x
,
354 zc
->sk
->key
.ec
.xlen
, zc
->sk
->key
.ec
.curve
);
355 xoff
= iec
->xoff(zc
->sk
->key
.ec
.curve
, &xlen
);
356 memmove(data
, data
+ xoff
, xlen
);
362 cc_do_sign(const br_ssl_client_certificate_class
**pctx
,
363 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
)
366 unsigned char hv
[64];
368 zc
= (ccert_context
*)pctx
;
369 memcpy(hv
, data
, hv_len
);
370 switch (zc
->sk
->key_type
) {
371 const br_hash_class
*hc
;
372 const unsigned char *hash_oid
;
377 hash_oid
= get_hash_oid(hash_id
);
378 if (hash_oid
== NULL
&& hash_id
!= 0) {
380 fprintf(stderr
, "ERROR: cannot RSA-sign with"
381 " unknown hash function: %d\n",
386 sig_len
= (zc
->sk
->key
.rsa
.n_bitlen
+ 7) >> 3;
389 fprintf(stderr
, "ERROR: cannot RSA-sign,"
390 " buffer is too small"
391 " (sig=%lu, buf=%lu)\n",
392 (unsigned long)sig_len
,
397 x
= br_rsa_pkcs1_sign_get_default()(
398 hash_oid
, hv
, hv_len
, &zc
->sk
->key
.rsa
, data
);
401 fprintf(stderr
, "ERROR: RSA-sign failure\n");
408 hc
= get_hash_impl(hash_id
);
411 fprintf(stderr
, "ERROR: cannot ECDSA-sign with"
412 " unknown hash function: %d\n",
419 fprintf(stderr
, "ERROR: cannot ECDSA-sign"
420 " (output buffer = %lu)\n",
425 sig_len
= br_ecdsa_sign_asn1_get_default()(
426 br_ec_get_default(), hc
, hv
, &zc
->sk
->key
.ec
, data
);
429 fprintf(stderr
, "ERROR: ECDSA-sign failure\n");
440 static const br_ssl_client_certificate_class ccert_vtable
= {
441 sizeof(ccert_context
),
453 free_alpn(void *alpn
)
455 xfree(*(char **)alpn
);
462 "usage: brssl client server[:port] [ options ]\n");
466 " -q suppress verbose messages\n");
468 " -trace activate extra debug messages (dump of all packets)\n");
470 " -sni name use this specific name for SNI\n");
472 " -nosni do not send any SNI\n");
474 " -mono use monodirectional buffering\n");
476 " -buf length set the I/O buffer length (in bytes)\n");
478 " -CA file add certificates in 'file' to trust anchors\n");
480 " -cert file set client certificate chain\n");
482 " -key file set client private key (for certificate authentication)\n");
484 " -nostaticecdh prohibit full-static ECDH (client certificate)\n");
486 " -list list supported names (protocols, algorithms...)\n");
488 " -vmin name set minimum supported version (default: TLS-1.0)\n");
490 " -vmax name set maximum supported version (default: TLS-1.2)\n");
492 " -cs names set list of supported cipher suites (comma-separated)\n");
494 " -hf names add support for some hash functions (comma-separated)\n");
496 " -minhello len set minimum ClientHello length (in bytes)\n");
498 " -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
500 " -noreneg prohibit renegotiations\n");
502 " -alpn name add protocol name to list of protocols (ALPN extension)\n");
504 " -strictalpn fail on ALPN mismatch\n");
509 do_client(int argc
, char *argv
[])
515 const char *server_name
;
519 anchor_list anchors
= VEC_INIT
;
521 VECTOR(char *) alpn_names
= VEC_INIT
;
522 cipher_suite
*suites
;
527 br_ssl_client_context cc
;
528 br_x509_minimal_context xc
;
529 x509_noanchor_context xwc
;
530 const br_hash_class
*dnhash
;
532 br_x509_certificate
*chain
;
536 unsigned char *iobuf
;
563 minhello_len
= (size_t)-1;
567 for (i
= 0; i
< argc
; i
++) {
572 if (server_name
!= NULL
) {
574 "ERROR: duplicate server name\n");
576 goto client_exit_error
;
581 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
583 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
585 } else if (eqstr(arg
, "-trace")) {
587 } else if (eqstr(arg
, "-sni")) {
590 "ERROR: no argument for '-sni'\n");
592 goto client_exit_error
;
595 fprintf(stderr
, "ERROR: duplicate SNI\n");
597 goto client_exit_error
;
600 } else if (eqstr(arg
, "-nosni")) {
602 fprintf(stderr
, "ERROR: duplicate SNI\n");
604 goto client_exit_error
;
607 } else if (eqstr(arg
, "-mono")) {
609 } else if (eqstr(arg
, "-buf")) {
612 "ERROR: no argument for '-buf'\n");
614 goto client_exit_error
;
617 if (iobuf_len
!= 0) {
619 "ERROR: duplicate I/O buffer length\n");
621 goto client_exit_error
;
623 iobuf_len
= parse_size(arg
);
624 if (iobuf_len
== (size_t)-1) {
626 goto client_exit_error
;
628 } else if (eqstr(arg
, "-CA")) {
631 "ERROR: no argument for '-CA'\n");
633 goto client_exit_error
;
636 if (read_trust_anchors(&anchors
, arg
) == 0) {
638 goto client_exit_error
;
640 } else if (eqstr(arg
, "-cert")) {
643 "ERROR: no argument for '-cert'\n");
645 goto client_exit_error
;
649 "ERROR: duplicate certificate chain\n");
651 goto client_exit_error
;
654 chain
= read_certificates(arg
, &chain_len
);
655 if (chain
== NULL
|| chain_len
== 0) {
656 goto client_exit_error
;
658 } else if (eqstr(arg
, "-key")) {
661 "ERROR: no argument for '-key'\n");
663 goto client_exit_error
;
667 "ERROR: duplicate private key\n");
669 goto client_exit_error
;
672 sk
= read_private_key(arg
);
674 goto client_exit_error
;
676 } else if (eqstr(arg
, "-nostaticecdh")) {
678 } else if (eqstr(arg
, "-list")) {
681 } else if (eqstr(arg
, "-vmin")) {
684 "ERROR: no argument for '-vmin'\n");
686 goto client_exit_error
;
691 "ERROR: duplicate minimum version\n");
693 goto client_exit_error
;
695 vmin
= parse_version(arg
, strlen(arg
));
698 "ERROR: unrecognised version '%s'\n",
701 goto client_exit_error
;
703 } else if (eqstr(arg
, "-vmax")) {
706 "ERROR: no argument for '-vmax'\n");
708 goto client_exit_error
;
713 "ERROR: duplicate maximum version\n");
715 goto client_exit_error
;
717 vmax
= parse_version(arg
, strlen(arg
));
720 "ERROR: unrecognised version '%s'\n",
723 goto client_exit_error
;
725 } else if (eqstr(arg
, "-cs")) {
728 "ERROR: no argument for '-cs'\n");
730 goto client_exit_error
;
733 if (suites
!= NULL
) {
734 fprintf(stderr
, "ERROR: duplicate list"
735 " of cipher suites\n");
737 goto client_exit_error
;
739 suites
= parse_suites(arg
, &num_suites
);
740 if (suites
== NULL
) {
742 goto client_exit_error
;
744 } else if (eqstr(arg
, "-hf")) {
749 "ERROR: no argument for '-hf'\n");
751 goto client_exit_error
;
754 x
= parse_hash_functions(arg
);
757 goto client_exit_error
;
760 } else if (eqstr(arg
, "-minhello")) {
763 "ERROR: no argument for '-minhello'\n");
765 goto client_exit_error
;
768 if (minhello_len
!= (size_t)-1) {
769 fprintf(stderr
, "ERROR: duplicate minimum"
770 " ClientHello length\n");
772 goto client_exit_error
;
774 minhello_len
= parse_size(arg
);
776 * Minimum ClientHello length must fit on 16 bits.
778 if (minhello_len
== (size_t)-1
779 || (((minhello_len
>> 12) >> 4) != 0))
782 goto client_exit_error
;
784 } else if (eqstr(arg
, "-fallback")) {
786 } else if (eqstr(arg
, "-noreneg")) {
787 flags
|= BR_OPT_NO_RENEGOTIATION
;
788 } else if (eqstr(arg
, "-alpn")) {
791 "ERROR: no argument for '-alpn'\n");
793 goto client_exit_error
;
795 VEC_ADD(alpn_names
, xstrdup(argv
[i
]));
796 } else if (eqstr(arg
, "-strictalpn")) {
797 flags
|= BR_OPT_FAIL_ON_ALPN_MISMATCH
;
799 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
801 goto client_exit_error
;
804 if (server_name
== NULL
) {
805 fprintf(stderr
, "ERROR: no server name/address provided\n");
807 goto client_exit_error
;
809 for (u
= strlen(server_name
); u
> 0; u
--) {
810 int c
= server_name
[u
- 1];
814 if (c
< '0' || c
> '9') {
820 host
= xstrdup(server_name
);
821 port
= xstrdup("443");
823 port
= xstrdup(server_name
+ u
);
825 memcpy(host
, server_name
, u
- 1);
832 if (chain
== NULL
&& sk
!= NULL
) {
833 fprintf(stderr
, "ERROR: private key specified, but"
834 " no certificate chain\n");
836 goto client_exit_error
;
838 if (chain
!= NULL
&& sk
== NULL
) {
839 fprintf(stderr
, "ERROR: certificate chain specified, but"
840 " no private key\n");
842 goto client_exit_error
;
852 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
853 " version combination\n");
855 goto client_exit_error
;
857 if (suites
== NULL
) {
860 for (u
= 0; cipher_suites
[u
].name
; u
++) {
861 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
867 suites
= xmalloc(num_suites
* sizeof *suites
);
869 for (u
= 0; cipher_suites
[u
].name
; u
++) {
870 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
873 suites
[num_suites
++] = cipher_suites
[u
];
878 hfuns
= (unsigned)-1;
880 if (iobuf_len
== 0) {
882 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
884 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
887 iobuf
= xmalloc(iobuf_len
);
890 * Compute implementation requirements and inject implementations.
892 suite_ids
= xmalloc((num_suites
+ 1) * sizeof *suite_ids
);
893 br_ssl_client_zero(&cc
);
894 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
896 for (u
= 0; hash_functions
[u
].name
; u
++) {
897 const br_hash_class
*hc
;
900 hc
= hash_functions
[u
].hclass
;
901 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
902 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
906 if (dnhash
== NULL
) {
907 fprintf(stderr
, "ERROR: no supported hash function\n");
908 goto client_exit_error
;
910 br_x509_minimal_init(&xc
, dnhash
,
911 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
912 if (vmin
<= BR_TLS11
) {
913 if (!(hfuns
& (1 << br_md5_ID
))) {
914 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
915 goto client_exit_error
;
917 if (!(hfuns
& (1 << br_sha1_ID
))) {
918 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
919 goto client_exit_error
;
922 for (u
= 0; u
< num_suites
; u
++) {
926 suite_ids
[u
] = suites
[u
].suite
;
927 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
929 "ERROR: cipher suite %s requires TLS 1.2\n",
931 goto client_exit_error
;
933 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
935 "ERROR: cipher suite %s requires SHA-1\n",
937 goto client_exit_error
;
939 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
941 "ERROR: cipher suite %s requires SHA-256\n",
943 goto client_exit_error
;
945 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
947 "ERROR: cipher suite %s requires SHA-384\n",
949 goto client_exit_error
;
951 /* TODO: algorithm implementation selection */
952 if ((req
& REQ_AESCBC
) != 0) {
953 br_ssl_engine_set_default_aes_cbc(&cc
.eng
);
955 if ((req
& REQ_AESGCM
) != 0) {
956 br_ssl_engine_set_default_aes_gcm(&cc
.eng
);
958 if ((req
& REQ_CHAPOL
) != 0) {
959 br_ssl_engine_set_default_chapol(&cc
.eng
);
961 if ((req
& REQ_3DESCBC
) != 0) {
962 br_ssl_engine_set_default_des_cbc(&cc
.eng
);
964 if ((req
& REQ_RSAKEYX
) != 0) {
965 br_ssl_client_set_default_rsapub(&cc
);
967 if ((req
& REQ_ECDHE_RSA
) != 0) {
968 br_ssl_engine_set_default_ec(&cc
.eng
);
969 br_ssl_engine_set_default_rsavrfy(&cc
.eng
);
971 if ((req
& REQ_ECDHE_ECDSA
) != 0) {
972 br_ssl_engine_set_default_ecdsa(&cc
.eng
);
974 if ((req
& REQ_ECDH
) != 0) {
975 br_ssl_engine_set_default_ec(&cc
.eng
);
979 suite_ids
[num_suites
++] = 0x5600;
981 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
983 for (u
= 0; hash_functions
[u
].name
; u
++) {
984 const br_hash_class
*hc
;
987 hc
= hash_functions
[u
].hclass
;
988 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
989 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
990 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
991 br_x509_minimal_set_hash(&xc
, id
, hc
);
994 if (vmin
<= BR_TLS11
) {
995 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
997 if (vmax
>= BR_TLS12
) {
998 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
999 br_ssl_engine_set_prf_sha256(&cc
.eng
,
1000 &br_tls12_sha256_prf
);
1002 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
1003 br_ssl_engine_set_prf_sha384(&cc
.eng
,
1004 &br_tls12_sha384_prf
);
1007 br_x509_minimal_set_rsa(&xc
, br_rsa_pkcs1_vrfy_get_default());
1008 br_x509_minimal_set_ecdsa(&xc
,
1009 br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
1012 * If there is no provided trust anchor, then certificate validation
1013 * will always fail. In that situation, we use our custom wrapper
1014 * that tolerates unknown anchors.
1016 if (VEC_LEN(anchors
) == 0) {
1019 "WARNING: no configured trust anchor\n");
1021 x509_noanchor_init(&xwc
, &xc
.vtable
);
1022 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
1024 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
1027 if (minhello_len
!= (size_t)-1) {
1028 br_ssl_client_set_min_clienthello_len(&cc
, minhello_len
);
1030 br_ssl_engine_set_all_flags(&cc
.eng
, flags
);
1031 if (VEC_LEN(alpn_names
) != 0) {
1032 br_ssl_engine_set_protocol_names(&cc
.eng
,
1033 (const char **)&VEC_ELT(alpn_names
, 0),
1034 VEC_LEN(alpn_names
));
1037 if (chain
!= NULL
) {
1038 zc
.vtable
= &ccert_vtable
;
1039 zc
.verbose
= verbose
;
1041 zc
.chain_len
= chain_len
;
1043 if (nostaticecdh
|| sk
->key_type
!= BR_KEYTYPE_EC
) {
1044 zc
.issuer_key_type
= 0;
1046 zc
.issuer_key_type
= get_cert_signer_algo(&chain
[0]);
1047 if (zc
.issuer_key_type
== 0) {
1048 goto client_exit_error
;
1051 br_ssl_client_set_client_certificate(&cc
, &zc
.vtable
);
1054 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
1055 br_ssl_client_reset(&cc
, sni
, 0);
1058 * On Unix systems, we need to avoid SIGPIPE.
1061 signal(SIGPIPE
, SIG_IGN
);
1065 * Connect to the peer.
1067 fd
= host_connect(host
, port
, verbose
);
1068 if (fd
== INVALID_SOCKET
) {
1069 goto client_exit_error
;
1073 * Run the engine until completion.
1075 if (run_ssl_engine(&cc
.eng
, fd
,
1076 (verbose
? RUN_ENGINE_VERBOSE
: 0)
1077 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
1079 goto client_exit_error
;
1085 * Release allocated structures.
1092 VEC_CLEAREXT(anchors
, &free_ta_contents
);
1093 VEC_CLEAREXT(alpn_names
, &free_alpn
);
1094 free_certificates(chain
, chain_len
);
1095 free_private_key(sk
);
1097 if (fd
!= INVALID_SOCKET
) {