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
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
43 host_connect(const char *host
, const char *port
, int verbose
)
45 struct addrinfo hints
, *si
, *p
;
49 memset(&hints
, 0, sizeof hints
);
50 hints
.ai_family
= PF_UNSPEC
;
51 hints
.ai_socktype
= SOCK_STREAM
;
52 err
= getaddrinfo(host
, port
, &hints
, &si
);
54 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
59 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
63 char tmp
[INET6_ADDRSTRLEN
+ 50];
65 sa
= (struct sockaddr
*)p
->ai_addr
;
66 if (sa
->sa_family
== AF_INET
) {
67 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
68 } else if (sa
->sa_family
== AF_INET6
) {
69 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
74 if (!inet_ntop(p
->ai_family
, addr
,
77 strcpy(tmp
, "<invalid>");
80 sprintf(tmp
, "<unknown family: %d>",
83 fprintf(stderr
, "connecting to: %s\n", tmp
);
85 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
92 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
103 fprintf(stderr
, "ERROR: failed to connect\n");
108 fprintf(stderr
, "connected.\n");
112 * We make the socket non-blocking, since we are going to use
113 * poll() to organise I/O.
115 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
123 "usage: brssl client server[:port] [ options ]\n");
127 " -q suppress verbose messages\n");
129 " -trace activate extra debug messages (dump of all packets)\n");
131 " -sni name use this specific name for SNI\n");
133 " -nosni do not send any SNI\n");
135 " -mono use monodirectional buffering\n");
137 " -buf length set the I/O buffer length (in bytes)\n");
139 " -CA file add certificates in 'file' to trust anchors\n");
141 " -list list supported names (protocols, algorithms...)\n");
143 " -vmin name set minimum supported version (default: TLS-1.0)\n");
145 " -vmax name set maximum supported version (default: TLS-1.2)\n");
147 " -cs names set list of supported cipher suites (comma-separated)\n");
149 " -hf names add support for some hash functions (comma-separated)\n");
151 " -minhello len set minimum ClientHello length (in bytes)\n");
153 " -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
158 do_client(int argc
, char *argv
[])
164 const char *server_name
;
168 anchor_list anchors
= VEC_INIT
;
170 cipher_suite
*suites
;
175 br_ssl_client_context cc
;
176 br_x509_minimal_context xc
;
177 x509_noanchor_context xwc
;
178 const br_hash_class
*dnhash
;
179 unsigned char *iobuf
;
201 minhello_len
= (size_t)-1;
204 for (i
= 0; i
< argc
; i
++) {
209 if (server_name
!= NULL
) {
211 "ERROR: duplicate server name\n");
213 goto client_exit_error
;
218 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
220 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
222 } else if (eqstr(arg
, "-trace")) {
224 } else if (eqstr(arg
, "-sni")) {
227 "ERROR: no argument for '-sni'\n");
229 goto client_exit_error
;
232 fprintf(stderr
, "ERROR: duplicate SNI\n");
234 goto client_exit_error
;
237 } else if (eqstr(arg
, "-nosni")) {
239 fprintf(stderr
, "ERROR: duplicate SNI\n");
241 goto client_exit_error
;
244 } else if (eqstr(arg
, "-mono")) {
246 } else if (eqstr(arg
, "-buf")) {
249 "ERROR: no argument for '-buf'\n");
251 goto client_exit_error
;
254 if (iobuf_len
!= 0) {
256 "ERROR: duplicate I/O buffer length\n");
258 goto client_exit_error
;
260 iobuf_len
= parse_size(arg
);
261 if (iobuf_len
== (size_t)-1) {
263 goto client_exit_error
;
265 } else if (eqstr(arg
, "-CA")) {
268 "ERROR: no argument for '-CA'\n");
270 goto client_exit_error
;
273 if (read_trust_anchors(&anchors
, arg
) == 0) {
275 goto client_exit_error
;
277 } else if (eqstr(arg
, "-list")) {
280 } else if (eqstr(arg
, "-vmin")) {
283 "ERROR: no argument for '-vmin'\n");
285 goto client_exit_error
;
290 "ERROR: duplicate minimum version\n");
292 goto client_exit_error
;
294 vmin
= parse_version(arg
, strlen(arg
));
297 "ERROR: unrecognised version '%s'\n",
300 goto client_exit_error
;
302 } else if (eqstr(arg
, "-vmax")) {
305 "ERROR: no argument for '-vmax'\n");
307 goto client_exit_error
;
312 "ERROR: duplicate maximum version\n");
314 goto client_exit_error
;
316 vmax
= parse_version(arg
, strlen(arg
));
319 "ERROR: unrecognised version '%s'\n",
322 goto client_exit_error
;
324 } else if (eqstr(arg
, "-cs")) {
327 "ERROR: no argument for '-cs'\n");
329 goto client_exit_error
;
332 if (suites
!= NULL
) {
333 fprintf(stderr
, "ERROR: duplicate list"
334 " of cipher suites\n");
336 goto client_exit_error
;
338 suites
= parse_suites(arg
, &num_suites
);
339 if (suites
== NULL
) {
341 goto client_exit_error
;
343 } else if (eqstr(arg
, "-hf")) {
348 "ERROR: no argument for '-hf'\n");
350 goto client_exit_error
;
353 x
= parse_hash_functions(arg
);
356 goto client_exit_error
;
359 } else if (eqstr(arg
, "-minhello")) {
362 "ERROR: no argument for '-minhello'\n");
364 goto client_exit_error
;
367 if (minhello_len
!= (size_t)-1) {
368 fprintf(stderr
, "ERROR: duplicate minium"
369 " ClientHello length\n");
371 goto client_exit_error
;
373 minhello_len
= parse_size(arg
);
375 * Minimum ClientHello length must fit on 16 bits.
377 if (minhello_len
== (size_t)-1
378 || (((minhello_len
>> 12) >> 4) != 0))
381 goto client_exit_error
;
383 } else if (eqstr(arg
, "-fallback")) {
386 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
388 goto client_exit_error
;
391 if (server_name
== NULL
) {
392 fprintf(stderr
, "ERROR: no server name/address provided\n");
394 goto client_exit_error
;
396 for (u
= strlen(server_name
); u
> 0; u
--) {
397 int c
= server_name
[u
- 1];
401 if (c
< '0' || c
> '9') {
407 host
= xstrdup(server_name
);
410 port
= xstrdup(server_name
+ u
);
412 memcpy(host
, server_name
, u
- 1);
426 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
427 " version combination\n");
429 goto client_exit_error
;
431 if (suites
== NULL
) {
434 for (u
= 0; cipher_suites
[u
].name
; u
++) {
435 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
441 suites
= xmalloc(num_suites
* sizeof *suites
);
443 for (u
= 0; cipher_suites
[u
].name
; u
++) {
444 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
447 suites
[num_suites
++] = cipher_suites
[u
];
452 hfuns
= (unsigned)-1;
454 if (iobuf_len
== 0) {
456 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
458 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
461 iobuf
= xmalloc(iobuf_len
);
464 * Compute implementation requirements and inject implementations.
466 suite_ids
= xmalloc((num_suites
+ 1) * sizeof *suite_ids
);
467 br_ssl_client_zero(&cc
);
468 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
470 for (u
= 0; hash_functions
[u
].name
; u
++) {
471 const br_hash_class
*hc
;
474 hc
= hash_functions
[u
].hclass
;
475 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
476 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
480 if (dnhash
== NULL
) {
481 fprintf(stderr
, "ERROR: no supported hash function\n");
482 goto client_exit_error
;
484 br_x509_minimal_init(&xc
, dnhash
,
485 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
486 if (vmin
<= BR_TLS11
) {
487 if (!(hfuns
& (1 << br_md5_ID
))) {
488 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
489 goto client_exit_error
;
491 if (!(hfuns
& (1 << br_sha1_ID
))) {
492 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
493 goto client_exit_error
;
496 for (u
= 0; u
< num_suites
; u
++) {
500 suite_ids
[u
] = suites
[u
].suite
;
501 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
503 "ERROR: cipher suite %s requires TLS 1.2\n",
505 goto client_exit_error
;
507 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
509 "ERROR: cipher suite %s requires SHA-1\n",
511 goto client_exit_error
;
513 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
515 "ERROR: cipher suite %s requires SHA-256\n",
517 goto client_exit_error
;
519 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
521 "ERROR: cipher suite %s requires SHA-384\n",
523 goto client_exit_error
;
525 /* TODO: algorithm implementation selection */
526 if ((req
& REQ_AESCBC
) != 0) {
527 br_ssl_engine_set_aes_cbc(&cc
.eng
,
528 &br_aes_ct_cbcenc_vtable
,
529 &br_aes_ct_cbcdec_vtable
);
530 br_ssl_engine_set_cbc(&cc
.eng
,
531 &br_sslrec_in_cbc_vtable
,
532 &br_sslrec_out_cbc_vtable
);
534 if ((req
& REQ_AESGCM
) != 0) {
535 br_ssl_engine_set_aes_ctr(&cc
.eng
,
536 &br_aes_ct_ctr_vtable
);
537 br_ssl_engine_set_ghash(&cc
.eng
,
539 br_ssl_engine_set_gcm(&cc
.eng
,
540 &br_sslrec_in_gcm_vtable
,
541 &br_sslrec_out_gcm_vtable
);
543 if ((req
& REQ_3DESCBC
) != 0) {
544 br_ssl_engine_set_des_cbc(&cc
.eng
,
545 &br_des_ct_cbcenc_vtable
,
546 &br_des_ct_cbcdec_vtable
);
547 br_ssl_engine_set_cbc(&cc
.eng
,
548 &br_sslrec_in_cbc_vtable
,
549 &br_sslrec_out_cbc_vtable
);
551 if ((req
& REQ_RSAKEYX
) != 0) {
552 br_ssl_client_set_rsapub(&cc
, &br_rsa_i31_public
);
554 if ((req
& REQ_ECDHE_RSA
) != 0) {
555 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
556 br_ssl_client_set_rsavrfy(&cc
, &br_rsa_i31_pkcs1_vrfy
);
558 if ((req
& REQ_ECDHE_ECDSA
) != 0) {
559 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
560 br_ssl_client_set_ecdsa(&cc
, &br_ecdsa_i31_vrfy_asn1
);
562 if ((req
& REQ_ECDH
) != 0) {
563 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
567 suite_ids
[num_suites
++] = 0x5600;
569 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
571 for (u
= 0; hash_functions
[u
].name
; u
++) {
572 const br_hash_class
*hc
;
575 hc
= hash_functions
[u
].hclass
;
576 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
577 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
578 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
579 br_x509_minimal_set_hash(&xc
, id
, hc
);
582 if (vmin
<= BR_TLS11
) {
583 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
585 if (vmax
>= BR_TLS12
) {
586 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
587 br_ssl_engine_set_prf_sha256(&cc
.eng
,
588 &br_tls12_sha256_prf
);
590 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
591 br_ssl_engine_set_prf_sha384(&cc
.eng
,
592 &br_tls12_sha384_prf
);
595 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
596 br_x509_minimal_set_ecdsa(&xc
,
597 &br_ec_prime_i31
, &br_ecdsa_i31_vrfy_asn1
);
600 * If there is no provided trust anchor, then certificate validation
601 * will always fail. In that situation, we use our custom wrapper
602 * that tolerates unknown anchors.
604 if (VEC_LEN(anchors
) == 0) {
607 "WARNING: no configured trust anchor\n");
609 x509_noanchor_init(&xwc
, &xc
.vtable
);
610 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
612 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
615 if (minhello_len
!= (size_t)-1) {
616 br_ssl_client_set_min_clienthello_len(&cc
, minhello_len
);
619 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
620 br_ssl_client_reset(&cc
, sni
, 0);
623 * Connect to the peer.
625 fd
= host_connect(host
, port
, verbose
);
627 goto client_exit_error
;
631 * Run the engine until completion.
633 if (run_ssl_engine(&cc
.eng
, fd
,
634 (verbose
? RUN_ENGINE_VERBOSE
: 0)
635 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
637 goto client_exit_error
;
643 * Release allocated structures.
649 VEC_CLEAREXT(anchors
, &free_ta_contents
);