blob: 95d0ae2b4f6823262b72c518af3f03689d7a38ca [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 Vaskod083db62016-01-19 10:31:29 +0100204 WRN("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 Vaskod083db62016-01-19 10:31:29 +0100222 WRN("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 Vaskod083db62016-01-19 10:31:29 +0100282 WRN("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
288 if (i < san_count) {
289 switch (map_type) {
290 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100291 WRN("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 Vaskod083db62016-01-19 10:31:29 +0100294 WRN("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 Vaskod083db62016-01-19 10:31:29 +0100297 WRN("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 Vaskod083db62016-01-19 10:31:29 +0100300 WRN("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)) {
336 VRB("Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
337 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 Vaskod083db62016-01-19 10:31:29 +0100344 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100353 VRB("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 Vaskod083db62016-01-19 10:31:29 +0100365 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100374 VRB("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 Vaskod083db62016-01-19 10:31:29 +0100386 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100395 VRB("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 Vaskod083db62016-01-19 10:31:29 +0100407 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100416 VRB("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 Vaskod083db62016-01-19 10:31:29 +0100428 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100437 VRB("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 Vaskod083db62016-01-19 10:31:29 +0100449 ERR("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 Vaskod083db62016-01-19 10:31:29 +0100458 VRB("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 Vasko5e3f3392016-01-20 11:13:01 +0100468 WRN("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 */
537 VRB("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
547 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
548 return 0;
549 }
550
551 /* print cert verify info */
552 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
553 VRB("Cert verify: depth %d.", depth);
554
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);
560 VRB("Cert verify: subject: %s.", cp);
561 OPENSSL_free(cp);
562 cp = X509_NAME_oneline(issuer, NULL, 0);
563 VRB("Cert verify: issuer: %s.", cp);
564 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);
578 VRB("Cert verify CRL: issuer: %s.", cp);
579 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);
584 VRB("Cert verify CRL: last update: %s.", cp);
585 free(cp);
586 cp = asn1time_to_str(next_update);
587 VRB("Cert verify CRL: next update: %s.", cp);
588 free(cp);
589
590 /* verify the signature on this CRL */
591 pubkey = X509_get_pubkey(cert);
592 if (X509_CRL_verify(crl, pubkey) <= 0) {
593 ERR("Cert verify CRL: invalid signature.");
594 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) {
607 ERR("Cert verify CRL: invalid nextUpdate field.");
608 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) {
613 ERR("Cert verify CRL: expired - revoking all certificates.");
614 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);
637 ERR("Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", serial, serial, cp);
638 OPENSSL_free(cp);
639 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
640 X509_OBJECT_free(obj);
641 return 0;
642 }
643 }
644 X509_OBJECT_free(obj);
645 }
646 }
647
648 /* cert-to-name already successful */
649 if (session->username) {
650 return 1;
651 }
652
653 /* cert-to-name */
654 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
655
656 if (rc) {
657 if (rc == -1) {
658 /* fatal error */
659 depth = 0;
660 }
661 /* rc == 1 is a normal CTN fail (no match found) */
662 goto fail;
663 }
664
665 /* cert-to-name match, now to extract the specific field from the peer cert */
666 if (map_type == NC_TLS_CTN_SPECIFIED) {
Michal Vasko77367452021-02-16 16:32:18 +0100667 lydict_insert(server_opts.ctx, username, 0, &session->username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100668 } else {
669 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
670 if (rc) {
671 if (rc == -1) {
672 depth = 0;
673 }
674 goto fail;
675 }
Michal Vasko77367452021-02-16 16:32:18 +0100676 lydict_insert_zc(server_opts.ctx, cp, &session->username);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100677 }
678
679 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
680
681 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
682 VRB("Cert verify: user verify callback revoked authorization.");
683 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
684 return 0;
685 }
686
687 return 1;
688
689fail:
690 if (depth > 0) {
691 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
692 return 1;
693 }
694
695 VRB("Cert-to-name unsuccessful, dropping the new client.");
696 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
697 return 0;
698}
699
700#else
701
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100702static int
703nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
704{
705 X509_STORE_CTX store_ctx;
706 X509_OBJECT obj;
707 X509_NAME *subject;
708 X509_NAME *issuer;
709 X509 *cert;
710 X509_CRL *crl;
711 X509_REVOKED *revoked;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200712
713 STACK_OF(X509) * cert_stack;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100714 EVP_PKEY *pubkey;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200715 struct nc_session *session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100716 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100717 long serial;
718 int i, n, rc, depth;
719 char *cp;
720 const char *username = NULL;
721 NC_TLS_CTN_MAPTYPE map_type = 0;
722 ASN1_TIME *last_update = NULL, *next_update = NULL;
723
Michal Vasko6d292992016-01-18 09:42:38 +0100724 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100725 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100726 if (!session) {
727 ERRINT;
728 return 0;
729 }
730
Michal Vasko2cc4c682016-03-01 09:16:48 +0100731 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100732
733 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200734 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100735 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100736 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200737 X509_free(session->opts.server.client_cert);
738 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100739 }
740 sk_X509_pop_free(cert_stack, X509_free);
741 }
742
743 /* standard certificate verification failed, so a trusted client cert must match to continue */
744 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200745 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100746 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
747 if (cert_stack) {
748 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200749 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100750 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
751 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100752 VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200753 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100754 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
755 sk_X509_pop_free(cert_stack, X509_free);
756 return 1;
757 }
758 }
759 sk_X509_pop_free(cert_stack, X509_free);
760 }
761
762 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
763 return 0;
764 }
765
766 /* print cert verify info */
767 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vaskod083db62016-01-19 10:31:29 +0100768 VRB("Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100769
770 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
771 subject = X509_get_subject_name(cert);
772 issuer = X509_get_issuer_name(cert);
773
774 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100775 VRB("Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100776 OPENSSL_free(cp);
777 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100778 VRB("Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100779 OPENSSL_free(cp);
780
781 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100782 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100783 /* try to retrieve a CRL corresponding to the _subject_ of
784 * the current certificate in order to verify it's integrity */
785 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100786 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700787 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100788 X509_STORE_CTX_cleanup(&store_ctx);
789 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200790 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100791 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100792 VRB("Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100793 OPENSSL_free(cp);
794
795 last_update = X509_CRL_get_lastUpdate(crl);
796 next_update = X509_CRL_get_nextUpdate(crl);
797 cp = asn1time_to_str(last_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100798 VRB("Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100799 free(cp);
800 cp = asn1time_to_str(next_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100801 VRB("Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100802 free(cp);
803
804 /* verify the signature on this CRL */
805 pubkey = X509_get_pubkey(cert);
806 if (X509_CRL_verify(crl, pubkey) <= 0) {
807 ERR("Cert verify CRL: invalid signature.");
808 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
809 X509_OBJECT_free_contents(&obj);
810 if (pubkey) {
811 EVP_PKEY_free(pubkey);
812 }
813 return 0;
814 }
815 if (pubkey) {
816 EVP_PKEY_free(pubkey);
817 }
818
819 /* check date of CRL to make sure it's not expired */
820 if (!next_update) {
821 ERR("Cert verify CRL: invalid nextUpdate field.");
822 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
823 X509_OBJECT_free_contents(&obj);
824 return 0;
825 }
826 if (X509_cmp_current_time(next_update) < 0) {
827 ERR("Cert verify CRL: expired - revoking all certificates.");
828 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
829 X509_OBJECT_free_contents(&obj);
830 return 0;
831 }
832 X509_OBJECT_free_contents(&obj);
833 }
834
835 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100836 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100837 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100838 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700839 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100840 X509_STORE_CTX_cleanup(&store_ctx);
841 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200842 if ((rc > 0) && crl) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100843 /* check if the current certificate is revoked by this CRL */
844 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
845 for (i = 0; i < n; i++) {
846 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
847 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
848 serial = ASN1_INTEGER_get(revoked->serialNumber);
849 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100850 ERR("Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", serial, serial, cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100851 OPENSSL_free(cp);
852 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
853 X509_OBJECT_free_contents(&obj);
854 return 0;
855 }
856 }
857 X509_OBJECT_free_contents(&obj);
858 }
859 }
860
861 /* cert-to-name already successful */
862 if (session->username) {
863 return 1;
864 }
865
866 /* cert-to-name */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100867 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100868
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100869 if (rc) {
870 if (rc == -1) {
871 /* fatal error */
872 depth = 0;
873 }
874 /* rc == 1 is a normal CTN fail (no match found) */
875 goto fail;
876 }
877
878 /* cert-to-name match, now to extract the specific field from the peer cert */
879 if (map_type == NC_TLS_CTN_SPECIFIED) {
Michal Vasko65e08c82021-03-11 10:06:24 +0100880 lydict_insert(server_opts.ctx, username, 0, &session->username);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100881 } else {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200882 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100883 if (rc) {
884 if (rc == -1) {
885 depth = 0;
886 }
887 goto fail;
888 }
Michal Vasko65e08c82021-03-11 10:06:24 +0100889 lydict_insert_zc(server_opts.ctx, cp, &session->username);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100890 }
891
892 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100893
894 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
895 VRB("Cert verify: user verify callback revoked authorization.");
896 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
897 return 0;
898 }
899
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100900 return 1;
901
902fail:
903 if (depth > 0) {
904 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
905 return 1;
906 }
907
908 VRB("Cert-to-name unsuccessful, dropping the new client.");
909 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
910 return 0;
911}
912
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100913#endif
914
Michal Vasko3031aae2016-01-27 16:07:18 +0100915static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100916nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100917{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100918 if (!name) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100919 if (opts->server_cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100920 lydict_remove(server_opts.ctx, opts->server_cert);
Michal Vaskoa8748792016-11-22 14:34:26 +0100921 }
922 opts->server_cert = NULL;
923 return 0;
924 }
925
Michal Vaskoe2713da2016-08-22 16:06:40 +0200926 if (opts->server_cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100927 lydict_remove(server_opts.ctx, opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100928 }
Michal Vasko77367452021-02-16 16:32:18 +0100929 lydict_insert(server_opts.ctx, name, 0, &opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100930
931 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100932}
933
934API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100935nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100936{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100937 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100938 struct nc_endpt *endpt;
939
Michal Vasko2e6defd2016-10-07 15:48:15 +0200940 if (!endpt_name) {
941 ERRARG("endpt_name");
942 return -1;
943 }
944
Michal Vasko51e514d2016-02-02 15:51:52 +0100945 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100946 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100947 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100948 return -1;
949 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100950 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100951 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100952 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100953
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100954 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100955}
956
957API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200958nc_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 +0100959{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100960 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200961 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200962 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200963
964 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200965 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
966 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200967 return -1;
968 }
969
Michal Vaskoadf30f02019-06-24 09:34:47 +0200970 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200971
972 /* UNLOCK */
973 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100974
975 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100976}
977
Michal Vasko4c1fb492017-01-30 14:31:07 +0100978API void
979nc_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 +0200980 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
981 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100982{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100983 if (!cert_clb) {
984 ERRARG("cert_clb");
985 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100986 }
987
Michal Vasko4c1fb492017-01-30 14:31:07 +0100988 server_opts.server_cert_clb = cert_clb;
989 server_opts.server_cert_data = user_data;
990 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100991}
992
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500993API void
994nc_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 +0200995 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 -0500996{
997 if (!cert_chain_clb) {
998 ERRARG("cert_chain_clb");
999 return;
1000 }
1001
1002 server_opts.server_cert_chain_clb = cert_chain_clb;
1003 server_opts.server_cert_chain_data = user_data;
1004 server_opts.server_cert_chain_data_free = free_user_data;
1005}
1006
Michal Vaskoc61c4492016-01-25 11:13:34 +01001007static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001008nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001009{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001010 if (!name) {
1011 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012 return -1;
1013 }
1014
Michal Vasko4c1fb492017-01-30 14:31:07 +01001015 ++opts->trusted_cert_list_count;
Michal Vasko77367452021-02-16 16:32:18 +01001016 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists,
1017 opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001018 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001019 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001020 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001021 }
Michal Vasko77367452021-02-16 16:32:18 +01001022 lydict_insert(server_opts.ctx, name, 0, &opts->trusted_cert_lists[opts->trusted_cert_list_count - 1]);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001023
1024 return 0;
1025}
1026
1027API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001028nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001029{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001030 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001031 struct nc_endpt *endpt;
1032
Michal Vasko2e6defd2016-10-07 15:48:15 +02001033 if (!endpt_name) {
1034 ERRARG("endpt_name");
1035 return -1;
1036 }
1037
Michal Vasko51e514d2016-02-02 15:51:52 +01001038 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001039 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001040 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001041 return -1;
1042 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001043 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001044 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001045 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001046
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001047 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001048}
1049
1050API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001051nc_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 +01001052{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001053 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001054 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001055 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001056
1057 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001058 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1059 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001060 return -1;
1061 }
1062
Michal Vaskoadf30f02019-06-24 09:34:47 +02001063 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001064
1065 /* UNLOCK */
1066 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001067
1068 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001069}
1070
Michal Vasko4c1fb492017-01-30 14:31:07 +01001071API void
1072nc_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 +02001073 int *cert_path_count, char ***cert_data, int *cert_data_count),
1074 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001075{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001076 if (!cert_list_clb) {
1077 ERRARG("cert_list_clb");
1078 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001079 }
1080
Michal Vasko4c1fb492017-01-30 14:31:07 +01001081 server_opts.trusted_cert_list_clb = cert_list_clb;
1082 server_opts.trusted_cert_list_data = user_data;
1083 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001084}
1085
1086static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001087nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001088{
1089 uint16_t i;
1090
1091 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001092 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1093 lydict_remove(server_opts.ctx, opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001094 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001095 free(opts->trusted_cert_lists);
1096 opts->trusted_cert_lists = NULL;
1097 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001098 return 0;
1099 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001100 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1101 if (!strcmp(opts->trusted_cert_lists[i], name)) {
1102 lydict_remove(server_opts.ctx, opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001103
Michal Vasko4c1fb492017-01-30 14:31:07 +01001104 --opts->trusted_cert_list_count;
1105 if (i < opts->trusted_cert_list_count - 1) {
1106 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1107 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001108 }
1109 return 0;
1110 }
1111 }
1112 }
1113
Michal Vasko4c1fb492017-01-30 14:31:07 +01001114 ERR("Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001115 return -1;
1116}
1117
1118API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001119nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001120{
1121 int ret;
1122 struct nc_endpt *endpt;
1123
Michal Vasko2e6defd2016-10-07 15:48:15 +02001124 if (!endpt_name) {
1125 ERRARG("endpt_name");
1126 return -1;
1127 }
1128
Michal Vaskoe2713da2016-08-22 16:06:40 +02001129 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001130 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001131 if (!endpt) {
1132 return -1;
1133 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001134 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001135 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001136 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001137
1138 return ret;
1139}
1140
1141API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001142nc_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 +02001143{
1144 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001145 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001146 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001147
1148 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001149 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1150 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001151 return -1;
1152 }
1153
Michal Vaskoadf30f02019-06-24 09:34:47 +02001154 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001155
1156 /* UNLOCK */
1157 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001158
1159 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001160}
1161
1162static int
Michal Vasko96830e32016-02-01 10:54:18 +01001163nc_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 +01001164{
Michal Vasko96830e32016-02-01 10:54:18 +01001165 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001166 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001167 return -1;
1168 }
1169
Michal Vasko96830e32016-02-01 10:54:18 +01001170 if (ca_file) {
Michal Vasko77367452021-02-16 16:32:18 +01001171 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1172 lydict_insert(server_opts.ctx, ca_file, 0, &opts->trusted_ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001173 }
1174
Michal Vasko96830e32016-02-01 10:54:18 +01001175 if (ca_dir) {
Michal Vasko77367452021-02-16 16:32:18 +01001176 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
1177 lydict_insert(server_opts.ctx, ca_dir, 0, &opts->trusted_ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001178 }
1179
1180 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001181}
1182
Michal Vaskoc61c4492016-01-25 11:13:34 +01001183API int
Michal Vasko96830e32016-02-01 10:54:18 +01001184nc_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 +01001185{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001186 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001187 struct nc_endpt *endpt;
1188
Michal Vasko2e6defd2016-10-07 15:48:15 +02001189 if (!endpt_name) {
1190 ERRARG("endpt_name");
1191 return -1;
1192 }
1193
Michal Vasko51e514d2016-02-02 15:51:52 +01001194 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001195 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001196 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001197 return -1;
1198 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001199 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001200 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001201 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001202
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001203 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001204}
1205
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001206API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001207nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file,
1208 const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001209{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001210 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001211 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001212 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001213
1214 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001215 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1216 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001217 return -1;
1218 }
1219
Michal Vaskoadf30f02019-06-24 09:34:47 +02001220 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001221
1222 /* UNLOCK */
1223 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001224
1225 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001226}
1227
Michal Vaskoc61c4492016-01-25 11:13:34 +01001228static int
Michal Vasko96830e32016-02-01 10:54:18 +01001229nc_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 +01001230{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001231 X509_LOOKUP *lookup;
1232
Michal Vasko96830e32016-02-01 10:54:18 +01001233 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001234 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001235 return -1;
1236 }
1237
Michal Vaskoc61c4492016-01-25 11:13:34 +01001238 if (!opts->crl_store) {
1239 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001240 }
1241
Michal Vasko96830e32016-02-01 10:54:18 +01001242 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001243 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001244 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001245 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001246 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001247 }
1248
Michal Vasko96830e32016-02-01 10:54:18 +01001249 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001250 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001251 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001252 }
1253 }
1254
Michal Vasko96830e32016-02-01 10:54:18 +01001255 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001256 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001257 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001258 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001259 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001260 }
1261
Michal Vasko96830e32016-02-01 10:54:18 +01001262 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001263 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001264 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001265 }
1266 }
1267
1268 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001269
1270fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001271 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001272}
1273
Michal Vaskoc61c4492016-01-25 11:13:34 +01001274API int
Michal Vasko96830e32016-02-01 10:54:18 +01001275nc_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 +01001276{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001277 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001278 struct nc_endpt *endpt;
1279
Michal Vasko2e6defd2016-10-07 15:48:15 +02001280 if (!endpt_name) {
1281 ERRARG("endpt_name");
1282 return -1;
1283 }
1284
Michal Vasko51e514d2016-02-02 15:51:52 +01001285 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001286 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001287 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001288 return -1;
1289 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001290 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001291 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001292 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001293
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001294 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001295}
1296
1297API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001298nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file,
1299 const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001300{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001301 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001302 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001303 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001304
1305 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001306 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1307 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001308 return -1;
1309 }
1310
Michal Vaskoadf30f02019-06-24 09:34:47 +02001311 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001312
1313 /* UNLOCK */
1314 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001315
1316 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001317}
1318
1319static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001320nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001321{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001322 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001323 return;
1324 }
1325
1326 X509_STORE_free(opts->crl_store);
1327 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001328}
1329
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001330API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001331nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001332{
Michal Vasko3031aae2016-01-27 16:07:18 +01001333 struct nc_endpt *endpt;
1334
Michal Vasko2e6defd2016-10-07 15:48:15 +02001335 if (!endpt_name) {
1336 ERRARG("endpt_name");
1337 return;
1338 }
1339
Michal Vasko51e514d2016-02-02 15:51:52 +01001340 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001341 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001342 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001343 return;
1344 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001345 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001346 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001347 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001348}
1349
Michal Vaskoc61c4492016-01-25 11:13:34 +01001350API void
Michal Vaskoadf30f02019-06-24 09:34:47 +02001351nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001352{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001353 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001354 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001355
1356 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001357 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1358 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001359 return;
1360 }
1361
Michal Vaskoadf30f02019-06-24 09:34:47 +02001362 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001363
1364 /* UNLOCK */
1365 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001366}
1367
1368static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001369nc_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 +02001370 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001371{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001372 struct nc_ctn *ctn, *new;
1373
Michal Vaskoc61c4492016-01-25 11:13:34 +01001374 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001375 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001376 opts->ctn = new = calloc(1, sizeof *new);
1377 if (!new) {
1378 ERRMEM;
1379 return -1;
1380 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001381 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001382 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001383 new = calloc(1, sizeof *new);
1384 if (!new) {
1385 ERRMEM;
1386 return -1;
1387 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001388 new->next = opts->ctn;
1389 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001390 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001391 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {}
Michal Vasko276f9d92017-02-02 11:15:55 +01001392 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001393 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001394 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001395 } else {
1396 /* insert after ctn */
1397 new = calloc(1, sizeof *new);
1398 if (!new) {
1399 ERRMEM;
1400 return -1;
1401 }
1402 new->next = ctn->next;
1403 ctn->next = new;
1404 }
1405 }
1406
1407 new->id = id;
1408 if (fingerprint) {
Michal Vasko77367452021-02-16 16:32:18 +01001409 lydict_remove(server_opts.ctx, new->fingerprint);
1410 lydict_insert(server_opts.ctx, fingerprint, 0, &new->fingerprint);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001411 }
1412 if (map_type) {
1413 new->map_type = map_type;
1414 }
1415 if (name) {
Michal Vasko77367452021-02-16 16:32:18 +01001416 lydict_remove(server_opts.ctx, new->name);
1417 lydict_insert(server_opts.ctx, name, 0, &new->name);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001418 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001419
1420 return 0;
1421}
1422
1423API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001424nc_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 +02001425 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001426{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001427 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001428 struct nc_endpt *endpt;
1429
Michal Vasko2e6defd2016-10-07 15:48:15 +02001430 if (!endpt_name) {
1431 ERRARG("endpt_name");
1432 return -1;
1433 }
1434
Michal Vasko51e514d2016-02-02 15:51:52 +01001435 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001436 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001437 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001438 return -1;
1439 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001440 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001441 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001442 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001443
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001444 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001445}
1446
1447API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001448nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id,
1449 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001450{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001451 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001452 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001453 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001454
1455 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001456 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1457 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001458 return -1;
1459 }
1460
Michal Vaskoadf30f02019-06-24 09:34:47 +02001461 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001462
1463 /* UNLOCK */
1464 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001465
1466 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001467}
1468
1469static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001470nc_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 +02001471 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001472{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001473 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001474 int ret = -1;
1475
Michal Vasko1a38c862016-01-15 15:50:07 +01001476 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001477 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001478 while (ctn) {
1479 lydict_remove(server_opts.ctx, ctn->fingerprint);
1480 lydict_remove(server_opts.ctx, ctn->name);
1481
1482 next = ctn->next;
1483 free(ctn);
1484 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001485
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001486 ret = 0;
1487 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001488 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001489 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001490 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001491 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001492 while (ctn) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001493 if (((id < 0) || (ctn->id == id)) &&
1494 (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) &&
1495 (!map_type || (ctn->map_type == map_type)) &&
1496 (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001497 lydict_remove(server_opts.ctx, ctn->fingerprint);
1498 lydict_remove(server_opts.ctx, ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001499
Michal Vasko5e3f3392016-01-20 11:13:01 +01001500 if (prev) {
1501 prev->next = ctn->next;
1502 next = ctn->next;
1503 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001504 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001505 next = ctn->next;
1506 }
1507 free(ctn);
1508 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001509
1510 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001511 } else {
1512 prev = ctn;
1513 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001514 }
1515 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001516 }
1517
1518 return ret;
1519}
1520
Michal Vaskoc61c4492016-01-25 11:13:34 +01001521API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001522nc_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 +02001523 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001524{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001525 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001526 struct nc_endpt *endpt;
1527
Michal Vasko2e6defd2016-10-07 15:48:15 +02001528 if (!endpt_name) {
1529 ERRARG("endpt_name");
1530 return -1;
1531 }
1532
Michal Vasko51e514d2016-02-02 15:51:52 +01001533 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001534 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001535 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001536 return -1;
1537 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001538 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001539 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001540 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001541
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001542 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001543}
1544
1545API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001546nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id,
1547 const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001548{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001549 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001550 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001551 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001552
1553 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001554 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1555 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001556 return -1;
1557 }
1558
Michal Vaskoadf30f02019-06-24 09:34:47 +02001559 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001560
1561 /* UNLOCK */
1562 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001563
1564 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001565}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001566
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001567static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001568nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001569 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001570{
1571 struct nc_ctn *ctn;
1572 int ret = -1;
1573
1574 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1575 if (id && *id && (*id != ctn->id)) {
1576 continue;
1577 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001578 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001579 continue;
1580 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001581 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001582 continue;
1583 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001584 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001585 continue;
1586 }
1587
1588 /* first match, good enough */
1589 if (id && !(*id)) {
1590 *id = ctn->id;
1591 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001592 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001593 *fingerprint = strdup(ctn->fingerprint);
1594 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001595 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001596 *map_type = ctn->map_type;
1597 }
Adam Richterd44680e2019-06-15 13:16:16 -07001598 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001599 *name = strdup(ctn->name);
1600 }
1601
1602 ret = 0;
1603 break;
1604 }
1605
1606 return ret;
1607}
1608
1609API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001610nc_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 +02001611 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001612{
1613 int ret;
1614 struct nc_endpt *endpt;
1615
1616 if (!endpt_name) {
1617 ERRARG("endpt_name");
1618 return -1;
1619 }
1620
1621 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001622 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001623 if (!endpt) {
1624 return -1;
1625 }
1626 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1627 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001628 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001629
1630 return ret;
1631}
1632
1633API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02001634nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
1635 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001636{
1637 int ret;
1638 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02001639 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001640
1641 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02001642 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
1643 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001644 return -1;
1645 }
1646
Michal Vaskoadf30f02019-06-24 09:34:47 +02001647 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001648
1649 /* UNLOCK */
1650 nc_server_ch_client_unlock(client);
1651
1652 return ret;
1653}
1654
Michal Vasko709598e2016-11-28 14:48:32 +01001655API const X509 *
1656nc_session_get_client_cert(const struct nc_session *session)
1657{
1658 if (!session || (session->side != NC_SERVER)) {
1659 ERRARG("session");
1660 return NULL;
1661 }
1662
1663 return session->opts.server.client_cert;
1664}
1665
Michal Vasko7060bcf2016-11-28 14:48:11 +01001666API void
1667nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1668{
1669 server_opts.user_verify_clb = verify_clb;
1670}
1671
Michal Vasko3031aae2016-01-27 16:07:18 +01001672void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001673nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001674{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001675 lydict_remove(server_opts.ctx, opts->server_cert);
1676 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001677 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1678 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001679 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001680 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001681}
Michal Vasko9e036d52016-01-08 10:49:26 +01001682
Michal Vasko6d292992016-01-18 09:42:38 +01001683static void
1684nc_tls_make_verify_key(void)
1685{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001686 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001687}
1688
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001689static X509 *
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001690tls_load_cert(const char *cert_path, const char *cert_data)
1691{
1692 X509 *cert;
1693
1694 if (cert_path) {
1695 cert = pem_to_cert(cert_path);
1696 } else {
1697 cert = base64der_to_cert(cert_data);
1698 }
1699
1700 if (!cert) {
1701 if (cert_path) {
1702 ERR("Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001703 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001704 } else {
1705 ERR("Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001706 ERR_reason_error_string(ERR_get_error()));
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001707 }
1708 }
1709 return cert;
1710}
1711
1712static int
1713nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1714{
1715 char **cert_paths = NULL, **cert_data = NULL;
1716 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1717 X509 *cert = NULL;
1718
1719 if (!server_opts.server_cert_chain_clb) {
1720 /* This is optional, so return OK */
1721 return 0;
1722 }
1723
1724 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001725 &cert_path_count, &cert_data, &cert_data_count)) {
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001726 ERR("Server certificate chain callback failed.");
1727 return -1;
1728 }
1729
1730 for (i = 0; i < cert_path_count; ++i) {
1731 cert = tls_load_cert(cert_paths[i], NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001732 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001733 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1734 ret = -1;
1735 goto cleanup;
1736 }
1737 }
1738
1739 for (i = 0; i < cert_data_count; ++i) {
1740 cert = tls_load_cert(NULL, cert_data[i]);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001741 if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) {
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001742 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1743 ret = -1;
1744 goto cleanup;
1745 }
1746 }
1747cleanup:
1748 for (i = 0; i < cert_path_count; ++i) {
1749 free(cert_paths[i]);
1750 }
1751 free(cert_paths);
1752 for (i = 0; i < cert_data_count; ++i) {
1753 free(cert_data[i]);
1754 }
1755 free(cert_data);
1756 /* cert is owned by the SSL_CTX */
1757
1758 return ret;
1759}
1760
Michal Vasko4c1fb492017-01-30 14:31:07 +01001761static int
1762nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1763{
1764 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001765 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001766 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001767 X509 *cert = NULL;
1768 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001769
1770 if (!cert_name) {
1771 ERR("Server certificate not set.");
1772 return -1;
1773 } else if (!server_opts.server_cert_clb) {
1774 ERR("Callback for retrieving the server certificate is not set.");
1775 return -1;
1776 }
1777
1778 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 +02001779 &privkey_data, &privkey_type)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001780 ERR("Server certificate callback failed.");
1781 return -1;
1782 }
1783
1784 /* load the certificate */
1785 if (cert_path) {
1786 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
1787 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1788 ret = -1;
1789 goto cleanup;
1790 }
1791 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001792 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001793 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
1794 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1795 ret = -1;
1796 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001797 }
1798 }
1799
1800 /* load the private key */
1801 if (privkey_path) {
1802 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
1803 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1804 ret = -1;
1805 goto cleanup;
1806 }
1807 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001808 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001809 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
1810 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1811 ret = -1;
1812 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001813 }
1814 }
1815
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001816 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1817
Michal Vasko4c1fb492017-01-30 14:31:07 +01001818cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001819 X509_free(cert);
1820 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001821 free(cert_path);
1822 free(cert_data);
1823 free(privkey_path);
1824 free(privkey_data);
1825 return ret;
1826}
1827
1828static void
1829tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1830{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001831 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001832
Michal Vasko4c1fb492017-01-30 14:31:07 +01001833 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001834 return;
1835 }
1836
1837 /* add the trusted certificate */
1838 if (X509_STORE_add_cert(cert_store, cert) != 1) {
1839 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1840 X509_free(cert);
1841 return;
1842 }
1843 X509_free(cert);
1844}
1845
1846static int
1847nc_tls_store_set_trusted_certs(X509_STORE *cert_store, const char **trusted_cert_lists, uint16_t trusted_cert_list_count)
1848{
1849 uint16_t i;
1850 int j;
1851 char **cert_paths, **cert_data;
1852 int cert_path_count, cert_data_count;
1853
1854 if (!server_opts.trusted_cert_list_clb) {
1855 ERR("Callback for retrieving trusted certificate lists is not set.");
1856 return -1;
1857 }
1858
1859 for (i = 0; i < trusted_cert_list_count; ++i) {
1860 cert_paths = cert_data = NULL;
1861 cert_path_count = cert_data_count = 0;
1862 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001863 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001864 ERR("Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
1865 return -1;
1866 }
1867
1868 for (j = 0; j < cert_path_count; ++j) {
1869 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1870 free(cert_paths[j]);
1871 }
1872 free(cert_paths);
1873
1874 for (j = 0; j < cert_data_count; ++j) {
1875 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1876 free(cert_data[j]);
1877 }
1878 free(cert_data);
1879 }
1880
1881 return 0;
1882}
1883
Michal Vasko3031aae2016-01-27 16:07:18 +01001884int
Michal Vasko0190bc32016-03-02 15:47:49 +01001885nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001886{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001887 X509_STORE *cert_store;
1888 SSL_CTX *tls_ctx;
1889 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001890 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001891 int ret;
1892 struct timespec ts_timeout, ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001893
Michal Vasko2cc4c682016-03-01 09:16:48 +01001894 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001895
Michal Vaskoe2713da2016-08-22 16:06:40 +02001896 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001897#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1898 tls_ctx = SSL_CTX_new(TLS_server_method());
1899#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001900 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001901#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001902 if (!tls_ctx) {
1903 ERR("Failed to create TLS context.");
1904 goto error;
1905 }
1906 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 +01001907 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001908 goto error;
1909 }
1910
1911 /* X509_STORE, managed (freed) with the context */
1912 cert_store = X509_STORE_new();
1913 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1914
Michal Vasko4c1fb492017-01-30 14:31:07 +01001915 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1916 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001917 }
1918
1919 if (opts->trusted_ca_file) {
1920 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1921 if (!lookup) {
1922 ERR("Failed to add a lookup method.");
1923 goto error;
1924 }
1925
1926 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
1927 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
1928 goto error;
1929 }
1930 }
1931
1932 if (opts->trusted_ca_dir) {
1933 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1934 if (!lookup) {
1935 ERR("Failed to add a lookup method.");
1936 goto error;
1937 }
1938
1939 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
1940 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
1941 goto error;
1942 }
1943 }
1944
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001945 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001946 session->ti.tls = SSL_new(tls_ctx);
1947
Michal Vasko4c1fb492017-01-30 14:31:07 +01001948 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001949 SSL_CTX_free(tls_ctx);
1950 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001951
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001952 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001953 ERR("Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001954 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001955 }
1956
1957 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001958 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001959 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1960
Michal Vasko6d292992016-01-18 09:42:38 +01001961 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001962 pthread_once(&verify_once, nc_tls_make_verify_key);
1963 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001964
Michal Vasko36c7be82017-02-22 13:37:59 +01001965 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001966 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001967 nc_addtimespec(&ts_timeout, timeout);
1968 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001969 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1970 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001971 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001972 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001973 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1974 ERR("SSL_accept timeout.");
1975 return 0;
1976 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001977 }
1978 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001979
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001980 if (ret != 1) {
1981 switch (SSL_get_error(session->ti.tls, ret)) {
1982 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01001983 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001984 break;
1985 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01001986 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001987 break;
1988 default:
Michal Vaskod083db62016-01-19 10:31:29 +01001989 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001990 break;
1991 }
1992 return -1;
1993 }
1994
Michal Vasko1a38c862016-01-15 15:50:07 +01001995 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001996
1997error:
1998 if (sock > -1) {
1999 close(sock);
2000 }
2001 SSL_CTX_free(tls_ctx);
2002 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002003}