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
*)sa
)->sin_addr
;
76 } else if (sa
->sa_family
== AF_INET6
) {
77 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
82 if (!inet_ntop(p
->ai_family
, addr
,
85 strcpy(tmp
, "<invalid>");
88 sprintf(tmp
, "<unknown family: %d>",
91 fprintf(stderr
, "connecting to: %s\n", tmp
);
93 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
94 if (fd
== INVALID_SOCKET
) {
100 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) == INVALID_SOCKET
) {
115 fprintf(stderr
, "ERROR: failed to connect\n");
116 return INVALID_SOCKET
;
120 fprintf(stderr
, "connected.\n");
124 * We make the socket non-blocking, since we are going to use
125 * poll() or select() to organise I/O.
132 ioctlsocket(fd
, FIONBIO
, &arg
);
135 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
141 const br_ssl_client_certificate_class
*vtable
;
143 br_x509_certificate
*chain
;
150 cc_start_name_list(const br_ssl_client_certificate_class
**pctx
)
154 zc
= (ccert_context
*)pctx
;
156 fprintf(stderr
, "Server requests a client certificate.\n");
157 fprintf(stderr
, "--- anchor DN list start ---\n");
162 cc_start_name(const br_ssl_client_certificate_class
**pctx
, size_t len
)
166 zc
= (ccert_context
*)pctx
;
168 fprintf(stderr
, "new anchor name, length = %u\n",
174 cc_append_name(const br_ssl_client_certificate_class
**pctx
,
175 const unsigned char *data
, size_t len
)
179 zc
= (ccert_context
*)pctx
;
183 for (u
= 0; u
< len
; u
++) {
185 fprintf(stderr
, " ");
186 } else if (u
> 0 && u
% 16 == 0) {
187 fprintf(stderr
, "\n ");
189 fprintf(stderr
, " %02x", data
[u
]);
192 fprintf(stderr
, "\n");
198 cc_end_name(const br_ssl_client_certificate_class
**pctx
)
204 cc_end_name_list(const br_ssl_client_certificate_class
**pctx
)
208 zc
= (ccert_context
*)pctx
;
210 fprintf(stderr
, "--- anchor DN list end ---\n");
215 print_hashes(unsigned hh
, unsigned hh2
)
219 for (i
= 0; i
< 8; i
++) {
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
);
232 choose_hash(unsigned hh
)
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
241 for (u
= 0; f
[u
] >= 0; u
++) {
242 if (((hh
>> f
[u
]) & 1) != 0) {
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
)
257 zc
= (ccert_context
*)pctx
;
258 scurve
= br_ssl_client_get_server_curve(cc
);
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");
268 if ((auth_types
& 0xFF00) != 0) {
269 fprintf(stderr
, "supported: ECDSA signatures:");
270 print_hashes(auth_types
>> 8, hashes
>> 8);
271 fprintf(stderr
, "\n");
273 if ((auth_types
& 0x010000) != 0) {
274 fprintf(stderr
, "supported:"
275 " fixed ECDH (cert signed with RSA)\n");
277 if ((auth_types
& 0x020000) != 0) {
278 fprintf(stderr
, "supported:"
279 " fixed ECDH (cert signed with ECDSA)\n");
282 fprintf(stderr
, "server key curve: %s (%d)\n",
283 ec_curve_name(scurve
), scurve
);
285 fprintf(stderr
, "server key is not EC\n");
288 switch (zc
->sk
->key_type
) {
290 if ((choices
->hash_id
= choose_hash(auth_types
)) >= 0) {
292 fprintf(stderr
, "using RSA, hash = %d (%s)\n",
294 hash_function_name(choices
->hash_id
));
296 choices
->auth_type
= BR_AUTH_RSA
;
297 choices
->chain
= zc
->chain
;
298 choices
->chain_len
= zc
->chain_len
;
303 if (zc
->issuer_key_type
!= 0
304 && scurve
== zc
->sk
->key
.ec
.curve
)
308 x
= (zc
->issuer_key_type
== BR_KEYTYPE_RSA
) ? 16 : 17;
309 if (((auth_types
>> x
) & 1) != 0) {
311 fprintf(stderr
, "using static ECDH\n");
313 choices
->auth_type
= BR_AUTH_ECDH
;
314 choices
->hash_id
= -1;
315 choices
->chain
= zc
->chain
;
316 choices
->chain_len
= zc
->chain_len
;
320 if ((choices
->hash_id
= choose_hash(auth_types
>> 8)) >= 0) {
322 fprintf(stderr
, "using ECDSA, hash = %d (%s)\n",
324 hash_function_name(choices
->hash_id
));
326 choices
->auth_type
= BR_AUTH_ECDSA
;
327 choices
->chain
= zc
->chain
;
328 choices
->chain_len
= zc
->chain_len
;
334 fprintf(stderr
, "no matching client certificate\n");
336 choices
->chain
= NULL
;
337 choices
->chain_len
= 0;
341 cc_do_keyx(const br_ssl_client_certificate_class
**pctx
,
342 unsigned char *data
, size_t *len
)
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
);
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
)
362 unsigned char hv
[64];
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
;
373 hash_oid
= get_hash_oid(hash_id
);
374 if (hash_oid
== NULL
&& hash_id
!= 0) {
376 fprintf(stderr
, "ERROR: cannot RSA-sign with"
377 " unknown hash function: %d\n",
382 sig_len
= (zc
->sk
->key
.rsa
.n_bitlen
+ 7) >> 3;
385 fprintf(stderr
, "ERROR: cannot RSA-sign,"
386 " buffer is too small"
387 " (sig=%lu, buf=%lu)\n",
388 (unsigned long)sig_len
,
393 x
= br_rsa_i31_pkcs1_sign(hash_oid
, hv
, hv_len
,
394 &zc
->sk
->key
.rsa
, data
);
397 fprintf(stderr
, "ERROR: RSA-sign failure\n");
404 hc
= get_hash_impl(hash_id
);
407 fprintf(stderr
, "ERROR: cannot ECDSA-sign with"
408 " unknown hash function: %d\n",
415 fprintf(stderr
, "ERROR: cannot ECDSA-sign"
416 " (output buffer = %lu)\n",
421 sig_len
= br_ecdsa_i31_sign_asn1(&br_ec_all_m15
,
422 hc
, hv
, &zc
->sk
->key
.ec
, data
);
425 fprintf(stderr
, "ERROR: ECDSA-sign failure\n");
436 static const br_ssl_client_certificate_class ccert_vtable
= {
437 sizeof(ccert_context
),
449 free_alpn(void *alpn
)
451 xfree(*(char **)alpn
);
458 "usage: brssl client server[:port] [ options ]\n");
462 " -q suppress verbose messages\n");
464 " -trace activate extra debug messages (dump of all packets)\n");
466 " -sni name use this specific name for SNI\n");
468 " -nosni do not send any SNI\n");
470 " -mono use monodirectional buffering\n");
472 " -buf length set the I/O buffer length (in bytes)\n");
474 " -CA file add certificates in 'file' to trust anchors\n");
476 " -cert file set client certificate chain\n");
478 " -key file set client private key (for certificate authentication)\n");
480 " -nostaticecdh prohibit full-static ECDH (client certificate)\n");
482 " -list list supported names (protocols, algorithms...)\n");
484 " -vmin name set minimum supported version (default: TLS-1.0)\n");
486 " -vmax name set maximum supported version (default: TLS-1.2)\n");
488 " -cs names set list of supported cipher suites (comma-separated)\n");
490 " -hf names add support for some hash functions (comma-separated)\n");
492 " -minhello len set minimum ClientHello length (in bytes)\n");
494 " -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
496 " -noreneg prohibit renegotiations\n");
498 " -alpn name add protocol name to list of protocols (ALPN extension)\n");
500 " -strictalpn fail on ALPN mismatch\n");
505 do_client(int argc
, char *argv
[])
511 const char *server_name
;
515 anchor_list anchors
= VEC_INIT
;
517 VECTOR(char *) alpn_names
= VEC_INIT
;
518 cipher_suite
*suites
;
523 br_ssl_client_context cc
;
524 br_x509_minimal_context xc
;
525 x509_noanchor_context xwc
;
526 const br_hash_class
*dnhash
;
528 br_x509_certificate
*chain
;
532 unsigned char *iobuf
;
559 minhello_len
= (size_t)-1;
563 for (i
= 0; i
< argc
; i
++) {
568 if (server_name
!= NULL
) {
570 "ERROR: duplicate server name\n");
572 goto client_exit_error
;
577 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
579 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
581 } else if (eqstr(arg
, "-trace")) {
583 } else if (eqstr(arg
, "-sni")) {
586 "ERROR: no argument for '-sni'\n");
588 goto client_exit_error
;
591 fprintf(stderr
, "ERROR: duplicate SNI\n");
593 goto client_exit_error
;
596 } else if (eqstr(arg
, "-nosni")) {
598 fprintf(stderr
, "ERROR: duplicate SNI\n");
600 goto client_exit_error
;
603 } else if (eqstr(arg
, "-mono")) {
605 } else if (eqstr(arg
, "-buf")) {
608 "ERROR: no argument for '-buf'\n");
610 goto client_exit_error
;
613 if (iobuf_len
!= 0) {
615 "ERROR: duplicate I/O buffer length\n");
617 goto client_exit_error
;
619 iobuf_len
= parse_size(arg
);
620 if (iobuf_len
== (size_t)-1) {
622 goto client_exit_error
;
624 } else if (eqstr(arg
, "-CA")) {
627 "ERROR: no argument for '-CA'\n");
629 goto client_exit_error
;
632 if (read_trust_anchors(&anchors
, arg
) == 0) {
634 goto client_exit_error
;
636 } else if (eqstr(arg
, "-cert")) {
639 "ERROR: no argument for '-cert'\n");
641 goto client_exit_error
;
645 "ERROR: duplicate certificate chain\n");
647 goto client_exit_error
;
650 chain
= read_certificates(arg
, &chain_len
);
651 if (chain
== NULL
|| chain_len
== 0) {
652 goto client_exit_error
;
654 } else if (eqstr(arg
, "-key")) {
657 "ERROR: no argument for '-key'\n");
659 goto client_exit_error
;
663 "ERROR: duplicate private key\n");
665 goto client_exit_error
;
668 sk
= read_private_key(arg
);
670 goto client_exit_error
;
672 } else if (eqstr(arg
, "-nostaticecdh")) {
674 } else if (eqstr(arg
, "-list")) {
677 } else if (eqstr(arg
, "-vmin")) {
680 "ERROR: no argument for '-vmin'\n");
682 goto client_exit_error
;
687 "ERROR: duplicate minimum version\n");
689 goto client_exit_error
;
691 vmin
= parse_version(arg
, strlen(arg
));
694 "ERROR: unrecognised version '%s'\n",
697 goto client_exit_error
;
699 } else if (eqstr(arg
, "-vmax")) {
702 "ERROR: no argument for '-vmax'\n");
704 goto client_exit_error
;
709 "ERROR: duplicate maximum version\n");
711 goto client_exit_error
;
713 vmax
= parse_version(arg
, strlen(arg
));
716 "ERROR: unrecognised version '%s'\n",
719 goto client_exit_error
;
721 } else if (eqstr(arg
, "-cs")) {
724 "ERROR: no argument for '-cs'\n");
726 goto client_exit_error
;
729 if (suites
!= NULL
) {
730 fprintf(stderr
, "ERROR: duplicate list"
731 " of cipher suites\n");
733 goto client_exit_error
;
735 suites
= parse_suites(arg
, &num_suites
);
736 if (suites
== NULL
) {
738 goto client_exit_error
;
740 } else if (eqstr(arg
, "-hf")) {
745 "ERROR: no argument for '-hf'\n");
747 goto client_exit_error
;
750 x
= parse_hash_functions(arg
);
753 goto client_exit_error
;
756 } else if (eqstr(arg
, "-minhello")) {
759 "ERROR: no argument for '-minhello'\n");
761 goto client_exit_error
;
764 if (minhello_len
!= (size_t)-1) {
765 fprintf(stderr
, "ERROR: duplicate minium"
766 " ClientHello length\n");
768 goto client_exit_error
;
770 minhello_len
= parse_size(arg
);
772 * Minimum ClientHello length must fit on 16 bits.
774 if (minhello_len
== (size_t)-1
775 || (((minhello_len
>> 12) >> 4) != 0))
778 goto client_exit_error
;
780 } else if (eqstr(arg
, "-fallback")) {
782 } else if (eqstr(arg
, "-noreneg")) {
783 flags
|= BR_OPT_NO_RENEGOTIATION
;
784 } else if (eqstr(arg
, "-alpn")) {
787 "ERROR: no argument for '-alpn'\n");
789 goto client_exit_error
;
791 VEC_ADD(alpn_names
, xstrdup(argv
[i
]));
792 } else if (eqstr(arg
, "-strictalpn")) {
793 flags
|= BR_OPT_FAIL_ON_ALPN_MISMATCH
;
795 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
797 goto client_exit_error
;
800 if (server_name
== NULL
) {
801 fprintf(stderr
, "ERROR: no server name/address provided\n");
803 goto client_exit_error
;
805 for (u
= strlen(server_name
); u
> 0; u
--) {
806 int c
= server_name
[u
- 1];
810 if (c
< '0' || c
> '9') {
816 host
= xstrdup(server_name
);
817 port
= xstrdup("443");
819 port
= xstrdup(server_name
+ u
);
821 memcpy(host
, server_name
, u
- 1);
828 if (chain
== NULL
&& sk
!= NULL
) {
829 fprintf(stderr
, "ERROR: private key specified, but"
830 " no certificate chain\n");
832 goto client_exit_error
;
834 if (chain
!= NULL
&& sk
== NULL
) {
835 fprintf(stderr
, "ERROR: certificate chain specified, but"
836 " no private key\n");
838 goto client_exit_error
;
848 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
849 " version combination\n");
851 goto client_exit_error
;
853 if (suites
== NULL
) {
856 for (u
= 0; cipher_suites
[u
].name
; u
++) {
857 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
863 suites
= xmalloc(num_suites
* sizeof *suites
);
865 for (u
= 0; cipher_suites
[u
].name
; u
++) {
866 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
869 suites
[num_suites
++] = cipher_suites
[u
];
874 hfuns
= (unsigned)-1;
876 if (iobuf_len
== 0) {
878 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
880 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
883 iobuf
= xmalloc(iobuf_len
);
886 * Compute implementation requirements and inject implementations.
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
);
892 for (u
= 0; hash_functions
[u
].name
; u
++) {
893 const br_hash_class
*hc
;
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) {
902 if (dnhash
== NULL
) {
903 fprintf(stderr
, "ERROR: no supported hash function\n");
904 goto client_exit_error
;
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
;
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
;
918 for (u
= 0; u
< num_suites
; u
++) {
922 suite_ids
[u
] = suites
[u
].suite
;
923 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
925 "ERROR: cipher suite %s requires TLS 1.2\n",
927 goto client_exit_error
;
929 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
931 "ERROR: cipher suite %s requires SHA-1\n",
933 goto client_exit_error
;
935 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
937 "ERROR: cipher suite %s requires SHA-256\n",
939 goto client_exit_error
;
941 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
943 "ERROR: cipher suite %s requires SHA-384\n",
945 goto client_exit_error
;
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
);
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
,
961 br_ssl_engine_set_gcm(&cc
.eng
,
962 &br_sslrec_in_gcm_vtable
,
963 &br_sslrec_out_gcm_vtable
);
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
);
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
);
982 if ((req
& REQ_RSAKEYX
) != 0) {
983 br_ssl_client_set_rsapub(&cc
, &br_rsa_i31_public
);
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
);
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
);
995 if ((req
& REQ_ECDH
) != 0) {
996 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
1000 suite_ids
[num_suites
++] = 0x5600;
1002 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
1004 for (u
= 0; hash_functions
[u
].name
; u
++) {
1005 const br_hash_class
*hc
;
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
);
1015 if (vmin
<= BR_TLS11
) {
1016 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
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
);
1023 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
1024 br_ssl_engine_set_prf_sha384(&cc
.eng
,
1025 &br_tls12_sha384_prf
);
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
);
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.
1037 if (VEC_LEN(anchors
) == 0) {
1040 "WARNING: no configured trust anchor\n");
1042 x509_noanchor_init(&xwc
, &xc
.vtable
);
1043 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
1045 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
1048 if (minhello_len
!= (size_t)-1) {
1049 br_ssl_client_set_min_clienthello_len(&cc
, minhello_len
);
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
));
1058 if (chain
!= NULL
) {
1059 zc
.vtable
= &ccert_vtable
;
1060 zc
.verbose
= verbose
;
1062 zc
.chain_len
= chain_len
;
1064 if (nostaticecdh
|| sk
->key_type
!= BR_KEYTYPE_EC
) {
1065 zc
.issuer_key_type
= 0;
1067 zc
.issuer_key_type
= get_cert_signer_algo(&chain
[0]);
1068 if (zc
.issuer_key_type
== 0) {
1069 goto client_exit_error
;
1072 br_ssl_client_set_client_certificate(&cc
, &zc
.vtable
);
1075 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
1076 br_ssl_client_reset(&cc
, sni
, 0);
1079 * On Unix systems, we need to avoid SIGPIPE.
1082 signal(SIGPIPE
, SIG_IGN
);
1086 * Connect to the peer.
1088 fd
= host_connect(host
, port
, verbose
);
1089 if (fd
== INVALID_SOCKET
) {
1090 goto client_exit_error
;
1094 * Run the engine until completion.
1096 if (run_ssl_engine(&cc
.eng
, fd
,
1097 (verbose
? RUN_ENGINE_VERBOSE
: 0)
1098 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
1100 goto client_exit_error
;
1106 * Release allocated structures.
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
);
1118 if (fd
!= INVALID_SOCKET
) {