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