blob: b18e79b94b0c2f5dadca0217fe4be5b0ef66a971 [file] [log] [blame]
Radek Krejci5da708a2015-09-01 17:33:23 +02001/**
Michal Vasko086311b2016-01-08 09:53:11 +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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5da708a2015-09-01 17:33:23 +020013 */
14
Michal Vaskoc14e3c82016-01-11 16:14:30 +010015#define _GNU_SOURCE
16
Michal Vaskoc14e3c82016-01-11 16:14:30 +010017#include <poll.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020018#include <string.h>
Michal Vaskof0537d82016-01-29 14:42:38 +010019#include <unistd.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010020
Michal Vaskoc14e3c82016-01-11 16:14:30 +010021#include <openssl/err.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <openssl/evp.h>
23#include <openssl/ssl.h>
Michal Vaskoe2713da2016-08-22 16:06:40 +020024#include <openssl/x509.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020025#include <openssl/x509v3.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010026
Michal Vasko316c9632020-04-15 11:06:57 +020027#include "compat.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020028#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010029#include "session_server.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010030#include "session_server_ch.h"
Radek Krejci5da708a2015-09-01 17:33:23 +020031
Rosen Penev4f552d62019-06-26 16:10:43 -070032#if OPENSSL_VERSION_NUMBER < 0x10100000L
33#define X509_STORE_CTX_get_by_subject X509_STORE_get_by_subject
34#endif
35
Michal Vasko3031aae2016-01-27 16:07:18 +010036struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010037pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER;
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038extern struct nc_server_opts server_opts;
Michal Vaskoc61c4492016-01-25 11:13:34 +010039
Michal Vasko5c2f7952016-01-22 13:16:31 +010040static pthread_key_t verify_key;
41static pthread_once_t verify_once = PTHREAD_ONCE_INIT;
42
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043static char *
Michal Vasko18aeb5d2017-02-17 09:23:56 +010044asn1time_to_str(const ASN1_TIME *t)
Michal Vasko086311b2016-01-08 09:53:11 +010045{
Michal Vaskoc14e3c82016-01-11 16:14:30 +010046 char *cp;
47 BIO *bio;
48 int n;
Radek Krejci5da708a2015-09-01 17:33:23 +020049
Michal Vaskoc14e3c82016-01-11 16:14:30 +010050 if (!t) {
51 return NULL;
52 }
53 bio = BIO_new(BIO_s_mem());
54 if (!bio) {
55 return NULL;
56 }
57 ASN1_TIME_print(bio, t);
58 n = BIO_pending(bio);
59 cp = malloc(n + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +010060 if (!cp) {
61 ERRMEM;
62 BIO_free(bio);
63 return NULL;
64 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010065 n = BIO_read(bio, cp, n);
66 if (n < 0) {
67 BIO_free(bio);
68 free(cp);
69 return NULL;
70 }
71 cp[n] = '\0';
72 BIO_free(bio);
73 return cp;
74}
75
76static void
77digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str)
78{
79 unsigned int i;
80
81 *str = malloc(dig_len * 3);
Michal Vasko4eb3c312016-03-01 14:09:37 +010082 if (!*str) {
83 ERRMEM;
84 return;
85 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010086 for (i = 0; i < dig_len - 1; ++i) {
87 sprintf((*str) + (i * 3), "%02x:", digest[i]);
88 }
89 sprintf((*str) + (i * 3), "%02x", digest[i]);
90}
91
92/* return NULL - SSL error can be retrieved */
93static X509 *
94base64der_to_cert(const char *in)
95{
96 X509 *out;
97 char *buf;
98 BIO *bio;
99
100 if (in == NULL) {
101 return NULL;
102 }
103
104 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
105 return NULL;
106 }
107 bio = BIO_new_mem_buf(buf, strlen(buf));
108 if (!bio) {
109 free(buf);
110 return NULL;
111 }
112
113 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
114 if (!out) {
115 free(buf);
116 BIO_free(bio);
117 return NULL;
118 }
119
120 free(buf);
121 BIO_free(bio);
122 return out;
123}
124
125/* return NULL - either errno or SSL error */
126static X509 *
127pem_to_cert(const char *path)
128{
129 FILE *fp;
130 X509 *out;
131
132 fp = fopen(path, "r");
133 if (!fp) {
134 return NULL;
135 }
136
137 out = PEM_read_X509(fp, NULL, NULL, NULL);
138 fclose(fp);
139 return out;
140}
141
Michal Vasko6e08cb72017-02-02 11:15:29 +0100142static EVP_PKEY *
Michal Vaskoddce1212019-05-24 09:58:49 +0200143base64der_to_privatekey(const char *in, const char *key_str)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100144{
145 EVP_PKEY *out;
146 char *buf;
147 BIO *bio;
148
149 if (in == NULL) {
150 return NULL;
151 }
152
Michal Vaskoddce1212019-05-24 09:58:49 +0200153 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 +0200154 key_str, " PRIVATE KEY-----") == -1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100155 return NULL;
156 }
157 bio = BIO_new_mem_buf(buf, strlen(buf));
158 if (!bio) {
159 free(buf);
160 return NULL;
161 }
162
163 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
164 if (!out) {
165 free(buf);
166 BIO_free(bio);
167 return NULL;
168 }
169
170 free(buf);
171 BIO_free(bio);
172 return out;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100173}
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100174
175static int
176cert_pubkey_match(X509 *cert1, X509 *cert2)
177{
178 ASN1_BIT_STRING *bitstr1, *bitstr2;
179
180 bitstr1 = X509_get0_pubkey_bitstr(cert1);
181 bitstr2 = X509_get0_pubkey_bitstr(cert2);
182
183 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
184 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
185 return 0;
186 }
187
188 return 1;
189}
190
191static int
192nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
193{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200194 STACK_OF(GENERAL_NAME) * san_names;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100195 GENERAL_NAME *san_name;
196 ASN1_OCTET_STRING *ip;
197 int i, san_count;
198 char *subject, *common_name;
199
200 if (map_type == NC_TLS_CTN_COMMON_NAME) {
201 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
202 common_name = strstr(subject, "CN=");
203 if (!common_name) {
Michal Vasko05532772021-06-03 12:12:38 +0200204 WRN(NULL, "Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100205 free(subject);
206 return 1;
207 }
208 common_name += 3;
209 if (strchr(common_name, '/')) {
210 *strchr(common_name, '/') = '\0';
211 }
212 *username = strdup(common_name);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100213 if (!*username) {
214 ERRMEM;
215 return 1;
216 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100217 free(subject);
218 } else {
219 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
220 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
221 if (!san_names) {
Michal Vasko05532772021-06-03 12:12:38 +0200222 WRN(NULL, "Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100223 return 1;
224 }
225
226 san_count = sk_GENERAL_NAME_num(san_names);
227 for (i = 0; i < san_count; ++i) {
228 san_name = sk_GENERAL_NAME_value(san_names, i);
229
230 /* rfc822Name (email) */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200231 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) &&
232 (san_name->type == GEN_EMAIL)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100233#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100234 *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100235#else
236 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name));
237#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100238 if (!*username) {
239 ERRMEM;
240 return 1;
241 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100242 break;
243 }
244
245 /* dNSName */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200246 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) &&
247 (san_name->type == GEN_DNS)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100248#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100249 *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100250#else
251 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName));
252#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100253 if (!*username) {
254 ERRMEM;
255 return 1;
256 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100257 break;
258 }
259
260 /* iPAddress */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200261 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_IP_ADDRESS)) &&
262 (san_name->type == GEN_IPADD)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100263 ip = san_name->d.iPAddress;
264 if (ip->length == 4) {
265 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 +0100266 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100267 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
268 return -1;
269 }
270 break;
271 } else if (ip->length == 16) {
272 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
273 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
274 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
275 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100276 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100277 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
278 return -1;
279 }
280 break;
281 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200282 WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100283 }
284 }
285 }
286 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
287
Vitaly Kuzina3b66322021-08-26 12:33:43 +0300288 if (i == san_count) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100289 switch (map_type) {
290 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200291 WRN(NULL, "Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100292 break;
293 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200294 WRN(NULL, "Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100295 break;
296 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vasko05532772021-06-03 12:12:38 +0200297 WRN(NULL, "Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100298 break;
299 case NC_TLS_CTN_SAN_ANY:
Michal Vasko05532772021-06-03 12:12:38 +0200300 WRN(NULL, "Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100301 break;
302 default:
303 break;
304 }
305 return 1;
306 }
307 }
308
309 return 0;
310}
311
312/* return: 0 - OK, 1 - no match, -1 - error */
313static int
Michal Vaskoc61c4492016-01-25 11:13:34 +0100314nc_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 +0100315{
316 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
317 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100318 unsigned char *buf = malloc(64);
319 unsigned int buf_len = 64;
320 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100321 struct nc_ctn *ctn;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100322
Michal Vasko4eb3c312016-03-01 14:09:37 +0100323 if (!buf) {
324 ERRMEM;
325 return -1;
326 }
327
Michal Vaskoc61c4492016-01-25 11:13:34 +0100328 if (!ctn_first || !cert || !map_type || !name) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100329 free(buf);
330 return -1;
331 }
332
Michal Vaskoc61c4492016-01-25 11:13:34 +0100333 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100334 /* first make sure the entry is valid */
335 if (!ctn->fingerprint || !ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
Michal Vasko05532772021-06-03 12:12:38 +0200336 VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100337 continue;
338 }
339
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100340 /* MD5 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100341 if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100342 if (!digest_md5) {
343 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200344 ERR(NULL, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100345 ret = -1;
346 goto cleanup;
347 }
348 digest_to_str(buf, buf_len, &digest_md5);
349 }
350
Michal Vasko5e3f3392016-01-20 11:13:01 +0100351 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100352 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200353 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100354 *map_type = ctn->map_type;
355 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
356 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100357 }
358 break;
359 }
360
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200361 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100362 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100363 if (!digest_sha1) {
364 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200365 ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100366 ret = -1;
367 goto cleanup;
368 }
369 digest_to_str(buf, buf_len, &digest_sha1);
370 }
371
Michal Vasko5e3f3392016-01-20 11:13:01 +0100372 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100373 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200374 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100375 *map_type = ctn->map_type;
376 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
377 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100378 }
379 break;
380 }
381
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200382 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100383 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100384 if (!digest_sha224) {
385 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200386 ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100387 ret = -1;
388 goto cleanup;
389 }
390 digest_to_str(buf, buf_len, &digest_sha224);
391 }
392
Michal Vasko5e3f3392016-01-20 11:13:01 +0100393 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100394 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200395 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100396 *map_type = ctn->map_type;
397 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
398 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100399 }
400 break;
401 }
402
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200403 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100404 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100405 if (!digest_sha256) {
406 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200407 ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100408 ret = -1;
409 goto cleanup;
410 }
411 digest_to_str(buf, buf_len, &digest_sha256);
412 }
413
Michal Vasko5e3f3392016-01-20 11:13:01 +0100414 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100415 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200416 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100417 *map_type = ctn->map_type;
418 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
419 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100420 }
421 break;
422 }
423
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200424 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100425 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100426 if (!digest_sha384) {
427 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200428 ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100429 ret = -1;
430 goto cleanup;
431 }
432 digest_to_str(buf, buf_len, &digest_sha384);
433 }
434
Michal Vasko5e3f3392016-01-20 11:13:01 +0100435 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100436 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200437 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100438 *map_type = ctn->map_type;
439 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
440 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100441 }
442 break;
443 }
444
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200445 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100446 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100447 if (!digest_sha512) {
448 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200449 ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100450 ret = -1;
451 goto cleanup;
452 }
453 digest_to_str(buf, buf_len, &digest_sha512);
454 }
455
Michal Vasko5e3f3392016-01-20 11:13:01 +0100456 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100457 /* we got ourselves a winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200458 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100459 *map_type = ctn->map_type;
460 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
461 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100462 }
463 break;
464 }
465
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200466 /* unknown */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100467 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200468 WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100469 }
470 }
471
Michal Vasko5e3f3392016-01-20 11:13:01 +0100472 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100473 ret = 1;
474 }
475
476cleanup:
477 free(digest_md5);
478 free(digest_sha1);
479 free(digest_sha224);
480 free(digest_sha256);
481 free(digest_sha384);
482 free(digest_sha512);
483 free(buf);
484 return ret;
485}
486
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100487#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
488
489static int
490nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
491{
492 X509_STORE_CTX *store_ctx;
493 X509_OBJECT *obj;
494 X509_NAME *subject;
495 X509_NAME *issuer;
496 X509 *cert;
497 X509_CRL *crl;
498 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200499
500 STACK_OF(X509) * cert_stack;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100501 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200502 struct nc_session *session;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100503 struct nc_server_tls_opts *opts;
504 const ASN1_INTEGER *serial;
505 int i, n, rc, depth;
506 char *cp;
507 const char *username = NULL;
508 NC_TLS_CTN_MAPTYPE map_type = 0;
509 const ASN1_TIME *last_update = NULL, *next_update = NULL;
510
511 /* get the thread session */
512 session = pthread_getspecific(verify_key);
513 if (!session) {
514 ERRINT;
515 return 0;
516 }
517
518 opts = session->data;
519
520 /* get the last certificate, that is the peer (client) certificate */
521 if (!session->opts.server.client_cert) {
522 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500523 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100524 X509_up_ref(session->opts.server.client_cert);
525 sk_X509_pop_free(cert_stack, X509_free);
526 }
527
528 /* standard certificate verification failed, so a trusted client cert must match to continue */
529 if (!preverify_ok) {
530 subject = X509_get_subject_name(session->opts.server.client_cert);
531 cert_stack = X509_STORE_CTX_get1_certs(x509_ctx, subject);
532 if (cert_stack) {
533 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
534 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
535 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
536 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200537 VRB(NULL, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200538 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100539 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
540 sk_X509_pop_free(cert_stack, X509_free);
541 return 1;
542 }
543 }
544 sk_X509_pop_free(cert_stack, X509_free);
545 }
546
Michal Vasko05532772021-06-03 12:12:38 +0200547 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 +0100548 return 0;
549 }
550
551 /* print cert verify info */
552 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200553 VRB(NULL, "Cert verify: depth %d.", depth);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100554
555 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
556 subject = X509_get_subject_name(cert);
557 issuer = X509_get_issuer_name(cert);
558
559 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200560 VRB(NULL, "Cert verify: subject: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100561 OPENSSL_free(cp);
562 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200563 VRB(NULL, "Cert verify: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100564 OPENSSL_free(cp);
565
566 /* check for revocation if set */
567 if (opts->crl_store) {
568 /* try to retrieve a CRL corresponding to the _subject_ of
569 * the current certificate in order to verify it's integrity */
570 store_ctx = X509_STORE_CTX_new();
571 obj = X509_OBJECT_new();
572 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700573 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100574 X509_STORE_CTX_free(store_ctx);
575 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200576 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100577 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200578 VRB(NULL, "Cert verify CRL: issuer: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100579 OPENSSL_free(cp);
580
581 last_update = X509_CRL_get0_lastUpdate(crl);
582 next_update = X509_CRL_get0_nextUpdate(crl);
583 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200584 VRB(NULL, "Cert verify CRL: last update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100585 free(cp);
586 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200587 VRB(NULL, "Cert verify CRL: next update: %s.", cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100588 free(cp);
589
590 /* verify the signature on this CRL */
591 pubkey = X509_get_pubkey(cert);
592 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200593 ERR(NULL, "Cert verify CRL: invalid signature.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100594 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
595 X509_OBJECT_free(obj);
596 if (pubkey) {
597 EVP_PKEY_free(pubkey);
598 }
599 return 0;
600 }
601 if (pubkey) {
602 EVP_PKEY_free(pubkey);
603 }
604
605 /* check date of CRL to make sure it's not expired */
606 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200607 ERR(NULL, "Cert verify CRL: invalid nextUpdate field.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100608 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
609 X509_OBJECT_free(obj);
610 return 0;
611 }
612 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200613 ERR(NULL, "Cert verify CRL: expired - revoking all certificates.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100614 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
615 X509_OBJECT_free(obj);
616 return 0;
617 }
618 X509_OBJECT_free(obj);
619 }
620
621 /* try to retrieve a CRL corresponding to the _issuer_ of
622 * the current certificate in order to check for revocation */
623 store_ctx = X509_STORE_CTX_new();
624 obj = X509_OBJECT_new();
625 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700626 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100627 X509_STORE_CTX_free(store_ctx);
628 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200629 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100630 /* check if the current certificate is revoked by this CRL */
631 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
632 for (i = 0; i < n; i++) {
633 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
634 serial = X509_REVOKED_get0_serialNumber(revoked);
635 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
636 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200637 ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
638 serial, serial, cp);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100639 OPENSSL_free(cp);
640 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
641 X509_OBJECT_free(obj);
642 return 0;
643 }
644 }
645 X509_OBJECT_free(obj);
646 }
647 }
648
649 /* cert-to-name already successful */
650 if (session->username) {
651 return 1;
652 }
653
654 /* cert-to-name */
655 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
656
657 if (rc) {
658 if (rc == -1) {
659 /* fatal error */
660 depth = 0;
661 }
662 /* rc == 1 is a normal CTN fail (no match found) */
663 goto fail;
664 }
665
666 /* cert-to-name match, now to extract the specific field from the peer cert */
667 if (map_type == NC_TLS_CTN_SPECIFIED) {
Michal Vasko93224072021-11-09 12:14:28 +0100668 session->username = strdup(username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100669 } else {
670 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
671 if (rc) {
672 if (rc == -1) {
673 depth = 0;
674 }
675 goto fail;
676 }
Michal Vasko93224072021-11-09 12:14:28 +0100677 session->username = cp;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100678 }
679
Michal Vasko05532772021-06-03 12:12:38 +0200680 VRB(NULL, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100681
682 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200683 VRB(NULL, "Cert verify: user verify callback revoked authorization.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100684 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
685 return 0;
686 }
687
688 return 1;
689
690fail:
691 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200692 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 +0100693 return 1;
694 }
695
Michal Vasko05532772021-06-03 12:12:38 +0200696 VRB(NULL, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100697 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
698 return 0;
699}
700
701#else
702
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100703static int
704nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
705{
706 X509_STORE_CTX store_ctx;
707 X509_OBJECT obj;
708 X509_NAME *subject;
709 X509_NAME *issuer;
710 X509 *cert;
711 X509_CRL *crl;
712 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200713
714 STACK_OF(X509) * cert_stack;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100715 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200716 struct nc_session *session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100717 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100718 long serial;
719 int i, n, rc, depth;
720 char *cp;
721 const char *username = NULL;
722 NC_TLS_CTN_MAPTYPE map_type = 0;
723 ASN1_TIME *last_update = NULL, *next_update = NULL;
724
Michal Vasko6d292992016-01-18 09:42:38 +0100725 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100726 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100727 if (!session) {
728 ERRINT;
729 return 0;
730 }
731
Michal Vasko2cc4c682016-03-01 09:16:48 +0100732 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100733
734 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200735 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100736 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100737 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200738 X509_free(session->opts.server.client_cert);
739 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100740 }
741 sk_X509_pop_free(cert_stack, X509_free);
742 }
743
744 /* standard certificate verification failed, so a trusted client cert must match to continue */
745 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200746 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100747 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
748 if (cert_stack) {
749 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200750 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100751 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
752 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05532772021-06-03 12:12:38 +0200753 VRB(session, "Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200754 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100755 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
756 sk_X509_pop_free(cert_stack, X509_free);
757 return 1;
758 }
759 }
760 sk_X509_pop_free(cert_stack, X509_free);
761 }
762
Michal Vasko05532772021-06-03 12:12:38 +0200763 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 +0100764 return 0;
765 }
766
767 /* print cert verify info */
768 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200769 VRB(session, "Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100770
771 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
772 subject = X509_get_subject_name(cert);
773 issuer = X509_get_issuer_name(cert);
774
775 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200776 VRB(session, "Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100777 OPENSSL_free(cp);
778 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200779 VRB(session, "Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100780 OPENSSL_free(cp);
781
782 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100783 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100784 /* try to retrieve a CRL corresponding to the _subject_ of
785 * the current certificate in order to verify it's integrity */
786 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100787 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700788 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100789 X509_STORE_CTX_cleanup(&store_ctx);
790 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200791 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100792 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200793 VRB(session, "Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100794 OPENSSL_free(cp);
795
796 last_update = X509_CRL_get_lastUpdate(crl);
797 next_update = X509_CRL_get_nextUpdate(crl);
798 cp = asn1time_to_str(last_update);
Michal Vasko05532772021-06-03 12:12:38 +0200799 VRB(session, "Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100800 free(cp);
801 cp = asn1time_to_str(next_update);
Michal Vasko05532772021-06-03 12:12:38 +0200802 VRB(session, "Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100803 free(cp);
804
805 /* verify the signature on this CRL */
806 pubkey = X509_get_pubkey(cert);
807 if (X509_CRL_verify(crl, pubkey) <= 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200808 ERR(session, "Cert verify CRL: invalid signature.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100809 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
810 X509_OBJECT_free_contents(&obj);
811 if (pubkey) {
812 EVP_PKEY_free(pubkey);
813 }
814 return 0;
815 }
816 if (pubkey) {
817 EVP_PKEY_free(pubkey);
818 }
819
820 /* check date of CRL to make sure it's not expired */
821 if (!next_update) {
Michal Vasko05532772021-06-03 12:12:38 +0200822 ERR(session, "Cert verify CRL: invalid nextUpdate field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100823 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
824 X509_OBJECT_free_contents(&obj);
825 return 0;
826 }
827 if (X509_cmp_current_time(next_update) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200828 ERR(session, "Cert verify CRL: expired - revoking all certificates.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100829 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
830 X509_OBJECT_free_contents(&obj);
831 return 0;
832 }
833 X509_OBJECT_free_contents(&obj);
834 }
835
836 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100837 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100838 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100839 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700840 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100841 X509_STORE_CTX_cleanup(&store_ctx);
842 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200843 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100844 /* check if the current certificate is revoked by this CRL */
845 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
846 for (i = 0; i < n; i++) {
847 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
848 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
849 serial = ASN1_INTEGER_get(revoked->serialNumber);
850 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200851 ERR(session, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
852 serial, serial, cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100853 OPENSSL_free(cp);
854 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
855 X509_OBJECT_free_contents(&obj);
856 return 0;
857 }
858 }
859 X509_OBJECT_free_contents(&obj);
860 }
861 }
862
863 /* cert-to-name already successful */
864 if (session->username) {
865 return 1;
866 }
867
868 /* cert-to-name */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100869 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100870
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100871 if (rc) {
872 if (rc == -1) {
873 /* fatal error */
874 depth = 0;
875 }
876 /* rc == 1 is a normal CTN fail (no match found) */
877 goto fail;
878 }
879
880 /* cert-to-name match, now to extract the specific field from the peer cert */
881 if (map_type == NC_TLS_CTN_SPECIFIED) {
Michal Vasko65e08c82021-03-11 10:06:24 +0100882 lydict_insert(server_opts.ctx, username, 0, &session->username);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100883 } else {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200884 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100885 if (rc) {
886 if (rc == -1) {
887 depth = 0;
888 }
889 goto fail;
890 }
Michal Vasko65e08c82021-03-11 10:06:24 +0100891 lydict_insert_zc(server_opts.ctx, cp, &session->username);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100892 }
893
Michal Vasko05532772021-06-03 12:12:38 +0200894 VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100895
896 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200897 VRB(session, "Cert verify: user verify callback revoked authorization.");
Michal Vasko7060bcf2016-11-28 14:48:11 +0100898 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
899 return 0;
900 }
901
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100902 return 1;
903
904fail:
905 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200906 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 +0100907 return 1;
908 }
909
Michal Vasko05532772021-06-03 12:12:38 +0200910 VRB(session, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100911 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
912 return 0;
913}
914
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100915#endif
916
Michal Vasko3031aae2016-01-27 16:07:18 +0100917static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100918nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100919{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100920 if (!name) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100921 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100922 free(opts->server_cert);
Michal Vaskoa8748792016-11-22 14:34:26 +0100923 }
924 opts->server_cert = NULL;
925 return 0;
926 }
927
Michal Vaskoe2713da2016-08-22 16:06:40 +0200928 if (opts->server_cert) {
Michal Vasko93224072021-11-09 12:14:28 +0100929 free(opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100930 }
Michal Vasko93224072021-11-09 12:14:28 +0100931 opts->server_cert = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100932
933 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100934}
935
936API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100937nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100938{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100939 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 struct nc_endpt *endpt;
941
Michal Vasko2e6defd2016-10-07 15:48:15 +0200942 if (!endpt_name) {
943 ERRARG("endpt_name");
944 return -1;
945 }
946
Michal Vasko51e514d2016-02-02 15:51:52 +0100947 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100948 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100949 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100950 return -1;
951 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100952 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100953 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100954 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100955
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100956 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100957}
958
959API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200960nc_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 +0100961{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100962 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200963 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200964 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200965
966 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200967 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
968 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200969 return -1;
970 }
971
Michal Vaskoadf30f02019-06-24 09:34:47 +0200972 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200973
974 /* UNLOCK */
975 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100976
977 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100978}
979
Michal Vasko4c1fb492017-01-30 14:31:07 +0100980API void
981nc_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 +0200982 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
983 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100984{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100985 if (!cert_clb) {
986 ERRARG("cert_clb");
987 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100988 }
989
Michal Vasko4c1fb492017-01-30 14:31:07 +0100990 server_opts.server_cert_clb = cert_clb;
991 server_opts.server_cert_data = user_data;
992 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100993}
994
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500995API void
996nc_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 +0200997 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 -0500998{
999 if (!cert_chain_clb) {
1000 ERRARG("cert_chain_clb");
1001 return;
1002 }
1003
1004 server_opts.server_cert_chain_clb = cert_chain_clb;
1005 server_opts.server_cert_chain_data = user_data;
1006 server_opts.server_cert_chain_data_free = free_user_data;
1007}
1008
Michal Vaskoc61c4492016-01-25 11:13:34 +01001009static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001010nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001011{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001012 if (!name) {
1013 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014 return -1;
1015 }
1016
Michal Vasko4c1fb492017-01-30 14:31:07 +01001017 ++opts->trusted_cert_list_count;
Michal Vasko77367452021-02-16 16:32:18 +01001018 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists,
1019 opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001020 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001021 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001022 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001023 }
Michal Vasko93224072021-11-09 12:14:28 +01001024 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001025
1026 return 0;
1027}
1028
1029API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001030nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001032 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001033 struct nc_endpt *endpt;
1034
Michal Vasko2e6defd2016-10-07 15:48:15 +02001035 if (!endpt_name) {
1036 ERRARG("endpt_name");
1037 return -1;
1038 }
1039
Michal Vasko51e514d2016-02-02 15:51:52 +01001040 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001041 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001042 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001043 return -1;
1044 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001045 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001046 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001047 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001048
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001049 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001050}
1051
1052API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001053nc_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 +01001054{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001055 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001056 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001057 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001058
1059 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001060 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1061 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001062 return -1;
1063 }
1064
Michal Vaskoadf30f02019-06-24 09:34:47 +02001065 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001066
1067 /* UNLOCK */
1068 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001069
1070 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001071}
1072
Michal Vasko4c1fb492017-01-30 14:31:07 +01001073API void
1074nc_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 +02001075 int *cert_path_count, char ***cert_data, int *cert_data_count),
1076 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001077{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001078 if (!cert_list_clb) {
1079 ERRARG("cert_list_clb");
1080 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001081 }
1082
Michal Vasko4c1fb492017-01-30 14:31:07 +01001083 server_opts.trusted_cert_list_clb = cert_list_clb;
1084 server_opts.trusted_cert_list_data = user_data;
1085 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001086}
1087
1088static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001089nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001090{
1091 uint16_t i;
1092
1093 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001094 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001095 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001096 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001097 free(opts->trusted_cert_lists);
1098 opts->trusted_cert_lists = NULL;
1099 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001100 return 0;
1101 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001102 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1103 if (!strcmp(opts->trusted_cert_lists[i], name)) {
Michal Vasko93224072021-11-09 12:14:28 +01001104 free(opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001105
Michal Vasko4c1fb492017-01-30 14:31:07 +01001106 --opts->trusted_cert_list_count;
1107 if (i < opts->trusted_cert_list_count - 1) {
1108 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1109 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001110 }
1111 return 0;
1112 }
1113 }
1114 }
1115
Michal Vasko05532772021-06-03 12:12:38 +02001116 ERR(NULL, "Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001117 return -1;
1118}
1119
1120API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001121nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001122{
1123 int ret;
1124 struct nc_endpt *endpt;
1125
Michal Vasko2e6defd2016-10-07 15:48:15 +02001126 if (!endpt_name) {
1127 ERRARG("endpt_name");
1128 return -1;
1129 }
1130
Michal Vaskoe2713da2016-08-22 16:06:40 +02001131 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001132 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001133 if (!endpt) {
1134 return -1;
1135 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001136 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001137 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001138 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001139
1140 return ret;
1141}
1142
1143API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001144nc_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 +02001145{
1146 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001147 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001148 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001149
1150 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001151 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1152 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001153 return -1;
1154 }
1155
Michal Vaskoadf30f02019-06-24 09:34:47 +02001156 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001157
1158 /* UNLOCK */
1159 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001160
1161 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001162}
1163
1164static int
Michal Vasko96830e32016-02-01 10:54:18 +01001165nc_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 +01001166{
Michal Vasko96830e32016-02-01 10:54:18 +01001167 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001168 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001169 return -1;
1170 }
1171
Michal Vasko96830e32016-02-01 10:54:18 +01001172 if (ca_file) {
Michal Vasko93224072021-11-09 12:14:28 +01001173 free(opts->trusted_ca_file);
1174 opts->trusted_ca_file = strdup(ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001175 }
1176
Michal Vasko96830e32016-02-01 10:54:18 +01001177 if (ca_dir) {
Michal Vasko93224072021-11-09 12:14:28 +01001178 free(opts->trusted_ca_dir);
1179 opts->trusted_ca_dir = strdup(ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001180 }
1181
1182 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001183}
1184
Michal Vaskoc61c4492016-01-25 11:13:34 +01001185API int
Michal Vasko96830e32016-02-01 10:54:18 +01001186nc_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 +01001187{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001188 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001189 struct nc_endpt *endpt;
1190
Michal Vasko2e6defd2016-10-07 15:48:15 +02001191 if (!endpt_name) {
1192 ERRARG("endpt_name");
1193 return -1;
1194 }
1195
Michal Vasko51e514d2016-02-02 15:51:52 +01001196 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001197 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001198 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001199 return -1;
1200 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001201 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001202 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001203 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001204
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001205 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001206}
1207
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001208API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001209nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file,
1210 const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001211{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001212 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001213 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001214 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001215
1216 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001217 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1218 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001219 return -1;
1220 }
1221
Michal Vaskoadf30f02019-06-24 09:34:47 +02001222 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001223
1224 /* UNLOCK */
1225 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001226
1227 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001228}
1229
Michal Vaskoc61c4492016-01-25 11:13:34 +01001230static int
Michal Vasko96830e32016-02-01 10:54:18 +01001231nc_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 +01001232{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001233 X509_LOOKUP *lookup;
1234
Michal Vasko96830e32016-02-01 10:54:18 +01001235 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001236 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001237 return -1;
1238 }
1239
Michal Vaskoc61c4492016-01-25 11:13:34 +01001240 if (!opts->crl_store) {
1241 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001242 }
1243
Michal Vasko96830e32016-02-01 10:54:18 +01001244 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001245 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001246 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001247 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001248 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001249 }
1250
Michal Vasko96830e32016-02-01 10:54:18 +01001251 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001252 ERR(NULL, "Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001253 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001254 }
1255 }
1256
Michal Vasko96830e32016-02-01 10:54:18 +01001257 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001258 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001259 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001260 ERR(NULL, "Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001261 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001262 }
1263
Michal Vasko96830e32016-02-01 10:54:18 +01001264 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001265 ERR(NULL, "Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001266 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001267 }
1268 }
1269
1270 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001271
1272fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001273 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001274}
1275
Michal Vaskoc61c4492016-01-25 11:13:34 +01001276API int
Michal Vasko96830e32016-02-01 10:54:18 +01001277nc_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 +01001278{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001279 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001280 struct nc_endpt *endpt;
1281
Michal Vasko2e6defd2016-10-07 15:48:15 +02001282 if (!endpt_name) {
1283 ERRARG("endpt_name");
1284 return -1;
1285 }
1286
Michal Vasko51e514d2016-02-02 15:51:52 +01001287 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001288 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001289 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001290 return -1;
1291 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001292 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001293 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001294 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001295
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001296 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001297}
1298
1299API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001300nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file,
1301 const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001302{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001303 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001304 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001305 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001306
1307 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001308 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1309 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001310 return -1;
1311 }
1312
Michal Vaskoadf30f02019-06-24 09:34:47 +02001313 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001314
1315 /* UNLOCK */
1316 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001317
1318 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001319}
1320
1321static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001322nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001323{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001324 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001325 return;
1326 }
1327
1328 X509_STORE_free(opts->crl_store);
1329 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001330}
1331
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001332API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001333nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001334{
Michal Vasko3031aae2016-01-27 16:07:18 +01001335 struct nc_endpt *endpt;
1336
Michal Vasko2e6defd2016-10-07 15:48:15 +02001337 if (!endpt_name) {
1338 ERRARG("endpt_name");
1339 return;
1340 }
1341
Michal Vasko51e514d2016-02-02 15:51:52 +01001342 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001343 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001344 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001345 return;
1346 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001347 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001348 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001349 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001350}
1351
Michal Vaskoc61c4492016-01-25 11:13:34 +01001352API void
Michal Vaskoadf30f02019-06-24 09:34:47 +02001353nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001354{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001355 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001356 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001357
1358 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001359 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1360 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001361 return;
1362 }
1363
Michal Vaskoadf30f02019-06-24 09:34:47 +02001364 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001365
1366 /* UNLOCK */
1367 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001368}
1369
1370static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001371nc_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 +02001372 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001373{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001374 struct nc_ctn *ctn, *new;
1375
Michal Vaskoc61c4492016-01-25 11:13:34 +01001376 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001377 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001378 opts->ctn = new = calloc(1, sizeof *new);
1379 if (!new) {
1380 ERRMEM;
1381 return -1;
1382 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001383 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001384 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001385 new = calloc(1, sizeof *new);
1386 if (!new) {
1387 ERRMEM;
1388 return -1;
1389 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001390 new->next = opts->ctn;
1391 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001392 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001393 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {}
Michal Vasko276f9d92017-02-02 11:15:55 +01001394 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001395 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001396 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001397 } else {
1398 /* insert after ctn */
1399 new = calloc(1, sizeof *new);
1400 if (!new) {
1401 ERRMEM;
1402 return -1;
1403 }
1404 new->next = ctn->next;
1405 ctn->next = new;
1406 }
1407 }
1408
1409 new->id = id;
1410 if (fingerprint) {
Michal Vasko93224072021-11-09 12:14:28 +01001411 free(new->fingerprint);
1412 new->fingerprint = strdup(fingerprint);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001413 }
1414 if (map_type) {
1415 new->map_type = map_type;
1416 }
1417 if (name) {
Michal Vasko93224072021-11-09 12:14:28 +01001418 free(new->name);
1419 new->name = strdup(name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001420 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001421
1422 return 0;
1423}
1424
1425API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001426nc_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 +02001427 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001428{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001429 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001430 struct nc_endpt *endpt;
1431
Michal Vasko2e6defd2016-10-07 15:48:15 +02001432 if (!endpt_name) {
1433 ERRARG("endpt_name");
1434 return -1;
1435 }
1436
Michal Vasko51e514d2016-02-02 15:51:52 +01001437 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001438 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001439 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001440 return -1;
1441 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001442 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001443 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001444 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001445
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001446 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001447}
1448
1449API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001450nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id,
1451 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001452{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001453 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001454 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001455 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001456
1457 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001458 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1459 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001460 return -1;
1461 }
1462
Michal Vaskoadf30f02019-06-24 09:34:47 +02001463 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001464
1465 /* UNLOCK */
1466 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001467
1468 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001469}
1470
1471static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001472nc_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 +02001473 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001474{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001475 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001476 int ret = -1;
1477
Michal Vasko1a38c862016-01-15 15:50:07 +01001478 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001479 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001480 while (ctn) {
Michal Vasko93224072021-11-09 12:14:28 +01001481 free(ctn->fingerprint);
1482 free(ctn->name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001483
1484 next = ctn->next;
1485 free(ctn);
1486 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001487
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001488 ret = 0;
1489 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001490 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001491 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001492 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001493 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001494 while (ctn) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001495 if (((id < 0) || (ctn->id == id)) &&
1496 (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) &&
1497 (!map_type || (ctn->map_type == map_type)) &&
1498 (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko93224072021-11-09 12:14:28 +01001499 free(ctn->fingerprint);
1500 free(ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001501
Michal Vasko5e3f3392016-01-20 11:13:01 +01001502 if (prev) {
1503 prev->next = ctn->next;
1504 next = ctn->next;
1505 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001506 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001507 next = ctn->next;
1508 }
1509 free(ctn);
1510 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001511
1512 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001513 } else {
1514 prev = ctn;
1515 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001516 }
1517 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001518 }
1519
1520 return ret;
1521}
1522
Michal Vaskoc61c4492016-01-25 11:13:34 +01001523API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001524nc_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 +02001525 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001526{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001527 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001528 struct nc_endpt *endpt;
1529
Michal Vasko2e6defd2016-10-07 15:48:15 +02001530 if (!endpt_name) {
1531 ERRARG("endpt_name");
1532 return -1;
1533 }
1534
Michal Vasko51e514d2016-02-02 15:51:52 +01001535 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001536 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001537 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001538 return -1;
1539 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001540 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001541 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001542 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001543
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001544 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001545}
1546
1547API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001548nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id,
1549 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001550{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001551 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001552 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001553 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001554
1555 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001556 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1557 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001558 return -1;
1559 }
1560
Michal Vaskoadf30f02019-06-24 09:34:47 +02001561 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001562
1563 /* UNLOCK */
1564 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001565
1566 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001567}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001568
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001569static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001570nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001571 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001572{
1573 struct nc_ctn *ctn;
1574 int ret = -1;
1575
1576 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1577 if (id && *id && (*id != ctn->id)) {
1578 continue;
1579 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001580 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001581 continue;
1582 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001583 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001584 continue;
1585 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001586 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001587 continue;
1588 }
1589
1590 /* first match, good enough */
1591 if (id && !(*id)) {
1592 *id = ctn->id;
1593 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001594 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001595 *fingerprint = strdup(ctn->fingerprint);
1596 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001597 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001598 *map_type = ctn->map_type;
1599 }
Adam Richterd44680e2019-06-15 13:16:16 -07001600 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001601 *name = strdup(ctn->name);
1602 }
1603
1604 ret = 0;
1605 break;
1606 }
1607
1608 return ret;
1609}
1610
1611API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001612nc_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 +02001613 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001614{
1615 int ret;
1616 struct nc_endpt *endpt;
1617
1618 if (!endpt_name) {
1619 ERRARG("endpt_name");
1620 return -1;
1621 }
1622
1623 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001624 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001625 if (!endpt) {
1626 return -1;
1627 }
1628 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1629 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001630 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001631
1632 return ret;
1633}
1634
1635API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001636nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
1637 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001638{
1639 int ret;
1640 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001641 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001642
1643 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001644 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1645 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001646 return -1;
1647 }
1648
Michal Vaskoadf30f02019-06-24 09:34:47 +02001649 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001650
1651 /* UNLOCK */
1652 nc_server_ch_client_unlock(client);
1653
1654 return ret;
1655}
1656
Michal Vasko709598e2016-11-28 14:48:32 +01001657API const X509 *
1658nc_session_get_client_cert(const struct nc_session *session)
1659{
1660 if (!session || (session->side != NC_SERVER)) {
1661 ERRARG("session");
1662 return NULL;
1663 }
1664
1665 return session->opts.server.client_cert;
1666}
1667
Michal Vasko7060bcf2016-11-28 14:48:11 +01001668API void
1669nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1670{
1671 server_opts.user_verify_clb = verify_clb;
1672}
1673
Michal Vasko3031aae2016-01-27 16:07:18 +01001674void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001675nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001676{
Michal Vasko93224072021-11-09 12:14:28 +01001677 free(opts->server_cert);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001678 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vasko93224072021-11-09 12:14:28 +01001679 free(opts->trusted_ca_file);
1680 free(opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001681 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001682 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001683}
Michal Vasko9e036d52016-01-08 10:49:26 +01001684
Michal Vasko6d292992016-01-18 09:42:38 +01001685static void
1686nc_tls_make_verify_key(void)
1687{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001688 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001689}
1690
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001691static X509 *
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001692tls_load_cert(const char *cert_path, const char *cert_data)
1693{
1694 X509 *cert;
1695
1696 if (cert_path) {
1697 cert = pem_to_cert(cert_path);
1698 } else {
1699 cert = base64der_to_cert(cert_data);
1700 }
1701
1702 if (!cert) {
1703 if (cert_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001704 ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001705 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001706 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001707 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001708 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001709 }
1710 }
1711 return cert;
1712}
1713
1714static int
1715nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1716{
1717 char **cert_paths = NULL, **cert_data = NULL;
1718 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1719 X509 *cert = NULL;
1720
1721 if (!server_opts.server_cert_chain_clb) {
1722 /* This is optional, so return OK */
1723 return 0;
1724 }
1725
1726 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001727 &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001728 ERR(NULL, "Server certificate chain callback failed.");
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001729 return -1;
1730 }
1731
1732 for (i = 0; i < cert_path_count; ++i) {
1733 cert = tls_load_cert(cert_paths[i], NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001734 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001735 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001736 ret = -1;
1737 goto cleanup;
1738 }
1739 }
1740
1741 for (i = 0; i < cert_data_count; ++i) {
1742 cert = tls_load_cert(NULL, cert_data[i]);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001743 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001744 ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001745 ret = -1;
1746 goto cleanup;
1747 }
1748 }
1749cleanup:
1750 for (i = 0; i < cert_path_count; ++i) {
1751 free(cert_paths[i]);
1752 }
1753 free(cert_paths);
1754 for (i = 0; i < cert_data_count; ++i) {
1755 free(cert_data[i]);
1756 }
1757 free(cert_data);
1758 /* cert is owned by the SSL_CTX */
1759
1760 return ret;
1761}
1762
Michal Vasko4c1fb492017-01-30 14:31:07 +01001763static int
1764nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1765{
1766 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001767 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001768 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001769 X509 *cert = NULL;
1770 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001771
1772 if (!cert_name) {
Michal Vasko05532772021-06-03 12:12:38 +02001773 ERR(NULL, "Server certificate not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001774 return -1;
1775 } else if (!server_opts.server_cert_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001776 ERR(NULL, "Callback for retrieving the server certificate is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001777 return -1;
1778 }
1779
1780 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 +02001781 &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001782 ERR(NULL, "Server certificate callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001783 return -1;
1784 }
1785
1786 /* load the certificate */
1787 if (cert_path) {
1788 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001789 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001790 ret = -1;
1791 goto cleanup;
1792 }
1793 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001794 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001795 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001796 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001797 ret = -1;
1798 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001799 }
1800 }
1801
1802 /* load the private key */
1803 if (privkey_path) {
1804 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001805 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001806 ret = -1;
1807 goto cleanup;
1808 }
1809 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001810 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001811 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001812 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001813 ret = -1;
1814 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001815 }
1816 }
1817
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001818 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1819
Michal Vasko4c1fb492017-01-30 14:31:07 +01001820cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001821 X509_free(cert);
1822 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001823 free(cert_path);
1824 free(cert_data);
1825 free(privkey_path);
1826 free(privkey_data);
1827 return ret;
1828}
1829
1830static void
1831tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1832{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001833 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001834
Michal Vasko4c1fb492017-01-30 14:31:07 +01001835 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001836 return;
1837 }
1838
1839 /* add the trusted certificate */
1840 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001841 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001842 X509_free(cert);
1843 return;
1844 }
1845 X509_free(cert);
1846}
1847
1848static int
Michal Vasko93224072021-11-09 12:14:28 +01001849nc_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 +01001850{
1851 uint16_t i;
1852 int j;
1853 char **cert_paths, **cert_data;
1854 int cert_path_count, cert_data_count;
1855
1856 if (!server_opts.trusted_cert_list_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001857 ERR(NULL, "Callback for retrieving trusted certificate lists is not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001858 return -1;
1859 }
1860
1861 for (i = 0; i < trusted_cert_list_count; ++i) {
1862 cert_paths = cert_data = NULL;
1863 cert_path_count = cert_data_count = 0;
1864 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001865 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko05532772021-06-03 12:12:38 +02001866 ERR(NULL, "Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001867 return -1;
1868 }
1869
1870 for (j = 0; j < cert_path_count; ++j) {
1871 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1872 free(cert_paths[j]);
1873 }
1874 free(cert_paths);
1875
1876 for (j = 0; j < cert_data_count; ++j) {
1877 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1878 free(cert_data[j]);
1879 }
1880 free(cert_data);
1881 }
1882
1883 return 0;
1884}
1885
Michal Vasko3031aae2016-01-27 16:07:18 +01001886int
Michal Vasko0190bc32016-03-02 15:47:49 +01001887nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001888{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001889 X509_STORE *cert_store;
1890 SSL_CTX *tls_ctx;
1891 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001892 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001893 int ret;
1894 struct timespec ts_timeout, ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001895
Michal Vasko2cc4c682016-03-01 09:16:48 +01001896 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001897
Michal Vaskoe2713da2016-08-22 16:06:40 +02001898 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001899#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1900 tls_ctx = SSL_CTX_new(TLS_server_method());
1901#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001902 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001903#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001904 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001905 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001906 goto error;
1907 }
1908 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 +01001909 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001910 goto error;
1911 }
1912
1913 /* X509_STORE, managed (freed) with the context */
1914 cert_store = X509_STORE_new();
1915 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1916
Michal Vasko4c1fb492017-01-30 14:31:07 +01001917 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1918 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001919 }
1920
1921 if (opts->trusted_ca_file) {
1922 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1923 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001924 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001925 goto error;
1926 }
1927
1928 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001929 ERR(session, "Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001930 goto error;
1931 }
1932 }
1933
1934 if (opts->trusted_ca_dir) {
1935 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1936 if (!lookup) {
Michal Vasko05532772021-06-03 12:12:38 +02001937 ERR(session, "Failed to add a lookup method.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001938 goto error;
1939 }
1940
1941 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001942 ERR(session, "Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001943 goto error;
1944 }
1945 }
1946
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001947 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001948 session->ti.tls = SSL_new(tls_ctx);
1949
Michal Vasko4c1fb492017-01-30 14:31:07 +01001950 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001951 SSL_CTX_free(tls_ctx);
1952 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001953
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001954 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001955 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001956 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001957 }
1958
1959 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001960 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001961 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1962
Michal Vasko6d292992016-01-18 09:42:38 +01001963 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001964 pthread_once(&verify_once, nc_tls_make_verify_key);
1965 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001966
Michal Vasko36c7be82017-02-22 13:37:59 +01001967 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001968 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001969 nc_addtimespec(&ts_timeout, timeout);
1970 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001971 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1972 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001973 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001974 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001975 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001976 ERR(session, "SSL_accept timeout.");
Michal Vasko36c7be82017-02-22 13:37:59 +01001977 return 0;
1978 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001979 }
1980 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001981
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001982 if (ret != 1) {
1983 switch (SSL_get_error(session->ti.tls, ret)) {
1984 case SSL_ERROR_SYSCALL:
Michal Vasko05532772021-06-03 12:12:38 +02001985 ERR(session, "SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001986 break;
1987 case SSL_ERROR_SSL:
Michal Vasko05532772021-06-03 12:12:38 +02001988 ERR(session, "SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001989 break;
1990 default:
Michal Vasko05532772021-06-03 12:12:38 +02001991 ERR(session, "SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001992 break;
1993 }
1994 return -1;
1995 }
1996
Michal Vasko1a38c862016-01-15 15:50:07 +01001997 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001998
1999error:
2000 if (sock > -1) {
2001 close(sock);
2002 }
2003 SSL_CTX_free(tls_ctx);
2004 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002005}