blob: 9e2db7473479395bdd5a380ab4ef57bd466ba621 [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 Vaskoddce1212019-05-24 09:58:49 +0200977 char **privkey_path, char **privkey_data, NC_SSH_KEY *privkey_type),
Michal Vasko4c1fb492017-01-30 14:31:07 +0100978 void *user_data, 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,
992 int *cert_path_count, char ***cert_data, int *cert_data_count),
993 void *user_data, void (*free_user_data)(void *user_data))
994{
995 if (!cert_chain_clb) {
996 ERRARG("cert_chain_clb");
997 return;
998 }
999
1000 server_opts.server_cert_chain_clb = cert_chain_clb;
1001 server_opts.server_cert_chain_data = user_data;
1002 server_opts.server_cert_chain_data_free = free_user_data;
1003}
1004
Michal Vaskoc61c4492016-01-25 11:13:34 +01001005static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001006nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001007{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001008 if (!name) {
1009 ERRARG("name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001010 return -1;
1011 }
1012
Michal Vasko4c1fb492017-01-30 14:31:07 +01001013 ++opts->trusted_cert_list_count;
1014 opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists, opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists);
1015 if (!opts->trusted_cert_lists) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001016 ERRMEM;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001017 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001018 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001019 opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = lydict_insert(server_opts.ctx, name, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001020
1021 return 0;
1022}
1023
1024API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001025nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001026{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001027 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001028 struct nc_endpt *endpt;
1029
Michal Vasko2e6defd2016-10-07 15:48:15 +02001030 if (!endpt_name) {
1031 ERRARG("endpt_name");
1032 return -1;
1033 }
1034
Michal Vasko51e514d2016-02-02 15:51:52 +01001035 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001036 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001037 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001038 return -1;
1039 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001040 ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001041 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001042 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001043
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001044 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001045}
1046
1047API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001048nc_server_tls_ch_client_add_trusted_cert_list(const char *client_name, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001049{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001050 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001051 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001052
Michal Vasko2e6defd2016-10-07 15:48:15 +02001053 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001054 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001055 return -1;
1056 }
1057
1058 /* LOCK */
1059 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1060 if (!client) {
1061 return -1;
1062 }
1063
Michal Vasko4c1fb492017-01-30 14:31:07 +01001064 ret = nc_server_tls_add_trusted_cert_list(name, client->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001065
1066 /* UNLOCK */
1067 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001068
1069 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001070}
1071
Michal Vasko4c1fb492017-01-30 14:31:07 +01001072API void
1073nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths,
1074 int *cert_path_count, char ***cert_data, int *cert_data_count),
1075 void *user_data, void (*free_user_data)(void *user_data))
Michal Vaskoc61c4492016-01-25 11:13:34 +01001076{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001077 if (!cert_list_clb) {
1078 ERRARG("cert_list_clb");
1079 return;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001080 }
1081
Michal Vasko4c1fb492017-01-30 14:31:07 +01001082 server_opts.trusted_cert_list_clb = cert_list_clb;
1083 server_opts.trusted_cert_list_data = user_data;
1084 server_opts.trusted_cert_list_data_free = free_user_data;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001085}
1086
1087static int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001088nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001089{
1090 uint16_t i;
1091
1092 if (!name) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001093 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1094 lydict_remove(server_opts.ctx, opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001095 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001096 free(opts->trusted_cert_lists);
1097 opts->trusted_cert_lists = NULL;
1098 opts->trusted_cert_list_count = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001099 return 0;
1100 } else {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001101 for (i = 0; i < opts->trusted_cert_list_count; ++i) {
1102 if (!strcmp(opts->trusted_cert_lists[i], name)) {
1103 lydict_remove(server_opts.ctx, opts->trusted_cert_lists[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001104
Michal Vasko4c1fb492017-01-30 14:31:07 +01001105 --opts->trusted_cert_list_count;
1106 if (i < opts->trusted_cert_list_count - 1) {
1107 memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1,
1108 (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001109 }
1110 return 0;
1111 }
1112 }
1113 }
1114
Michal Vasko4c1fb492017-01-30 14:31:07 +01001115 ERR("Certificate list \"%s\" not found.", name);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001116 return -1;
1117}
1118
1119API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001120nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001121{
1122 int ret;
1123 struct nc_endpt *endpt;
1124
Michal Vasko2e6defd2016-10-07 15:48:15 +02001125 if (!endpt_name) {
1126 ERRARG("endpt_name");
1127 return -1;
1128 }
1129
Michal Vaskoe2713da2016-08-22 16:06:40 +02001130 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001131 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001132 if (!endpt) {
1133 return -1;
1134 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001135 ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001136 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001137 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001138
1139 return ret;
1140}
1141
1142API int
Michal Vasko4c1fb492017-01-30 14:31:07 +01001143nc_server_tls_ch_client_del_trusted_cert_list(const char *client_name, const char *name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001144{
1145 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001146 struct nc_ch_client *client;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001147
Michal Vasko2e6defd2016-10-07 15:48:15 +02001148 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001149 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001150 return -1;
1151 }
1152
1153 /* LOCK */
1154 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1155 if (!client) {
1156 return -1;
1157 }
1158
Michal Vasko4c1fb492017-01-30 14:31:07 +01001159 ret = nc_server_tls_del_trusted_cert_list(name, client->opts.tls);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001160
1161 /* UNLOCK */
1162 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001163
1164 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001165}
1166
1167static int
Michal Vasko96830e32016-02-01 10:54:18 +01001168nc_server_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001169{
Michal Vasko96830e32016-02-01 10:54:18 +01001170 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001171 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001172 return -1;
1173 }
1174
Michal Vasko96830e32016-02-01 10:54:18 +01001175 if (ca_file) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001176 if (opts->trusted_ca_file) {
1177 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001178 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001179 opts->trusted_ca_file = lydict_insert(server_opts.ctx, ca_file, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001180 }
1181
Michal Vasko96830e32016-02-01 10:54:18 +01001182 if (ca_dir) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001183 if (opts->trusted_ca_dir) {
1184 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001185 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001186 opts->trusted_ca_dir = lydict_insert(server_opts.ctx, ca_dir, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001187 }
1188
1189 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001190}
1191
Michal Vaskoc61c4492016-01-25 11:13:34 +01001192API int
Michal Vasko96830e32016-02-01 10:54:18 +01001193nc_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 +01001194{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001195 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001196 struct nc_endpt *endpt;
1197
Michal Vasko2e6defd2016-10-07 15:48:15 +02001198 if (!endpt_name) {
1199 ERRARG("endpt_name");
1200 return -1;
1201 }
1202
Michal Vasko51e514d2016-02-02 15:51:52 +01001203 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001204 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001205 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001206 return -1;
1207 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001208 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001209 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001210 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001211
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001212 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001213}
1214
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001215API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001216nc_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 +01001217{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001218 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001219 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001220
Michal Vasko2e6defd2016-10-07 15:48:15 +02001221 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001222 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001223 return -1;
1224 }
1225
1226 /* LOCK */
1227 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1228 if (!client) {
1229 return -1;
1230 }
1231
1232 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, client->opts.tls);
1233
1234 /* UNLOCK */
1235 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001236
1237 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001238}
1239
Michal Vaskoc61c4492016-01-25 11:13:34 +01001240static int
Michal Vasko96830e32016-02-01 10:54:18 +01001241nc_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 +01001242{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001243 X509_LOOKUP *lookup;
1244
Michal Vasko96830e32016-02-01 10:54:18 +01001245 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001246 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001247 return -1;
1248 }
1249
Michal Vaskoc61c4492016-01-25 11:13:34 +01001250 if (!opts->crl_store) {
1251 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001252 }
1253
Michal Vasko96830e32016-02-01 10:54:18 +01001254 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001255 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001256 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001257 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001258 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001259 }
1260
Michal Vasko96830e32016-02-01 10:54:18 +01001261 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001262 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001263 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001264 }
1265 }
1266
Michal Vasko96830e32016-02-01 10:54:18 +01001267 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001268 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001269 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001270 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001271 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001272 }
1273
Michal Vasko96830e32016-02-01 10:54:18 +01001274 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001275 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001276 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001277 }
1278 }
1279
1280 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001281
1282fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001283 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001284}
1285
Michal Vaskoc61c4492016-01-25 11:13:34 +01001286API int
Michal Vasko96830e32016-02-01 10:54:18 +01001287nc_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 +01001288{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001289 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001290 struct nc_endpt *endpt;
1291
Michal Vasko2e6defd2016-10-07 15:48:15 +02001292 if (!endpt_name) {
1293 ERRARG("endpt_name");
1294 return -1;
1295 }
1296
Michal Vasko51e514d2016-02-02 15:51:52 +01001297 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001298 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001299 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001300 return -1;
1301 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001302 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001303 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001304 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001305
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001306 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001307}
1308
1309API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001310nc_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 +01001311{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001312 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001313 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001314
Michal Vasko2e6defd2016-10-07 15:48:15 +02001315 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001316 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001317 return -1;
1318 }
1319
1320 /* LOCK */
1321 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1322 if (!client) {
1323 return -1;
1324 }
1325
1326 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, client->opts.tls);
1327
1328 /* UNLOCK */
1329 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001330
1331 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001332}
1333
1334static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001335nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001336{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001337 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001338 return;
1339 }
1340
1341 X509_STORE_free(opts->crl_store);
1342 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001343}
1344
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001345API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001346nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001347{
Michal Vasko3031aae2016-01-27 16:07:18 +01001348 struct nc_endpt *endpt;
1349
Michal Vasko2e6defd2016-10-07 15:48:15 +02001350 if (!endpt_name) {
1351 ERRARG("endpt_name");
1352 return;
1353 }
1354
Michal Vasko51e514d2016-02-02 15:51:52 +01001355 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001356 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001357 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001358 return;
1359 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001360 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001361 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001362 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001363}
1364
Michal Vaskoc61c4492016-01-25 11:13:34 +01001365API void
Michal Vasko2e6defd2016-10-07 15:48:15 +02001366nc_server_tls_ch_client_clear_crls(const char *client_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001367{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001368 struct nc_ch_client *client;
1369
1370 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001371 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001372 return;
1373 }
1374
1375 /* LOCK */
1376 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1377 if (!client) {
1378 return;
1379 }
1380
1381 nc_server_tls_clear_crls(client->opts.tls);
1382
1383 /* UNLOCK */
1384 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001385}
1386
1387static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001388nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1389 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001390{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001391 struct nc_ctn *ctn, *new;
1392
Michal Vaskoc61c4492016-01-25 11:13:34 +01001393 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001394 /* the first item */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001395 opts->ctn = new = calloc(1, sizeof *new);
1396 if (!new) {
1397 ERRMEM;
1398 return -1;
1399 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001400 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001401 /* insert at the beginning */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001402 new = calloc(1, sizeof *new);
1403 if (!new) {
1404 ERRMEM;
1405 return -1;
1406 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001407 new->next = opts->ctn;
1408 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001409 } else {
Andrew Langefeld62bc6712018-08-28 16:17:16 -05001410 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next);
Michal Vasko276f9d92017-02-02 11:15:55 +01001411 if (ctn->id == id) {
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001412 /* it exists already */
Michal Vasko276f9d92017-02-02 11:15:55 +01001413 new = ctn;
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001414 } else {
1415 /* insert after ctn */
1416 new = calloc(1, sizeof *new);
1417 if (!new) {
1418 ERRMEM;
1419 return -1;
1420 }
1421 new->next = ctn->next;
1422 ctn->next = new;
1423 }
1424 }
1425
1426 new->id = id;
1427 if (fingerprint) {
1428 if (new->fingerprint) {
1429 lydict_remove(server_opts.ctx, new->fingerprint);
1430 }
1431 new->fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0);
1432 }
1433 if (map_type) {
1434 new->map_type = map_type;
1435 }
1436 if (name) {
1437 if (new->name) {
1438 lydict_remove(server_opts.ctx, new->name);
1439 }
1440 new->name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001441 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001442
1443 return 0;
1444}
1445
1446API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001447nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1448 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001449{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001450 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001451 struct nc_endpt *endpt;
1452
Michal Vasko2e6defd2016-10-07 15:48:15 +02001453 if (!endpt_name) {
1454 ERRARG("endpt_name");
1455 return -1;
1456 }
1457
Michal Vasko51e514d2016-02-02 15:51:52 +01001458 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001459 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001460 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001461 return -1;
1462 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001463 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001464 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001465 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001466
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001467 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001468}
1469
1470API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001471nc_server_tls_ch_client_add_ctn(const char *client_name, uint32_t id, const char *fingerprint,
1472 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001473{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001474 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001475 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001476
Michal Vasko2e6defd2016-10-07 15:48:15 +02001477 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001478 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001479 return -1;
1480 }
1481
1482 /* LOCK */
1483 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1484 if (!client) {
1485 return -1;
1486 }
1487
1488 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, client->opts.tls);
1489
1490 /* UNLOCK */
1491 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001492
1493 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001494}
1495
1496static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001497nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1498 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001499{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001500 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001501 int ret = -1;
1502
Michal Vasko1a38c862016-01-15 15:50:07 +01001503 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001504 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001505 while (ctn) {
1506 lydict_remove(server_opts.ctx, ctn->fingerprint);
1507 lydict_remove(server_opts.ctx, ctn->name);
1508
1509 next = ctn->next;
1510 free(ctn);
1511 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001512
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001513 ret = 0;
1514 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001515 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001516 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001517 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001518 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001519 while (ctn) {
1520 if (((id < 0) || (ctn->id == id))
1521 && (!fingerprint || !strcmp(ctn->fingerprint, fingerprint))
1522 && (!map_type || (ctn->map_type == map_type))
1523 && (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001524 lydict_remove(server_opts.ctx, ctn->fingerprint);
1525 lydict_remove(server_opts.ctx, ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001526
Michal Vasko5e3f3392016-01-20 11:13:01 +01001527 if (prev) {
1528 prev->next = ctn->next;
1529 next = ctn->next;
1530 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001531 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001532 next = ctn->next;
1533 }
1534 free(ctn);
1535 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001536
1537 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001538 } else {
1539 prev = ctn;
1540 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001541 }
1542 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001543 }
1544
1545 return ret;
1546}
1547
Michal Vaskoc61c4492016-01-25 11:13:34 +01001548API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001549nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1550 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001551{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001552 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001553 struct nc_endpt *endpt;
1554
Michal Vasko2e6defd2016-10-07 15:48:15 +02001555 if (!endpt_name) {
1556 ERRARG("endpt_name");
1557 return -1;
1558 }
1559
Michal Vasko51e514d2016-02-02 15:51:52 +01001560 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001561 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001562 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001563 return -1;
1564 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001565 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001566 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001567 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001568
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001569 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001570}
1571
1572API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001573nc_server_tls_ch_client_del_ctn(const char *client_name, int64_t id, const char *fingerprint,
1574 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001575{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001576 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001577 struct nc_ch_client *client;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001578
Michal Vasko2e6defd2016-10-07 15:48:15 +02001579 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001580 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001581 return -1;
1582 }
1583
1584 /* LOCK */
1585 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1586 if (!client) {
1587 return -1;
1588 }
1589
1590 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, client->opts.tls);
1591
1592 /* UNLOCK */
1593 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001594
1595 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001596}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001597
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001598static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001599nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001600 struct nc_server_tls_opts *opts)
1601{
1602 struct nc_ctn *ctn;
1603 int ret = -1;
1604
1605 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1606 if (id && *id && (*id != ctn->id)) {
1607 continue;
1608 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001609 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001610 continue;
1611 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001612 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001613 continue;
1614 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001615 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001616 continue;
1617 }
1618
1619 /* first match, good enough */
1620 if (id && !(*id)) {
1621 *id = ctn->id;
1622 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001623 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001624 *fingerprint = strdup(ctn->fingerprint);
1625 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001626 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001627 *map_type = ctn->map_type;
1628 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +01001629 if (name && !(*name) && ctn->name && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001630 *name = strdup(ctn->name);
1631 }
1632
1633 ret = 0;
1634 break;
1635 }
1636
1637 return ret;
1638}
1639
1640API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001641nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1642 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001643{
1644 int ret;
1645 struct nc_endpt *endpt;
1646
1647 if (!endpt_name) {
1648 ERRARG("endpt_name");
1649 return -1;
1650 }
1651
1652 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001653 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001654 if (!endpt) {
1655 return -1;
1656 }
1657 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1658 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +01001659 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001660
1661 return ret;
1662}
1663
1664API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001665nc_server_tls_ch_client_get_ctn(const char *client_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1666 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001667{
1668 int ret;
1669 struct nc_ch_client *client;
1670
1671 if (!client_name) {
1672 ERRARG("client_name");
1673 return -1;
1674 }
1675
1676 /* LOCK */
1677 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1678 if (!client) {
1679 return -1;
1680 }
1681
1682 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, client->opts.tls);
1683
1684 /* UNLOCK */
1685 nc_server_ch_client_unlock(client);
1686
1687 return ret;
1688}
1689
Michal Vasko709598e2016-11-28 14:48:32 +01001690API const X509 *
1691nc_session_get_client_cert(const struct nc_session *session)
1692{
1693 if (!session || (session->side != NC_SERVER)) {
1694 ERRARG("session");
1695 return NULL;
1696 }
1697
1698 return session->opts.server.client_cert;
1699}
1700
Michal Vasko7060bcf2016-11-28 14:48:11 +01001701API void
1702nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
1703{
1704 server_opts.user_verify_clb = verify_clb;
1705}
1706
Michal Vasko3031aae2016-01-27 16:07:18 +01001707void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001708nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001709{
Michal Vasko4c1fb492017-01-30 14:31:07 +01001710 lydict_remove(server_opts.ctx, opts->server_cert);
1711 nc_server_tls_del_trusted_cert_list(NULL, opts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001712 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1713 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001714 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001715 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001716}
Michal Vasko9e036d52016-01-08 10:49:26 +01001717
Michal Vasko6d292992016-01-18 09:42:38 +01001718static void
1719nc_tls_make_verify_key(void)
1720{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001721 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001722}
1723
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001724static X509*
1725tls_load_cert(const char *cert_path, const char *cert_data)
1726{
1727 X509 *cert;
1728
1729 if (cert_path) {
1730 cert = pem_to_cert(cert_path);
1731 } else {
1732 cert = base64der_to_cert(cert_data);
1733 }
1734
1735 if (!cert) {
1736 if (cert_path) {
1737 ERR("Loading a trusted certificate (path \"%s\") failed (%s).", cert_path,
1738 ERR_reason_error_string(ERR_get_error()));
1739 } else {
1740 ERR("Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
1741 ERR_reason_error_string(ERR_get_error()));
1742 }
1743 }
1744 return cert;
1745}
1746
1747static int
1748nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name)
1749{
1750 char **cert_paths = NULL, **cert_data = NULL;
1751 int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0;
1752 X509 *cert = NULL;
1753
1754 if (!server_opts.server_cert_chain_clb) {
1755 /* This is optional, so return OK */
1756 return 0;
1757 }
1758
1759 if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths,
1760 &cert_path_count, &cert_data, &cert_data_count)) {
1761 ERR("Server certificate chain callback failed.");
1762 return -1;
1763 }
1764
1765 for (i = 0; i < cert_path_count; ++i) {
1766 cert = tls_load_cert(cert_paths[i], NULL);
1767 if (!cert || SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1) {
1768 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1769 ret = -1;
1770 goto cleanup;
1771 }
1772 }
1773
1774 for (i = 0; i < cert_data_count; ++i) {
1775 cert = tls_load_cert(NULL, cert_data[i]);
1776 if (!cert || SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1) {
1777 ERR("Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error()));
1778 ret = -1;
1779 goto cleanup;
1780 }
1781 }
1782cleanup:
1783 for (i = 0; i < cert_path_count; ++i) {
1784 free(cert_paths[i]);
1785 }
1786 free(cert_paths);
1787 for (i = 0; i < cert_data_count; ++i) {
1788 free(cert_data[i]);
1789 }
1790 free(cert_data);
1791 /* cert is owned by the SSL_CTX */
1792
1793 return ret;
1794}
1795
Michal Vasko4c1fb492017-01-30 14:31:07 +01001796static int
1797nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name)
1798{
1799 char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001800 int ret = 0;
1801 NC_SSH_KEY privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +01001802 X509 *cert = NULL;
1803 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001804
1805 if (!cert_name) {
1806 ERR("Server certificate not set.");
1807 return -1;
1808 } else if (!server_opts.server_cert_clb) {
1809 ERR("Callback for retrieving the server certificate is not set.");
1810 return -1;
1811 }
1812
1813 if (server_opts.server_cert_clb(cert_name, server_opts.server_cert_data, &cert_path, &cert_data, &privkey_path,
Michal Vaskoddce1212019-05-24 09:58:49 +02001814 &privkey_data, &privkey_type)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001815 ERR("Server certificate callback failed.");
1816 return -1;
1817 }
1818
1819 /* load the certificate */
1820 if (cert_path) {
1821 if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
1822 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1823 ret = -1;
1824 goto cleanup;
1825 }
1826 } else {
Michal Vasko6e08cb72017-02-02 11:15:29 +01001827 cert = base64der_to_cert(cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001828 if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) {
1829 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1830 ret = -1;
1831 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001832 }
1833 }
1834
1835 /* load the private key */
1836 if (privkey_path) {
1837 if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
1838 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1839 ret = -1;
1840 goto cleanup;
1841 }
1842 } else {
Michal Vaskoddce1212019-05-24 09:58:49 +02001843 pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001844 if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) {
1845 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1846 ret = -1;
1847 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001848 }
1849 }
1850
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001851 ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name);
1852
Michal Vasko4c1fb492017-01-30 14:31:07 +01001853cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001854 X509_free(cert);
1855 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001856 free(cert_path);
1857 free(cert_data);
1858 free(privkey_path);
1859 free(privkey_data);
1860 return ret;
1861}
1862
1863static void
1864tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data)
1865{
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001866 X509 *cert = tls_load_cert(cert_path, cert_data);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001867 if (!cert) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001868 return;
1869 }
1870
1871 /* add the trusted certificate */
1872 if (X509_STORE_add_cert(cert_store, cert) != 1) {
1873 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1874 X509_free(cert);
1875 return;
1876 }
1877 X509_free(cert);
1878}
1879
1880static int
1881nc_tls_store_set_trusted_certs(X509_STORE *cert_store, const char **trusted_cert_lists, uint16_t trusted_cert_list_count)
1882{
1883 uint16_t i;
1884 int j;
1885 char **cert_paths, **cert_data;
1886 int cert_path_count, cert_data_count;
1887
1888 if (!server_opts.trusted_cert_list_clb) {
1889 ERR("Callback for retrieving trusted certificate lists is not set.");
1890 return -1;
1891 }
1892
1893 for (i = 0; i < trusted_cert_list_count; ++i) {
1894 cert_paths = cert_data = NULL;
1895 cert_path_count = cert_data_count = 0;
1896 if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data,
1897 &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) {
1898 ERR("Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]);
1899 return -1;
1900 }
1901
1902 for (j = 0; j < cert_path_count; ++j) {
1903 tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL);
1904 free(cert_paths[j]);
1905 }
1906 free(cert_paths);
1907
1908 for (j = 0; j < cert_data_count; ++j) {
1909 tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]);
1910 free(cert_data[j]);
1911 }
1912 free(cert_data);
1913 }
1914
1915 return 0;
1916}
1917
Michal Vasko3031aae2016-01-27 16:07:18 +01001918int
Michal Vasko0190bc32016-03-02 15:47:49 +01001919nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001920{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001921 X509_STORE *cert_store;
1922 SSL_CTX *tls_ctx;
1923 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001924 struct nc_server_tls_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001925 int ret;
1926 struct timespec ts_timeout, ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001927
Michal Vasko2cc4c682016-03-01 09:16:48 +01001928 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001929
Michal Vaskoe2713da2016-08-22 16:06:40 +02001930 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001931#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
1932 tls_ctx = SSL_CTX_new(TLS_server_method());
1933#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001934 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001935#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001936 if (!tls_ctx) {
1937 ERR("Failed to create TLS context.");
1938 goto error;
1939 }
1940 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 +01001941 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001942 goto error;
1943 }
1944
1945 /* X509_STORE, managed (freed) with the context */
1946 cert_store = X509_STORE_new();
1947 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1948
Michal Vasko4c1fb492017-01-30 14:31:07 +01001949 if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) {
1950 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001951 }
1952
1953 if (opts->trusted_ca_file) {
1954 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1955 if (!lookup) {
1956 ERR("Failed to add a lookup method.");
1957 goto error;
1958 }
1959
1960 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
1961 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
1962 goto error;
1963 }
1964 }
1965
1966 if (opts->trusted_ca_dir) {
1967 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1968 if (!lookup) {
1969 ERR("Failed to add a lookup method.");
1970 goto error;
1971 }
1972
1973 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
1974 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
1975 goto error;
1976 }
1977 }
1978
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001979 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001980 session->ti.tls = SSL_new(tls_ctx);
1981
Michal Vasko4c1fb492017-01-30 14:31:07 +01001982 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001983 SSL_CTX_free(tls_ctx);
1984 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001985
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001986 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001987 ERR("Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001988 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001989 }
1990
1991 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001992 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001993 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1994
Michal Vasko6d292992016-01-18 09:42:38 +01001995 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001996 pthread_once(&verify_once, nc_tls_make_verify_key);
1997 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001998
Michal Vasko36c7be82017-02-22 13:37:59 +01001999 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02002000 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01002001 nc_addtimespec(&ts_timeout, timeout);
2002 }
Michal Vasko0190bc32016-03-02 15:47:49 +01002003 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
2004 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01002005 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02002006 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01002007 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
2008 ERR("SSL_accept timeout.");
2009 return 0;
2010 }
Michal Vasko0190bc32016-03-02 15:47:49 +01002011 }
2012 }
Michal Vaskob48aa812016-01-18 14:13:09 +01002013
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002014 if (ret != 1) {
2015 switch (SSL_get_error(session->ti.tls, ret)) {
2016 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01002017 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002018 break;
2019 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01002020 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002021 break;
2022 default:
Michal Vaskod083db62016-01-19 10:31:29 +01002023 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002024 break;
2025 }
2026 return -1;
2027 }
2028
Michal Vasko1a38c862016-01-15 15:50:07 +01002029 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002030
2031error:
2032 if (sock > -1) {
2033 close(sock);
2034 }
2035 SSL_CTX_free(tls_ctx);
2036 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002037}