blob: 76b3c8ce8fb51383173df63b190df32299bf97c8 [file] [log] [blame]
Radek Krejci5da708a2015-09-01 17:33:23 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server_tls.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 TLS server session manipulation functions
Radek Krejci5da708a2015-09-01 17:33:23 +02005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci5da708a2015-09-01 17:33:23 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5da708a2015-09-01 17:33:23 +020014 */
15
Michal Vaskoc14e3c82016-01-11 16:14:30 +010016#define _GNU_SOURCE
17
Michal Vaskoc14e3c82016-01-11 16:14:30 +010018#include <poll.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <string.h>
Michal Vaskof0537d82016-01-29 14:42:38 +010020#include <unistd.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010021
Michal Vaskoc14e3c82016-01-11 16:14:30 +010022#include <openssl/err.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <openssl/evp.h>
24#include <openssl/ssl.h>
Michal Vaskoe2713da2016-08-22 16:06:40 +020025#include <openssl/x509.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020026#include <openssl/x509v3.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010027
Michal Vasko316c9632020-04-15 11:06:57 +020028#include "compat.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010030#include "session_server.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010031#include "session_server_ch.h"
Radek Krejci5da708a2015-09-01 17:33:23 +020032
Rosen Penev4f552d62019-06-26 16:10:43 -070033#if OPENSSL_VERSION_NUMBER < 0x10100000L
34#define X509_STORE_CTX_get_by_subject X509_STORE_get_by_subject
35#endif
36
Michal Vasko3031aae2016-01-27 16:07:18 +010037struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010038pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER;
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039extern struct nc_server_opts server_opts;
Michal Vaskoc61c4492016-01-25 11:13:34 +010040
Michal Vasko5c2f7952016-01-22 13:16:31 +010041static pthread_key_t verify_key;
42static pthread_once_t verify_once = PTHREAD_ONCE_INIT;
43
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044static char *
Michal Vasko18aeb5d2017-02-17 09:23:56 +010045asn1time_to_str(const ASN1_TIME *t)
Michal Vasko086311b2016-01-08 09:53:11 +010046{
Michal Vaskoc14e3c82016-01-11 16:14:30 +010047 char *cp;
48 BIO *bio;
49 int n;
Radek Krejci5da708a2015-09-01 17:33:23 +020050
Michal Vaskoc14e3c82016-01-11 16:14:30 +010051 if (!t) {
52 return NULL;
53 }
54 bio = BIO_new(BIO_s_mem());
55 if (!bio) {
56 return NULL;
57 }
58 ASN1_TIME_print(bio, t);
59 n = BIO_pending(bio);
60 cp = malloc(n + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +010061 if (!cp) {
62 ERRMEM;
63 BIO_free(bio);
64 return NULL;
65 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010066 n = BIO_read(bio, cp, n);
67 if (n < 0) {
68 BIO_free(bio);
69 free(cp);
70 return NULL;
71 }
72 cp[n] = '\0';
73 BIO_free(bio);
74 return cp;
75}
76
77static void
78digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str)
79{
80 unsigned int i;
81
82 *str = malloc(dig_len * 3);
Michal Vasko4eb3c312016-03-01 14:09:37 +010083 if (!*str) {
84 ERRMEM;
85 return;
86 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010087 for (i = 0; i < dig_len - 1; ++i) {
88 sprintf((*str) + (i * 3), "%02x:", digest[i]);
89 }
90 sprintf((*str) + (i * 3), "%02x", digest[i]);
91}
92
93/* return NULL - SSL error can be retrieved */
94static X509 *
95base64der_to_cert(const char *in)
96{
97 X509 *out;
98 char *buf;
99 BIO *bio;
100
101 if (in == NULL) {
102 return NULL;
103 }
104
105 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
106 return NULL;
107 }
108 bio = BIO_new_mem_buf(buf, strlen(buf));
109 if (!bio) {
110 free(buf);
111 return NULL;
112 }
113
114 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
115 if (!out) {
116 free(buf);
117 BIO_free(bio);
118 return NULL;
119 }
120
121 free(buf);
122 BIO_free(bio);
123 return out;
124}
125
126/* return NULL - either errno or SSL error */
127static X509 *
128pem_to_cert(const char *path)
129{
130 FILE *fp;
131 X509 *out;
132
133 fp = fopen(path, "r");
134 if (!fp) {
135 return NULL;
136 }
137
138 out = PEM_read_X509(fp, NULL, NULL, NULL);
139 fclose(fp);
140 return out;
141}
142
Michal Vasko6e08cb72017-02-02 11:15:29 +0100143static EVP_PKEY *
Michal Vaskoddce1212019-05-24 09:58:49 +0200144base64der_to_privatekey(const char *in, const char *key_str)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100145{
146 EVP_PKEY *out;
147 char *buf;
148 BIO *bio;
149
150 if (in == NULL) {
151 return NULL;
152 }
153
Michal Vaskoddce1212019-05-24 09:58:49 +0200154 if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", key_str, " PRIVATE KEY-----\n", in, "\n-----END ",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200155 key_str, " PRIVATE KEY-----") == -1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100156 return NULL;
157 }
158 bio = BIO_new_mem_buf(buf, strlen(buf));
159 if (!bio) {
160 free(buf);
161 return NULL;
162 }
163
164 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
165 if (!out) {
166 free(buf);
167 BIO_free(bio);
168 return NULL;
169 }
170
171 free(buf);
172 BIO_free(bio);
173 return out;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100174}
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100175
176static int
177cert_pubkey_match(X509 *cert1, X509 *cert2)
178{
179 ASN1_BIT_STRING *bitstr1, *bitstr2;
180
181 bitstr1 = X509_get0_pubkey_bitstr(cert1);
182 bitstr2 = X509_get0_pubkey_bitstr(cert2);
183
184 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
185 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
186 return 0;
187 }
188
189 return 1;
190}
191
192static int
193nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
194{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200195 STACK_OF(GENERAL_NAME) * san_names;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100196 GENERAL_NAME *san_name;
197 ASN1_OCTET_STRING *ip;
198 int i, san_count;
199 char *subject, *common_name;
200
Michal Vaskod9a78642022-02-24 16:25:21 +0100201 *username = NULL;
202
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100203 if (map_type == NC_TLS_CTN_COMMON_NAME) {
204 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
205 common_name = strstr(subject, "CN=");
206 if (!common_name) {
Michal Vasko05532772021-06-03 12:12:38 +0200207 WRN(NULL, "Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100208 free(subject);
209 return 1;
210 }
211 common_name += 3;
212 if (strchr(common_name, '/')) {
213 *strchr(common_name, '/') = '\0';
214 }
215 *username = strdup(common_name);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100216 if (!*username) {
217 ERRMEM;
Roytakbe391922023-07-27 19:59:24 +0200218 return -1;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100219 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100220 free(subject);
221 } else {
222 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
223 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
224 if (!san_names) {
Michal Vasko05532772021-06-03 12:12:38 +0200225 WRN(NULL, "Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100226 return 1;
227 }
228
229 san_count = sk_GENERAL_NAME_num(san_names);
230 for (i = 0; i < san_count; ++i) {
231 san_name = sk_GENERAL_NAME_value(san_names, i);
232
233 /* rfc822Name (email) */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200234 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) &&
235 (san_name->type == GEN_EMAIL)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100236#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100237 *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100238#else
239 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name));
240#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100241 if (!*username) {
242 ERRMEM;
Roytakbe391922023-07-27 19:59:24 +0200243 return -1;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100244 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100245 break;
246 }
247
248 /* dNSName */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200249 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) &&
250 (san_name->type == GEN_DNS)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100251#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100252 *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100253#else
254 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName));
255#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100256 if (!*username) {
257 ERRMEM;
Roytakbe391922023-07-27 19:59:24 +0200258 return -1;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100259 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100260 break;
261 }
262
263 /* iPAddress */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200264 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_IP_ADDRESS)) &&
265 (san_name->type == GEN_IPADD)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100266 ip = san_name->d.iPAddress;
267 if (ip->length == 4) {
268 if (asprintf(username, "%d.%d.%d.%d", ip->data[0], ip->data[1], ip->data[2], ip->data[3]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100269 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100270 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
271 return -1;
272 }
273 break;
274 } else if (ip->length == 16) {
275 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
276 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
277 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
278 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100279 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100280 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
281 return -1;
282 }
283 break;
284 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200285 WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100286 }
287 }
288 }
289 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
290
Vitaly Kuzina3b66322021-08-26 12:33:43 +0300291 if (i == san_count) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100292 switch (map_type) {
293 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200294 WRN(NULL, "Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100295 break;
296 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200297 WRN(NULL, "Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100298 break;
299 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vasko05532772021-06-03 12:12:38 +0200300 WRN(NULL, "Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100301 break;
302 case NC_TLS_CTN_SAN_ANY:
Michal Vasko05532772021-06-03 12:12:38 +0200303 WRN(NULL, "Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100304 break;
305 default:
306 break;
307 }
308 return 1;
309 }
310 }
311
312 return 0;
313}
314
315/* return: 0 - OK, 1 - no match, -1 - error */
316static int
Roytakbe391922023-07-27 19:59:24 +0200317nc_tls_cert_to_name(struct nc_session *session, struct nc_ctn *ctn_first, X509 *cert)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100318{
319 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
320 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100321 unsigned char *buf = malloc(64);
322 unsigned int buf_len = 64;
323 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100324 struct nc_ctn *ctn;
Roytakbe391922023-07-27 19:59:24 +0200325 NC_TLS_CTN_MAPTYPE map_type;
326 char *username = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100327
Michal Vasko4eb3c312016-03-01 14:09:37 +0100328 if (!buf) {
329 ERRMEM;
330 return -1;
331 }
332
Roytakbe391922023-07-27 19:59:24 +0200333 if (!session || !ctn_first || !cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100334 free(buf);
335 return -1;
336 }
337
Michal Vaskoc61c4492016-01-25 11:13:34 +0100338 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Roytakbe391922023-07-27 19:59:24 +0200339 /* reset map_type */
340 map_type = NC_TLS_CTN_UNKNOWN;
341
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100342 /* first make sure the entry is valid */
343 if (!ctn->fingerprint || !ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
Michal Vasko05532772021-06-03 12:12:38 +0200344 VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100345 continue;
346 }
347
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100348 /* MD5 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100349 if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100350 if (!digest_md5) {
351 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200352 ERR(NULL, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100353 ret = -1;
354 goto cleanup;
355 }
356 digest_to_str(buf, buf_len, &digest_md5);
357 }
358
Michal Vasko5e3f3392016-01-20 11:13:01 +0100359 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Roytakbe391922023-07-27 19:59:24 +0200360 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200361 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200362 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100363 }
364
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200365 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100366 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100367 if (!digest_sha1) {
368 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200369 ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100370 ret = -1;
371 goto cleanup;
372 }
373 digest_to_str(buf, buf_len, &digest_sha1);
374 }
375
Michal Vasko5e3f3392016-01-20 11:13:01 +0100376 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Roytakbe391922023-07-27 19:59:24 +0200377 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200378 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200379 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100380 }
381
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200382 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100383 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100384 if (!digest_sha224) {
385 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200386 ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100387 ret = -1;
388 goto cleanup;
389 }
390 digest_to_str(buf, buf_len, &digest_sha224);
391 }
392
Michal Vasko5e3f3392016-01-20 11:13:01 +0100393 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Roytakbe391922023-07-27 19:59:24 +0200394 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200395 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200396 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100397 }
398
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200399 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100400 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100401 if (!digest_sha256) {
402 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200403 ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100404 ret = -1;
405 goto cleanup;
406 }
407 digest_to_str(buf, buf_len, &digest_sha256);
408 }
409
Michal Vasko5e3f3392016-01-20 11:13:01 +0100410 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Roytakbe391922023-07-27 19:59:24 +0200411 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200412 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200413 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100414 }
415
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200416 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100417 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100418 if (!digest_sha384) {
419 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200420 ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100421 ret = -1;
422 goto cleanup;
423 }
424 digest_to_str(buf, buf_len, &digest_sha384);
425 }
426
Michal Vasko5e3f3392016-01-20 11:13:01 +0100427 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Roytakbe391922023-07-27 19:59:24 +0200428 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200429 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200430 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100431 }
432
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200433 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100434 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100435 if (!digest_sha512) {
436 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200437 ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100438 ret = -1;
439 goto cleanup;
440 }
441 digest_to_str(buf, buf_len, &digest_sha512);
442 }
443
Michal Vasko5e3f3392016-01-20 11:13:01 +0100444 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Roytakbe391922023-07-27 19:59:24 +0200445 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200446 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200447 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100448 }
449
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200450 /* unknown */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100451 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200452 WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Roytakbe391922023-07-27 19:59:24 +0200453 continue;
454 }
455
456 if (map_type != NC_TLS_CTN_UNKNOWN) {
457 /* found a fingerprint match */
458 if (map_type == NC_TLS_CTN_SPECIFIED) {
459 /* specified -> get username from the ctn entry */
460 session->username = strdup(ctn->name);
461 if (!session->username) {
462 ERRMEM;
463 ret = -1;
464 goto cleanup;
465 }
466 } else {
467 /* try to get the username from the cert with this ctn's map type */
468 ret = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &username);
469 if (ret == -1) {
470 /* fatal error */
471 goto cleanup;
472 } else if (ret) {
473 /* didn't get username, try next ctn entry */
474 continue;
475 }
476
477 /* success */
478 session->username = username;
479 }
480
481 /* matching fingerprint found and username obtained, success */
482 ret = 0;
483 goto cleanup;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100484 }
485 }
486
Michal Vasko5e3f3392016-01-20 11:13:01 +0100487 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100488 ret = 1;
489 }
490
491cleanup:
492 free(digest_md5);
493 free(digest_sha1);
494 free(digest_sha224);
495 free(digest_sha256);
496 free(digest_sha384);
497 free(digest_sha512);
498 free(buf);
499 return ret;
500}
501
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100502#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
503
504static int
505nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
506{
507 X509_STORE_CTX *store_ctx;
508 X509_OBJECT *obj;
509 X509_NAME *subject;
510 X509_NAME *issuer;
511 X509 *cert;
512 X509_CRL *crl;
513 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200514
515 STACK_OF(X509) * cert_stack;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100516 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200517 struct nc_session *session;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100518 struct nc_server_tls_opts *opts;
519 const ASN1_INTEGER *serial;
520 int i, n, rc, depth;
521 char *cp;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100522 const ASN1_TIME *last_update = NULL, *next_update = NULL;
523
524 /* get the thread session */
525 session = pthread_getspecific(verify_key);
526 if (!session) {
527 ERRINT;
528 return 0;
529 }
530
531 opts = session->data;
532
533 /* get the last certificate, that is the peer (client) certificate */
534 if (!session->opts.server.client_cert) {
535 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500536 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100537 X509_up_ref(session->opts.server.client_cert);
538 sk_X509_pop_free(cert_stack, X509_free);
539 }
540
541 /* standard certificate verification failed, so a trusted client cert must match to continue */
542 if (!preverify_ok) {
543 subject = X509_get_subject_name(session->opts.server.client_cert);
544 cert_stack = X509_STORE_CTX_get1_certs(x509_ctx, subject);
545 if (cert_stack) {
546 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
547 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
548 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
549 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200550 VRB(NULL, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200551 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100552 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
553 sk_X509_pop_free(cert_stack, X509_free);
554 return 1;
555 }
556 }
557 sk_X509_pop_free(cert_stack, X509_free);
558 }
559
Michal Vasko05532772021-06-03 12:12:38 +0200560 ERR(NULL, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100561 return 0;
562 }
563
564 /* print cert verify info */
565 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200566 VRB(NULL, "Cert verify: depth %d.", depth);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100567
568 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
569 subject = X509_get_subject_name(cert);
570 issuer = X509_get_issuer_name(cert);
571
572 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200573 VRB(NULL, "Cert verify: subject: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100574 OPENSSL_free(cp);
575 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200576 VRB(NULL, "Cert verify: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100577 OPENSSL_free(cp);
578
579 /* check for revocation if set */
580 if (opts->crl_store) {
581 /* try to retrieve a CRL corresponding to the _subject_ of
582 * the current certificate in order to verify it's integrity */
583 store_ctx = X509_STORE_CTX_new();
584 obj = X509_OBJECT_new();
585 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700586 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100587 X509_STORE_CTX_free(store_ctx);
588 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200589 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100590 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200591 VRB(NULL, "Cert verify CRL: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100592 OPENSSL_free(cp);
593
594 last_update = X509_CRL_get0_lastUpdate(crl);
595 next_update = X509_CRL_get0_nextUpdate(crl);
596 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200597 VRB(NULL, "Cert verify CRL: last update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100598 free(cp);
599 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200600 VRB(NULL, "Cert verify CRL: next update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100601 free(cp);
602
603 /* verify the signature on this CRL */
604 pubkey = X509_get_pubkey(cert);
605 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200606 ERR(NULL, "Cert verify CRL: invalid signature.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100607 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
608 X509_OBJECT_free(obj);
609 if (pubkey) {
610 EVP_PKEY_free(pubkey);
611 }
612 return 0;
613 }
614 if (pubkey) {
615 EVP_PKEY_free(pubkey);
616 }
617
618 /* check date of CRL to make sure it's not expired */
619 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200620 ERR(NULL, "Cert verify CRL: invalid nextUpdate field.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100621 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
622 X509_OBJECT_free(obj);
623 return 0;
624 }
625 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200626 ERR(NULL, "Cert verify CRL: expired - revoking all certificates.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100627 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
628 X509_OBJECT_free(obj);
629 return 0;
630 }
631 X509_OBJECT_free(obj);
632 }
633
634 /* try to retrieve a CRL corresponding to the _issuer_ of
635 * the current certificate in order to check for revocation */
636 store_ctx = X509_STORE_CTX_new();
637 obj = X509_OBJECT_new();
638 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700639 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100640 X509_STORE_CTX_free(store_ctx);
641 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200642 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100643 /* check if the current certificate is revoked by this CRL */
644 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
645 for (i = 0; i < n; i++) {
646 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
647 serial = X509_REVOKED_get0_serialNumber(revoked);
648 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
649 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200650 ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
651 serial, serial, cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100652 OPENSSL_free(cp);
653 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
654 X509_OBJECT_free(obj);
655 return 0;
656 }
657 }
658 X509_OBJECT_free(obj);
659 }
660 }
661
662 /* cert-to-name already successful */
663 if (session->username) {
664 return 1;
665 }
666
667 /* cert-to-name */
Roytakbe391922023-07-27 19:59:24 +0200668 rc = nc_tls_cert_to_name(session, opts->ctn, cert);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100669 if (rc) {
670 if (rc == -1) {
671 /* fatal error */
672 depth = 0;
673 }
674 /* rc == 1 is a normal CTN fail (no match found) */
675 goto fail;
676 }
677
Michal Vasko05532772021-06-03 12:12:38 +0200678 VRB(NULL, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100679
680 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200681 VRB(NULL, "Cert verify: user verify callback revoked authorization.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100682 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
683 return 0;
684 }
685
686 return 1;
687
688fail:
689 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200690 VRB(NULL, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100691 return 1;
692 }
693
Michal Vasko05532772021-06-03 12:12:38 +0200694 VRB(NULL, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100695 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
696 return 0;
697}
698
699#else
700
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100701static int
702nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
703{
704 X509_STORE_CTX store_ctx;
705 X509_OBJECT obj;
706 X509_NAME *subject;
707 X509_NAME *issuer;
708 X509 *cert;
709 X509_CRL *crl;
710 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200711
712 STACK_OF(X509) * cert_stack;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100713 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200714 struct nc_session *session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100715 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100716 long serial;
717 int i, n, rc, depth;
718 char *cp;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100719 ASN1_TIME *last_update = NULL, *next_update = NULL;
720
Michal Vasko6d292992016-01-18 09:42:38 +0100721 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100722 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100723 if (!session) {
724 ERRINT;
725 return 0;
726 }
727
Michal Vasko2cc4c682016-03-01 09:16:48 +0100728 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100729
730 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200731 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100732 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100733 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200734 X509_free(session->opts.server.client_cert);
735 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100736 }
737 sk_X509_pop_free(cert_stack, X509_free);
738 }
739
740 /* standard certificate verification failed, so a trusted client cert must match to continue */
741 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200742 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100743 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
744 if (cert_stack) {
745 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200746 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100747 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
748 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200749 VRB(session, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200750 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100751 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
752 sk_X509_pop_free(cert_stack, X509_free);
753 return 1;
754 }
755 }
756 sk_X509_pop_free(cert_stack, X509_free);
757 }
758
Michal Vasko05532772021-06-03 12:12:38 +0200759 ERR(session, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100760 return 0;
761 }
762
763 /* print cert verify info */
764 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200765 VRB(session, "Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100766
767 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
768 subject = X509_get_subject_name(cert);
769 issuer = X509_get_issuer_name(cert);
770
771 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200772 VRB(session, "Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100773 OPENSSL_free(cp);
774 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200775 VRB(session, "Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100776 OPENSSL_free(cp);
777
778 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100779 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100780 /* try to retrieve a CRL corresponding to the _subject_ of
781 * the current certificate in order to verify it's integrity */
782 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100783 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700784 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100785 X509_STORE_CTX_cleanup(&store_ctx);
786 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200787 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100788 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200789 VRB(session, "Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100790 OPENSSL_free(cp);
791
792 last_update = X509_CRL_get_lastUpdate(crl);
793 next_update = X509_CRL_get_nextUpdate(crl);
794 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200795 VRB(session, "Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100796 free(cp);
797 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200798 VRB(session, "Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100799 free(cp);
800
801 /* verify the signature on this CRL */
802 pubkey = X509_get_pubkey(cert);
803 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200804 ERR(session, "Cert verify CRL: invalid signature.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100805 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
806 X509_OBJECT_free_contents(&obj);
807 if (pubkey) {
808 EVP_PKEY_free(pubkey);
809 }
810 return 0;
811 }
812 if (pubkey) {
813 EVP_PKEY_free(pubkey);
814 }
815
816 /* check date of CRL to make sure it's not expired */
817 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200818 ERR(session, "Cert verify CRL: invalid nextUpdate field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100819 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
820 X509_OBJECT_free_contents(&obj);
821 return 0;
822 }
823 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200824 ERR(session, "Cert verify CRL: expired - revoking all certificates.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100825 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
826 X509_OBJECT_free_contents(&obj);
827 return 0;
828 }
829 X509_OBJECT_free_contents(&obj);
830 }
831
832 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100833 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100834 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100835 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700836 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100837 X509_STORE_CTX_cleanup(&store_ctx);
838 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200839 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100840 /* check if the current certificate is revoked by this CRL */
841 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
842 for (i = 0; i < n; i++) {
843 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
844 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
845 serial = ASN1_INTEGER_get(revoked->serialNumber);
846 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200847 ERR(session, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
848 serial, serial, cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100849 OPENSSL_free(cp);
850 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
851 X509_OBJECT_free_contents(&obj);
852 return 0;
853 }
854 }
855 X509_OBJECT_free_contents(&obj);
856 }
857 }
858
859 /* cert-to-name already successful */
860 if (session->username) {
861 return 1;
862 }
863
864 /* cert-to-name */
Roytakbe391922023-07-27 19:59:24 +0200865 rc = nc_tls_cert_to_name(session, opts->ctn, cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100866 if (rc) {
867 if (rc == -1) {
868 /* fatal error */
869 depth = 0;
870 }
871 /* rc == 1 is a normal CTN fail (no match found) */
872 goto fail;
873 }
874
Michal Vasko05532772021-06-03 12:12:38 +0200875 VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100876
877 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200878 VRB(session, "Cert verify: user verify callback revoked authorization.");
Michal Vasko7060bcf2016-11-28 14:48:11 +0100879 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
880 return 0;
881 }
882
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100883 return 1;
884
885fail:
886 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200887 VRB(session, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100888 return 1;
889 }
890
Michal Vasko05532772021-06-03 12:12:38 +0200891 VRB(session, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100892 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
893 return 0;
894}
895
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100896#endif
897
Michal Vasko3031aae2016-01-27 16:07:18 +0100898static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100899nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100900{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100901 if (!name) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100902 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100903 free(opts->server_cert);
Michal Vaskoa8748792016-11-22 14:34:26 +0100904 }
905 opts->server_cert = NULL;
906 return 0;
907 }
908
Michal Vaskoe2713da2016-08-22 16:06:40 +0200909 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100910 free(opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100911 }
Michal Vasko93224072021-11-09 12:14:28 +0100912 opts->server_cert = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100913
914 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100915}
916
917API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100918nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100919{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100920 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100921 struct nc_endpt *endpt;
922
Michal Vasko2e6defd2016-10-07 15:48:15 +0200923 if (!endpt_name) {
924 ERRARG("endpt_name");
925 return -1;
926 }
927
Michal Vasko51e514d2016-02-02 15:51:52 +0100928 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100929 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100930 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100931 return -1;
932 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100933 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100934 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +0100935 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100936
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100937 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100938}
939
940API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200941nc_server_tls_ch_client_endpt_set_server_cert(const char *client_name, const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100942{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100943 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200944 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200945 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200946
947 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200948 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
949 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200950 return -1;
951 }
952
Michal Vaskoadf30f02019-06-24 09:34:47 +0200953 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200954
955 /* UNLOCK */
956 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100957
958 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100959}
960
Michal Vasko4c1fb492017-01-30 14:31:07 +0100961API void
962nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200963 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
964 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100965{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100966 if (!cert_clb) {
967 ERRARG("cert_clb");
968 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100969 }
970
Michal Vasko4c1fb492017-01-30 14:31:07 +0100971 server_opts.server_cert_clb = cert_clb;
972 server_opts.server_cert_data = user_data;
973 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100974}
975
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500976API void
977nc_server_tls_set_server_cert_chain_clb(int (*cert_chain_clb)(const char *name, void *user_data, char ***cert_paths,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200978 int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data))
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500979{
980 if (!cert_chain_clb) {
981 ERRARG("cert_chain_clb");
982 return;
983 }
984
985 server_opts.server_cert_chain_clb = cert_chain_clb;
986 server_opts.server_cert_chain_data = user_data;
987 server_opts.server_cert_chain_data_free = free_user_data;
988}
989
Michal Vaskoc61c4492016-01-25 11:13:34 +0100990static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100991nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100992{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100993 if (!name) {
994 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100995 return -1;
996 }
997
Michal Vasko4c1fb492017-01-30 14:31:07 +0100998 ++opts->trusted_cert_list_count;
Michal Vasko77367452021-02-16 16:32:18 +0100999 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists,
1000 opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001001 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001002 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001003 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001004 }
Michal Vasko93224072021-11-09 12:14:28 +01001005 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006
1007 return 0;
1008}
1009
1010API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001011nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001013 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 struct nc_endpt *endpt;
1015
Michal Vasko2e6defd2016-10-07 15:48:15 +02001016 if (!endpt_name) {
1017 ERRARG("endpt_name");
1018 return -1;
1019 }
1020
Michal Vasko51e514d2016-02-02 15:51:52 +01001021 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001022 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001023 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001024 return -1;
1025 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001026 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001027 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001028 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001029
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001030 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001031}
1032
1033API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001034nc_server_tls_ch_client_endpt_add_trusted_cert_list(const char *client_name, const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001035{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001036 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001037 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001038 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001039
1040 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001041 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1042 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001043 return -1;
1044 }
1045
Michal Vaskoadf30f02019-06-24 09:34:47 +02001046 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001047
1048 /* UNLOCK */
1049 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001050
1051 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001052}
1053
Michal Vasko4c1fb492017-01-30 14:31:07 +01001054API void
1055nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001056 int *cert_path_count, char ***cert_data, int *cert_data_count),
1057 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001058{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001059 if (!cert_list_clb) {
1060 ERRARG("cert_list_clb");
1061 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001062 }
1063
Michal Vasko4c1fb492017-01-30 14:31:07 +01001064 server_opts.trusted_cert_list_clb = cert_list_clb;
1065 server_opts.trusted_cert_list_data = user_data;
1066 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001067}
1068
1069static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001070nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001071{
1072 uint16_t i;
1073
1074 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001075 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001076 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001077 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001078 free(opts->trusted_cert_lists);
1079 opts->trusted_cert_lists = NULL;
1080 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001081 return 0;
1082 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001083 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1084 if (!strcmp(opts->trusted_cert_lists[i], name)) {
Michal Vasko93224072021-11-09 12:14:28 +01001085 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001086
Michal Vasko4c1fb492017-01-30 14:31:07 +01001087 --opts->trusted_cert_list_count;
1088 if (i < opts->trusted_cert_list_count - 1) {
1089 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1090 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001091 }
1092 return 0;
1093 }
1094 }
1095 }
1096
Michal Vasko05532772021-06-03 12:12:38 +02001097 ERR(NULL, "Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001098 return -1;
1099}
1100
1101API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001102nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001103{
1104 int ret;
1105 struct nc_endpt *endpt;
1106
Michal Vasko2e6defd2016-10-07 15:48:15 +02001107 if (!endpt_name) {
1108 ERRARG("endpt_name");
1109 return -1;
1110 }
1111
Michal Vaskoe2713da2016-08-22 16:06:40 +02001112 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001113 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001114 if (!endpt) {
1115 return -1;
1116 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001117 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001118 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001119 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001120
1121 return ret;
1122}
1123
1124API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001125nc_server_tls_ch_client_endpt_del_trusted_cert_list(const char *client_name, const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001126{
1127 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001128 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001129 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001130
1131 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001132 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1133 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001134 return -1;
1135 }
1136
Michal Vaskoadf30f02019-06-24 09:34:47 +02001137 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001138
1139 /* UNLOCK */
1140 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001141
1142 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001143}
1144
1145static int
Michal Vasko96830e32016-02-01 10:54:18 +01001146nc_server_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001147{
Michal Vasko96830e32016-02-01 10:54:18 +01001148 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001149 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001150 return -1;
1151 }
1152
Michal Vasko96830e32016-02-01 10:54:18 +01001153 if (ca_file) {
Michal Vasko93224072021-11-09 12:14:28 +01001154 free(opts->trusted_ca_file);
1155 opts->trusted_ca_file = strdup(ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001156 }
1157
Michal Vasko96830e32016-02-01 10:54:18 +01001158 if (ca_dir) {
Michal Vasko93224072021-11-09 12:14:28 +01001159 free(opts->trusted_ca_dir);
1160 opts->trusted_ca_dir = strdup(ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001161 }
1162
1163 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001164}
1165
Michal Vaskoc61c4492016-01-25 11:13:34 +01001166API int
Michal Vasko96830e32016-02-01 10:54:18 +01001167nc_server_tls_endpt_set_trusted_ca_paths(const char *endpt_name, const char *ca_file, const char *ca_dir)
Michal Vasko086311b2016-01-08 09:53:11 +01001168{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001169 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001170 struct nc_endpt *endpt;
1171
Michal Vasko2e6defd2016-10-07 15:48:15 +02001172 if (!endpt_name) {
1173 ERRARG("endpt_name");
1174 return -1;
1175 }
1176
Michal Vasko51e514d2016-02-02 15:51:52 +01001177 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001178 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001179 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001180 return -1;
1181 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001182 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001183 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001184 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001185
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001186 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001187}
1188
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001189API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001190nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file,
1191 const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001192{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001193 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001194 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001195 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001196
1197 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001198 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1199 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001200 return -1;
1201 }
1202
Michal Vaskoadf30f02019-06-24 09:34:47 +02001203 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001204
1205 /* UNLOCK */
1206 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001207
1208 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001209}
1210
Michal Vaskoc61c4492016-01-25 11:13:34 +01001211static int
Michal Vasko96830e32016-02-01 10:54:18 +01001212nc_server_tls_set_crl_paths(const char *crl_file, const char *crl_dir, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001213{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214 X509_LOOKUP *lookup;
1215
Michal Vasko96830e32016-02-01 10:54:18 +01001216 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001217 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001218 return -1;
1219 }
1220
Michal Vaskoc61c4492016-01-25 11:13:34 +01001221 if (!opts->crl_store) {
1222 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001223 }
1224
Michal Vasko96830e32016-02-01 10:54:18 +01001225 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001226 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001227 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001228 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001229 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001230 }
1231
Michal Vasko96830e32016-02-01 10:54:18 +01001232 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001233 ERR(NULL, "Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001234 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001235 }
1236 }
1237
Michal Vasko96830e32016-02-01 10:54:18 +01001238 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001239 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001240 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001241 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001242 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001243 }
1244
Michal Vasko96830e32016-02-01 10:54:18 +01001245 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001246 ERR(NULL, "Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001247 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001248 }
1249 }
1250
1251 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001252
1253fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001254 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001255}
1256
Michal Vaskoc61c4492016-01-25 11:13:34 +01001257API int
Michal Vasko96830e32016-02-01 10:54:18 +01001258nc_server_tls_endpt_set_crl_paths(const char *endpt_name, const char *crl_file, const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001259{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001260 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001261 struct nc_endpt *endpt;
1262
Michal Vasko2e6defd2016-10-07 15:48:15 +02001263 if (!endpt_name) {
1264 ERRARG("endpt_name");
1265 return -1;
1266 }
1267
Michal Vasko51e514d2016-02-02 15:51:52 +01001268 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001269 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001270 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001271 return -1;
1272 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001273 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001274 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001275 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001276
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001277 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001278}
1279
1280API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001281nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file,
1282 const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001283{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001284 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001285 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001286 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001287
1288 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001289 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1290 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001291 return -1;
1292 }
1293
Michal Vaskoadf30f02019-06-24 09:34:47 +02001294 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001295
1296 /* UNLOCK */
1297 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001298
1299 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001300}
1301
1302static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001303nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001304{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001305 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001306 return;
1307 }
1308
1309 X509_STORE_free(opts->crl_store);
1310 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001311}
1312
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001313API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001314nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001315{
Michal Vasko3031aae2016-01-27 16:07:18 +01001316 struct nc_endpt *endpt;
1317
Michal Vasko2e6defd2016-10-07 15:48:15 +02001318 if (!endpt_name) {
1319 ERRARG("endpt_name");
1320 return;
1321 }
1322
Michal Vasko51e514d2016-02-02 15:51:52 +01001323 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001324 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001325 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001326 return;
1327 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001328 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001329 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001330 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001331}
1332
Michal Vaskoc61c4492016-01-25 11:13:34 +01001333API void
Michal Vaskoadf30f02019-06-24 09:34:47 +02001334nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001336 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001337 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001338
1339 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001340 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1341 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001342 return;
1343 }
1344
Michal Vaskoadf30f02019-06-24 09:34:47 +02001345 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001346
1347 /* UNLOCK */
1348 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001349}
1350
1351static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001352nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001353 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001354{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001355 struct nc_ctn *ctn, *new;
1356
Michal Vaskoc61c4492016-01-25 11:13:34 +01001357 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001358 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001359 opts->ctn = new = calloc(1, sizeof *new);
1360 if (!new) {
1361 ERRMEM;
1362 return -1;
1363 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001364 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001365 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001366 new = calloc(1, sizeof *new);
1367 if (!new) {
1368 ERRMEM;
1369 return -1;
1370 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001371 new->next = opts->ctn;
1372 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001373 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001374 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {}
Michal Vasko276f9d92017-02-02 11:15:55 +01001375 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001376 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001377 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001378 } else {
1379 /* insert after ctn */
1380 new = calloc(1, sizeof *new);
1381 if (!new) {
1382 ERRMEM;
1383 return -1;
1384 }
1385 new->next = ctn->next;
1386 ctn->next = new;
1387 }
1388 }
1389
1390 new->id = id;
1391 if (fingerprint) {
Michal Vasko93224072021-11-09 12:14:28 +01001392 free(new->fingerprint);
1393 new->fingerprint = strdup(fingerprint);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001394 }
1395 if (map_type) {
1396 new->map_type = map_type;
1397 }
1398 if (name) {
Michal Vasko93224072021-11-09 12:14:28 +01001399 free(new->name);
1400 new->name = strdup(name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001401 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001402
1403 return 0;
1404}
1405
1406API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001407nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001408 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001409{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001410 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001411 struct nc_endpt *endpt;
1412
Michal Vasko2e6defd2016-10-07 15:48:15 +02001413 if (!endpt_name) {
1414 ERRARG("endpt_name");
1415 return -1;
1416 }
1417
Michal Vasko51e514d2016-02-02 15:51:52 +01001418 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001419 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001420 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001421 return -1;
1422 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001423 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001424 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001425 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001426
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001427 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001428}
1429
1430API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001431nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id,
1432 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001433{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001434 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001435 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001436 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001437
1438 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001439 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1440 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001441 return -1;
1442 }
1443
Michal Vaskoadf30f02019-06-24 09:34:47 +02001444 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001445
1446 /* UNLOCK */
1447 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001448
1449 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001450}
1451
1452static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001453nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001454 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001455{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001456 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001457 int ret = -1;
1458
Michal Vasko1a38c862016-01-15 15:50:07 +01001459 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001460 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001461 while (ctn) {
Michal Vasko93224072021-11-09 12:14:28 +01001462 free(ctn->fingerprint);
1463 free(ctn->name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001464
1465 next = ctn->next;
1466 free(ctn);
1467 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001468
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001469 ret = 0;
1470 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001471 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001472 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001473 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001474 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001475 while (ctn) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001476 if (((id < 0) || (ctn->id == id)) &&
1477 (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) &&
1478 (!map_type || (ctn->map_type == map_type)) &&
1479 (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko93224072021-11-09 12:14:28 +01001480 free(ctn->fingerprint);
1481 free(ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001482
Michal Vasko5e3f3392016-01-20 11:13:01 +01001483 if (prev) {
1484 prev->next = ctn->next;
1485 next = ctn->next;
1486 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001487 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001488 next = ctn->next;
1489 }
1490 free(ctn);
1491 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001492
1493 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001494 } else {
1495 prev = ctn;
1496 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001497 }
1498 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001499 }
1500
1501 return ret;
1502}
1503
Michal Vaskoc61c4492016-01-25 11:13:34 +01001504API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001505nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001506 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001507{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001508 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001509 struct nc_endpt *endpt;
1510
Michal Vasko2e6defd2016-10-07 15:48:15 +02001511 if (!endpt_name) {
1512 ERRARG("endpt_name");
1513 return -1;
1514 }
1515
Michal Vasko51e514d2016-02-02 15:51:52 +01001516 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001517 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001518 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001519 return -1;
1520 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001521 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001522 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001523 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001524
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001525 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001526}
1527
1528API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001529nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id,
1530 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001531{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001532 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001533 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001534 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001535
1536 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001537 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1538 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001539 return -1;
1540 }
1541
Michal Vaskoadf30f02019-06-24 09:34:47 +02001542 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001543
1544 /* UNLOCK */
1545 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001546
1547 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001548}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001549
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001550static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001551nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001552 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001553{
1554 struct nc_ctn *ctn;
1555 int ret = -1;
1556
1557 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1558 if (id && *id && (*id != ctn->id)) {
1559 continue;
1560 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001561 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001562 continue;
1563 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001564 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001565 continue;
1566 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001567 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001568 continue;
1569 }
1570
1571 /* first match, good enough */
1572 if (id && !(*id)) {
1573 *id = ctn->id;
1574 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001575 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001576 *fingerprint = strdup(ctn->fingerprint);
1577 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001578 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001579 *map_type = ctn->map_type;
1580 }
Adam Richterd44680e2019-06-15 13:16:16 -07001581 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001582 *name = strdup(ctn->name);
1583 }
1584
1585 ret = 0;
1586 break;
1587 }
1588
1589 return ret;
1590}
1591
1592API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001593nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
Michal Vaskoadf30f02019-06-24 09:34:47 +02001594 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001595{
1596 int ret;
1597 struct nc_endpt *endpt;
1598
1599 if (!endpt_name) {
1600 ERRARG("endpt_name");
1601 return -1;
1602 }
1603
1604 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001605 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001606 if (!endpt) {
1607 return -1;
1608 }
1609 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1610 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001611 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001612
1613 return ret;
1614}
1615
1616API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001617nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
1618 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001619{
1620 int ret;
1621 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001622 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001623
1624 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001625 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1626 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001627 return -1;
1628 }
1629
Michal Vaskoadf30f02019-06-24 09:34:47 +02001630 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001631
1632 /* UNLOCK */
1633 nc_server_ch_client_unlock(client);
1634
1635 return ret;
1636}
1637
Michal Vasko709598e2016-11-28 14:48:32 +01001638API const X509 *
1639nc_session_get_client_cert(const struct nc_session *session)
1640{
1641 if (!session || (session->side != NC_SERVER)) {
1642 ERRARG("session");
1643 return NULL;
1644 }
1645
1646 return session->opts.server.client_cert;
1647}
1648
Michal Vasko7060bcf2016-11-28 14:48:11 +01001649API void
1650nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1651{
1652 server_opts.user_verify_clb = verify_clb;
1653}
1654
Michal Vasko3031aae2016-01-27 16:07:18 +01001655void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001656nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001657{
Michal Vasko93224072021-11-09 12:14:28 +01001658 free(opts->server_cert);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001659 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vasko93224072021-11-09 12:14:28 +01001660 free(opts->trusted_ca_file);
1661 free(opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001662 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001663 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001664}
Michal Vasko9e036d52016-01-08 10:49:26 +01001665
Michal Vasko6d292992016-01-18 09:42:38 +01001666static void
1667nc_tls_make_verify_key(void)
1668{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001669 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001670}
1671
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001672static X509 *
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001673tls_load_cert(const char *cert_path, const char *cert_data)
1674{
1675 X509 *cert;
1676
1677 if (cert_path) {
1678 cert = pem_to_cert(cert_path);
1679 } else {
1680 cert = base64der_to_cert(cert_data);
1681 }
1682
1683 if (!cert) {
1684 if (cert_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001685 ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001686 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001687 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001688 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001689 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001690 }
1691 }
1692 return cert;
1693}
1694
1695static int
1696nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1697{
1698 char **cert_paths = NULL, **cert_data = NULL;
1699 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1700 X509 *cert = NULL;
1701
1702 if (!server_opts.server_cert_chain_clb) {
1703 /* This is optional, so return OK */
1704 return 0;
1705 }
1706
1707 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001708 &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001709 ERR(NULL, "Server certificate chain callback failed.");
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001710 return -1;
1711 }
1712
1713 for (i = 0; i < cert_path_count; ++i) {
1714 cert = tls_load_cert(cert_paths[i], NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001715 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001716 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001717 ret = -1;
1718 goto cleanup;
1719 }
1720 }
1721
1722 for (i = 0; i < cert_data_count; ++i) {
1723 cert = tls_load_cert(NULL, cert_data[i]);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001724 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001725 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001726 ret = -1;
1727 goto cleanup;
1728 }
1729 }
1730cleanup:
1731 for (i = 0; i < cert_path_count; ++i) {
1732 free(cert_paths[i]);
1733 }
1734 free(cert_paths);
1735 for (i = 0; i < cert_data_count; ++i) {
1736 free(cert_data[i]);
1737 }
1738 free(cert_data);
1739 /* cert is owned by the SSL_CTX */
1740
1741 return ret;
1742}
1743
Michal Vasko4c1fb492017-01-30 14:31:07 +01001744static int
1745nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1746{
1747 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001748 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001749 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001750 X509 *cert = NULL;
1751 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001752
1753 if (!cert_name) {
Michal Vasko05532772021-06-03 12:12:38 +02001754 ERR(NULL, "Server certificate not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001755 return -1;
1756 } else if (!server_opts.server_cert_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001757 ERR(NULL, "Callback for retrieving the server certificate is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001758 return -1;
1759 }
1760
1761 if (server_opts.server_cert_clb(cert_name, server_opts.server_cert_data, &cert_path, &cert_data, &privkey_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001762 &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001763 ERR(NULL, "Server certificate callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001764 return -1;
1765 }
1766
1767 /* load the certificate */
1768 if (cert_path) {
1769 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001770 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001771 ret = -1;
1772 goto cleanup;
1773 }
1774 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001775 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001776 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001777 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001778 ret = -1;
1779 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001780 }
1781 }
1782
1783 /* load the private key */
1784 if (privkey_path) {
1785 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001786 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001787 ret = -1;
1788 goto cleanup;
1789 }
1790 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001791 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001792 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001793 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001794 ret = -1;
1795 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001796 }
1797 }
1798
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001799 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1800
Michal Vasko4c1fb492017-01-30 14:31:07 +01001801cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001802 X509_free(cert);
1803 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001804 free(cert_path);
1805 free(cert_data);
1806 free(privkey_path);
1807 free(privkey_data);
1808 return ret;
1809}
1810
1811static void
1812tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1813{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001814 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001815
Michal Vasko4c1fb492017-01-30 14:31:07 +01001816 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001817 return;
1818 }
1819
1820 /* add the trusted certificate */
1821 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001822 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001823 X509_free(cert);
1824 return;
1825 }
1826 X509_free(cert);
1827}
1828
1829static int
Michal Vasko93224072021-11-09 12:14:28 +01001830nc_tls_store_set_trusted_certs(X509_STORE *cert_store, char **trusted_cert_lists, uint16_t trusted_cert_list_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001831{
1832 uint16_t i;
1833 int j;
1834 char **cert_paths, **cert_data;
1835 int cert_path_count, cert_data_count;
1836
1837 if (!server_opts.trusted_cert_list_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001838 ERR(NULL, "Callback for retrieving trusted certificate lists is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001839 return -1;
1840 }
1841
1842 for (i = 0; i < trusted_cert_list_count; ++i) {
1843 cert_paths = cert_data = NULL;
1844 cert_path_count = cert_data_count = 0;
1845 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001846 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001847 ERR(NULL, "Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001848 return -1;
1849 }
1850
1851 for (j = 0; j < cert_path_count; ++j) {
1852 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1853 free(cert_paths[j]);
1854 }
1855 free(cert_paths);
1856
1857 for (j = 0; j < cert_data_count; ++j) {
1858 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1859 free(cert_data[j]);
1860 }
1861 free(cert_data);
1862 }
1863
1864 return 0;
1865}
1866
Michal Vasko9af52322022-07-25 14:39:30 +02001867static int
1868nc_server_tls_accept_check(int accept_ret, struct nc_session *session)
1869{
1870 int verify;
1871
1872 /* check certificate verification result */
1873 verify = SSL_get_verify_result(session->ti.tls);
1874 switch (verify) {
1875 case X509_V_OK:
1876 if (accept_ret == 1) {
1877 VRB(session, "Client certificate verified.");
1878 }
1879 break;
1880 default:
1881 ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify));
1882 }
1883
1884 if (accept_ret != 1) {
1885 switch (SSL_get_error(session->ti.tls, accept_ret)) {
1886 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001887 ERR(session, "SSL accept failed (%s).", strerror(errno));
Michal Vasko9af52322022-07-25 14:39:30 +02001888 break;
1889 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001890 ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +02001891 break;
1892 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001893 ERR(session, "SSL accept failed.");
Michal Vasko9af52322022-07-25 14:39:30 +02001894 break;
1895 }
1896 }
1897
1898 return accept_ret;
1899}
1900
Michal Vasko3031aae2016-01-27 16:07:18 +01001901int
Michal Vasko0190bc32016-03-02 15:47:49 +01001902nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001903{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001904 X509_STORE *cert_store;
1905 SSL_CTX *tls_ctx;
1906 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001907 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001908 int ret;
roman6ece9c52022-06-22 09:29:17 +02001909 struct timespec ts_timeout;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001910
Michal Vasko2cc4c682016-03-01 09:16:48 +01001911 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001912
Michal Vaskoe2713da2016-08-22 16:06:40 +02001913 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001914#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1915 tls_ctx = SSL_CTX_new(TLS_server_method());
1916#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001917 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001918#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001919 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001920 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001921 goto error;
1922 }
1923 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001924 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001925 goto error;
1926 }
1927
1928 /* X509_STORE, managed (freed) with the context */
1929 cert_store = X509_STORE_new();
1930 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1931
Michal Vasko4c1fb492017-01-30 14:31:07 +01001932 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1933 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001934 }
1935
1936 if (opts->trusted_ca_file) {
1937 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1938 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001939 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001940 goto error;
1941 }
1942
1943 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001944 ERR(session, "Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001945 goto error;
1946 }
1947 }
1948
1949 if (opts->trusted_ca_dir) {
1950 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1951 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001952 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001953 goto error;
1954 }
1955
1956 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001957 ERR(session, "Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001958 goto error;
1959 }
1960 }
1961
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001962 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001963 session->ti.tls = SSL_new(tls_ctx);
1964
Michal Vasko4c1fb492017-01-30 14:31:07 +01001965 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001966 SSL_CTX_free(tls_ctx);
1967 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001968
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001969 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001970 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001971 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001972 }
1973
1974 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001975 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001976 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1977
Michal Vasko6d292992016-01-18 09:42:38 +01001978 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001979 pthread_once(&verify_once, nc_tls_make_verify_key);
1980 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001981
Michal Vasko36c7be82017-02-22 13:37:59 +01001982 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001983 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001984 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001985 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1986 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001987 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +02001988 ERR(session, "SSL accept timeout.");
roman6ece9c52022-06-22 09:29:17 +02001989 return 0;
Michal Vasko0190bc32016-03-02 15:47:49 +01001990 }
1991 }
Michal Vasko9af52322022-07-25 14:39:30 +02001992 if (nc_server_tls_accept_check(ret, session) != 1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001993 return -1;
1994 }
1995
Michal Vasko1a38c862016-01-15 15:50:07 +01001996 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001997
1998error:
1999 if (sock > -1) {
2000 close(sock);
2001 }
2002 SSL_CTX_free(tls_ctx);
2003 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002004}