1*96b8bb3eStb /* $OpenBSD: client.c,v 1.11 2022/07/07 13:12:57 tb Exp $ */
29231079cSbluhm /*
31f83e6f0Sbluhm * Copyright (c) 2018-2019 Alexander Bluhm <bluhm@openbsd.org>
49231079cSbluhm *
59231079cSbluhm * Permission to use, copy, modify, and distribute this software for any
69231079cSbluhm * purpose with or without fee is hereby granted, provided that the above
79231079cSbluhm * copyright notice and this permission notice appear in all copies.
89231079cSbluhm *
99231079cSbluhm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
109231079cSbluhm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119231079cSbluhm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
129231079cSbluhm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139231079cSbluhm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149231079cSbluhm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
159231079cSbluhm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
169231079cSbluhm */
179231079cSbluhm
189231079cSbluhm #include <sys/types.h>
199231079cSbluhm #include <sys/socket.h>
209231079cSbluhm
219231079cSbluhm #include <err.h>
229231079cSbluhm #include <netdb.h>
239231079cSbluhm #include <stdio.h>
2422303e31Sbluhm #include <string.h>
259231079cSbluhm #include <unistd.h>
269231079cSbluhm
279231079cSbluhm #include <openssl/err.h>
289231079cSbluhm #include <openssl/ssl.h>
299231079cSbluhm
309231079cSbluhm #include "util.h"
319231079cSbluhm
329231079cSbluhm void __dead usage(void);
339231079cSbluhm
349231079cSbluhm void __dead
usage(void)359231079cSbluhm usage(void)
369231079cSbluhm {
371f83e6f0Sbluhm fprintf(stderr, "usage: client [-Lsv] [-C CA] [-c crt -k key] "
38f9b27065Sbluhm "[-l ciphers] [-V version] host port\n");
399231079cSbluhm exit(2);
409231079cSbluhm }
419231079cSbluhm
429231079cSbluhm int
main(int argc,char * argv[])439231079cSbluhm main(int argc, char *argv[])
449231079cSbluhm {
459231079cSbluhm const SSL_METHOD *method;
469231079cSbluhm SSL_CTX *ctx;
479231079cSbluhm SSL *ssl;
489231079cSbluhm BIO *bio;
497ca394abSbluhm SSL_SESSION *session = NULL;
501f83e6f0Sbluhm int ch, error, listciphers = 0, sessionreuse = 0, verify = 0;
51f9b27065Sbluhm int version = 0;
52d59d2e41Sbluhm char buf[256];
531f83e6f0Sbluhm char *ca = NULL, *crt = NULL, *key = NULL, *ciphers = NULL;
541f83e6f0Sbluhm char *host_port, *host = "127.0.0.1", *port = "0";
559231079cSbluhm
56f9b27065Sbluhm while ((ch = getopt(argc, argv, "C:c:k:Ll:p:sV:v")) != -1) {
57a8d85e88Sbluhm switch (ch) {
58a8d85e88Sbluhm case 'C':
59a8d85e88Sbluhm ca = optarg;
60a8d85e88Sbluhm break;
61a8d85e88Sbluhm case 'c':
62a8d85e88Sbluhm crt = optarg;
63a8d85e88Sbluhm break;
64a8d85e88Sbluhm case 'k':
65a8d85e88Sbluhm key = optarg;
66a8d85e88Sbluhm break;
671f83e6f0Sbluhm case 'L':
681f83e6f0Sbluhm listciphers = 1;
691f83e6f0Sbluhm break;
701f83e6f0Sbluhm case 'l':
711f83e6f0Sbluhm ciphers = optarg;
721f83e6f0Sbluhm break;
737ca394abSbluhm case 's':
747ca394abSbluhm /* multiple reueses are possible */
757ca394abSbluhm sessionreuse++;
767ca394abSbluhm break;
77f9b27065Sbluhm case 'V':
78f9b27065Sbluhm if (strcmp(optarg, "TLS1") == 0) {
79f9b27065Sbluhm version = TLS1_VERSION;
80f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_1") == 0) {
81f9b27065Sbluhm version = TLS1_1_VERSION;
82f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_2") == 0) {
83f9b27065Sbluhm version = TLS1_2_VERSION;
84f9b27065Sbluhm #ifdef TLS1_3_VERSION
85f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_3") == 0) {
86f9b27065Sbluhm version = TLS1_3_VERSION;
87f9b27065Sbluhm #endif
88f9b27065Sbluhm } else {
89f9b27065Sbluhm errx(1, "unknown protocol version: %s", optarg);
90f9b27065Sbluhm }
91f9b27065Sbluhm break;
92a8d85e88Sbluhm case 'v':
93a8d85e88Sbluhm verify = 1;
94a8d85e88Sbluhm break;
95a8d85e88Sbluhm default:
96a8d85e88Sbluhm usage();
97a8d85e88Sbluhm }
98a8d85e88Sbluhm }
99a8d85e88Sbluhm argc -= optind;
100a8d85e88Sbluhm argv += optind;
101a8d85e88Sbluhm if (argc == 2) {
102a8d85e88Sbluhm host = argv[0];
103a8d85e88Sbluhm port = argv[1];
1041f83e6f0Sbluhm } else if (!listciphers) {
1059231079cSbluhm usage();
1069231079cSbluhm }
1079231079cSbluhm if (asprintf(&host_port, strchr(host, ':') ? "[%s]:%s" : "%s:%s",
1089231079cSbluhm host, port) == -1)
1099231079cSbluhm err(1, "asprintf host port");
110a8d85e88Sbluhm if ((crt == NULL && key != NULL) || (crt != NULL && key == NULL))
111a8d85e88Sbluhm errx(1, "certificate and private key must be used together");
1129231079cSbluhm
1139231079cSbluhm SSL_library_init();
1149231079cSbluhm SSL_load_error_strings();
11522303e31Sbluhm print_version();
1169231079cSbluhm
1179231079cSbluhm /* setup method and context */
118188261f9Sbluhm #if OPENSSL_VERSION_NUMBER >= 0x1010000f
119188261f9Sbluhm method = TLS_client_method();
120188261f9Sbluhm if (method == NULL)
121188261f9Sbluhm err_ssl(1, "TLS_client_method");
122188261f9Sbluhm #else
123f9b27065Sbluhm switch (version) {
124f9b27065Sbluhm case TLS1_VERSION:
125f9b27065Sbluhm method = TLSv1_client_method();
126f9b27065Sbluhm break;
127f9b27065Sbluhm case TLS1_1_VERSION:
128f9b27065Sbluhm method = TLSv1_1_client_method();
129f9b27065Sbluhm break;
130f9b27065Sbluhm case TLS1_2_VERSION:
131f9b27065Sbluhm method = TLSv1_2_client_method();
132f9b27065Sbluhm break;
133f9b27065Sbluhm #ifdef TLS1_3_VERSION
134f9b27065Sbluhm case TLS1_3_VERSION:
135f9b27065Sbluhm err(1, "TLS1_3 not supported");
136f9b27065Sbluhm #endif
137f9b27065Sbluhm default:
1389231079cSbluhm method = SSLv23_client_method();
139f9b27065Sbluhm break;
140f9b27065Sbluhm }
1419231079cSbluhm if (method == NULL)
1429231079cSbluhm err_ssl(1, "SSLv23_client_method");
143188261f9Sbluhm #endif
1449231079cSbluhm ctx = SSL_CTX_new(method);
1459231079cSbluhm if (ctx == NULL)
1469231079cSbluhm err_ssl(1, "SSL_CTX_new");
1479231079cSbluhm
148f9b27065Sbluhm #if OPENSSL_VERSION_NUMBER >= 0x1010000f
149f9b27065Sbluhm if (version) {
150f9b27065Sbluhm if (SSL_CTX_set_min_proto_version(ctx, version) != 1)
151f9b27065Sbluhm err_ssl(1, "SSL_CTX_set_min_proto_version");
152f9b27065Sbluhm if (SSL_CTX_set_max_proto_version(ctx, version) != 1)
153f9b27065Sbluhm err_ssl(1, "SSL_CTX_set_max_proto_version");
154f9b27065Sbluhm }
155f9b27065Sbluhm #endif
156f9b27065Sbluhm
157a8d85e88Sbluhm /* load client certificate */
158a8d85e88Sbluhm if (crt != NULL) {
159a8d85e88Sbluhm if (SSL_CTX_use_certificate_file(ctx, crt,
160a8d85e88Sbluhm SSL_FILETYPE_PEM) <= 0)
161a8d85e88Sbluhm err_ssl(1, "SSL_CTX_use_certificate_file");
162a8d85e88Sbluhm if (SSL_CTX_use_PrivateKey_file(ctx, key,
163a8d85e88Sbluhm SSL_FILETYPE_PEM) <= 0)
164a8d85e88Sbluhm err_ssl(1, "SSL_CTX_use_PrivateKey_file");
165a8d85e88Sbluhm if (SSL_CTX_check_private_key(ctx) <= 0)
166a8d85e88Sbluhm err_ssl(1, "SSL_CTX_check_private_key");
167a8d85e88Sbluhm }
168a8d85e88Sbluhm
169a8d85e88Sbluhm /* verify server certificate */
170a8d85e88Sbluhm if (ca != NULL) {
171a8d85e88Sbluhm if (SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0)
172a8d85e88Sbluhm err_ssl(1, "SSL_CTX_load_verify_locations");
173a8d85e88Sbluhm }
174a8d85e88Sbluhm SSL_CTX_set_verify(ctx, verify ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
175a8d85e88Sbluhm verify_callback);
176a8d85e88Sbluhm
1777ca394abSbluhm if (sessionreuse) {
1787ca394abSbluhm SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT);
1797ca394abSbluhm }
1807ca394abSbluhm
1811f83e6f0Sbluhm if (ciphers) {
1821f83e6f0Sbluhm if (SSL_CTX_set_cipher_list(ctx, ciphers) <= 0)
1831f83e6f0Sbluhm err_ssl(1, "SSL_CTX_set_cipher_list");
1841f83e6f0Sbluhm }
1851f83e6f0Sbluhm
1861f83e6f0Sbluhm if (listciphers) {
187*96b8bb3eStb STACK_OF(SSL_CIPHER) *supported_ciphers;
188*96b8bb3eStb
189*96b8bb3eStb #if OPENSSL_VERSION_NUMBER < 0x1010000f
190*96b8bb3eStb #define SSL_get1_supported_ciphers SSL_get_ciphers
191*96b8bb3eStb #endif
1921f83e6f0Sbluhm ssl = SSL_new(ctx);
1931f83e6f0Sbluhm if (ssl == NULL)
1941f83e6f0Sbluhm err_ssl(1, "SSL_new");
195*96b8bb3eStb supported_ciphers = SSL_get1_supported_ciphers(ssl);
196*96b8bb3eStb if (supported_ciphers == NULL)
197*96b8bb3eStb err_ssl(1, "SSL_get1_supported_ciphers");
198*96b8bb3eStb print_ciphers(supported_ciphers);
199*96b8bb3eStb
200*96b8bb3eStb #if OPENSSL_VERSION_NUMBER >= 0x1010000f
201*96b8bb3eStb sk_SSL_CIPHER_free(supported_ciphers);
202*96b8bb3eStb #endif
2031f83e6f0Sbluhm return 0;
2041f83e6f0Sbluhm }
2051f83e6f0Sbluhm
2067ca394abSbluhm do {
2077ca394abSbluhm /* setup bio for socket operations */
2089231079cSbluhm bio = BIO_new_connect(host_port);
2099231079cSbluhm if (bio == NULL)
2109231079cSbluhm err_ssl(1, "BIO_new_connect");
2119231079cSbluhm
2129231079cSbluhm /* connect */
2139231079cSbluhm if (BIO_do_connect(bio) <= 0)
2149231079cSbluhm err_ssl(1, "BIO_do_connect");
2159231079cSbluhm printf("connect ");
2169231079cSbluhm print_sockname(bio);
2179231079cSbluhm printf("connect ");
2189231079cSbluhm print_peername(bio);
2199231079cSbluhm
2209231079cSbluhm /* do ssl client handshake */
2217ca394abSbluhm ssl = SSL_new(ctx);
2227ca394abSbluhm if (ssl == NULL)
2237ca394abSbluhm err_ssl(1, "SSL_new");
2249231079cSbluhm SSL_set_bio(ssl, bio, bio);
2257ca394abSbluhm /* resuse session if possible */
2267ca394abSbluhm if (session != NULL) {
2277ca394abSbluhm if (SSL_set_session(ssl, session) <= 0)
2287ca394abSbluhm err_ssl(1, "SSL_set_session");
2297ca394abSbluhm }
2309231079cSbluhm if ((error = SSL_connect(ssl)) <= 0)
2319231079cSbluhm err_ssl(1, "SSL_connect %d", error);
2327ca394abSbluhm printf("session %d: %s\n", sessionreuse,
2337ca394abSbluhm SSL_session_reused(ssl) ? "reuse" : "new");
2347ca394abSbluhm if (fflush(stdout) != 0)
2357ca394abSbluhm err(1, "fflush stdout");
2369231079cSbluhm
2379231079cSbluhm /* print session statistics */
2387ca394abSbluhm if (sessionreuse) {
2397ca394abSbluhm session = SSL_get1_session(ssl);
2407ca394abSbluhm if (session == NULL)
2417ca394abSbluhm err_ssl(1, "SSL1_get_session");
2427ca394abSbluhm } else {
2439231079cSbluhm session = SSL_get_session(ssl);
2449231079cSbluhm if (session == NULL)
2459231079cSbluhm err_ssl(1, "SSL_get_session");
2467ca394abSbluhm }
2479231079cSbluhm if (SSL_SESSION_print_fp(stdout, session) <= 0)
2489231079cSbluhm err_ssl(1, "SSL_SESSION_print_fp");
2499231079cSbluhm
2507ca394abSbluhm /* read server greeting and write client hello over TLS */
2519231079cSbluhm if ((error = SSL_read(ssl, buf, 9)) <= 0)
2529231079cSbluhm err_ssl(1, "SSL_read %d", error);
2539231079cSbluhm if (error != 9)
2549231079cSbluhm errx(1, "read not 9 bytes greeting: %d", error);
2559231079cSbluhm buf[9] = '\0';
2569231079cSbluhm printf("<<< %s", buf);
2579231079cSbluhm if (fflush(stdout) != 0)
2589231079cSbluhm err(1, "fflush stdout");
2599231079cSbluhm strlcpy(buf, "hello\n", sizeof(buf));
2609231079cSbluhm printf(">>> %s", buf);
2619231079cSbluhm if (fflush(stdout) != 0)
2629231079cSbluhm err(1, "fflush stdout");
2639231079cSbluhm if ((error = SSL_write(ssl, buf, 6)) <= 0)
2649231079cSbluhm err_ssl(1, "SSL_write %d", error);
2659231079cSbluhm if (error != 6)
2669231079cSbluhm errx(1, "write not 6 bytes hello: %d", error);
2679231079cSbluhm
2689231079cSbluhm /* shutdown connection */
2699231079cSbluhm if ((error = SSL_shutdown(ssl)) < 0)
2709231079cSbluhm err_ssl(1, "SSL_shutdown unidirectional %d", error);
2719231079cSbluhm if (error <= 0) {
2729231079cSbluhm if ((error = SSL_shutdown(ssl)) <= 0)
2737ca394abSbluhm err_ssl(1, "SSL_shutdown bidirectional %d",
2747ca394abSbluhm error);
2759231079cSbluhm }
2769231079cSbluhm
2779231079cSbluhm SSL_free(ssl);
2787ca394abSbluhm } while (sessionreuse--);
2797ca394abSbluhm
2809231079cSbluhm SSL_CTX_free(ctx);
2819231079cSbluhm
2829231079cSbluhm printf("success\n");
2839231079cSbluhm
2849231079cSbluhm return 0;
2859231079cSbluhm }
286