1*562d56f4Stb /* $OpenBSD: server.c,v 1.12 2023/02/01 14:39:09 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>
247ca394abSbluhm #include <stdlib.h>
2522303e31Sbluhm #include <string.h>
269231079cSbluhm #include <unistd.h>
279231079cSbluhm
289231079cSbluhm #include <openssl/err.h>
299231079cSbluhm #include <openssl/ssl.h>
309231079cSbluhm
319231079cSbluhm #include "util.h"
329231079cSbluhm
339231079cSbluhm void __dead usage(void);
349231079cSbluhm
359231079cSbluhm void __dead
usage(void)369231079cSbluhm usage(void)
379231079cSbluhm {
381f83e6f0Sbluhm fprintf(stderr, "usage: server [-Lsvv] [-C CA] [-c crt -k key] "
39f9b27065Sbluhm "[-l ciphers] [-p dhparam] [-V version] [host port]\n");
409231079cSbluhm exit(2);
419231079cSbluhm }
429231079cSbluhm
439231079cSbluhm int
main(int argc,char * argv[])449231079cSbluhm main(int argc, char *argv[])
459231079cSbluhm {
469231079cSbluhm const SSL_METHOD *method;
479231079cSbluhm SSL_CTX *ctx;
489231079cSbluhm SSL *ssl;
497ca394abSbluhm BIO *abio, *cbio;
509231079cSbluhm SSL_SESSION *session;
511f83e6f0Sbluhm int ch, error, listciphers = 0, sessionreuse = 0, verify = 0;
52f9b27065Sbluhm int version = 0;
531f83e6f0Sbluhm char buf[256], *dhparam = NULL;
541f83e6f0Sbluhm char *ca = NULL, *crt = NULL, *key = NULL, *ciphers = NULL;
55a8d85e88Sbluhm char *host_port, *host = "127.0.0.1", *port = "0";
569231079cSbluhm
57f9b27065Sbluhm while ((ch = getopt(argc, argv, "C:c:k:Ll:p:sV:v")) != -1) {
58a8d85e88Sbluhm switch (ch) {
59a8d85e88Sbluhm case 'C':
60a8d85e88Sbluhm ca = optarg;
61a8d85e88Sbluhm break;
62a8d85e88Sbluhm case 'c':
63a8d85e88Sbluhm crt = optarg;
64a8d85e88Sbluhm break;
65a8d85e88Sbluhm case 'k':
66a8d85e88Sbluhm key = optarg;
67a8d85e88Sbluhm break;
681f83e6f0Sbluhm case 'L':
691f83e6f0Sbluhm listciphers = 1;
701f83e6f0Sbluhm break;
711f83e6f0Sbluhm case 'l':
721f83e6f0Sbluhm ciphers = optarg;
731f83e6f0Sbluhm break;
741f83e6f0Sbluhm case 'p':
751f83e6f0Sbluhm dhparam = optarg;
761f83e6f0Sbluhm break;
777ca394abSbluhm case 's':
787ca394abSbluhm /* multiple reueses are possible */
797ca394abSbluhm sessionreuse++;
807ca394abSbluhm break;
81f9b27065Sbluhm case 'V':
82f9b27065Sbluhm if (strcmp(optarg, "TLS1") == 0) {
83f9b27065Sbluhm version = TLS1_VERSION;
84f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_1") == 0) {
85f9b27065Sbluhm version = TLS1_1_VERSION;
86f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_2") == 0) {
87f9b27065Sbluhm version = TLS1_2_VERSION;
88f9b27065Sbluhm } else if (strcmp(optarg, "TLS1_3") == 0) {
89f9b27065Sbluhm version = TLS1_3_VERSION;
90f9b27065Sbluhm } else {
91f9b27065Sbluhm errx(1, "unknown protocol version: %s", optarg);
92f9b27065Sbluhm }
93f9b27065Sbluhm break;
94a8d85e88Sbluhm case 'v':
95a8d85e88Sbluhm /* use twice to force client cert */
96a8d85e88Sbluhm verify++;
97a8d85e88Sbluhm break;
98a8d85e88Sbluhm default:
99a8d85e88Sbluhm usage();
100a8d85e88Sbluhm }
101a8d85e88Sbluhm }
102a8d85e88Sbluhm argc -= optind;
103a8d85e88Sbluhm argv += optind;
104a8d85e88Sbluhm if (argc == 2) {
105a8d85e88Sbluhm host = argv[0];
106a8d85e88Sbluhm port = argv[1];
1071f83e6f0Sbluhm } else if (argc != 0 && !listciphers) {
1089231079cSbluhm usage();
1099231079cSbluhm }
1109231079cSbluhm if (asprintf(&host_port, strchr(host, ':') ? "[%s]:%s" : "%s:%s",
1119231079cSbluhm host, port) == -1)
1129231079cSbluhm err(1, "asprintf host port");
113a8d85e88Sbluhm if ((crt == NULL && key != NULL) || (crt != NULL && key == NULL))
114a8d85e88Sbluhm errx(1, "certificate and private key must be used together");
115a8d85e88Sbluhm if (crt == NULL && asprintf(&crt, "%s.crt", host) == -1)
1169231079cSbluhm err(1, "asprintf crt");
117a8d85e88Sbluhm if (key == NULL && asprintf(&key, "%s.key", host) == -1)
1189231079cSbluhm err(1, "asprintf key");
1199231079cSbluhm
1209231079cSbluhm SSL_library_init();
1219231079cSbluhm SSL_load_error_strings();
12222303e31Sbluhm print_version();
1239231079cSbluhm
1249231079cSbluhm /* setup method and context */
125188261f9Sbluhm #if OPENSSL_VERSION_NUMBER >= 0x1010000f
126188261f9Sbluhm method = TLS_server_method();
127188261f9Sbluhm if (method == NULL)
128188261f9Sbluhm err_ssl(1, "TLS_server_method");
129188261f9Sbluhm #else
130f9b27065Sbluhm switch (version) {
131f9b27065Sbluhm case TLS1_VERSION:
132f9b27065Sbluhm method = TLSv1_server_method();
133f9b27065Sbluhm break;
134f9b27065Sbluhm case TLS1_1_VERSION:
135f9b27065Sbluhm method = TLSv1_1_server_method();
136f9b27065Sbluhm break;
137f9b27065Sbluhm case TLS1_2_VERSION:
138f9b27065Sbluhm method = TLSv1_2_server_method();
139f9b27065Sbluhm break;
140f9b27065Sbluhm #ifdef TLS1_3_VERSION
141f9b27065Sbluhm case TLS1_3_VERSION:
142f9b27065Sbluhm err(1, "TLS1_3 not supported");
143f9b27065Sbluhm #endif
144f9b27065Sbluhm default:
1459231079cSbluhm method = SSLv23_server_method();
146f9b27065Sbluhm break;
147f9b27065Sbluhm }
1489231079cSbluhm if (method == NULL)
1499231079cSbluhm err_ssl(1, "SSLv23_server_method");
150188261f9Sbluhm #endif
1519231079cSbluhm ctx = SSL_CTX_new(method);
1529231079cSbluhm if (ctx == NULL)
1539231079cSbluhm err_ssl(1, "SSL_CTX_new");
1549231079cSbluhm
155f9b27065Sbluhm #if OPENSSL_VERSION_NUMBER >= 0x1010000f
156f9b27065Sbluhm if (version) {
157f9b27065Sbluhm if (SSL_CTX_set_min_proto_version(ctx, version) != 1)
158f9b27065Sbluhm err_ssl(1, "SSL_CTX_set_min_proto_version");
159f9b27065Sbluhm if (SSL_CTX_set_max_proto_version(ctx, version) != 1)
160f9b27065Sbluhm err_ssl(1, "SSL_CTX_set_max_proto_version");
161f9b27065Sbluhm }
162f9b27065Sbluhm #endif
163f9b27065Sbluhm
1641f83e6f0Sbluhm #if OPENSSL_VERSION_NUMBER >= 0x10100000
1651f83e6f0Sbluhm /* needed to use DHE cipher with libressl */
1661f83e6f0Sbluhm if (SSL_CTX_set_dh_auto(ctx, 1) <= 0)
1671f83e6f0Sbluhm err_ssl(1, "SSL_CTX_set_dh_auto");
1681f83e6f0Sbluhm #endif
1691f83e6f0Sbluhm /* needed to use ADH, EDH, DHE cipher with openssl */
1701f83e6f0Sbluhm if (dhparam != NULL) {
1711f83e6f0Sbluhm DH *dh;
1721f83e6f0Sbluhm FILE *file;
1731f83e6f0Sbluhm
1741f83e6f0Sbluhm file = fopen(dhparam, "r");
1751f83e6f0Sbluhm if (file == NULL)
1761f83e6f0Sbluhm err(1, "fopen %s", dhparam);
1771f83e6f0Sbluhm dh = PEM_read_DHparams(file, NULL, NULL, NULL);
1781f83e6f0Sbluhm if (dh == NULL)
1791f83e6f0Sbluhm err_ssl(1, "PEM_read_DHparams");
1801f83e6f0Sbluhm if (SSL_CTX_set_tmp_dh(ctx, dh) <= 0)
1811f83e6f0Sbluhm err_ssl(1, "SSL_CTX_set_tmp_dh");
1821f83e6f0Sbluhm fclose(file);
1831f83e6f0Sbluhm }
1841f83e6f0Sbluhm
1859231079cSbluhm /* load server certificate */
1869231079cSbluhm if (SSL_CTX_use_certificate_file(ctx, crt, SSL_FILETYPE_PEM) <= 0)
1879231079cSbluhm err_ssl(1, "SSL_CTX_use_certificate_file");
1889231079cSbluhm if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0)
1899231079cSbluhm err_ssl(1, "SSL_CTX_use_PrivateKey_file");
1909231079cSbluhm if (SSL_CTX_check_private_key(ctx) <= 0)
1919231079cSbluhm err_ssl(1, "SSL_CTX_check_private_key");
1929231079cSbluhm
193a8d85e88Sbluhm /* request client certificate and verify it */
194a8d85e88Sbluhm if (ca != NULL) {
195a8d85e88Sbluhm STACK_OF(X509_NAME) *x509stack;
196a8d85e88Sbluhm
197a8d85e88Sbluhm x509stack = SSL_load_client_CA_file(ca);
198a8d85e88Sbluhm if (x509stack == NULL)
199a8d85e88Sbluhm err_ssl(1, "SSL_load_client_CA_file");
200a8d85e88Sbluhm SSL_CTX_set_client_CA_list(ctx, x509stack);
201a8d85e88Sbluhm if (SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0)
202a8d85e88Sbluhm err_ssl(1, "SSL_CTX_load_verify_locations");
203a8d85e88Sbluhm }
204a8d85e88Sbluhm SSL_CTX_set_verify(ctx,
205a8d85e88Sbluhm verify == 0 ? SSL_VERIFY_NONE :
206a8d85e88Sbluhm verify == 1 ? SSL_VERIFY_PEER :
207a8d85e88Sbluhm SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
208a8d85e88Sbluhm verify_callback);
209a8d85e88Sbluhm
2107ca394abSbluhm if (sessionreuse) {
2117ca394abSbluhm uint32_t context;
2127ca394abSbluhm
2137ca394abSbluhm SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
2147ca394abSbluhm context = arc4random();
2157ca394abSbluhm if (SSL_CTX_set_session_id_context(ctx,
2167ca394abSbluhm (unsigned char *)&context, sizeof(context)) <= 0)
2177ca394abSbluhm err_ssl(1, "SSL_CTX_set_session_id_context");
2187ca394abSbluhm }
2197ca394abSbluhm
2201f83e6f0Sbluhm if (ciphers) {
2211f83e6f0Sbluhm if (SSL_CTX_set_cipher_list(ctx, ciphers) <= 0)
2221f83e6f0Sbluhm err_ssl(1, "SSL_CTX_set_cipher_list");
2231f83e6f0Sbluhm }
2241f83e6f0Sbluhm
2251f83e6f0Sbluhm if (listciphers) {
22696b8bb3eStb STACK_OF(SSL_CIPHER) *supported_ciphers;
22796b8bb3eStb
2281f83e6f0Sbluhm ssl = SSL_new(ctx);
2291f83e6f0Sbluhm if (ssl == NULL)
2301f83e6f0Sbluhm err_ssl(1, "SSL_new");
23196b8bb3eStb supported_ciphers = SSL_get1_supported_ciphers(ssl);
23296b8bb3eStb if (supported_ciphers == NULL)
23396b8bb3eStb err_ssl(1, "SSL_get1_supported_ciphers");
23496b8bb3eStb print_ciphers(supported_ciphers);
23596b8bb3eStb
23696b8bb3eStb sk_SSL_CIPHER_free(supported_ciphers);
2371f83e6f0Sbluhm return 0;
2381f83e6f0Sbluhm }
2391f83e6f0Sbluhm
2407ca394abSbluhm /* setup bio for socket operations */
2417ca394abSbluhm abio = BIO_new_accept(host_port);
2427ca394abSbluhm if (abio == NULL)
2437ca394abSbluhm err_ssl(1, "BIO_new_accept");
2447ca394abSbluhm
2457ca394abSbluhm /* bind, listen */
2467ca394abSbluhm if (BIO_do_accept(abio) <= 0)
2477ca394abSbluhm err_ssl(1, "BIO_do_accept setup");
2487ca394abSbluhm printf("listen ");
2497ca394abSbluhm print_sockname(abio);
2507ca394abSbluhm
2517ca394abSbluhm /* fork to background and set timeout */
2527ca394abSbluhm if (daemon(1, 1) == -1)
2537ca394abSbluhm err(1, "daemon");
2545a9255e7Sbluhm alarm(10);
2557ca394abSbluhm
2567ca394abSbluhm do {
2577ca394abSbluhm /* accept connection */
2587ca394abSbluhm if (BIO_do_accept(abio) <= 0)
2597ca394abSbluhm err_ssl(1, "BIO_do_accept wait");
2607ca394abSbluhm cbio = BIO_pop(abio);
2617ca394abSbluhm printf("accept ");
2627ca394abSbluhm print_sockname(cbio);
2637ca394abSbluhm printf("accept ");
2647ca394abSbluhm print_peername(cbio);
2657ca394abSbluhm
2667ca394abSbluhm /* do ssl server handshake */
2679231079cSbluhm ssl = SSL_new(ctx);
2689231079cSbluhm if (ssl == NULL)
2699231079cSbluhm err_ssl(1, "SSL_new");
2707ca394abSbluhm SSL_set_bio(ssl, cbio, cbio);
2719231079cSbluhm if ((error = SSL_accept(ssl)) <= 0)
2729231079cSbluhm err_ssl(1, "SSL_accept %d", error);
2737ca394abSbluhm printf("session %d: %s\n", sessionreuse,
2747ca394abSbluhm SSL_session_reused(ssl) ? "reuse" : "new");
2757ca394abSbluhm if (fflush(stdout) != 0)
2767ca394abSbluhm err(1, "fflush stdout");
2777ca394abSbluhm
2789231079cSbluhm
2799231079cSbluhm /* print session statistics */
2809231079cSbluhm session = SSL_get_session(ssl);
2819231079cSbluhm if (session == NULL)
2829231079cSbluhm err_ssl(1, "SSL_get_session");
2839231079cSbluhm if (SSL_SESSION_print_fp(stdout, session) <= 0)
2849231079cSbluhm err_ssl(1, "SSL_SESSION_print_fp");
2859231079cSbluhm
2867ca394abSbluhm /* write server greeting and read client hello over TLS */
2879231079cSbluhm strlcpy(buf, "greeting\n", sizeof(buf));
2889231079cSbluhm printf(">>> %s", buf);
2899231079cSbluhm if (fflush(stdout) != 0)
2909231079cSbluhm err(1, "fflush stdout");
2919231079cSbluhm if ((error = SSL_write(ssl, buf, 9)) <= 0)
2929231079cSbluhm err_ssl(1, "SSL_write %d", error);
2939231079cSbluhm if (error != 9)
2949231079cSbluhm errx(1, "write not 9 bytes greeting: %d", error);
2959231079cSbluhm if ((error = SSL_read(ssl, buf, 6)) <= 0)
2969231079cSbluhm err_ssl(1, "SSL_read %d", error);
2979231079cSbluhm if (error != 6)
2989231079cSbluhm errx(1, "read not 6 bytes hello: %d", error);
2999231079cSbluhm buf[6] = '\0';
3009231079cSbluhm printf("<<< %s", buf);
3019231079cSbluhm if (fflush(stdout) != 0)
3029231079cSbluhm err(1, "fflush stdout");
3039231079cSbluhm
3049231079cSbluhm /* shutdown connection */
3059231079cSbluhm if ((error = SSL_shutdown(ssl)) < 0)
3069231079cSbluhm err_ssl(1, "SSL_shutdown unidirectional %d", error);
3079231079cSbluhm if (error <= 0) {
3089231079cSbluhm if ((error = SSL_shutdown(ssl)) <= 0)
3097ca394abSbluhm err_ssl(1, "SSL_shutdown bidirectional %d",
3107ca394abSbluhm error);
3119231079cSbluhm }
3129231079cSbluhm
3139231079cSbluhm SSL_free(ssl);
3147ca394abSbluhm } while (sessionreuse--);
3157ca394abSbluhm
3169231079cSbluhm SSL_CTX_free(ctx);
3179231079cSbluhm
3189231079cSbluhm printf("success\n");
3199231079cSbluhm
3209231079cSbluhm return 0;
3219231079cSbluhm }
322