blob: 2dc68dfcb05010d66a76cd1bb7ba8522f70a2e8b [file] [log] [blame]
Radek Krejci5da708a2015-09-01 17:33:23 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server_tls.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
roman3f9b65c2023-06-05 14:26:58 +02004 * @author Roman Janota <janota@cesnet.cz>
Michal Vasko95ea9ff2021-11-09 12:29:14 +01005 * @brief libnetconf2 TLS server session manipulation functions
Radek Krejci5da708a2015-09-01 17:33:23 +02006 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01007 * @copyright
roman3f9b65c2023-06-05 14:26:58 +02008 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Radek Krejci5da708a2015-09-01 17:33:23 +02009 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010010 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010013 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010014 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5da708a2015-09-01 17:33:23 +020015 */
16
Michal Vaskoc14e3c82016-01-11 16:14:30 +010017#define _GNU_SOURCE
18
Michal Vaskoc14e3c82016-01-11 16:14:30 +010019#include <poll.h>
roman3f9b65c2023-06-05 14:26:58 +020020#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <string.h>
Michal Vaskof0537d82016-01-29 14:42:38 +010024#include <unistd.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010025
roman3f9b65c2023-06-05 14:26:58 +020026#include <curl/curl.h>
27#include <openssl/bio.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010028#include <openssl/err.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include <openssl/evp.h>
30#include <openssl/ssl.h>
Michal Vaskoe2713da2016-08-22 16:06:40 +020031#include <openssl/x509.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <openssl/x509v3.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010033
Michal Vasko316c9632020-04-15 11:06:57 +020034#include "compat.h"
roman3f9b65c2023-06-05 14:26:58 +020035#include "config.h"
36#include "log_p.h"
37#include "session.h"
38#include "session_p.h"
Rosen Penev4f552d62019-06-26 16:10:43 -070039
Michal Vasko3031aae2016-01-27 16:07:18 +010040struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010041pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER;
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042extern struct nc_server_opts server_opts;
Michal Vaskoc61c4492016-01-25 11:13:34 +010043
Michal Vasko5c2f7952016-01-22 13:16:31 +010044static pthread_key_t verify_key;
45static pthread_once_t verify_once = PTHREAD_ONCE_INIT;
46
Michal Vaskoc14e3c82016-01-11 16:14:30 +010047static char *
Michal Vasko18aeb5d2017-02-17 09:23:56 +010048asn1time_to_str(const ASN1_TIME *t)
Michal Vasko086311b2016-01-08 09:53:11 +010049{
Michal Vaskoc14e3c82016-01-11 16:14:30 +010050 char *cp;
51 BIO *bio;
52 int n;
Radek Krejci5da708a2015-09-01 17:33:23 +020053
Michal Vaskoc14e3c82016-01-11 16:14:30 +010054 if (!t) {
55 return NULL;
56 }
roman3f9b65c2023-06-05 14:26:58 +020057
Michal Vaskoc14e3c82016-01-11 16:14:30 +010058 bio = BIO_new(BIO_s_mem());
59 if (!bio) {
60 return NULL;
61 }
roman3f9b65c2023-06-05 14:26:58 +020062
Michal Vaskoc14e3c82016-01-11 16:14:30 +010063 ASN1_TIME_print(bio, t);
64 n = BIO_pending(bio);
65 cp = malloc(n + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +010066 if (!cp) {
67 ERRMEM;
68 BIO_free(bio);
69 return NULL;
70 }
roman3f9b65c2023-06-05 14:26:58 +020071
Michal Vaskoc14e3c82016-01-11 16:14:30 +010072 n = BIO_read(bio, cp, n);
73 if (n < 0) {
74 BIO_free(bio);
75 free(cp);
76 return NULL;
77 }
roman3f9b65c2023-06-05 14:26:58 +020078
Michal Vaskoc14e3c82016-01-11 16:14:30 +010079 cp[n] = '\0';
80 BIO_free(bio);
81 return cp;
82}
83
84static void
85digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str)
86{
87 unsigned int i;
88
89 *str = malloc(dig_len * 3);
Michal Vasko4eb3c312016-03-01 14:09:37 +010090 if (!*str) {
91 ERRMEM;
92 return;
93 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010094 for (i = 0; i < dig_len - 1; ++i) {
95 sprintf((*str) + (i * 3), "%02x:", digest[i]);
96 }
97 sprintf((*str) + (i * 3), "%02x", digest[i]);
98}
99
100/* return NULL - SSL error can be retrieved */
101static X509 *
102base64der_to_cert(const char *in)
103{
104 X509 *out;
105 char *buf;
106 BIO *bio;
107
108 if (in == NULL) {
109 return NULL;
110 }
111
112 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
113 return NULL;
114 }
115 bio = BIO_new_mem_buf(buf, strlen(buf));
116 if (!bio) {
117 free(buf);
118 return NULL;
119 }
120
121 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
122 if (!out) {
123 free(buf);
124 BIO_free(bio);
125 return NULL;
126 }
127
128 free(buf);
129 BIO_free(bio);
130 return out;
131}
132
Michal Vasko6e08cb72017-02-02 11:15:29 +0100133static EVP_PKEY *
Michal Vaskoddce1212019-05-24 09:58:49 +0200134base64der_to_privatekey(const char *in, const char *key_str)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100135{
136 EVP_PKEY *out;
137 char *buf;
138 BIO *bio;
139
140 if (in == NULL) {
141 return NULL;
142 }
143
Michal Vaskoddce1212019-05-24 09:58:49 +0200144 if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", key_str, " PRIVATE KEY-----\n", in, "\n-----END ",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200145 key_str, " PRIVATE KEY-----") == -1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100146 return NULL;
147 }
148 bio = BIO_new_mem_buf(buf, strlen(buf));
149 if (!bio) {
150 free(buf);
151 return NULL;
152 }
153
154 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
155 if (!out) {
156 free(buf);
157 BIO_free(bio);
158 return NULL;
159 }
160
161 free(buf);
162 BIO_free(bio);
163 return out;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100164}
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100165
166static int
167cert_pubkey_match(X509 *cert1, X509 *cert2)
168{
169 ASN1_BIT_STRING *bitstr1, *bitstr2;
170
171 bitstr1 = X509_get0_pubkey_bitstr(cert1);
172 bitstr2 = X509_get0_pubkey_bitstr(cert2);
173
174 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
175 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
176 return 0;
177 }
178
179 return 1;
180}
181
182static int
183nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
184{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200185 STACK_OF(GENERAL_NAME) * san_names;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100186 GENERAL_NAME *san_name;
187 ASN1_OCTET_STRING *ip;
188 int i, san_count;
189 char *subject, *common_name;
190
Michal Vaskod9a78642022-02-24 16:25:21 +0100191 *username = NULL;
192
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100193 if (map_type == NC_TLS_CTN_COMMON_NAME) {
194 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
195 common_name = strstr(subject, "CN=");
196 if (!common_name) {
Michal Vasko05532772021-06-03 12:12:38 +0200197 WRN(NULL, "Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100198 free(subject);
199 return 1;
200 }
201 common_name += 3;
202 if (strchr(common_name, '/')) {
203 *strchr(common_name, '/') = '\0';
204 }
205 *username = strdup(common_name);
roman3a95bb22023-10-26 11:07:17 +0200206 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100207 free(subject);
208 } else {
209 /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */
210 san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL);
211 if (!san_names) {
Michal Vasko05532772021-06-03 12:12:38 +0200212 WRN(NULL, "Certificate has no SANs or failed to retrieve them.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100213 return 1;
214 }
215
216 san_count = sk_GENERAL_NAME_num(san_names);
217 for (i = 0; i < san_count; ++i) {
218 san_name = sk_GENERAL_NAME_value(san_names, i);
219
220 /* rfc822Name (email) */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200221 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) &&
222 (san_name->type == GEN_EMAIL)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100223 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name));
roman3a95bb22023-10-26 11:07:17 +0200224 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100225 break;
226 }
227
228 /* dNSName */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200229 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) &&
230 (san_name->type == GEN_DNS)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100231 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName));
roman3a95bb22023-10-26 11:07:17 +0200232 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100233 break;
234 }
235
236 /* iPAddress */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200237 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_IP_ADDRESS)) &&
238 (san_name->type == GEN_IPADD)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100239 ip = san_name->d.iPAddress;
240 if (ip->length == 4) {
241 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 +0100242 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100243 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
244 return -1;
245 }
246 break;
247 } else if (ip->length == 16) {
248 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
249 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
250 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
251 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -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 {
Michal Vasko05532772021-06-03 12:12:38 +0200258 WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100259 }
260 }
261 }
262 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
263
Vitaly Kuzina3b66322021-08-26 12:33:43 +0300264 if (i == san_count) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100265 switch (map_type) {
266 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200267 WRN(NULL, "Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100268 break;
269 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200270 WRN(NULL, "Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100271 break;
272 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vasko05532772021-06-03 12:12:38 +0200273 WRN(NULL, "Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100274 break;
275 case NC_TLS_CTN_SAN_ANY:
Michal Vasko05532772021-06-03 12:12:38 +0200276 WRN(NULL, "Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100277 break;
278 default:
279 break;
280 }
281 return 1;
282 }
283 }
284
285 return 0;
286}
287
288/* return: 0 - OK, 1 - no match, -1 - error */
289static int
Roytakbe391922023-07-27 19:59:24 +0200290nc_tls_cert_to_name(struct nc_session *session, struct nc_ctn *ctn_first, X509 *cert)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100291{
292 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
293 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
roman3a95bb22023-10-26 11:07:17 +0200294 unsigned char *buf;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100295 unsigned int buf_len = 64;
296 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100297 struct nc_ctn *ctn;
Roytakbe391922023-07-27 19:59:24 +0200298 NC_TLS_CTN_MAPTYPE map_type;
299 char *username = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100300
roman3a95bb22023-10-26 11:07:17 +0200301 buf = malloc(buf_len);
302 NC_CHECK_ERRMEM_RET(!buf, -1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100303
roman84473212023-10-12 16:29:17 +0200304 if (!session || !cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100305 free(buf);
306 return -1;
307 }
308
Michal Vaskoc61c4492016-01-25 11:13:34 +0100309 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Roytakbe391922023-07-27 19:59:24 +0200310 /* reset map_type */
311 map_type = NC_TLS_CTN_UNKNOWN;
312
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100313 /* first make sure the entry is valid */
roman3f9b65c2023-06-05 14:26:58 +0200314 if (!ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
Michal Vasko05532772021-06-03 12:12:38 +0200315 VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100316 continue;
317 }
318
roman3f9b65c2023-06-05 14:26:58 +0200319 /* if ctn has no fingerprint, it will match any certificate */
320 if (!ctn->fingerprint) {
321 map_type = ctn->map_type;
322
323 /* MD5 */
324 } else if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100325 if (!digest_md5) {
326 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200327 ERR(NULL, "Calculating MD5 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_md5);
332 }
333
Michal Vasko5e3f3392016-01-20 11:13:01 +0100334 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Roytakbe391922023-07-27 19:59:24 +0200335 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200336 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200337 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100338 }
roman3f9b65c2023-06-05 14:26:58 +0200339 free(digest_md5);
340 digest_md5 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100341
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200342 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100343 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100344 if (!digest_sha1) {
345 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200346 ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100347 ret = -1;
348 goto cleanup;
349 }
350 digest_to_str(buf, buf_len, &digest_sha1);
351 }
352
Michal Vasko5e3f3392016-01-20 11:13:01 +0100353 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Roytakbe391922023-07-27 19:59:24 +0200354 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200355 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200356 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100357 }
roman3f9b65c2023-06-05 14:26:58 +0200358 free(digest_sha1);
359 digest_sha1 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100360
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200361 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100362 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100363 if (!digest_sha224) {
364 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200365 ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100366 ret = -1;
367 goto cleanup;
368 }
369 digest_to_str(buf, buf_len, &digest_sha224);
370 }
371
Michal Vasko5e3f3392016-01-20 11:13:01 +0100372 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Roytakbe391922023-07-27 19:59:24 +0200373 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200374 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200375 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100376 }
roman3f9b65c2023-06-05 14:26:58 +0200377 free(digest_sha224);
378 digest_sha224 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100379
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200380 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100381 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100382 if (!digest_sha256) {
383 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200384 ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100385 ret = -1;
386 goto cleanup;
387 }
388 digest_to_str(buf, buf_len, &digest_sha256);
389 }
390
Michal Vasko5e3f3392016-01-20 11:13:01 +0100391 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Roytakbe391922023-07-27 19:59:24 +0200392 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200393 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200394 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100395 }
roman3f9b65c2023-06-05 14:26:58 +0200396 free(digest_sha256);
397 digest_sha256 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100398
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200399 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100400 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100401 if (!digest_sha384) {
402 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200403 ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100404 ret = -1;
405 goto cleanup;
406 }
407 digest_to_str(buf, buf_len, &digest_sha384);
408 }
409
Michal Vasko5e3f3392016-01-20 11:13:01 +0100410 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Roytakbe391922023-07-27 19:59:24 +0200411 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200412 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200413 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100414 }
roman3f9b65c2023-06-05 14:26:58 +0200415 free(digest_sha384);
416 digest_sha384 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100417
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200418 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100419 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100420 if (!digest_sha512) {
421 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200422 ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100423 ret = -1;
424 goto cleanup;
425 }
426 digest_to_str(buf, buf_len, &digest_sha512);
427 }
428
Michal Vasko5e3f3392016-01-20 11:13:01 +0100429 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Roytakbe391922023-07-27 19:59:24 +0200430 /* we got ourselves a potential winner! */
Michal Vasko05532772021-06-03 12:12:38 +0200431 VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200432 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100433 }
roman3f9b65c2023-06-05 14:26:58 +0200434 free(digest_sha512);
435 digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100436
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200437 /* unknown */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100438 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200439 WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Roytakbe391922023-07-27 19:59:24 +0200440 continue;
441 }
442
443 if (map_type != NC_TLS_CTN_UNKNOWN) {
444 /* found a fingerprint match */
445 if (map_type == NC_TLS_CTN_SPECIFIED) {
446 /* specified -> get username from the ctn entry */
447 session->username = strdup(ctn->name);
roman3a95bb22023-10-26 11:07:17 +0200448 NC_CHECK_ERRMEM_GOTO(!session->username, ret = -1, cleanup);
Roytakbe391922023-07-27 19:59:24 +0200449 } else {
450 /* try to get the username from the cert with this ctn's map type */
451 ret = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &username);
452 if (ret == -1) {
453 /* fatal error */
454 goto cleanup;
455 } else if (ret) {
456 /* didn't get username, try next ctn entry */
457 continue;
458 }
459
460 /* success */
461 session->username = username;
462 }
463
464 /* matching fingerprint found and username obtained, success */
465 ret = 0;
466 goto cleanup;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100467 }
468 }
469
Michal Vasko5e3f3392016-01-20 11:13:01 +0100470 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100471 ret = 1;
472 }
473
474cleanup:
475 free(digest_md5);
476 free(digest_sha1);
477 free(digest_sha224);
478 free(digest_sha256);
479 free(digest_sha384);
480 free(digest_sha512);
481 free(buf);
482 return ret;
483}
484
roman3f9b65c2023-06-05 14:26:58 +0200485static int
486nc_server_tls_check_crl(X509_STORE *crl_store, X509_STORE_CTX *x509_ctx, X509 *cert,
487 const X509_NAME *subject, const X509_NAME *issuer)
488{
489 int n, i, ret = 0;
490 X509_STORE_CTX *store_ctx = NULL;
491 X509_OBJECT *obj = NULL;
492 X509_CRL *crl;
493 X509_REVOKED *revoked;
494 EVP_PKEY *pubkey;
495 const ASN1_INTEGER *serial;
496 const ASN1_TIME *last_update = NULL, *next_update = NULL;
497 char *cp;
498
499 store_ctx = X509_STORE_CTX_new();
roman3a95bb22023-10-26 11:07:17 +0200500 NC_CHECK_ERRMEM_GOTO(!store_ctx, ret = -1, cleanup);
roman3f9b65c2023-06-05 14:26:58 +0200501
502 /* init store context */
503 ret = X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
504 if (!ret) {
505 ERR(NULL, "Initializing x509 store ctx failed (%s).", ERR_reason_error_string(ERR_get_error()));
506 ret = -1;
507 goto cleanup;
508 }
509 ret = 0;
510
511 /* try to find a CRL entry that corresponds to the current certificate in question */
512 obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, subject);
513 crl = X509_OBJECT_get0_X509_CRL(obj);
514 X509_OBJECT_free(obj);
515 if (crl) {
516 /* found it */
517 cp = X509_NAME_oneline(subject, NULL, 0);
518 VRB(NULL, "Cert verify CRL: issuer: %s.", cp);
519 OPENSSL_free(cp);
520
521 last_update = X509_CRL_get0_lastUpdate(crl);
522 next_update = X509_CRL_get0_nextUpdate(crl);
523 cp = asn1time_to_str(last_update);
524 VRB(NULL, "Cert verify CRL: last update: %s.", cp);
525 free(cp);
526 cp = asn1time_to_str(next_update);
527 VRB(NULL, "Cert verify CRL: next update: %s.", cp);
528 free(cp);
529
530 /* verify the signature on this CRL */
531 pubkey = X509_get0_pubkey(cert);
532 if (X509_CRL_verify(crl, pubkey) <= 0) {
533 ERR(NULL, "Cert verify CRL: invalid signature.");
534 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
535 ret = -1;
536 goto cleanup;
537 }
538
539 /* check date of CRL to make sure it's not expired */
540 if (!next_update) {
541 ERR(NULL, "Cert verify CRL: invalid nextUpdate field.");
542 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
543 ret = -1;
544 goto cleanup;
545 }
546
547 if (X509_cmp_current_time(next_update) < 0) {
548 ERR(NULL, "Cert verify CRL: expired - revoking all certificates.");
549 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
550 ret = -1;
551 goto cleanup;
552 }
553 }
554
555 /* try to retrieve a CRL corresponding to the _issuer_ of
556 * the current certificate in order to check for revocation */
557 obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, issuer);
558 crl = X509_OBJECT_get0_X509_CRL(obj);
559 if (crl) {
560 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
561 for (i = 0; i < n; i++) {
562 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
563 serial = X509_REVOKED_get0_serialNumber(revoked);
564 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
565 cp = X509_NAME_oneline(issuer, NULL, 0);
566 ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
567 serial, serial, cp);
568 OPENSSL_free(cp);
569 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
570 ret = -1;
571 goto cleanup;
572 }
573 }
574 }
575
576cleanup:
577 X509_STORE_CTX_free(store_ctx);
578 X509_OBJECT_free(obj);
579 return ret;
580}
581
582static int
583nc_server_tls_ts_ref_get_certs(const char *referenced_name, struct nc_certificate **certs, uint16_t *cert_count)
584{
585 uint16_t i;
586 struct nc_truststore *ts = &server_opts.truststore;
587
588 *certs = NULL;
589 *cert_count = 0;
590
591 /* lookup name */
592 for (i = 0; i < ts->cert_bag_count; i++) {
593 if (!strcmp(referenced_name, ts->cert_bags[i].name)) {
594 break;
595 }
596 }
597
598 if (i == ts->cert_bag_count) {
599 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
600 return -1;
601 }
602
603 *certs = ts->cert_bags[i].certs;
604 *cert_count = ts->cert_bags[i].cert_count;
605 return 0;
606}
607
608/* In case a CA chain verification failed an end-entity certificate must match.
609 * The meaning of local_or_referenced is that it states, which end-entity certificates to check
610 * (1 = current endpoint's, 2 = referenced endpoint's).
611 */
612static int
613nc_server_tls_do_preverify(struct nc_session *session, X509_STORE_CTX *x509_ctx, int local_or_referenced)
614{
615 X509_STORE *store;
616 struct nc_cert_grouping *ee_certs;
617 int i, ret;
618 X509 *cert;
619 struct nc_certificate *certs;
620 uint16_t cert_count;
621
622 store = X509_STORE_CTX_get0_store(x509_ctx);
623 if (!store) {
624 ERR(session, "Error getting store from context (%s).", ERR_reason_error_string(ERR_get_error()));
625 return -1;
626 }
627
628 /* get the data from the store */
629 ee_certs = X509_STORE_get_ex_data(store, local_or_referenced);
630 if (!ee_certs) {
631 ERR(session, "Error getting data from store (%s).", ERR_reason_error_string(ERR_get_error()));
632 return -1;
633 }
634
635 if (ee_certs->store == NC_STORE_LOCAL) {
636 /* local definition */
637 certs = ee_certs->certs;
638 cert_count = ee_certs->cert_count;
639 } else {
640 /* truststore reference */
641 if (nc_server_tls_ts_ref_get_certs(ee_certs->ts_ref, &certs, &cert_count)) {
642 ERR(NULL, "Error getting end-entity certificates from the truststore reference \"%s\".", ee_certs->ts_ref);
643 return -1;
644 }
645 }
646
647 for (i = 0; i < cert_count; i++) {
648 cert = base64der_to_cert(certs[i].data);
649 ret = cert_pubkey_match(session->opts.server.client_cert, cert);
650 X509_free(cert);
651 if (ret) {
652 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
653 * this callback will be called again with the same current certificate and preverify_ok == 1 */
654 VRB(session, "Cert verify: fail (%s), but the end-entity certificate is trusted, continuing.",
655 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
656 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
657 return 1;
658 }
659 }
660
661 return 0;
662}
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100663
664static int
665nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
666{
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100667 X509_NAME *subject;
668 X509_NAME *issuer;
669 X509 *cert;
roman3f9b65c2023-06-05 14:26:58 +0200670 char *cp;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200671
672 STACK_OF(X509) * cert_stack;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200673 struct nc_session *session;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100674 struct nc_server_tls_opts *opts;
roman3f9b65c2023-06-05 14:26:58 +0200675 int rc, depth;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100676
677 /* get the thread session */
678 session = pthread_getspecific(verify_key);
679 if (!session) {
680 ERRINT;
681 return 0;
682 }
683
684 opts = session->data;
685
686 /* get the last certificate, that is the peer (client) certificate */
687 if (!session->opts.server.client_cert) {
688 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500689 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100690 X509_up_ref(session->opts.server.client_cert);
691 sk_X509_pop_free(cert_stack, X509_free);
692 }
693
roman3f9b65c2023-06-05 14:26:58 +0200694 /* standard certificate verification failed, so an end-entity client cert must match to continue */
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100695 if (!preverify_ok) {
roman3f9b65c2023-06-05 14:26:58 +0200696 /* check current endpoint's end-entity certs */
697 rc = nc_server_tls_do_preverify(session, x509_ctx, 1);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100698 if (rc == -1) {
roman3f9b65c2023-06-05 14:26:58 +0200699 return 0;
700 } else if (rc == 1) {
701 return 1;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100702 }
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100703
roman3f9b65c2023-06-05 14:26:58 +0200704 /* no match, continue */
705 if (opts->endpt_client_ref) {
706 /* check referenced endpoint's end-entity certs */
707 rc = nc_server_tls_do_preverify(session, x509_ctx, 2);
708 if (rc == -1) {
709 return 0;
710 } else if (rc == 1) {
711 return 1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100712 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100713 }
714
roman3f9b65c2023-06-05 14:26:58 +0200715 /* no match, fail */
Michal Vasko05532772021-06-03 12:12:38 +0200716 ERR(session, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100717 return 0;
718 }
719
720 /* print cert verify info */
721 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200722 VRB(session, "Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100723
724 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
725 subject = X509_get_subject_name(cert);
726 issuer = X509_get_issuer_name(cert);
727
728 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200729 VRB(session, "Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100730 OPENSSL_free(cp);
731 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200732 VRB(session, "Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100733 OPENSSL_free(cp);
734
roman3f9b65c2023-06-05 14:26:58 +0200735 /* check if the current certificate is revoked if CRL is set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100736 if (opts->crl_store) {
roman3f9b65c2023-06-05 14:26:58 +0200737 rc = nc_server_tls_check_crl(opts->crl_store, x509_ctx, cert, subject, issuer);
738 if (rc) {
739 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100740 }
741 }
742
743 /* cert-to-name already successful */
744 if (session->username) {
745 return 1;
746 }
747
748 /* cert-to-name */
Roytakbe391922023-07-27 19:59:24 +0200749 rc = nc_tls_cert_to_name(session, opts->ctn, cert);
roman3f9b65c2023-06-05 14:26:58 +0200750 if (rc == -1) {
751 /* fatal error */
752 depth = 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100753 goto fail;
roman3f9b65c2023-06-05 14:26:58 +0200754 } else if ((rc == 1) && !opts->endpt_client_ref) {
755 /* no match found and no referenced endpoint */
756 goto fail;
757 } else if ((rc == 1) && opts->endpt_client_ref) {
758 /* no match found, but has a referenced endpoint so try it */
759 rc = nc_tls_cert_to_name(session, opts->endpt_client_ref->opts.tls->ctn, cert);
760 if (rc) {
761 if (rc == -1) {
762 /* fatal error */
763 depth = 0;
764 }
765 /* rc == 1 is a normal CTN fail (no match found) */
766 goto fail;
767 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100768 }
769
Michal Vasko05532772021-06-03 12:12:38 +0200770 VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100771
772 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200773 VRB(session, "Cert verify: user verify callback revoked authorization.");
Michal Vasko7060bcf2016-11-28 14:48:11 +0100774 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
775 return 0;
776 }
777
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100778 return 1;
779
780fail:
781 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200782 VRB(session, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100783 return 1;
784 }
785
Michal Vasko05532772021-06-03 12:12:38 +0200786 VRB(session, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100787 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
788 return 0;
789}
790
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100791static int
Michal Vaskof585ac72016-11-25 15:16:38 +0100792nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200793 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100794{
795 struct nc_ctn *ctn;
796 int ret = -1;
797
798 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
799 if (id && *id && (*id != ctn->id)) {
800 continue;
801 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100802 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100803 continue;
804 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100805 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100806 continue;
807 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100808 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100809 continue;
810 }
811
812 /* first match, good enough */
813 if (id && !(*id)) {
814 *id = ctn->id;
815 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100816 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100817 *fingerprint = strdup(ctn->fingerprint);
818 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100819 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100820 *map_type = ctn->map_type;
821 }
Adam Richterd44680e2019-06-15 13:16:16 -0700822 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100823 *name = strdup(ctn->name);
824 }
825
826 ret = 0;
827 break;
828 }
829
830 return ret;
831}
832
833API int
Michal Vaskof585ac72016-11-25 15:16:38 +0100834nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
Michal Vaskoadf30f02019-06-24 09:34:47 +0200835 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100836{
837 int ret;
838 struct nc_endpt *endpt;
839
roman40672412023-05-04 11:10:22 +0200840 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100841
842 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100843 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100844 if (!endpt) {
845 return -1;
846 }
847 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
848 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +0100849 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100850
851 return ret;
852}
853
854API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200855nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
856 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100857{
858 int ret;
859 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200860 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100861
862 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200863 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
864 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100865 return -1;
866 }
867
Michal Vaskoadf30f02019-06-24 09:34:47 +0200868 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100869
870 /* UNLOCK */
871 nc_server_ch_client_unlock(client);
872
873 return ret;
874}
875
Michal Vasko709598e2016-11-28 14:48:32 +0100876API const X509 *
877nc_session_get_client_cert(const struct nc_session *session)
878{
879 if (!session || (session->side != NC_SERVER)) {
roman40672412023-05-04 11:10:22 +0200880 ERRARG(session, "session");
Michal Vasko709598e2016-11-28 14:48:32 +0100881 return NULL;
882 }
883
884 return session->opts.server.client_cert;
885}
886
Michal Vasko7060bcf2016-11-28 14:48:11 +0100887API void
888nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
889{
890 server_opts.user_verify_clb = verify_clb;
891}
892
Michal Vasko6d292992016-01-18 09:42:38 +0100893static void
894nc_tls_make_verify_key(void)
895{
Michal Vasko5c2f7952016-01-22 13:16:31 +0100896 pthread_key_create(&verify_key, NULL);
Michal Vasko6d292992016-01-18 09:42:38 +0100897}
898
roman3f9b65c2023-06-05 14:26:58 +0200899static int
900nc_server_tls_ks_ref_get_cert_key(const char *referenced_key_name, const char *referenced_cert_name,
901 char **privkey_data, NC_PRIVKEY_FORMAT *privkey_type, char **cert_data)
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500902{
roman3f9b65c2023-06-05 14:26:58 +0200903 uint16_t i, j;
904 struct nc_keystore *ks = &server_opts.keystore;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500905
roman3f9b65c2023-06-05 14:26:58 +0200906 *privkey_data = NULL;
907 *cert_data = NULL;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500908
roman3f9b65c2023-06-05 14:26:58 +0200909 /* lookup name */
910 for (i = 0; i < ks->asym_key_count; i++) {
911 if (!strcmp(referenced_key_name, ks->asym_keys[i].name)) {
912 break;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500913 }
914 }
roman3f9b65c2023-06-05 14:26:58 +0200915 if (i == ks->asym_key_count) {
916 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_key_name);
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500917 return -1;
918 }
919
roman3f9b65c2023-06-05 14:26:58 +0200920 for (j = 0; j < ks->asym_keys[i].cert_count; j++) {
921 if (!strcmp(referenced_cert_name, ks->asym_keys[i].certs[i].name)) {
922 break;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500923 }
924 }
roman3f9b65c2023-06-05 14:26:58 +0200925 if (j == ks->asym_keys[i].cert_count) {
926 ERR(NULL, "Keystore certificate entry \"%s\" associated with the key \"%s\" not found.",
927 referenced_cert_name, referenced_key_name);
928 return -1;
929 }
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500930
roman3f9b65c2023-06-05 14:26:58 +0200931 *privkey_data = ks->asym_keys[i].privkey_data;
932 *privkey_type = ks->asym_keys[i].privkey_type;
933 *cert_data = ks->asym_keys[i].certs[j].data;
934 return 0;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500935}
936
Michal Vasko4c1fb492017-01-30 14:31:07 +0100937static int
roman3f9b65c2023-06-05 14:26:58 +0200938nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, struct nc_server_tls_opts *opts)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100939{
roman3f9b65c2023-06-05 14:26:58 +0200940 char *privkey_data = NULL, *cert_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200941 int ret = 0;
roman3f9b65c2023-06-05 14:26:58 +0200942 NC_PRIVKEY_FORMAT privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100943 X509 *cert = NULL;
944 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100945
roman3f9b65c2023-06-05 14:26:58 +0200946 NC_CHECK_ARG_RET(NULL, tls_ctx, opts, -1);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100947
roman3f9b65c2023-06-05 14:26:58 +0200948 /* get data needed for setting the server cert */
949 if (opts->store == NC_STORE_LOCAL) {
950 /* local definition */
951 cert_data = opts->cert_data;
952 privkey_data = opts->privkey_data;
953 privkey_type = opts->privkey_type;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100954 } else {
roman3f9b65c2023-06-05 14:26:58 +0200955 /* keystore */
956 ret = nc_server_tls_ks_ref_get_cert_key(opts->key_ref, opts->cert_ref, &privkey_data, &privkey_type, &cert_data);
957 if (ret) {
958 ERR(NULL, "Getting server certificate from the keystore reference \"%s\" failed.", opts->key_ref);
959 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100960 }
961 }
962
roman3f9b65c2023-06-05 14:26:58 +0200963 /* load the cert */
964 cert = base64der_to_cert(cert_data);
965 if (!cert) {
966 ERR(NULL, "Converting certificate data to certificate format failed.");
967 ret = -1;
968 goto cleanup;
969 }
970
971 /* set server cert */
972 ret = SSL_CTX_use_certificate(tls_ctx, cert);
973 if (ret != 1) {
974 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
975 ret = -1;
976 goto cleanup;
977 }
978
Michal Vasko4c1fb492017-01-30 14:31:07 +0100979 /* load the private key */
roman3f9b65c2023-06-05 14:26:58 +0200980 pkey = base64der_to_privatekey(privkey_data, nc_privkey_format_to_str(privkey_type));
981 if (!pkey) {
982 ERR(NULL, "Converting private key data to private key format failed.");
983 ret = -1;
984 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100985 }
986
roman3f9b65c2023-06-05 14:26:58 +0200987 /* set server key */
988 ret = SSL_CTX_use_PrivateKey(tls_ctx, pkey);
989 if (ret != 1) {
990 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
991 ret = -1;
992 goto cleanup;
993 }
994
995 ret = 0;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500996
Michal Vasko4c1fb492017-01-30 14:31:07 +0100997cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +0100998 X509_free(cert);
999 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001000 return ret;
1001}
1002
roman3f9b65c2023-06-05 14:26:58 +02001003static int
1004tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_data)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001005{
roman3f9b65c2023-06-05 14:26:58 +02001006 X509 *cert;
1007
1008 cert = base64der_to_cert(cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001009
Michal Vasko4c1fb492017-01-30 14:31:07 +01001010 if (!cert) {
roman3f9b65c2023-06-05 14:26:58 +02001011 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
1012 ERR_reason_error_string(ERR_get_error()));
1013 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001014 }
1015
1016 /* add the trusted certificate */
1017 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001018 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001019 X509_free(cert);
roman3f9b65c2023-06-05 14:26:58 +02001020 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001021 }
1022 X509_free(cert);
roman3f9b65c2023-06-05 14:26:58 +02001023
1024 return 0;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001025}
1026
1027static int
roman3f9b65c2023-06-05 14:26:58 +02001028nc_tls_store_set_trusted_certs(X509_STORE *cert_store, struct nc_cert_grouping *ca_certs)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001029{
1030 uint16_t i;
roman3f9b65c2023-06-05 14:26:58 +02001031 struct nc_certificate *certs;
1032 uint16_t cert_count;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001033
roman3f9b65c2023-06-05 14:26:58 +02001034 if (ca_certs->store == NC_STORE_LOCAL) {
1035 /* local definition */
1036 certs = ca_certs->certs;
1037 cert_count = ca_certs->cert_count;
1038 } else {
1039 /* truststore */
1040 if (nc_server_tls_ts_ref_get_certs(ca_certs->ts_ref, &certs, &cert_count)) {
1041 ERR(NULL, "Error getting certificate-authority certificates from the truststore reference \"%s\".", ca_certs->ts_ref);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001042 return -1;
1043 }
roman3f9b65c2023-06-05 14:26:58 +02001044 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001045
roman3f9b65c2023-06-05 14:26:58 +02001046 for (i = 0; i < cert_count; i++) {
1047 if (tls_store_add_trusted_cert(cert_store, certs[i].data)) {
1048 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001049 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001050 }
1051
1052 return 0;
1053}
1054
Michal Vasko9af52322022-07-25 14:39:30 +02001055static int
roman3f9b65c2023-06-05 14:26:58 +02001056nc_server_tls_crl_path(struct nc_session *session, const char *crl_path, X509_STORE *store)
1057{
1058 int ret = 0;
1059 X509_CRL *crl = NULL;
1060 FILE *f;
1061
1062 f = fopen(crl_path, "r");
1063 if (!f) {
1064 ERR(session, "Unable to open CRL file \"%s\".", crl_path);
1065 return -1;
1066 }
1067
1068 /* try DER first */
1069 crl = d2i_X509_CRL_fp(f, NULL);
1070 if (crl) {
1071 /* success */
1072 goto ok;
1073 }
1074
1075 /* DER failed, try PEM */
1076 rewind(f);
1077 crl = PEM_read_X509_CRL(f, NULL, NULL, NULL);
1078 if (!crl) {
1079 ERR(session, "Reading CRL from file \"%s\" failed.", crl_path);
1080 ret = -1;
1081 goto cleanup;
1082 }
1083
1084ok:
1085 ret = X509_STORE_add_crl(store, crl);
1086 if (!ret) {
1087 ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error()));
1088 ret = -1;
1089 goto cleanup;
1090 }
1091 /* ok */
1092 ret = 0;
1093
1094cleanup:
1095 fclose(f);
1096 X509_CRL_free(crl);
1097 return ret;
1098}
1099
1100static size_t
1101nc_server_tls_curl_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
1102{
1103 struct nc_curl_data *data;
1104
1105 size = nmemb;
1106
1107 data = (struct nc_curl_data *)userdata;
1108
1109 data->data = nc_realloc(data->data, data->size + size);
roman3a95bb22023-10-26 11:07:17 +02001110 NC_CHECK_ERRMEM_RET(!data->data, 0);
roman3f9b65c2023-06-05 14:26:58 +02001111
1112 memcpy(&data->data[data->size], ptr, size);
1113 data->size += size;
1114
1115 return size;
1116}
1117
1118static int
1119nc_server_tls_curl_init(struct nc_session *session, CURL **handle, struct nc_curl_data *data)
1120{
1121 NC_CHECK_ARG_RET(session, handle, data, -1);
1122
1123 *handle = NULL;
1124
1125 *handle = curl_easy_init();
1126 if (!*handle) {
1127 ERR(session, "Initializing CURL failed.");
1128 return -1;
1129 }
1130
1131 if (curl_easy_setopt(*handle, CURLOPT_WRITEFUNCTION, nc_server_tls_curl_cb)) {
1132 ERR(session, "Setting curl callback failed.");
1133 return -1;
1134 }
1135
1136 if (curl_easy_setopt(*handle, CURLOPT_WRITEDATA, data)) {
1137 ERR(session, "Setting curl callback data failed.");
1138 return -1;
1139 }
1140
1141 return 0;
1142}
1143
1144static int
1145nc_server_tls_curl_fetch(struct nc_session *session, CURL *handle, const char *url)
1146{
1147 char err_buf[CURL_ERROR_SIZE];
1148
1149 /* set uri */
1150 if (curl_easy_setopt(handle, CURLOPT_URL, url)) {
1151 ERR(session, "Setting URI \"%s\" to download CRL from failed.", url);
1152 return -1;
1153 }
1154
1155 /* set err buf */
1156 curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, err_buf);
1157
1158 /* download */
1159 if (curl_easy_perform(handle)) {
1160 ERR(session, "Downloading CRL from \"%s\" failed (%s).", url, err_buf);
1161 return -1;
1162 }
1163
1164 return 0;
1165}
1166
1167static int
1168nc_server_tls_add_crl_to_store(struct nc_session *session, struct nc_curl_data *downloaded, X509_STORE *store)
1169{
1170 int ret = 0;
1171 X509_CRL *crl = NULL;
1172 BIO *bio = NULL;
1173
1174 /* try DER first */
1175 crl = d2i_X509_CRL(NULL, (const unsigned char **) &downloaded->data, downloaded->size);
1176 if (crl) {
1177 /* it was DER */
1178 goto ok;
1179 }
1180
1181 /* DER failed, try PEM next */
1182 bio = BIO_new_mem_buf(downloaded->data, downloaded->size);
1183 if (!bio) {
1184 ERR(session, "Creating new bio failed (%s).", ERR_reason_error_string(ERR_get_error()));
1185 ret = -1;
1186 goto cleanup;
1187 }
1188
1189 /* try to parse PEM from the downloaded data */
1190 crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
1191 if (!crl) {
1192 ERR(session, "Reading downloaded CRL failed (%s).", ERR_reason_error_string(ERR_get_error()));
1193 ret = -1;
1194 goto cleanup;
1195 }
1196
1197ok:
1198 /* we obtained the CRL, now add it to the CRL store */
1199 ret = X509_STORE_add_crl(store, crl);
1200 if (!ret) {
1201 ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error()));
1202 ret = -1;
1203 goto cleanup;
1204 }
1205 /* ok */
1206 ret = 0;
1207
1208cleanup:
1209 X509_CRL_free(crl);
1210 BIO_free(bio);
1211 return ret;
1212}
1213
1214static int
1215nc_server_tls_crl_url(struct nc_session *session, const char *url, X509_STORE *store)
1216{
1217 int ret = 0;
1218 CURL *handle = NULL;
1219 struct nc_curl_data downloaded = {0};
1220
1221 /* init curl */
1222 ret = nc_server_tls_curl_init(session, &handle, &downloaded);
1223 if (ret) {
1224 goto cleanup;
1225 }
1226
1227 VRB(session, "Downloading CRL from \"%s\".", url);
1228
1229 /* download the CRL */
1230 ret = nc_server_tls_curl_fetch(session, handle, url);
1231 if (ret) {
1232 goto cleanup;
1233 }
1234
1235 /* convert the downloaded data to CRL and add it to the store */
1236 ret = nc_server_tls_add_crl_to_store(session, &downloaded, store);
1237 if (ret) {
1238 goto cleanup;
1239 }
1240
1241cleanup:
1242 curl_easy_cleanup(handle);
1243 return ret;
1244}
1245
1246static int
1247nc_server_tls_crl_cert_ext(struct nc_session *session, X509_STORE *cert_store, X509_STORE *crl_store)
1248{
1249 int ret = 0, i, j, k, gtype;
1250 CURL *handle = NULL;
1251 struct nc_curl_data downloaded = {0};
1252
1253 STACK_OF(X509_OBJECT) * objs;
1254 X509_OBJECT *obj;
1255 X509 *cert;
1256
1257 STACK_OF(DIST_POINT) * dist_points;
1258 DIST_POINT *dist_point;
1259 GENERAL_NAMES *general_names;
1260 GENERAL_NAME *general_name;
1261 ASN1_STRING *asn_string_uri;
1262 const char *crl_distpoint_uri;
1263
1264 /* init curl */
1265 ret = nc_server_tls_curl_init(session, &handle, &downloaded);
1266 if (ret) {
1267 goto cleanup;
1268 }
1269
1270 /* treat all entries in the cert_store as X509_OBJECTs */
1271 objs = X509_STORE_get0_objects(cert_store);
1272 if (!objs) {
1273 ERR(session, "Getting certificates from store failed (%s).", ERR_reason_error_string(ERR_get_error()));
1274 ret = -1;
1275 goto cleanup;
1276 }
1277
1278 /* iterate over all the CAs */
1279 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1280 obj = sk_X509_OBJECT_value(objs, i);
1281 cert = X509_OBJECT_get0_X509(obj);
1282 if (!cert) {
1283 /* the object on this index was not a certificate */
1284 continue;
1285 }
1286
1287 /* get all the distribution points for this CA */
1288 dist_points = X509_get_ext_d2i(cert, NID_crl_distribution_points, NULL, NULL);
1289
1290 /* iterate over all the dist points (there can be multiple for a single cert) */
1291 for (j = 0; j < sk_DIST_POINT_num(dist_points); j++) {
1292 dist_point = sk_DIST_POINT_value(dist_points, j);
1293 if (!dist_point) {
1294 continue;
1295 }
1296 general_names = dist_point->distpoint->name.fullname;
1297
1298 /* iterate over all the GeneralesNames in the distribution point */
1299 for (k = 0; k < sk_GENERAL_NAME_num(general_names); k++) {
1300 general_name = sk_GENERAL_NAME_value(general_names, k);
1301 asn_string_uri = GENERAL_NAME_get0_value(general_name, &gtype);
1302
1303 /* check if the general name is a URI and has a valid length */
1304 if ((gtype != GEN_URI) || (ASN1_STRING_length(asn_string_uri) <= 6)) {
1305 continue;
1306 }
1307
1308 crl_distpoint_uri = (const char *) ASN1_STRING_get0_data(asn_string_uri);
1309
1310 VRB(session, "Downloading CRL from \"%s\".", crl_distpoint_uri);
1311
1312 /* download the CRL */
1313 ret = nc_server_tls_curl_fetch(session, handle, crl_distpoint_uri);
1314 if (ret) {
1315 /* failed to download the CRL from this entry, try th next */
1316 continue;
1317 }
1318
1319 /* convert the downloaded data to CRL and add it to the store */
1320 ret = nc_server_tls_add_crl_to_store(session, &downloaded, crl_store);
1321 if (ret) {
1322 goto cleanup;
1323 }
1324
1325 /* the CRL was downloaded, no need to download it again using different protocol */
1326 break;
1327 }
1328 }
1329 }
1330
1331cleanup:
1332 curl_easy_cleanup(handle);
1333 return ret;
1334}
1335
1336static int
1337nc_tls_store_set_crl(struct nc_session *session, struct nc_server_tls_opts *opts, X509_STORE *store)
1338{
1339 if (!opts->crl_store) {
1340 /* first call on this endpoint */
1341 opts->crl_store = X509_STORE_new();
roman3a95bb22023-10-26 11:07:17 +02001342 NC_CHECK_ERRMEM_GOTO(!opts->crl_store,; , fail);
roman3f9b65c2023-06-05 14:26:58 +02001343 }
1344
1345 if (opts->crl_path) {
1346 if (nc_server_tls_crl_path(session, opts->crl_path, opts->crl_store)) {
1347 goto fail;
1348 }
1349 } else if (opts->crl_url) {
1350 if (nc_server_tls_crl_url(session, opts->crl_url, opts->crl_store)) {
1351 goto fail;
1352 }
1353 } else {
1354 if (nc_server_tls_crl_cert_ext(session, store, opts->crl_store)) {
1355 goto fail;
1356 }
1357 }
1358
1359 return 0;
1360
1361fail:
1362 return -1;
1363}
1364
1365static int
Michal Vasko9af52322022-07-25 14:39:30 +02001366nc_server_tls_accept_check(int accept_ret, struct nc_session *session)
1367{
1368 int verify;
1369
1370 /* check certificate verification result */
1371 verify = SSL_get_verify_result(session->ti.tls);
1372 switch (verify) {
1373 case X509_V_OK:
1374 if (accept_ret == 1) {
1375 VRB(session, "Client certificate verified.");
1376 }
1377 break;
1378 default:
1379 ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify));
1380 }
1381
1382 if (accept_ret != 1) {
1383 switch (SSL_get_error(session->ti.tls, accept_ret)) {
1384 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001385 ERR(session, "SSL accept failed (%s).", strerror(errno));
Michal Vasko9af52322022-07-25 14:39:30 +02001386 break;
1387 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001388 ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +02001389 break;
1390 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001391 ERR(session, "SSL accept failed.");
Michal Vasko9af52322022-07-25 14:39:30 +02001392 break;
1393 }
1394 }
1395
1396 return accept_ret;
1397}
1398
Michal Vasko3031aae2016-01-27 16:07:18 +01001399int
roman3f9b65c2023-06-05 14:26:58 +02001400nc_accept_tls_session(struct nc_session *session, struct nc_server_tls_opts *opts, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001401{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001402 X509_STORE *cert_store;
1403 SSL_CTX *tls_ctx;
Michal Vasko36c7be82017-02-22 13:37:59 +01001404 int ret;
roman6ece9c52022-06-22 09:29:17 +02001405 struct timespec ts_timeout;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001406
Michal Vaskoe2713da2016-08-22 16:06:40 +02001407 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001408 tls_ctx = SSL_CTX_new(TLS_server_method());
roman3f9b65c2023-06-05 14:26:58 +02001409
Michal Vaskoe2713da2016-08-22 16:06:40 +02001410 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001411 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001412 goto error;
1413 }
roman3f9b65c2023-06-05 14:26:58 +02001414
Michal Vaskoe2713da2016-08-22 16:06:40 +02001415 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify);
roman3f9b65c2023-06-05 14:26:58 +02001416 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001417 goto error;
1418 }
1419
1420 /* X509_STORE, managed (freed) with the context */
1421 cert_store = X509_STORE_new();
roman3f9b65c2023-06-05 14:26:58 +02001422 if (!cert_store) {
1423 ERR(session, "Creating certificate store failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001424 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001425 }
1426
roman3f9b65c2023-06-05 14:26:58 +02001427 /* set end-entity certs as cert store data, retrieve them if verification fails later */
1428 ret = X509_STORE_set_ex_data(cert_store, 1, &opts->ee_certs);
1429 if (!ret) {
1430 ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error()));
1431 goto error;
1432 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001433
roman3f9b65c2023-06-05 14:26:58 +02001434 /* do the same for referenced endpoint's end entity certs */
1435 if (opts->endpt_client_ref) {
1436 ret = X509_STORE_set_ex_data(cert_store, 2, &opts->endpt_client_ref->opts.tls->ee_certs);
1437 if (!ret) {
1438 ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001439 goto error;
1440 }
1441 }
1442
roman3f9b65c2023-06-05 14:26:58 +02001443 /* set store to the context */
1444 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1445
1446 /* set certificate authority certs */
1447 if (nc_tls_store_set_trusted_certs(cert_store, &opts->ca_certs)) {
1448 goto error;
1449 }
1450
1451 /* set referenced endpoint's CA certs if set */
1452 if (opts->endpt_client_ref) {
1453 if (nc_tls_store_set_trusted_certs(cert_store, &opts->endpt_client_ref->opts.tls->ca_certs)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001454 goto error;
1455 }
roman3f9b65c2023-06-05 14:26:58 +02001456 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001457
roman3f9b65c2023-06-05 14:26:58 +02001458 /* set Certificate Revocation List if configured */
1459 if (opts->crl_path || opts->crl_url || opts->crl_cert_ext) {
1460 if (nc_tls_store_set_crl(session, opts, cert_store)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001461 goto error;
1462 }
1463 }
1464
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001465 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001466 session->ti.tls = SSL_new(tls_ctx);
1467
Michal Vasko4c1fb492017-01-30 14:31:07 +01001468 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001469 SSL_CTX_free(tls_ctx);
1470 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001471
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001472 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001473 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001474 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001475 }
1476
roman3f9b65c2023-06-05 14:26:58 +02001477 /* set TLS versions for the current SSL session */
1478 if (opts->tls_versions) {
1479 if (!(opts->tls_versions & NC_TLS_VERSION_10)) {
1480 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1);
1481 }
1482 if (!(opts->tls_versions & NC_TLS_VERSION_11)) {
1483 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_1);
1484 }
1485 if (!(opts->tls_versions & NC_TLS_VERSION_12)) {
1486 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_2);
1487 }
1488 if (!(opts->tls_versions & NC_TLS_VERSION_13)) {
1489 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_3);
1490 }
1491 }
1492
1493 /* set TLS cipher suites */
1494 if (opts->ciphers) {
1495 /* set for TLS1.2 and lower */
1496 SSL_set_cipher_list(session->ti.tls, opts->ciphers);
1497 /* set for TLS1.3 */
1498 SSL_set_ciphersuites(session->ti.tls, opts->ciphers);
1499 }
1500
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001501 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001502 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001503 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1504
Michal Vasko6d292992016-01-18 09:42:38 +01001505 /* store session on per-thread basis */
Michal Vasko5c2f7952016-01-22 13:16:31 +01001506 pthread_once(&verify_once, nc_tls_make_verify_key);
1507 pthread_setspecific(verify_key, session);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001508
Michal Vasko36c7be82017-02-22 13:37:59 +01001509 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001510 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001511 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001512 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1513 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001514 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +02001515 ERR(session, "SSL accept timeout.");
roman6ece9c52022-06-22 09:29:17 +02001516 return 0;
Michal Vasko0190bc32016-03-02 15:47:49 +01001517 }
1518 }
Michal Vasko9af52322022-07-25 14:39:30 +02001519 if (nc_server_tls_accept_check(ret, session) != 1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001520 return -1;
1521 }
1522
Michal Vasko1a38c862016-01-15 15:50:07 +01001523 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001524
1525error:
1526 if (sock > -1) {
1527 close(sock);
1528 }
1529 SSL_CTX_free(tls_ctx);
1530 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001531}