blob: d5adf0b3c693dc5f112d386f16becc5e0d0d2504 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
Michal Vaskoc14e3c82016-01-11 16:14:30 +010023#define _GNU_SOURCE
24
25#include <string.h>
26#include <poll.h>
27
28#include <openssl/ssl.h>
29#include <openssl/evp.h>
30#include <openssl/err.h>
31#include <openssl/x509v3.h>
32
Michal Vasko1a38c862016-01-15 15:50:07 +010033#include "libnetconf.h"
Radek Krejci5da708a2015-09-01 17:33:23 +020034
Michal Vaskoc14e3c82016-01-11 16:14:30 +010035extern struct nc_server_opts server_opts;
Michal Vasko6d292992016-01-18 09:42:38 +010036struct nc_tls_server_opts tls_opts = {
Michal Vaskob48aa812016-01-18 14:13:09 +010037 .tls_ctx_lock = PTHREAD_MUTEX_INITIALIZER,
38 .crl_lock = PTHREAD_MUTEX_INITIALIZER,
39 .ctn_lock = PTHREAD_MUTEX_INITIALIZER,
Michal Vasko6d292992016-01-18 09:42:38 +010040 .verify_once = PTHREAD_ONCE_INIT
41};
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042
43static char *
44asn1time_to_str(ASN1_TIME *t)
Michal Vasko086311b2016-01-08 09:53:11 +010045{
Michal Vaskoc14e3c82016-01-11 16:14:30 +010046 char *cp;
47 BIO *bio;
48 int n;
Radek Krejci5da708a2015-09-01 17:33:23 +020049
Michal Vaskoc14e3c82016-01-11 16:14:30 +010050 if (!t) {
51 return NULL;
52 }
53 bio = BIO_new(BIO_s_mem());
54 if (!bio) {
55 return NULL;
56 }
57 ASN1_TIME_print(bio, t);
58 n = BIO_pending(bio);
59 cp = malloc(n + 1);
60 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);
77 for (i = 0; i < dig_len - 1; ++i) {
78 sprintf((*str) + (i * 3), "%02x:", digest[i]);
79 }
80 sprintf((*str) + (i * 3), "%02x", digest[i]);
81}
82
83/* return NULL - SSL error can be retrieved */
84static X509 *
85base64der_to_cert(const char *in)
86{
87 X509 *out;
88 char *buf;
89 BIO *bio;
90
91 if (in == NULL) {
92 return NULL;
93 }
94
95 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
96 return NULL;
97 }
98 bio = BIO_new_mem_buf(buf, strlen(buf));
99 if (!bio) {
100 free(buf);
101 return NULL;
102 }
103
104 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
105 if (!out) {
106 free(buf);
107 BIO_free(bio);
108 return NULL;
109 }
110
111 free(buf);
112 BIO_free(bio);
113 return out;
114}
115
116/* return NULL - either errno or SSL error */
117static X509 *
118pem_to_cert(const char *path)
119{
120 FILE *fp;
121 X509 *out;
122
123 fp = fopen(path, "r");
124 if (!fp) {
125 return NULL;
126 }
127
128 out = PEM_read_X509(fp, NULL, NULL, NULL);
129 fclose(fp);
130 return out;
131}
132
133static EVP_PKEY *
134base64der_to_privatekey(const char *in, int rsa)
135{
136 EVP_PKEY *out;
137 char *buf;
138 BIO *bio;
139
140 if (in == NULL) {
141 return NULL;
142 }
143
144 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) {
145 return NULL;
146 }
147 bio = BIO_new_mem_buf(buf, strlen(buf));
148 if (!bio) {
149 free(buf);
150 return NULL;
151 }
152
153 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
154 if (!out) {
155 free(buf);
156 BIO_free(bio);
157 return NULL;
158 }
159
160 free(buf);
161 BIO_free(bio);
162 return out;
163}
164
165static int
166cert_pubkey_match(X509 *cert1, X509 *cert2)
167{
168 ASN1_BIT_STRING *bitstr1, *bitstr2;
169
170 bitstr1 = X509_get0_pubkey_bitstr(cert1);
171 bitstr2 = X509_get0_pubkey_bitstr(cert2);
172
173 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
174 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
175 return 0;
176 }
177
178 return 1;
179}
180
181static int
182nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
183{
184 STACK_OF(GENERAL_NAME) *san_names;
185 GENERAL_NAME *san_name;
186 ASN1_OCTET_STRING *ip;
187 int i, san_count;
188 char *subject, *common_name;
189
190 if (map_type == NC_TLS_CTN_COMMON_NAME) {
191 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
192 common_name = strstr(subject, "CN=");
193 if (!common_name) {
Michal Vaskod083db62016-01-19 10:31:29 +0100194 WRN("Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100195 free(subject);
196 return 1;
197 }
198 common_name += 3;
199 if (strchr(common_name, '/')) {
200 *strchr(common_name, '/') = '\0';
201 }
202 *username = strdup(common_name);
203 free(subject);
204 } else {
205 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
206 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
207 if (!san_names) {
Michal Vaskod083db62016-01-19 10:31:29 +0100208 WRN("Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100209 return 1;
210 }
211
212 san_count = sk_GENERAL_NAME_num(san_names);
213 for (i = 0; i < san_count; ++i) {
214 san_name = sk_GENERAL_NAME_value(san_names, i);
215
216 /* rfc822Name (email) */
217 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_RFC822_NAME) &&
218 san_name->type == GEN_EMAIL) {
219 *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name));
220 break;
221 }
222
223 /* dNSName */
224 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_DNS_NAME) &&
225 san_name->type == GEN_DNS) {
226 *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName));
227 break;
228 }
229
230 /* iPAddress */
231 if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_IP_ADDRESS) &&
232 san_name->type == GEN_IPADD) {
233 ip = san_name->d.iPAddress;
234 if (ip->length == 4) {
235 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 +0100236 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100237 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
238 return -1;
239 }
240 break;
241 } else if (ip->length == 16) {
242 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
243 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
244 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
245 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100246 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100247 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
248 return -1;
249 }
250 break;
251 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100252 WRN("SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100253 }
254 }
255 }
256 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
257
258 if (i < san_count) {
259 switch (map_type) {
260 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100261 WRN("Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100262 break;
263 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vaskod083db62016-01-19 10:31:29 +0100264 WRN("Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100265 break;
266 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vaskod083db62016-01-19 10:31:29 +0100267 WRN("Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100268 break;
269 case NC_TLS_CTN_SAN_ANY:
Michal Vaskod083db62016-01-19 10:31:29 +0100270 WRN("Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100271 break;
272 default:
273 break;
274 }
275 return 1;
276 }
277 }
278
279 return 0;
280}
281
282/* return: 0 - OK, 1 - no match, -1 - error */
283static int
284nc_tls_cert_to_name(X509 *cert, NC_TLS_CTN_MAPTYPE *map_type, const char **name)
285{
286 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
287 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
288 uint16_t i;
289 unsigned char *buf = malloc(64);
290 unsigned int buf_len = 64;
291 int ret = 0;
292
Michal Vaskob48aa812016-01-18 14:13:09 +0100293 if (!cert || !map_type || !name) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100294 free(buf);
295 return -1;
296 }
297
Michal Vaskob48aa812016-01-18 14:13:09 +0100298 /* LOCK */
299 pthread_mutex_lock(&tls_opts.ctn_lock);
300
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100301 for (i = 0; i < tls_opts.ctn_count; ++i) {
302 /* MD5 */
303 if (!strncmp(tls_opts.ctn[i].fingerprint, "01", 2)) {
304 if (!digest_md5) {
305 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100306 ERR("Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100307 ret = -1;
308 goto cleanup;
309 }
310 digest_to_str(buf, buf_len, &digest_md5);
311 }
312
313 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_md5)) {
314 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100315 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100316 *map_type = tls_opts.ctn[i].map_type;
317 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
318 *name = tls_opts.ctn[i].name;
319 }
320 break;
321 }
322
323 /* SHA-1 */
324 } else if (!strncmp(tls_opts.ctn[i].fingerprint, "02", 2)) {
325 if (!digest_sha1) {
326 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100327 ERR("Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100328 ret = -1;
329 goto cleanup;
330 }
331 digest_to_str(buf, buf_len, &digest_sha1);
332 }
333
334 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha1)) {
335 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100336 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100337 *map_type = tls_opts.ctn[i].map_type;
338 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
339 *name = tls_opts.ctn[i].name;
340 }
341 break;
342 }
343
344 /* SHA-224 */
345 } else if (!strncmp(tls_opts.ctn[i].fingerprint, "03", 2)) {
346 if (!digest_sha224) {
347 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100348 ERR("Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100349 ret = -1;
350 goto cleanup;
351 }
352 digest_to_str(buf, buf_len, &digest_sha224);
353 }
354
355 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha224)) {
356 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100357 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100358 *map_type = tls_opts.ctn[i].map_type;
359 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
360 *name = tls_opts.ctn[i].name;
361 }
362 break;
363 }
364
365 /* SHA-256 */
366 } else if (!strncmp(tls_opts.ctn[i].fingerprint, "04", 2)) {
367 if (!digest_sha256) {
368 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100369 ERR("Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100370 ret = -1;
371 goto cleanup;
372 }
373 digest_to_str(buf, buf_len, &digest_sha256);
374 }
375
376 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha256)) {
377 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100378 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100379 *map_type = tls_opts.ctn[i].map_type;
380 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
381 *name = tls_opts.ctn[i].name;
382 }
383 break;
384 }
385
386 /* SHA-384 */
387 } else if (!strncmp(tls_opts.ctn[i].fingerprint, "05", 2)) {
388 if (!digest_sha384) {
389 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100390 ERR("Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100391 ret = -1;
392 goto cleanup;
393 }
394 digest_to_str(buf, buf_len, &digest_sha384);
395 }
396
397 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha384)) {
398 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100399 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100400 *map_type = tls_opts.ctn[i].map_type;
401 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
402 *name = tls_opts.ctn[i].name;
403 }
404 break;
405 }
406
407 /* SHA-512 */
408 } else if (!strncmp(tls_opts.ctn[i].fingerprint, "06", 2)) {
409 if (!digest_sha512) {
410 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100411 ERR("Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100412 ret = -1;
413 goto cleanup;
414 }
415 digest_to_str(buf, buf_len, &digest_sha512);
416 }
417
418 if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha512)) {
419 /* we got ourselves a winner! */
Michal Vaskod083db62016-01-19 10:31:29 +0100420 VRB("Cert verify CTN: entry with a matching fingerprint found.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100421 *map_type = tls_opts.ctn[i].map_type;
422 if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) {
423 *name = tls_opts.ctn[i].name;
424 }
425 break;
426 }
427
428 /* unknown */
429 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100430 WRN("Unknown fingerprint algorithm used (%s), skipping.", tls_opts.ctn[i].fingerprint);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100431 }
432 }
433
434 if (i == tls_opts.ctn_count) {
435 ret = 1;
436 }
437
438cleanup:
Michal Vaskob48aa812016-01-18 14:13:09 +0100439 /* UNLOCK */
440 pthread_mutex_unlock(&tls_opts.ctn_lock);
441
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100442 free(digest_md5);
443 free(digest_sha1);
444 free(digest_sha224);
445 free(digest_sha256);
446 free(digest_sha384);
447 free(digest_sha512);
448 free(buf);
449 return ret;
450}
451
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100452static int
453nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
454{
455 X509_STORE_CTX store_ctx;
456 X509_OBJECT obj;
457 X509_NAME *subject;
458 X509_NAME *issuer;
459 X509 *cert;
460 X509_CRL *crl;
461 X509_REVOKED *revoked;
462 STACK_OF(X509) *cert_stack;
463 EVP_PKEY *pubkey;
464 struct nc_session* session;
465 long serial;
466 int i, n, rc, depth;
467 char *cp;
468 const char *username = NULL;
469 NC_TLS_CTN_MAPTYPE map_type = 0;
470 ASN1_TIME *last_update = NULL, *next_update = NULL;
471
Michal Vasko6d292992016-01-18 09:42:38 +0100472 /* get the thread session */
473 session = pthread_getspecific(tls_opts.verify_key);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100474
475 /* get the last certificate, that is the peer (client) certificate */
Michal Vasko06e22432016-01-15 10:30:06 +0100476 if (!session->tls_cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100477 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
478 /* TODO all that is needed, but function X509_up_ref not present in older OpenSSL versions
479 session->cert = sk_X509_value(cert_stack, sk_X509_num(cert_stack) - 1);
480 X509_up_ref(session->cert);
481 sk_X509_pop_free(cert_stack, X509_free); */
482 while ((cert = sk_X509_pop(cert_stack))) {
Michal Vasko06e22432016-01-15 10:30:06 +0100483 X509_free(session->tls_cert);
484 session->tls_cert = cert;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100485 }
486 sk_X509_pop_free(cert_stack, X509_free);
487 }
488
489 /* standard certificate verification failed, so a trusted client cert must match to continue */
490 if (!preverify_ok) {
Michal Vasko06e22432016-01-15 10:30:06 +0100491 subject = X509_get_subject_name(session->tls_cert);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100492 cert_stack = X509_STORE_get1_certs(x509_ctx, subject);
493 if (cert_stack) {
494 for (i = 0; i < sk_X509_num(cert_stack); ++i) {
Michal Vasko06e22432016-01-15 10:30:06 +0100495 if (cert_pubkey_match(session->tls_cert, sk_X509_value(cert_stack, i))) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100496 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
497 * this callback will be called again with the same current certificate and preverify_ok == 1 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100498 VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.",
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100499 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
500 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
501 sk_X509_pop_free(cert_stack, X509_free);
502 return 1;
503 }
504 }
505 sk_X509_pop_free(cert_stack, X509_free);
506 }
507
508 ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
509 return 0;
510 }
511
512 /* print cert verify info */
513 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vaskod083db62016-01-19 10:31:29 +0100514 VRB("Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100515
516 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
517 subject = X509_get_subject_name(cert);
518 issuer = X509_get_issuer_name(cert);
519
520 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100521 VRB("Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100522 OPENSSL_free(cp);
523 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100524 VRB("Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100525 OPENSSL_free(cp);
526
Michal Vaskob48aa812016-01-18 14:13:09 +0100527 /* LOCK */
528 pthread_mutex_lock(&tls_opts.crl_lock);
529
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100530 /* check for revocation if set */
531 if (tls_opts.crl_store) {
532 /* try to retrieve a CRL corresponding to the _subject_ of
533 * the current certificate in order to verify it's integrity */
534 memset((char *)&obj, 0, sizeof(obj));
535 X509_STORE_CTX_init(&store_ctx, tls_opts.crl_store, NULL, NULL);
536 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
537 X509_STORE_CTX_cleanup(&store_ctx);
538 crl = obj.data.crl;
539 if (rc > 0 && crl) {
540 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100541 VRB("Cert verify CRL: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100542 OPENSSL_free(cp);
543
544 last_update = X509_CRL_get_lastUpdate(crl);
545 next_update = X509_CRL_get_nextUpdate(crl);
546 cp = asn1time_to_str(last_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100547 VRB("Cert verify CRL: last update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100548 free(cp);
549 cp = asn1time_to_str(next_update);
Michal Vaskod083db62016-01-19 10:31:29 +0100550 VRB("Cert verify CRL: next update: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100551 free(cp);
552
553 /* verify the signature on this CRL */
554 pubkey = X509_get_pubkey(cert);
555 if (X509_CRL_verify(crl, pubkey) <= 0) {
556 ERR("Cert verify CRL: invalid signature.");
557 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
558 X509_OBJECT_free_contents(&obj);
559 if (pubkey) {
560 EVP_PKEY_free(pubkey);
561 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100562 /* UNLOCK */
563 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100564 return 0;
565 }
566 if (pubkey) {
567 EVP_PKEY_free(pubkey);
568 }
569
570 /* check date of CRL to make sure it's not expired */
571 if (!next_update) {
572 ERR("Cert verify CRL: invalid nextUpdate field.");
573 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
574 X509_OBJECT_free_contents(&obj);
Michal Vaskob48aa812016-01-18 14:13:09 +0100575 /* UNLOCK */
576 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100577 return 0;
578 }
579 if (X509_cmp_current_time(next_update) < 0) {
580 ERR("Cert verify CRL: expired - revoking all certificates.");
581 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
582 X509_OBJECT_free_contents(&obj);
Michal Vaskob48aa812016-01-18 14:13:09 +0100583 /* UNLOCK */
584 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100585 return 0;
586 }
587 X509_OBJECT_free_contents(&obj);
588 }
589
590 /* try to retrieve a CRL corresponding to the _issuer_ of
Michal Vaskob48aa812016-01-18 14:13:09 +0100591 * the current certificate in order to check for revocation */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100592 memset((char *)&obj, 0, sizeof(obj));
593 X509_STORE_CTX_init(&store_ctx, tls_opts.crl_store, NULL, NULL);
594 rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
595 X509_STORE_CTX_cleanup(&store_ctx);
596 crl = obj.data.crl;
597 if (rc > 0 && crl) {
598 /* check if the current certificate is revoked by this CRL */
599 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
600 for (i = 0; i < n; i++) {
601 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
602 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
603 serial = ASN1_INTEGER_get(revoked->serialNumber);
604 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vaskod083db62016-01-19 10:31:29 +0100605 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 +0100606 OPENSSL_free(cp);
607 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
608 X509_OBJECT_free_contents(&obj);
Michal Vaskob48aa812016-01-18 14:13:09 +0100609 /* UNLOCK */
610 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100611 return 0;
612 }
613 }
614 X509_OBJECT_free_contents(&obj);
615 }
616 }
617
Michal Vaskob48aa812016-01-18 14:13:09 +0100618 /* UNLOCK */
619 pthread_mutex_unlock(&tls_opts.crl_lock);
620
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100621 /* cert-to-name already successful */
622 if (session->username) {
623 return 1;
624 }
625
626 /* cert-to-name */
627 rc = nc_tls_cert_to_name(cert, &map_type, &username);
628 if (rc) {
629 if (rc == -1) {
630 /* fatal error */
631 depth = 0;
632 }
633 /* rc == 1 is a normal CTN fail (no match found) */
634 goto fail;
635 }
636
637 /* cert-to-name match, now to extract the specific field from the peer cert */
638 if (map_type == NC_TLS_CTN_SPECIFIED) {
639 session->username = lydict_insert(server_opts.ctx, username, 0);
640 } else {
Michal Vasko06e22432016-01-15 10:30:06 +0100641 rc = nc_tls_ctn_get_username_from_cert(session->tls_cert, map_type, &cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100642 if (rc) {
643 if (rc == -1) {
644 depth = 0;
645 }
646 goto fail;
647 }
648 session->username = lydict_insert_zc(server_opts.ctx, cp);
649 }
650
651 VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username);
652 return 1;
653
654fail:
655 if (depth > 0) {
656 VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
657 return 1;
658 }
659
660 VRB("Cert-to-name unsuccessful, dropping the new client.");
661 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
662 return 0;
663}
664
665API int
666nc_tls_server_set_cert(const char *cert)
667{
668 X509 *x509_cert;
669
670 if (!cert) {
671 ERRARG;
672 return -1;
673 }
674
Michal Vaskob48aa812016-01-18 14:13:09 +0100675 /* LOCK */
676 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
677
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100678 if (!tls_opts.tls_ctx) {
679 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
680 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100681 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100682 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100683 }
684 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
685 }
686
687 x509_cert = base64der_to_cert(cert);
688 if (!x509_cert || (SSL_CTX_use_certificate(tls_opts.tls_ctx, x509_cert) != 1)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100689 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100690 X509_free(x509_cert);
Michal Vaskob48aa812016-01-18 14:13:09 +0100691 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100692 }
693 X509_free(x509_cert);
694
Michal Vaskob48aa812016-01-18 14:13:09 +0100695 /* UNLOCK */
696 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100697 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100698
699fail:
700 /* UNLOCK */
701 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
702 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100703}
704
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100705API int
706nc_tls_server_set_cert_path(const char *cert_path)
707{
708 if (!cert_path) {
709 ERRARG;
710 return -1;
711 }
712
Michal Vaskob48aa812016-01-18 14:13:09 +0100713 /* LOCK */
714 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
715
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100716 if (!tls_opts.tls_ctx) {
717 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
718 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100719 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100720 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100721 }
722 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
723 }
724
725 if (SSL_CTX_use_certificate_file(tls_opts.tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100726 ERR("Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +0100727 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100728 }
729
Michal Vaskob48aa812016-01-18 14:13:09 +0100730 /* UNLOCK */
731 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100732 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100733
734fail:
735 /* UNLOCK */
736 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
737 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100738}
739
740API int
741nc_tls_server_set_key(const char *privkey, int is_rsa)
742{
743 EVP_PKEY *key;;
744
745 if (!privkey) {
746 ERRARG;
747 return -1;
748 }
749
Michal Vaskob48aa812016-01-18 14:13:09 +0100750 /* LOCK */
751 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
752
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100753 if (!tls_opts.tls_ctx) {
754 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
755 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100756 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100757 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100758 }
759 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
760 }
761
762 key = base64der_to_privatekey(privkey, is_rsa);
763 if (!key || (SSL_CTX_use_PrivateKey(tls_opts.tls_ctx, key) != 1)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100764 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100765 EVP_PKEY_free(key);
Michal Vaskob48aa812016-01-18 14:13:09 +0100766 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100767 }
768 EVP_PKEY_free(key);
769
Michal Vaskob48aa812016-01-18 14:13:09 +0100770 /* UNLOCK */
771 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100772 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100773
774fail:
775 /* UNLOCK */
776 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
777 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100778}
779
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100780API int
781nc_tls_server_set_key_path(const char *privkey_path)
782{
783 if (!privkey_path) {
784 ERRARG;
785 return -1;
786 }
787
Michal Vaskob48aa812016-01-18 14:13:09 +0100788 /* LOCK */
789 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
790
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100791 if (!tls_opts.tls_ctx) {
792 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
793 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100794 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100795 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100796 }
797 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
798 }
799
800 if (SSL_CTX_use_PrivateKey_file(tls_opts.tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100801 ERR("Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +0100802 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100803 }
804
Michal Vaskob48aa812016-01-18 14:13:09 +0100805 /* UNLOCK */
806 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100807 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100808
809fail:
810 /* UNLOCK */
811 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
812 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100813}
814
815API int
816nc_tls_server_add_trusted_cert(const char *cert)
817{
818 X509_STORE *cert_store;
819 X509 *x509_cert;
820
821 if (!cert) {
822 ERRARG;
823 return -1;
824 }
825
Michal Vaskob48aa812016-01-18 14:13:09 +0100826 /* LOCK */
827 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
828
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100829 if (!tls_opts.tls_ctx) {
830 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
831 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100832 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100833 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100834 }
835 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
836 }
837
838 cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx);
839 if (!cert_store) {
840 cert_store = X509_STORE_new();
841 SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store);
842 }
843
844 x509_cert = base64der_to_cert(cert);
845 if (!x509_cert || (X509_STORE_add_cert(cert_store, x509_cert) != 1)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100846 ERR("Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100847 X509_free(x509_cert);
Michal Vaskob48aa812016-01-18 14:13:09 +0100848 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100849 }
850 X509_free(x509_cert);
851
Michal Vaskob48aa812016-01-18 14:13:09 +0100852 /* UNLOCK */
853 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100854 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100855
856fail:
857 /* UNLOCK */
858 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
859 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100860}
861
862API int
863nc_tls_server_add_trusted_cert_path(const char *cert_path)
864{
865 X509_STORE *cert_store;
866 X509 *x509_cert;
867
868 if (!cert_path) {
869 ERRARG;
870 return -1;
871 }
872
Michal Vaskob48aa812016-01-18 14:13:09 +0100873 /* LOCK */
874 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
875
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100876 if (!tls_opts.tls_ctx) {
877 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
878 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100879 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100880 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100881 }
882 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
883 }
884
885 cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx);
886 if (!cert_store) {
887 cert_store = X509_STORE_new();
888 SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store);
889 }
890
891 errno = 0;
892 x509_cert = pem_to_cert(cert_path);
893 if (!x509_cert || (X509_STORE_add_cert(cert_store, x509_cert) != 1)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100894 ERR("Adding a trusted certificate failed (%s).",
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100895 (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error())));
896 X509_free(x509_cert);
Michal Vaskob48aa812016-01-18 14:13:09 +0100897 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100898 }
899 X509_free(x509_cert);
900
Michal Vaskob48aa812016-01-18 14:13:09 +0100901 /* UNLOCK */
902 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100903 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100904
905fail:
906 /* UNLOCK */
907 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
908 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100909}
910
911API int
912nc_tls_server_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path)
913{
914 X509_STORE *cert_store;
915 X509_LOOKUP *lookup;
916
917 if (!cacert_file_path && !cacert_dir_path) {
918 ERRARG;
919 return -1;
920 }
921
Michal Vaskob48aa812016-01-18 14:13:09 +0100922 /* LOCK */
923 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
924
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100925 if (!tls_opts.tls_ctx) {
926 tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method());
927 if (!tls_opts.tls_ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100928 ERR("Failed to create TLS context.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100929 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100930 }
931 SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
932 }
933
934 cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx);
935 if (!cert_store) {
936 cert_store = X509_STORE_new();
937 SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store);
938 }
939
940 if (cacert_file_path) {
941 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
942 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +0100943 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100944 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100945 }
946
947 if (X509_LOOKUP_load_file(lookup, cacert_file_path, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100948 ERR("Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +0100949 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100950 }
951 }
952
953 if (cacert_dir_path) {
954 lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
955 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +0100956 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +0100957 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100958 }
959
960 if (X509_LOOKUP_add_dir(lookup, cacert_dir_path, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100961 ERR("Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +0100962 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100963 }
964 }
965
Michal Vaskob48aa812016-01-18 14:13:09 +0100966 /* UNLOCK */
967 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100968 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +0100969
970fail:
971 /* UNLOCK */
972 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
973 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100974}
975
976API void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100977nc_tls_server_destroy_certs(void)
Michal Vasko086311b2016-01-08 09:53:11 +0100978{
Michal Vaskob48aa812016-01-18 14:13:09 +0100979 /* LOCK */
980 pthread_mutex_lock(&tls_opts.tls_ctx_lock);
981
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100982 if (!tls_opts.tls_ctx) {
983 return;
984 }
Michal Vasko086311b2016-01-08 09:53:11 +0100985
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100986 SSL_CTX_free(tls_opts.tls_ctx);
987 tls_opts.tls_ctx = NULL;
Michal Vaskob48aa812016-01-18 14:13:09 +0100988
989 /* UNLOCK */
990 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100991}
992
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100993API int
994nc_tls_server_set_crl_locations(const char *crl_file_path, const char *crl_dir_path)
995{
996 X509_LOOKUP *lookup;
997
998 if (!crl_file_path && !crl_dir_path) {
999 ERRARG;
1000 return -1;
1001 }
1002
Michal Vaskob48aa812016-01-18 14:13:09 +01001003 /* LOCK */
1004 pthread_mutex_lock(&tls_opts.crl_lock);
1005
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006 if (!tls_opts.crl_store) {
1007 tls_opts.crl_store = X509_STORE_new();
1008 }
1009
1010 if (crl_file_path) {
1011 lookup = X509_STORE_add_lookup(tls_opts.crl_store, X509_LOOKUP_file());
1012 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001013 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001014 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015 }
1016
1017 if (X509_LOOKUP_load_file(lookup, crl_file_path, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001018 ERR("Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001019 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001020 }
1021 }
1022
1023 if (crl_dir_path) {
1024 lookup = X509_STORE_add_lookup(tls_opts.crl_store, X509_LOOKUP_hash_dir());
1025 if (!lookup) {
Michal Vaskod083db62016-01-19 10:31:29 +01001026 ERR("Failed to add a lookup method.");
Michal Vaskob48aa812016-01-18 14:13:09 +01001027 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028 }
1029
1030 if (X509_LOOKUP_add_dir(lookup, crl_dir_path, X509_FILETYPE_PEM) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001031 ERR("Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskob48aa812016-01-18 14:13:09 +01001032 goto fail;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001033 }
1034 }
1035
Michal Vaskob48aa812016-01-18 14:13:09 +01001036 /* UNLOCK */
1037 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001038 return 0;
Michal Vaskob48aa812016-01-18 14:13:09 +01001039
1040fail:
1041 /* UNLOCK */
1042 pthread_mutex_unlock(&tls_opts.crl_lock);
1043 return -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001044}
1045
1046API void
1047nc_tls_server_destroy_crls(void)
1048{
Michal Vaskob48aa812016-01-18 14:13:09 +01001049 /* LOCK */
1050 pthread_mutex_lock(&tls_opts.crl_lock);
1051
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001052 if (!tls_opts.crl_store) {
1053 return;
1054 }
1055
1056 X509_STORE_free(tls_opts.crl_store);
1057 tls_opts.crl_store = NULL;
Michal Vaskob48aa812016-01-18 14:13:09 +01001058
1059 /* UNLOCK */
1060 pthread_mutex_unlock(&tls_opts.crl_lock);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001061}
1062
1063API int
1064nc_tls_server_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
1065{
Michal Vasko1a38c862016-01-15 15:50:07 +01001066 if (!fingerprint || !map_type || ((map_type == NC_TLS_CTN_SPECIFIED) && !name)
1067 || ((map_type != NC_TLS_CTN_SPECIFIED) && name)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001068 ERRARG;
1069 return -1;
1070 }
1071
Michal Vaskob48aa812016-01-18 14:13:09 +01001072 /* LOCK */
1073 pthread_mutex_lock(&tls_opts.ctn_lock);
1074
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001075 ++tls_opts.ctn_count;
1076 tls_opts.ctn = realloc(tls_opts.ctn, tls_opts.ctn_count * sizeof *tls_opts.ctn);
1077
1078 tls_opts.ctn[tls_opts.ctn_count - 1].id = id;
1079 tls_opts.ctn[tls_opts.ctn_count - 1].fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0);
1080 tls_opts.ctn[tls_opts.ctn_count - 1].map_type = map_type;
1081 tls_opts.ctn[tls_opts.ctn_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1082
Michal Vaskob48aa812016-01-18 14:13:09 +01001083 /* UNLOCK */
1084 pthread_mutex_unlock(&tls_opts.ctn_lock);
1085
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001086 return 0;
1087}
1088
1089API int
1090nc_tls_server_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name)
1091{
1092 uint16_t i;
1093 int ret = -1;
1094
Michal Vaskob48aa812016-01-18 14:13:09 +01001095 /* LOCK */
1096 pthread_mutex_lock(&tls_opts.ctn_lock);
1097
Michal Vasko1a38c862016-01-15 15:50:07 +01001098 if ((id < 0) && !fingerprint && !map_type && !name) {
1099 for (i = 0; i < tls_opts.ctn_count; ++i) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001100 lydict_remove(server_opts.ctx, tls_opts.ctn[i].fingerprint);
1101 lydict_remove(server_opts.ctx, tls_opts.ctn[i].name);
1102
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001103 ret = 0;
1104 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001105 free(tls_opts.ctn);
1106 tls_opts.ctn = NULL;
1107 tls_opts.ctn_count = 0;
1108 } else {
1109 for (i = 0; i < tls_opts.ctn_count; ++i) {
1110 if (((id < 0) || (tls_opts.ctn[i].id == id))
1111 && (!fingerprint || !strcmp(tls_opts.ctn[i].fingerprint, fingerprint))
1112 && (!map_type || (tls_opts.ctn[i].map_type == map_type))
1113 && (!name || (tls_opts.ctn[i].name && !strcmp(tls_opts.ctn[i].name, name)))) {
1114 lydict_remove(server_opts.ctx, tls_opts.ctn[i].fingerprint);
1115 lydict_remove(server_opts.ctx, tls_opts.ctn[i].name);
1116
1117 --tls_opts.ctn_count;
Michal Vasko5b003bf2016-01-19 10:56:19 +01001118 memcpy(&tls_opts.ctn[i], &tls_opts.ctn[tls_opts.ctn_count], sizeof *tls_opts.ctn);
Michal Vasko1a38c862016-01-15 15:50:07 +01001119
1120 ret = 0;
1121 }
1122 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001123 }
1124
Michal Vaskob48aa812016-01-18 14:13:09 +01001125 /* UNLOCK */
1126 pthread_mutex_unlock(&tls_opts.ctn_lock);
1127
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001128 return ret;
1129}
1130
1131API void
1132nc_tls_server_free_opts(void)
1133{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001134 nc_tls_server_destroy_certs();
1135 nc_tls_server_destroy_crls();
Michal Vasko1a38c862016-01-15 15:50:07 +01001136 nc_tls_server_del_ctn(-1, NULL, 0, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001137}
Michal Vasko9e036d52016-01-08 10:49:26 +01001138
Michal Vasko6d292992016-01-18 09:42:38 +01001139static void
1140nc_tls_make_verify_key(void)
1141{
1142 pthread_key_create(&tls_opts.verify_key, NULL);
1143}
1144
Michal Vasko9e036d52016-01-08 10:49:26 +01001145int
1146nc_accept_tls_session(struct nc_session *session, int sock, int timeout)
1147{
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001148 int ret, elapsed = 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001149 struct pollfd pfd;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001150 struct timespec old_ts, new_ts;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001151
1152 pfd.fd = sock;
1153 pfd.events = POLLIN;
1154 pfd.revents = 0;
1155
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001156 if (timeout > 0) {
1157 clock_gettime(CLOCK_MONOTONIC_RAW, &old_ts);
1158 }
1159
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001160 /* poll for a new connection */
1161 errno = 0;
1162 ret = poll(&pfd, 1, timeout);
1163 if (!ret) {
1164 /* we timeouted */
1165 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001166 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001167 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001168 ERR("poll failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001169 close(sock);
1170 return -1;
1171 }
1172
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001173 if (timeout > 0) {
1174 /* decrease timeout */
1175 clock_gettime(CLOCK_MONOTONIC_RAW, &new_ts);
1176
1177 elapsed = (new_ts.tv_sec - old_ts.tv_sec) * 1000;
1178 elapsed += (new_ts.tv_nsec - old_ts.tv_nsec) / 1000000;
1179 }
1180
Michal Vaskob48aa812016-01-18 14:13:09 +01001181 /* data waiting, prepare session */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001182 session->ti_type = NC_TI_OPENSSL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001183
Michal Vaskob48aa812016-01-18 14:13:09 +01001184 /* LOCK */
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001185 ret = nc_timedlock(&tls_opts.tls_ctx_lock, timeout, &elapsed);
1186 if (ret < 1) {
1187 return ret;
1188 }
1189
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001190 session->ti.tls = SSL_new(tls_opts.tls_ctx);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001191
Michal Vaskob48aa812016-01-18 14:13:09 +01001192 /* UNLOCK */
1193 pthread_mutex_unlock(&tls_opts.tls_ctx_lock);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001194
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001195 if (!session->ti.tls) {
Michal Vaskod083db62016-01-19 10:31:29 +01001196 ERR("Failed to create TLS structure from context.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001197 close(sock);
1198 return -1;
1199 }
1200
1201 SSL_set_fd(session->ti.tls, sock);
1202 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1203
Michal Vasko6d292992016-01-18 09:42:38 +01001204 /* store session on per-thread basis */
1205 pthread_once(&tls_opts.verify_once, nc_tls_make_verify_key);
1206 pthread_setspecific(tls_opts.verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001207
1208 ret = SSL_accept(session->ti.tls);
Michal Vaskob48aa812016-01-18 14:13:09 +01001209
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001210 if (ret != 1) {
1211 switch (SSL_get_error(session->ti.tls, ret)) {
1212 case SSL_ERROR_SYSCALL:
Michal Vaskod083db62016-01-19 10:31:29 +01001213 ERR("SSL_accept failed (%s).", strerror(errno));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001214 break;
1215 case SSL_ERROR_SSL:
Michal Vaskod083db62016-01-19 10:31:29 +01001216 ERR("SSL_accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001217 break;
1218 default:
Michal Vaskod083db62016-01-19 10:31:29 +01001219 ERR("SSL_accept failed.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001220 break;
1221 }
1222 return -1;
1223 }
1224
Michal Vasko1a38c862016-01-15 15:50:07 +01001225 return 1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001226}