blob: 80513d99ee334e4a51ddd73520b30cdf9b70ceeb [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;
218 return 1;
219 }
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;
243 return 1;
244 }
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;
258 return 1;
259 }
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
Michal Vaskoc61c4492016-01-25 11:13:34 +0100317nc_tls_cert_to_name(struct nc_ctn *ctn_first, X509 *cert, NC_TLS_CTN_MAPTYPE *map_type, const char **name)
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;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100325
Michal Vasko4eb3c312016-03-01 14:09:37 +0100326 if (!buf) {
327 ERRMEM;
328 return -1;
329 }
330
Michal Vaskoc61c4492016-01-25 11:13:34 +0100331 if (!ctn_first || !cert || !map_type || !name) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100332 free(buf);
333 return -1;
334 }
335
Michal Vaskoc61c4492016-01-25 11:13:34 +0100336 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100337 /* first make sure the entry is valid */
338 if (!ctn->fingerprint || !ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
Michal Vasko05532772021-06-03 12:12:38 +0200339 VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100340 continue;
341 }
342
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100343 /* MD5 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100344 if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100345 if (!digest_md5) {
346 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200347 ERR(NULL, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100348 ret = -1;
349 goto cleanup;
350 }
351 digest_to_str(buf, buf_len, &digest_md5);
352 }
353
Michal Vasko5e3f3392016-01-20 11:13:01 +0100354 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100355 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200356 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100357 *map_type = ctn->map_type;
358 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
359 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100360 }
361 break;
362 }
363
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200364 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100365 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100366 if (!digest_sha1) {
367 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200368 ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100369 ret = -1;
370 goto cleanup;
371 }
372 digest_to_str(buf, buf_len, &digest_sha1);
373 }
374
Michal Vasko5e3f3392016-01-20 11:13:01 +0100375 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100376 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200377 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100378 *map_type = ctn->map_type;
379 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
380 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100381 }
382 break;
383 }
384
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200385 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100386 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100387 if (!digest_sha224) {
388 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200389 ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100390 ret = -1;
391 goto cleanup;
392 }
393 digest_to_str(buf, buf_len, &digest_sha224);
394 }
395
Michal Vasko5e3f3392016-01-20 11:13:01 +0100396 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100397 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200398 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100399 *map_type = ctn->map_type;
400 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
401 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100402 }
403 break;
404 }
405
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200406 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100407 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100408 if (!digest_sha256) {
409 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200410 ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100411 ret = -1;
412 goto cleanup;
413 }
414 digest_to_str(buf, buf_len, &digest_sha256);
415 }
416
Michal Vasko5e3f3392016-01-20 11:13:01 +0100417 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100418 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200419 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100420 *map_type = ctn->map_type;
421 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
422 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100423 }
424 break;
425 }
426
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200427 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100428 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100429 if (!digest_sha384) {
430 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200431 ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100432 ret = -1;
433 goto cleanup;
434 }
435 digest_to_str(buf, buf_len, &digest_sha384);
436 }
437
Michal Vasko5e3f3392016-01-20 11:13:01 +0100438 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100439 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200440 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100441 *map_type = ctn->map_type;
442 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
443 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100444 }
445 break;
446 }
447
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200448 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100449 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100450 if (!digest_sha512) {
451 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200452 ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100453 ret = -1;
454 goto cleanup;
455 }
456 digest_to_str(buf, buf_len, &digest_sha512);
457 }
458
Michal Vasko5e3f3392016-01-20 11:13:01 +0100459 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100460 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200461 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100462 *map_type = ctn->map_type;
463 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
464 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100465 }
466 break;
467 }
468
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200469 /* unknown */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100470 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200471 WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100472 }
473 }
474
Michal Vasko5e3f3392016-01-20 11:13:01 +0100475 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100476 ret = 1;
477 }
478
479cleanup:
480 free(digest_md5);
481 free(digest_sha1);
482 free(digest_sha224);
483 free(digest_sha256);
484 free(digest_sha384);
485 free(digest_sha512);
486 free(buf);
487 return ret;
488}
489
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100490#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
491
492static int
493nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
494{
495 X509_STORE_CTX *store_ctx;
496 X509_OBJECT *obj;
497 X509_NAME *subject;
498 X509_NAME *issuer;
499 X509 *cert;
500 X509_CRL *crl;
501 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200502
503 STACK_OF(X509) * cert_stack;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100504 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200505 struct nc_session *session;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100506 struct nc_server_tls_opts *opts;
507 const ASN1_INTEGER *serial;
508 int i, n, rc, depth;
509 char *cp;
510 const char *username = NULL;
511 NC_TLS_CTN_MAPTYPE map_type = 0;
512 const ASN1_TIME *last_update = NULL, *next_update = NULL;
513
514 /* get the thread session */
515 session = pthread_getspecific(verify_key);
516 if (!session) {
517 ERRINT;
518 return 0;
519 }
520
521 opts = session->data;
522
523 /* get the last certificate, that is the peer (client) certificate */
524 if (!session->opts.server.client_cert) {
525 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500526 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100527 X509_up_ref(session->opts.server.client_cert);
528 sk_X509_pop_free(cert_stack, X509_free);
529 }
530
531 /* standard certificate verification failed, so a trusted client cert must match to continue */
532 if (!preverify_ok) {
533 subject = X509_get_subject_name(session->opts.server.client_cert);
534 cert_stack = X509_STORE_CTX_get1_certs(x509_ctx, subject);
535 if (cert_stack) {
536 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
537 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
538 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
539 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200540 VRB(NULL, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200541 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100542 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
543 sk_X509_pop_free(cert_stack, X509_free);
544 return 1;
545 }
546 }
547 sk_X509_pop_free(cert_stack, X509_free);
548 }
549
Michal Vasko05532772021-06-03 12:12:38 +0200550 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 +0100551 return 0;
552 }
553
554 /* print cert verify info */
555 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200556 VRB(NULL, "Cert verify: depth %d.", depth);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100557
558 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
559 subject = X509_get_subject_name(cert);
560 issuer = X509_get_issuer_name(cert);
561
562 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200563 VRB(NULL, "Cert verify: subject: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100564 OPENSSL_free(cp);
565 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200566 VRB(NULL, "Cert verify: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100567 OPENSSL_free(cp);
568
569 /* check for revocation if set */
570 if (opts->crl_store) {
571 /* try to retrieve a CRL corresponding to the _subject_ of
572 * the current certificate in order to verify it's integrity */
573 store_ctx = X509_STORE_CTX_new();
574 obj = X509_OBJECT_new();
575 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700576 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100577 X509_STORE_CTX_free(store_ctx);
578 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200579 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100580 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200581 VRB(NULL, "Cert verify CRL: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100582 OPENSSL_free(cp);
583
584 last_update = X509_CRL_get0_lastUpdate(crl);
585 next_update = X509_CRL_get0_nextUpdate(crl);
586 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200587 VRB(NULL, "Cert verify CRL: last update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100588 free(cp);
589 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200590 VRB(NULL, "Cert verify CRL: next update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100591 free(cp);
592
593 /* verify the signature on this CRL */
594 pubkey = X509_get_pubkey(cert);
595 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200596 ERR(NULL, "Cert verify CRL: invalid signature.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100597 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
598 X509_OBJECT_free(obj);
599 if (pubkey) {
600 EVP_PKEY_free(pubkey);
601 }
602 return 0;
603 }
604 if (pubkey) {
605 EVP_PKEY_free(pubkey);
606 }
607
608 /* check date of CRL to make sure it's not expired */
609 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200610 ERR(NULL, "Cert verify CRL: invalid nextUpdate field.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100611 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
612 X509_OBJECT_free(obj);
613 return 0;
614 }
615 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200616 ERR(NULL, "Cert verify CRL: expired - revoking all certificates.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100617 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
618 X509_OBJECT_free(obj);
619 return 0;
620 }
621 X509_OBJECT_free(obj);
622 }
623
624 /* try to retrieve a CRL corresponding to the _issuer_ of
625 * the current certificate in order to check for revocation */
626 store_ctx = X509_STORE_CTX_new();
627 obj = X509_OBJECT_new();
628 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700629 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100630 X509_STORE_CTX_free(store_ctx);
631 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200632 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100633 /* check if the current certificate is revoked by this CRL */
634 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
635 for (i = 0; i < n; i++) {
636 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
637 serial = X509_REVOKED_get0_serialNumber(revoked);
638 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
639 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200640 ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
641 serial, serial, cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100642 OPENSSL_free(cp);
643 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
644 X509_OBJECT_free(obj);
645 return 0;
646 }
647 }
648 X509_OBJECT_free(obj);
649 }
650 }
651
652 /* cert-to-name already successful */
653 if (session->username) {
654 return 1;
655 }
656
657 /* cert-to-name */
658 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
659
660 if (rc) {
661 if (rc == -1) {
662 /* fatal error */
663 depth = 0;
664 }
665 /* rc == 1 is a normal CTN fail (no match found) */
666 goto fail;
667 }
668
669 /* cert-to-name match, now to extract the specific field from the peer cert */
670 if (map_type == NC_TLS_CTN_SPECIFIED) {
Michal Vasko93224072021-11-09 12:14:28 +0100671 session->username = strdup(username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100672 } else {
673 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
674 if (rc) {
675 if (rc == -1) {
676 depth = 0;
677 }
678 goto fail;
679 }
Michal Vasko93224072021-11-09 12:14:28 +0100680 session->username = cp;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100681 }
682
Michal Vasko05532772021-06-03 12:12:38 +0200683 VRB(NULL, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100684
685 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200686 VRB(NULL, "Cert verify: user verify callback revoked authorization.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100687 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
688 return 0;
689 }
690
691 return 1;
692
693fail:
694 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200695 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 +0100696 return 1;
697 }
698
Michal Vasko05532772021-06-03 12:12:38 +0200699 VRB(NULL, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100700 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
701 return 0;
702}
703
704#else
705
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100706static int
707nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
708{
709 X509_STORE_CTX store_ctx;
710 X509_OBJECT obj;
711 X509_NAME *subject;
712 X509_NAME *issuer;
713 X509 *cert;
714 X509_CRL *crl;
715 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200716
717 STACK_OF(X509) * cert_stack;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100718 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200719 struct nc_session *session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100720 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100721 long serial;
722 int i, n, rc, depth;
723 char *cp;
724 const char *username = NULL;
725 NC_TLS_CTN_MAPTYPE map_type = 0;
726 ASN1_TIME *last_update = NULL, *next_update = NULL;
727
Michal Vasko6d292992016-01-18 09:42:38 +0100728 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100729 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100730 if (!session) {
731 ERRINT;
732 return 0;
733 }
734
Michal Vasko2cc4c682016-03-01 09:16:48 +0100735 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100736
737 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200738 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100739 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100740 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200741 X509_free(session->opts.server.client_cert);
742 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100743 }
744 sk_X509_pop_free(cert_stack, X509_free);
745 }
746
747 /* standard certificate verification failed, so a trusted client cert must match to continue */
748 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200749 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100750 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
751 if (cert_stack) {
752 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200753 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100754 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
755 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200756 VRB(session, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200757 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100758 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
759 sk_X509_pop_free(cert_stack, X509_free);
760 return 1;
761 }
762 }
763 sk_X509_pop_free(cert_stack, X509_free);
764 }
765
Michal Vasko05532772021-06-03 12:12:38 +0200766 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 +0100767 return 0;
768 }
769
770 /* print cert verify info */
771 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200772 VRB(session, "Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100773
774 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
775 subject = X509_get_subject_name(cert);
776 issuer = X509_get_issuer_name(cert);
777
778 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200779 VRB(session, "Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100780 OPENSSL_free(cp);
781 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200782 VRB(session, "Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100783 OPENSSL_free(cp);
784
785 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100786 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100787 /* try to retrieve a CRL corresponding to the _subject_ of
788 * the current certificate in order to verify it's integrity */
789 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100790 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700791 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100792 X509_STORE_CTX_cleanup(&store_ctx);
793 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200794 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100795 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200796 VRB(session, "Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100797 OPENSSL_free(cp);
798
799 last_update = X509_CRL_get_lastUpdate(crl);
800 next_update = X509_CRL_get_nextUpdate(crl);
801 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200802 VRB(session, "Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100803 free(cp);
804 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200805 VRB(session, "Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100806 free(cp);
807
808 /* verify the signature on this CRL */
809 pubkey = X509_get_pubkey(cert);
810 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200811 ERR(session, "Cert verify CRL: invalid signature.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100812 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
813 X509_OBJECT_free_contents(&obj);
814 if (pubkey) {
815 EVP_PKEY_free(pubkey);
816 }
817 return 0;
818 }
819 if (pubkey) {
820 EVP_PKEY_free(pubkey);
821 }
822
823 /* check date of CRL to make sure it's not expired */
824 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200825 ERR(session, "Cert verify CRL: invalid nextUpdate field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100826 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
827 X509_OBJECT_free_contents(&obj);
828 return 0;
829 }
830 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200831 ERR(session, "Cert verify CRL: expired - revoking all certificates.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100832 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
833 X509_OBJECT_free_contents(&obj);
834 return 0;
835 }
836 X509_OBJECT_free_contents(&obj);
837 }
838
839 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100840 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100841 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100842 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700843 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100844 X509_STORE_CTX_cleanup(&store_ctx);
845 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200846 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100847 /* check if the current certificate is revoked by this CRL */
848 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
849 for (i = 0; i < n; i++) {
850 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
851 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
852 serial = ASN1_INTEGER_get(revoked->serialNumber);
853 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200854 ERR(session, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
855 serial, serial, cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100856 OPENSSL_free(cp);
857 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
858 X509_OBJECT_free_contents(&obj);
859 return 0;
860 }
861 }
862 X509_OBJECT_free_contents(&obj);
863 }
864 }
865
866 /* cert-to-name already successful */
867 if (session->username) {
868 return 1;
869 }
870
871 /* cert-to-name */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100872 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100873
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100874 if (rc) {
875 if (rc == -1) {
876 /* fatal error */
877 depth = 0;
878 }
879 /* rc == 1 is a normal CTN fail (no match found) */
880 goto fail;
881 }
882
883 /* cert-to-name match, now to extract the specific field from the peer cert */
884 if (map_type == NC_TLS_CTN_SPECIFIED) {
Barbaros Tokaoglubaa1e482021-12-11 16:04:37 +0300885 session->username = strdup(username);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100886 } else {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200887 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100888 if (rc) {
889 if (rc == -1) {
890 depth = 0;
891 }
892 goto fail;
893 }
Barbaros Tokaoglubaa1e482021-12-11 16:04:37 +0300894 session->username = cp;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100895 }
896
Michal Vasko05532772021-06-03 12:12:38 +0200897 VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100898
899 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200900 VRB(session, "Cert verify: user verify callback revoked authorization.");
Michal Vasko7060bcf2016-11-28 14:48:11 +0100901 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
902 return 0;
903 }
904
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100905 return 1;
906
907fail:
908 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200909 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 +0100910 return 1;
911 }
912
Michal Vasko05532772021-06-03 12:12:38 +0200913 VRB(session, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100914 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
915 return 0;
916}
917
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100918#endif
919
Michal Vasko3031aae2016-01-27 16:07:18 +0100920static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100921nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100922{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100923 if (!name) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100924 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100925 free(opts->server_cert);
Michal Vaskoa8748792016-11-22 14:34:26 +0100926 }
927 opts->server_cert = NULL;
928 return 0;
929 }
930
Michal Vaskoe2713da2016-08-22 16:06:40 +0200931 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100932 free(opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100933 }
Michal Vasko93224072021-11-09 12:14:28 +0100934 opts->server_cert = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100935
936 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100937}
938
939API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100940nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100941{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100942 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100943 struct nc_endpt *endpt;
944
Michal Vasko2e6defd2016-10-07 15:48:15 +0200945 if (!endpt_name) {
946 ERRARG("endpt_name");
947 return -1;
948 }
949
Michal Vasko51e514d2016-02-02 15:51:52 +0100950 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100951 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100952 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100953 return -1;
954 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100955 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100956 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100957 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100958
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100959 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100960}
961
962API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200963nc_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 +0100964{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100965 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200966 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200967 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200968
969 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200970 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
971 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200972 return -1;
973 }
974
Michal Vaskoadf30f02019-06-24 09:34:47 +0200975 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200976
977 /* UNLOCK */
978 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100979
980 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100981}
982
Michal Vasko4c1fb492017-01-30 14:31:07 +0100983API void
984nc_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 +0200985 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
986 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100987{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100988 if (!cert_clb) {
989 ERRARG("cert_clb");
990 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100991 }
992
Michal Vasko4c1fb492017-01-30 14:31:07 +0100993 server_opts.server_cert_clb = cert_clb;
994 server_opts.server_cert_data = user_data;
995 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100996}
997
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500998API void
999nc_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 +02001000 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 -05001001{
1002 if (!cert_chain_clb) {
1003 ERRARG("cert_chain_clb");
1004 return;
1005 }
1006
1007 server_opts.server_cert_chain_clb = cert_chain_clb;
1008 server_opts.server_cert_chain_data = user_data;
1009 server_opts.server_cert_chain_data_free = free_user_data;
1010}
1011
Michal Vaskoc61c4492016-01-25 11:13:34 +01001012static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001013nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001014{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001015 if (!name) {
1016 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001017 return -1;
1018 }
1019
Michal Vasko4c1fb492017-01-30 14:31:07 +01001020 ++opts->trusted_cert_list_count;
Michal Vasko77367452021-02-16 16:32:18 +01001021 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists,
1022 opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001023 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001024 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001025 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001026 }
Michal Vasko93224072021-11-09 12:14:28 +01001027 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028
1029 return 0;
1030}
1031
1032API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001033nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001034{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001035 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001036 struct nc_endpt *endpt;
1037
Michal Vasko2e6defd2016-10-07 15:48:15 +02001038 if (!endpt_name) {
1039 ERRARG("endpt_name");
1040 return -1;
1041 }
1042
Michal Vasko51e514d2016-02-02 15:51:52 +01001043 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001044 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001045 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001046 return -1;
1047 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001048 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001049 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001050 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001051
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001052 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001053}
1054
1055API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001056nc_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 +01001057{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001058 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001059 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001060 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001061
1062 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001063 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1064 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001065 return -1;
1066 }
1067
Michal Vaskoadf30f02019-06-24 09:34:47 +02001068 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001069
1070 /* UNLOCK */
1071 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001072
1073 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001074}
1075
Michal Vasko4c1fb492017-01-30 14:31:07 +01001076API void
1077nc_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 +02001078 int *cert_path_count, char ***cert_data, int *cert_data_count),
1079 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001080{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001081 if (!cert_list_clb) {
1082 ERRARG("cert_list_clb");
1083 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001084 }
1085
Michal Vasko4c1fb492017-01-30 14:31:07 +01001086 server_opts.trusted_cert_list_clb = cert_list_clb;
1087 server_opts.trusted_cert_list_data = user_data;
1088 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001089}
1090
1091static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001092nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001093{
1094 uint16_t i;
1095
1096 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001097 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001098 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001099 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001100 free(opts->trusted_cert_lists);
1101 opts->trusted_cert_lists = NULL;
1102 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001103 return 0;
1104 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001105 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1106 if (!strcmp(opts->trusted_cert_lists[i], name)) {
Michal Vasko93224072021-11-09 12:14:28 +01001107 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001108
Michal Vasko4c1fb492017-01-30 14:31:07 +01001109 --opts->trusted_cert_list_count;
1110 if (i < opts->trusted_cert_list_count - 1) {
1111 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1112 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001113 }
1114 return 0;
1115 }
1116 }
1117 }
1118
Michal Vasko05532772021-06-03 12:12:38 +02001119 ERR(NULL, "Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001120 return -1;
1121}
1122
1123API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001124nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001125{
1126 int ret;
1127 struct nc_endpt *endpt;
1128
Michal Vasko2e6defd2016-10-07 15:48:15 +02001129 if (!endpt_name) {
1130 ERRARG("endpt_name");
1131 return -1;
1132 }
1133
Michal Vaskoe2713da2016-08-22 16:06:40 +02001134 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001135 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001136 if (!endpt) {
1137 return -1;
1138 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001139 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001140 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001141 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001142
1143 return ret;
1144}
1145
1146API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001147nc_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 +02001148{
1149 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001150 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001151 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001152
1153 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001154 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1155 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001156 return -1;
1157 }
1158
Michal Vaskoadf30f02019-06-24 09:34:47 +02001159 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001160
1161 /* UNLOCK */
1162 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001163
1164 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001165}
1166
1167static int
Michal Vasko96830e32016-02-01 10:54:18 +01001168nc_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 +01001169{
Michal Vasko96830e32016-02-01 10:54:18 +01001170 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001171 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001172 return -1;
1173 }
1174
Michal Vasko96830e32016-02-01 10:54:18 +01001175 if (ca_file) {
Michal Vasko93224072021-11-09 12:14:28 +01001176 free(opts->trusted_ca_file);
1177 opts->trusted_ca_file = strdup(ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001178 }
1179
Michal Vasko96830e32016-02-01 10:54:18 +01001180 if (ca_dir) {
Michal Vasko93224072021-11-09 12:14:28 +01001181 free(opts->trusted_ca_dir);
1182 opts->trusted_ca_dir = strdup(ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001183 }
1184
1185 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001186}
1187
Michal Vaskoc61c4492016-01-25 11:13:34 +01001188API int
Michal Vasko96830e32016-02-01 10:54:18 +01001189nc_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 +01001190{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001191 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001192 struct nc_endpt *endpt;
1193
Michal Vasko2e6defd2016-10-07 15:48:15 +02001194 if (!endpt_name) {
1195 ERRARG("endpt_name");
1196 return -1;
1197 }
1198
Michal Vasko51e514d2016-02-02 15:51:52 +01001199 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001200 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001201 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001202 return -1;
1203 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001204 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001205 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001206 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001207
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001208 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001209}
1210
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001211API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001212nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file,
1213 const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001215 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001216 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001217 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001218
1219 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001220 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1221 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001222 return -1;
1223 }
1224
Michal Vaskoadf30f02019-06-24 09:34:47 +02001225 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001226
1227 /* UNLOCK */
1228 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001229
1230 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001231}
1232
Michal Vaskoc61c4492016-01-25 11:13:34 +01001233static int
Michal Vasko96830e32016-02-01 10:54:18 +01001234nc_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 +01001235{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001236 X509_LOOKUP *lookup;
1237
Michal Vasko96830e32016-02-01 10:54:18 +01001238 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001239 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001240 return -1;
1241 }
1242
Michal Vaskoc61c4492016-01-25 11:13:34 +01001243 if (!opts->crl_store) {
1244 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001245 }
1246
Michal Vasko96830e32016-02-01 10:54:18 +01001247 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001248 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001249 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001250 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001251 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001252 }
1253
Michal Vasko96830e32016-02-01 10:54:18 +01001254 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001255 ERR(NULL, "Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001256 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001257 }
1258 }
1259
Michal Vasko96830e32016-02-01 10:54:18 +01001260 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001261 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001262 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001263 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001264 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001265 }
1266
Michal Vasko96830e32016-02-01 10:54:18 +01001267 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001268 ERR(NULL, "Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001269 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001270 }
1271 }
1272
1273 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001274
1275fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001276 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001277}
1278
Michal Vaskoc61c4492016-01-25 11:13:34 +01001279API int
Michal Vasko96830e32016-02-01 10:54:18 +01001280nc_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 +01001281{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001282 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001283 struct nc_endpt *endpt;
1284
Michal Vasko2e6defd2016-10-07 15:48:15 +02001285 if (!endpt_name) {
1286 ERRARG("endpt_name");
1287 return -1;
1288 }
1289
Michal Vasko51e514d2016-02-02 15:51:52 +01001290 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001291 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001292 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001293 return -1;
1294 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001295 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001296 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001297 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001298
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001299 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001300}
1301
1302API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001303nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file,
1304 const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001305{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001306 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001307 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001308 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001309
1310 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001311 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1312 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001313 return -1;
1314 }
1315
Michal Vaskoadf30f02019-06-24 09:34:47 +02001316 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001317
1318 /* UNLOCK */
1319 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001320
1321 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001322}
1323
1324static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001325nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001326{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001327 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001328 return;
1329 }
1330
1331 X509_STORE_free(opts->crl_store);
1332 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001333}
1334
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001336nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001337{
Michal Vasko3031aae2016-01-27 16:07:18 +01001338 struct nc_endpt *endpt;
1339
Michal Vasko2e6defd2016-10-07 15:48:15 +02001340 if (!endpt_name) {
1341 ERRARG("endpt_name");
1342 return;
1343 }
1344
Michal Vasko51e514d2016-02-02 15:51:52 +01001345 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001346 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001347 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001348 return;
1349 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001350 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001351 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001352 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001353}
1354
Michal Vaskoc61c4492016-01-25 11:13:34 +01001355API void
Michal Vaskoadf30f02019-06-24 09:34:47 +02001356nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001357{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001358 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001359 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001360
1361 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001362 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1363 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001364 return;
1365 }
1366
Michal Vaskoadf30f02019-06-24 09:34:47 +02001367 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001368
1369 /* UNLOCK */
1370 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001371}
1372
1373static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001374nc_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 +02001375 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001376{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001377 struct nc_ctn *ctn, *new;
1378
Michal Vaskoc61c4492016-01-25 11:13:34 +01001379 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001380 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001381 opts->ctn = new = calloc(1, sizeof *new);
1382 if (!new) {
1383 ERRMEM;
1384 return -1;
1385 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001386 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001387 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001388 new = calloc(1, sizeof *new);
1389 if (!new) {
1390 ERRMEM;
1391 return -1;
1392 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001393 new->next = opts->ctn;
1394 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001395 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001396 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {}
Michal Vasko276f9d92017-02-02 11:15:55 +01001397 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001398 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001399 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001400 } else {
1401 /* insert after ctn */
1402 new = calloc(1, sizeof *new);
1403 if (!new) {
1404 ERRMEM;
1405 return -1;
1406 }
1407 new->next = ctn->next;
1408 ctn->next = new;
1409 }
1410 }
1411
1412 new->id = id;
1413 if (fingerprint) {
Michal Vasko93224072021-11-09 12:14:28 +01001414 free(new->fingerprint);
1415 new->fingerprint = strdup(fingerprint);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001416 }
1417 if (map_type) {
1418 new->map_type = map_type;
1419 }
1420 if (name) {
Michal Vasko93224072021-11-09 12:14:28 +01001421 free(new->name);
1422 new->name = strdup(name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001423 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001424
1425 return 0;
1426}
1427
1428API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001429nc_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 +02001430 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001431{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001432 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001433 struct nc_endpt *endpt;
1434
Michal Vasko2e6defd2016-10-07 15:48:15 +02001435 if (!endpt_name) {
1436 ERRARG("endpt_name");
1437 return -1;
1438 }
1439
Michal Vasko51e514d2016-02-02 15:51:52 +01001440 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001441 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001442 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001443 return -1;
1444 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001445 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001446 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001447 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001448
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001449 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001450}
1451
1452API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001453nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id,
1454 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001455{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001456 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001457 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001458 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001459
1460 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001461 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1462 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001463 return -1;
1464 }
1465
Michal Vaskoadf30f02019-06-24 09:34:47 +02001466 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001467
1468 /* UNLOCK */
1469 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001470
1471 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001472}
1473
1474static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001475nc_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 +02001476 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001477{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001478 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001479 int ret = -1;
1480
Michal Vasko1a38c862016-01-15 15:50:07 +01001481 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001482 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001483 while (ctn) {
Michal Vasko93224072021-11-09 12:14:28 +01001484 free(ctn->fingerprint);
1485 free(ctn->name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001486
1487 next = ctn->next;
1488 free(ctn);
1489 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001490
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001491 ret = 0;
1492 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001493 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001494 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001495 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001496 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001497 while (ctn) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001498 if (((id < 0) || (ctn->id == id)) &&
1499 (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) &&
1500 (!map_type || (ctn->map_type == map_type)) &&
1501 (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko93224072021-11-09 12:14:28 +01001502 free(ctn->fingerprint);
1503 free(ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001504
Michal Vasko5e3f3392016-01-20 11:13:01 +01001505 if (prev) {
1506 prev->next = ctn->next;
1507 next = ctn->next;
1508 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001509 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001510 next = ctn->next;
1511 }
1512 free(ctn);
1513 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001514
1515 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001516 } else {
1517 prev = ctn;
1518 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001519 }
1520 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001521 }
1522
1523 return ret;
1524}
1525
Michal Vaskoc61c4492016-01-25 11:13:34 +01001526API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001527nc_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 +02001528 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001529{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001530 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001531 struct nc_endpt *endpt;
1532
Michal Vasko2e6defd2016-10-07 15:48:15 +02001533 if (!endpt_name) {
1534 ERRARG("endpt_name");
1535 return -1;
1536 }
1537
Michal Vasko51e514d2016-02-02 15:51:52 +01001538 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001539 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001540 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001541 return -1;
1542 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001543 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001544 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001545 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001546
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001547 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001548}
1549
1550API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001551nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id,
1552 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001553{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001554 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001555 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001556 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001557
1558 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001559 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1560 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001561 return -1;
1562 }
1563
Michal Vaskoadf30f02019-06-24 09:34:47 +02001564 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001565
1566 /* UNLOCK */
1567 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001568
1569 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001570}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001571
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001572static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001573nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001574 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001575{
1576 struct nc_ctn *ctn;
1577 int ret = -1;
1578
1579 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1580 if (id && *id && (*id != ctn->id)) {
1581 continue;
1582 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001583 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001584 continue;
1585 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001586 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001587 continue;
1588 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001589 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001590 continue;
1591 }
1592
1593 /* first match, good enough */
1594 if (id && !(*id)) {
1595 *id = ctn->id;
1596 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001597 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001598 *fingerprint = strdup(ctn->fingerprint);
1599 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001600 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001601 *map_type = ctn->map_type;
1602 }
Adam Richterd44680e2019-06-15 13:16:16 -07001603 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001604 *name = strdup(ctn->name);
1605 }
1606
1607 ret = 0;
1608 break;
1609 }
1610
1611 return ret;
1612}
1613
1614API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001615nc_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 +02001616 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001617{
1618 int ret;
1619 struct nc_endpt *endpt;
1620
1621 if (!endpt_name) {
1622 ERRARG("endpt_name");
1623 return -1;
1624 }
1625
1626 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001627 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001628 if (!endpt) {
1629 return -1;
1630 }
1631 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1632 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001633 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001634
1635 return ret;
1636}
1637
1638API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001639nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
1640 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001641{
1642 int ret;
1643 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001644 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001645
1646 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001647 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1648 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001649 return -1;
1650 }
1651
Michal Vaskoadf30f02019-06-24 09:34:47 +02001652 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001653
1654 /* UNLOCK */
1655 nc_server_ch_client_unlock(client);
1656
1657 return ret;
1658}
1659
Michal Vasko709598e2016-11-28 14:48:32 +01001660API const X509 *
1661nc_session_get_client_cert(const struct nc_session *session)
1662{
1663 if (!session || (session->side != NC_SERVER)) {
1664 ERRARG("session");
1665 return NULL;
1666 }
1667
1668 return session->opts.server.client_cert;
1669}
1670
Michal Vasko7060bcf2016-11-28 14:48:11 +01001671API void
1672nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1673{
1674 server_opts.user_verify_clb = verify_clb;
1675}
1676
Michal Vasko3031aae2016-01-27 16:07:18 +01001677void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001678nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001679{
Michal Vasko93224072021-11-09 12:14:28 +01001680 free(opts->server_cert);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001681 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vasko93224072021-11-09 12:14:28 +01001682 free(opts->trusted_ca_file);
1683 free(opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001684 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001685 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001686}
Michal Vasko9e036d52016-01-08 10:49:26 +01001687
Michal Vasko6d292992016-01-18 09:42:38 +01001688static void
1689nc_tls_make_verify_key(void)
1690{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001691 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001692}
1693
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001694static X509 *
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001695tls_load_cert(const char *cert_path, const char *cert_data)
1696{
1697 X509 *cert;
1698
1699 if (cert_path) {
1700 cert = pem_to_cert(cert_path);
1701 } else {
1702 cert = base64der_to_cert(cert_data);
1703 }
1704
1705 if (!cert) {
1706 if (cert_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001707 ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001708 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001709 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001710 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001711 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001712 }
1713 }
1714 return cert;
1715}
1716
1717static int
1718nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1719{
1720 char **cert_paths = NULL, **cert_data = NULL;
1721 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1722 X509 *cert = NULL;
1723
1724 if (!server_opts.server_cert_chain_clb) {
1725 /* This is optional, so return OK */
1726 return 0;
1727 }
1728
1729 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001730 &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001731 ERR(NULL, "Server certificate chain callback failed.");
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001732 return -1;
1733 }
1734
1735 for (i = 0; i < cert_path_count; ++i) {
1736 cert = tls_load_cert(cert_paths[i], NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001737 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001738 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001739 ret = -1;
1740 goto cleanup;
1741 }
1742 }
1743
1744 for (i = 0; i < cert_data_count; ++i) {
1745 cert = tls_load_cert(NULL, cert_data[i]);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001746 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001747 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001748 ret = -1;
1749 goto cleanup;
1750 }
1751 }
1752cleanup:
1753 for (i = 0; i < cert_path_count; ++i) {
1754 free(cert_paths[i]);
1755 }
1756 free(cert_paths);
1757 for (i = 0; i < cert_data_count; ++i) {
1758 free(cert_data[i]);
1759 }
1760 free(cert_data);
1761 /* cert is owned by the SSL_CTX */
1762
1763 return ret;
1764}
1765
Michal Vasko4c1fb492017-01-30 14:31:07 +01001766static int
1767nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1768{
1769 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001770 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001771 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001772 X509 *cert = NULL;
1773 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001774
1775 if (!cert_name) {
Michal Vasko05532772021-06-03 12:12:38 +02001776 ERR(NULL, "Server certificate not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001777 return -1;
1778 } else if (!server_opts.server_cert_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001779 ERR(NULL, "Callback for retrieving the server certificate is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001780 return -1;
1781 }
1782
1783 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 +02001784 &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001785 ERR(NULL, "Server certificate callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001786 return -1;
1787 }
1788
1789 /* load the certificate */
1790 if (cert_path) {
1791 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001792 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001793 ret = -1;
1794 goto cleanup;
1795 }
1796 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001797 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001798 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001799 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001800 ret = -1;
1801 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001802 }
1803 }
1804
1805 /* load the private key */
1806 if (privkey_path) {
1807 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001808 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001809 ret = -1;
1810 goto cleanup;
1811 }
1812 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001813 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001814 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001815 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001816 ret = -1;
1817 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001818 }
1819 }
1820
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001821 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1822
Michal Vasko4c1fb492017-01-30 14:31:07 +01001823cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001824 X509_free(cert);
1825 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001826 free(cert_path);
1827 free(cert_data);
1828 free(privkey_path);
1829 free(privkey_data);
1830 return ret;
1831}
1832
1833static void
1834tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1835{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001836 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001837
Michal Vasko4c1fb492017-01-30 14:31:07 +01001838 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001839 return;
1840 }
1841
1842 /* add the trusted certificate */
1843 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001844 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001845 X509_free(cert);
1846 return;
1847 }
1848 X509_free(cert);
1849}
1850
1851static int
Michal Vasko93224072021-11-09 12:14:28 +01001852nc_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 +01001853{
1854 uint16_t i;
1855 int j;
1856 char **cert_paths, **cert_data;
1857 int cert_path_count, cert_data_count;
1858
1859 if (!server_opts.trusted_cert_list_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001860 ERR(NULL, "Callback for retrieving trusted certificate lists is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001861 return -1;
1862 }
1863
1864 for (i = 0; i < trusted_cert_list_count; ++i) {
1865 cert_paths = cert_data = NULL;
1866 cert_path_count = cert_data_count = 0;
1867 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001868 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001869 ERR(NULL, "Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001870 return -1;
1871 }
1872
1873 for (j = 0; j < cert_path_count; ++j) {
1874 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1875 free(cert_paths[j]);
1876 }
1877 free(cert_paths);
1878
1879 for (j = 0; j < cert_data_count; ++j) {
1880 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1881 free(cert_data[j]);
1882 }
1883 free(cert_data);
1884 }
1885
1886 return 0;
1887}
1888
Michal Vasko9af52322022-07-25 14:39:30 +02001889static int
1890nc_server_tls_accept_check(int accept_ret, struct nc_session *session)
1891{
1892 int verify;
1893
1894 /* check certificate verification result */
1895 verify = SSL_get_verify_result(session->ti.tls);
1896 switch (verify) {
1897 case X509_V_OK:
1898 if (accept_ret == 1) {
1899 VRB(session, "Client certificate verified.");
1900 }
1901 break;
1902 default:
1903 ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify));
1904 }
1905
1906 if (accept_ret != 1) {
1907 switch (SSL_get_error(session->ti.tls, accept_ret)) {
1908 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001909 ERR(session, "SSL accept failed (%s).", strerror(errno));
Michal Vasko9af52322022-07-25 14:39:30 +02001910 break;
1911 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001912 ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +02001913 break;
1914 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001915 ERR(session, "SSL accept failed.");
Michal Vasko9af52322022-07-25 14:39:30 +02001916 break;
1917 }
1918 }
1919
1920 return accept_ret;
1921}
1922
Michal Vasko3031aae2016-01-27 16:07:18 +01001923int
Michal Vasko0190bc32016-03-02 15:47:49 +01001924nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001925{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001926 X509_STORE *cert_store;
1927 SSL_CTX *tls_ctx;
1928 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001929 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001930 int ret;
roman6ece9c52022-06-22 09:29:17 +02001931 struct timespec ts_timeout;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001932
Michal Vasko2cc4c682016-03-01 09:16:48 +01001933 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001934
Michal Vaskoe2713da2016-08-22 16:06:40 +02001935 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001936#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1937 tls_ctx = SSL_CTX_new(TLS_server_method());
1938#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001939 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001940#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001941 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001942 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001943 goto error;
1944 }
1945 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 +01001946 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001947 goto error;
1948 }
1949
1950 /* X509_STORE, managed (freed) with the context */
1951 cert_store = X509_STORE_new();
1952 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1953
Michal Vasko4c1fb492017-01-30 14:31:07 +01001954 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1955 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001956 }
1957
1958 if (opts->trusted_ca_file) {
1959 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1960 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001961 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001962 goto error;
1963 }
1964
1965 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001966 ERR(session, "Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001967 goto error;
1968 }
1969 }
1970
1971 if (opts->trusted_ca_dir) {
1972 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1973 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001974 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001975 goto error;
1976 }
1977
1978 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001979 ERR(session, "Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001980 goto error;
1981 }
1982 }
1983
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001984 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001985 session->ti.tls = SSL_new(tls_ctx);
1986
Michal Vasko4c1fb492017-01-30 14:31:07 +01001987 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001988 SSL_CTX_free(tls_ctx);
1989 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001990
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001991 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001992 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001993 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001994 }
1995
1996 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001997 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001998 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1999
Michal Vasko6d292992016-01-18 09:42:38 +01002000 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01002001 pthread_once(&verify_once, nc_tls_make_verify_key);
2002 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002003
Michal Vasko36c7be82017-02-22 13:37:59 +01002004 if (timeout > -1) {
roman6ece9c52022-06-22 09:29:17 +02002005 nc_gettimespec_mono_add(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01002006 }
Michal Vasko0190bc32016-03-02 15:47:49 +01002007 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
2008 usleep(NC_TIMEOUT_STEP);
Michal Vasko60d8ffb2022-07-21 11:08:34 +02002009 if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +02002010 ERR(session, "SSL accept timeout.");
roman6ece9c52022-06-22 09:29:17 +02002011 return 0;
Michal Vasko0190bc32016-03-02 15:47:49 +01002012 }
2013 }
Michal Vasko9af52322022-07-25 14:39:30 +02002014 if (nc_server_tls_accept_check(ret, session) != 1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002015 return -1;
2016 }
2017
Michal Vasko1a38c862016-01-15 15:50:07 +01002018 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002019
2020error:
2021 if (sock > -1) {
2022 close(sock);
2023 }
2024 SSL_CTX_free(tls_ctx);
2025 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002026}