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 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 923 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 924 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 925 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 926 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 927 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 928 | return -1; |
| 929 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 930 | ret = nc_server_tls_set_server_cert(name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 931 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 932 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 933 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 934 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 938 | 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] | 939 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 940 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 941 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 942 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 943 | |
| 944 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 945 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 946 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 947 | return -1; |
| 948 | } |
| 949 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 950 | ret = nc_server_tls_set_server_cert(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 951 | |
| 952 | /* UNLOCK */ |
| 953 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 954 | |
| 955 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 958 | API void |
| 959 | 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] | 960 | char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, |
| 961 | void (*free_user_data)(void *user_data)) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 962 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 963 | if (!cert_clb) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 964 | ERRARG(NULL, "cert_clb"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 965 | return; |
Michal Vasko | a874879 | 2016-11-22 14:34:26 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 968 | server_opts.server_cert_clb = cert_clb; |
| 969 | server_opts.server_cert_data = user_data; |
| 970 | server_opts.server_cert_data_free = free_user_data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 973 | API void |
| 974 | 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] | 975 | 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] | 976 | { |
| 977 | if (!cert_chain_clb) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 978 | ERRARG(NULL, "cert_chain_clb"); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
| 982 | server_opts.server_cert_chain_clb = cert_chain_clb; |
| 983 | server_opts.server_cert_chain_data = user_data; |
| 984 | server_opts.server_cert_chain_data_free = free_user_data; |
| 985 | } |
| 986 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 987 | static int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 988 | 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] | 989 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 990 | NC_CHECK_ARG_RET(NULL, name, -1); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 991 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 992 | ++opts->trusted_cert_list_count; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 993 | opts->trusted_cert_lists = nc_realloc(opts->trusted_cert_lists, |
| 994 | opts->trusted_cert_list_count * sizeof *opts->trusted_cert_lists); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 995 | if (!opts->trusted_cert_lists) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 996 | ERRMEM; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 997 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 998 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 999 | opts->trusted_cert_lists[opts->trusted_cert_list_count - 1] = strdup(name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | API int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1005 | 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] | 1006 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1007 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1008 | struct nc_endpt *endpt; |
| 1009 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1010 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1011 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1012 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1013 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1014 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1015 | return -1; |
| 1016 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1017 | ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1018 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1019 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1020 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1021 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1025 | 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] | 1026 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1027 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1028 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1029 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1030 | |
| 1031 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1032 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1033 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1034 | return -1; |
| 1035 | } |
| 1036 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1037 | ret = nc_server_tls_add_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* UNLOCK */ |
| 1040 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1041 | |
| 1042 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1045 | API void |
| 1046 | 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] | 1047 | int *cert_path_count, char ***cert_data, int *cert_data_count), |
| 1048 | void *user_data, void (*free_user_data)(void *user_data)) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1049 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1050 | if (!cert_list_clb) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1051 | ERRARG(NULL, "cert_list_clb"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1052 | return; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1055 | server_opts.trusted_cert_list_clb = cert_list_clb; |
| 1056 | server_opts.trusted_cert_list_data = user_data; |
| 1057 | server_opts.trusted_cert_list_data_free = free_user_data; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1061 | 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] | 1062 | { |
| 1063 | uint16_t i; |
| 1064 | |
| 1065 | if (!name) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1066 | for (i = 0; i < opts->trusted_cert_list_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1067 | free(opts->trusted_cert_lists[i]); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1068 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1069 | free(opts->trusted_cert_lists); |
| 1070 | opts->trusted_cert_lists = NULL; |
| 1071 | opts->trusted_cert_list_count = 0; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1072 | return 0; |
| 1073 | } else { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1074 | for (i = 0; i < opts->trusted_cert_list_count; ++i) { |
| 1075 | if (!strcmp(opts->trusted_cert_lists[i], name)) { |
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 | --opts->trusted_cert_list_count; |
| 1079 | if (i < opts->trusted_cert_list_count - 1) { |
| 1080 | memmove(opts->trusted_cert_lists + i, opts->trusted_cert_lists + i + 1, |
| 1081 | (opts->trusted_cert_list_count - i) * sizeof *opts->trusted_cert_lists); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1082 | } |
| 1083 | return 0; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1088 | ERR(NULL, "Certificate list \"%s\" not found.", name); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | API int |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1093 | 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] | 1094 | { |
| 1095 | int ret; |
| 1096 | struct nc_endpt *endpt; |
| 1097 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1098 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1099 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1100 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1101 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1102 | if (!endpt) { |
| 1103 | return -1; |
| 1104 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1105 | ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1106 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1107 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1108 | |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
| 1112 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1113 | 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] | 1114 | { |
| 1115 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1116 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1117 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1118 | |
| 1119 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1120 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1121 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
| 1124 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1125 | ret = nc_server_tls_del_trusted_cert_list(name, endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1126 | |
| 1127 | /* UNLOCK */ |
| 1128 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1129 | |
| 1130 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | static int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1134 | 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] | 1135 | { |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1136 | if (!ca_file && !ca_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1137 | ERRARG(NULL, "ca_file and ca_dir"); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1138 | return -1; |
| 1139 | } |
| 1140 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1141 | if (ca_file) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1142 | free(opts->trusted_ca_file); |
| 1143 | opts->trusted_ca_file = strdup(ca_file); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1146 | if (ca_dir) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1147 | free(opts->trusted_ca_dir); |
| 1148 | opts->trusted_ca_dir = strdup(ca_dir); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1154 | API int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1155 | 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] | 1156 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1157 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1158 | struct nc_endpt *endpt; |
| 1159 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1160 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1161 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1162 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1163 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1164 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1165 | return -1; |
| 1166 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1167 | 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] | 1168 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1169 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1170 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1171 | return ret; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1174 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1175 | nc_server_tls_ch_client_endpt_set_trusted_ca_paths(const char *client_name, const char *endpt_name, const char *ca_file, |
| 1176 | const char *ca_dir) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1177 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1178 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1179 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1180 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1181 | |
| 1182 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1183 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1184 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1185 | return -1; |
| 1186 | } |
| 1187 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1188 | 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] | 1189 | |
| 1190 | /* UNLOCK */ |
| 1191 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1192 | |
| 1193 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1196 | static int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1197 | 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] | 1198 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1199 | X509_LOOKUP *lookup; |
| 1200 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1201 | if (!crl_file && !crl_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1202 | ERRARG(NULL, "crl_file and crl_dir"); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
| 1205 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1206 | if (!opts->crl_store) { |
| 1207 | opts->crl_store = X509_STORE_new(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1210 | if (crl_file) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1211 | lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1212 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1213 | ERR(NULL, "Failed to add a lookup method."); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1214 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1217 | if (X509_LOOKUP_load_file(lookup, crl_file, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1218 | 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] | 1219 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1223 | if (crl_dir) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1224 | lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1225 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1226 | ERR(NULL, "Failed to add a lookup method."); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1227 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1230 | if (X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1231 | 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] | 1232 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1237 | |
| 1238 | fail: |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1239 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1242 | API int |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 1243 | 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] | 1244 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1245 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1246 | struct nc_endpt *endpt; |
| 1247 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1248 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1249 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1250 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1251 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1252 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1253 | return -1; |
| 1254 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1255 | 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] | 1256 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1257 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1258 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1259 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1263 | nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *endpt_name, const char *crl_file, |
| 1264 | const char *crl_dir) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1265 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1266 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1267 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1268 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1269 | |
| 1270 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1271 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1272 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1273 | return -1; |
| 1274 | } |
| 1275 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1276 | 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] | 1277 | |
| 1278 | /* UNLOCK */ |
| 1279 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1280 | |
| 1281 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1285 | nc_server_tls_clear_crls(struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1286 | { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1287 | if (!opts->crl_store) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | X509_STORE_free(opts->crl_store); |
| 1292 | opts->crl_store = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1295 | API void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1296 | nc_server_tls_endpt_clear_crls(const char *endpt_name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1297 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1298 | struct nc_endpt *endpt; |
| 1299 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1300 | if (!endpt_name) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1301 | ERRARG(NULL, "endpt_name"); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1302 | return; |
| 1303 | } |
| 1304 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1305 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1306 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1307 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1308 | return; |
| 1309 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1310 | nc_server_tls_clear_crls(endpt->opts.tls); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1311 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1312 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1315 | API void |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1316 | 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] | 1317 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1318 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1319 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1320 | |
| 1321 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1322 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1323 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1324 | return; |
| 1325 | } |
| 1326 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1327 | nc_server_tls_clear_crls(endpt->opts.tls); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1328 | |
| 1329 | /* UNLOCK */ |
| 1330 | nc_server_ch_client_unlock(client); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1334 | 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] | 1335 | struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1336 | { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1337 | struct nc_ctn *ctn, *new; |
| 1338 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1339 | if (!opts->ctn) { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1340 | /* the first item */ |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1341 | opts->ctn = new = calloc(1, sizeof *new); |
| 1342 | if (!new) { |
| 1343 | ERRMEM; |
| 1344 | return -1; |
| 1345 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1346 | } else if (opts->ctn->id > id) { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1347 | /* insert at the beginning */ |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1348 | new = calloc(1, sizeof *new); |
| 1349 | if (!new) { |
| 1350 | ERRMEM; |
| 1351 | return -1; |
| 1352 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1353 | new->next = opts->ctn; |
| 1354 | opts->ctn = new; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1355 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1356 | 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] | 1357 | if (ctn->id == id) { |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1358 | /* it exists already */ |
Michal Vasko | 276f9d9 | 2017-02-02 11:15:55 +0100 | [diff] [blame] | 1359 | new = ctn; |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1360 | } else { |
| 1361 | /* insert after ctn */ |
| 1362 | new = calloc(1, sizeof *new); |
| 1363 | if (!new) { |
| 1364 | ERRMEM; |
| 1365 | return -1; |
| 1366 | } |
| 1367 | new->next = ctn->next; |
| 1368 | ctn->next = new; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | new->id = id; |
| 1373 | if (fingerprint) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1374 | free(new->fingerprint); |
| 1375 | new->fingerprint = strdup(fingerprint); |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1376 | } |
| 1377 | if (map_type) { |
| 1378 | new->map_type = map_type; |
| 1379 | } |
| 1380 | if (name) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1381 | free(new->name); |
| 1382 | new->name = strdup(name); |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1383 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1389 | 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] | 1390 | const char *name) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1391 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1392 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1393 | struct nc_endpt *endpt; |
| 1394 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1395 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1396 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1397 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1398 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1399 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1400 | return -1; |
| 1401 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1402 | 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] | 1403 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1404 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1405 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1406 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1410 | nc_server_tls_ch_client_endpt_add_ctn(const char *client_name, const char *endpt_name, uint32_t id, |
| 1411 | const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1412 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1413 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1414 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1415 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1416 | |
| 1417 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1418 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1419 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1420 | return -1; |
| 1421 | } |
| 1422 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1423 | 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] | 1424 | |
| 1425 | /* UNLOCK */ |
| 1426 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1427 | |
| 1428 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | static int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1432 | 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] | 1433 | struct nc_server_tls_opts *opts) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1434 | { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1435 | struct nc_ctn *ctn, *next, *prev; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1436 | int ret = -1; |
| 1437 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1438 | if ((id < 0) && !fingerprint && !map_type && !name) { |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1439 | ctn = opts->ctn; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1440 | while (ctn) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1441 | free(ctn->fingerprint); |
| 1442 | free(ctn->name); |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1443 | |
| 1444 | next = ctn->next; |
| 1445 | free(ctn); |
| 1446 | ctn = next; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1447 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1448 | ret = 0; |
| 1449 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1450 | opts->ctn = NULL; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1451 | } else { |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1452 | prev = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1453 | ctn = opts->ctn; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1454 | while (ctn) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1455 | if (((id < 0) || (ctn->id == id)) && |
| 1456 | (!fingerprint || !strcmp(ctn->fingerprint, fingerprint)) && |
| 1457 | (!map_type || (ctn->map_type == map_type)) && |
| 1458 | (!name || (ctn->name && !strcmp(ctn->name, name)))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1459 | free(ctn->fingerprint); |
| 1460 | free(ctn->name); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1461 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1462 | if (prev) { |
| 1463 | prev->next = ctn->next; |
| 1464 | next = ctn->next; |
| 1465 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1466 | opts->ctn = ctn->next; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1467 | next = ctn->next; |
| 1468 | } |
| 1469 | free(ctn); |
| 1470 | ctn = next; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1471 | |
| 1472 | ret = 0; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 1473 | } else { |
| 1474 | prev = ctn; |
| 1475 | ctn = ctn->next; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1476 | } |
| 1477 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1483 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1484 | 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] | 1485 | const char *name) |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1486 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1487 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1488 | struct nc_endpt *endpt; |
| 1489 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1490 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1491 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1492 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1493 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1494 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1495 | return -1; |
| 1496 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1497 | 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] | 1498 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1499 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1500 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1501 | return ret; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1505 | nc_server_tls_ch_client_endpt_del_ctn(const char *client_name, const char *endpt_name, int64_t id, |
| 1506 | const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, 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 | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1509 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1510 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1511 | |
| 1512 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1513 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1514 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1515 | return -1; |
| 1516 | } |
| 1517 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1518 | 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] | 1519 | |
| 1520 | /* UNLOCK */ |
| 1521 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1522 | |
| 1523 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1524 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1525 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1526 | static int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 1527 | 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] | 1528 | struct nc_server_tls_opts *opts) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1529 | { |
| 1530 | struct nc_ctn *ctn; |
| 1531 | int ret = -1; |
| 1532 | |
| 1533 | for (ctn = opts->ctn; ctn; ctn = ctn->next) { |
| 1534 | if (id && *id && (*id != ctn->id)) { |
| 1535 | continue; |
| 1536 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1537 | if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1538 | continue; |
| 1539 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1540 | 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] | 1541 | continue; |
| 1542 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1543 | if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1544 | continue; |
| 1545 | } |
| 1546 | |
| 1547 | /* first match, good enough */ |
| 1548 | if (id && !(*id)) { |
| 1549 | *id = ctn->id; |
| 1550 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1551 | if (fingerprint && !(*fingerprint) && ctn->fingerprint) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1552 | *fingerprint = strdup(ctn->fingerprint); |
| 1553 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 1554 | if (map_type && !(*map_type) && ctn->map_type) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1555 | *map_type = ctn->map_type; |
| 1556 | } |
Adam Richter | d44680e | 2019-06-15 13:16:16 -0700 | [diff] [blame] | 1557 | if (name && !(*name) && ctn->name) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1558 | *name = strdup(ctn->name); |
| 1559 | } |
| 1560 | |
| 1561 | ret = 0; |
| 1562 | break; |
| 1563 | } |
| 1564 | |
| 1565 | return ret; |
| 1566 | } |
| 1567 | |
| 1568 | API int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 1569 | 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] | 1570 | char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1571 | { |
| 1572 | int ret; |
| 1573 | struct nc_endpt *endpt; |
| 1574 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1575 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1578 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1579 | if (!endpt) { |
| 1580 | return -1; |
| 1581 | } |
| 1582 | ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
| 1583 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1584 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1585 | |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1590 | nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint, |
| 1591 | NC_TLS_CTN_MAPTYPE *map_type, char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1592 | { |
| 1593 | int ret; |
| 1594 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1595 | struct nc_ch_endpt *endpt; |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1596 | |
| 1597 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1598 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 1599 | if (!endpt) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 1600 | return -1; |
| 1601 | } |
| 1602 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 1603 | 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] | 1604 | |
| 1605 | /* UNLOCK */ |
| 1606 | nc_server_ch_client_unlock(client); |
| 1607 | |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 1611 | API const X509 * |
| 1612 | nc_session_get_client_cert(const struct nc_session *session) |
| 1613 | { |
| 1614 | if (!session || (session->side != NC_SERVER)) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1615 | ERRARG(session, "session"); |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 1616 | return NULL; |
| 1617 | } |
| 1618 | |
| 1619 | return session->opts.server.client_cert; |
| 1620 | } |
| 1621 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 1622 | API void |
| 1623 | nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)) |
| 1624 | { |
| 1625 | server_opts.user_verify_clb = verify_clb; |
| 1626 | } |
| 1627 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1628 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1629 | nc_server_tls_clear_opts(struct nc_server_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1630 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1631 | free(opts->server_cert); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1632 | nc_server_tls_del_trusted_cert_list(NULL, opts); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1633 | free(opts->trusted_ca_file); |
| 1634 | free(opts->trusted_ca_dir); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1635 | nc_server_tls_clear_crls(opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1636 | nc_server_tls_del_ctn(-1, NULL, 0, NULL, opts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1637 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1638 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1639 | static void |
| 1640 | nc_tls_make_verify_key(void) |
| 1641 | { |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1642 | pthread_key_create(&verify_key, NULL); |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1645 | static X509 * |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1646 | tls_load_cert(const char *cert_path, const char *cert_data) |
| 1647 | { |
| 1648 | X509 *cert; |
| 1649 | |
| 1650 | if (cert_path) { |
| 1651 | cert = pem_to_cert(cert_path); |
| 1652 | } else { |
| 1653 | cert = base64der_to_cert(cert_data); |
| 1654 | } |
| 1655 | |
| 1656 | if (!cert) { |
| 1657 | if (cert_path) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1658 | ERR(NULL, "Loading a trusted certificate (path \"%s\") failed (%s).", cert_path, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1659 | ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1660 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1661 | ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1662 | ERR_reason_error_string(ERR_get_error())); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | return cert; |
| 1666 | } |
| 1667 | |
| 1668 | static int |
| 1669 | nc_tls_ctx_set_server_cert_chain(SSL_CTX *tls_ctx, const char *cert_name) |
| 1670 | { |
| 1671 | char **cert_paths = NULL, **cert_data = NULL; |
| 1672 | int cert_path_count = 0, cert_data_count = 0, ret = 0, i = 0; |
| 1673 | X509 *cert = NULL; |
| 1674 | |
| 1675 | if (!server_opts.server_cert_chain_clb) { |
| 1676 | /* This is optional, so return OK */ |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | 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] | 1681 | &cert_path_count, &cert_data, &cert_data_count)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1682 | ERR(NULL, "Server certificate chain callback failed."); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1683 | return -1; |
| 1684 | } |
| 1685 | |
| 1686 | for (i = 0; i < cert_path_count; ++i) { |
| 1687 | cert = tls_load_cert(cert_paths[i], NULL); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1688 | if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1689 | 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] | 1690 | ret = -1; |
| 1691 | goto cleanup; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | for (i = 0; i < cert_data_count; ++i) { |
| 1696 | cert = tls_load_cert(NULL, cert_data[i]); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1697 | if (!cert || (SSL_CTX_add_extra_chain_cert(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1698 | 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] | 1699 | ret = -1; |
| 1700 | goto cleanup; |
| 1701 | } |
| 1702 | } |
| 1703 | cleanup: |
| 1704 | for (i = 0; i < cert_path_count; ++i) { |
| 1705 | free(cert_paths[i]); |
| 1706 | } |
| 1707 | free(cert_paths); |
| 1708 | for (i = 0; i < cert_data_count; ++i) { |
| 1709 | free(cert_data[i]); |
| 1710 | } |
| 1711 | free(cert_data); |
| 1712 | /* cert is owned by the SSL_CTX */ |
| 1713 | |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1717 | static int |
| 1718 | nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, const char *cert_name) |
| 1719 | { |
| 1720 | 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] | 1721 | int ret = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1722 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1723 | X509 *cert = NULL; |
| 1724 | EVP_PKEY *pkey = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1725 | |
| 1726 | if (!cert_name) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1727 | ERR(NULL, "Server certificate not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1728 | return -1; |
| 1729 | } else if (!server_opts.server_cert_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1730 | ERR(NULL, "Callback for retrieving the server certificate is not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1731 | return -1; |
| 1732 | } |
| 1733 | |
| 1734 | 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] | 1735 | &privkey_data, &privkey_type)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1736 | ERR(NULL, "Server certificate callback failed."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1737 | return -1; |
| 1738 | } |
| 1739 | |
| 1740 | /* load the certificate */ |
| 1741 | if (cert_path) { |
| 1742 | 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] | 1743 | 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] | 1744 | ret = -1; |
| 1745 | goto cleanup; |
| 1746 | } |
| 1747 | } else { |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1748 | cert = base64der_to_cert(cert_data); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1749 | if (!cert || (SSL_CTX_use_certificate(tls_ctx, cert) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1750 | 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] | 1751 | ret = -1; |
| 1752 | goto cleanup; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | /* load the private key */ |
| 1757 | if (privkey_path) { |
| 1758 | 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] | 1759 | 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] | 1760 | ret = -1; |
| 1761 | goto cleanup; |
| 1762 | } |
| 1763 | } else { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1764 | pkey = base64der_to_privatekey(privkey_data, nc_keytype2str(privkey_type)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1765 | if (!pkey || (SSL_CTX_use_PrivateKey(tls_ctx, pkey) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1766 | 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] | 1767 | ret = -1; |
| 1768 | goto cleanup; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1772 | ret = nc_tls_ctx_set_server_cert_chain(tls_ctx, cert_name); |
| 1773 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1774 | cleanup: |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1775 | X509_free(cert); |
| 1776 | EVP_PKEY_free(pkey); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1777 | free(cert_path); |
| 1778 | free(cert_data); |
| 1779 | free(privkey_path); |
| 1780 | free(privkey_data); |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
| 1784 | static void |
| 1785 | tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_path, const char *cert_data) |
| 1786 | { |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1787 | X509 *cert = tls_load_cert(cert_path, cert_data); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1788 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1789 | if (!cert) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1790 | return; |
| 1791 | } |
| 1792 | |
| 1793 | /* add the trusted certificate */ |
| 1794 | if (X509_STORE_add_cert(cert_store, cert) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1795 | 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] | 1796 | X509_free(cert); |
| 1797 | return; |
| 1798 | } |
| 1799 | X509_free(cert); |
| 1800 | } |
| 1801 | |
| 1802 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1803 | 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] | 1804 | { |
| 1805 | uint16_t i; |
| 1806 | int j; |
| 1807 | char **cert_paths, **cert_data; |
| 1808 | int cert_path_count, cert_data_count; |
| 1809 | |
| 1810 | if (!server_opts.trusted_cert_list_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1811 | ERR(NULL, "Callback for retrieving trusted certificate lists is not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1812 | return -1; |
| 1813 | } |
| 1814 | |
| 1815 | for (i = 0; i < trusted_cert_list_count; ++i) { |
| 1816 | cert_paths = cert_data = NULL; |
| 1817 | cert_path_count = cert_data_count = 0; |
| 1818 | 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] | 1819 | &cert_paths, &cert_path_count, &cert_data, &cert_data_count)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1820 | 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] | 1821 | return -1; |
| 1822 | } |
| 1823 | |
| 1824 | for (j = 0; j < cert_path_count; ++j) { |
| 1825 | tls_store_add_trusted_cert(cert_store, cert_paths[j], NULL); |
| 1826 | free(cert_paths[j]); |
| 1827 | } |
| 1828 | free(cert_paths); |
| 1829 | |
| 1830 | for (j = 0; j < cert_data_count; ++j) { |
| 1831 | tls_store_add_trusted_cert(cert_store, NULL, cert_data[j]); |
| 1832 | free(cert_data[j]); |
| 1833 | } |
| 1834 | free(cert_data); |
| 1835 | } |
| 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1840 | static int |
| 1841 | nc_server_tls_accept_check(int accept_ret, struct nc_session *session) |
| 1842 | { |
| 1843 | int verify; |
| 1844 | |
| 1845 | /* check certificate verification result */ |
| 1846 | verify = SSL_get_verify_result(session->ti.tls); |
| 1847 | switch (verify) { |
| 1848 | case X509_V_OK: |
| 1849 | if (accept_ret == 1) { |
| 1850 | VRB(session, "Client certificate verified."); |
| 1851 | } |
| 1852 | break; |
| 1853 | default: |
| 1854 | ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify)); |
| 1855 | } |
| 1856 | |
| 1857 | if (accept_ret != 1) { |
| 1858 | switch (SSL_get_error(session->ti.tls, accept_ret)) { |
| 1859 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1860 | ERR(session, "SSL accept failed (%s).", strerror(errno)); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1861 | break; |
| 1862 | case SSL_ERROR_SSL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1863 | 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] | 1864 | break; |
| 1865 | default: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1866 | ERR(session, "SSL accept failed."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1867 | break; |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | return accept_ret; |
| 1872 | } |
| 1873 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1874 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1875 | nc_accept_tls_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1876 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1877 | X509_STORE *cert_store; |
| 1878 | SSL_CTX *tls_ctx; |
| 1879 | X509_LOOKUP *lookup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1880 | struct nc_server_tls_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1881 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1882 | struct timespec ts_timeout; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1883 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1884 | opts = session->data; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1885 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1886 | /* SSL_CTX */ |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 1887 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
| 1888 | tls_ctx = SSL_CTX_new(TLS_server_method()); |
| 1889 | #else |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1890 | tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 1891 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1892 | if (!tls_ctx) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1893 | ERR(session, "Failed to create TLS context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1894 | goto error; |
| 1895 | } |
| 1896 | 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] | 1897 | 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] | 1898 | goto error; |
| 1899 | } |
| 1900 | |
| 1901 | /* X509_STORE, managed (freed) with the context */ |
| 1902 | cert_store = X509_STORE_new(); |
| 1903 | SSL_CTX_set_cert_store(tls_ctx, cert_store); |
| 1904 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1905 | if (nc_tls_store_set_trusted_certs(cert_store, opts->trusted_cert_lists, opts->trusted_cert_list_count)) { |
| 1906 | goto error; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | if (opts->trusted_ca_file) { |
| 1910 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file()); |
| 1911 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1912 | ERR(session, "Failed to add a lookup method."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1913 | goto error; |
| 1914 | } |
| 1915 | |
| 1916 | 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] | 1917 | 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] | 1918 | goto error; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | if (opts->trusted_ca_dir) { |
| 1923 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir()); |
| 1924 | if (!lookup) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1925 | ERR(session, "Failed to add a lookup method."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1926 | goto error; |
| 1927 | } |
| 1928 | |
| 1929 | 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] | 1930 | 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] | 1931 | goto error; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1935 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1936 | session->ti.tls = SSL_new(tls_ctx); |
| 1937 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1938 | /* context can be freed already, trusted certs must be freed manually */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1939 | SSL_CTX_free(tls_ctx); |
| 1940 | tls_ctx = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1941 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1942 | if (!session->ti.tls) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1943 | ERR(session, "Failed to create TLS structure from context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1944 | goto error; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | SSL_set_fd(session->ti.tls, sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1948 | sock = -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1949 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 1950 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1951 | /* store session on per-thread basis */ |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1952 | pthread_once(&verify_once, nc_tls_make_verify_key); |
| 1953 | pthread_setspecific(verify_key, session); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1954 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1955 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1956 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1957 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1958 | while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) { |
| 1959 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1960 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1961 | ERR(session, "SSL accept timeout."); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1962 | return 0; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1963 | } |
| 1964 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1965 | if (nc_server_tls_accept_check(ret, session) != 1) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1966 | return -1; |
| 1967 | } |
| 1968 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1969 | return 1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1970 | |
| 1971 | error: |
| 1972 | if (sock > -1) { |
| 1973 | close(sock); |
| 1974 | } |
| 1975 | SSL_CTX_free(tls_ctx); |
| 1976 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1977 | } |