blob: a11c11b35763e8b25f18fec88ac402b513771f21 [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 Vaskoc14e3c82016-01-11 16:14:30 +010041extern struct nc_server_opts server_opts;
Michal Vaskoc61c4492016-01-25 11:13:34 +010042
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043static char *
Michal Vasko18aeb5d2017-02-17 09:23:56 +010044asn1time_to_str(const 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 }
roman3f9b65c2023-06-05 14:26:58 +020053
Michal Vaskoc14e3c82016-01-11 16:14:30 +010054 bio = BIO_new(BIO_s_mem());
55 if (!bio) {
56 return NULL;
57 }
roman3f9b65c2023-06-05 14:26:58 +020058
Michal Vaskoc14e3c82016-01-11 16:14:30 +010059 ASN1_TIME_print(bio, t);
60 n = BIO_pending(bio);
61 cp = malloc(n + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +010062 if (!cp) {
63 ERRMEM;
64 BIO_free(bio);
65 return NULL;
66 }
roman3f9b65c2023-06-05 14:26:58 +020067
Michal Vaskoc14e3c82016-01-11 16:14:30 +010068 n = BIO_read(bio, cp, n);
69 if (n < 0) {
70 BIO_free(bio);
71 free(cp);
72 return NULL;
73 }
roman3f9b65c2023-06-05 14:26:58 +020074
Michal Vaskoc14e3c82016-01-11 16:14:30 +010075 cp[n] = '\0';
76 BIO_free(bio);
77 return cp;
78}
79
80static void
81digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str)
82{
83 unsigned int i;
84
85 *str = malloc(dig_len * 3);
Michal Vasko4eb3c312016-03-01 14:09:37 +010086 if (!*str) {
87 ERRMEM;
88 return;
89 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +010090 for (i = 0; i < dig_len - 1; ++i) {
91 sprintf((*str) + (i * 3), "%02x:", digest[i]);
92 }
93 sprintf((*str) + (i * 3), "%02x", digest[i]);
94}
95
96/* return NULL - SSL error can be retrieved */
97static X509 *
98base64der_to_cert(const char *in)
99{
100 X509 *out;
101 char *buf;
102 BIO *bio;
103
104 if (in == NULL) {
105 return NULL;
106 }
107
108 if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) {
109 return NULL;
110 }
111 bio = BIO_new_mem_buf(buf, strlen(buf));
112 if (!bio) {
113 free(buf);
114 return NULL;
115 }
116
117 out = PEM_read_bio_X509(bio, NULL, NULL, NULL);
118 if (!out) {
119 free(buf);
120 BIO_free(bio);
121 return NULL;
122 }
123
124 free(buf);
125 BIO_free(bio);
126 return out;
127}
128
Michal Vasko6e08cb72017-02-02 11:15:29 +0100129static EVP_PKEY *
Michal Vaskoddce1212019-05-24 09:58:49 +0200130base64der_to_privatekey(const char *in, const char *key_str)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100131{
132 EVP_PKEY *out;
133 char *buf;
134 BIO *bio;
135
136 if (in == NULL) {
137 return NULL;
138 }
139
Michal Vaskoddce1212019-05-24 09:58:49 +0200140 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 +0200141 key_str, " PRIVATE KEY-----") == -1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100142 return NULL;
143 }
144 bio = BIO_new_mem_buf(buf, strlen(buf));
145 if (!bio) {
146 free(buf);
147 return NULL;
148 }
149
150 out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
151 if (!out) {
152 free(buf);
153 BIO_free(bio);
154 return NULL;
155 }
156
157 free(buf);
158 BIO_free(bio);
159 return out;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100160}
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100161
162static int
163cert_pubkey_match(X509 *cert1, X509 *cert2)
164{
165 ASN1_BIT_STRING *bitstr1, *bitstr2;
166
167 bitstr1 = X509_get0_pubkey_bitstr(cert1);
168 bitstr2 = X509_get0_pubkey_bitstr(cert2);
169
170 if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) ||
171 memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) {
172 return 0;
173 }
174
175 return 1;
176}
177
178static int
179nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username)
180{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200181 STACK_OF(GENERAL_NAME) * san_names;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100182 GENERAL_NAME *san_name;
183 ASN1_OCTET_STRING *ip;
184 int i, san_count;
185 char *subject, *common_name;
186
Michal Vaskod9a78642022-02-24 16:25:21 +0100187 *username = NULL;
188
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100189 if (map_type == NC_TLS_CTN_COMMON_NAME) {
190 subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0);
191 common_name = strstr(subject, "CN=");
192 if (!common_name) {
Michal Vasko05532772021-06-03 12:12:38 +0200193 WRN(NULL, "Certificate does not include the commonName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100194 free(subject);
195 return 1;
196 }
197 common_name += 3;
198 if (strchr(common_name, '/')) {
199 *strchr(common_name, '/') = '\0';
200 }
201 *username = strdup(common_name);
roman3a95bb22023-10-26 11:07:17 +0200202 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100203 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 Vasko05532772021-06-03 12:12:38 +0200208 WRN(NULL, "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) */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200217 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) &&
218 (san_name->type == GEN_EMAIL)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100219 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name));
roman3a95bb22023-10-26 11:07:17 +0200220 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100221 break;
222 }
223
224 /* dNSName */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200225 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) &&
226 (san_name->type == GEN_DNS)) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100227 *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName));
roman3a95bb22023-10-26 11:07:17 +0200228 NC_CHECK_ERRMEM_RET(!*username, 1);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100229 break;
230 }
231
232 /* iPAddress */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200233 if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_IP_ADDRESS)) &&
234 (san_name->type == GEN_IPADD)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100235 ip = san_name->d.iPAddress;
236 if (ip->length == 4) {
237 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 +0100238 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100239 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
240 return -1;
241 }
242 break;
243 } else if (ip->length == 16) {
244 if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
245 ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5],
246 ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11],
247 ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100248 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100249 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
250 return -1;
251 }
252 break;
253 } else {
Michal Vasko05532772021-06-03 12:12:38 +0200254 WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100255 }
256 }
257 }
258 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
259
Vitaly Kuzina3b66322021-08-26 12:33:43 +0300260 if (i == san_count) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100261 switch (map_type) {
262 case NC_TLS_CTN_SAN_RFC822_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200263 WRN(NULL, "Certificate does not include the SAN rfc822Name field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100264 break;
265 case NC_TLS_CTN_SAN_DNS_NAME:
Michal Vasko05532772021-06-03 12:12:38 +0200266 WRN(NULL, "Certificate does not include the SAN dNSName field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100267 break;
268 case NC_TLS_CTN_SAN_IP_ADDRESS:
Michal Vasko05532772021-06-03 12:12:38 +0200269 WRN(NULL, "Certificate does not include the SAN iPAddress field.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100270 break;
271 case NC_TLS_CTN_SAN_ANY:
Michal Vasko05532772021-06-03 12:12:38 +0200272 WRN(NULL, "Certificate does not include any relevant SAN fields.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100273 break;
274 default:
275 break;
276 }
277 return 1;
278 }
279 }
280
281 return 0;
282}
283
284/* return: 0 - OK, 1 - no match, -1 - error */
285static int
Roytakbe391922023-07-27 19:59:24 +0200286nc_tls_cert_to_name(struct nc_session *session, struct nc_ctn *ctn_first, X509 *cert)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100287{
288 char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
289 char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
roman3a95bb22023-10-26 11:07:17 +0200290 unsigned char *buf;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100291 unsigned int buf_len = 64;
292 int ret = 0;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100293 struct nc_ctn *ctn;
Roytakbe391922023-07-27 19:59:24 +0200294 NC_TLS_CTN_MAPTYPE map_type;
295 char *username = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100296
roman3a95bb22023-10-26 11:07:17 +0200297 buf = malloc(buf_len);
298 NC_CHECK_ERRMEM_RET(!buf, -1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100299
roman84473212023-10-12 16:29:17 +0200300 if (!session || !cert) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100301 free(buf);
302 return -1;
303 }
304
Michal Vaskoc61c4492016-01-25 11:13:34 +0100305 for (ctn = ctn_first; ctn; ctn = ctn->next) {
Roytakbe391922023-07-27 19:59:24 +0200306 /* reset map_type */
307 map_type = NC_TLS_CTN_UNKNOWN;
308
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100309 /* first make sure the entry is valid */
roman3f9b65c2023-06-05 14:26:58 +0200310 if (!ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) {
roman78df0fa2023-11-02 10:33:57 +0100311 VRB(session, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id);
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100312 continue;
313 }
314
roman3f9b65c2023-06-05 14:26:58 +0200315 /* if ctn has no fingerprint, it will match any certificate */
316 if (!ctn->fingerprint) {
317 map_type = ctn->map_type;
318
319 /* MD5 */
320 } else if (!strncmp(ctn->fingerprint, "01", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100321 if (!digest_md5) {
322 if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100323 ERR(session, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100324 ret = -1;
325 goto cleanup;
326 }
327 digest_to_str(buf, buf_len, &digest_md5);
328 }
329
Michal Vasko5e3f3392016-01-20 11:13:01 +0100330 if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) {
Roytakbe391922023-07-27 19:59:24 +0200331 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100332 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200333 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100334 }
roman3f9b65c2023-06-05 14:26:58 +0200335 free(digest_md5);
336 digest_md5 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100337
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200338 /* SHA-1 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100339 } else if (!strncmp(ctn->fingerprint, "02", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100340 if (!digest_sha1) {
341 if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100342 ERR(session, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100343 ret = -1;
344 goto cleanup;
345 }
346 digest_to_str(buf, buf_len, &digest_sha1);
347 }
348
Michal Vasko5e3f3392016-01-20 11:13:01 +0100349 if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) {
Roytakbe391922023-07-27 19:59:24 +0200350 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100351 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200352 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100353 }
roman3f9b65c2023-06-05 14:26:58 +0200354 free(digest_sha1);
355 digest_sha1 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100356
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200357 /* SHA-224 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100358 } else if (!strncmp(ctn->fingerprint, "03", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100359 if (!digest_sha224) {
360 if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100361 ERR(session, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100362 ret = -1;
363 goto cleanup;
364 }
365 digest_to_str(buf, buf_len, &digest_sha224);
366 }
367
Michal Vasko5e3f3392016-01-20 11:13:01 +0100368 if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) {
Roytakbe391922023-07-27 19:59:24 +0200369 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100370 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200371 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100372 }
roman3f9b65c2023-06-05 14:26:58 +0200373 free(digest_sha224);
374 digest_sha224 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100375
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200376 /* SHA-256 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100377 } else if (!strncmp(ctn->fingerprint, "04", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100378 if (!digest_sha256) {
379 if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100380 ERR(session, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100381 ret = -1;
382 goto cleanup;
383 }
384 digest_to_str(buf, buf_len, &digest_sha256);
385 }
386
Michal Vasko5e3f3392016-01-20 11:13:01 +0100387 if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) {
Roytakbe391922023-07-27 19:59:24 +0200388 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100389 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200390 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100391 }
roman3f9b65c2023-06-05 14:26:58 +0200392 free(digest_sha256);
393 digest_sha256 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100394
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200395 /* SHA-384 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100396 } else if (!strncmp(ctn->fingerprint, "05", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100397 if (!digest_sha384) {
398 if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100399 ERR(session, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100400 ret = -1;
401 goto cleanup;
402 }
403 digest_to_str(buf, buf_len, &digest_sha384);
404 }
405
Michal Vasko5e3f3392016-01-20 11:13:01 +0100406 if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) {
Roytakbe391922023-07-27 19:59:24 +0200407 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100408 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200409 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100410 }
roman3f9b65c2023-06-05 14:26:58 +0200411 free(digest_sha384);
412 digest_sha384 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100413
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200414 /* SHA-512 */
Michal Vasko5e3f3392016-01-20 11:13:01 +0100415 } else if (!strncmp(ctn->fingerprint, "06", 2)) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100416 if (!digest_sha512) {
417 if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) {
roman78df0fa2023-11-02 10:33:57 +0100418 ERR(session, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100419 ret = -1;
420 goto cleanup;
421 }
422 digest_to_str(buf, buf_len, &digest_sha512);
423 }
424
Michal Vasko5e3f3392016-01-20 11:13:01 +0100425 if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) {
Roytakbe391922023-07-27 19:59:24 +0200426 /* we got ourselves a potential winner! */
roman78df0fa2023-11-02 10:33:57 +0100427 VRB(session, "Cert verify CTN: entry with a matching fingerprint found.");
Roytakbe391922023-07-27 19:59:24 +0200428 map_type = ctn->map_type;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100429 }
roman3f9b65c2023-06-05 14:26:58 +0200430 free(digest_sha512);
431 digest_sha512 = NULL;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100432
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200433 /* unknown */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100434 } else {
roman78df0fa2023-11-02 10:33:57 +0100435 WRN(session, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint);
Roytakbe391922023-07-27 19:59:24 +0200436 continue;
437 }
438
439 if (map_type != NC_TLS_CTN_UNKNOWN) {
440 /* found a fingerprint match */
441 if (map_type == NC_TLS_CTN_SPECIFIED) {
442 /* specified -> get username from the ctn entry */
443 session->username = strdup(ctn->name);
roman3a95bb22023-10-26 11:07:17 +0200444 NC_CHECK_ERRMEM_GOTO(!session->username, ret = -1, cleanup);
Roytakbe391922023-07-27 19:59:24 +0200445 } else {
446 /* try to get the username from the cert with this ctn's map type */
447 ret = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &username);
448 if (ret == -1) {
449 /* fatal error */
450 goto cleanup;
451 } else if (ret) {
452 /* didn't get username, try next ctn entry */
453 continue;
454 }
455
456 /* success */
457 session->username = username;
458 }
459
460 /* matching fingerprint found and username obtained, success */
461 ret = 0;
462 goto cleanup;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100463 }
464 }
465
Michal Vasko5e3f3392016-01-20 11:13:01 +0100466 if (!ctn) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100467 ret = 1;
468 }
469
470cleanup:
471 free(digest_md5);
472 free(digest_sha1);
473 free(digest_sha224);
474 free(digest_sha256);
475 free(digest_sha384);
476 free(digest_sha512);
477 free(buf);
478 return ret;
479}
480
roman3f9b65c2023-06-05 14:26:58 +0200481static int
482nc_server_tls_check_crl(X509_STORE *crl_store, X509_STORE_CTX *x509_ctx, X509 *cert,
483 const X509_NAME *subject, const X509_NAME *issuer)
484{
485 int n, i, ret = 0;
486 X509_STORE_CTX *store_ctx = NULL;
487 X509_OBJECT *obj = NULL;
488 X509_CRL *crl;
489 X509_REVOKED *revoked;
490 EVP_PKEY *pubkey;
491 const ASN1_INTEGER *serial;
492 const ASN1_TIME *last_update = NULL, *next_update = NULL;
493 char *cp;
494
495 store_ctx = X509_STORE_CTX_new();
roman3a95bb22023-10-26 11:07:17 +0200496 NC_CHECK_ERRMEM_GOTO(!store_ctx, ret = -1, cleanup);
roman3f9b65c2023-06-05 14:26:58 +0200497
498 /* init store context */
499 ret = X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
500 if (!ret) {
501 ERR(NULL, "Initializing x509 store ctx failed (%s).", ERR_reason_error_string(ERR_get_error()));
502 ret = -1;
503 goto cleanup;
504 }
505 ret = 0;
506
507 /* try to find a CRL entry that corresponds to the current certificate in question */
508 obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, subject);
509 crl = X509_OBJECT_get0_X509_CRL(obj);
510 X509_OBJECT_free(obj);
511 if (crl) {
512 /* found it */
513 cp = X509_NAME_oneline(subject, NULL, 0);
514 VRB(NULL, "Cert verify CRL: issuer: %s.", cp);
515 OPENSSL_free(cp);
516
517 last_update = X509_CRL_get0_lastUpdate(crl);
518 next_update = X509_CRL_get0_nextUpdate(crl);
519 cp = asn1time_to_str(last_update);
520 VRB(NULL, "Cert verify CRL: last update: %s.", cp);
521 free(cp);
522 cp = asn1time_to_str(next_update);
523 VRB(NULL, "Cert verify CRL: next update: %s.", cp);
524 free(cp);
525
526 /* verify the signature on this CRL */
527 pubkey = X509_get0_pubkey(cert);
528 if (X509_CRL_verify(crl, pubkey) <= 0) {
529 ERR(NULL, "Cert verify CRL: invalid signature.");
530 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
531 ret = -1;
532 goto cleanup;
533 }
534
535 /* check date of CRL to make sure it's not expired */
536 if (!next_update) {
537 ERR(NULL, "Cert verify CRL: invalid nextUpdate field.");
538 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
539 ret = -1;
540 goto cleanup;
541 }
542
543 if (X509_cmp_current_time(next_update) < 0) {
544 ERR(NULL, "Cert verify CRL: expired - revoking all certificates.");
545 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
546 ret = -1;
547 goto cleanup;
548 }
549 }
550
551 /* try to retrieve a CRL corresponding to the _issuer_ of
552 * the current certificate in order to check for revocation */
553 obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, issuer);
554 crl = X509_OBJECT_get0_X509_CRL(obj);
555 if (crl) {
556 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
557 for (i = 0; i < n; i++) {
558 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
559 serial = X509_REVOKED_get0_serialNumber(revoked);
560 if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) {
561 cp = X509_NAME_oneline(issuer, NULL, 0);
562 ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.",
563 serial, serial, cp);
564 OPENSSL_free(cp);
565 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
566 ret = -1;
567 goto cleanup;
568 }
569 }
570 }
571
572cleanup:
573 X509_STORE_CTX_free(store_ctx);
574 X509_OBJECT_free(obj);
575 return ret;
576}
577
578static int
579nc_server_tls_ts_ref_get_certs(const char *referenced_name, struct nc_certificate **certs, uint16_t *cert_count)
580{
581 uint16_t i;
582 struct nc_truststore *ts = &server_opts.truststore;
583
584 *certs = NULL;
585 *cert_count = 0;
586
587 /* lookup name */
588 for (i = 0; i < ts->cert_bag_count; i++) {
589 if (!strcmp(referenced_name, ts->cert_bags[i].name)) {
590 break;
591 }
592 }
593
594 if (i == ts->cert_bag_count) {
595 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
596 return -1;
597 }
598
599 *certs = ts->cert_bags[i].certs;
600 *cert_count = ts->cert_bags[i].cert_count;
601 return 0;
602}
603
604/* In case a CA chain verification failed an end-entity certificate must match.
605 * The meaning of local_or_referenced is that it states, which end-entity certificates to check
606 * (1 = current endpoint's, 2 = referenced endpoint's).
607 */
608static int
609nc_server_tls_do_preverify(struct nc_session *session, X509_STORE_CTX *x509_ctx, int local_or_referenced)
610{
611 X509_STORE *store;
612 struct nc_cert_grouping *ee_certs;
613 int i, ret;
614 X509 *cert;
615 struct nc_certificate *certs;
616 uint16_t cert_count;
617
618 store = X509_STORE_CTX_get0_store(x509_ctx);
619 if (!store) {
620 ERR(session, "Error getting store from context (%s).", ERR_reason_error_string(ERR_get_error()));
621 return -1;
622 }
623
624 /* get the data from the store */
625 ee_certs = X509_STORE_get_ex_data(store, local_or_referenced);
626 if (!ee_certs) {
627 ERR(session, "Error getting data from store (%s).", ERR_reason_error_string(ERR_get_error()));
628 return -1;
629 }
630
631 if (ee_certs->store == NC_STORE_LOCAL) {
632 /* local definition */
633 certs = ee_certs->certs;
634 cert_count = ee_certs->cert_count;
635 } else {
636 /* truststore reference */
637 if (nc_server_tls_ts_ref_get_certs(ee_certs->ts_ref, &certs, &cert_count)) {
638 ERR(NULL, "Error getting end-entity certificates from the truststore reference \"%s\".", ee_certs->ts_ref);
639 return -1;
640 }
641 }
642
643 for (i = 0; i < cert_count; i++) {
644 cert = base64der_to_cert(certs[i].data);
645 ret = cert_pubkey_match(session->opts.server.client_cert, cert);
646 X509_free(cert);
647 if (ret) {
648 /* we are just overriding the failed standard certificate verification (preverify_ok == 0),
649 * this callback will be called again with the same current certificate and preverify_ok == 1 */
650 VRB(session, "Cert verify: fail (%s), but the end-entity certificate is trusted, continuing.",
651 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx)));
652 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
653 return 1;
654 }
655 }
656
657 return 0;
658}
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100659
660static int
661nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
662{
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100663 X509_NAME *subject;
664 X509_NAME *issuer;
665 X509 *cert;
roman3f9b65c2023-06-05 14:26:58 +0200666 char *cp;
roman78df0fa2023-11-02 10:33:57 +0100667 X509_STORE *store;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200668
669 STACK_OF(X509) * cert_stack;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200670 struct nc_session *session;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100671 struct nc_server_tls_opts *opts;
roman78df0fa2023-11-02 10:33:57 +0100672 struct nc_endpt *referenced_endpt;
roman3f9b65c2023-06-05 14:26:58 +0200673 int rc, depth;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100674
roman78df0fa2023-11-02 10:33:57 +0100675 store = X509_STORE_CTX_get0_store(x509_ctx);
676 if (!store) {
677 ERR(NULL, "Error getting store from context (%s).", ERR_reason_error_string(ERR_get_error()));
678 return 0;
679 }
680
681 /* get session from the store */
682 session = X509_STORE_get_ex_data(store, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100683 if (!session) {
roman78df0fa2023-11-02 10:33:57 +0100684 ERR(session, "Error getting session from store (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100685 return 0;
686 }
687
688 opts = session->data;
689
690 /* get the last certificate, that is the peer (client) certificate */
691 if (!session->opts.server.client_cert) {
692 cert_stack = X509_STORE_CTX_get1_chain(x509_ctx);
Andrew Langefeld62bc6712018-08-28 16:17:16 -0500693 session->opts.server.client_cert = sk_X509_value(cert_stack, 0);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100694 X509_up_ref(session->opts.server.client_cert);
695 sk_X509_pop_free(cert_stack, X509_free);
696 }
697
roman3f9b65c2023-06-05 14:26:58 +0200698 /* standard certificate verification failed, so an end-entity client cert must match to continue */
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100699 if (!preverify_ok) {
roman3f9b65c2023-06-05 14:26:58 +0200700 /* check current endpoint's end-entity certs */
701 rc = nc_server_tls_do_preverify(session, x509_ctx, 1);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100702 if (rc == -1) {
roman3f9b65c2023-06-05 14:26:58 +0200703 return 0;
704 } else if (rc == 1) {
705 return 1;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100706 }
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100707
roman3f9b65c2023-06-05 14:26:58 +0200708 /* no match, continue */
roman78df0fa2023-11-02 10:33:57 +0100709 if (opts->referenced_endpt_name) {
roman3f9b65c2023-06-05 14:26:58 +0200710 /* check referenced endpoint's end-entity certs */
711 rc = nc_server_tls_do_preverify(session, x509_ctx, 2);
712 if (rc == -1) {
713 return 0;
714 } else if (rc == 1) {
715 return 1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100716 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100717 }
718
roman3f9b65c2023-06-05 14:26:58 +0200719 /* no match, fail */
Michal Vasko05532772021-06-03 12:12:38 +0200720 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 +0100721 return 0;
722 }
723
724 /* print cert verify info */
725 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
Michal Vasko05532772021-06-03 12:12:38 +0200726 VRB(session, "Cert verify: depth %d.", depth);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100727
728 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
729 subject = X509_get_subject_name(cert);
730 issuer = X509_get_issuer_name(cert);
731
732 cp = X509_NAME_oneline(subject, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200733 VRB(session, "Cert verify: subject: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100734 OPENSSL_free(cp);
735 cp = X509_NAME_oneline(issuer, NULL, 0);
Michal Vasko05532772021-06-03 12:12:38 +0200736 VRB(session, "Cert verify: issuer: %s.", cp);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100737 OPENSSL_free(cp);
738
roman3f9b65c2023-06-05 14:26:58 +0200739 /* check if the current certificate is revoked if CRL is set */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100740 if (opts->crl_store) {
roman3f9b65c2023-06-05 14:26:58 +0200741 rc = nc_server_tls_check_crl(opts->crl_store, x509_ctx, cert, subject, issuer);
742 if (rc) {
743 return 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100744 }
745 }
746
747 /* cert-to-name already successful */
748 if (session->username) {
749 return 1;
750 }
751
752 /* cert-to-name */
Roytakbe391922023-07-27 19:59:24 +0200753 rc = nc_tls_cert_to_name(session, opts->ctn, cert);
roman3f9b65c2023-06-05 14:26:58 +0200754 if (rc == -1) {
755 /* fatal error */
756 depth = 0;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100757 goto fail;
roman78df0fa2023-11-02 10:33:57 +0100758 } else if ((rc == 1) && !opts->referenced_endpt_name) {
roman3f9b65c2023-06-05 14:26:58 +0200759 /* no match found and no referenced endpoint */
760 goto fail;
roman78df0fa2023-11-02 10:33:57 +0100761 } else if ((rc == 1) && opts->referenced_endpt_name) {
roman3f9b65c2023-06-05 14:26:58 +0200762 /* no match found, but has a referenced endpoint so try it */
roman78df0fa2023-11-02 10:33:57 +0100763 if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) {
764 /* fatal error */
765 ERRINT;
766 depth = 0;
767 goto fail;
768 }
769
770 rc = nc_tls_cert_to_name(session, referenced_endpt->opts.tls->ctn, cert);
roman3f9b65c2023-06-05 14:26:58 +0200771 if (rc) {
772 if (rc == -1) {
773 /* fatal error */
774 depth = 0;
775 }
776 /* rc == 1 is a normal CTN fail (no match found) */
777 goto fail;
778 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100779 }
780
Michal Vasko05532772021-06-03 12:12:38 +0200781 VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100782
783 if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) {
Michal Vasko05532772021-06-03 12:12:38 +0200784 VRB(session, "Cert verify: user verify callback revoked authorization.");
Michal Vasko7060bcf2016-11-28 14:48:11 +0100785 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
786 return 0;
787 }
788
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100789 return 1;
790
791fail:
792 if (depth > 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200793 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 +0100794 return 1;
795 }
796
Michal Vasko05532772021-06-03 12:12:38 +0200797 VRB(session, "Cert-to-name unsuccessful, dropping the new client.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100798 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
799 return 0;
800}
801
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100802static int
Michal Vaskof585ac72016-11-25 15:16:38 +0100803nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200804 struct nc_server_tls_opts *opts)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100805{
806 struct nc_ctn *ctn;
807 int ret = -1;
808
809 for (ctn = opts->ctn; ctn; ctn = ctn->next) {
810 if (id && *id && (*id != ctn->id)) {
811 continue;
812 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100813 if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100814 continue;
815 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100816 if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100817 continue;
818 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100819 if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100820 continue;
821 }
822
823 /* first match, good enough */
824 if (id && !(*id)) {
825 *id = ctn->id;
826 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100827 if (fingerprint && !(*fingerprint) && ctn->fingerprint) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100828 *fingerprint = strdup(ctn->fingerprint);
829 }
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100830 if (map_type && !(*map_type) && ctn->map_type) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100831 *map_type = ctn->map_type;
832 }
Adam Richterd44680e2019-06-15 13:16:16 -0700833 if (name && !(*name) && ctn->name) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100834 *name = strdup(ctn->name);
835 }
836
837 ret = 0;
838 break;
839 }
840
841 return ret;
842}
843
844API int
Michal Vaskof585ac72016-11-25 15:16:38 +0100845nc_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 +0200846 char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100847{
848 int ret;
849 struct nc_endpt *endpt;
850
roman40672412023-05-04 11:10:22 +0200851 NC_CHECK_ARG_RET(NULL, endpt_name, -1);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100852
853 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100854 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100855 if (!endpt) {
856 return -1;
857 }
858 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
859 /* UNLOCK */
romanc1d2b092023-02-02 08:58:27 +0100860 pthread_rwlock_unlock(&server_opts.config_lock);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100861
862 return ret;
863}
864
865API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200866nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint,
867 NC_TLS_CTN_MAPTYPE *map_type, char **name)
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100868{
869 int ret;
870 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200871 struct nc_ch_endpt *endpt;
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100872
873 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200874 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client);
875 if (!endpt) {
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100876 return -1;
877 }
878
Michal Vaskoadf30f02019-06-24 09:34:47 +0200879 ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100880
881 /* UNLOCK */
882 nc_server_ch_client_unlock(client);
883
884 return ret;
885}
886
Michal Vasko709598e2016-11-28 14:48:32 +0100887API const X509 *
888nc_session_get_client_cert(const struct nc_session *session)
889{
890 if (!session || (session->side != NC_SERVER)) {
roman40672412023-05-04 11:10:22 +0200891 ERRARG(session, "session");
Michal Vasko709598e2016-11-28 14:48:32 +0100892 return NULL;
893 }
894
895 return session->opts.server.client_cert;
896}
897
Michal Vasko7060bcf2016-11-28 14:48:11 +0100898API void
899nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session))
900{
901 server_opts.user_verify_clb = verify_clb;
902}
903
roman3f9b65c2023-06-05 14:26:58 +0200904static int
905nc_server_tls_ks_ref_get_cert_key(const char *referenced_key_name, const char *referenced_cert_name,
906 char **privkey_data, NC_PRIVKEY_FORMAT *privkey_type, char **cert_data)
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500907{
roman3f9b65c2023-06-05 14:26:58 +0200908 uint16_t i, j;
909 struct nc_keystore *ks = &server_opts.keystore;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500910
roman3f9b65c2023-06-05 14:26:58 +0200911 *privkey_data = NULL;
912 *cert_data = NULL;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500913
roman3f9b65c2023-06-05 14:26:58 +0200914 /* lookup name */
915 for (i = 0; i < ks->asym_key_count; i++) {
916 if (!strcmp(referenced_key_name, ks->asym_keys[i].name)) {
917 break;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500918 }
919 }
roman3f9b65c2023-06-05 14:26:58 +0200920 if (i == ks->asym_key_count) {
921 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_key_name);
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500922 return -1;
923 }
924
roman3f9b65c2023-06-05 14:26:58 +0200925 for (j = 0; j < ks->asym_keys[i].cert_count; j++) {
roman78df0fa2023-11-02 10:33:57 +0100926 if (!strcmp(referenced_cert_name, ks->asym_keys[i].certs[j].name)) {
roman3f9b65c2023-06-05 14:26:58 +0200927 break;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500928 }
929 }
roman3f9b65c2023-06-05 14:26:58 +0200930 if (j == ks->asym_keys[i].cert_count) {
931 ERR(NULL, "Keystore certificate entry \"%s\" associated with the key \"%s\" not found.",
932 referenced_cert_name, referenced_key_name);
933 return -1;
934 }
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500935
roman3f9b65c2023-06-05 14:26:58 +0200936 *privkey_data = ks->asym_keys[i].privkey_data;
937 *privkey_type = ks->asym_keys[i].privkey_type;
938 *cert_data = ks->asym_keys[i].certs[j].data;
939 return 0;
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500940}
941
Michal Vasko4c1fb492017-01-30 14:31:07 +0100942static int
roman3f9b65c2023-06-05 14:26:58 +0200943nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, struct nc_server_tls_opts *opts)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100944{
roman3f9b65c2023-06-05 14:26:58 +0200945 char *privkey_data = NULL, *cert_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200946 int ret = 0;
roman3f9b65c2023-06-05 14:26:58 +0200947 NC_PRIVKEY_FORMAT privkey_type;
Michal Vasko6e08cb72017-02-02 11:15:29 +0100948 X509 *cert = NULL;
949 EVP_PKEY *pkey = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100950
roman3f9b65c2023-06-05 14:26:58 +0200951 NC_CHECK_ARG_RET(NULL, tls_ctx, opts, -1);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100952
roman3f9b65c2023-06-05 14:26:58 +0200953 /* get data needed for setting the server cert */
954 if (opts->store == NC_STORE_LOCAL) {
955 /* local definition */
956 cert_data = opts->cert_data;
957 privkey_data = opts->privkey_data;
958 privkey_type = opts->privkey_type;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100959 } else {
roman3f9b65c2023-06-05 14:26:58 +0200960 /* keystore */
961 ret = nc_server_tls_ks_ref_get_cert_key(opts->key_ref, opts->cert_ref, &privkey_data, &privkey_type, &cert_data);
962 if (ret) {
963 ERR(NULL, "Getting server certificate from the keystore reference \"%s\" failed.", opts->key_ref);
964 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100965 }
966 }
967
roman3f9b65c2023-06-05 14:26:58 +0200968 /* load the cert */
969 cert = base64der_to_cert(cert_data);
970 if (!cert) {
971 ERR(NULL, "Converting certificate data to certificate format failed.");
972 ret = -1;
973 goto cleanup;
974 }
975
976 /* set server cert */
977 ret = SSL_CTX_use_certificate(tls_ctx, cert);
978 if (ret != 1) {
979 ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
980 ret = -1;
981 goto cleanup;
982 }
983
Michal Vasko4c1fb492017-01-30 14:31:07 +0100984 /* load the private key */
roman3f9b65c2023-06-05 14:26:58 +0200985 pkey = base64der_to_privatekey(privkey_data, nc_privkey_format_to_str(privkey_type));
986 if (!pkey) {
987 ERR(NULL, "Converting private key data to private key format failed.");
988 ret = -1;
989 goto cleanup;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100990 }
991
roman3f9b65c2023-06-05 14:26:58 +0200992 /* set server key */
993 ret = SSL_CTX_use_PrivateKey(tls_ctx, pkey);
994 if (ret != 1) {
995 ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error()));
996 ret = -1;
997 goto cleanup;
998 }
999
1000 ret = 0;
Andrew Langefeld440b6c72018-08-27 16:26:20 -05001001
Michal Vasko4c1fb492017-01-30 14:31:07 +01001002cleanup:
Michal Vasko6e08cb72017-02-02 11:15:29 +01001003 X509_free(cert);
1004 EVP_PKEY_free(pkey);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001005 return ret;
1006}
1007
roman3f9b65c2023-06-05 14:26:58 +02001008static int
1009tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_data)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001010{
roman3f9b65c2023-06-05 14:26:58 +02001011 X509 *cert;
1012
1013 cert = base64der_to_cert(cert_data);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001014
Michal Vasko4c1fb492017-01-30 14:31:07 +01001015 if (!cert) {
roman3f9b65c2023-06-05 14:26:58 +02001016 ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data,
1017 ERR_reason_error_string(ERR_get_error()));
1018 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001019 }
1020
1021 /* add the trusted certificate */
1022 if (X509_STORE_add_cert(cert_store, cert) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +02001023 ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001024 X509_free(cert);
roman3f9b65c2023-06-05 14:26:58 +02001025 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001026 }
1027 X509_free(cert);
roman3f9b65c2023-06-05 14:26:58 +02001028
1029 return 0;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001030}
1031
1032static int
roman3f9b65c2023-06-05 14:26:58 +02001033nc_tls_store_set_trusted_certs(X509_STORE *cert_store, struct nc_cert_grouping *ca_certs)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001034{
1035 uint16_t i;
roman3f9b65c2023-06-05 14:26:58 +02001036 struct nc_certificate *certs;
1037 uint16_t cert_count;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001038
roman3f9b65c2023-06-05 14:26:58 +02001039 if (ca_certs->store == NC_STORE_LOCAL) {
1040 /* local definition */
1041 certs = ca_certs->certs;
1042 cert_count = ca_certs->cert_count;
1043 } else {
1044 /* truststore */
1045 if (nc_server_tls_ts_ref_get_certs(ca_certs->ts_ref, &certs, &cert_count)) {
1046 ERR(NULL, "Error getting certificate-authority certificates from the truststore reference \"%s\".", ca_certs->ts_ref);
Michal Vasko4c1fb492017-01-30 14:31:07 +01001047 return -1;
1048 }
roman3f9b65c2023-06-05 14:26:58 +02001049 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001050
roman3f9b65c2023-06-05 14:26:58 +02001051 for (i = 0; i < cert_count; i++) {
1052 if (tls_store_add_trusted_cert(cert_store, certs[i].data)) {
1053 return -1;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001054 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001055 }
1056
1057 return 0;
1058}
1059
Michal Vasko9af52322022-07-25 14:39:30 +02001060static int
roman3f9b65c2023-06-05 14:26:58 +02001061nc_server_tls_crl_path(struct nc_session *session, const char *crl_path, X509_STORE *store)
1062{
1063 int ret = 0;
1064 X509_CRL *crl = NULL;
1065 FILE *f;
1066
1067 f = fopen(crl_path, "r");
1068 if (!f) {
1069 ERR(session, "Unable to open CRL file \"%s\".", crl_path);
1070 return -1;
1071 }
1072
1073 /* try DER first */
1074 crl = d2i_X509_CRL_fp(f, NULL);
1075 if (crl) {
1076 /* success */
1077 goto ok;
1078 }
1079
1080 /* DER failed, try PEM */
1081 rewind(f);
1082 crl = PEM_read_X509_CRL(f, NULL, NULL, NULL);
1083 if (!crl) {
1084 ERR(session, "Reading CRL from file \"%s\" failed.", crl_path);
1085 ret = -1;
1086 goto cleanup;
1087 }
1088
1089ok:
1090 ret = X509_STORE_add_crl(store, crl);
1091 if (!ret) {
1092 ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error()));
1093 ret = -1;
1094 goto cleanup;
1095 }
1096 /* ok */
1097 ret = 0;
1098
1099cleanup:
1100 fclose(f);
1101 X509_CRL_free(crl);
1102 return ret;
1103}
1104
1105static size_t
1106nc_server_tls_curl_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
1107{
1108 struct nc_curl_data *data;
1109
1110 size = nmemb;
1111
1112 data = (struct nc_curl_data *)userdata;
1113
1114 data->data = nc_realloc(data->data, data->size + size);
roman3a95bb22023-10-26 11:07:17 +02001115 NC_CHECK_ERRMEM_RET(!data->data, 0);
roman3f9b65c2023-06-05 14:26:58 +02001116
1117 memcpy(&data->data[data->size], ptr, size);
1118 data->size += size;
1119
1120 return size;
1121}
1122
1123static int
1124nc_server_tls_curl_init(struct nc_session *session, CURL **handle, struct nc_curl_data *data)
1125{
1126 NC_CHECK_ARG_RET(session, handle, data, -1);
1127
1128 *handle = NULL;
1129
1130 *handle = curl_easy_init();
1131 if (!*handle) {
1132 ERR(session, "Initializing CURL failed.");
1133 return -1;
1134 }
1135
1136 if (curl_easy_setopt(*handle, CURLOPT_WRITEFUNCTION, nc_server_tls_curl_cb)) {
1137 ERR(session, "Setting curl callback failed.");
1138 return -1;
1139 }
1140
1141 if (curl_easy_setopt(*handle, CURLOPT_WRITEDATA, data)) {
1142 ERR(session, "Setting curl callback data failed.");
1143 return -1;
1144 }
1145
1146 return 0;
1147}
1148
1149static int
1150nc_server_tls_curl_fetch(struct nc_session *session, CURL *handle, const char *url)
1151{
1152 char err_buf[CURL_ERROR_SIZE];
1153
1154 /* set uri */
1155 if (curl_easy_setopt(handle, CURLOPT_URL, url)) {
1156 ERR(session, "Setting URI \"%s\" to download CRL from failed.", url);
1157 return -1;
1158 }
1159
1160 /* set err buf */
1161 curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, err_buf);
1162
1163 /* download */
1164 if (curl_easy_perform(handle)) {
1165 ERR(session, "Downloading CRL from \"%s\" failed (%s).", url, err_buf);
1166 return -1;
1167 }
1168
1169 return 0;
1170}
1171
1172static int
1173nc_server_tls_add_crl_to_store(struct nc_session *session, struct nc_curl_data *downloaded, X509_STORE *store)
1174{
1175 int ret = 0;
1176 X509_CRL *crl = NULL;
1177 BIO *bio = NULL;
1178
1179 /* try DER first */
1180 crl = d2i_X509_CRL(NULL, (const unsigned char **) &downloaded->data, downloaded->size);
1181 if (crl) {
1182 /* it was DER */
1183 goto ok;
1184 }
1185
1186 /* DER failed, try PEM next */
1187 bio = BIO_new_mem_buf(downloaded->data, downloaded->size);
1188 if (!bio) {
1189 ERR(session, "Creating new bio failed (%s).", ERR_reason_error_string(ERR_get_error()));
1190 ret = -1;
1191 goto cleanup;
1192 }
1193
1194 /* try to parse PEM from the downloaded data */
1195 crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
1196 if (!crl) {
1197 ERR(session, "Reading downloaded CRL failed (%s).", ERR_reason_error_string(ERR_get_error()));
1198 ret = -1;
1199 goto cleanup;
1200 }
1201
1202ok:
1203 /* we obtained the CRL, now add it to the CRL store */
1204 ret = X509_STORE_add_crl(store, crl);
1205 if (!ret) {
1206 ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error()));
1207 ret = -1;
1208 goto cleanup;
1209 }
1210 /* ok */
1211 ret = 0;
1212
1213cleanup:
1214 X509_CRL_free(crl);
1215 BIO_free(bio);
1216 return ret;
1217}
1218
1219static int
1220nc_server_tls_crl_url(struct nc_session *session, const char *url, X509_STORE *store)
1221{
1222 int ret = 0;
1223 CURL *handle = NULL;
1224 struct nc_curl_data downloaded = {0};
1225
1226 /* init curl */
1227 ret = nc_server_tls_curl_init(session, &handle, &downloaded);
1228 if (ret) {
1229 goto cleanup;
1230 }
1231
1232 VRB(session, "Downloading CRL from \"%s\".", url);
1233
1234 /* download the CRL */
1235 ret = nc_server_tls_curl_fetch(session, handle, url);
1236 if (ret) {
1237 goto cleanup;
1238 }
1239
1240 /* convert the downloaded data to CRL and add it to the store */
1241 ret = nc_server_tls_add_crl_to_store(session, &downloaded, store);
1242 if (ret) {
1243 goto cleanup;
1244 }
1245
1246cleanup:
1247 curl_easy_cleanup(handle);
1248 return ret;
1249}
1250
1251static int
1252nc_server_tls_crl_cert_ext(struct nc_session *session, X509_STORE *cert_store, X509_STORE *crl_store)
1253{
1254 int ret = 0, i, j, k, gtype;
1255 CURL *handle = NULL;
1256 struct nc_curl_data downloaded = {0};
1257
1258 STACK_OF(X509_OBJECT) * objs;
1259 X509_OBJECT *obj;
1260 X509 *cert;
1261
1262 STACK_OF(DIST_POINT) * dist_points;
1263 DIST_POINT *dist_point;
1264 GENERAL_NAMES *general_names;
1265 GENERAL_NAME *general_name;
1266 ASN1_STRING *asn_string_uri;
1267 const char *crl_distpoint_uri;
1268
1269 /* init curl */
1270 ret = nc_server_tls_curl_init(session, &handle, &downloaded);
1271 if (ret) {
1272 goto cleanup;
1273 }
1274
1275 /* treat all entries in the cert_store as X509_OBJECTs */
1276 objs = X509_STORE_get0_objects(cert_store);
1277 if (!objs) {
1278 ERR(session, "Getting certificates from store failed (%s).", ERR_reason_error_string(ERR_get_error()));
1279 ret = -1;
1280 goto cleanup;
1281 }
1282
1283 /* iterate over all the CAs */
1284 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1285 obj = sk_X509_OBJECT_value(objs, i);
1286 cert = X509_OBJECT_get0_X509(obj);
1287 if (!cert) {
1288 /* the object on this index was not a certificate */
1289 continue;
1290 }
1291
1292 /* get all the distribution points for this CA */
1293 dist_points = X509_get_ext_d2i(cert, NID_crl_distribution_points, NULL, NULL);
1294
1295 /* iterate over all the dist points (there can be multiple for a single cert) */
1296 for (j = 0; j < sk_DIST_POINT_num(dist_points); j++) {
1297 dist_point = sk_DIST_POINT_value(dist_points, j);
1298 if (!dist_point) {
1299 continue;
1300 }
1301 general_names = dist_point->distpoint->name.fullname;
1302
1303 /* iterate over all the GeneralesNames in the distribution point */
1304 for (k = 0; k < sk_GENERAL_NAME_num(general_names); k++) {
1305 general_name = sk_GENERAL_NAME_value(general_names, k);
1306 asn_string_uri = GENERAL_NAME_get0_value(general_name, &gtype);
1307
1308 /* check if the general name is a URI and has a valid length */
1309 if ((gtype != GEN_URI) || (ASN1_STRING_length(asn_string_uri) <= 6)) {
1310 continue;
1311 }
1312
1313 crl_distpoint_uri = (const char *) ASN1_STRING_get0_data(asn_string_uri);
1314
1315 VRB(session, "Downloading CRL from \"%s\".", crl_distpoint_uri);
1316
1317 /* download the CRL */
1318 ret = nc_server_tls_curl_fetch(session, handle, crl_distpoint_uri);
1319 if (ret) {
1320 /* failed to download the CRL from this entry, try th next */
1321 continue;
1322 }
1323
1324 /* convert the downloaded data to CRL and add it to the store */
1325 ret = nc_server_tls_add_crl_to_store(session, &downloaded, crl_store);
1326 if (ret) {
1327 goto cleanup;
1328 }
1329
1330 /* the CRL was downloaded, no need to download it again using different protocol */
1331 break;
1332 }
1333 }
1334 }
1335
1336cleanup:
1337 curl_easy_cleanup(handle);
1338 return ret;
1339}
1340
1341static int
1342nc_tls_store_set_crl(struct nc_session *session, struct nc_server_tls_opts *opts, X509_STORE *store)
1343{
1344 if (!opts->crl_store) {
1345 /* first call on this endpoint */
1346 opts->crl_store = X509_STORE_new();
roman124a4362023-10-26 15:36:22 +02001347 NC_CHECK_ERRMEM_GOTO(!opts->crl_store, , fail);
roman3f9b65c2023-06-05 14:26:58 +02001348 }
1349
1350 if (opts->crl_path) {
1351 if (nc_server_tls_crl_path(session, opts->crl_path, opts->crl_store)) {
1352 goto fail;
1353 }
1354 } else if (opts->crl_url) {
1355 if (nc_server_tls_crl_url(session, opts->crl_url, opts->crl_store)) {
1356 goto fail;
1357 }
1358 } else {
1359 if (nc_server_tls_crl_cert_ext(session, store, opts->crl_store)) {
1360 goto fail;
1361 }
1362 }
1363
1364 return 0;
1365
1366fail:
1367 return -1;
1368}
1369
1370static int
Michal Vasko9af52322022-07-25 14:39:30 +02001371nc_server_tls_accept_check(int accept_ret, struct nc_session *session)
1372{
1373 int verify;
1374
1375 /* check certificate verification result */
1376 verify = SSL_get_verify_result(session->ti.tls);
1377 switch (verify) {
1378 case X509_V_OK:
1379 if (accept_ret == 1) {
1380 VRB(session, "Client certificate verified.");
1381 }
1382 break;
1383 default:
1384 ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify));
1385 }
1386
1387 if (accept_ret != 1) {
1388 switch (SSL_get_error(session->ti.tls, accept_ret)) {
1389 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001390 ERR(session, "SSL accept failed (%s).", strerror(errno));
Michal Vasko9af52322022-07-25 14:39:30 +02001391 break;
1392 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001393 ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +02001394 break;
1395 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +02001396 ERR(session, "SSL accept failed.");
Michal Vasko9af52322022-07-25 14:39:30 +02001397 break;
1398 }
1399 }
1400
1401 return accept_ret;
1402}
1403
Michal Vasko3031aae2016-01-27 16:07:18 +01001404int
roman3f9b65c2023-06-05 14:26:58 +02001405nc_accept_tls_session(struct nc_session *session, struct nc_server_tls_opts *opts, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001406{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001407 X509_STORE *cert_store;
1408 SSL_CTX *tls_ctx;
Michal Vasko36c7be82017-02-22 13:37:59 +01001409 int ret;
roman6ece9c52022-06-22 09:29:17 +02001410 struct timespec ts_timeout;
roman78df0fa2023-11-02 10:33:57 +01001411 struct nc_endpt *referenced_endpt;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001412
Michal Vaskoe2713da2016-08-22 16:06:40 +02001413 /* SSL_CTX */
Michal Vasko18aeb5d2017-02-17 09:23:56 +01001414 tls_ctx = SSL_CTX_new(TLS_server_method());
roman3f9b65c2023-06-05 14:26:58 +02001415
Michal Vaskoe2713da2016-08-22 16:06:40 +02001416 if (!tls_ctx) {
Michal Vasko05532772021-06-03 12:12:38 +02001417 ERR(session, "Failed to create TLS context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001418 goto error;
1419 }
roman3f9b65c2023-06-05 14:26:58 +02001420
Michal Vaskoe2713da2016-08-22 16:06:40 +02001421 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 +02001422 if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001423 goto error;
1424 }
1425
1426 /* X509_STORE, managed (freed) with the context */
1427 cert_store = X509_STORE_new();
roman3f9b65c2023-06-05 14:26:58 +02001428 if (!cert_store) {
1429 ERR(session, "Creating certificate store failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001430 goto error;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001431 }
1432
roman78df0fa2023-11-02 10:33:57 +01001433 /* store the session, retrieve it when needed */
1434 ret = X509_STORE_set_ex_data(cert_store, 0, session);
1435 if (!ret) {
1436 ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error()));
1437 goto error;
1438 }
1439
roman3f9b65c2023-06-05 14:26:58 +02001440 /* set end-entity certs as cert store data, retrieve them if verification fails later */
1441 ret = X509_STORE_set_ex_data(cert_store, 1, &opts->ee_certs);
1442 if (!ret) {
1443 ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error()));
1444 goto error;
1445 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001446
roman3f9b65c2023-06-05 14:26:58 +02001447 /* do the same for referenced endpoint's end entity certs */
roman78df0fa2023-11-02 10:33:57 +01001448 if (opts->referenced_endpt_name) {
1449 if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) {
1450 ERRINT;
1451 goto error;
1452 }
1453
1454 ret = X509_STORE_set_ex_data(cert_store, 2, &referenced_endpt->opts.tls->ee_certs);
roman3f9b65c2023-06-05 14:26:58 +02001455 if (!ret) {
1456 ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001457 goto error;
1458 }
1459 }
1460
roman3f9b65c2023-06-05 14:26:58 +02001461 /* set store to the context */
1462 SSL_CTX_set_cert_store(tls_ctx, cert_store);
1463
1464 /* set certificate authority certs */
1465 if (nc_tls_store_set_trusted_certs(cert_store, &opts->ca_certs)) {
1466 goto error;
1467 }
1468
1469 /* set referenced endpoint's CA certs if set */
roman78df0fa2023-11-02 10:33:57 +01001470 if (opts->referenced_endpt_name) {
1471 if (nc_tls_store_set_trusted_certs(cert_store, &referenced_endpt->opts.tls->ca_certs)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001472 goto error;
1473 }
roman3f9b65c2023-06-05 14:26:58 +02001474 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001475
roman3f9b65c2023-06-05 14:26:58 +02001476 /* set Certificate Revocation List if configured */
1477 if (opts->crl_path || opts->crl_url || opts->crl_cert_ext) {
1478 if (nc_tls_store_set_crl(session, opts, cert_store)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001479 goto error;
1480 }
1481 }
1482
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001483 session->ti_type = NC_TI_OPENSSL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001484 session->ti.tls = SSL_new(tls_ctx);
1485
Michal Vasko4c1fb492017-01-30 14:31:07 +01001486 /* context can be freed already, trusted certs must be freed manually */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001487 SSL_CTX_free(tls_ctx);
1488 tls_ctx = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001489
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001490 if (!session->ti.tls) {
Michal Vasko05532772021-06-03 12:12:38 +02001491 ERR(session, "Failed to create TLS structure from context.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001492 goto error;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001493 }
1494
roman3f9b65c2023-06-05 14:26:58 +02001495 /* set TLS versions for the current SSL session */
1496 if (opts->tls_versions) {
1497 if (!(opts->tls_versions & NC_TLS_VERSION_10)) {
1498 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1);
1499 }
1500 if (!(opts->tls_versions & NC_TLS_VERSION_11)) {
1501 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_1);
1502 }
1503 if (!(opts->tls_versions & NC_TLS_VERSION_12)) {
1504 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_2);
1505 }
1506 if (!(opts->tls_versions & NC_TLS_VERSION_13)) {
1507 SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_3);
1508 }
1509 }
1510
1511 /* set TLS cipher suites */
1512 if (opts->ciphers) {
1513 /* set for TLS1.2 and lower */
1514 SSL_set_cipher_list(session->ti.tls, opts->ciphers);
1515 /* set for TLS1.3 */
1516 SSL_set_ciphersuites(session->ti.tls, opts->ciphers);
1517 }
1518
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001519 SSL_set_fd(session->ti.tls, sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001520 sock = -1;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001521 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
1522
Michal Vasko36c7be82017-02-22 13:37:59 +01001523 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001524 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001525 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001526 while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
1527 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001528 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +02001529 ERR(session, "SSL accept timeout.");
roman6ece9c52022-06-22 09:29:17 +02001530 return 0;
Michal Vasko0190bc32016-03-02 15:47:49 +01001531 }
1532 }
Michal Vasko9af52322022-07-25 14:39:30 +02001533 if (nc_server_tls_accept_check(ret, session) != 1) {
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001534 return -1;
1535 }
1536
Michal Vasko1a38c862016-01-15 15:50:07 +01001537 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001538
1539error:
1540 if (sock > -1) {
1541 close(sock);
1542 }
1543 SSL_CTX_free(tls_ctx);
1544 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001545}