blob: 54324182a4eaee3f32f1fa0dab863ee4f3cd8fbb [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
17#include <string.h>
18#include <poll.h>
Michal Vaskof0537d82016-01-29 14:42:38 +010019#include <unistd.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010020
21#include <openssl/ssl.h>
22#include <openssl/evp.h>
23#include <openssl/err.h>
24#include <openssl/x509v3.h>
Michal Vaskoe2713da2016-08-22 16:06:40 +020025#include <openssl/x509.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010026
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include "session_server.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010028#include "session_server_ch.h"
29#include "libnetconf.h"
Radek Krejci5da708a2015-09-01 17:33:23 +020030
Michal Vasko3031aae2016-01-27 16:07:18 +010031struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010032pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER;
Michal Vaskoc14e3c82016-01-11 16:14:30 +010033extern struct nc_server_opts server_opts;
Michal Vaskoc61c4492016-01-25 11:13:34 +010034
Michal Vasko5c2f7952016-01-22 13:16:31 +010035static pthread_key_t verify_key;
36static pthread_once_t verify_once = PTHREAD_ONCE_INIT;
37
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038static char *
Michal Vasko18aeb5d2017-02-17 09:23:56 +010039asn1time_to_str(const ASN1_TIME *t)
Michal Vasko086311b2016-01-08 09:53:11 +010040{
Michal Vaskoc14e3c82016-01-11 16:14:30 +010041 char *cp;
42 BIO *bio;
43 int n;
Radek Krejci5da708a2015-09-01 17:33:23 +020044
Michal Vaskoc14e3c82016-01-11 16:14:30 +010045 if (!t) {
46 return NULL;
47 }
48 bio = BIO_new(BIO_s_mem());
49 if (!bio) {
50 return NULL;
51 }
52 ASN1_TIME_print(bio, t);
53 n = BIO_pending(bio);
54 cp = malloc(n + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +010055 if (!cp) {
56 ERRMEM;
57 BIO_free(bio);
58 return NULL;
59 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010060 n = BIO_read(bio, cp, n);
61 if (n < 0) {
62 BIO_free(bio);
63 free(cp);
64 return NULL;
65 }
66 cp[n] = '\0';
67 BIO_free(bio);
68 return cp;
69}
70
71static void
72digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str)
73{
74 unsigned int i;
75
76 *str = malloc(dig_len * 3);
Michal Vasko4eb3c312016-03-01 14:09:37 +010077 if (!*str) {
78 ERRMEM;
79 return;
80 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010081 for (i = 0; i < dig_len - 1; ++i) {
82 sprintf((*str) + (i * 3), "%02x:", digest[i]);
83 }
84 sprintf((*str) + (i * 3), "%02x", digest[i]);
85}
86
87/* return NULL - SSL error can be retrieved */
88static X509 *
89base64der_to_cert(const char *in)
90{
91 X509 *out;
92 char *buf;
93 BIO *bio;
94
95 if (in == NULL) {
96 return NULL;
97 }
98
99 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
100 return NULL;
101 }
102 bio = BIO_new_mem_buf(buf, strlen(buf));
103 if (!bio) {
104 free(buf);
105 return NULL;
106 }
107
108 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
109 if (!out) {
110 free(buf);
111 BIO_free(bio);
112 return NULL;
113 }
114
115 free(buf);
116 BIO_free(bio);
117 return out;
118}
119
120/* return NULL - either errno or SSL error */
121static X509 *
122pem_to_cert(const char *path)
123{
124 FILE *fp;
125 X509 *out;
126
127 fp = fopen(path, "r");
128 if (!fp) {
129 return NULL;
130 }
131
132 out = PEM_read_X509(fp, NULL, NULL, NULL);
133 fclose(fp);
134 return out;
135}
136
Michal Vasko6e08cb72017-02-02 11:15:29 +0100137static EVP_PKEY *
Michal Vaskoddce1212019-05-24 09:58:49 +0200138base64der_to_privatekey(const char *in, const char *key_str)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100139{
140 EVP_PKEY *out;
141 char *buf;
142 BIO *bio;
143
144 if (in == NULL) {
145 return NULL;
146 }
147
Michal Vaskoddce1212019-05-24 09:58:49 +0200148 if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", key_str, " PRIVATE KEY-----\n", in, "\n-----END ",
149 key_str, " PRIVATE KEY-----") == -1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100150 return NULL;
151 }
152 bio = BIO_new_mem_buf(buf, strlen(buf));
153 if (!bio) {
154 free(buf);
155 return NULL;
156 }
157
158 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
159 if (!out) {
160 free(buf);
161 BIO_free(bio);
162 return NULL;
163 }
164
165 free(buf);
166 BIO_free(bio);
167 return out;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100168}
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100169
170static int
171cert_pubkey_match(X509 *cert1, X509 *cert2)
172{
173 ASN1_BIT_STRING *bitstr1, *bitstr2;
174
175 bitstr1 = X509_get0_pubkey_bitstr(cert1);
176 bitstr2 = X509_get0_pubkey_bitstr(cert2);
177
178 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
179 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
180 return 0;
181 }
182
183 return 1;
184}
185
186static int
187nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
188{
189 STACK_OF(GENERAL_NAME) *san_names;
190 GENERAL_NAME *san_name;
191 ASN1_OCTET_STRING *ip;
192 int i, san_count;
193 char *subject, *common_name;
194
195 if (map_type == NC_TLS_CTN_COMMON_NAME) {
196 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
197 common_name = strstr(subject, "CN=");
198 if (!common_name) {
Michal Vaskod083db62016-01-19 10:31:29 +0100199 WRN("Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100200 free(subject);
201 return 1;
202 }
203 common_name += 3;
204 if (strchr(common_name, '/')) {
205 *strchr(common_name, '/') = '\0';
206 }
207 *username = strdup(common_name);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100208 if (!*username) {
209 ERRMEM;
210 return 1;
211 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100212 free(subject);
213 } else {
214 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
215 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
216 if (!san_names) {
Michal Vaskod083db62016-01-19 10:31:29 +0100217 WRN("Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100218 return 1;
219 }
220
221 san_count = sk_GENERAL_NAME_num(san_names);
222 for (i = 0; i < san_count; ++i) {
223 san_name = sk_GENERAL_NAME_value(san_names, i);
224
225 /* rfc822Name (email) */
226 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_RFC822_NAME) &&
227 san_name->type == GEN_EMAIL) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100228#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100229 *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100230#else
231 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name));
232#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100233 if (!*username) {
234 ERRMEM;
235 return 1;
236 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100237 break;
238 }
239
240 /* dNSName */
241 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_DNS_NAME) &&
242 san_name->type == GEN_DNS) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100243#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100244 *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100245#else
246 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName));
247#endif
Michal Vasko4eb3c312016-03-01 14:09:37 +0100248 if (!*username) {
249 ERRMEM;
250 return 1;
251 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100252 break;
253 }
254
255 /* iPAddress */
256 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_IP_ADDRESS) &&
257 san_name->type == GEN_IPADD) {
258 ip = san_name->d.iPAddress;
259 if (ip->length == 4) {
260 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 +0100261 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100262 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
263 return -1;
264 }
265 break;
266 } else if (ip->length == 16) {
267 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
268 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
269 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
270 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100271 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100272 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
273 return -1;
274 }
275 break;
276 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100277 WRN("SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100278 }
279 }
280 }
281 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
282
283 if (i < san_count) {
284 switch (map_type) {
285 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100286 WRN("Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100287 break;
288 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100289 WRN("Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100290 break;
291 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vaskod083db62016-01-19 10:31:29 +0100292 WRN("Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100293 break;
294 case NC_TLS_CTN_SAN_ANY:
Michal Vaskod083db62016-01-19 10:31:29 +0100295 WRN("Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100296 break;
297 default:
298 break;
299 }
300 return 1;
301 }
302 }
303
304 return 0;
305}
306
307/* return: 0 - OK, 1 - no match, -1 - error */
308static int
Michal Vaskoc61c4492016-01-25 11:13:34 +0100309nc_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 +0100310{
311 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
312 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100313 unsigned char *buf = malloc(64);
314 unsigned int buf_len = 64;
315 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100316 struct nc_ctn *ctn;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100317
Michal Vasko4eb3c312016-03-01 14:09:37 +0100318 if (!buf) {
319 ERRMEM;
320 return -1;
321 }
322
Michal Vaskoc61c4492016-01-25 11:13:34 +0100323 if (!ctn_first || !cert || !map_type || !name) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100324 free(buf);
325 return -1;
326 }
327
Michal Vaskoc61c4492016-01-25 11:13:34 +0100328 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100329 /* first make sure the entry is valid */
330 if (!ctn->fingerprint || !ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
331 VRB("Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
332 continue;
333 }
334
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100335 /* MD5 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100336 if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100337 if (!digest_md5) {
338 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100339 ERR("Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100340 ret = -1;
341 goto cleanup;
342 }
343 digest_to_str(buf, buf_len, &digest_md5);
344 }
345
Michal Vasko5e3f3392016-01-20 11:13:01 +0100346 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100347 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100348 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100349 *map_type = ctn->map_type;
350 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
351 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100352 }
353 break;
354 }
355
356 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100357 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100358 if (!digest_sha1) {
359 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100360 ERR("Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100361 ret = -1;
362 goto cleanup;
363 }
364 digest_to_str(buf, buf_len, &digest_sha1);
365 }
366
Michal Vasko5e3f3392016-01-20 11:13:01 +0100367 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100368 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100369 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100370 *map_type = ctn->map_type;
371 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
372 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100373 }
374 break;
375 }
376
377 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100378 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100379 if (!digest_sha224) {
380 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100381 ERR("Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100382 ret = -1;
383 goto cleanup;
384 }
385 digest_to_str(buf, buf_len, &digest_sha224);
386 }
387
Michal Vasko5e3f3392016-01-20 11:13:01 +0100388 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100389 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100390 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100391 *map_type = ctn->map_type;
392 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
393 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100394 }
395 break;
396 }
397
398 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100399 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100400 if (!digest_sha256) {
401 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100402 ERR("Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100403 ret = -1;
404 goto cleanup;
405 }
406 digest_to_str(buf, buf_len, &digest_sha256);
407 }
408
Michal Vasko5e3f3392016-01-20 11:13:01 +0100409 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100410 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100411 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100412 *map_type = ctn->map_type;
413 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
414 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100415 }
416 break;
417 }
418
419 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100420 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100421 if (!digest_sha384) {
422 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100423 ERR("Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100424 ret = -1;
425 goto cleanup;
426 }
427 digest_to_str(buf, buf_len, &digest_sha384);
428 }
429
Michal Vasko5e3f3392016-01-20 11:13:01 +0100430 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100431 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100432 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100433 *map_type = ctn->map_type;
434 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
435 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100436 }
437 break;
438 }
439
440 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100441 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100442 if (!digest_sha512) {
443 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100444 ERR("Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100445 ret = -1;
446 goto cleanup;
447 }
448 digest_to_str(buf, buf_len, &digest_sha512);
449 }
450
Michal Vasko5e3f3392016-01-20 11:13:01 +0100451 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100452 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100453 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100454 *map_type = ctn->map_type;
455 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
456 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100457 }
458 break;
459 }
460
461 /* unknown */
462 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +0100463 WRN("Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100464 }
465 }
466
Michal Vasko5e3f3392016-01-20 11:13:01 +0100467 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100468 ret = 1;
469 }
470
471cleanup:
472 free(digest_md5);
473 free(digest_sha1);
474 free(digest_sha224);
475 free(digest_sha256);
476 free(digest_sha384);
477 free(digest_sha512);
478 free(buf);
479 return ret;
480}
481
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100482#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
483
484static int
485nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
486{
487 X509_STORE_CTX *store_ctx;
488 X509_OBJECT *obj;
489 X509_NAME *subject;
490 X509_NAME *issuer;
491 X509 *cert;
492 X509_CRL *crl;
493 X509_REVOKED *revoked;
494 STACK_OF(X509) *cert_stack;
495 EVP_PKEY *pubkey;
496 struct nc_session* session;
497 struct nc_server_tls_opts *opts;
498 const ASN1_INTEGER *serial;
499 int i, n, rc, depth;
500 char *cp;
501 const char *username = NULL;
502 NC_TLS_CTN_MAPTYPE map_type = 0;
503 const ASN1_TIME *last_update = NULL, *next_update = NULL;
504
505 /* get the thread session */
506 session = pthread_getspecific(verify_key);
507 if (!session) {
508 ERRINT;
509 return 0;
510 }
511
512 opts = session->data;
513
514 /* get the last certificate, that is the peer (client) certificate */
515 if (!session->opts.server.client_cert) {
516 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500517 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100518 X509_up_ref(session->opts.server.client_cert);
519 sk_X509_pop_free(cert_stack, X509_free);
520 }
521
522 /* standard certificate verification failed, so a trusted client cert must match to continue */
523 if (!preverify_ok) {
524 subject = X509_get_subject_name(session->opts.server.client_cert);
525 cert_stack = X509_STORE_CTX_get1_certs(x509_ctx, subject);
526 if (cert_stack) {
527 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
528 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
529 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
530 * this callback will be called again with the same current certificate and preverify_ok == 1 */
531 VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.",
532 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
533 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
534 sk_X509_pop_free(cert_stack, X509_free);
535 return 1;
536 }
537 }
538 sk_X509_pop_free(cert_stack, X509_free);
539 }
540
541 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
542 return 0;
543 }
544
545 /* print cert verify info */
546 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
547 VRB("Cert verify: depth %d.", depth);
548
549 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
550 subject = X509_get_subject_name(cert);
551 issuer = X509_get_issuer_name(cert);
552
553 cp = X509_NAME_oneline(subject, NULL, 0);
554 VRB("Cert verify: subject: %s.", cp);
555 OPENSSL_free(cp);
556 cp = X509_NAME_oneline(issuer, NULL, 0);
557 VRB("Cert verify: issuer: %s.", cp);
558 OPENSSL_free(cp);
559
560 /* check for revocation if set */
561 if (opts->crl_store) {
562 /* try to retrieve a CRL corresponding to the _subject_ of
563 * the current certificate in order to verify it's integrity */
564 store_ctx = X509_STORE_CTX_new();
565 obj = X509_OBJECT_new();
566 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
567 rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
568 X509_STORE_CTX_free(store_ctx);
569 crl = X509_OBJECT_get0_X509_CRL(obj);
570 if (rc > 0 && crl) {
571 cp = X509_NAME_oneline(subject, NULL, 0);
572 VRB("Cert verify CRL: issuer: %s.", cp);
573 OPENSSL_free(cp);
574
575 last_update = X509_CRL_get0_lastUpdate(crl);
576 next_update = X509_CRL_get0_nextUpdate(crl);
577 cp = asn1time_to_str(last_update);
578 VRB("Cert verify CRL: last update: %s.", cp);
579 free(cp);
580 cp = asn1time_to_str(next_update);
581 VRB("Cert verify CRL: next update: %s.", cp);
582 free(cp);
583
584 /* verify the signature on this CRL */
585 pubkey = X509_get_pubkey(cert);
586 if (X509_CRL_verify(crl, pubkey) <= 0) {
587 ERR("Cert verify CRL: invalid signature.");
588 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
589 X509_OBJECT_free(obj);
590 if (pubkey) {
591 EVP_PKEY_free(pubkey);
592 }
593 return 0;
594 }
595 if (pubkey) {
596 EVP_PKEY_free(pubkey);
597 }
598
599 /* check date of CRL to make sure it's not expired */
600 if (!next_update) {
601 ERR("Cert verify CRL: invalid nextUpdate field.");
602 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
603 X509_OBJECT_free(obj);
604 return 0;
605 }
606 if (X509_cmp_current_time(next_update) < 0) {
607 ERR("Cert verify CRL: expired - revoking all certificates.");
608 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
609 X509_OBJECT_free(obj);
610 return 0;
611 }
612 X509_OBJECT_free(obj);
613 }
614
615 /* try to retrieve a CRL corresponding to the _issuer_ of
616 * the current certificate in order to check for revocation */
617 store_ctx = X509_STORE_CTX_new();
618 obj = X509_OBJECT_new();
619 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
620 rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
621 X509_STORE_CTX_free(store_ctx);
622 crl = X509_OBJECT_get0_X509_CRL(obj);
623 if (rc > 0 && crl) {
624 /* check if the current certificate is revoked by this CRL */
625 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
626 for (i = 0; i < n; i++) {
627 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
628 serial = X509_REVOKED_get0_serialNumber(revoked);
629 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
630 cp = X509_NAME_oneline(issuer, NULL, 0);
631 ERR("Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", serial, serial, cp);
632 OPENSSL_free(cp);
633 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
634 X509_OBJECT_free(obj);
635 return 0;
636 }
637 }
638 X509_OBJECT_free(obj);
639 }
640 }
641
642 /* cert-to-name already successful */
643 if (session->username) {
644 return 1;
645 }
646
647 /* cert-to-name */
648 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
649
650 if (rc) {
651 if (rc == -1) {
652 /* fatal error */
653 depth = 0;
654 }
655 /* rc == 1 is a normal CTN fail (no match found) */
656 goto fail;
657 }
658
659 /* cert-to-name match, now to extract the specific field from the peer cert */
660 if (map_type == NC_TLS_CTN_SPECIFIED) {
661 session->username = lydict_insert(server_opts.ctx, username, 0);
662 } else {
663 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
664 if (rc) {
665 if (rc == -1) {
666 depth = 0;
667 }
668 goto fail;
669 }
670 session->username = lydict_insert_zc(server_opts.ctx, cp);
671 }
672
673 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
674
675 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
676 VRB("Cert verify: user verify callback revoked authorization.");
677 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
678 return 0;
679 }
680
681 return 1;
682
683fail:
684 if (depth > 0) {
685 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
686 return 1;
687 }
688
689 VRB("Cert-to-name unsuccessful, dropping the new client.");
690 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
691 return 0;
692}
693
694#else
695
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100696static int
697nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
698{
699 X509_STORE_CTX store_ctx;
700 X509_OBJECT obj;
701 X509_NAME *subject;
702 X509_NAME *issuer;
703 X509 *cert;
704 X509_CRL *crl;
705 X509_REVOKED *revoked;
706 STACK_OF(X509) *cert_stack;
707 EVP_PKEY *pubkey;
708 struct nc_session* session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100709 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100710 long serial;
711 int i, n, rc, depth;
712 char *cp;
713 const char *username = NULL;
714 NC_TLS_CTN_MAPTYPE map_type = 0;
715 ASN1_TIME *last_update = NULL, *next_update = NULL;
716
Michal Vasko6d292992016-01-18 09:42:38 +0100717 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100718 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100719 if (!session) {
720 ERRINT;
721 return 0;
722 }
723
Michal Vasko2cc4c682016-03-01 09:16:48 +0100724 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100725
726 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200727 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100728 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100729 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200730 X509_free(session->opts.server.client_cert);
731 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100732 }
733 sk_X509_pop_free(cert_stack, X509_free);
734 }
735
736 /* standard certificate verification failed, so a trusted client cert must match to continue */
737 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200738 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100739 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
740 if (cert_stack) {
741 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200742 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100743 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
744 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100745 VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100746 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
747 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
748 sk_X509_pop_free(cert_stack, X509_free);
749 return 1;
750 }
751 }
752 sk_X509_pop_free(cert_stack, X509_free);
753 }
754
755 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
756 return 0;
757 }
758
759 /* print cert verify info */
760 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vaskod083db62016-01-19 10:31:29 +0100761 VRB("Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100762
763 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
764 subject = X509_get_subject_name(cert);
765 issuer = X509_get_issuer_name(cert);
766
767 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100768 VRB("Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100769 OPENSSL_free(cp);
770 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100771 VRB("Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100772 OPENSSL_free(cp);
773
774 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100775 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100776 /* try to retrieve a CRL corresponding to the _subject_ of
777 * the current certificate in order to verify it's integrity */
778 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100779 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100780 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
781 X509_STORE_CTX_cleanup(&store_ctx);
782 crl = obj.data.crl;
783 if (rc > 0 && crl) {
784 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100785 VRB("Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100786 OPENSSL_free(cp);
787
788 last_update = X509_CRL_get_lastUpdate(crl);
789 next_update = X509_CRL_get_nextUpdate(crl);
790 cp = asn1time_to_str(last_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100791 VRB("Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100792 free(cp);
793 cp = asn1time_to_str(next_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100794 VRB("Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100795 free(cp);
796
797 /* verify the signature on this CRL */
798 pubkey = X509_get_pubkey(cert);
799 if (X509_CRL_verify(crl, pubkey) <= 0) {
800 ERR("Cert verify CRL: invalid signature.");
801 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
802 X509_OBJECT_free_contents(&obj);
803 if (pubkey) {
804 EVP_PKEY_free(pubkey);
805 }
806 return 0;
807 }
808 if (pubkey) {
809 EVP_PKEY_free(pubkey);
810 }
811
812 /* check date of CRL to make sure it's not expired */
813 if (!next_update) {
814 ERR("Cert verify CRL: invalid nextUpdate field.");
815 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
816 X509_OBJECT_free_contents(&obj);
817 return 0;
818 }
819 if (X509_cmp_current_time(next_update) < 0) {
820 ERR("Cert verify CRL: expired - revoking all certificates.");
821 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
822 X509_OBJECT_free_contents(&obj);
823 return 0;
824 }
825 X509_OBJECT_free_contents(&obj);
826 }
827
828 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100829 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100830 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100831 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100832 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
833 X509_STORE_CTX_cleanup(&store_ctx);
834 crl = obj.data.crl;
835 if (rc > 0 && crl) {
836 /* check if the current certificate is revoked by this CRL */
837 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
838 for (i = 0; i < n; i++) {
839 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
840 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
841 serial = ASN1_INTEGER_get(revoked->serialNumber);
842 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100843 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 +0100844 OPENSSL_free(cp);
845 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
846 X509_OBJECT_free_contents(&obj);
847 return 0;
848 }
849 }
850 X509_OBJECT_free_contents(&obj);
851 }
852 }
853
854 /* cert-to-name already successful */
855 if (session->username) {
856 return 1;
857 }
858
859 /* cert-to-name */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100860 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100861
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100862 if (rc) {
863 if (rc == -1) {
864 /* fatal error */
865 depth = 0;
866 }
867 /* rc == 1 is a normal CTN fail (no match found) */
868 goto fail;
869 }
870
871 /* cert-to-name match, now to extract the specific field from the peer cert */
872 if (map_type == NC_TLS_CTN_SPECIFIED) {
873 session->username = lydict_insert(server_opts.ctx, username, 0);
874 } else {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200875 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100876 if (rc) {
877 if (rc == -1) {
878 depth = 0;
879 }
880 goto fail;
881 }
882 session->username = lydict_insert_zc(server_opts.ctx, cp);
883 }
884
885 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100886
887 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
888 VRB("Cert verify: user verify callback revoked authorization.");
889 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
890 return 0;
891 }
892
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100893 return 1;
894
895fail:
896 if (depth > 0) {
897 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
898 return 1;
899 }
900
901 VRB("Cert-to-name unsuccessful, dropping the new client.");
902 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
903 return 0;
904}
905
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100906#endif
907
Michal Vasko3031aae2016-01-27 16:07:18 +0100908static int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100909nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100910{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100911 if (!name) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100912 if (opts->server_cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100913 lydict_remove(server_opts.ctx, opts->server_cert);
Michal Vaskoa8748792016-11-22 14:34:26 +0100914 }
915 opts->server_cert = NULL;
916 return 0;
917 }
918
Michal Vaskoe2713da2016-08-22 16:06:40 +0200919 if (opts->server_cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100920 lydict_remove(server_opts.ctx, opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100921 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100922 opts->server_cert = lydict_insert(server_opts.ctx, name, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100923
924 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100925}
926
927API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100928nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100929{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100930 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100931 struct nc_endpt *endpt;
932
Michal Vasko2e6defd2016-10-07 15:48:15 +0200933 if (!endpt_name) {
934 ERRARG("endpt_name");
935 return -1;
936 }
937
Michal Vasko51e514d2016-02-02 15:51:52 +0100938 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100939 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100941 return -1;
942 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100943 ret = nc_server_tls_set_server_cert(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100944 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100945 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100946
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100947 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100948}
949
950API int
Michal Vasko4c1fb492017-01-30 14:31:07 +0100951nc_server_tls_ch_client_set_server_cert(const char *client_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100952{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100953 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200954 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100955
Michal Vasko2e6defd2016-10-07 15:48:15 +0200956 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +0100957 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +0200958 return -1;
959 }
960
961 /* LOCK */
962 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
963 if (!client) {
964 return -1;
965 }
966
Michal Vasko4c1fb492017-01-30 14:31:07 +0100967 ret = nc_server_tls_set_server_cert(name, client->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200968
969 /* UNLOCK */
970 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100971
972 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100973}
974
Michal Vasko4c1fb492017-01-30 14:31:07 +0100975API void
976nc_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 +0200977 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
978 void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +0100979{
Michal Vasko4c1fb492017-01-30 14:31:07 +0100980 if (!cert_clb) {
981 ERRARG("cert_clb");
982 return;
Michal Vaskoa8748792016-11-22 14:34:26 +0100983 }
984
Michal Vasko4c1fb492017-01-30 14:31:07 +0100985 server_opts.server_cert_clb = cert_clb;
986 server_opts.server_cert_data = user_data;
987 server_opts.server_cert_data_free = free_user_data;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100988}
989
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500990API void
991nc_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 +0200992 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 -0500993{
994 if (!cert_chain_clb) {
995 ERRARG("cert_chain_clb");
996 return;
997 }
998
999 server_opts.server_cert_chain_clb = cert_chain_clb;
1000 server_opts.server_cert_chain_data = user_data;
1001 server_opts.server_cert_chain_data_free = free_user_data;
1002}
1003
Michal Vaskoc61c4492016-01-25 11:13:34 +01001004static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001005nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001006{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001007 if (!name) {
1008 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001009 return -1;
1010 }
1011
Michal Vasko4c1fb492017-01-30 14:31:07 +01001012 ++opts->trusted_cert_list_count;
1013 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists, opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
1014 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001015 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001016 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001017 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001018 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = lydict_insert(server_opts.ctx, name, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001019
1020 return 0;
1021}
1022
1023API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001024nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001025{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001026 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001027 struct nc_endpt *endpt;
1028
Michal Vasko2e6defd2016-10-07 15:48:15 +02001029 if (!endpt_name) {
1030 ERRARG("endpt_name");
1031 return -1;
1032 }
1033
Michal Vasko51e514d2016-02-02 15:51:52 +01001034 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001035 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001036 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001037 return -1;
1038 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001039 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001040 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001041 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001042
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001043 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001044}
1045
1046API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001047nc_server_tls_ch_client_add_trusted_cert_list(const char *client_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001048{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001049 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001050 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001051
Michal Vasko2e6defd2016-10-07 15:48:15 +02001052 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001053 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001054 return -1;
1055 }
1056
1057 /* LOCK */
1058 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1059 if (!client) {
1060 return -1;
1061 }
1062
Michal Vasko4c1fb492017-01-30 14:31:07 +01001063 ret = nc_server_tls_add_trusted_cert_list(name, client->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,
1073 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 Vasko4c1fb492017-01-30 14:31:07 +01001142nc_server_tls_ch_client_del_trusted_cert_list(const char *client_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 Vaskoe2713da2016-08-22 16:06:40 +02001146
Michal Vasko2e6defd2016-10-07 15:48:15 +02001147 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001148 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001149 return -1;
1150 }
1151
1152 /* LOCK */
1153 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1154 if (!client) {
1155 return -1;
1156 }
1157
Michal Vasko4c1fb492017-01-30 14:31:07 +01001158 ret = nc_server_tls_del_trusted_cert_list(name, client->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001159
1160 /* UNLOCK */
1161 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001162
1163 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001164}
1165
1166static int
Michal Vasko96830e32016-02-01 10:54:18 +01001167nc_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 +01001168{
Michal Vasko96830e32016-02-01 10:54:18 +01001169 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001170 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001171 return -1;
1172 }
1173
Michal Vasko96830e32016-02-01 10:54:18 +01001174 if (ca_file) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001175 if (opts->trusted_ca_file) {
1176 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001177 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001178 opts->trusted_ca_file = lydict_insert(server_opts.ctx, ca_file, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001179 }
1180
Michal Vasko96830e32016-02-01 10:54:18 +01001181 if (ca_dir) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001182 if (opts->trusted_ca_dir) {
1183 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001184 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001185 opts->trusted_ca_dir = lydict_insert(server_opts.ctx, ca_dir, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001186 }
1187
1188 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001189}
1190
Michal Vaskoc61c4492016-01-25 11:13:34 +01001191API int
Michal Vasko96830e32016-02-01 10:54:18 +01001192nc_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 +01001193{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001194 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001195 struct nc_endpt *endpt;
1196
Michal Vasko2e6defd2016-10-07 15:48:15 +02001197 if (!endpt_name) {
1198 ERRARG("endpt_name");
1199 return -1;
1200 }
1201
Michal Vasko51e514d2016-02-02 15:51:52 +01001202 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001203 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001204 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001205 return -1;
1206 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001207 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001208 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001209 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001210
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001211 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001212}
1213
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001215nc_server_tls_ch_client_set_trusted_ca_paths(const char *client_name, const char *ca_file, const char *ca_dir)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001216{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001217 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001218 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001219
Michal Vasko2e6defd2016-10-07 15:48:15 +02001220 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001221 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001222 return -1;
1223 }
1224
1225 /* LOCK */
1226 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1227 if (!client) {
1228 return -1;
1229 }
1230
1231 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, client->opts.tls);
1232
1233 /* UNLOCK */
1234 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001235
1236 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001237}
1238
Michal Vaskoc61c4492016-01-25 11:13:34 +01001239static int
Michal Vasko96830e32016-02-01 10:54:18 +01001240nc_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 +01001241{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001242 X509_LOOKUP *lookup;
1243
Michal Vasko96830e32016-02-01 10:54:18 +01001244 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001245 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001246 return -1;
1247 }
1248
Michal Vaskoc61c4492016-01-25 11:13:34 +01001249 if (!opts->crl_store) {
1250 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001251 }
1252
Michal Vasko96830e32016-02-01 10:54:18 +01001253 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001254 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001255 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001256 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001257 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001258 }
1259
Michal Vasko96830e32016-02-01 10:54:18 +01001260 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001261 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001262 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001263 }
1264 }
1265
Michal Vasko96830e32016-02-01 10:54:18 +01001266 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001267 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001268 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001269 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001270 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001271 }
1272
Michal Vasko96830e32016-02-01 10:54:18 +01001273 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001274 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001275 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001276 }
1277 }
1278
1279 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001280
1281fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001282 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001283}
1284
Michal Vaskoc61c4492016-01-25 11:13:34 +01001285API int
Michal Vasko96830e32016-02-01 10:54:18 +01001286nc_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 +01001287{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001288 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001289 struct nc_endpt *endpt;
1290
Michal Vasko2e6defd2016-10-07 15:48:15 +02001291 if (!endpt_name) {
1292 ERRARG("endpt_name");
1293 return -1;
1294 }
1295
Michal Vasko51e514d2016-02-02 15:51:52 +01001296 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001297 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001298 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001299 return -1;
1300 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001301 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001302 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001303 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001304
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001305 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001306}
1307
1308API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001309nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *crl_file, const char *crl_dir)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001310{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001311 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001312 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001313
Michal Vasko2e6defd2016-10-07 15:48:15 +02001314 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001315 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001316 return -1;
1317 }
1318
1319 /* LOCK */
1320 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1321 if (!client) {
1322 return -1;
1323 }
1324
1325 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, client->opts.tls);
1326
1327 /* UNLOCK */
1328 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001329
1330 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001331}
1332
1333static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001334nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001335{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001336 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001337 return;
1338 }
1339
1340 X509_STORE_free(opts->crl_store);
1341 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001342}
1343
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001344API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001345nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001346{
Michal Vasko3031aae2016-01-27 16:07:18 +01001347 struct nc_endpt *endpt;
1348
Michal Vasko2e6defd2016-10-07 15:48:15 +02001349 if (!endpt_name) {
1350 ERRARG("endpt_name");
1351 return;
1352 }
1353
Michal Vasko51e514d2016-02-02 15:51:52 +01001354 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001355 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001356 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001357 return;
1358 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001359 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001360 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001361 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001362}
1363
Michal Vaskoc61c4492016-01-25 11:13:34 +01001364API void
Michal Vasko2e6defd2016-10-07 15:48:15 +02001365nc_server_tls_ch_client_clear_crls(const char *client_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001366{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001367 struct nc_ch_client *client;
1368
1369 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001370 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001371 return;
1372 }
1373
1374 /* LOCK */
1375 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1376 if (!client) {
1377 return;
1378 }
1379
1380 nc_server_tls_clear_crls(client->opts.tls);
1381
1382 /* UNLOCK */
1383 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001384}
1385
1386static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001387nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1388 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001389{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001390 struct nc_ctn *ctn, *new;
1391
Michal Vaskoc61c4492016-01-25 11:13:34 +01001392 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001393 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001394 opts->ctn = new = calloc(1, sizeof *new);
1395 if (!new) {
1396 ERRMEM;
1397 return -1;
1398 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001399 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001400 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001401 new = calloc(1, sizeof *new);
1402 if (!new) {
1403 ERRMEM;
1404 return -1;
1405 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001406 new->next = opts->ctn;
1407 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001408 } else {
Andrew Langefeld62bc6712018-08-28 16:17:16 -05001409 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next);
Michal Vasko276f9d92017-02-02 11:15:55 +01001410 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001411 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001412 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001413 } else {
1414 /* insert after ctn */
1415 new = calloc(1, sizeof *new);
1416 if (!new) {
1417 ERRMEM;
1418 return -1;
1419 }
1420 new->next = ctn->next;
1421 ctn->next = new;
1422 }
1423 }
1424
1425 new->id = id;
1426 if (fingerprint) {
1427 if (new->fingerprint) {
1428 lydict_remove(server_opts.ctx, new->fingerprint);
1429 }
1430 new->fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0);
1431 }
1432 if (map_type) {
1433 new->map_type = map_type;
1434 }
1435 if (name) {
1436 if (new->name) {
1437 lydict_remove(server_opts.ctx, new->name);
1438 }
1439 new->name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001440 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001441
1442 return 0;
1443}
1444
1445API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001446nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1447 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001448{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001449 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001450 struct nc_endpt *endpt;
1451
Michal Vasko2e6defd2016-10-07 15:48:15 +02001452 if (!endpt_name) {
1453 ERRARG("endpt_name");
1454 return -1;
1455 }
1456
Michal Vasko51e514d2016-02-02 15:51:52 +01001457 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001458 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001459 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001460 return -1;
1461 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001462 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001463 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001464 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001465
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001466 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001467}
1468
1469API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001470nc_server_tls_ch_client_add_ctn(const char *client_name, uint32_t id, const char *fingerprint,
1471 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001472{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001473 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001474 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001475
Michal Vasko2e6defd2016-10-07 15:48:15 +02001476 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001477 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001478 return -1;
1479 }
1480
1481 /* LOCK */
1482 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1483 if (!client) {
1484 return -1;
1485 }
1486
1487 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, client->opts.tls);
1488
1489 /* UNLOCK */
1490 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001491
1492 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001493}
1494
1495static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001496nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1497 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001498{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001499 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001500 int ret = -1;
1501
Michal Vasko1a38c862016-01-15 15:50:07 +01001502 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001503 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001504 while (ctn) {
1505 lydict_remove(server_opts.ctx, ctn->fingerprint);
1506 lydict_remove(server_opts.ctx, ctn->name);
1507
1508 next = ctn->next;
1509 free(ctn);
1510 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001511
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001512 ret = 0;
1513 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001514 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001515 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001516 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001517 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001518 while (ctn) {
1519 if (((id < 0) || (ctn->id == id))
1520 && (!fingerprint || !strcmp(ctn->fingerprint, fingerprint))
1521 && (!map_type || (ctn->map_type == map_type))
1522 && (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001523 lydict_remove(server_opts.ctx, ctn->fingerprint);
1524 lydict_remove(server_opts.ctx, ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001525
Michal Vasko5e3f3392016-01-20 11:13:01 +01001526 if (prev) {
1527 prev->next = ctn->next;
1528 next = ctn->next;
1529 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001530 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001531 next = ctn->next;
1532 }
1533 free(ctn);
1534 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001535
1536 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001537 } else {
1538 prev = ctn;
1539 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001540 }
1541 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001542 }
1543
1544 return ret;
1545}
1546
Michal Vaskoc61c4492016-01-25 11:13:34 +01001547API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001548nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1549 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001550{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001551 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001552 struct nc_endpt *endpt;
1553
Michal Vasko2e6defd2016-10-07 15:48:15 +02001554 if (!endpt_name) {
1555 ERRARG("endpt_name");
1556 return -1;
1557 }
1558
Michal Vasko51e514d2016-02-02 15:51:52 +01001559 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001560 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001561 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001562 return -1;
1563 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001564 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001565 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001566 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001567
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001568 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001569}
1570
1571API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001572nc_server_tls_ch_client_del_ctn(const char *client_name, int64_t id, const char *fingerprint,
1573 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001574{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001575 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001576 struct nc_ch_client *client;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001577
Michal Vasko2e6defd2016-10-07 15:48:15 +02001578 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001579 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001580 return -1;
1581 }
1582
1583 /* LOCK */
1584 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1585 if (!client) {
1586 return -1;
1587 }
1588
1589 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, client->opts.tls);
1590
1591 /* UNLOCK */
1592 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001593
1594 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001595}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001596
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001597static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001598nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001599 struct nc_server_tls_opts *opts)
1600{
1601 struct nc_ctn *ctn;
1602 int ret = -1;
1603
1604 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1605 if (id && *id && (*id != ctn->id)) {
1606 continue;
1607 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001608 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001609 continue;
1610 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001611 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001612 continue;
1613 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001614 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001615 continue;
1616 }
1617
1618 /* first match, good enough */
1619 if (id && !(*id)) {
1620 *id = ctn->id;
1621 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001622 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001623 *fingerprint = strdup(ctn->fingerprint);
1624 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001625 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001626 *map_type = ctn->map_type;
1627 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001628 if (name && !(*name) && ctn->name && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001629 *name = strdup(ctn->name);
1630 }
1631
1632 ret = 0;
1633 break;
1634 }
1635
1636 return ret;
1637}
1638
1639API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001640nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1641 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001642{
1643 int ret;
1644 struct nc_endpt *endpt;
1645
1646 if (!endpt_name) {
1647 ERRARG("endpt_name");
1648 return -1;
1649 }
1650
1651 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001652 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001653 if (!endpt) {
1654 return -1;
1655 }
1656 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1657 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001658 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001659
1660 return ret;
1661}
1662
1663API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001664nc_server_tls_ch_client_get_ctn(const char *client_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1665 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001666{
1667 int ret;
1668 struct nc_ch_client *client;
1669
1670 if (!client_name) {
1671 ERRARG("client_name");
1672 return -1;
1673 }
1674
1675 /* LOCK */
1676 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1677 if (!client) {
1678 return -1;
1679 }
1680
1681 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, client->opts.tls);
1682
1683 /* UNLOCK */
1684 nc_server_ch_client_unlock(client);
1685
1686 return ret;
1687}
1688
Michal Vasko709598e2016-11-28 14:48:32 +01001689API const X509 *
1690nc_session_get_client_cert(const struct nc_session *session)
1691{
1692 if (!session || (session->side != NC_SERVER)) {
1693 ERRARG("session");
1694 return NULL;
1695 }
1696
1697 return session->opts.server.client_cert;
1698}
1699
Michal Vasko7060bcf2016-11-28 14:48:11 +01001700API void
1701nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1702{
1703 server_opts.user_verify_clb = verify_clb;
1704}
1705
Michal Vasko3031aae2016-01-27 16:07:18 +01001706void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001707nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001708{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001709 lydict_remove(server_opts.ctx, opts->server_cert);
1710 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001711 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1712 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001713 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001714 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001715}
Michal Vasko9e036d52016-01-08 10:49:26 +01001716
Michal Vasko6d292992016-01-18 09:42:38 +01001717static void
1718nc_tls_make_verify_key(void)
1719{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001720 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001721}
1722
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001723static X509*
1724tls_load_cert(const char *cert_path, const char *cert_data)
1725{
1726 X509 *cert;
1727
1728 if (cert_path) {
1729 cert = pem_to_cert(cert_path);
1730 } else {
1731 cert = base64der_to_cert(cert_data);
1732 }
1733
1734 if (!cert) {
1735 if (cert_path) {
1736 ERR("Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
1737 ERR_reason_error_string(ERR_get_error()));
1738 } else {
1739 ERR("Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
1740 ERR_reason_error_string(ERR_get_error()));
1741 }
1742 }
1743 return cert;
1744}
1745
1746static int
1747nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1748{
1749 char **cert_paths = NULL, **cert_data = NULL;
1750 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1751 X509 *cert = NULL;
1752
1753 if (!server_opts.server_cert_chain_clb) {
1754 /* This is optional, so return OK */
1755 return 0;
1756 }
1757
1758 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
1759 &cert_path_count, &cert_data, &cert_data_count)) {
1760 ERR("Server certificate chain callback failed.");
1761 return -1;
1762 }
1763
1764 for (i = 0; i < cert_path_count; ++i) {
1765 cert = tls_load_cert(cert_paths[i], NULL);
1766 if (!cert || SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1) {
1767 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1768 ret = -1;
1769 goto cleanup;
1770 }
1771 }
1772
1773 for (i = 0; i < cert_data_count; ++i) {
1774 cert = tls_load_cert(NULL, cert_data[i]);
1775 if (!cert || SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1) {
1776 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1777 ret = -1;
1778 goto cleanup;
1779 }
1780 }
1781cleanup:
1782 for (i = 0; i < cert_path_count; ++i) {
1783 free(cert_paths[i]);
1784 }
1785 free(cert_paths);
1786 for (i = 0; i < cert_data_count; ++i) {
1787 free(cert_data[i]);
1788 }
1789 free(cert_data);
1790 /* cert is owned by the SSL_CTX */
1791
1792 return ret;
1793}
1794
Michal Vasko4c1fb492017-01-30 14:31:07 +01001795static int
1796nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1797{
1798 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001799 int ret = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001800 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001801 X509 *cert = NULL;
1802 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001803
1804 if (!cert_name) {
1805 ERR("Server certificate not set.");
1806 return -1;
1807 } else if (!server_opts.server_cert_clb) {
1808 ERR("Callback for retrieving the server certificate is not set.");
1809 return -1;
1810 }
1811
1812 if (server_opts.server_cert_clb(cert_name, server_opts.server_cert_data, &cert_path, &cert_data, &privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001813 &privkey_data, &privkey_type)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001814 ERR("Server certificate callback failed.");
1815 return -1;
1816 }
1817
1818 /* load the certificate */
1819 if (cert_path) {
1820 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
1821 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1822 ret = -1;
1823 goto cleanup;
1824 }
1825 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001826 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001827 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
1828 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1829 ret = -1;
1830 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001831 }
1832 }
1833
1834 /* load the private key */
1835 if (privkey_path) {
1836 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
1837 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1838 ret = -1;
1839 goto cleanup;
1840 }
1841 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001842 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001843 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
1844 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1845 ret = -1;
1846 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001847 }
1848 }
1849
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001850 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1851
Michal Vasko4c1fb492017-01-30 14:31:07 +01001852cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001853 X509_free(cert);
1854 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001855 free(cert_path);
1856 free(cert_data);
1857 free(privkey_path);
1858 free(privkey_data);
1859 return ret;
1860}
1861
1862static void
1863tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1864{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001865 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001866 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001867 return;
1868 }
1869
1870 /* add the trusted certificate */
1871 if (X509_STORE_add_cert(cert_store, cert) != 1) {
1872 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1873 X509_free(cert);
1874 return;
1875 }
1876 X509_free(cert);
1877}
1878
1879static int
1880nc_tls_store_set_trusted_certs(X509_STORE *cert_store, const char **trusted_cert_lists, uint16_t trusted_cert_list_count)
1881{
1882 uint16_t i;
1883 int j;
1884 char **cert_paths, **cert_data;
1885 int cert_path_count, cert_data_count;
1886
1887 if (!server_opts.trusted_cert_list_clb) {
1888 ERR("Callback for retrieving trusted certificate lists is not set.");
1889 return -1;
1890 }
1891
1892 for (i = 0; i < trusted_cert_list_count; ++i) {
1893 cert_paths = cert_data = NULL;
1894 cert_path_count = cert_data_count = 0;
1895 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
1896 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
1897 ERR("Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
1898 return -1;
1899 }
1900
1901 for (j = 0; j < cert_path_count; ++j) {
1902 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1903 free(cert_paths[j]);
1904 }
1905 free(cert_paths);
1906
1907 for (j = 0; j < cert_data_count; ++j) {
1908 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1909 free(cert_data[j]);
1910 }
1911 free(cert_data);
1912 }
1913
1914 return 0;
1915}
1916
Michal Vasko3031aae2016-01-27 16:07:18 +01001917int
Michal Vasko0190bc32016-03-02 15:47:49 +01001918nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001919{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001920 X509_STORE *cert_store;
1921 SSL_CTX *tls_ctx;
1922 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001923 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001924 int ret;
1925 struct timespec ts_timeout, ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001926
Michal Vasko2cc4c682016-03-01 09:16:48 +01001927 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001928
Michal Vaskoe2713da2016-08-22 16:06:40 +02001929 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001930#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1931 tls_ctx = SSL_CTX_new(TLS_server_method());
1932#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001933 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001934#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001935 if (!tls_ctx) {
1936 ERR("Failed to create TLS context.");
1937 goto error;
1938 }
1939 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 +01001940 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001941 goto error;
1942 }
1943
1944 /* X509_STORE, managed (freed) with the context */
1945 cert_store = X509_STORE_new();
1946 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1947
Michal Vasko4c1fb492017-01-30 14:31:07 +01001948 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1949 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001950 }
1951
1952 if (opts->trusted_ca_file) {
1953 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1954 if (!lookup) {
1955 ERR("Failed to add a lookup method.");
1956 goto error;
1957 }
1958
1959 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
1960 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
1961 goto error;
1962 }
1963 }
1964
1965 if (opts->trusted_ca_dir) {
1966 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1967 if (!lookup) {
1968 ERR("Failed to add a lookup method.");
1969 goto error;
1970 }
1971
1972 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
1973 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
1974 goto error;
1975 }
1976 }
1977
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001978 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001979 session->ti.tls = SSL_new(tls_ctx);
1980
Michal Vasko4c1fb492017-01-30 14:31:07 +01001981 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001982 SSL_CTX_free(tls_ctx);
1983 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001984
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001985 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001986 ERR("Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001987 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001988 }
1989
1990 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001991 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001992 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1993
Michal Vasko6d292992016-01-18 09:42:38 +01001994 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001995 pthread_once(&verify_once, nc_tls_make_verify_key);
1996 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001997
Michal Vasko36c7be82017-02-22 13:37:59 +01001998 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001999 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01002000 nc_addtimespec(&ts_timeout, timeout);
2001 }
Michal Vasko0190bc32016-03-02 15:47:49 +01002002 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
2003 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01002004 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02002005 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01002006 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
2007 ERR("SSL_accept timeout.");
2008 return 0;
2009 }
Michal Vasko0190bc32016-03-02 15:47:49 +01002010 }
2011 }
Michal Vaskob48aa812016-01-18 14:13:09 +01002012
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002013 if (ret != 1) {
2014 switch (SSL_get_error(session->ti.tls, ret)) {
2015 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01002016 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002017 break;
2018 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01002019 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002020 break;
2021 default:
Michal Vaskod083db62016-01-19 10:31:29 +01002022 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002023 break;
2024 }
2025 return -1;
2026 }
2027
Michal Vasko1a38c862016-01-15 15:50:07 +01002028 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002029
2030error:
2031 if (sock > -1) {
2032 close(sock);
2033 }
2034 SSL_CTX_free(tls_ctx);
2035 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002036}