blob: 17b1f5bbaea4b19295f3c24ef4ddc47b3ef7991a [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 Vaskoc14e3c82016-01-11 16:14:30 +0100677 if (!cert) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200678 ERRARG("cert");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100679 return -1;
680 }
681
Michal Vaskoe2713da2016-08-22 16:06:40 +0200682 if (opts->server_cert) {
683 X509_free(opts->server_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100684 }
685
Michal Vaskoe2713da2016-08-22 16:06:40 +0200686 opts->server_cert = base64der_to_cert(cert);
687 if (!opts->server_cert) {
Michal Vaskod083db62016-01-19 10:31:29 +0100688 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +0200689 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100690 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100691
692 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100693}
694
695API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100696nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100697{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100698 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100699 struct nc_endpt *endpt;
700
Michal Vasko2e6defd2016-10-07 15:48:15 +0200701 if (!endpt_name) {
702 ERRARG("endpt_name");
703 return -1;
704 }
705
Michal Vasko51e514d2016-02-02 15:51:52 +0100706 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200707 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100708 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100709 return -1;
710 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200711 ret = nc_server_tls_set_cert(cert, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100712 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100713 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100714
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100715 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100716}
717
718API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200719nc_server_tls_ch_client_set_cert(const char *client_name, const char *cert)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100720{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100721 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200722 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100723
Michal Vasko2e6defd2016-10-07 15:48:15 +0200724 if (!client_name) {
725 ERRARG("endpt_name");
726 return -1;
727 }
728
729 /* LOCK */
730 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
731 if (!client) {
732 return -1;
733 }
734
735 ret = nc_server_tls_set_cert(cert, client->opts.tls);
736
737 /* UNLOCK */
738 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100739
740 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100741}
742
743static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100744nc_server_tls_set_cert_path(const char *cert_path, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100745{
Michal Vaskoc61c4492016-01-25 11:13:34 +0100746 if (!cert_path) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200747 ERRARG("cert_path");
Michal Vaskoc61c4492016-01-25 11:13:34 +0100748 return -1;
749 }
750
Michal Vaskoe2713da2016-08-22 16:06:40 +0200751 if (opts->server_cert) {
752 X509_free(opts->server_cert);
Michal Vaskoc61c4492016-01-25 11:13:34 +0100753 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200754 errno = 0;
755 opts->server_cert = pem_to_cert(cert_path);
756 if (!opts->server_cert) {
757 ERR("Loading the server certificate failed (%s).", (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error())));
758 return -1;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100759 }
760
Michal Vaskoc61c4492016-01-25 11:13:34 +0100761 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100762}
763
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100764API int
Michal Vaskoda514772016-02-01 11:32:01 +0100765nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100766{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100767 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100768 struct nc_endpt *endpt;
769
Michal Vasko2e6defd2016-10-07 15:48:15 +0200770 if (!endpt_name) {
771 ERRARG("endpt_name");
772 return -1;
773 }
774
Michal Vasko51e514d2016-02-02 15:51:52 +0100775 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200776 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100777 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100778 return -1;
779 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200780 ret = nc_server_tls_set_cert_path(cert_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100781 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100782 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100783
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100784 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100785}
786
787API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200788nc_server_tls_ch_client_set_cert_path(const char *client_name, const char *cert_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100789{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100790 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200791 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100792
Michal Vasko2e6defd2016-10-07 15:48:15 +0200793 if (!client_name) {
794 ERRARG("endpt_name");
795 return -1;
796 }
797
798 /* LOCK */
799 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
800 if (!client) {
801 return -1;
802 }
803
804 ret = nc_server_tls_set_cert_path(cert_path, client->opts.tls);
805
806 /* UNLOCK */
807 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100808
809 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100810}
811
812static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100813nc_server_tls_set_key(const char *privkey, int is_rsa, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100814{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100815 if (!privkey) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200816 ERRARG("privkey");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100817 return -1;
818 }
819
Michal Vaskoe2713da2016-08-22 16:06:40 +0200820 if (opts->server_key) {
821 EVP_PKEY_free(opts->server_key);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100822 }
823
Michal Vaskoe2713da2016-08-22 16:06:40 +0200824 opts->server_key = base64der_to_privatekey(privkey, is_rsa);
825 if (!opts->server_key) {
Michal Vaskod083db62016-01-19 10:31:29 +0100826 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +0200827 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100828 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100829
830 return 0;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100831}
832
833API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100834nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100835{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100836 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100837 struct nc_endpt *endpt;
838
Michal Vasko2e6defd2016-10-07 15:48:15 +0200839 if (!endpt_name) {
840 ERRARG("endpt_name");
841 return -1;
842 }
843
Michal Vasko51e514d2016-02-02 15:51:52 +0100844 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200845 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100846 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100847 return -1;
848 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200849 ret = nc_server_tls_set_key(privkey, is_rsa, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100850 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100851 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100852
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100853 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100854}
855
856API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200857nc_server_tls_ch_client_set_key(const char *client_name, const char *privkey, int is_rsa)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100858{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100859 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200860 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100861
Michal Vasko2e6defd2016-10-07 15:48:15 +0200862 if (!client_name) {
863 ERRARG("endpt_name");
864 return -1;
865 }
866
867 /* LOCK */
868 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
869 if (!client) {
870 return -1;
871 }
872
873 ret = nc_server_tls_set_key(privkey, is_rsa, client->opts.tls);
874
875 /* UNLOCK */
876 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100877
878 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100879}
880
881static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100882nc_server_tls_set_key_path(const char *privkey_path, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100883{
Michal Vaskoe2713da2016-08-22 16:06:40 +0200884 FILE *file;
885
Michal Vaskoc61c4492016-01-25 11:13:34 +0100886 if (!privkey_path) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200887 ERRARG("privkey_path");
Michal Vaskoc61c4492016-01-25 11:13:34 +0100888 return -1;
889 }
890
Michal Vaskoe2713da2016-08-22 16:06:40 +0200891 file = fopen(privkey_path, "r");
892 if (!file) {
893 ERR("Cannot open the file \"%s\" for reading (%s).", privkey_path, strerror(errno));
894 return -1;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100895 }
896
Michal Vaskoe2713da2016-08-22 16:06:40 +0200897 if (opts->server_key) {
898 EVP_PKEY_free(opts->server_key);
899 }
900 opts->server_key = PEM_read_PrivateKey(file, NULL, NULL, NULL);
901 fclose(file);
902
903 if (!opts->server_key) {
Michal Vaskoc61c4492016-01-25 11:13:34 +0100904 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +0200905 return -1;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100906 }
907
Michal Vaskoc61c4492016-01-25 11:13:34 +0100908 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100909}
910
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100911API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100912nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100913{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100914 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100915 struct nc_endpt *endpt;
916
Michal Vasko2e6defd2016-10-07 15:48:15 +0200917 if (!endpt_name) {
918 ERRARG("endpt_name");
919 return -1;
920 }
921
Michal Vasko51e514d2016-02-02 15:51:52 +0100922 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200923 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100924 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100925 return -1;
926 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200927 ret = nc_server_tls_set_key_path(privkey_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +0100928 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100929 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +0100930
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100931 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100932}
933
934API int
Michal Vasko2e6defd2016-10-07 15:48:15 +0200935nc_server_tls_ch_client_set_key_path(const char *client_name, const char *privkey_path)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100936{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100937 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200938 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100939
Michal Vasko2e6defd2016-10-07 15:48:15 +0200940 if (!client_name) {
941 ERRARG("endpt_name");
942 return -1;
943 }
944
945 /* LOCK */
946 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
947 if (!client) {
948 return -1;
949 }
950
951 ret = nc_server_tls_set_key_path(privkey_path, client->opts.tls);
952
953 /* UNLOCK */
954 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100955
956 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100957}
958
959static int
Michal Vaskoe2713da2016-08-22 16:06:40 +0200960nc_server_tls_add_trusted_cert(const char *name, const char *cert, struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +0100961{
Michal Vaskoe2713da2016-08-22 16:06:40 +0200962 X509 *crt;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100963
964 if (!cert) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200965 ERRARG("cert");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100966 return -1;
967 }
968
Michal Vaskoe2713da2016-08-22 16:06:40 +0200969 crt = base64der_to_cert(cert);
970 if (!crt) {
971 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
972 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100973 }
974
Michal Vaskoe2713da2016-08-22 16:06:40 +0200975 ++opts->trusted_cert_count;
976 opts->trusted_certs = nc_realloc(opts->trusted_certs, opts->trusted_cert_count * sizeof *opts->trusted_certs);
977 if (!opts->trusted_certs) {
978 ERRMEM;
979 X509_free(crt);
980 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100981 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200982 opts->trusted_certs[opts->trusted_cert_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
983 opts->trusted_certs[opts->trusted_cert_count - 1].cert = crt;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100984
985 return 0;
986}
987
988API int
Michal Vaskoe2713da2016-08-22 16:06:40 +0200989nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert_name, const char *cert)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100990{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100991 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100992 struct nc_endpt *endpt;
993
Michal Vasko2e6defd2016-10-07 15:48:15 +0200994 if (!endpt_name) {
995 ERRARG("endpt_name");
996 return -1;
997 }
998
Michal Vasko51e514d2016-02-02 15:51:52 +0100999 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001000 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001001 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001002 return -1;
1003 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001004 ret = nc_server_tls_add_trusted_cert(cert_name, cert, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001005 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001006 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001007
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001008 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001009}
1010
1011API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001012nc_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 +01001013{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001014 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001015 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001016
Michal Vasko2e6defd2016-10-07 15:48:15 +02001017 if (!client_name) {
1018 ERRARG("endpt_name");
1019 return -1;
1020 }
1021
1022 /* LOCK */
1023 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1024 if (!client) {
1025 return -1;
1026 }
1027
1028 ret = nc_server_tls_add_trusted_cert(cert_name, cert, client->opts.tls);
1029
1030 /* UNLOCK */
1031 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001032
1033 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001034}
1035
1036static int
Michal Vaskoe2713da2016-08-22 16:06:40 +02001037nc_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 +01001038{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001039 X509 *cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001040
1041 if (!cert_path) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001042 ERRARG("cert");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001043 return -1;
1044 }
1045
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001046 errno = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001047 cert = pem_to_cert(cert_path);
1048 if (!cert) {
1049 ERR("Loading the server certificate failed (%s).", (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error())));
1050 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001051 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001052
1053 ++opts->trusted_cert_count;
1054 opts->trusted_certs = nc_realloc(opts->trusted_certs, opts->trusted_cert_count * sizeof *opts->trusted_certs);
1055 if (!opts->trusted_certs) {
1056 ERRMEM;
1057 X509_free(cert);
1058 return -1;
1059 }
1060 opts->trusted_certs[opts->trusted_cert_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1061 opts->trusted_certs[opts->trusted_cert_count - 1].cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001062
1063 return 0;
1064}
1065
1066API int
Michal Vaskoe2713da2016-08-22 16:06:40 +02001067nc_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 +01001068{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001069 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001070 struct nc_endpt *endpt;
1071
Michal Vasko2e6defd2016-10-07 15:48:15 +02001072 if (!endpt_name) {
1073 ERRARG("endpt_name");
1074 return -1;
1075 }
1076
Michal Vasko51e514d2016-02-02 15:51:52 +01001077 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001078 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001079 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001080 return -1;
1081 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001082 ret = nc_server_tls_add_trusted_cert_path(cert_name, cert_path, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001083 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001084 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001085
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001086 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001087}
1088
1089API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001090nc_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 +01001091{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001092 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001093 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001094
Michal Vasko2e6defd2016-10-07 15:48:15 +02001095 if (!client_name) {
1096 ERRARG("endpt_name");
1097 return -1;
1098 }
1099
1100 /* LOCK */
1101 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1102 if (!client) {
1103 return -1;
1104 }
1105
1106 ret = nc_server_tls_add_trusted_cert_path(cert_name, cert_path, client->opts.tls);
1107
1108 /* UNLOCK */
1109 nc_server_ch_client_unlock(client);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001110
1111 return ret;
1112}
1113
1114static int
1115nc_server_tls_del_trusted_cert(const char *name, struct nc_server_tls_opts *opts)
1116{
1117 uint16_t i;
1118
1119 if (!name) {
1120 for (i = 0; i < opts->trusted_cert_count; ++i) {
1121 lydict_remove(server_opts.ctx, opts->trusted_certs[i].name);
1122 X509_free(opts->trusted_certs[i].cert);
1123 }
1124 free(opts->trusted_certs);
1125 opts->trusted_certs = NULL;
1126 opts->trusted_cert_count = 0;
1127 return 0;
1128 } else {
1129 for (i = 0; i < opts->trusted_cert_count; ++i) {
1130 if (!strcmp(opts->trusted_certs[i].name, name)) {
1131 lydict_remove(server_opts.ctx, opts->trusted_certs[i].name);
1132 X509_free(opts->trusted_certs[i].cert);
1133
1134 --opts->trusted_cert_count;
1135 if (i < opts->trusted_cert_count - 1) {
1136 memmove(opts->trusted_certs + i, opts->trusted_certs + i + 1,
1137 (opts->trusted_cert_count - i) * sizeof *opts->trusted_certs);
1138 }
1139 return 0;
1140 }
1141 }
1142 }
1143
1144 ERR("Certificate \"%s\" not found.", name);
1145 return -1;
1146}
1147
1148API int
1149nc_server_tls_endpt_del_trusted_cert(const char *endpt_name, const char *cert_name)
1150{
1151 int ret;
1152 struct nc_endpt *endpt;
1153
Michal Vasko2e6defd2016-10-07 15:48:15 +02001154 if (!endpt_name) {
1155 ERRARG("endpt_name");
1156 return -1;
1157 }
1158
Michal Vaskoe2713da2016-08-22 16:06:40 +02001159 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001160 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001161 if (!endpt) {
1162 return -1;
1163 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001164 ret = nc_server_tls_del_trusted_cert(cert_name, endpt->opts.tls);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001165 /* UNLOCK */
1166 nc_server_endpt_unlock(endpt);
1167
1168 return ret;
1169}
1170
1171API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001172nc_server_tls_ch_client_del_trusted_cert(const char *client_name, const char *cert_name)
Michal Vaskoe2713da2016-08-22 16:06:40 +02001173{
1174 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001175 struct nc_ch_client *client;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001176
Michal Vasko2e6defd2016-10-07 15:48:15 +02001177 if (!client_name) {
1178 ERRARG("endpt_name");
1179 return -1;
1180 }
1181
1182 /* LOCK */
1183 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1184 if (!client) {
1185 return -1;
1186 }
1187
1188 ret = nc_server_tls_del_trusted_cert(cert_name, client->opts.tls);
1189
1190 /* UNLOCK */
1191 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001192
1193 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001194}
1195
1196static int
Michal Vasko96830e32016-02-01 10:54:18 +01001197nc_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 +01001198{
Michal Vasko96830e32016-02-01 10:54:18 +01001199 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001200 ERRARG("ca_file and ca_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001201 return -1;
1202 }
1203
Michal Vasko96830e32016-02-01 10:54:18 +01001204 if (ca_file) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001205 if (opts->trusted_ca_file) {
1206 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001207 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001208 opts->trusted_ca_file = lydict_insert(server_opts.ctx, ca_file, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001209 }
1210
Michal Vasko96830e32016-02-01 10:54:18 +01001211 if (ca_dir) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001212 if (opts->trusted_ca_dir) {
1213 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001215 opts->trusted_ca_dir = lydict_insert(server_opts.ctx, ca_dir, 0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001216 }
1217
1218 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001219}
1220
Michal Vaskoc61c4492016-01-25 11:13:34 +01001221API int
Michal Vasko96830e32016-02-01 10:54:18 +01001222nc_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 +01001223{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001224 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001225 struct nc_endpt *endpt;
1226
Michal Vasko2e6defd2016-10-07 15:48:15 +02001227 if (!endpt_name) {
1228 ERRARG("endpt_name");
1229 return -1;
1230 }
1231
Michal Vasko51e514d2016-02-02 15:51:52 +01001232 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001233 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001234 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001235 return -1;
1236 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001237 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001238 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001239 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001240
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001241 return ret;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001242}
1243
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001244API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001245nc_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 +01001246{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001247 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001248 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001249
Michal Vasko2e6defd2016-10-07 15:48:15 +02001250 if (!client_name) {
1251 ERRARG("endpt_name");
1252 return -1;
1253 }
1254
1255 /* LOCK */
1256 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1257 if (!client) {
1258 return -1;
1259 }
1260
1261 ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, client->opts.tls);
1262
1263 /* UNLOCK */
1264 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001265
1266 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001267}
1268
Michal Vaskoc61c4492016-01-25 11:13:34 +01001269static int
Michal Vasko96830e32016-02-01 10:54:18 +01001270nc_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 +01001271{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001272 X509_LOOKUP *lookup;
1273
Michal Vasko96830e32016-02-01 10:54:18 +01001274 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001275 ERRARG("crl_file and crl_dir");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001276 return -1;
1277 }
1278
Michal Vaskoc61c4492016-01-25 11:13:34 +01001279 if (!opts->crl_store) {
1280 opts->crl_store = X509_STORE_new();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001281 }
1282
Michal Vasko96830e32016-02-01 10:54:18 +01001283 if (crl_file) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001284 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001285 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001286 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001287 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001288 }
1289
Michal Vasko96830e32016-02-01 10:54:18 +01001290 if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001291 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001292 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001293 }
1294 }
1295
Michal Vasko96830e32016-02-01 10:54:18 +01001296 if (crl_dir) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001297 lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001298 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001299 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001300 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001301 }
1302
Michal Vasko96830e32016-02-01 10:54:18 +01001303 if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001304 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001305 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001306 }
1307 }
1308
1309 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001310
1311fail:
Michal Vaskob48aa812016-01-18 14:13:09 +01001312 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001313}
1314
Michal Vaskoc61c4492016-01-25 11:13:34 +01001315API int
Michal Vasko96830e32016-02-01 10:54:18 +01001316nc_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 +01001317{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001318 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001319 struct nc_endpt *endpt;
1320
Michal Vasko2e6defd2016-10-07 15:48:15 +02001321 if (!endpt_name) {
1322 ERRARG("endpt_name");
1323 return -1;
1324 }
1325
Michal Vasko51e514d2016-02-02 15:51:52 +01001326 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001327 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001328 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001329 return -1;
1330 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001331 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001332 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001333 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001334
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001335 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001336}
1337
1338API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001339nc_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 +01001340{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001341 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001342 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001343
Michal Vasko2e6defd2016-10-07 15:48:15 +02001344 if (!client_name) {
1345 ERRARG("endpt_name");
1346 return -1;
1347 }
1348
1349 /* LOCK */
1350 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1351 if (!client) {
1352 return -1;
1353 }
1354
1355 ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, client->opts.tls);
1356
1357 /* UNLOCK */
1358 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001359
1360 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001361}
1362
1363static void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001364nc_server_tls_clear_crls(struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001365{
Michal Vaskoc61c4492016-01-25 11:13:34 +01001366 if (!opts->crl_store) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001367 return;
1368 }
1369
1370 X509_STORE_free(opts->crl_store);
1371 opts->crl_store = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001372}
1373
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001374API void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001375nc_server_tls_endpt_clear_crls(const char *endpt_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001376{
Michal Vasko3031aae2016-01-27 16:07:18 +01001377 struct nc_endpt *endpt;
1378
Michal Vasko2e6defd2016-10-07 15:48:15 +02001379 if (!endpt_name) {
1380 ERRARG("endpt_name");
1381 return;
1382 }
1383
Michal Vasko51e514d2016-02-02 15:51:52 +01001384 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001385 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001386 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001387 return;
1388 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001389 nc_server_tls_clear_crls(endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001390 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001391 nc_server_endpt_unlock(endpt);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001392}
1393
Michal Vaskoc61c4492016-01-25 11:13:34 +01001394API void
Michal Vasko2e6defd2016-10-07 15:48:15 +02001395nc_server_tls_ch_client_clear_crls(const char *client_name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001396{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001397 struct nc_ch_client *client;
1398
1399 if (!client_name) {
1400 ERRARG("endpt_name");
1401 return;
1402 }
1403
1404 /* LOCK */
1405 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1406 if (!client) {
1407 return;
1408 }
1409
1410 nc_server_tls_clear_crls(client->opts.tls);
1411
1412 /* UNLOCK */
1413 nc_server_ch_client_unlock(client);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001414}
1415
1416static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001417nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1418 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001419{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001420 struct nc_ctn *ctn, *new;
1421
Michal Vasko45e53ae2016-04-07 11:46:03 +02001422 if (!fingerprint) {
1423 ERRARG("fingerprint");
1424 return -1;
1425 } else if (!map_type) {
1426 ERRARG("map_type");
1427 return -1;
1428 } else if (((map_type == NC_TLS_CTN_SPECIFIED) && !name)
Michal Vasko1a38c862016-01-15 15:50:07 +01001429 || ((map_type != NC_TLS_CTN_SPECIFIED) && name)) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001430 ERRARG("map_type and name");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001431 return -1;
1432 }
1433
Michal Vasko5e3f3392016-01-20 11:13:01 +01001434 new = malloc(sizeof *new);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001435 if (!new) {
1436 ERRMEM;
1437 return -1;
1438 }
Michal Vasko5e3f3392016-01-20 11:13:01 +01001439
Michal Vasko5e3f3392016-01-20 11:13:01 +01001440 new->fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0);
1441 new->name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001442 new->id = id;
1443 new->map_type = map_type;
1444 new->next = NULL;
1445
Michal Vaskoc61c4492016-01-25 11:13:34 +01001446 if (!opts->ctn) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001447 /* the first item */
Michal Vaskoc61c4492016-01-25 11:13:34 +01001448 opts->ctn = new;
1449 } else if (opts->ctn->id > id) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001450 /* insert at the beginning */
Michal Vaskoc61c4492016-01-25 11:13:34 +01001451 new->next = opts->ctn;
1452 opts->ctn = new;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001453 } else {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001454 for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next);
Michal Vasko5e3f3392016-01-20 11:13:01 +01001455 /* insert after ctn */
1456 new->next = ctn->next;
1457 ctn->next = new;
1458 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001459
1460 return 0;
1461}
1462
1463API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001464nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1465 const char *name)
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001466{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001467 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001468 struct nc_endpt *endpt;
1469
Michal Vasko2e6defd2016-10-07 15:48:15 +02001470 if (!endpt_name) {
1471 ERRARG("endpt_name");
1472 return -1;
1473 }
1474
Michal Vasko51e514d2016-02-02 15:51:52 +01001475 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001476 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001477 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001478 return -1;
1479 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001480 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001481 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001482 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001483
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001484 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001485}
1486
1487API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001488nc_server_tls_ch_client_add_ctn(const char *client_name, uint32_t id, const char *fingerprint,
1489 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001490{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001491 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001492 struct nc_ch_client *client;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001493
Michal Vasko2e6defd2016-10-07 15:48:15 +02001494 if (!client_name) {
1495 ERRARG("endpt_name");
1496 return -1;
1497 }
1498
1499 /* LOCK */
1500 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1501 if (!client) {
1502 return -1;
1503 }
1504
1505 ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, client->opts.tls);
1506
1507 /* UNLOCK */
1508 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001509
1510 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001511}
1512
1513static int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001514nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name,
1515 struct nc_server_tls_opts *opts)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001516{
Michal Vasko5e3f3392016-01-20 11:13:01 +01001517 struct nc_ctn *ctn, *next, *prev;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001518 int ret = -1;
1519
Michal Vasko1a38c862016-01-15 15:50:07 +01001520 if ((id < 0) && !fingerprint && !map_type && !name) {
Michal Vaskoc61c4492016-01-25 11:13:34 +01001521 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001522 while (ctn) {
1523 lydict_remove(server_opts.ctx, ctn->fingerprint);
1524 lydict_remove(server_opts.ctx, ctn->name);
1525
1526 next = ctn->next;
1527 free(ctn);
1528 ctn = next;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001529
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001530 ret = 0;
1531 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001532 opts->ctn = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001533 } else {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001534 prev = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001535 ctn = opts->ctn;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001536 while (ctn) {
1537 if (((id < 0) || (ctn->id == id))
1538 && (!fingerprint || !strcmp(ctn->fingerprint, fingerprint))
1539 && (!map_type || (ctn->map_type == map_type))
1540 && (!name || (ctn->name && !strcmp(ctn->name, name)))) {
Michal Vasko5e3f3392016-01-20 11:13:01 +01001541 lydict_remove(server_opts.ctx, ctn->fingerprint);
1542 lydict_remove(server_opts.ctx, ctn->name);
Michal Vasko1a38c862016-01-15 15:50:07 +01001543
Michal Vasko5e3f3392016-01-20 11:13:01 +01001544 if (prev) {
1545 prev->next = ctn->next;
1546 next = ctn->next;
1547 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001548 opts->ctn = ctn->next;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001549 next = ctn->next;
1550 }
1551 free(ctn);
1552 ctn = next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001553
1554 ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +01001555 } else {
1556 prev = ctn;
1557 ctn = ctn->next;
Michal Vasko1a38c862016-01-15 15:50:07 +01001558 }
1559 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001560 }
1561
1562 return ret;
1563}
1564
Michal Vaskoc61c4492016-01-25 11:13:34 +01001565API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001566nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type,
1567 const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001568{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001569 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001570 struct nc_endpt *endpt;
1571
Michal Vasko2e6defd2016-10-07 15:48:15 +02001572 if (!endpt_name) {
1573 ERRARG("endpt_name");
1574 return -1;
1575 }
1576
Michal Vasko51e514d2016-02-02 15:51:52 +01001577 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001578 endpt = nc_server_endpt_lock(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001579 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001580 return -1;
1581 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001582 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vasko51e514d2016-02-02 15:51:52 +01001583 /* UNLOCK */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001584 nc_server_endpt_unlock(endpt);
Michal Vasko3031aae2016-01-27 16:07:18 +01001585
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001586 return ret;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001587}
1588
1589API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001590nc_server_tls_ch_client_del_ctn(const char *client_name, int64_t id, const char *fingerprint,
1591 NC_TLS_CTN_MAPTYPE map_type, const char *name)
Michal Vaskoc61c4492016-01-25 11:13:34 +01001592{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001593 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001594 struct nc_ch_client *client;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001595
Michal Vasko2e6defd2016-10-07 15:48:15 +02001596 if (!client_name) {
1597 ERRARG("endpt_name");
1598 return -1;
1599 }
1600
1601 /* LOCK */
1602 client = nc_server_ch_client_lock(client_name, NC_TI_OPENSSL, NULL);
1603 if (!client) {
1604 return -1;
1605 }
1606
1607 ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, client->opts.tls);
1608
1609 /* UNLOCK */
1610 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001611
1612 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +01001613}
Michal Vaskoc61c4492016-01-25 11:13:34 +01001614
Michal Vasko3031aae2016-01-27 16:07:18 +01001615void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001616nc_server_tls_clear_opts(struct nc_server_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001617{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001618 EVP_PKEY_free(opts->server_key);
1619 X509_free(opts->server_cert);
1620 nc_server_tls_del_trusted_cert(NULL, opts);
1621 lydict_remove(server_opts.ctx, opts->trusted_ca_file);
1622 lydict_remove(server_opts.ctx, opts->trusted_ca_dir);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001623 nc_server_tls_clear_crls(opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001624 nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts);
Michal Vasko086311b2016-01-08 09:53:11 +01001625}
Michal Vasko9e036d52016-01-08 10:49:26 +01001626
Michal Vasko6d292992016-01-18 09:42:38 +01001627static void
1628nc_tls_make_verify_key(void)
1629{
Michal Vasko5c2f7952016-01-22 13:16:31 +01001630 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +01001631}
1632
Michal Vasko3031aae2016-01-27 16:07:18 +01001633int
Michal Vasko0190bc32016-03-02 15:47:49 +01001634nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001635{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001636 X509_STORE *cert_store;
1637 SSL_CTX *tls_ctx;
1638 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001639 struct nc_server_tls_opts *opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001640 int ret, elapsed_usec = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001641 uint16_t i;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001642
Michal Vasko2cc4c682016-03-01 09:16:48 +01001643 opts = session->data;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001644
Michal Vaskoe2713da2016-08-22 16:06:40 +02001645 /* SSL_CTX */
1646 tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
1647 if (!tls_ctx) {
1648 ERR("Failed to create TLS context.");
1649 goto error;
1650 }
1651 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
1652 if (SSL_CTX_use_PrivateKey(tls_ctx, opts->server_key) != 1) {
1653 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
1654 goto error;
1655 }
1656 if (SSL_CTX_use_certificate(tls_ctx, opts->server_cert) != 1) {
1657 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1658 goto error;
1659 }
1660
1661 /* X509_STORE, managed (freed) with the context */
1662 cert_store = X509_STORE_new();
1663 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1664
1665 for (i = 0; i < opts->trusted_cert_count; ++i) {
1666 if (X509_STORE_add_cert(cert_store, opts->trusted_certs[i].cert) != 1) {
1667 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
1668 goto error;
1669 }
1670 }
1671
1672 if (opts->trusted_ca_file) {
1673 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
1674 if (!lookup) {
1675 ERR("Failed to add a lookup method.");
1676 goto error;
1677 }
1678
1679 if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) {
1680 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
1681 goto error;
1682 }
1683 }
1684
1685 if (opts->trusted_ca_dir) {
1686 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
1687 if (!lookup) {
1688 ERR("Failed to add a lookup method.");
1689 goto error;
1690 }
1691
1692 if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) {
1693 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
1694 goto error;
1695 }
1696 }
1697
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001698 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001699 session->ti.tls = SSL_new(tls_ctx);
1700
1701 /* context can be freed already */
1702 SSL_CTX_free(tls_ctx);
1703 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001704
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001705 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001706 ERR("Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001707 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001708 }
1709
1710 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001711 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001712 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1713
Michal Vasko6d292992016-01-18 09:42:38 +01001714 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001715 pthread_once(&verify_once, nc_tls_make_verify_key);
1716 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001717
Michal Vasko0190bc32016-03-02 15:47:49 +01001718 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1719 usleep(NC_TIMEOUT_STEP);
1720 elapsed_usec += NC_TIMEOUT_STEP;
1721 if ((timeout > -1) && (elapsed_usec / 1000 >= timeout)) {
1722 ERR("SSL_accept timeout.");
1723 return 0;
1724 }
1725 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001726
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001727 if (ret != 1) {
1728 switch (SSL_get_error(session->ti.tls, ret)) {
1729 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01001730 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001731 break;
1732 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01001733 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001734 break;
1735 default:
Michal Vaskod083db62016-01-19 10:31:29 +01001736 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001737 break;
1738 }
1739 return -1;
1740 }
1741
Michal Vasko1a38c862016-01-15 15:50:07 +01001742 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001743
1744error:
1745 if (sock > -1) {
1746 close(sock);
1747 }
1748 SSL_CTX_free(tls_ctx);
1749 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001750}