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