blob: e0095f359fbf8c9d485588b831c488ffee855bae [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 *
39asn1time_to_str(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
137static EVP_PKEY *
138base64der_to_privatekey(const char *in, int rsa)
139{
140 EVP_PKEY *out;
141 char *buf;
142 BIO *bio;
143
144 if (in == NULL) {
145 return NULL;
146 }
147
148 if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", (rsa ? "RSA" : "DSA"), " PRIVATE KEY-----\n", in, "\n-----END ", (rsa ? "RSA" : "DSA"), " PRIVATE KEY-----") == -1) {
149 return NULL;
150 }
151 bio = BIO_new_mem_buf(buf, strlen(buf));
152 if (!bio) {
153 free(buf);
154 return NULL;
155 }
156
157 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
158 if (!out) {
159 free(buf);
160 BIO_free(bio);
161 return NULL;
162 }
163
164 free(buf);
165 BIO_free(bio);
166 return out;
167}
168
169static int
170cert_pubkey_match(X509 *cert1, X509 *cert2)
171{
172 ASN1_BIT_STRING *bitstr1, *bitstr2;
173
174 bitstr1 = X509_get0_pubkey_bitstr(cert1);
175 bitstr2 = X509_get0_pubkey_bitstr(cert2);
176
177 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
178 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
179 return 0;
180 }
181
182 return 1;
183}
184
185static int
186nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
187{
188 STACK_OF(GENERAL_NAME) *san_names;
189 GENERAL_NAME *san_name;
190 ASN1_OCTET_STRING *ip;
191 int i, san_count;
192 char *subject, *common_name;
193
194 if (map_type == NC_TLS_CTN_COMMON_NAME) {
195 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
196 common_name = strstr(subject, "CN=");
197 if (!common_name) {
Michal Vaskod083db62016-01-19 10:31:29 +0100198 WRN("Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100199 free(subject);
200 return 1;
201 }
202 common_name += 3;
203 if (strchr(common_name, '/')) {
204 *strchr(common_name, '/') = '\0';
205 }
206 *username = strdup(common_name);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100207 if (!*username) {
208 ERRMEM;
209 return 1;
210 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100211 free(subject);
212 } else {
213 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
214 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
215 if (!san_names) {
Michal Vaskod083db62016-01-19 10:31:29 +0100216 WRN("Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100217 return 1;
218 }
219
220 san_count = sk_GENERAL_NAME_num(san_names);
221 for (i = 0; i < san_count; ++i) {
222 san_name = sk_GENERAL_NAME_value(san_names, i);
223
224 /* rfc822Name (email) */
225 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_RFC822_NAME) &&
226 san_name->type == GEN_EMAIL) {
227 *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100228 if (!*username) {
229 ERRMEM;
230 return 1;
231 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100232 break;
233 }
234
235 /* dNSName */
236 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_DNS_NAME) &&
237 san_name->type == GEN_DNS) {
238 *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100239 if (!*username) {
240 ERRMEM;
241 return 1;
242 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100243 break;
244 }
245
246 /* iPAddress */
247 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_IP_ADDRESS) &&
248 san_name->type == GEN_IPADD) {
249 ip = san_name->d.iPAddress;
250 if (ip->length == 4) {
251 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 +0100252 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100253 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
254 return -1;
255 }
256 break;
257 } else if (ip->length == 16) {
258 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
259 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
260 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
261 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100262 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100263 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
264 return -1;
265 }
266 break;
267 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100268 WRN("SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100269 }
270 }
271 }
272 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
273
274 if (i < san_count) {
275 switch (map_type) {
276 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100277 WRN("Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100278 break;
279 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100280 WRN("Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100281 break;
282 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vaskod083db62016-01-19 10:31:29 +0100283 WRN("Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100284 break;
285 case NC_TLS_CTN_SAN_ANY:
Michal Vaskod083db62016-01-19 10:31:29 +0100286 WRN("Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100287 break;
288 default:
289 break;
290 }
291 return 1;
292 }
293 }
294
295 return 0;
296}
297
298/* return: 0 - OK, 1 - no match, -1 - error */
299static int
Michal Vaskoc61c4492016-01-25 11:13:34 +0100300nc_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 +0100301{
302 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
303 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100304 unsigned char *buf = malloc(64);
305 unsigned int buf_len = 64;
306 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100307 struct nc_ctn *ctn;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100308
Michal Vasko4eb3c312016-03-01 14:09:37 +0100309 if (!buf) {
310 ERRMEM;
311 return -1;
312 }
313
Michal Vaskoc61c4492016-01-25 11:13:34 +0100314 if (!ctn_first || !cert || !map_type || !name) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100315 free(buf);
316 return -1;
317 }
318
Michal Vaskoc61c4492016-01-25 11:13:34 +0100319 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100320 /* MD5 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100321 if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100322 if (!digest_md5) {
323 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100324 ERR("Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100325 ret = -1;
326 goto cleanup;
327 }
328 digest_to_str(buf, buf_len, &digest_md5);
329 }
330
Michal Vasko5e3f3392016-01-20 11:13:01 +0100331 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100332 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100333 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100334 *map_type = ctn->map_type;
335 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
336 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100337 }
338 break;
339 }
340
341 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100342 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100343 if (!digest_sha1) {
344 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100345 ERR("Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100346 ret = -1;
347 goto cleanup;
348 }
349 digest_to_str(buf, buf_len, &digest_sha1);
350 }
351
Michal Vasko5e3f3392016-01-20 11:13:01 +0100352 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100353 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100354 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100355 *map_type = ctn->map_type;
356 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
357 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100358 }
359 break;
360 }
361
362 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100363 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100364 if (!digest_sha224) {
365 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100366 ERR("Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100367 ret = -1;
368 goto cleanup;
369 }
370 digest_to_str(buf, buf_len, &digest_sha224);
371 }
372
Michal Vasko5e3f3392016-01-20 11:13:01 +0100373 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100374 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100375 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100376 *map_type = ctn->map_type;
377 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
378 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100379 }
380 break;
381 }
382
383 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100384 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100385 if (!digest_sha256) {
386 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100387 ERR("Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100388 ret = -1;
389 goto cleanup;
390 }
391 digest_to_str(buf, buf_len, &digest_sha256);
392 }
393
Michal Vasko5e3f3392016-01-20 11:13:01 +0100394 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100395 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100396 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100397 *map_type = ctn->map_type;
398 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
399 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100400 }
401 break;
402 }
403
404 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100405 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100406 if (!digest_sha384) {
407 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100408 ERR("Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100409 ret = -1;
410 goto cleanup;
411 }
412 digest_to_str(buf, buf_len, &digest_sha384);
413 }
414
Michal Vasko5e3f3392016-01-20 11:13:01 +0100415 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100416 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100417 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100418 *map_type = ctn->map_type;
419 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
420 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100421 }
422 break;
423 }
424
425 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100426 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100427 if (!digest_sha512) {
428 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100429 ERR("Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100430 ret = -1;
431 goto cleanup;
432 }
433 digest_to_str(buf, buf_len, &digest_sha512);
434 }
435
Michal Vasko5e3f3392016-01-20 11:13:01 +0100436 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100437 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100438 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vasko5e3f3392016-01-20 11:13:01 +0100439 *map_type = ctn->map_type;
440 if (ctn->map_type == NC_TLS_CTN_SPECIFIED) {
441 *name = ctn->name;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100442 }
443 break;
444 }
445
446 /* unknown */
447 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +0100448 WRN("Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100449 }
450 }
451
Michal Vasko5e3f3392016-01-20 11:13:01 +0100452 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100453 ret = 1;
454 }
455
456cleanup:
457 free(digest_md5);
458 free(digest_sha1);
459 free(digest_sha224);
460 free(digest_sha256);
461 free(digest_sha384);
462 free(digest_sha512);
463 free(buf);
464 return ret;
465}
466
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100467static int
468nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
469{
470 X509_STORE_CTX store_ctx;
471 X509_OBJECT obj;
472 X509_NAME *subject;
473 X509_NAME *issuer;
474 X509 *cert;
475 X509_CRL *crl;
476 X509_REVOKED *revoked;
477 STACK_OF(X509) *cert_stack;
478 EVP_PKEY *pubkey;
479 struct nc_session* session;
Michal Vasko3031aae2016-01-27 16:07:18 +0100480 struct nc_server_tls_opts *opts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100481 long serial;
482 int i, n, rc, depth;
483 char *cp;
484 const char *username = NULL;
485 NC_TLS_CTN_MAPTYPE map_type = 0;
486 ASN1_TIME *last_update = NULL, *next_update = NULL;
487
Michal Vasko6d292992016-01-18 09:42:38 +0100488 /* get the thread session */
Michal Vasko5c2f7952016-01-22 13:16:31 +0100489 session = pthread_getspecific(verify_key);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100490 if (!session) {
491 ERRINT;
492 return 0;
493 }
494
Michal Vasko2cc4c682016-03-01 09:16:48 +0100495 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100496
497 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200498 if (!session->opts.server.client_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100499 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
500 /* TODO all that is needed, but function X509_up_ref not present in older OpenSSL versions
501 session->cert = sk_X509_value(cert_stack, sk_X509_num(cert_stack) - 1);
502 X509_up_ref(session->cert);
503 sk_X509_pop_free(cert_stack, X509_free); */
504 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200505 X509_free(session->opts.server.client_cert);
506 session->opts.server.client_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100507 }
508 sk_X509_pop_free(cert_stack, X509_free);
509 }
510
511 /* standard certificate verification failed, so a trusted client cert must match to continue */
512 if (!preverify_ok) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200513 subject = X509_get_subject_name(session->opts.server.client_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100514 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
515 if (cert_stack) {
516 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200517 if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100518 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
519 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100520 VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100521 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
522 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
523 sk_X509_pop_free(cert_stack, X509_free);
524 return 1;
525 }
526 }
527 sk_X509_pop_free(cert_stack, X509_free);
528 }
529
530 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
531 return 0;
532 }
533
534 /* print cert verify info */
535 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vaskod083db62016-01-19 10:31:29 +0100536 VRB("Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100537
538 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
539 subject = X509_get_subject_name(cert);
540 issuer = X509_get_issuer_name(cert);
541
542 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100543 VRB("Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100544 OPENSSL_free(cp);
545 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100546 VRB("Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100547 OPENSSL_free(cp);
548
549 /* check for revocation if set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100550 if (opts->crl_store) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100551 /* try to retrieve a CRL corresponding to the _subject_ of
552 * the current certificate in order to verify it's integrity */
553 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100554 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100555 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
556 X509_STORE_CTX_cleanup(&store_ctx);
557 crl = obj.data.crl;
558 if (rc > 0 && crl) {
559 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100560 VRB("Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100561 OPENSSL_free(cp);
562
563 last_update = X509_CRL_get_lastUpdate(crl);
564 next_update = X509_CRL_get_nextUpdate(crl);
565 cp = asn1time_to_str(last_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100566 VRB("Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100567 free(cp);
568 cp = asn1time_to_str(next_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100569 VRB("Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100570 free(cp);
571
572 /* verify the signature on this CRL */
573 pubkey = X509_get_pubkey(cert);
574 if (X509_CRL_verify(crl, pubkey) <= 0) {
575 ERR("Cert verify CRL: invalid signature.");
576 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
577 X509_OBJECT_free_contents(&obj);
578 if (pubkey) {
579 EVP_PKEY_free(pubkey);
580 }
581 return 0;
582 }
583 if (pubkey) {
584 EVP_PKEY_free(pubkey);
585 }
586
587 /* check date of CRL to make sure it's not expired */
588 if (!next_update) {
589 ERR("Cert verify CRL: invalid nextUpdate field.");
590 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
591 X509_OBJECT_free_contents(&obj);
592 return 0;
593 }
594 if (X509_cmp_current_time(next_update) < 0) {
595 ERR("Cert verify CRL: expired - revoking all certificates.");
596 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
597 X509_OBJECT_free_contents(&obj);
598 return 0;
599 }
600 X509_OBJECT_free_contents(&obj);
601 }
602
603 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100604 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100605 memset((char *)&obj, 0, sizeof(obj));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100606 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100607 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
608 X509_STORE_CTX_cleanup(&store_ctx);
609 crl = obj.data.crl;
610 if (rc > 0 && crl) {
611 /* check if the current certificate is revoked by this CRL */
612 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
613 for (i = 0; i < n; i++) {
614 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
615 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
616 serial = ASN1_INTEGER_get(revoked->serialNumber);
617 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100618 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 +0100619 OPENSSL_free(cp);
620 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
621 X509_OBJECT_free_contents(&obj);
622 return 0;
623 }
624 }
625 X509_OBJECT_free_contents(&obj);
626 }
627 }
628
629 /* cert-to-name already successful */
630 if (session->username) {
631 return 1;
632 }
633
634 /* cert-to-name */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100635 rc = nc_tls_cert_to_name(opts->ctn, cert, &map_type, &username);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100636
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100637 if (rc) {
638 if (rc == -1) {
639 /* fatal error */
640 depth = 0;
641 }
642 /* rc == 1 is a normal CTN fail (no match found) */
643 goto fail;
644 }
645
646 /* cert-to-name match, now to extract the specific field from the peer cert */
647 if (map_type == NC_TLS_CTN_SPECIFIED) {
648 session->username = lydict_insert(server_opts.ctx, username, 0);
649 } else {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200650 rc = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100651 if (rc) {
652 if (rc == -1) {
653 depth = 0;
654 }
655 goto fail;
656 }
657 session->username = lydict_insert_zc(server_opts.ctx, cp);
658 }
659
660 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
661 return 1;
662
663fail:
664 if (depth > 0) {
665 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
666 return 1;
667 }
668
669 VRB("Cert-to-name unsuccessful, dropping the new client.");
670 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
671 return 0;
672}
673
Michal Vasko3031aae2016-01-27 16:07:18 +0100674static int
675nc_server_tls_set_cert(const char *cert, struct nc_server_tls_opts *opts)
676{
Michal Vaskoa8748792016-11-22 14:34:26 +0100677 X509 *crt;
678
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100679 if (!cert) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100680 if (opts->server_cert) {
681 X509_free(opts->server_cert);
682 }
683 opts->server_cert = NULL;
684 return 0;
685 }
686
687 crt = base64der_to_cert(cert);
688 if (!crt) {
689 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100690 return -1;
691 }
692
Michal Vaskoe2713da2016-08-22 16:06:40 +0200693 if (opts->server_cert) {
694 X509_free(opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100695 }
Michal Vaskoa8748792016-11-22 14:34:26 +0100696 opts->server_cert = crt;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100697
698 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100699}
700
701API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100702nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100703{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100704 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100705 struct nc_endpt *endpt;
706
Michal Vasko2e6defd2016-10-07 15:48:15 +0200707 if (!endpt_name) {
708 ERRARG("endpt_name");
709 return -1;
710 }
711
Michal Vasko51e514d2016-02-02 15:51:52 +0100712 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200713 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100714 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100715 return -1;
716 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200717 ret = nc_server_tls_set_cert(cert, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100718 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100719 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100720
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100721 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100722}
723
724API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200725nc_server_tls_ch_client_set_cert(const char *client_name, const char *cert)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100726{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100727 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200728 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100729
Michal Vasko2e6defd2016-10-07 15:48:15 +0200730 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +0100731 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +0200732 return -1;
733 }
734
735 /* LOCK */
736 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
737 if (!client) {
738 return -1;
739 }
740
741 ret = nc_server_tls_set_cert(cert, client->opts.tls);
742
743 /* UNLOCK */
744 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100745
746 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100747}
748
749static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100750nc_server_tls_set_cert_path(const char *cert_path, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100751{
Michal Vaskoa8748792016-11-22 14:34:26 +0100752 X509 *crt;
753
Michal Vaskoc61c4492016-01-25 11:13:34 +0100754 if (!cert_path) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100755 if (opts->server_cert) {
756 X509_free(opts->server_cert);
757 }
758 opts->server_cert = NULL;
759 return 0;
760 }
761
762 errno = 0;
763 crt = pem_to_cert(cert_path);
764 if (!crt) {
765 ERR("Loading the server certificate failed (%s).", (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error())));
Michal Vaskoc61c4492016-01-25 11:13:34 +0100766 return -1;
767 }
768
Michal Vaskoe2713da2016-08-22 16:06:40 +0200769 if (opts->server_cert) {
770 X509_free(opts->server_cert);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100771 }
Michal Vaskoa8748792016-11-22 14:34:26 +0100772 opts->server_cert = crt;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100773
Michal Vaskoc61c4492016-01-25 11:13:34 +0100774 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100775}
776
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100777API int
Michal Vaskoda514772016-02-01 11:32:01 +0100778nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100779{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100780 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100781 struct nc_endpt *endpt;
782
Michal Vasko2e6defd2016-10-07 15:48:15 +0200783 if (!endpt_name) {
784 ERRARG("endpt_name");
785 return -1;
786 }
787
Michal Vasko51e514d2016-02-02 15:51:52 +0100788 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200789 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100790 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100791 return -1;
792 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200793 ret = nc_server_tls_set_cert_path(cert_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100794 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100795 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100796
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100797 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100798}
799
800API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200801nc_server_tls_ch_client_set_cert_path(const char *client_name, const char *cert_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100802{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100803 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200804 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100805
Michal Vasko2e6defd2016-10-07 15:48:15 +0200806 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +0100807 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +0200808 return -1;
809 }
810
811 /* LOCK */
812 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
813 if (!client) {
814 return -1;
815 }
816
817 ret = nc_server_tls_set_cert_path(cert_path, client->opts.tls);
818
819 /* UNLOCK */
820 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100821
822 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100823}
824
825static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100826nc_server_tls_set_key(const char *privkey, int is_rsa, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100827{
Michal Vaskoa8748792016-11-22 14:34:26 +0100828 EVP_PKEY *pkey;
829
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100830 if (!privkey) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100831 if (opts->server_key) {
832 EVP_PKEY_free(opts->server_key);
833 }
834 opts->server_key = NULL;
835 return 0;
836 }
837
838 pkey = base64der_to_privatekey(privkey, is_rsa);
839 if (!pkey) {
840 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100841 return -1;
842 }
843
Michal Vaskoe2713da2016-08-22 16:06:40 +0200844 if (opts->server_key) {
845 EVP_PKEY_free(opts->server_key);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100846 }
Michal Vaskoa8748792016-11-22 14:34:26 +0100847 opts->server_key = pkey;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100848
849 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100850}
851
852API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100853nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100854{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100855 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100856 struct nc_endpt *endpt;
857
Michal Vasko2e6defd2016-10-07 15:48:15 +0200858 if (!endpt_name) {
859 ERRARG("endpt_name");
860 return -1;
861 }
862
Michal Vasko51e514d2016-02-02 15:51:52 +0100863 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200864 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100865 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100866 return -1;
867 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200868 ret = nc_server_tls_set_key(privkey, is_rsa, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100869 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100870 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100871
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100872 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100873}
874
875API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200876nc_server_tls_ch_client_set_key(const char *client_name, const char *privkey, int is_rsa)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100877{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100878 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200879 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100880
Michal Vasko2e6defd2016-10-07 15:48:15 +0200881 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +0100882 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +0200883 return -1;
884 }
885
886 /* LOCK */
887 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
888 if (!client) {
889 return -1;
890 }
891
892 ret = nc_server_tls_set_key(privkey, is_rsa, client->opts.tls);
893
894 /* UNLOCK */
895 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100896
897 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100898}
899
900static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100901nc_server_tls_set_key_path(const char *privkey_path, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100902{
Michal Vaskoe2713da2016-08-22 16:06:40 +0200903 FILE *file;
904
Michal Vaskoc61c4492016-01-25 11:13:34 +0100905 if (!privkey_path) {
Michal Vaskoa8748792016-11-22 14:34:26 +0100906 if (opts->server_key) {
907 EVP_PKEY_free(opts->server_key);
908 }
909 opts->server_key = NULL;
910 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100911 }
912
Michal Vaskoe2713da2016-08-22 16:06:40 +0200913 file = fopen(privkey_path, "r");
914 if (!file) {
915 ERR("Cannot open the file \"%s\" for reading (%s).", privkey_path, strerror(errno));
916 return -1;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100917 }
918
Michal Vaskoe2713da2016-08-22 16:06:40 +0200919 if (opts->server_key) {
920 EVP_PKEY_free(opts->server_key);
921 }
922 opts->server_key = PEM_read_PrivateKey(file, NULL, NULL, NULL);
923 fclose(file);
924
925 if (!opts->server_key) {
Michal Vaskoc61c4492016-01-25 11:13:34 +0100926 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +0200927 return -1;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100928 }
929
Michal Vaskoc61c4492016-01-25 11:13:34 +0100930 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100931}
932
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100933API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100934nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100935{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100936 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100937 struct nc_endpt *endpt;
938
Michal Vasko2e6defd2016-10-07 15:48:15 +0200939 if (!endpt_name) {
940 ERRARG("endpt_name");
941 return -1;
942 }
943
Michal Vasko51e514d2016-02-02 15:51:52 +0100944 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200945 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100946 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100947 return -1;
948 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200949 ret = nc_server_tls_set_key_path(privkey_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100950 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100951 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100952
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100953 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100954}
955
956API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200957nc_server_tls_ch_client_set_key_path(const char *client_name, const char *privkey_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100958{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100959 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200960 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100961
Michal Vasko2e6defd2016-10-07 15:48:15 +0200962 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +0100963 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +0200964 return -1;
965 }
966
967 /* LOCK */
968 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
969 if (!client) {
970 return -1;
971 }
972
973 ret = nc_server_tls_set_key_path(privkey_path, client->opts.tls);
974
975 /* UNLOCK */
976 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100977
978 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100979}
980
981static int
Michal Vaskoe2713da2016-08-22 16:06:40 +0200982nc_server_tls_add_trusted_cert(const char *name, const char *cert, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100983{
Michal Vaskoe2713da2016-08-22 16:06:40 +0200984 X509 *crt;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100985
986 if (!cert) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200987 ERRARG("cert");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100988 return -1;
989 }
990
Michal Vaskoe2713da2016-08-22 16:06:40 +0200991 crt = base64der_to_cert(cert);
992 if (!crt) {
993 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
994 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100995 }
996
Michal Vaskoe2713da2016-08-22 16:06:40 +0200997 ++opts->trusted_cert_count;
998 opts->trusted_certs = nc_realloc(opts->trusted_certs, opts->trusted_cert_count * sizeof *opts->trusted_certs);
999 if (!opts->trusted_certs) {
1000 ERRMEM;
1001 X509_free(crt);
1002 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001003 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001004 opts->trusted_certs[opts->trusted_cert_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1005 opts->trusted_certs[opts->trusted_cert_count - 1].cert = crt;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006
1007 return 0;
1008}
1009
1010API int
Michal Vaskoe2713da2016-08-22 16:06:40 +02001011nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert_name, const char *cert)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001013 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 struct nc_endpt *endpt;
1015
Michal Vasko2e6defd2016-10-07 15:48:15 +02001016 if (!endpt_name) {
1017 ERRARG("endpt_name");
1018 return -1;
1019 }
1020
Michal Vasko51e514d2016-02-02 15:51:52 +01001021 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001022 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001023 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001024 return -1;
1025 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001026 ret = nc_server_tls_add_trusted_cert(cert_name, cert, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001027 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001028 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001029
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001030 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001031}
1032
1033API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001034nc_server_tls_ch_client_add_trusted_cert(const char *client_name, const char *cert_name, const char *cert)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001035{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001036 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001037 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001038
Michal Vasko2e6defd2016-10-07 15:48:15 +02001039 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001040 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001041 return -1;
1042 }
1043
1044 /* LOCK */
1045 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1046 if (!client) {
1047 return -1;
1048 }
1049
1050 ret = nc_server_tls_add_trusted_cert(cert_name, cert, client->opts.tls);
1051
1052 /* UNLOCK */
1053 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001054
1055 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001056}
1057
1058static int
Michal Vaskoe2713da2016-08-22 16:06:40 +02001059nc_server_tls_add_trusted_cert_path(const char *name, const char *cert_path, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001060{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001061 X509 *cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001062
1063 if (!cert_path) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001064 ERRARG("cert");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001065 return -1;
1066 }
1067
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001068 errno = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001069 cert = pem_to_cert(cert_path);
1070 if (!cert) {
1071 ERR("Loading the server certificate failed (%s).", (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error())));
1072 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001073 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001074
1075 ++opts->trusted_cert_count;
1076 opts->trusted_certs = nc_realloc(opts->trusted_certs, opts->trusted_cert_count * sizeof *opts->trusted_certs);
1077 if (!opts->trusted_certs) {
1078 ERRMEM;
1079 X509_free(cert);
1080 return -1;
1081 }
1082 opts->trusted_certs[opts->trusted_cert_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1083 opts->trusted_certs[opts->trusted_cert_count - 1].cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001084
1085 return 0;
1086}
1087
1088API int
Michal Vaskoe2713da2016-08-22 16:06:40 +02001089nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_name, const char *cert_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001090{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001091 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001092 struct nc_endpt *endpt;
1093
Michal Vasko2e6defd2016-10-07 15:48:15 +02001094 if (!endpt_name) {
1095 ERRARG("endpt_name");
1096 return -1;
1097 }
1098
Michal Vasko51e514d2016-02-02 15:51:52 +01001099 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001100 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001101 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001102 return -1;
1103 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001104 ret = nc_server_tls_add_trusted_cert_path(cert_name, cert_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001105 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001106 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001107
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001108 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001109}
1110
1111API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001112nc_server_tls_ch_client_add_trusted_cert_path(const char *client_name, const char *cert_name, const char *cert_path)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001113{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001114 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001115 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001116
Michal Vasko2e6defd2016-10-07 15:48:15 +02001117 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001118 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001119 return -1;
1120 }
1121
1122 /* LOCK */
1123 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1124 if (!client) {
1125 return -1;
1126 }
1127
1128 ret = nc_server_tls_add_trusted_cert_path(cert_name, cert_path, client->opts.tls);
1129
1130 /* UNLOCK */
1131 nc_server_ch_client_unlock(client);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001132
1133 return ret;
1134}
1135
1136static int
1137nc_server_tls_del_trusted_cert(const char *name, struct nc_server_tls_opts *opts)
1138{
1139 uint16_t i;
1140
1141 if (!name) {
1142 for (i = 0; i < opts->trusted_cert_count; ++i) {
1143 lydict_remove(server_opts.ctx, opts->trusted_certs[i].name);
1144 X509_free(opts->trusted_certs[i].cert);
1145 }
1146 free(opts->trusted_certs);
1147 opts->trusted_certs = NULL;
1148 opts->trusted_cert_count = 0;
1149 return 0;
1150 } else {
1151 for (i = 0; i < opts->trusted_cert_count; ++i) {
1152 if (!strcmp(opts->trusted_certs[i].name, name)) {
1153 lydict_remove(server_opts.ctx, opts->trusted_certs[i].name);
1154 X509_free(opts->trusted_certs[i].cert);
1155
1156 --opts->trusted_cert_count;
1157 if (i < opts->trusted_cert_count - 1) {
1158 memmove(opts->trusted_certs + i, opts->trusted_certs + i + 1,
1159 (opts->trusted_cert_count - i) * sizeof *opts->trusted_certs);
1160 }
1161 return 0;
1162 }
1163 }
1164 }
1165
1166 ERR("Certificate \"%s\" not found.", name);
1167 return -1;
1168}
1169
1170API int
1171nc_server_tls_endpt_del_trusted_cert(const char *endpt_name, const char *cert_name)
1172{
1173 int ret;
1174 struct nc_endpt *endpt;
1175
Michal Vasko2e6defd2016-10-07 15:48:15 +02001176 if (!endpt_name) {
1177 ERRARG("endpt_name");
1178 return -1;
1179 }
1180
Michal Vaskoe2713da2016-08-22 16:06:40 +02001181 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001182 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001183 if (!endpt) {
1184 return -1;
1185 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001186 ret = nc_server_tls_del_trusted_cert(cert_name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001187 /* UNLOCK */
1188 nc_server_endpt_unlock(endpt);
1189
1190 return ret;
1191}
1192
1193API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001194nc_server_tls_ch_client_del_trusted_cert(const char *client_name, const char *cert_name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001195{
1196 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001197 struct nc_ch_client *client;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001198
Michal Vasko2e6defd2016-10-07 15:48:15 +02001199 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001200 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001201 return -1;
1202 }
1203
1204 /* LOCK */
1205 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1206 if (!client) {
1207 return -1;
1208 }
1209
1210 ret = nc_server_tls_del_trusted_cert(cert_name, client->opts.tls);
1211
1212 /* UNLOCK */
1213 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001214
1215 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001216}
1217
1218static int
Michal Vasko96830e32016-02-01 10:54:18 +01001219nc_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 +01001220{
Michal Vasko96830e32016-02-01 10:54:18 +01001221 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001222 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001223 return -1;
1224 }
1225
Michal Vasko96830e32016-02-01 10:54:18 +01001226 if (ca_file) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001227 if (opts->trusted_ca_file) {
1228 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001229 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001230 opts->trusted_ca_file = lydict_insert(server_opts.ctx, ca_file, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001231 }
1232
Michal Vasko96830e32016-02-01 10:54:18 +01001233 if (ca_dir) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001234 if (opts->trusted_ca_dir) {
1235 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001236 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001237 opts->trusted_ca_dir = lydict_insert(server_opts.ctx, ca_dir, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001238 }
1239
1240 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001241}
1242
Michal Vaskoc61c4492016-01-25 11:13:34 +01001243API int
Michal Vasko96830e32016-02-01 10:54:18 +01001244nc_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 +01001245{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001246 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001247 struct nc_endpt *endpt;
1248
Michal Vasko2e6defd2016-10-07 15:48:15 +02001249 if (!endpt_name) {
1250 ERRARG("endpt_name");
1251 return -1;
1252 }
1253
Michal Vasko51e514d2016-02-02 15:51:52 +01001254 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001255 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001256 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001257 return -1;
1258 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001259 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001260 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001261 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001262
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001263 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001264}
1265
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001266API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001267nc_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 +01001268{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001269 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001270 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001271
Michal Vasko2e6defd2016-10-07 15:48:15 +02001272 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001273 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001274 return -1;
1275 }
1276
1277 /* LOCK */
1278 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1279 if (!client) {
1280 return -1;
1281 }
1282
1283 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, client->opts.tls);
1284
1285 /* UNLOCK */
1286 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001287
1288 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001289}
1290
Michal Vaskoc61c4492016-01-25 11:13:34 +01001291static int
Michal Vasko96830e32016-02-01 10:54:18 +01001292nc_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 +01001293{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001294 X509_LOOKUP *lookup;
1295
Michal Vasko96830e32016-02-01 10:54:18 +01001296 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001297 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001298 return -1;
1299 }
1300
Michal Vaskoc61c4492016-01-25 11:13:34 +01001301 if (!opts->crl_store) {
1302 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001303 }
1304
Michal Vasko96830e32016-02-01 10:54:18 +01001305 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001306 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001307 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001308 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001309 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001310 }
1311
Michal Vasko96830e32016-02-01 10:54:18 +01001312 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001313 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001314 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001315 }
1316 }
1317
Michal Vasko96830e32016-02-01 10:54:18 +01001318 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001319 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001320 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001321 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001322 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001323 }
1324
Michal Vasko96830e32016-02-01 10:54:18 +01001325 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001326 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001327 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001328 }
1329 }
1330
1331 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001332
1333fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001334 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335}
1336
Michal Vaskoc61c4492016-01-25 11:13:34 +01001337API int
Michal Vasko96830e32016-02-01 10:54:18 +01001338nc_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 +01001339{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001340 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001341 struct nc_endpt *endpt;
1342
Michal Vasko2e6defd2016-10-07 15:48:15 +02001343 if (!endpt_name) {
1344 ERRARG("endpt_name");
1345 return -1;
1346 }
1347
Michal Vasko51e514d2016-02-02 15:51:52 +01001348 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001349 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001350 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001351 return -1;
1352 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001353 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001354 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001355 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001356
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001357 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001358}
1359
1360API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001361nc_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 +01001362{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001363 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001364 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001365
Michal Vasko2e6defd2016-10-07 15:48:15 +02001366 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001367 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001368 return -1;
1369 }
1370
1371 /* LOCK */
1372 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1373 if (!client) {
1374 return -1;
1375 }
1376
1377 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, client->opts.tls);
1378
1379 /* UNLOCK */
1380 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001381
1382 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001383}
1384
1385static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001386nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001387{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001388 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001389 return;
1390 }
1391
1392 X509_STORE_free(opts->crl_store);
1393 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001394}
1395
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001396API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001397nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001398{
Michal Vasko3031aae2016-01-27 16:07:18 +01001399 struct nc_endpt *endpt;
1400
Michal Vasko2e6defd2016-10-07 15:48:15 +02001401 if (!endpt_name) {
1402 ERRARG("endpt_name");
1403 return;
1404 }
1405
Michal Vasko51e514d2016-02-02 15:51:52 +01001406 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001407 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001408 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001409 return;
1410 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001411 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001412 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001413 nc_server_endpt_unlock(endpt);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001414}
1415
Michal Vaskoc61c4492016-01-25 11:13:34 +01001416API void
Michal Vasko2e6defd2016-10-07 15:48:15 +02001417nc_server_tls_ch_client_clear_crls(const char *client_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001418{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001419 struct nc_ch_client *client;
1420
1421 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001422 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001423 return;
1424 }
1425
1426 /* LOCK */
1427 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1428 if (!client) {
1429 return;
1430 }
1431
1432 nc_server_tls_clear_crls(client->opts.tls);
1433
1434 /* UNLOCK */
1435 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001436}
1437
1438static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001439nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1440 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001441{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001442 struct nc_ctn *ctn, *new;
1443
Michal Vasko45e53ae2016-04-07 11:46:03 +02001444 if (!fingerprint) {
1445 ERRARG("fingerprint");
1446 return -1;
1447 } else if (!map_type) {
1448 ERRARG("map_type");
1449 return -1;
1450 } else if (((map_type == NC_TLS_CTN_SPECIFIED) && !name)
Michal Vasko1a38c862016-01-15 15:50:07 +01001451 || ((map_type != NC_TLS_CTN_SPECIFIED) && name)) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001452 ERRARG("map_type and name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001453 return -1;
1454 }
1455
Michal Vasko5e3f3392016-01-20 11:13:01 +01001456 new = malloc(sizeof *new);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001457 if (!new) {
1458 ERRMEM;
1459 return -1;
1460 }
Michal Vasko5e3f3392016-01-20 11:13:01 +01001461
Michal Vasko5e3f3392016-01-20 11:13:01 +01001462 new->fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0);
1463 new->name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001464 new->id = id;
1465 new->map_type = map_type;
1466 new->next = NULL;
1467
Michal Vaskoc61c4492016-01-25 11:13:34 +01001468 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001469 /* the first item */
Michal Vaskoc61c4492016-01-25 11:13:34 +01001470 opts->ctn = new;
1471 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001472 /* insert at the beginning */
Michal Vaskoc61c4492016-01-25 11:13:34 +01001473 new->next = opts->ctn;
1474 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001475 } else {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001476 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001477 /* insert after ctn */
1478 new->next = ctn->next;
1479 ctn->next = new;
1480 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001481
1482 return 0;
1483}
1484
1485API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001486nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1487 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001488{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001489 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001490 struct nc_endpt *endpt;
1491
Michal Vasko2e6defd2016-10-07 15:48:15 +02001492 if (!endpt_name) {
1493 ERRARG("endpt_name");
1494 return -1;
1495 }
1496
Michal Vasko51e514d2016-02-02 15:51:52 +01001497 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001498 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001499 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001500 return -1;
1501 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001502 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001503 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001504 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001505
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001506 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001507}
1508
1509API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001510nc_server_tls_ch_client_add_ctn(const char *client_name, uint32_t id, const char *fingerprint,
1511 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001512{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001513 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001514 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001515
Michal Vasko2e6defd2016-10-07 15:48:15 +02001516 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001517 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001518 return -1;
1519 }
1520
1521 /* LOCK */
1522 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1523 if (!client) {
1524 return -1;
1525 }
1526
1527 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, client->opts.tls);
1528
1529 /* UNLOCK */
1530 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001531
1532 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001533}
1534
1535static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001536nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1537 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001538{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001539 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001540 int ret = -1;
1541
Michal Vasko1a38c862016-01-15 15:50:07 +01001542 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001543 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001544 while (ctn) {
1545 lydict_remove(server_opts.ctx, ctn->fingerprint);
1546 lydict_remove(server_opts.ctx, ctn->name);
1547
1548 next = ctn->next;
1549 free(ctn);
1550 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001551
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001552 ret = 0;
1553 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001554 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001555 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001556 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001557 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001558 while (ctn) {
1559 if (((id < 0) || (ctn->id == id))
1560 && (!fingerprint || !strcmp(ctn->fingerprint, fingerprint))
1561 && (!map_type || (ctn->map_type == map_type))
1562 && (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001563 lydict_remove(server_opts.ctx, ctn->fingerprint);
1564 lydict_remove(server_opts.ctx, ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001565
Michal Vasko5e3f3392016-01-20 11:13:01 +01001566 if (prev) {
1567 prev->next = ctn->next;
1568 next = ctn->next;
1569 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001570 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001571 next = ctn->next;
1572 }
1573 free(ctn);
1574 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001575
1576 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001577 } else {
1578 prev = ctn;
1579 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001580 }
1581 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001582 }
1583
1584 return ret;
1585}
1586
Michal Vaskoc61c4492016-01-25 11:13:34 +01001587API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001588nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1589 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001590{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001591 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001592 struct nc_endpt *endpt;
1593
Michal Vasko2e6defd2016-10-07 15:48:15 +02001594 if (!endpt_name) {
1595 ERRARG("endpt_name");
1596 return -1;
1597 }
1598
Michal Vasko51e514d2016-02-02 15:51:52 +01001599 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001600 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001601 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001602 return -1;
1603 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001604 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001605 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001606 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001607
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001608 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001609}
1610
1611API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001612nc_server_tls_ch_client_del_ctn(const char *client_name, int64_t id, const char *fingerprint,
1613 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001614{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001615 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001616 struct nc_ch_client *client;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001617
Michal Vasko2e6defd2016-10-07 15:48:15 +02001618 if (!client_name) {
Michal Vasko9bf49542016-11-23 13:50:15 +01001619 ERRARG("client_name");
Michal Vasko2e6defd2016-10-07 15:48:15 +02001620 return -1;
1621 }
1622
1623 /* LOCK */
1624 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1625 if (!client) {
1626 return -1;
1627 }
1628
1629 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, client->opts.tls);
1630
1631 /* UNLOCK */
1632 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001633
1634 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001635}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001636
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001637static int
Michal Vaskof585ac72016-11-25 15:16:38 +01001638nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001639 struct nc_server_tls_opts *opts)
1640{
1641 struct nc_ctn *ctn;
1642 int ret = -1;
1643
1644 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
1645 if (id && *id && (*id != ctn->id)) {
1646 continue;
1647 }
1648 if (fingerprint && *fingerprint && strcmp(*fingerprint, ctn->fingerprint)) {
1649 continue;
1650 }
1651 if (map_type && *map_type && (*map_type != ctn->map_type)) {
1652 continue;
1653 }
1654 if (name && *name && strcmp(*name, ctn->name)) {
1655 continue;
1656 }
1657
1658 /* first match, good enough */
1659 if (id && !(*id)) {
1660 *id = ctn->id;
1661 }
1662 if (fingerprint && !(*fingerprint)) {
1663 *fingerprint = strdup(ctn->fingerprint);
1664 }
1665 if (map_type && !(*map_type)) {
1666 *map_type = ctn->map_type;
1667 }
1668 if (name && !(*name) && ctn->name) {
1669 *name = strdup(ctn->name);
1670 }
1671
1672 ret = 0;
1673 break;
1674 }
1675
1676 return ret;
1677}
1678
1679API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001680nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1681 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001682{
1683 int ret;
1684 struct nc_endpt *endpt;
1685
1686 if (!endpt_name) {
1687 ERRARG("endpt_name");
1688 return -1;
1689 }
1690
1691 /* LOCK */
1692 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
1693 if (!endpt) {
1694 return -1;
1695 }
1696 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
1697 /* UNLOCK */
1698 nc_server_endpt_unlock(endpt);
1699
1700 return ret;
1701}
1702
1703API int
Michal Vaskof585ac72016-11-25 15:16:38 +01001704nc_server_tls_ch_client_get_ctn(const char *client_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
1705 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +01001706{
1707 int ret;
1708 struct nc_ch_client *client;
1709
1710 if (!client_name) {
1711 ERRARG("client_name");
1712 return -1;
1713 }
1714
1715 /* LOCK */
1716 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1717 if (!client) {
1718 return -1;
1719 }
1720
1721 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, client->opts.tls);
1722
1723 /* UNLOCK */
1724 nc_server_ch_client_unlock(client);
1725
1726 return ret;
1727}
1728
Michal Vasko3031aae2016-01-27 16:07:18 +01001729void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001730nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001731{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001732 EVP_PKEY_free(opts->server_key);
1733 X509_free(opts->server_cert);
1734 nc_server_tls_del_trusted_cert(NULL, opts);
1735 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1736 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001737 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001738 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001739}
Michal Vasko9e036d52016-01-08 10:49:26 +01001740
Michal Vasko6d292992016-01-18 09:42:38 +01001741static void
1742nc_tls_make_verify_key(void)
1743{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001744 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001745}
1746
Michal Vasko3031aae2016-01-27 16:07:18 +01001747int
Michal Vasko0190bc32016-03-02 15:47:49 +01001748nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001749{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001750 X509_STORE *cert_store;
1751 SSL_CTX *tls_ctx;
1752 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001753 struct nc_server_tls_opts *opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001754 int ret, elapsed_usec = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001755 uint16_t i;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001756
Michal Vasko2cc4c682016-03-01 09:16:48 +01001757 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001758
Michal Vaskoe2713da2016-08-22 16:06:40 +02001759 /* SSL_CTX */
1760 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
1761 if (!tls_ctx) {
1762 ERR("Failed to create TLS context.");
1763 goto error;
1764 }
1765 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
1766 if (SSL_CTX_use_PrivateKey(tls_ctx, opts->server_key) != 1) {
1767 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1768 goto error;
1769 }
1770 if (SSL_CTX_use_certificate(tls_ctx, opts->server_cert) != 1) {
1771 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1772 goto error;
1773 }
1774
1775 /* X509_STORE, managed (freed) with the context */
1776 cert_store = X509_STORE_new();
1777 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1778
1779 for (i = 0; i < opts->trusted_cert_count; ++i) {
1780 if (X509_STORE_add_cert(cert_store, opts->trusted_certs[i].cert) != 1) {
1781 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1782 goto error;
1783 }
1784 }
1785
1786 if (opts->trusted_ca_file) {
1787 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1788 if (!lookup) {
1789 ERR("Failed to add a lookup method.");
1790 goto error;
1791 }
1792
1793 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
1794 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
1795 goto error;
1796 }
1797 }
1798
1799 if (opts->trusted_ca_dir) {
1800 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1801 if (!lookup) {
1802 ERR("Failed to add a lookup method.");
1803 goto error;
1804 }
1805
1806 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
1807 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
1808 goto error;
1809 }
1810 }
1811
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001812 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001813 session->ti.tls = SSL_new(tls_ctx);
1814
1815 /* context can be freed already */
1816 SSL_CTX_free(tls_ctx);
1817 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001818
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001819 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001820 ERR("Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001821 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001822 }
1823
1824 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001825 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001826 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1827
Michal Vasko6d292992016-01-18 09:42:38 +01001828 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001829 pthread_once(&verify_once, nc_tls_make_verify_key);
1830 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001831
Michal Vasko0190bc32016-03-02 15:47:49 +01001832 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1833 usleep(NC_TIMEOUT_STEP);
1834 elapsed_usec += NC_TIMEOUT_STEP;
1835 if ((timeout > -1) && (elapsed_usec / 1000 >= timeout)) {
1836 ERR("SSL_accept timeout.");
1837 return 0;
1838 }
1839 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001840
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001841 if (ret != 1) {
1842 switch (SSL_get_error(session->ti.tls, ret)) {
1843 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01001844 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001845 break;
1846 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01001847 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001848 break;
1849 default:
Michal Vaskod083db62016-01-19 10:31:29 +01001850 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001851 break;
1852 }
1853 return -1;
1854 }
1855
Michal Vasko1a38c862016-01-15 15:50:07 +01001856 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001857
1858error:
1859 if (sock > -1) {
1860 close(sock);
1861 }
1862 SSL_CTX_free(tls_ctx);
1863 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001864}