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
) {
62 char tmp
[INET6_ADDRSTRLEN
+ 50];
64 sa
= (struct sockaddr
*)p
->ai_addr
;
65 if (sa
->sa_family
== AF_INET
) {
66 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
67 } else if (sa
->sa_family
== AF_INET6
) {
68 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
73 if (!inet_ntop(p
->ai_family
, addr
, tmp
, sizeof tmp
)) {
74 strcpy(tmp
, "<invalid>");
77 sprintf(tmp
, "<unknown family: %d>",
81 fprintf(stderr
, "connecting to: %s\n", tmp
);
83 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
90 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
101 fprintf(stderr
, "ERROR: failed to connect\n");
106 fprintf(stderr
, "connected.\n");
110 * We make the socket non-blocking, since we are going to use
111 * poll() to organise I/O.
113 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
121 "usage: brssl client server[:port] [ options ]\n");
125 " -q suppress verbose messages\n");
127 " -trace activate extra debug messages (dump of all packets)\n");
129 " -sni name use this specific name for SNI\n");
131 " -nosni do not send any SNI\n");
133 " -mono use monodirectional buffering\n");
135 " -buf length set the I/O buffer length (in bytes)\n");
137 " -CA file add certificates in 'file' to trust anchors\n");
139 " -list list supported names (protocols, algorithms...)\n");
141 " -vmin name set minimum supported version (default: TLS-1.0)\n");
143 " -vmax name set maximum supported version (default: TLS-1.2)\n");
145 " -cs names set list of supported cipher suites (comma-separated)\n");
147 " -hf names add support for some hash functions (comma-separated)\n");
152 do_client(int argc
, char *argv
[])
158 const char *server_name
;
162 anchor_list anchors
= VEC_INIT
;
164 cipher_suite
*suites
;
169 br_ssl_client_context cc
;
170 br_x509_minimal_context xc
;
171 x509_noanchor_context xwc
;
172 const br_hash_class
*dnhash
;
173 unsigned char *iobuf
;
194 for (i
= 0; i
< argc
; i
++) {
199 if (server_name
!= NULL
) {
201 "ERROR: duplicate server name\n");
203 goto client_exit_error
;
208 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
210 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
212 } else if (eqstr(arg
, "-trace")) {
214 } else if (eqstr(arg
, "-sni")) {
217 "ERROR: no argument for '-sni'\n");
219 goto client_exit_error
;
222 fprintf(stderr
, "ERROR: duplicate SNI\n");
224 goto client_exit_error
;
227 } else if (eqstr(arg
, "-nosni")) {
229 fprintf(stderr
, "ERROR: duplicate SNI\n");
231 goto client_exit_error
;
234 } else if (eqstr(arg
, "-mono")) {
236 } else if (eqstr(arg
, "-buf")) {
239 "ERROR: no argument for '-buf'\n");
241 goto client_exit_error
;
244 if (iobuf_len
!= 0) {
246 "ERROR: duplicate I/O buffer length\n");
248 goto client_exit_error
;
250 iobuf_len
= parse_size(arg
);
251 if (iobuf_len
== (size_t)-1) {
253 goto client_exit_error
;
255 } else if (eqstr(arg
, "-CA")) {
258 "ERROR: no argument for '-CA'\n");
260 goto client_exit_error
;
263 if (read_trust_anchors(&anchors
, arg
) == 0) {
265 goto client_exit_error
;
267 } else if (eqstr(arg
, "-list")) {
270 } else if (eqstr(arg
, "-vmin")) {
273 "ERROR: no argument for '-vmin'\n");
275 goto client_exit_error
;
280 "ERROR: duplicate minimum version\n");
282 goto client_exit_error
;
284 vmin
= parse_version(arg
, strlen(arg
));
287 "ERROR: unrecognised version '%s'\n",
290 goto client_exit_error
;
292 } else if (eqstr(arg
, "-vmax")) {
295 "ERROR: no argument for '-vmax'\n");
297 goto client_exit_error
;
302 "ERROR: duplicate maximum version\n");
304 goto client_exit_error
;
306 vmax
= parse_version(arg
, strlen(arg
));
309 "ERROR: unrecognised version '%s'\n",
312 goto client_exit_error
;
314 } else if (eqstr(arg
, "-cs")) {
317 "ERROR: no argument for '-cs'\n");
319 goto client_exit_error
;
322 if (suites
!= NULL
) {
323 fprintf(stderr
, "ERROR: duplicate list"
324 " of cipher suites\n");
326 goto client_exit_error
;
328 suites
= parse_suites(arg
, &num_suites
);
329 if (suites
== NULL
) {
331 goto client_exit_error
;
333 } else if (eqstr(arg
, "-hf")) {
338 "ERROR: no argument for '-hf'\n");
340 goto client_exit_error
;
343 x
= parse_hash_functions(arg
);
346 goto client_exit_error
;
350 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
352 goto client_exit_error
;
355 if (server_name
== NULL
) {
356 fprintf(stderr
, "ERROR: no server name/address provided\n");
358 goto client_exit_error
;
360 for (u
= strlen(server_name
); u
> 0; u
--) {
361 int c
= server_name
[u
- 1];
365 if (c
< '0' || c
> '9') {
371 host
= xstrdup(server_name
);
374 port
= xstrdup(server_name
+ u
);
376 memcpy(host
, server_name
, u
- 1);
390 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
391 " version combination\n");
393 goto client_exit_error
;
395 if (suites
== NULL
) {
398 for (u
= 0; cipher_suites
[u
].name
; u
++) {
399 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
405 suites
= xmalloc(num_suites
* sizeof *suites
);
407 for (u
= 0; cipher_suites
[u
].name
; u
++) {
408 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
411 suites
[num_suites
++] = cipher_suites
[u
];
416 hfuns
= (unsigned)-1;
418 if (iobuf_len
== 0) {
420 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
422 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
425 iobuf
= xmalloc(iobuf_len
);
428 * Compute implementation requirements and inject implementations.
430 suite_ids
= xmalloc(num_suites
* sizeof *suite_ids
);
431 br_ssl_client_zero(&cc
);
432 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
434 for (u
= 0; hash_functions
[u
].name
; u
++) {
435 const br_hash_class
*hc
;
438 hc
= hash_functions
[u
].hclass
;
439 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
440 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
444 if (dnhash
== NULL
) {
445 fprintf(stderr
, "ERROR: no supported hash function\n");
446 goto client_exit_error
;
448 br_x509_minimal_init(&xc
, dnhash
,
449 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
450 if (vmin
<= BR_TLS11
) {
451 if (!(hfuns
& (1 << br_md5_ID
))) {
452 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
453 goto client_exit_error
;
455 if (!(hfuns
& (1 << br_sha1_ID
))) {
456 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
457 goto client_exit_error
;
460 for (u
= 0; u
< num_suites
; u
++) {
464 suite_ids
[u
] = suites
[u
].suite
;
465 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
467 "ERROR: cipher suite %s requires TLS 1.2\n",
469 goto client_exit_error
;
471 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
473 "ERROR: cipher suite %s requires SHA-1\n",
475 goto client_exit_error
;
477 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
479 "ERROR: cipher suite %s requires SHA-256\n",
481 goto client_exit_error
;
483 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
485 "ERROR: cipher suite %s requires SHA-384\n",
487 goto client_exit_error
;
489 /* TODO: algorithm implementation selection */
490 if ((req
& REQ_AESCBC
) != 0) {
491 br_ssl_engine_set_aes_cbc(&cc
.eng
,
492 &br_aes_ct_cbcenc_vtable
,
493 &br_aes_ct_cbcdec_vtable
);
494 br_ssl_engine_set_cbc(&cc
.eng
,
495 &br_sslrec_in_cbc_vtable
,
496 &br_sslrec_out_cbc_vtable
);
498 if ((req
& REQ_AESGCM
) != 0) {
499 br_ssl_engine_set_aes_ctr(&cc
.eng
,
500 &br_aes_ct_ctr_vtable
);
501 br_ssl_engine_set_ghash(&cc
.eng
,
503 br_ssl_engine_set_gcm(&cc
.eng
,
504 &br_sslrec_in_gcm_vtable
,
505 &br_sslrec_out_gcm_vtable
);
507 if ((req
& REQ_3DESCBC
) != 0) {
508 br_ssl_engine_set_des_cbc(&cc
.eng
,
509 &br_des_ct_cbcenc_vtable
,
510 &br_des_ct_cbcdec_vtable
);
511 br_ssl_engine_set_cbc(&cc
.eng
,
512 &br_sslrec_in_cbc_vtable
,
513 &br_sslrec_out_cbc_vtable
);
515 if ((req
& REQ_RSAKEYX
) != 0) {
516 br_ssl_client_set_rsapub(&cc
, &br_rsa_i31_public
);
518 if ((req
& REQ_ECDHE_RSA
) != 0) {
519 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
520 br_ssl_client_set_rsavrfy(&cc
, &br_rsa_i31_pkcs1_vrfy
);
522 if ((req
& REQ_ECDHE_ECDSA
) != 0) {
523 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
524 br_ssl_client_set_ecdsa(&cc
, &br_ecdsa_i31_vrfy_asn1
);
526 if ((req
& REQ_ECDH
) != 0) {
527 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
530 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
532 for (u
= 0; hash_functions
[u
].name
; u
++) {
533 const br_hash_class
*hc
;
536 hc
= hash_functions
[u
].hclass
;
537 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
538 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
539 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
540 br_x509_minimal_set_hash(&xc
, id
, hc
);
543 if (vmin
<= BR_TLS11
) {
544 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
546 if (vmax
>= BR_TLS12
) {
547 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
548 br_ssl_engine_set_prf_sha256(&cc
.eng
,
549 &br_tls12_sha256_prf
);
551 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
552 br_ssl_engine_set_prf_sha384(&cc
.eng
,
553 &br_tls12_sha384_prf
);
556 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
557 br_x509_minimal_set_ecdsa(&xc
,
558 &br_ec_prime_i31
, &br_ecdsa_i31_vrfy_asn1
);
561 * If there is no provided trust anchor, then certificate validation
562 * will always fail. In that situation, we use our custom wrapper
563 * that tolerates unknown anchors.
565 if (VEC_LEN(anchors
) == 0) {
568 "WARNING: no configured trust anchor\n");
570 x509_noanchor_init(&xwc
, &xc
.vtable
);
571 br_ssl_engine_set_x509(&cc
.eng
, &xwc
.vtable
);
573 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
576 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
577 br_ssl_client_reset(&cc
, sni
, 0);
580 * Connect to the peer.
582 fd
= host_connect(host
, port
, verbose
);
584 goto client_exit_error
;
588 * Run the engine until completion.
590 if (run_ssl_engine(&cc
.eng
, fd
,
591 (verbose
? RUN_ENGINE_VERBOSE
: 0)
592 | (trace
? RUN_ENGINE_TRACE
: 0)) != 0)
594 goto client_exit_error
;
600 * Release allocated structures.
606 VEC_CLEAREXT(anchors
, &free_ta_contents
);