blob: 0c227ce751c4be4714c1170d7280a1825cd6709a [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
roman40672412023-05-04 11:10:22 +0200923 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200924
Michal Vasko51e514d2016-02-02 15:51:52 +0100925 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100926 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100927 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100928 return -1;
929 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100930 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100931 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +0100932 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100933
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100934 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100935}
936
937API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200938nc_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 +0100939{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100940 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200941 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200942 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200943
944 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200945 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
946 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200947 return -1;
948 }
949
Michal Vaskoadf30f02019-06-24 09:34:47 +0200950 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200951
952 /* UNLOCK */
953 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100954
955 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100956}
957
Michal Vasko4c1fb492017-01-30 14:31:07 +0100958API void
959nc_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 +0200960 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
961 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100962{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100963 if (!cert_clb) {
roman40672412023-05-04 11:10:22 +0200964 ERRARG(NULL, "cert_clb");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100965 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100966 }
967
Michal Vasko4c1fb492017-01-30 14:31:07 +0100968 server_opts.server_cert_clb = cert_clb;
969 server_opts.server_cert_data = user_data;
970 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100971}
972
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500973API void
974nc_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 +0200975 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 -0500976{
977 if (!cert_chain_clb) {
roman40672412023-05-04 11:10:22 +0200978 ERRARG(NULL, "cert_chain_clb");
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500979 return;
980 }
981
982 server_opts.server_cert_chain_clb = cert_chain_clb;
983 server_opts.server_cert_chain_data = user_data;
984 server_opts.server_cert_chain_data_free = free_user_data;
985}
986
Michal Vaskoc61c4492016-01-25 11:13:34 +0100987static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100988nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100989{
roman40672412023-05-04 11:10:22 +0200990 NC_CHECK_ARG_RET(NULL, name, -1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100991
Michal Vasko4c1fb492017-01-30 14:31:07 +0100992 ++opts->trusted_cert_list_count;
Michal Vasko77367452021-02-16 16:32:18 +0100993 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists,
994 opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100995 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200996 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200997 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100998 }
Michal Vasko93224072021-11-09 12:14:28 +0100999 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001000
1001 return 0;
1002}
1003
1004API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001005nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001007 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001008 struct nc_endpt *endpt;
1009
roman40672412023-05-04 11:10:22 +02001010 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001011
Michal Vasko51e514d2016-02-02 15:51:52 +01001012 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001013 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001015 return -1;
1016 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001017 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001018 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001019 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001020
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001021 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001022}
1023
1024API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001025nc_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 +01001026{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001027 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001028 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001029 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001030
1031 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001032 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1033 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001034 return -1;
1035 }
1036
Michal Vaskoadf30f02019-06-24 09:34:47 +02001037 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001038
1039 /* UNLOCK */
1040 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001041
1042 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001043}
1044
Michal Vasko4c1fb492017-01-30 14:31:07 +01001045API void
1046nc_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 +02001047 int *cert_path_count, char ***cert_data, int *cert_data_count),
1048 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001049{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001050 if (!cert_list_clb) {
roman40672412023-05-04 11:10:22 +02001051 ERRARG(NULL, "cert_list_clb");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001052 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001053 }
1054
Michal Vasko4c1fb492017-01-30 14:31:07 +01001055 server_opts.trusted_cert_list_clb = cert_list_clb;
1056 server_opts.trusted_cert_list_data = user_data;
1057 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001058}
1059
1060static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001061nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001062{
1063 uint16_t i;
1064
1065 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001066 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001067 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001068 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001069 free(opts->trusted_cert_lists);
1070 opts->trusted_cert_lists = NULL;
1071 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001072 return 0;
1073 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001074 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1075 if (!strcmp(opts->trusted_cert_lists[i], name)) {
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 --opts->trusted_cert_list_count;
1079 if (i < opts->trusted_cert_list_count - 1) {
1080 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1081 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001082 }
1083 return 0;
1084 }
1085 }
1086 }
1087
Michal Vasko05532772021-06-03 12:12:38 +02001088 ERR(NULL, "Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001089 return -1;
1090}
1091
1092API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001093nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001094{
1095 int ret;
1096 struct nc_endpt *endpt;
1097
roman40672412023-05-04 11:10:22 +02001098 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001099
Michal Vaskoe2713da2016-08-22 16:06:40 +02001100 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001101 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001102 if (!endpt) {
1103 return -1;
1104 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001105 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001106 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001107 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001108
1109 return ret;
1110}
1111
1112API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001113nc_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 +02001114{
1115 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001116 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001117 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001118
1119 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001120 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1121 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001122 return -1;
1123 }
1124
Michal Vaskoadf30f02019-06-24 09:34:47 +02001125 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001126
1127 /* UNLOCK */
1128 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001129
1130 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001131}
1132
1133static int
Michal Vasko96830e32016-02-01 10:54:18 +01001134nc_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 +01001135{
Michal Vasko96830e32016-02-01 10:54:18 +01001136 if (!ca_file && !ca_dir) {
roman40672412023-05-04 11:10:22 +02001137 ERRARG(NULL, "ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001138 return -1;
1139 }
1140
Michal Vasko96830e32016-02-01 10:54:18 +01001141 if (ca_file) {
Michal Vasko93224072021-11-09 12:14:28 +01001142 free(opts->trusted_ca_file);
1143 opts->trusted_ca_file = strdup(ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001144 }
1145
Michal Vasko96830e32016-02-01 10:54:18 +01001146 if (ca_dir) {
Michal Vasko93224072021-11-09 12:14:28 +01001147 free(opts->trusted_ca_dir);
1148 opts->trusted_ca_dir = strdup(ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001149 }
1150
1151 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001152}
1153
Michal Vaskoc61c4492016-01-25 11:13:34 +01001154API int
Michal Vasko96830e32016-02-01 10:54:18 +01001155nc_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 +01001156{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001157 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001158 struct nc_endpt *endpt;
1159
roman40672412023-05-04 11:10:22 +02001160 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001161
Michal Vasko51e514d2016-02-02 15:51:52 +01001162 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001163 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001164 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001165 return -1;
1166 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001167 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001168 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001169 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001170
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001171 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001172}
1173
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001174API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001175nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file,
1176 const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001177{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001178 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001179 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001180 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001181
1182 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001183 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1184 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001185 return -1;
1186 }
1187
Michal Vaskoadf30f02019-06-24 09:34:47 +02001188 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001189
1190 /* UNLOCK */
1191 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001192
1193 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001194}
1195
Michal Vaskoc61c4492016-01-25 11:13:34 +01001196static int
Michal Vasko96830e32016-02-01 10:54:18 +01001197nc_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 +01001198{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001199 X509_LOOKUP *lookup;
1200
Michal Vasko96830e32016-02-01 10:54:18 +01001201 if (!crl_file && !crl_dir) {
roman40672412023-05-04 11:10:22 +02001202 ERRARG(NULL, "crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001203 return -1;
1204 }
1205
Michal Vaskoc61c4492016-01-25 11:13:34 +01001206 if (!opts->crl_store) {
1207 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001208 }
1209
Michal Vasko96830e32016-02-01 10:54:18 +01001210 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001211 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001212 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001213 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001214 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001215 }
1216
Michal Vasko96830e32016-02-01 10:54:18 +01001217 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001218 ERR(NULL, "Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001219 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001220 }
1221 }
1222
Michal Vasko96830e32016-02-01 10:54:18 +01001223 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001224 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001225 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001226 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001227 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001228 }
1229
Michal Vasko96830e32016-02-01 10:54:18 +01001230 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001231 ERR(NULL, "Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001232 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001233 }
1234 }
1235
1236 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001237
1238fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001239 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001240}
1241
Michal Vaskoc61c4492016-01-25 11:13:34 +01001242API int
Michal Vasko96830e32016-02-01 10:54:18 +01001243nc_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 +01001244{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001245 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001246 struct nc_endpt *endpt;
1247
roman40672412023-05-04 11:10:22 +02001248 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001249
Michal Vasko51e514d2016-02-02 15:51:52 +01001250 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001251 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001252 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001253 return -1;
1254 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001255 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001256 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001257 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001258
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001259 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001260}
1261
1262API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001263nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file,
1264 const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001265{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001266 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001267 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001268 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001269
1270 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001271 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1272 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001273 return -1;
1274 }
1275
Michal Vaskoadf30f02019-06-24 09:34:47 +02001276 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001277
1278 /* UNLOCK */
1279 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001280
1281 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001282}
1283
1284static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001285nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001286{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001287 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001288 return;
1289 }
1290
1291 X509_STORE_free(opts->crl_store);
1292 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001293}
1294
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001295API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001296nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001297{
Michal Vasko3031aae2016-01-27 16:07:18 +01001298 struct nc_endpt *endpt;
1299
Michal Vasko2e6defd2016-10-07 15:48:15 +02001300 if (!endpt_name) {
roman40672412023-05-04 11:10:22 +02001301 ERRARG(NULL, "endpt_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001302 return;
1303 }
1304
Michal Vasko51e514d2016-02-02 15:51:52 +01001305 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001306 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001307 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001308 return;
1309 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001310 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001311 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001312 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001313}
1314
Michal Vaskoc61c4492016-01-25 11:13:34 +01001315API void
Michal Vaskoadf30f02019-06-24 09:34:47 +02001316nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001317{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001318 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001319 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001320
1321 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001322 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1323 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001324 return;
1325 }
1326
Michal Vaskoadf30f02019-06-24 09:34:47 +02001327 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001328
1329 /* UNLOCK */
1330 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001331}
1332
1333static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001334nc_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 +02001335 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001336{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001337 struct nc_ctn *ctn, *new;
1338
Michal Vaskoc61c4492016-01-25 11:13:34 +01001339 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001340 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001341 opts->ctn = new = calloc(1, sizeof *new);
1342 if (!new) {
1343 ERRMEM;
1344 return -1;
1345 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001346 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001347 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001348 new = calloc(1, sizeof *new);
1349 if (!new) {
1350 ERRMEM;
1351 return -1;
1352 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001353 new->next = opts->ctn;
1354 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001355 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001356 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {}
Michal Vasko276f9d92017-02-02 11:15:55 +01001357 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001358 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001359 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001360 } else {
1361 /* insert after ctn */
1362 new = calloc(1, sizeof *new);
1363 if (!new) {
1364 ERRMEM;
1365 return -1;
1366 }
1367 new->next = ctn->next;
1368 ctn->next = new;
1369 }
1370 }
1371
1372 new->id = id;
1373 if (fingerprint) {
Michal Vasko93224072021-11-09 12:14:28 +01001374 free(new->fingerprint);
1375 new->fingerprint = strdup(fingerprint);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001376 }
1377 if (map_type) {
1378 new->map_type = map_type;
1379 }
1380 if (name) {
Michal Vasko93224072021-11-09 12:14:28 +01001381 free(new->name);
1382 new->name = strdup(name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001383 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001384
1385 return 0;
1386}
1387
1388API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001389nc_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 +02001390 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001391{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001392 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001393 struct nc_endpt *endpt;
1394
roman40672412023-05-04 11:10:22 +02001395 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001396
Michal Vasko51e514d2016-02-02 15:51:52 +01001397 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001398 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001399 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001400 return -1;
1401 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001402 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001403 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001404 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001405
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001406 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001407}
1408
1409API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001410nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id,
1411 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001412{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001413 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001414 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001415 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001416
1417 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001418 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1419 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001420 return -1;
1421 }
1422
Michal Vaskoadf30f02019-06-24 09:34:47 +02001423 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001424
1425 /* UNLOCK */
1426 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001427
1428 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001429}
1430
1431static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001432nc_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 +02001433 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001434{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001435 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001436 int ret = -1;
1437
Michal Vasko1a38c862016-01-15 15:50:07 +01001438 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001439 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001440 while (ctn) {
Michal Vasko93224072021-11-09 12:14:28 +01001441 free(ctn->fingerprint);
1442 free(ctn->name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001443
1444 next = ctn->next;
1445 free(ctn);
1446 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001447
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001448 ret = 0;
1449 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001450 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001451 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001452 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001453 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001454 while (ctn) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001455 if (((id < 0) || (ctn->id == id)) &&
1456 (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) &&
1457 (!map_type || (ctn->map_type == map_type)) &&
1458 (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko93224072021-11-09 12:14:28 +01001459 free(ctn->fingerprint);
1460 free(ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001461
Michal Vasko5e3f3392016-01-20 11:13:01 +01001462 if (prev) {
1463 prev->next = ctn->next;
1464 next = ctn->next;
1465 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001466 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001467 next = ctn->next;
1468 }
1469 free(ctn);
1470 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001471
1472 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001473 } else {
1474 prev = ctn;
1475 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001476 }
1477 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001478 }
1479
1480 return ret;
1481}
1482
Michal Vaskoc61c4492016-01-25 11:13:34 +01001483API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001484nc_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 +02001485 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001486{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001487 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001488 struct nc_endpt *endpt;
1489
roman40672412023-05-04 11:10:22 +02001490 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001491
Michal Vasko51e514d2016-02-02 15:51:52 +01001492 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001493 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001494 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001495 return -1;
1496 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001497 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001498 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001499 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001500
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001501 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001502}
1503
1504API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001505nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id,
1506 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001507{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001508 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001509 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001510 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001511
1512 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001513 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1514 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001515 return -1;
1516 }
1517
Michal Vaskoadf30f02019-06-24 09:34:47 +02001518 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001519
1520 /* UNLOCK */
1521 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001522
1523 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001524}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001525
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001526static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001527nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001528 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001529{
1530 struct nc_ctn *ctn;
1531 int ret = -1;
1532
1533 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1534 if (id && *id && (*id != ctn->id)) {
1535 continue;
1536 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001537 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001538 continue;
1539 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001540 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001541 continue;
1542 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001543 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001544 continue;
1545 }
1546
1547 /* first match, good enough */
1548 if (id && !(*id)) {
1549 *id = ctn->id;
1550 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001551 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001552 *fingerprint = strdup(ctn->fingerprint);
1553 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001554 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001555 *map_type = ctn->map_type;
1556 }
Adam Richterd44680e2019-06-15 13:16:16 -07001557 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001558 *name = strdup(ctn->name);
1559 }
1560
1561 ret = 0;
1562 break;
1563 }
1564
1565 return ret;
1566}
1567
1568API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001569nc_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 +02001570 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001571{
1572 int ret;
1573 struct nc_endpt *endpt;
1574
roman40672412023-05-04 11:10:22 +02001575 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001576
1577 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001578 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001579 if (!endpt) {
1580 return -1;
1581 }
1582 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1583 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +01001584 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001585
1586 return ret;
1587}
1588
1589API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001590nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
1591 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001592{
1593 int ret;
1594 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001595 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001596
1597 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001598 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1599 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001600 return -1;
1601 }
1602
Michal Vaskoadf30f02019-06-24 09:34:47 +02001603 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001604
1605 /* UNLOCK */
1606 nc_server_ch_client_unlock(client);
1607
1608 return ret;
1609}
1610
Michal Vasko709598e2016-11-28 14:48:32 +01001611API const X509 *
1612nc_session_get_client_cert(const struct nc_session *session)
1613{
1614 if (!session || (session->side != NC_SERVER)) {
roman40672412023-05-04 11:10:22 +02001615 ERRARG(session, "session");
Michal Vasko709598e2016-11-28 14:48:32 +01001616 return NULL;
1617 }
1618
1619 return session->opts.server.client_cert;
1620}
1621
Michal Vasko7060bcf2016-11-28 14:48:11 +01001622API void
1623nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1624{
1625 server_opts.user_verify_clb = verify_clb;
1626}
1627
Michal Vasko3031aae2016-01-27 16:07:18 +01001628void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001629nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001630{
Michal Vasko93224072021-11-09 12:14:28 +01001631 free(opts->server_cert);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001632 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vasko93224072021-11-09 12:14:28 +01001633 free(opts->trusted_ca_file);
1634 free(opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001635 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001636 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001637}
Michal Vasko9e036d52016-01-08 10:49:26 +01001638
Michal Vasko6d292992016-01-18 09:42:38 +01001639static void
1640nc_tls_make_verify_key(void)
1641{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001642 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001643}
1644
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001645static X509 *
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001646tls_load_cert(const char *cert_path, const char *cert_data)
1647{
1648 X509 *cert;
1649
1650 if (cert_path) {
1651 cert = pem_to_cert(cert_path);
1652 } else {
1653 cert = base64der_to_cert(cert_data);
1654 }
1655
1656 if (!cert) {
1657 if (cert_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001658 ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001659 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001660 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001661 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001662 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001663 }
1664 }
1665 return cert;
1666}
1667
1668static int
1669nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1670{
1671 char **cert_paths = NULL, **cert_data = NULL;
1672 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1673 X509 *cert = NULL;
1674
1675 if (!server_opts.server_cert_chain_clb) {
1676 /* This is optional, so return OK */
1677 return 0;
1678 }
1679
1680 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001681 &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001682 ERR(NULL, "Server certificate chain callback failed.");
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001683 return -1;
1684 }
1685
1686 for (i = 0; i < cert_path_count; ++i) {
1687 cert = tls_load_cert(cert_paths[i], NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001688 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001689 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001690 ret = -1;
1691 goto cleanup;
1692 }
1693 }
1694
1695 for (i = 0; i < cert_data_count; ++i) {
1696 cert = tls_load_cert(NULL, cert_data[i]);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001697 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001698 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001699 ret = -1;
1700 goto cleanup;
1701 }
1702 }
1703cleanup:
1704 for (i = 0; i < cert_path_count; ++i) {
1705 free(cert_paths[i]);
1706 }
1707 free(cert_paths);
1708 for (i = 0; i < cert_data_count; ++i) {
1709 free(cert_data[i]);
1710 }
1711 free(cert_data);
1712 /* cert is owned by the SSL_CTX */
1713
1714 return ret;
1715}
1716
Michal Vasko4c1fb492017-01-30 14:31:07 +01001717static int
1718nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1719{
1720 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001721 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001722 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001723 X509 *cert = NULL;
1724 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001725
1726 if (!cert_name) {
Michal Vasko05532772021-06-03 12:12:38 +02001727 ERR(NULL, "Server certificate not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001728 return -1;
1729 } else if (!server_opts.server_cert_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001730 ERR(NULL, "Callback for retrieving the server certificate is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001731 return -1;
1732 }
1733
1734 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 +02001735 &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001736 ERR(NULL, "Server certificate callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001737 return -1;
1738 }
1739
1740 /* load the certificate */
1741 if (cert_path) {
1742 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001743 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001744 ret = -1;
1745 goto cleanup;
1746 }
1747 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001748 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001749 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001750 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001751 ret = -1;
1752 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001753 }
1754 }
1755
1756 /* load the private key */
1757 if (privkey_path) {
1758 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001759 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001760 ret = -1;
1761 goto cleanup;
1762 }
1763 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001764 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001765 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001766 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001767 ret = -1;
1768 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001769 }
1770 }
1771
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001772 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1773
Michal Vasko4c1fb492017-01-30 14:31:07 +01001774cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001775 X509_free(cert);
1776 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001777 free(cert_path);
1778 free(cert_data);
1779 free(privkey_path);
1780 free(privkey_data);
1781 return ret;
1782}
1783
1784static void
1785tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1786{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001787 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001788
Michal Vasko4c1fb492017-01-30 14:31:07 +01001789 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001790 return;
1791 }
1792
1793 /* add the trusted certificate */
1794 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001795 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001796 X509_free(cert);
1797 return;
1798 }
1799 X509_free(cert);
1800}
1801
1802static int
Michal Vasko93224072021-11-09 12:14:28 +01001803nc_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 +01001804{
1805 uint16_t i;
1806 int j;
1807 char **cert_paths, **cert_data;
1808 int cert_path_count, cert_data_count;
1809
1810 if (!server_opts.trusted_cert_list_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001811 ERR(NULL, "Callback for retrieving trusted certificate lists is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001812 return -1;
1813 }
1814
1815 for (i = 0; i < trusted_cert_list_count; ++i) {
1816 cert_paths = cert_data = NULL;
1817 cert_path_count = cert_data_count = 0;
1818 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001819 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001820 ERR(NULL, "Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001821 return -1;
1822 }
1823
1824 for (j = 0; j < cert_path_count; ++j) {
1825 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1826 free(cert_paths[j]);
1827 }
1828 free(cert_paths);
1829
1830 for (j = 0; j < cert_data_count; ++j) {
1831 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1832 free(cert_data[j]);
1833 }
1834 free(cert_data);
1835 }
1836
1837 return 0;
1838}
1839
Michal Vasko9af52322022-07-25 14:39:30 +02001840static int
1841nc_server_tls_accept_check(int accept_ret, struct nc_session *session)
1842{
1843 int verify;
1844
1845 /* check certificate verification result */
1846 verify = SSL_get_verify_result(session->ti.tls);
1847 switch (verify) {
1848 case X509_V_OK:
1849 if (accept_ret == 1) {
1850 VRB(session, "Client certificate verified.");
1851 }
1852 break;
1853 default:
1854 ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify));
1855 }
1856
1857 if (accept_ret != 1) {
1858 switch (SSL_get_error(session->ti.tls, accept_ret)) {
1859 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001860 ERR(session, "SSL accept failed (%s).", strerror(errno));
Michal Vasko9af52322022-07-25 14:39:30 +02001861 break;
1862 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001863 ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +02001864 break;
1865 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001866 ERR(session, "SSL accept failed.");
Michal Vasko9af52322022-07-25 14:39:30 +02001867 break;
1868 }
1869 }
1870
1871 return accept_ret;
1872}
1873
Michal Vasko3031aae2016-01-27 16:07:18 +01001874int
Michal Vasko0190bc32016-03-02 15:47:49 +01001875nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001876{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001877 X509_STORE *cert_store;
1878 SSL_CTX *tls_ctx;
1879 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001880 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001881 int ret;
roman6ece9c52022-06-22 09:29:17 +02001882 struct timespec ts_timeout;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001883
Michal Vasko2cc4c682016-03-01 09:16:48 +01001884 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001885
Michal Vaskoe2713da2016-08-22 16:06:40 +02001886 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001887#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1888 tls_ctx = SSL_CTX_new(TLS_server_method());
1889#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001890 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001891#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001892 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001893 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001894 goto error;
1895 }
1896 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 +01001897 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001898 goto error;
1899 }
1900
1901 /* X509_STORE, managed (freed) with the context */
1902 cert_store = X509_STORE_new();
1903 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1904
Michal Vasko4c1fb492017-01-30 14:31:07 +01001905 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1906 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001907 }
1908
1909 if (opts->trusted_ca_file) {
1910 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1911 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001912 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001913 goto error;
1914 }
1915
1916 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001917 ERR(session, "Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001918 goto error;
1919 }
1920 }
1921
1922 if (opts->trusted_ca_dir) {
1923 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1924 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001925 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001926 goto error;
1927 }
1928
1929 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001930 ERR(session, "Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001931 goto error;
1932 }
1933 }
1934
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001935 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001936 session->ti.tls = SSL_new(tls_ctx);
1937
Michal Vasko4c1fb492017-01-30 14:31:07 +01001938 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001939 SSL_CTX_free(tls_ctx);
1940 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001941
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001942 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001943 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001944 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001945 }
1946
1947 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001948 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001949 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1950
Michal Vasko6d292992016-01-18 09:42:38 +01001951 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001952 pthread_once(&verify_once, nc_tls_make_verify_key);
1953 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001954
Michal Vasko36c7be82017-02-22 13:37:59 +01001955 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001956 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001957 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001958 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1959 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001960 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +02001961 ERR(session, "SSL accept timeout.");
roman6ece9c52022-06-22 09:29:17 +02001962 return 0;
Michal Vasko0190bc32016-03-02 15:47:49 +01001963 }
1964 }
Michal Vasko9af52322022-07-25 14:39:30 +02001965 if (nc_server_tls_accept_check(ret, session) != 1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001966 return -1;
1967 }
1968
Michal Vasko1a38c862016-01-15 15:50:07 +01001969 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001970
1971error:
1972 if (sock > -1) {
1973 close(sock);
1974 }
1975 SSL_CTX_free(tls_ctx);
1976 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001977}