Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_server_tls.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 TLS server session manipulation functions |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 18 | #include <poll.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 19 | #include <string.h> |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 20 | #include <unistd.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 21 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 22 | #include <openssl/err.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 23 | #include <openssl/evp.h> |
| 24 | #include <openssl/ssl.h> |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 25 | #include <openssl/x509.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 26 | #include <openssl/x509v3.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 27 | |
Michal Vasko | 316c963 | 2020-04-15 11:06:57 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 29 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 30 | #include "session_server.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 31 | #include "session_server_ch.h" |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 32 | |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 33 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 34 | #define X509_STORE_CTX_get_by_subject X509_STORE_get_by_subject |
| 35 | #endif |
| 36 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 37 | struct nc_server_tls_opts tls_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 38 | pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 39 | extern struct nc_server_opts server_opts; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 40 | |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 41 | static pthread_key_t verify_key; |
| 42 | static pthread_once_t verify_once = PTHREAD_ONCE_INIT; |
| 43 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 44 | static char * |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 45 | asn1time_to_str(const ASN1_TIME *t) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 46 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 47 | char *cp; |
| 48 | BIO *bio; |
| 49 | int n; |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 50 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 51 | if (!t) { |
| 52 | return NULL; |
| 53 | } |
| 54 | bio = BIO_new(BIO_s_mem()); |
| 55 | if (!bio) { |
| 56 | return NULL; |
| 57 | } |
| 58 | ASN1_TIME_print(bio, t); |
| 59 | n = BIO_pending(bio); |
| 60 | cp = malloc(n + 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 61 | if (!cp) { |
| 62 | ERRMEM; |
| 63 | BIO_free(bio); |
| 64 | return NULL; |
| 65 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 66 | n = BIO_read(bio, cp, n); |
| 67 | if (n < 0) { |
| 68 | BIO_free(bio); |
| 69 | free(cp); |
| 70 | return NULL; |
| 71 | } |
| 72 | cp[n] = '\0'; |
| 73 | BIO_free(bio); |
| 74 | return cp; |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str) |
| 79 | { |
| 80 | unsigned int i; |
| 81 | |
| 82 | *str = malloc(dig_len * 3); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 83 | if (!*str) { |
| 84 | ERRMEM; |
| 85 | return; |
| 86 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 87 | for (i = 0; i < dig_len - 1; ++i) { |
| 88 | sprintf((*str) + (i * 3), "%02x:", digest[i]); |
| 89 | } |
| 90 | sprintf((*str) + (i * 3), "%02x", digest[i]); |
| 91 | } |
| 92 | |
| 93 | /* return NULL - SSL error can be retrieved */ |
| 94 | static X509 * |
| 95 | base64der_to_cert(const char *in) |
| 96 | { |
| 97 | X509 *out; |
| 98 | char *buf; |
| 99 | BIO *bio; |
| 100 | |
| 101 | if (in == NULL) { |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) { |
| 106 | return NULL; |
| 107 | } |
| 108 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 109 | if (!bio) { |
| 110 | free(buf); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | out = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 115 | if (!out) { |
| 116 | free(buf); |
| 117 | BIO_free(bio); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | free(buf); |
| 122 | BIO_free(bio); |
| 123 | return out; |
| 124 | } |
| 125 | |
| 126 | /* return NULL - either errno or SSL error */ |
| 127 | static X509 * |
| 128 | pem_to_cert(const char *path) |
| 129 | { |
| 130 | FILE *fp; |
| 131 | X509 *out; |
| 132 | |
| 133 | fp = fopen(path, "r"); |
| 134 | if (!fp) { |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | out = PEM_read_X509(fp, NULL, NULL, NULL); |
| 139 | fclose(fp); |
| 140 | return out; |
| 141 | } |
| 142 | |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 143 | static EVP_PKEY * |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 144 | base64der_to_privatekey(const char *in, const char *key_str) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 145 | { |
| 146 | EVP_PKEY *out; |
| 147 | char *buf; |
| 148 | BIO *bio; |
| 149 | |
| 150 | if (in == NULL) { |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 154 | if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", key_str, " PRIVATE KEY-----\n", in, "\n-----END ", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 155 | key_str, " PRIVATE KEY-----") == -1) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
| 158 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 159 | if (!bio) { |
| 160 | free(buf); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 165 | if (!out) { |
| 166 | free(buf); |
| 167 | BIO_free(bio); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | free(buf); |
| 172 | BIO_free(bio); |
| 173 | return out; |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 174 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 175 | |
| 176 | static int |
| 177 | cert_pubkey_match(X509 *cert1, X509 *cert2) |
| 178 | { |
| 179 | ASN1_BIT_STRING *bitstr1, *bitstr2; |
| 180 | |
| 181 | bitstr1 = X509_get0_pubkey_bitstr(cert1); |
| 182 | bitstr2 = X509_get0_pubkey_bitstr(cert2); |
| 183 | |
| 184 | if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) || |
| 185 | memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) { |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username) |
| 194 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 195 | STACK_OF(GENERAL_NAME) * san_names; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 196 | GENERAL_NAME *san_name; |
| 197 | ASN1_OCTET_STRING *ip; |
| 198 | int i, san_count; |
| 199 | char *subject, *common_name; |
| 200 | |
Michal Vasko | d9a7864 | 2022-02-24 16:25:21 +0100 | [diff] [blame] | 201 | *username = NULL; |
| 202 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 203 | if (map_type == NC_TLS_CTN_COMMON_NAME) { |
| 204 | subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0); |
| 205 | common_name = strstr(subject, "CN="); |
| 206 | if (!common_name) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 207 | WRN(NULL, "Certificate does not include the commonName field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 208 | free(subject); |
| 209 | return 1; |
| 210 | } |
| 211 | common_name += 3; |
| 212 | if (strchr(common_name, '/')) { |
| 213 | *strchr(common_name, '/') = '\0'; |
| 214 | } |
| 215 | *username = strdup(common_name); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 216 | if (!*username) { |
| 217 | ERRMEM; |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 218 | return -1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 219 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 220 | free(subject); |
| 221 | } else { |
| 222 | /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */ |
| 223 | san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL); |
| 224 | if (!san_names) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 225 | WRN(NULL, "Certificate has no SANs or failed to retrieve them."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | san_count = sk_GENERAL_NAME_num(san_names); |
| 230 | for (i = 0; i < san_count; ++i) { |
| 231 | san_name = sk_GENERAL_NAME_value(san_names, i); |
| 232 | |
| 233 | /* rfc822Name (email) */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 234 | if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) && |
| 235 | (san_name->type == GEN_EMAIL)) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 236 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 237 | *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name)); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 238 | #else |
| 239 | *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name)); |
| 240 | #endif |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 241 | if (!*username) { |
| 242 | ERRMEM; |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 243 | return -1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 244 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
| 248 | /* dNSName */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 249 | if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) && |
| 250 | (san_name->type == GEN_DNS)) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 251 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 252 | *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName)); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 253 | #else |
| 254 | *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName)); |
| 255 | #endif |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 256 | if (!*username) { |
| 257 | ERRMEM; |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 258 | return -1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 259 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | |
| 263 | /* iPAddress */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 264 | if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_IP_ADDRESS)) && |
| 265 | (san_name->type == GEN_IPADD)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 266 | ip = san_name->d.iPAddress; |
| 267 | if (ip->length == 4) { |
| 268 | if (asprintf(username, "%d.%d.%d.%d", ip->data[0], ip->data[1], ip->data[2], ip->data[3]) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 269 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 270 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 271 | return -1; |
| 272 | } |
| 273 | break; |
| 274 | } else if (ip->length == 16) { |
| 275 | if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
| 276 | ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5], |
| 277 | ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11], |
| 278 | ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 279 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 280 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 281 | return -1; |
| 282 | } |
| 283 | break; |
| 284 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 285 | WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 290 | |
Vitaly Kuzin | a3b6632 | 2021-08-26 12:33:43 +0300 | [diff] [blame] | 291 | if (i == san_count) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 292 | switch (map_type) { |
| 293 | case NC_TLS_CTN_SAN_RFC822_NAME: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 294 | WRN(NULL, "Certificate does not include the SAN rfc822Name field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 295 | break; |
| 296 | case NC_TLS_CTN_SAN_DNS_NAME: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 297 | WRN(NULL, "Certificate does not include the SAN dNSName field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 298 | break; |
| 299 | case NC_TLS_CTN_SAN_IP_ADDRESS: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 300 | WRN(NULL, "Certificate does not include the SAN iPAddress field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 301 | break; |
| 302 | case NC_TLS_CTN_SAN_ANY: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 303 | WRN(NULL, "Certificate does not include any relevant SAN fields."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 304 | break; |
| 305 | default: |
| 306 | break; |
| 307 | } |
| 308 | return 1; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* return: 0 - OK, 1 - no match, -1 - error */ |
| 316 | static int |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 317 | nc_tls_cert_to_name(struct nc_session *session, struct nc_ctn *ctn_first, X509 *cert) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 318 | { |
| 319 | char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL; |
| 320 | char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 321 | unsigned char *buf = malloc(64); |
| 322 | unsigned int buf_len = 64; |
| 323 | int ret = 0; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 324 | struct nc_ctn *ctn; |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 325 | NC_TLS_CTN_MAPTYPE map_type; |
| 326 | char *username = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 327 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 328 | if (!buf) { |
| 329 | ERRMEM; |
| 330 | return -1; |
| 331 | } |
| 332 | |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 333 | if (!session || !ctn_first || !cert) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 334 | free(buf); |
| 335 | return -1; |
| 336 | } |
| 337 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 338 | for (ctn = ctn_first; ctn; ctn = ctn->next) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 339 | /* reset map_type */ |
| 340 | map_type = NC_TLS_CTN_UNKNOWN; |
| 341 | |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 342 | /* first make sure the entry is valid */ |
| 343 | if (!ctn->fingerprint || !ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 344 | VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id); |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 345 | continue; |
| 346 | } |
| 347 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 348 | /* MD5 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 349 | if (!strncmp(ctn->fingerprint, "01", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 350 | if (!digest_md5) { |
| 351 | if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 352 | ERR(NULL, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 353 | ret = -1; |
| 354 | goto cleanup; |
| 355 | } |
| 356 | digest_to_str(buf, buf_len, &digest_md5); |
| 357 | } |
| 358 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 359 | if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 360 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 361 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 362 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 365 | /* SHA-1 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 366 | } else if (!strncmp(ctn->fingerprint, "02", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 367 | if (!digest_sha1) { |
| 368 | if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 369 | ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 370 | ret = -1; |
| 371 | goto cleanup; |
| 372 | } |
| 373 | digest_to_str(buf, buf_len, &digest_sha1); |
| 374 | } |
| 375 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 376 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 377 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 378 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 379 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 382 | /* SHA-224 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 383 | } else if (!strncmp(ctn->fingerprint, "03", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 384 | if (!digest_sha224) { |
| 385 | if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 386 | ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 387 | ret = -1; |
| 388 | goto cleanup; |
| 389 | } |
| 390 | digest_to_str(buf, buf_len, &digest_sha224); |
| 391 | } |
| 392 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 393 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 394 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 395 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 396 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 399 | /* SHA-256 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 400 | } else if (!strncmp(ctn->fingerprint, "04", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 401 | if (!digest_sha256) { |
| 402 | if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 403 | ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 404 | ret = -1; |
| 405 | goto cleanup; |
| 406 | } |
| 407 | digest_to_str(buf, buf_len, &digest_sha256); |
| 408 | } |
| 409 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 410 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 411 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 412 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 413 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 416 | /* SHA-384 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 417 | } else if (!strncmp(ctn->fingerprint, "05", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 418 | if (!digest_sha384) { |
| 419 | if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 420 | ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 421 | ret = -1; |
| 422 | goto cleanup; |
| 423 | } |
| 424 | digest_to_str(buf, buf_len, &digest_sha384); |
| 425 | } |
| 426 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 427 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 428 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 429 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 430 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 433 | /* SHA-512 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 434 | } else if (!strncmp(ctn->fingerprint, "06", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 435 | if (!digest_sha512) { |
| 436 | if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 437 | ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 438 | ret = -1; |
| 439 | goto cleanup; |
| 440 | } |
| 441 | digest_to_str(buf, buf_len, &digest_sha512); |
| 442 | } |
| 443 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 444 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 445 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 446 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 447 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 450 | /* unknown */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 451 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 452 | WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 453 | continue; |
| 454 | } |
| 455 | |
| 456 | if (map_type != NC_TLS_CTN_UNKNOWN) { |
| 457 | /* found a fingerprint match */ |
| 458 | if (map_type == NC_TLS_CTN_SPECIFIED) { |
| 459 | /* specified -> get username from the ctn entry */ |
| 460 | session->username = strdup(ctn->name); |
| 461 | if (!session->username) { |
| 462 | ERRMEM; |
| 463 | ret = -1; |
| 464 | goto cleanup; |
| 465 | } |
| 466 | } else { |
| 467 | /* try to get the username from the cert with this ctn's map type */ |
| 468 | ret = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &username); |
| 469 | if (ret == -1) { |
| 470 | /* fatal error */ |
| 471 | goto cleanup; |
| 472 | } else if (ret) { |
| 473 | /* didn't get username, try next ctn entry */ |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | /* success */ |
| 478 | session->username = username; |
| 479 | } |
| 480 | |
| 481 | /* matching fingerprint found and username obtained, success */ |
| 482 | ret = 0; |
| 483 | goto cleanup; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 487 | if (!ctn) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 488 | ret = 1; |
| 489 | } |
| 490 | |
| 491 | cleanup: |
| 492 | free(digest_md5); |
| 493 | free(digest_sha1); |
| 494 | free(digest_sha224); |
| 495 | free(digest_sha256); |
| 496 | free(digest_sha384); |
| 497 | free(digest_sha512); |
| 498 | free(buf); |
| 499 | return ret; |
| 500 | } |
| 501 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 502 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
| 503 | |
| 504 | static int |
| 505 | nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 506 | { |
| 507 | X509_STORE_CTX *store_ctx; |
| 508 | X509_OBJECT *obj; |
| 509 | X509_NAME *subject; |
| 510 | X509_NAME *issuer; |
| 511 | X509 *cert; |
| 512 | X509_CRL *crl; |
| 513 | X509_REVOKED *revoked; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 514 | |
| 515 | STACK_OF(X509) * cert_stack; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 516 | EVP_PKEY *pubkey; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 517 | struct nc_session *session; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 518 | struct nc_server_tls_opts *opts; |
| 519 | const ASN1_INTEGER *serial; |
| 520 | int i, n, rc, depth; |
| 521 | char *cp; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 522 | const ASN1_TIME *last_update = NULL, *next_update = NULL; |
| 523 | |
| 524 | /* get the thread session */ |
| 525 | session = pthread_getspecific(verify_key); |
| 526 | if (!session) { |
| 527 | ERRINT; |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | opts = session->data; |
| 532 | |
| 533 | /* get the last certificate, that is the peer (client) certificate */ |
| 534 | if (!session->opts.server.client_cert) { |
| 535 | cert_stack = X509_STORE_CTX_get1_chain(x509_ctx); |
Andrew Langefeld | 62bc671 | 2018-08-28 16:17:16 -0500 | [diff] [blame] | 536 | session->opts.server.client_cert = sk_X509_value(cert_stack, 0); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 537 | X509_up_ref(session->opts.server.client_cert); |
| 538 | sk_X509_pop_free(cert_stack, X509_free); |
| 539 | } |
| 540 | |
| 541 | /* standard certificate verification failed, so a trusted client cert must match to continue */ |
| 542 | if (!preverify_ok) { |
| 543 | subject = X509_get_subject_name(session->opts.server.client_cert); |
| 544 | cert_stack = X509_STORE_CTX_get1_certs(x509_ctx, subject); |
| 545 | if (cert_stack) { |
| 546 | for (i = 0; i < sk_X509_num(cert_stack); ++i) { |
| 547 | if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) { |
| 548 | /* we are just overriding the failed standard certificate verification (preverify_ok == 0), |
| 549 | * this callback will be called again with the same current certificate and preverify_ok == 1 */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 550 | VRB(NULL, "Cert verify: fail (%s), but the client certificate is trusted, continuing.", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 551 | X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 552 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
| 553 | sk_X509_pop_free(cert_stack, X509_free); |
| 554 | return 1; |
| 555 | } |
| 556 | } |
| 557 | sk_X509_pop_free(cert_stack, X509_free); |
| 558 | } |
| 559 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 560 | ERR(NULL, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* print cert verify info */ |
| 565 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 566 | VRB(NULL, "Cert verify: depth %d.", depth); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 567 | |
| 568 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 569 | subject = X509_get_subject_name(cert); |
| 570 | issuer = X509_get_issuer_name(cert); |
| 571 | |
| 572 | cp = X509_NAME_oneline(subject, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 573 | VRB(NULL, "Cert verify: subject: %s.", cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 574 | OPENSSL_free(cp); |
| 575 | cp = X509_NAME_oneline(issuer, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 576 | VRB(NULL, "Cert verify: issuer: %s.", cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 577 | OPENSSL_free(cp); |
| 578 | |
| 579 | /* check for revocation if set */ |
| 580 | if (opts->crl_store) { |
| 581 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 582 | * the current certificate in order to verify it's integrity */ |
| 583 | store_ctx = X509_STORE_CTX_new(); |
| 584 | obj = X509_OBJECT_new(); |
| 585 | X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 586 | rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 587 | X509_STORE_CTX_free(store_ctx); |
| 588 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 589 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 590 | cp = X509_NAME_oneline(subject, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 591 | VRB(NULL, "Cert verify CRL: issuer: %s.", cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 592 | OPENSSL_free(cp); |
| 593 | |
| 594 | last_update = X509_CRL_get0_lastUpdate(crl); |
| 595 | next_update = X509_CRL_get0_nextUpdate(crl); |
| 596 | cp = asn1time_to_str(last_update); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 597 | VRB(NULL, "Cert verify CRL: last update: %s.", cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 598 | free(cp); |
| 599 | cp = asn1time_to_str(next_update); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 600 | VRB(NULL, "Cert verify CRL: next update: %s.", cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 601 | free(cp); |
| 602 | |
| 603 | /* verify the signature on this CRL */ |
| 604 | pubkey = X509_get_pubkey(cert); |
| 605 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 606 | ERR(NULL, "Cert verify CRL: invalid signature."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 607 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 608 | X509_OBJECT_free(obj); |
| 609 | if (pubkey) { |
| 610 | EVP_PKEY_free(pubkey); |
| 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | if (pubkey) { |
| 615 | EVP_PKEY_free(pubkey); |
| 616 | } |
| 617 | |
| 618 | /* check date of CRL to make sure it's not expired */ |
| 619 | if (!next_update) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 620 | ERR(NULL, "Cert verify CRL: invalid nextUpdate field."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 621 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 622 | X509_OBJECT_free(obj); |
| 623 | return 0; |
| 624 | } |
| 625 | if (X509_cmp_current_time(next_update) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 626 | ERR(NULL, "Cert verify CRL: expired - revoking all certificates."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 627 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 628 | X509_OBJECT_free(obj); |
| 629 | return 0; |
| 630 | } |
| 631 | X509_OBJECT_free(obj); |
| 632 | } |
| 633 | |
| 634 | /* try to retrieve a CRL corresponding to the _issuer_ of |
| 635 | * the current certificate in order to check for revocation */ |
| 636 | store_ctx = X509_STORE_CTX_new(); |
| 637 | obj = X509_OBJECT_new(); |
| 638 | X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 639 | rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 640 | X509_STORE_CTX_free(store_ctx); |
| 641 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 642 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 643 | /* check if the current certificate is revoked by this CRL */ |
| 644 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 645 | for (i = 0; i < n; i++) { |
| 646 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 647 | serial = X509_REVOKED_get0_serialNumber(revoked); |
| 648 | if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) { |
| 649 | cp = X509_NAME_oneline(issuer, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 650 | ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", |
| 651 | serial, serial, cp); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 652 | OPENSSL_free(cp); |
| 653 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 654 | X509_OBJECT_free(obj); |
| 655 | return 0; |
| 656 | } |
| 657 | } |
| 658 | X509_OBJECT_free(obj); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* cert-to-name already successful */ |
| 663 | if (session->username) { |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | /* cert-to-name */ |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 668 | rc = nc_tls_cert_to_name(session, opts->ctn, cert); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 669 | if (rc) { |
| 670 | if (rc == -1) { |
| 671 | /* fatal error */ |
| 672 | depth = 0; |
| 673 | } |
| 674 | /* rc == 1 is a normal CTN fail (no match found) */ |
| 675 | goto fail; |
| 676 | } |
| 677 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 678 | VRB(NULL, "Cert verify CTN: new client username recognized as \"%s\".", session->username); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 679 | |
| 680 | if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 681 | VRB(NULL, "Cert verify: user verify callback revoked authorization."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 682 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | return 1; |
| 687 | |
| 688 | fail: |
| 689 | if (depth > 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 690 | VRB(NULL, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 691 | return 1; |
| 692 | } |
| 693 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 694 | VRB(NULL, "Cert-to-name unsuccessful, dropping the new client."); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 695 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | #else |
| 700 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 701 | static int |
| 702 | nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 703 | { |
| 704 | X509_STORE_CTX store_ctx; |
| 705 | X509_OBJECT obj; |
| 706 | X509_NAME *subject; |
| 707 | X509_NAME *issuer; |
| 708 | X509 *cert; |
| 709 | X509_CRL *crl; |
| 710 | X509_REVOKED *revoked; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 711 | |
| 712 | STACK_OF(X509) * cert_stack; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 713 | EVP_PKEY *pubkey; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 714 | struct nc_session *session; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 715 | struct nc_server_tls_opts *opts; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 716 | long serial; |
| 717 | int i, n, rc, depth; |
| 718 | char *cp; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 719 | ASN1_TIME *last_update = NULL, *next_update = NULL; |
| 720 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 721 | /* get the thread session */ |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 722 | session = pthread_getspecific(verify_key); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 723 | if (!session) { |
| 724 | ERRINT; |
| 725 | return 0; |
| 726 | } |
| 727 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 728 | opts = session->data; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 729 | |
| 730 | /* get the last certificate, that is the peer (client) certificate */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 731 | if (!session->opts.server.client_cert) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 732 | cert_stack = X509_STORE_CTX_get1_chain(x509_ctx); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 733 | while ((cert = sk_X509_pop(cert_stack))) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 734 | X509_free(session->opts.server.client_cert); |
| 735 | session->opts.server.client_cert = cert; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 736 | } |
| 737 | sk_X509_pop_free(cert_stack, X509_free); |
| 738 | } |
| 739 | |
| 740 | /* standard certificate verification failed, so a trusted client cert must match to continue */ |
| 741 | if (!preverify_ok) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 742 | subject = X509_get_subject_name(session->opts.server.client_cert); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 743 | cert_stack = X509_STORE_get1_certs(x509_ctx, subject); |
| 744 | if (cert_stack) { |
| 745 | for (i = 0; i < sk_X509_num(cert_stack); ++i) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 746 | if (cert_pubkey_match(session->opts.server.client_cert, sk_X509_value(cert_stack, i))) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 747 | /* we are just overriding the failed standard certificate verification (preverify_ok == 0), |
| 748 | * this callback will be called again with the same current certificate and preverify_ok == 1 */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 749 | VRB(session, "Cert verify: fail (%s), but the client certificate is trusted, continuing.", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 750 | X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 751 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
| 752 | sk_X509_pop_free(cert_stack, X509_free); |
| 753 | return 1; |
| 754 | } |
| 755 | } |
| 756 | sk_X509_pop_free(cert_stack, X509_free); |
| 757 | } |
| 758 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 759 | ERR(session, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* print cert verify info */ |
| 764 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 765 | VRB(session, "Cert verify: depth %d.", depth); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 766 | |
| 767 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 768 | subject = X509_get_subject_name(cert); |
| 769 | issuer = X509_get_issuer_name(cert); |
| 770 | |
| 771 | cp = X509_NAME_oneline(subject, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 772 | VRB(session, "Cert verify: subject: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 773 | OPENSSL_free(cp); |
| 774 | cp = X509_NAME_oneline(issuer, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 775 | VRB(session, "Cert verify: issuer: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 776 | OPENSSL_free(cp); |
| 777 | |
| 778 | /* check for revocation if set */ |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 779 | if (opts->crl_store) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 780 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 781 | * the current certificate in order to verify it's integrity */ |
| 782 | memset((char *)&obj, 0, sizeof(obj)); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 783 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 784 | rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 785 | X509_STORE_CTX_cleanup(&store_ctx); |
| 786 | crl = obj.data.crl; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 787 | if ((rc > 0) && crl) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 788 | cp = X509_NAME_oneline(subject, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 789 | VRB(session, "Cert verify CRL: issuer: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 790 | OPENSSL_free(cp); |
| 791 | |
| 792 | last_update = X509_CRL_get_lastUpdate(crl); |
| 793 | next_update = X509_CRL_get_nextUpdate(crl); |
| 794 | cp = asn1time_to_str(last_update); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 795 | VRB(session, "Cert verify CRL: last update: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 796 | free(cp); |
| 797 | cp = asn1time_to_str(next_update); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 798 | VRB(session, "Cert verify CRL: next update: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 799 | free(cp); |
| 800 | |
| 801 | /* verify the signature on this CRL */ |
| 802 | pubkey = X509_get_pubkey(cert); |
| 803 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 804 | ERR(session, "Cert verify CRL: invalid signature."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 805 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 806 | X509_OBJECT_free_contents(&obj); |
| 807 | if (pubkey) { |
| 808 | EVP_PKEY_free(pubkey); |
| 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | if (pubkey) { |
| 813 | EVP_PKEY_free(pubkey); |
| 814 | } |
| 815 | |
| 816 | /* check date of CRL to make sure it's not expired */ |
| 817 | if (!next_update) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 818 | ERR(session, "Cert verify CRL: invalid nextUpdate field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 819 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 820 | X509_OBJECT_free_contents(&obj); |
| 821 | return 0; |
| 822 | } |
| 823 | if (X509_cmp_current_time(next_update) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 824 | ERR(session, "Cert verify CRL: expired - revoking all certificates."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 825 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 826 | X509_OBJECT_free_contents(&obj); |
| 827 | return 0; |
| 828 | } |
| 829 | X509_OBJECT_free_contents(&obj); |
| 830 | } |
| 831 | |
| 832 | /* try to retrieve a CRL corresponding to the _issuer_ of |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 833 | * the current certificate in order to check for revocation */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 834 | memset((char *)&obj, 0, sizeof(obj)); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 835 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 836 | rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 837 | X509_STORE_CTX_cleanup(&store_ctx); |
| 838 | crl = obj.data.crl; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 839 | if ((rc > 0) && crl) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 840 | /* check if the current certificate is revoked by this CRL */ |
| 841 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 842 | for (i = 0; i < n; i++) { |
| 843 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 844 | if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) { |
| 845 | serial = ASN1_INTEGER_get(revoked->serialNumber); |
| 846 | cp = X509_NAME_oneline(issuer, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 847 | ERR(session, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", |
| 848 | serial, serial, cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 849 | OPENSSL_free(cp); |
| 850 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 851 | X509_OBJECT_free_contents(&obj); |
| 852 | return 0; |
| 853 | } |
| 854 | } |
| 855 | X509_OBJECT_free_contents(&obj); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* cert-to-name already successful */ |
| 860 | if (session->username) { |
| 861 | return 1; |
| 862 | } |
| 863 | |
| 864 | /* cert-to-name */ |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 865 | rc = nc_tls_cert_to_name(session, opts->ctn, cert); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 866 | if (rc) { |
| 867 | if (rc == -1) { |
| 868 | /* fatal error */ |
| 869 | depth = 0; |
| 870 | } |
| 871 | /* rc == 1 is a normal CTN fail (no match found) */ |
| 872 | goto fail; |
| 873 | } |
| 874 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 875 | VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username); |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 876 | |
| 877 | if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 878 | VRB(session, "Cert verify: user verify callback revoked authorization."); |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 879 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 880 | return 0; |
| 881 | } |
| 882 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 883 | return 1; |
| 884 | |
| 885 | fail: |
| 886 | if (depth > 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 887 | VRB(session, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 888 | return 1; |
| 889 | } |
| 890 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 891 | VRB(session, "Cert-to-name unsuccessful, dropping the new client."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 892 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 893 | return 0; |
| 894 | } |
| 895 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 896 | #endif |
| 897 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 898 | static int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 899 | nc_server_tls_set_server_cert(const char *name, struct nc_server_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 900 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 901 | if (!name) { |
Michal Vasko | a874879 | 2016-11-22 14:34:26 +0100 | [diff] [blame] | 902 | if (opts->server_cert) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 903 | free(opts->server_cert); |
Michal Vasko | a874879 | 2016-11-22 14:34:26 +0100 | [diff] [blame] | 904 | } |
| 905 | opts->server_cert = NULL; |
| 906 | return 0; |
| 907 | } |
| 908 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 909 | if (opts->server_cert) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 910 | free(opts->server_cert); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 911 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 912 | opts->server_cert = strdup(name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 913 | |
| 914 | return 0; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | API int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 918 | nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 919 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 920 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 921 | struct nc_endpt *endpt; |
| 922 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 923 | if (!endpt_name) { |
| 924 | ERRARG("endpt_name"); |
| 925 | return -1; |
| 926 | } |
| 927 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 928 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 929 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 930 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 931 | return -1; |
| 932 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 933 | ret = nc_server_tls_set_server_cert(name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 934 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 935 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 936 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 937 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 941 | nc_server_tls_ch_client_endpt_set_server_cert(const char *client_name, const char *endpt_name, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 942 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 943 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 944 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 945 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 946 | |
| 947 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 948 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 949 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 950 | return -1; |
| 951 | } |
| 952 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 953 | ret = nc_server_tls_set_server_cert(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 954 | |
| 955 | /* UNLOCK */ |
| 956 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 957 | |
| 958 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 959 | } |
| 960 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 961 | API void |
| 962 | nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 963 | char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, |
| 964 | void (*free_user_data)(void *user_data)) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 965 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 966 | if (!cert_clb) { |
| 967 | ERRARG("cert_clb"); |
| 968 | return; |
Michal Vasko | a874879 | 2016-11-22 14:34:26 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 971 | server_opts.server_cert_clb = cert_clb; |
| 972 | server_opts.server_cert_data = user_data; |
| 973 | server_opts.server_cert_data_free = free_user_data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 974 | } |
| 975 | |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 976 | API void |
| 977 | nc_server_tls_set_server_cert_chain_clb(int (*cert_chain_clb)(const char *name, void *user_data, char ***cert_paths, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 978 | int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data)) |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 979 | { |
| 980 | if (!cert_chain_clb) { |
| 981 | ERRARG("cert_chain_clb"); |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | server_opts.server_cert_chain_clb = cert_chain_clb; |
| 986 | server_opts.server_cert_chain_data = user_data; |
| 987 | server_opts.server_cert_chain_data_free = free_user_data; |
| 988 | } |
| 989 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 990 | static int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 991 | nc_server_tls_add_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 992 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 993 | if (!name) { |
| 994 | ERRARG("name"); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 995 | return -1; |
| 996 | } |
| 997 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 998 | ++opts->trusted_cert_list_count; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 999 | opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists, |
| 1000 | opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1001 | if (!opts->trusted_cert_lists) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1002 | ERRMEM; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1003 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1004 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1005 | opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | API int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1011 | nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1012 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1013 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1014 | struct nc_endpt *endpt; |
| 1015 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1016 | if (!endpt_name) { |
| 1017 | ERRARG("endpt_name"); |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1021 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1022 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1023 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1024 | return -1; |
| 1025 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1026 | ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1027 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1028 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1029 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1030 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1034 | nc_server_tls_ch_client_endpt_add_trusted_cert_list(const char *client_name, const char *endpt_name, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1035 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1036 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1037 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1038 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1041 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1042 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1043 | return -1; |
| 1044 | } |
| 1045 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1046 | ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1047 | |
| 1048 | /* UNLOCK */ |
| 1049 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1050 | |
| 1051 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1054 | API void |
| 1055 | nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1056 | int *cert_path_count, char ***cert_data, int *cert_data_count), |
| 1057 | void *user_data, void (*free_user_data)(void *user_data)) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1058 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1059 | if (!cert_list_clb) { |
| 1060 | ERRARG("cert_list_clb"); |
| 1061 | return; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1064 | server_opts.trusted_cert_list_clb = cert_list_clb; |
| 1065 | server_opts.trusted_cert_list_data = user_data; |
| 1066 | server_opts.trusted_cert_list_data_free = free_user_data; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1070 | nc_server_tls_del_trusted_cert_list(const char *name, struct nc_server_tls_opts *opts) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1071 | { |
| 1072 | uint16_t i; |
| 1073 | |
| 1074 | if (!name) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1075 | for (i = 0; i < opts->trusted_cert_list_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1076 | free(opts->trusted_cert_lists[i]); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1077 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1078 | free(opts->trusted_cert_lists); |
| 1079 | opts->trusted_cert_lists = NULL; |
| 1080 | opts->trusted_cert_list_count = 0; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1081 | return 0; |
| 1082 | } else { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1083 | for (i = 0; i < opts->trusted_cert_list_count; ++i) { |
| 1084 | if (!strcmp(opts->trusted_cert_lists[i], name)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1085 | free(opts->trusted_cert_lists[i]); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1086 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1087 | --opts->trusted_cert_list_count; |
| 1088 | if (i < opts->trusted_cert_list_count - 1) { |
| 1089 | memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1, |
| 1090 | (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1091 | } |
| 1092 | return 0; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1097 | ERR(NULL, "Certificate list \"%s\" not found.", name); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1098 | return -1; |
| 1099 | } |
| 1100 | |
| 1101 | API int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1102 | nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1103 | { |
| 1104 | int ret; |
| 1105 | struct nc_endpt *endpt; |
| 1106 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1107 | if (!endpt_name) { |
| 1108 | ERRARG("endpt_name"); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1112 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1113 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1114 | if (!endpt) { |
| 1115 | return -1; |
| 1116 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1117 | ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1118 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1119 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1125 | nc_server_tls_ch_client_endpt_del_trusted_cert_list(const char *client_name, const char *endpt_name, const char *name) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1126 | { |
| 1127 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1128 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1129 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1130 | |
| 1131 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1132 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1133 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1134 | return -1; |
| 1135 | } |
| 1136 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1137 | ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1138 | |
| 1139 | /* UNLOCK */ |
| 1140 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1141 | |
| 1142 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | static int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1146 | nc_server_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1147 | { |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1148 | if (!ca_file && !ca_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1149 | ERRARG("ca_file and ca_dir"); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1150 | return -1; |
| 1151 | } |
| 1152 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1153 | if (ca_file) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1154 | free(opts->trusted_ca_file); |
| 1155 | opts->trusted_ca_file = strdup(ca_file); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1158 | if (ca_dir) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1159 | free(opts->trusted_ca_dir); |
| 1160 | opts->trusted_ca_dir = strdup(ca_dir); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1166 | API int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1167 | nc_server_tls_endpt_set_trusted_ca_paths(const char *endpt_name, const char *ca_file, const char *ca_dir) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1168 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1169 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1170 | struct nc_endpt *endpt; |
| 1171 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1172 | if (!endpt_name) { |
| 1173 | ERRARG("endpt_name"); |
| 1174 | return -1; |
| 1175 | } |
| 1176 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1177 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1178 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1179 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1182 | ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1183 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1184 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1185 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1186 | return ret; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1189 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1190 | nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file, |
| 1191 | const char *ca_dir) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1192 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1193 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1194 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1195 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1196 | |
| 1197 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1198 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1199 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1200 | return -1; |
| 1201 | } |
| 1202 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1203 | ret = nc_server_tls_set_trusted_ca_paths(ca_file, ca_dir, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1204 | |
| 1205 | /* UNLOCK */ |
| 1206 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1207 | |
| 1208 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1211 | static int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1212 | nc_server_tls_set_crl_paths(const char *crl_file, const char *crl_dir, struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1213 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1214 | X509_LOOKUP *lookup; |
| 1215 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1216 | if (!crl_file && !crl_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1217 | ERRARG("crl_file and crl_dir"); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1218 | return -1; |
| 1219 | } |
| 1220 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1221 | if (!opts->crl_store) { |
| 1222 | opts->crl_store = X509_STORE_new(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1225 | if (crl_file) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1226 | lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1227 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1228 | ERR(NULL, "Failed to add a lookup method."); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1229 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1232 | if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1233 | ERR(NULL, "Failed to add a revocation lookup file (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1234 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1238 | if (crl_dir) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1239 | lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1240 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1241 | ERR(NULL, "Failed to add a lookup method."); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1242 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1243 | } |
| 1244 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1245 | if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1246 | ERR(NULL, "Failed to add a revocation lookup directory (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1247 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1252 | |
| 1253 | fail: |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1254 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1257 | API int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1258 | nc_server_tls_endpt_set_crl_paths(const char *endpt_name, const char *crl_file, const char *crl_dir) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1259 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1260 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1261 | struct nc_endpt *endpt; |
| 1262 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1263 | if (!endpt_name) { |
| 1264 | ERRARG("endpt_name"); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1268 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1269 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1270 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1271 | return -1; |
| 1272 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1273 | ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1274 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1275 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1276 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1277 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1281 | nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file, |
| 1282 | const char *crl_dir) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1283 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1284 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1285 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1286 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1289 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1290 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1291 | return -1; |
| 1292 | } |
| 1293 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1294 | ret = nc_server_tls_set_crl_paths(crl_file, crl_dir, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1295 | |
| 1296 | /* UNLOCK */ |
| 1297 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1298 | |
| 1299 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1303 | nc_server_tls_clear_crls(struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1304 | { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1305 | if (!opts->crl_store) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | |
| 1309 | X509_STORE_free(opts->crl_store); |
| 1310 | opts->crl_store = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1313 | API void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1314 | nc_server_tls_endpt_clear_crls(const char *endpt_name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1315 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1316 | struct nc_endpt *endpt; |
| 1317 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1318 | if (!endpt_name) { |
| 1319 | ERRARG("endpt_name"); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1323 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1324 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1325 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1326 | return; |
| 1327 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1328 | nc_server_tls_clear_crls(endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1329 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1330 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1333 | API void |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1334 | nc_server_tls_ch_client_endpt_clear_crls(const char *client_name, const char *endpt_name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1335 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1336 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1337 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1338 | |
| 1339 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1340 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1341 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1342 | return; |
| 1343 | } |
| 1344 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1345 | nc_server_tls_clear_crls(endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1346 | |
| 1347 | /* UNLOCK */ |
| 1348 | nc_server_ch_client_unlock(client); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | static int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1352 | nc_server_tls_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1353 | struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1354 | { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1355 | struct nc_ctn *ctn, *new; |
| 1356 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1357 | if (!opts->ctn) { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1358 | /* the first item */ |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1359 | opts->ctn = new = calloc(1, sizeof *new); |
| 1360 | if (!new) { |
| 1361 | ERRMEM; |
| 1362 | return -1; |
| 1363 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1364 | } else if (opts->ctn->id > id) { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1365 | /* insert at the beginning */ |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1366 | new = calloc(1, sizeof *new); |
| 1367 | if (!new) { |
| 1368 | ERRMEM; |
| 1369 | return -1; |
| 1370 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1371 | new->next = opts->ctn; |
| 1372 | opts->ctn = new; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1373 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1374 | for (ctn = opts->ctn; ctn->next && ctn->next->id <= id; ctn = ctn->next) {} |
Michal Vasko | 276f9d9 | 2017-02-02 11:15:55 +0100 | [diff] [blame] | 1375 | if (ctn->id == id) { |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1376 | /* it exists already */ |
Michal Vasko | 276f9d9 | 2017-02-02 11:15:55 +0100 | [diff] [blame] | 1377 | new = ctn; |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1378 | } else { |
| 1379 | /* insert after ctn */ |
| 1380 | new = calloc(1, sizeof *new); |
| 1381 | if (!new) { |
| 1382 | ERRMEM; |
| 1383 | return -1; |
| 1384 | } |
| 1385 | new->next = ctn->next; |
| 1386 | ctn->next = new; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | new->id = id; |
| 1391 | if (fingerprint) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1392 | free(new->fingerprint); |
| 1393 | new->fingerprint = strdup(fingerprint); |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1394 | } |
| 1395 | if (map_type) { |
| 1396 | new->map_type = map_type; |
| 1397 | } |
| 1398 | if (name) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1399 | free(new->name); |
| 1400 | new->name = strdup(name); |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1401 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1407 | nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1408 | const char *name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1409 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1410 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1411 | struct nc_endpt *endpt; |
| 1412 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1413 | if (!endpt_name) { |
| 1414 | ERRARG("endpt_name"); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1418 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1419 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1420 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1421 | return -1; |
| 1422 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1423 | ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1424 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1425 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1426 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1427 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1431 | nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id, |
| 1432 | const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1433 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1434 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1435 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1436 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1437 | |
| 1438 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1439 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1440 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1441 | return -1; |
| 1442 | } |
| 1443 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1444 | ret = nc_server_tls_add_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1445 | |
| 1446 | /* UNLOCK */ |
| 1447 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1448 | |
| 1449 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | static int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1453 | nc_server_tls_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1454 | struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1455 | { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1456 | struct nc_ctn *ctn, *next, *prev; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1457 | int ret = -1; |
| 1458 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1459 | if ((id < 0) && !fingerprint && !map_type && !name) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1460 | ctn = opts->ctn; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1461 | while (ctn) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1462 | free(ctn->fingerprint); |
| 1463 | free(ctn->name); |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1464 | |
| 1465 | next = ctn->next; |
| 1466 | free(ctn); |
| 1467 | ctn = next; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1468 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1469 | ret = 0; |
| 1470 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1471 | opts->ctn = NULL; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1472 | } else { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1473 | prev = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1474 | ctn = opts->ctn; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1475 | while (ctn) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1476 | if (((id < 0) || (ctn->id == id)) && |
| 1477 | (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) && |
| 1478 | (!map_type || (ctn->map_type == map_type)) && |
| 1479 | (!name || (ctn->name && !strcmp(ctn->name, name)))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1480 | free(ctn->fingerprint); |
| 1481 | free(ctn->name); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1482 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1483 | if (prev) { |
| 1484 | prev->next = ctn->next; |
| 1485 | next = ctn->next; |
| 1486 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1487 | opts->ctn = ctn->next; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1488 | next = ctn->next; |
| 1489 | } |
| 1490 | free(ctn); |
| 1491 | ctn = next; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1492 | |
| 1493 | ret = 0; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1494 | } else { |
| 1495 | prev = ctn; |
| 1496 | ctn = ctn->next; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1497 | } |
| 1498 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | return ret; |
| 1502 | } |
| 1503 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1504 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1505 | nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1506 | const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1507 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1508 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1509 | struct nc_endpt *endpt; |
| 1510 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1511 | if (!endpt_name) { |
| 1512 | ERRARG("endpt_name"); |
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1516 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1517 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1518 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1519 | return -1; |
| 1520 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1521 | ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1522 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1523 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1524 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1525 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1529 | nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id, |
| 1530 | const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1531 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1532 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1533 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1534 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1535 | |
| 1536 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1537 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1538 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1539 | return -1; |
| 1540 | } |
| 1541 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1542 | ret = nc_server_tls_del_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1543 | |
| 1544 | /* UNLOCK */ |
| 1545 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1546 | |
| 1547 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1548 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1549 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1550 | static int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 1551 | nc_server_tls_get_ctn(uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, char **name, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1552 | struct nc_server_tls_opts *opts) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1553 | { |
| 1554 | struct nc_ctn *ctn; |
| 1555 | int ret = -1; |
| 1556 | |
| 1557 | for (ctn = opts->ctn; ctn; ctn = ctn->next) { |
| 1558 | if (id && *id && (*id != ctn->id)) { |
| 1559 | continue; |
| 1560 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1561 | if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1562 | continue; |
| 1563 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1564 | if (map_type && *map_type && (!ctn->map_type || (*map_type != ctn->map_type))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1565 | continue; |
| 1566 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1567 | if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1568 | continue; |
| 1569 | } |
| 1570 | |
| 1571 | /* first match, good enough */ |
| 1572 | if (id && !(*id)) { |
| 1573 | *id = ctn->id; |
| 1574 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1575 | if (fingerprint && !(*fingerprint) && ctn->fingerprint) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1576 | *fingerprint = strdup(ctn->fingerprint); |
| 1577 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1578 | if (map_type && !(*map_type) && ctn->map_type) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1579 | *map_type = ctn->map_type; |
| 1580 | } |
Adam Richter | d44680e | 2019-06-15 13:16:16 -0700 | [diff] [blame] | 1581 | if (name && !(*name) && ctn->name) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1582 | *name = strdup(ctn->name); |
| 1583 | } |
| 1584 | |
| 1585 | ret = 0; |
| 1586 | break; |
| 1587 | } |
| 1588 | |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | API int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 1593 | nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1594 | char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1595 | { |
| 1596 | int ret; |
| 1597 | struct nc_endpt *endpt; |
| 1598 | |
| 1599 | if (!endpt_name) { |
| 1600 | ERRARG("endpt_name"); |
| 1601 | return -1; |
| 1602 | } |
| 1603 | |
| 1604 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1605 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1606 | if (!endpt) { |
| 1607 | return -1; |
| 1608 | } |
| 1609 | ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
| 1610 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 1611 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1612 | |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
| 1616 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1617 | nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint, |
| 1618 | NC_TLS_CTN_MAPTYPE *map_type, char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1619 | { |
| 1620 | int ret; |
| 1621 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1622 | struct nc_ch_endpt *endpt; |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1625 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1626 | if (!endpt) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1627 | return -1; |
| 1628 | } |
| 1629 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1630 | ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* UNLOCK */ |
| 1633 | nc_server_ch_client_unlock(client); |
| 1634 | |
| 1635 | return ret; |
| 1636 | } |
| 1637 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 1638 | API const X509 * |
| 1639 | nc_session_get_client_cert(const struct nc_session *session) |
| 1640 | { |
| 1641 | if (!session || (session->side != NC_SERVER)) { |
| 1642 | ERRARG("session"); |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | |
| 1646 | return session->opts.server.client_cert; |
| 1647 | } |
| 1648 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 1649 | API void |
| 1650 | nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)) |
| 1651 | { |
| 1652 | server_opts.user_verify_clb = verify_clb; |
| 1653 | } |
| 1654 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1655 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1656 | nc_server_tls_clear_opts(struct nc_server_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1657 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1658 | free(opts->server_cert); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1659 | nc_server_tls_del_trusted_cert_list(NULL, opts); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1660 | free(opts->trusted_ca_file); |
| 1661 | free(opts->trusted_ca_dir); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1662 | nc_server_tls_clear_crls(opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1663 | nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1664 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1665 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1666 | static void |
| 1667 | nc_tls_make_verify_key(void) |
| 1668 | { |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1669 | pthread_key_create(&verify_key, NULL); |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1672 | static X509 * |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1673 | tls_load_cert(const char *cert_path, const char *cert_data) |
| 1674 | { |
| 1675 | X509 *cert; |
| 1676 | |
| 1677 | if (cert_path) { |
| 1678 | cert = pem_to_cert(cert_path); |
| 1679 | } else { |
| 1680 | cert = base64der_to_cert(cert_data); |
| 1681 | } |
| 1682 | |
| 1683 | if (!cert) { |
| 1684 | if (cert_path) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1685 | ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1686 | ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1687 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1688 | ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1689 | ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | return cert; |
| 1693 | } |
| 1694 | |
| 1695 | static int |
| 1696 | nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name) |
| 1697 | { |
| 1698 | char **cert_paths = NULL, **cert_data = NULL; |
| 1699 | int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0; |
| 1700 | X509 *cert = NULL; |
| 1701 | |
| 1702 | if (!server_opts.server_cert_chain_clb) { |
| 1703 | /* This is optional, so return OK */ |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | if (server_opts.server_cert_chain_clb(cert_name, server_opts.server_cert_chain_data, &cert_paths, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1708 | &cert_path_count, &cert_data, &cert_data_count)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1709 | ERR(NULL, "Server certificate chain callback failed."); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1710 | return -1; |
| 1711 | } |
| 1712 | |
| 1713 | for (i = 0; i < cert_path_count; ++i) { |
| 1714 | cert = tls_load_cert(cert_paths[i], NULL); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1715 | if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1716 | ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1717 | ret = -1; |
| 1718 | goto cleanup; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | for (i = 0; i < cert_data_count; ++i) { |
| 1723 | cert = tls_load_cert(NULL, cert_data[i]); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1724 | if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1725 | ERR(NULL, "Loading the server certificate chain failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1726 | ret = -1; |
| 1727 | goto cleanup; |
| 1728 | } |
| 1729 | } |
| 1730 | cleanup: |
| 1731 | for (i = 0; i < cert_path_count; ++i) { |
| 1732 | free(cert_paths[i]); |
| 1733 | } |
| 1734 | free(cert_paths); |
| 1735 | for (i = 0; i < cert_data_count; ++i) { |
| 1736 | free(cert_data[i]); |
| 1737 | } |
| 1738 | free(cert_data); |
| 1739 | /* cert is owned by the SSL_CTX */ |
| 1740 | |
| 1741 | return ret; |
| 1742 | } |
| 1743 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1744 | static int |
| 1745 | nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name) |
| 1746 | { |
| 1747 | char *cert_path = NULL, *cert_data = NULL, *privkey_path = NULL, *privkey_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1748 | int ret = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1749 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1750 | X509 *cert = NULL; |
| 1751 | EVP_PKEY *pkey = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1752 | |
| 1753 | if (!cert_name) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1754 | ERR(NULL, "Server certificate not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1755 | return -1; |
| 1756 | } else if (!server_opts.server_cert_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1757 | ERR(NULL, "Callback for retrieving the server certificate is not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1758 | return -1; |
| 1759 | } |
| 1760 | |
| 1761 | if (server_opts.server_cert_clb(cert_name, server_opts.server_cert_data, &cert_path, &cert_data, &privkey_path, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1762 | &privkey_data, &privkey_type)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1763 | ERR(NULL, "Server certificate callback failed."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1764 | return -1; |
| 1765 | } |
| 1766 | |
| 1767 | /* load the certificate */ |
| 1768 | if (cert_path) { |
| 1769 | if (SSL_CTX_use_certificate_file(tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1770 | ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1771 | ret = -1; |
| 1772 | goto cleanup; |
| 1773 | } |
| 1774 | } else { |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1775 | cert = base64der_to_cert(cert_data); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1776 | if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1777 | ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1778 | ret = -1; |
| 1779 | goto cleanup; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* load the private key */ |
| 1784 | if (privkey_path) { |
| 1785 | if (SSL_CTX_use_PrivateKey_file(tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1786 | ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1787 | ret = -1; |
| 1788 | goto cleanup; |
| 1789 | } |
| 1790 | } else { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1791 | pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1792 | if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1793 | ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1794 | ret = -1; |
| 1795 | goto cleanup; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1799 | ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name); |
| 1800 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1801 | cleanup: |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1802 | X509_free(cert); |
| 1803 | EVP_PKEY_free(pkey); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1804 | free(cert_path); |
| 1805 | free(cert_data); |
| 1806 | free(privkey_path); |
| 1807 | free(privkey_data); |
| 1808 | return ret; |
| 1809 | } |
| 1810 | |
| 1811 | static void |
| 1812 | tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data) |
| 1813 | { |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1814 | X509 *cert = tls_load_cert(cert_path, cert_data); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1815 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1816 | if (!cert) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1817 | return; |
| 1818 | } |
| 1819 | |
| 1820 | /* add the trusted certificate */ |
| 1821 | if (X509_STORE_add_cert(cert_store, cert) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1822 | ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1823 | X509_free(cert); |
| 1824 | return; |
| 1825 | } |
| 1826 | X509_free(cert); |
| 1827 | } |
| 1828 | |
| 1829 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1830 | nc_tls_store_set_trusted_certs(X509_STORE *cert_store, char **trusted_cert_lists, uint16_t trusted_cert_list_count) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1831 | { |
| 1832 | uint16_t i; |
| 1833 | int j; |
| 1834 | char **cert_paths, **cert_data; |
| 1835 | int cert_path_count, cert_data_count; |
| 1836 | |
| 1837 | if (!server_opts.trusted_cert_list_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1838 | ERR(NULL, "Callback for retrieving trusted certificate lists is not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1839 | return -1; |
| 1840 | } |
| 1841 | |
| 1842 | for (i = 0; i < trusted_cert_list_count; ++i) { |
| 1843 | cert_paths = cert_data = NULL; |
| 1844 | cert_path_count = cert_data_count = 0; |
| 1845 | if (server_opts.trusted_cert_list_clb(trusted_cert_lists[i], server_opts.trusted_cert_list_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1846 | &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1847 | ERR(NULL, "Trusted certificate list callback for \"%s\" failed.", trusted_cert_lists[i]); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1848 | return -1; |
| 1849 | } |
| 1850 | |
| 1851 | for (j = 0; j < cert_path_count; ++j) { |
| 1852 | tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL); |
| 1853 | free(cert_paths[j]); |
| 1854 | } |
| 1855 | free(cert_paths); |
| 1856 | |
| 1857 | for (j = 0; j < cert_data_count; ++j) { |
| 1858 | tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]); |
| 1859 | free(cert_data[j]); |
| 1860 | } |
| 1861 | free(cert_data); |
| 1862 | } |
| 1863 | |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1867 | static int |
| 1868 | nc_server_tls_accept_check(int accept_ret, struct nc_session *session) |
| 1869 | { |
| 1870 | int verify; |
| 1871 | |
| 1872 | /* check certificate verification result */ |
| 1873 | verify = SSL_get_verify_result(session->ti.tls); |
| 1874 | switch (verify) { |
| 1875 | case X509_V_OK: |
| 1876 | if (accept_ret == 1) { |
| 1877 | VRB(session, "Client certificate verified."); |
| 1878 | } |
| 1879 | break; |
| 1880 | default: |
| 1881 | ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify)); |
| 1882 | } |
| 1883 | |
| 1884 | if (accept_ret != 1) { |
| 1885 | switch (SSL_get_error(session->ti.tls, accept_ret)) { |
| 1886 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1887 | ERR(session, "SSL accept failed (%s).", strerror(errno)); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1888 | break; |
| 1889 | case SSL_ERROR_SSL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1890 | ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1891 | break; |
| 1892 | default: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1893 | ERR(session, "SSL accept failed."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1894 | break; |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | return accept_ret; |
| 1899 | } |
| 1900 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1901 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1902 | nc_accept_tls_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1903 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1904 | X509_STORE *cert_store; |
| 1905 | SSL_CTX *tls_ctx; |
| 1906 | X509_LOOKUP *lookup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1907 | struct nc_server_tls_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1908 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1909 | struct timespec ts_timeout; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1910 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1911 | opts = session->data; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1912 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1913 | /* SSL_CTX */ |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 1914 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
| 1915 | tls_ctx = SSL_CTX_new(TLS_server_method()); |
| 1916 | #else |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1917 | tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 1918 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1919 | if (!tls_ctx) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1920 | ERR(session, "Failed to create TLS context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1921 | goto error; |
| 1922 | } |
| 1923 | SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1924 | if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts->server_cert)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1925 | goto error; |
| 1926 | } |
| 1927 | |
| 1928 | /* X509_STORE, managed (freed) with the context */ |
| 1929 | cert_store = X509_STORE_new(); |
| 1930 | SSL_CTX_set_cert_store(tls_ctx, cert_store); |
| 1931 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1932 | if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) { |
| 1933 | goto error; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | if (opts->trusted_ca_file) { |
| 1937 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file()); |
| 1938 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1939 | ERR(session, "Failed to add a lookup method."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1940 | goto error; |
| 1941 | } |
| 1942 | |
| 1943 | if (X509_LOOKUP_load_file(lookup, opts->trusted_ca_file, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1944 | ERR(session, "Failed to add a trusted cert file (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1945 | goto error; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | if (opts->trusted_ca_dir) { |
| 1950 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir()); |
| 1951 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1952 | ERR(session, "Failed to add a lookup method."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1953 | goto error; |
| 1954 | } |
| 1955 | |
| 1956 | if (X509_LOOKUP_add_dir(lookup, opts->trusted_ca_dir, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1957 | ERR(session, "Failed to add a trusted cert directory (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1958 | goto error; |
| 1959 | } |
| 1960 | } |
| 1961 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1962 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1963 | session->ti.tls = SSL_new(tls_ctx); |
| 1964 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1965 | /* context can be freed already, trusted certs must be freed manually */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1966 | SSL_CTX_free(tls_ctx); |
| 1967 | tls_ctx = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1968 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1969 | if (!session->ti.tls) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1970 | ERR(session, "Failed to create TLS structure from context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1971 | goto error; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | SSL_set_fd(session->ti.tls, sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1975 | sock = -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1976 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 1977 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1978 | /* store session on per-thread basis */ |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1979 | pthread_once(&verify_once, nc_tls_make_verify_key); |
| 1980 | pthread_setspecific(verify_key, session); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1981 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1982 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1983 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1984 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1985 | while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) { |
| 1986 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1987 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1988 | ERR(session, "SSL accept timeout."); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1989 | return 0; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1990 | } |
| 1991 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1992 | if (nc_server_tls_accept_check(ret, session) != 1) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1993 | return -1; |
| 1994 | } |
| 1995 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1996 | return 1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1997 | |
| 1998 | error: |
| 1999 | if (sock > -1) { |
| 2000 | close(sock); |
| 2001 | } |
| 2002 | SSL_CTX_free(tls_ctx); |
| 2003 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2004 | } |