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>
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
) {
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 inet_ntop(p
->ai_family
, addr
, tmp
, sizeof tmp
);
76 sprintf(tmp
, "<unknown family: %d>",
80 fprintf(stderr
, "connecting to: %s\n", tmp
);
82 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
89 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
100 fprintf(stderr
, "ERROR: failed to connect\n");
105 fprintf(stderr
, "connected.\n");
109 * We make the socket non-blocking, since we are going to use
110 * poll() to organise I/O.
112 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
120 "usage: brssl client server[:port] [ options ]\n");
124 " -q suppress verbose messages\n");
126 " -trace activate extra debug messages (dump of all packets)\n");
128 " -sni name use this specific name for SNI\n");
130 " -nosni do not send any SNI\n");
132 " -mono use monodirectional buffering\n");
134 " -buf length set the I/O buffer length (in bytes)\n");
136 " -CA file add certificates in 'file' to trust anchors\n");
138 " -list list supported names (protocols, algorithms...)\n");
140 " -vmin name set minimum supported version (default: TLS-1.0)\n");
142 " -vmax name set maximum supported version (default: TLS-1.2)\n");
144 " -cs names set list of supported cipher suites (comma-separated)\n");
146 " -hf names add support for some hash functions (comma-separated)\n");
151 do_client(int argc
, char *argv
[])
157 const char *server_name
;
161 anchor_list anchors
= VEC_INIT
;
163 cipher_suite
*suites
;
168 br_ssl_client_context cc
;
169 br_x509_minimal_context xc
;
170 x509_noanchor_context xwc
;
171 const br_hash_class
*dnhash
;
172 unsigned char *iobuf
;
193 for (i
= 0; i
< argc
; i
++) {
198 if (server_name
!= NULL
) {
200 "ERROR: duplicate server name\n");
202 goto client_exit_error
;
207 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
209 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
211 } else if (eqstr(arg
, "-trace")) {
213 } else if (eqstr(arg
, "-sni")) {
216 "ERROR: no argument for '-sni'\n");
218 goto client_exit_error
;
221 fprintf(stderr
, "ERROR: duplicate SNI\n");
223 goto client_exit_error
;
226 } else if (eqstr(arg
, "-nosni")) {
228 fprintf(stderr
, "ERROR: duplicate SNI\n");
230 goto client_exit_error
;
233 } else if (eqstr(arg
, "-mono")) {
235 } else if (eqstr(arg
, "-buf")) {
238 "ERROR: no argument for '-buf'\n");
240 goto client_exit_error
;
243 if (iobuf_len
!= 0) {
245 "ERROR: duplicate I/O buffer length\n");
247 goto client_exit_error
;
249 iobuf_len
= strtoul(arg
, 0, 10);
250 } else if (eqstr(arg
, "-CA")) {
253 "ERROR: no argument for '-CA'\n");
255 goto client_exit_error
;
258 if (read_trust_anchors(&anchors
, arg
) == 0) {
260 goto client_exit_error
;
262 } else if (eqstr(arg
, "-list")) {
265 } else if (eqstr(arg
, "-vmin")) {
268 "ERROR: no argument for '-vmin'\n");
270 goto client_exit_error
;
275 "ERROR: duplicate minimum version\n");
277 goto client_exit_error
;
279 vmin
= parse_version(arg
, strlen(arg
));
282 "ERROR: unrecognised version '%s'\n",
285 goto client_exit_error
;
287 } else if (eqstr(arg
, "-vmax")) {
290 "ERROR: no argument for '-vmax'\n");
292 goto client_exit_error
;
297 "ERROR: duplicate maximum version\n");
299 goto client_exit_error
;
301 vmax
= parse_version(arg
, strlen(arg
));
304 "ERROR: unrecognised version '%s'\n",
307 goto client_exit_error
;
309 } else if (eqstr(arg
, "-cs")) {
312 "ERROR: no argument for '-cs'\n");
314 goto client_exit_error
;
317 if (suites
!= NULL
) {
318 fprintf(stderr
, "ERROR: duplicate list"
319 " of cipher suites\n");
321 goto client_exit_error
;
323 suites
= parse_suites(arg
, &num_suites
);
324 if (suites
== NULL
) {
326 goto client_exit_error
;
328 } else if (eqstr(arg
, "-hf")) {
333 "ERROR: no argument for '-hf'\n");
335 goto client_exit_error
;
338 x
= parse_hash_functions(arg
);
341 goto client_exit_error
;
345 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
347 goto client_exit_error
;
350 if (server_name
== NULL
) {
351 fprintf(stderr
, "ERROR: no server name/address provided\n");
353 goto client_exit_error
;
355 for (u
= strlen(server_name
); u
> 0; u
--) {
356 int c
= server_name
[u
- 1];
360 if (c
< '0' || c
> '9') {
366 host
= xstrdup(server_name
);
369 port
= xstrdup(server_name
+ u
);
371 memcpy(host
, server_name
, u
- 1);
385 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
386 " version combination\n");
388 goto client_exit_error
;
390 if (suites
== NULL
) {
393 for (u
= 0; cipher_suites
[u
].name
; u
++) {
394 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
400 suites
= xmalloc(num_suites
* sizeof *suites
);
402 for (u
= 0; cipher_suites
[u
].name
; u
++) {
403 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
406 suites
[num_suites
++] = cipher_suites
[u
];
411 hfuns
= (unsigned)-1;
413 if (iobuf_len
== 0) {
415 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
417 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
420 iobuf
= xmalloc(iobuf_len
);
423 * Compute implementation requirements and inject implementations.
425 suite_ids
= xmalloc(num_suites
* sizeof *suite_ids
);
426 br_ssl_client_zero(&cc
);
427 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
429 for (u
= 0; hash_functions
[u
].name
; u
++) {
430 const br_hash_class
*hc
;
433 hc
= hash_functions
[u
].hclass
;
434 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
435 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
439 if (dnhash
== NULL
) {
440 fprintf(stderr
, "ERROR: no supported hash function\n");
441 goto client_exit_error
;
443 br_x509_minimal_init(&xc
, dnhash
,
444 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
445 if (vmin
<= BR_TLS11
) {
446 if (!(hfuns
& (1 << br_md5_ID
))) {
447 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
448 goto client_exit_error
;
450 if (!(hfuns
& (1 << br_sha1_ID
))) {
451 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
452 goto client_exit_error
;
455 for (u
= 0; u
< num_suites
; u
++) {
459 suite_ids
[u
] = suites
[u
].suite
;
460 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
462 "ERROR: cipher suite %s requires TLS 1.2\n",
464 goto client_exit_error
;
466 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
468 "ERROR: cipher suite %s requires SHA-1\n",
470 goto client_exit_error
;
472 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
474 "ERROR: cipher suite %s requires SHA-256\n",
476 goto client_exit_error
;
478 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
480 "ERROR: cipher suite %s requires SHA-384\n",
482 goto client_exit_error
;
484 /* TODO: algorithm implementation selection */
485 if ((req
& REQ_AESCBC
) != 0) {
486 br_ssl_engine_set_aes_cbc(&cc
.eng
,
487 &br_aes_ct_cbcenc_vtable
,
488 &br_aes_ct_cbcdec_vtable
);
489 br_ssl_engine_set_cbc(&cc
.eng
,
490 &br_sslrec_in_cbc_vtable
,
491 &br_sslrec_out_cbc_vtable
);
493 if ((req
& REQ_AESGCM
) != 0) {
494 br_ssl_engine_set_aes_ctr(&cc
.eng
,
495 &br_aes_ct_ctr_vtable
);
496 br_ssl_engine_set_ghash(&cc
.eng
,
498 br_ssl_engine_set_gcm(&cc
.eng
,
499 &br_sslrec_in_gcm_vtable
,
500 &br_sslrec_out_gcm_vtable
);
502 if ((req
& REQ_3DESCBC
) != 0) {
503 br_ssl_engine_set_des_cbc(&cc
.eng
,
504 &br_des_ct_cbcenc_vtable
,
505 &br_des_ct_cbcdec_vtable
);
506 br_ssl_engine_set_cbc(&cc
.eng
,
507 &br_sslrec_in_cbc_vtable
,
508 &br_sslrec_out_cbc_vtable
);
510 if ((req
& REQ_RSAKEYX
) != 0) {
511 br_ssl_client_set_rsapub(&cc
, &br_rsa_i31_public
);
513 if ((req
& REQ_ECDHE_RSA
) != 0) {
514 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
515 br_ssl_client_set_rsavrfy(&cc
, &br_rsa_i31_pkcs1_vrfy
);
517 if ((req
& REQ_ECDHE_ECDSA
) != 0) {
518 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
519 br_ssl_client_set_ecdsa(&cc
, &br_ecdsa_i31_vrfy_asn1
);
521 if ((req
& REQ_ECDH
) != 0) {
522 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
525 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
527 for (u
= 0; hash_functions
[u
].name
; u
++) {
528 const br_hash_class
*hc
;
531 hc
= hash_functions
[u
].hclass
;
532 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
533 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
534 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
535 br_x509_minimal_set_hash(&xc
, id
, hc
);
538 if (vmin
<= BR_TLS11
) {
539 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
541 if (vmax
>= BR_TLS12
) {
542 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
543 br_ssl_engine_set_prf_sha256(&cc
.eng
,
544 &br_tls12_sha256_prf
);
546 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
547 br_ssl_engine_set_prf_sha384(&cc
.eng
,
548 &br_tls12_sha384_prf
);
551 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
552 br_x509_minimal_set_ecdsa(&xc
,
553 &br_ec_prime_i31
, &br_ecdsa_i31_vrfy_asn1
);
556 * If there is no provided trust anchor, then certificate validation
557 * will always fail. In that situation, we use our custom wrapper
558 * that tolerates unknown anchors.
560 if (VEC_LEN(anchors
) == 0) {
563 "WARNING: no configured trust anchor\n");
565 x509_noanchor_init(&xwc
, &xc
.vtable
);
566 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
568 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
571 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
572 br_ssl_client_reset(&cc
, sni
, 0);
575 * Connect to the peer.
577 fd
= host_connect(host
, port
, verbose
);
579 goto client_exit_error
;
583 * Run the engine until completion.
585 if (run_ssl_engine(&cc
.eng
, fd
,
586 (verbose
? RUN_ENGINE_VERBOSE
: 0)
587 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
589 goto client_exit_error
;
595 * Release allocated structures.
601 VEC_CLEAREXT(anchors
, &free_ta_contents
);