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
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
44 host_connect(const char *host
, const char *port
, int verbose
)
46 struct addrinfo hints
, *si
, *p
;
50 memset(&hints
, 0, sizeof hints
);
51 hints
.ai_family
= PF_UNSPEC
;
52 hints
.ai_socktype
= SOCK_STREAM
;
53 err
= getaddrinfo(host
, port
, &hints
, &si
);
55 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
60 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
64 char tmp
[INET6_ADDRSTRLEN
+ 50];
66 sa
= (struct sockaddr
*)p
->ai_addr
;
67 if (sa
->sa_family
== AF_INET
) {
68 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
69 } else if (sa
->sa_family
== AF_INET6
) {
70 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
75 if (!inet_ntop(p
->ai_family
, addr
,
78 strcpy(tmp
, "<invalid>");
81 sprintf(tmp
, "<unknown family: %d>",
84 fprintf(stderr
, "connecting to: %s\n", tmp
);
86 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
93 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
104 fprintf(stderr
, "ERROR: failed to connect\n");
109 fprintf(stderr
, "connected.\n");
113 * We make the socket non-blocking, since we are going to use
114 * poll() to organise I/O.
116 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
121 const br_ssl_client_certificate_class
*vtable
;
123 br_x509_certificate
*chain
;
130 cc_start_name_list(const br_ssl_client_certificate_class
**pctx
)
134 zc
= (ccert_context
*)pctx
;
136 fprintf(stderr
, "Server requests a client certificate.\n");
137 fprintf(stderr
, "--- anchor DN list start ---\n");
142 cc_start_name(const br_ssl_client_certificate_class
**pctx
, size_t len
)
146 zc
= (ccert_context
*)pctx
;
148 fprintf(stderr
, "new anchor name, length = %u\n",
154 cc_append_name(const br_ssl_client_certificate_class
**pctx
,
155 const unsigned char *data
, size_t len
)
159 zc
= (ccert_context
*)pctx
;
163 for (u
= 0; u
< len
; u
++) {
165 fprintf(stderr
, " ");
166 } else if (u
> 0 && u
% 16 == 0) {
167 fprintf(stderr
, "\n ");
169 fprintf(stderr
, " %02x", data
[u
]);
172 fprintf(stderr
, "\n");
178 cc_end_name(const br_ssl_client_certificate_class
**pctx
)
184 cc_end_name_list(const br_ssl_client_certificate_class
**pctx
)
188 zc
= (ccert_context
*)pctx
;
190 fprintf(stderr
, "--- anchor DN list end ---\n");
195 print_hashes(unsigned hh
, unsigned hh2
)
199 for (i
= 0; i
< 8; i
++) {
202 name
= hash_function_name(i
);
203 if (((hh
>> i
) & 1) != 0) {
204 fprintf(stderr
, " %s", name
);
205 } else if (((hh2
>> i
) & 1) != 0) {
206 fprintf(stderr
, " (%s)", name
);
212 choose_hash(unsigned hh
)
214 static const int f
[] = {
215 br_sha256_ID
, br_sha224_ID
, br_sha384_ID
, br_sha512_ID
,
216 br_sha1_ID
, br_md5sha1_ID
, -1
221 for (u
= 0; f
[u
] >= 0; u
++) {
222 if (((hh
>> f
[u
]) & 1) != 0) {
230 cc_choose(const br_ssl_client_certificate_class
**pctx
,
231 const br_ssl_client_context
*cc
, uint32_t auth_types
,
232 br_ssl_client_certificate
*choices
)
237 zc
= (ccert_context
*)pctx
;
238 scurve
= br_ssl_client_get_server_curve(cc
);
242 hashes
= br_ssl_client_get_server_hashes(cc
);
243 if ((auth_types
& 0x00FF) != 0) {
244 fprintf(stderr
, "supported: RSA signatures:");
245 print_hashes(auth_types
, hashes
);
246 fprintf(stderr
, "\n");
248 if ((auth_types
& 0xFF00) != 0) {
249 fprintf(stderr
, "supported: ECDSA signatures:");
250 print_hashes(auth_types
>> 8, hashes
>> 8);
251 fprintf(stderr
, "\n");
253 if ((auth_types
& 0x010000) != 0) {
254 fprintf(stderr
, "supported:"
255 " fixed ECDH (cert signed with RSA)\n");
257 if ((auth_types
& 0x020000) != 0) {
258 fprintf(stderr
, "supported:"
259 " fixed ECDH (cert signed with ECDSA)\n");
262 fprintf(stderr
, "server key curve: %s (%d)\n",
263 ec_curve_name(scurve
), scurve
);
265 fprintf(stderr
, "server key is not EC\n");
268 switch (zc
->sk
->key_type
) {
270 if ((choices
->hash_id
= choose_hash(auth_types
)) >= 0) {
272 fprintf(stderr
, "using RSA, hash = %d (%s)\n",
274 hash_function_name(choices
->hash_id
));
276 choices
->auth_type
= BR_AUTH_RSA
;
277 choices
->chain
= zc
->chain
;
278 choices
->chain_len
= zc
->chain_len
;
283 if (zc
->issuer_key_type
!= 0
284 && scurve
== zc
->sk
->key
.ec
.curve
)
288 x
= (zc
->issuer_key_type
== BR_KEYTYPE_RSA
) ? 16 : 17;
289 if (((auth_types
>> x
) & 1) != 0) {
291 fprintf(stderr
, "using static ECDH\n");
293 choices
->auth_type
= BR_AUTH_ECDH
;
294 choices
->hash_id
= -1;
295 choices
->chain
= zc
->chain
;
296 choices
->chain_len
= zc
->chain_len
;
300 if ((choices
->hash_id
= choose_hash(auth_types
>> 8)) >= 0) {
302 fprintf(stderr
, "using ECDSA, hash = %d (%s)\n",
304 hash_function_name(choices
->hash_id
));
306 choices
->auth_type
= BR_AUTH_ECDSA
;
307 choices
->chain
= zc
->chain
;
308 choices
->chain_len
= zc
->chain_len
;
314 fprintf(stderr
, "no matching client certificate\n");
316 choices
->chain
= NULL
;
317 choices
->chain_len
= 0;
321 cc_do_keyx(const br_ssl_client_certificate_class
**pctx
,
322 unsigned char *data
, size_t *len
)
328 zc
= (ccert_context
*)pctx
;
329 r
= br_ec_all_m15
.mul(data
, *len
, zc
->sk
->key
.ec
.x
,
330 zc
->sk
->key
.ec
.xlen
, zc
->sk
->key
.ec
.curve
);
331 xoff
= br_ec_all_m15
.xoff(zc
->sk
->key
.ec
.curve
, &xlen
);
332 memmove(data
, data
+ xoff
, xlen
);
338 cc_do_sign(const br_ssl_client_certificate_class
**pctx
,
339 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
)
342 unsigned char hv
[64];
344 zc
= (ccert_context
*)pctx
;
345 memcpy(hv
, data
, hv_len
);
346 switch (zc
->sk
->key_type
) {
347 const br_hash_class
*hc
;
348 const unsigned char *hash_oid
;
353 hash_oid
= get_hash_oid(hash_id
);
354 if (hash_oid
== NULL
&& hash_id
!= 0) {
356 fprintf(stderr
, "ERROR: cannot RSA-sign with"
357 " unknown hash function: %d\n",
362 sig_len
= (zc
->sk
->key
.rsa
.n_bitlen
+ 7) >> 3;
365 fprintf(stderr
, "ERROR: cannot RSA-sign,"
366 " buffer is too small"
367 " (sig=%lu, buf=%lu)\n",
368 (unsigned long)sig_len
,
373 x
= br_rsa_i31_pkcs1_sign(hash_oid
, hv
, hv_len
,
374 &zc
->sk
->key
.rsa
, data
);
377 fprintf(stderr
, "ERROR: RSA-sign failure\n");
384 hc
= get_hash_impl(hash_id
);
387 fprintf(stderr
, "ERROR: cannot ECDSA-sign with"
388 " unknown hash function: %d\n",
395 fprintf(stderr
, "ERROR: cannot ECDSA-sign"
396 " (output buffer = %lu)\n",
401 sig_len
= br_ecdsa_i31_sign_asn1(&br_ec_all_m15
,
402 hc
, hv
, &zc
->sk
->key
.ec
, data
);
405 fprintf(stderr
, "ERROR: ECDSA-sign failure\n");
416 static const br_ssl_client_certificate_class ccert_vtable
= {
417 sizeof(ccert_context
),
429 free_alpn(void *alpn
)
431 xfree(*(char **)alpn
);
438 "usage: brssl client server[:port] [ options ]\n");
442 " -q suppress verbose messages\n");
444 " -trace activate extra debug messages (dump of all packets)\n");
446 " -sni name use this specific name for SNI\n");
448 " -nosni do not send any SNI\n");
450 " -mono use monodirectional buffering\n");
452 " -buf length set the I/O buffer length (in bytes)\n");
454 " -CA file add certificates in 'file' to trust anchors\n");
456 " -cert file set client certificate chain\n");
458 " -key file set client private key (for certificate authentication)\n");
460 " -nostaticecdh prohibit full-static ECDH (client certificate)\n");
462 " -list list supported names (protocols, algorithms...)\n");
464 " -vmin name set minimum supported version (default: TLS-1.0)\n");
466 " -vmax name set maximum supported version (default: TLS-1.2)\n");
468 " -cs names set list of supported cipher suites (comma-separated)\n");
470 " -hf names add support for some hash functions (comma-separated)\n");
472 " -minhello len set minimum ClientHello length (in bytes)\n");
474 " -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
476 " -noreneg prohibit renegotiations\n");
478 " -alpn name add protocol name to list of protocols (ALPN extension)\n");
480 " -strictalpn fail on ALPN mismatch\n");
485 do_client(int argc
, char *argv
[])
491 const char *server_name
;
495 anchor_list anchors
= VEC_INIT
;
497 VECTOR(const char *) alpn_names
= VEC_INIT
;
498 cipher_suite
*suites
;
503 br_ssl_client_context cc
;
504 br_x509_minimal_context xc
;
505 x509_noanchor_context xwc
;
506 const br_hash_class
*dnhash
;
508 br_x509_certificate
*chain
;
512 unsigned char *iobuf
;
539 minhello_len
= (size_t)-1;
543 for (i
= 0; i
< argc
; i
++) {
548 if (server_name
!= NULL
) {
550 "ERROR: duplicate server name\n");
552 goto client_exit_error
;
557 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
559 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
561 } else if (eqstr(arg
, "-trace")) {
563 } else if (eqstr(arg
, "-sni")) {
566 "ERROR: no argument for '-sni'\n");
568 goto client_exit_error
;
571 fprintf(stderr
, "ERROR: duplicate SNI\n");
573 goto client_exit_error
;
576 } else if (eqstr(arg
, "-nosni")) {
578 fprintf(stderr
, "ERROR: duplicate SNI\n");
580 goto client_exit_error
;
583 } else if (eqstr(arg
, "-mono")) {
585 } else if (eqstr(arg
, "-buf")) {
588 "ERROR: no argument for '-buf'\n");
590 goto client_exit_error
;
593 if (iobuf_len
!= 0) {
595 "ERROR: duplicate I/O buffer length\n");
597 goto client_exit_error
;
599 iobuf_len
= parse_size(arg
);
600 if (iobuf_len
== (size_t)-1) {
602 goto client_exit_error
;
604 } else if (eqstr(arg
, "-CA")) {
607 "ERROR: no argument for '-CA'\n");
609 goto client_exit_error
;
612 if (read_trust_anchors(&anchors
, arg
) == 0) {
614 goto client_exit_error
;
616 } else if (eqstr(arg
, "-cert")) {
619 "ERROR: no argument for '-cert'\n");
621 goto client_exit_error
;
625 "ERROR: duplicate certificate chain\n");
627 goto client_exit_error
;
630 chain
= read_certificates(arg
, &chain_len
);
631 if (chain
== NULL
|| chain_len
== 0) {
632 goto client_exit_error
;
634 } else if (eqstr(arg
, "-key")) {
637 "ERROR: no argument for '-key'\n");
639 goto client_exit_error
;
643 "ERROR: duplicate private key\n");
645 goto client_exit_error
;
648 sk
= read_private_key(arg
);
650 goto client_exit_error
;
652 } else if (eqstr(arg
, "-nostaticecdh")) {
654 } else if (eqstr(arg
, "-list")) {
657 } else if (eqstr(arg
, "-vmin")) {
660 "ERROR: no argument for '-vmin'\n");
662 goto client_exit_error
;
667 "ERROR: duplicate minimum version\n");
669 goto client_exit_error
;
671 vmin
= parse_version(arg
, strlen(arg
));
674 "ERROR: unrecognised version '%s'\n",
677 goto client_exit_error
;
679 } else if (eqstr(arg
, "-vmax")) {
682 "ERROR: no argument for '-vmax'\n");
684 goto client_exit_error
;
689 "ERROR: duplicate maximum version\n");
691 goto client_exit_error
;
693 vmax
= parse_version(arg
, strlen(arg
));
696 "ERROR: unrecognised version '%s'\n",
699 goto client_exit_error
;
701 } else if (eqstr(arg
, "-cs")) {
704 "ERROR: no argument for '-cs'\n");
706 goto client_exit_error
;
709 if (suites
!= NULL
) {
710 fprintf(stderr
, "ERROR: duplicate list"
711 " of cipher suites\n");
713 goto client_exit_error
;
715 suites
= parse_suites(arg
, &num_suites
);
716 if (suites
== NULL
) {
718 goto client_exit_error
;
720 } else if (eqstr(arg
, "-hf")) {
725 "ERROR: no argument for '-hf'\n");
727 goto client_exit_error
;
730 x
= parse_hash_functions(arg
);
733 goto client_exit_error
;
736 } else if (eqstr(arg
, "-minhello")) {
739 "ERROR: no argument for '-minhello'\n");
741 goto client_exit_error
;
744 if (minhello_len
!= (size_t)-1) {
745 fprintf(stderr
, "ERROR: duplicate minium"
746 " ClientHello length\n");
748 goto client_exit_error
;
750 minhello_len
= parse_size(arg
);
752 * Minimum ClientHello length must fit on 16 bits.
754 if (minhello_len
== (size_t)-1
755 || (((minhello_len
>> 12) >> 4) != 0))
758 goto client_exit_error
;
760 } else if (eqstr(arg
, "-fallback")) {
762 } else if (eqstr(arg
, "-noreneg")) {
763 flags
|= BR_OPT_NO_RENEGOTIATION
;
764 } else if (eqstr(arg
, "-alpn")) {
767 "ERROR: no argument for '-alpn'\n");
769 goto client_exit_error
;
771 VEC_ADD(alpn_names
, xstrdup(argv
[i
]));
772 } else if (eqstr(arg
, "-strictalpn")) {
773 flags
|= BR_OPT_FAIL_ON_ALPN_MISMATCH
;
775 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
777 goto client_exit_error
;
780 if (server_name
== NULL
) {
781 fprintf(stderr
, "ERROR: no server name/address provided\n");
783 goto client_exit_error
;
785 for (u
= strlen(server_name
); u
> 0; u
--) {
786 int c
= server_name
[u
- 1];
790 if (c
< '0' || c
> '9') {
796 host
= xstrdup(server_name
);
797 port
= xstrdup("443");
799 port
= xstrdup(server_name
+ u
);
801 memcpy(host
, server_name
, u
- 1);
808 if (chain
== NULL
&& sk
!= NULL
) {
809 fprintf(stderr
, "ERROR: private key specified, but"
810 " no certificate chain\n");
812 goto client_exit_error
;
814 if (chain
!= NULL
&& sk
== NULL
) {
815 fprintf(stderr
, "ERROR: certificate chain specified, but"
816 " no private key\n");
818 goto client_exit_error
;
828 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
829 " version combination\n");
831 goto client_exit_error
;
833 if (suites
== NULL
) {
836 for (u
= 0; cipher_suites
[u
].name
; u
++) {
837 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
843 suites
= xmalloc(num_suites
* sizeof *suites
);
845 for (u
= 0; cipher_suites
[u
].name
; u
++) {
846 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
849 suites
[num_suites
++] = cipher_suites
[u
];
854 hfuns
= (unsigned)-1;
856 if (iobuf_len
== 0) {
858 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
860 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
863 iobuf
= xmalloc(iobuf_len
);
866 * Compute implementation requirements and inject implementations.
868 suite_ids
= xmalloc((num_suites
+ 1) * sizeof *suite_ids
);
869 br_ssl_client_zero(&cc
);
870 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
872 for (u
= 0; hash_functions
[u
].name
; u
++) {
873 const br_hash_class
*hc
;
876 hc
= hash_functions
[u
].hclass
;
877 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
878 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
882 if (dnhash
== NULL
) {
883 fprintf(stderr
, "ERROR: no supported hash function\n");
884 goto client_exit_error
;
886 br_x509_minimal_init(&xc
, dnhash
,
887 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
888 if (vmin
<= BR_TLS11
) {
889 if (!(hfuns
& (1 << br_md5_ID
))) {
890 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
891 goto client_exit_error
;
893 if (!(hfuns
& (1 << br_sha1_ID
))) {
894 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
895 goto client_exit_error
;
898 for (u
= 0; u
< num_suites
; u
++) {
902 suite_ids
[u
] = suites
[u
].suite
;
903 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
905 "ERROR: cipher suite %s requires TLS 1.2\n",
907 goto client_exit_error
;
909 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
911 "ERROR: cipher suite %s requires SHA-1\n",
913 goto client_exit_error
;
915 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
917 "ERROR: cipher suite %s requires SHA-256\n",
919 goto client_exit_error
;
921 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
923 "ERROR: cipher suite %s requires SHA-384\n",
925 goto client_exit_error
;
927 /* TODO: algorithm implementation selection */
928 if ((req
& REQ_AESCBC
) != 0) {
929 br_ssl_engine_set_aes_cbc(&cc
.eng
,
930 &br_aes_ct_cbcenc_vtable
,
931 &br_aes_ct_cbcdec_vtable
);
932 br_ssl_engine_set_cbc(&cc
.eng
,
933 &br_sslrec_in_cbc_vtable
,
934 &br_sslrec_out_cbc_vtable
);
936 if ((req
& REQ_AESGCM
) != 0) {
937 br_ssl_engine_set_aes_ctr(&cc
.eng
,
938 &br_aes_ct_ctr_vtable
);
939 br_ssl_engine_set_ghash(&cc
.eng
,
941 br_ssl_engine_set_gcm(&cc
.eng
,
942 &br_sslrec_in_gcm_vtable
,
943 &br_sslrec_out_gcm_vtable
);
945 if ((req
& REQ_CHAPOL
) != 0) {
946 br_ssl_engine_set_chacha20(&cc
.eng
,
947 &br_chacha20_ct_run
);
948 br_ssl_engine_set_poly1305(&cc
.eng
,
949 &br_poly1305_ctmul_run
);
950 br_ssl_engine_set_chapol(&cc
.eng
,
951 &br_sslrec_in_chapol_vtable
,
952 &br_sslrec_out_chapol_vtable
);
954 if ((req
& REQ_3DESCBC
) != 0) {
955 br_ssl_engine_set_des_cbc(&cc
.eng
,
956 &br_des_ct_cbcenc_vtable
,
957 &br_des_ct_cbcdec_vtable
);
958 br_ssl_engine_set_cbc(&cc
.eng
,
959 &br_sslrec_in_cbc_vtable
,
960 &br_sslrec_out_cbc_vtable
);
962 if ((req
& REQ_RSAKEYX
) != 0) {
963 br_ssl_client_set_rsapub(&cc
, &br_rsa_i31_public
);
965 if ((req
& REQ_ECDHE_RSA
) != 0) {
966 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
967 br_ssl_engine_set_rsavrfy(&cc
.eng
,
968 &br_rsa_i31_pkcs1_vrfy
);
970 if ((req
& REQ_ECDHE_ECDSA
) != 0) {
971 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
972 br_ssl_engine_set_ecdsa(&cc
.eng
,
973 &br_ecdsa_i31_vrfy_asn1
);
975 if ((req
& REQ_ECDH
) != 0) {
976 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
980 suite_ids
[num_suites
++] = 0x5600;
982 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
984 for (u
= 0; hash_functions
[u
].name
; u
++) {
985 const br_hash_class
*hc
;
988 hc
= hash_functions
[u
].hclass
;
989 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
990 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
991 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
992 br_x509_minimal_set_hash(&xc
, id
, hc
);
995 if (vmin
<= BR_TLS11
) {
996 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
998 if (vmax
>= BR_TLS12
) {
999 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
1000 br_ssl_engine_set_prf_sha256(&cc
.eng
,
1001 &br_tls12_sha256_prf
);
1003 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
1004 br_ssl_engine_set_prf_sha384(&cc
.eng
,
1005 &br_tls12_sha384_prf
);
1008 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
1009 br_x509_minimal_set_ecdsa(&xc
,
1010 &br_ec_all_m15
, &br_ecdsa_i31_vrfy_asn1
);
1013 * If there is no provided trust anchor, then certificate validation
1014 * will always fail. In that situation, we use our custom wrapper
1015 * that tolerates unknown anchors.
1017 if (VEC_LEN(anchors
) == 0) {
1020 "WARNING: no configured trust anchor\n");
1022 x509_noanchor_init(&xwc
, &xc
.vtable
);
1023 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
1025 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
1028 if (minhello_len
!= (size_t)-1) {
1029 br_ssl_client_set_min_clienthello_len(&cc
, minhello_len
);
1031 br_ssl_engine_set_all_flags(&cc
.eng
, flags
);
1032 if (VEC_LEN(alpn_names
) != 0) {
1033 br_ssl_engine_set_protocol_names(&cc
.eng
,
1034 &VEC_ELT(alpn_names
, 0), 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 * We need to avoid SIGPIPE.
1060 signal(SIGPIPE
, SIG_IGN
);
1063 * Connect to the peer.
1065 fd
= host_connect(host
, port
, verbose
);
1067 goto client_exit_error
;
1071 * Run the engine until completion.
1073 if (run_ssl_engine(&cc
.eng
, fd
,
1074 (verbose
? RUN_ENGINE_VERBOSE
: 0)
1075 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
1077 goto client_exit_error
;
1083 * Release allocated structures.
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
);