Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_server_tls.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4 | * @author Roman Janota <janota@cesnet.cz> |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 5 | * @brief libnetconf2 TLS server session manipulation functions |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 7 | * @copyright |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 9 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 13 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 14 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 17 | #define _GNU_SOURCE |
| 18 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 19 | #include <poll.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 23 | #include <string.h> |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 25 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 26 | #include <curl/curl.h> |
| 27 | #include <openssl/bio.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 28 | #include <openssl/err.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 29 | #include <openssl/evp.h> |
| 30 | #include <openssl/ssl.h> |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 31 | #include <openssl/x509.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 32 | #include <openssl/x509v3.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 33 | |
Michal Vasko | 316c963 | 2020-04-15 11:06:57 +0200 | [diff] [blame] | 34 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 35 | #include "config.h" |
| 36 | #include "log_p.h" |
| 37 | #include "session.h" |
| 38 | #include "session_p.h" |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 39 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 40 | struct nc_server_tls_opts tls_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 41 | pthread_mutex_t tls_ch_opts_lock = PTHREAD_MUTEX_INITIALIZER; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 42 | extern struct nc_server_opts server_opts; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 43 | |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 44 | static pthread_key_t verify_key; |
| 45 | static pthread_once_t verify_once = PTHREAD_ONCE_INIT; |
| 46 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 47 | static char * |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 48 | asn1time_to_str(const ASN1_TIME *t) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 49 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 50 | char *cp; |
| 51 | BIO *bio; |
| 52 | int n; |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 53 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 54 | if (!t) { |
| 55 | return NULL; |
| 56 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 57 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 58 | bio = BIO_new(BIO_s_mem()); |
| 59 | if (!bio) { |
| 60 | return NULL; |
| 61 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 62 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 63 | ASN1_TIME_print(bio, t); |
| 64 | n = BIO_pending(bio); |
| 65 | cp = malloc(n + 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 66 | if (!cp) { |
| 67 | ERRMEM; |
| 68 | BIO_free(bio); |
| 69 | return NULL; |
| 70 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 71 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 72 | n = BIO_read(bio, cp, n); |
| 73 | if (n < 0) { |
| 74 | BIO_free(bio); |
| 75 | free(cp); |
| 76 | return NULL; |
| 77 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 78 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 79 | cp[n] = '\0'; |
| 80 | BIO_free(bio); |
| 81 | return cp; |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | digest_to_str(const unsigned char *digest, unsigned int dig_len, char **str) |
| 86 | { |
| 87 | unsigned int i; |
| 88 | |
| 89 | *str = malloc(dig_len * 3); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 90 | if (!*str) { |
| 91 | ERRMEM; |
| 92 | return; |
| 93 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 94 | for (i = 0; i < dig_len - 1; ++i) { |
| 95 | sprintf((*str) + (i * 3), "%02x:", digest[i]); |
| 96 | } |
| 97 | sprintf((*str) + (i * 3), "%02x", digest[i]); |
| 98 | } |
| 99 | |
| 100 | /* return NULL - SSL error can be retrieved */ |
| 101 | static X509 * |
| 102 | base64der_to_cert(const char *in) |
| 103 | { |
| 104 | X509 *out; |
| 105 | char *buf; |
| 106 | BIO *bio; |
| 107 | |
| 108 | if (in == NULL) { |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) { |
| 113 | return NULL; |
| 114 | } |
| 115 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 116 | if (!bio) { |
| 117 | free(buf); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | out = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 122 | if (!out) { |
| 123 | free(buf); |
| 124 | BIO_free(bio); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | free(buf); |
| 129 | BIO_free(bio); |
| 130 | return out; |
| 131 | } |
| 132 | |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 133 | static EVP_PKEY * |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 134 | base64der_to_privatekey(const char *in, const char *key_str) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 135 | { |
| 136 | EVP_PKEY *out; |
| 137 | char *buf; |
| 138 | BIO *bio; |
| 139 | |
| 140 | if (in == NULL) { |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 144 | 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] | 145 | key_str, " PRIVATE KEY-----") == -1) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 149 | if (!bio) { |
| 150 | free(buf); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 155 | if (!out) { |
| 156 | free(buf); |
| 157 | BIO_free(bio); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | free(buf); |
| 162 | BIO_free(bio); |
| 163 | return out; |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 164 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 165 | |
| 166 | static int |
| 167 | cert_pubkey_match(X509 *cert1, X509 *cert2) |
| 168 | { |
| 169 | ASN1_BIT_STRING *bitstr1, *bitstr2; |
| 170 | |
| 171 | bitstr1 = X509_get0_pubkey_bitstr(cert1); |
| 172 | bitstr2 = X509_get0_pubkey_bitstr(cert2); |
| 173 | |
| 174 | if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) || |
| 175 | memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | static int |
| 183 | nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username) |
| 184 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 185 | STACK_OF(GENERAL_NAME) * san_names; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 186 | GENERAL_NAME *san_name; |
| 187 | ASN1_OCTET_STRING *ip; |
| 188 | int i, san_count; |
| 189 | char *subject, *common_name; |
| 190 | |
Michal Vasko | d9a7864 | 2022-02-24 16:25:21 +0100 | [diff] [blame] | 191 | *username = NULL; |
| 192 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 193 | if (map_type == NC_TLS_CTN_COMMON_NAME) { |
| 194 | subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0); |
| 195 | common_name = strstr(subject, "CN="); |
| 196 | if (!common_name) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 197 | WRN(NULL, "Certificate does not include the commonName field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 198 | free(subject); |
| 199 | return 1; |
| 200 | } |
| 201 | common_name += 3; |
| 202 | if (strchr(common_name, '/')) { |
| 203 | *strchr(common_name, '/') = '\0'; |
| 204 | } |
| 205 | *username = strdup(common_name); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 206 | if (!*username) { |
| 207 | ERRMEM; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 208 | return 1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 209 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 210 | free(subject); |
| 211 | } else { |
| 212 | /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */ |
| 213 | san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL); |
| 214 | if (!san_names) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 215 | WRN(NULL, "Certificate has no SANs or failed to retrieve them."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | san_count = sk_GENERAL_NAME_num(san_names); |
| 220 | for (i = 0; i < san_count; ++i) { |
| 221 | san_name = sk_GENERAL_NAME_value(san_names, i); |
| 222 | |
| 223 | /* rfc822Name (email) */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 224 | if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_RFC822_NAME)) && |
| 225 | (san_name->type == GEN_EMAIL)) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 226 | *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.rfc822Name)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 227 | if (!*username) { |
| 228 | ERRMEM; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 229 | return 1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 230 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
| 234 | /* dNSName */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 235 | if (((map_type == NC_TLS_CTN_SAN_ANY) || (map_type == NC_TLS_CTN_SAN_DNS_NAME)) && |
| 236 | (san_name->type == GEN_DNS)) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 237 | *username = strdup((char *)ASN1_STRING_get0_data(san_name->d.dNSName)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 238 | if (!*username) { |
| 239 | ERRMEM; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 240 | return 1; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 241 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | |
| 245 | /* iPAddress */ |
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_IP_ADDRESS)) && |
| 247 | (san_name->type == GEN_IPADD)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 248 | ip = san_name->d.iPAddress; |
| 249 | if (ip->length == 4) { |
| 250 | 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] | 251 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 252 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 253 | return -1; |
| 254 | } |
| 255 | break; |
| 256 | } else if (ip->length == 16) { |
| 257 | if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
| 258 | ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5], |
| 259 | ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11], |
| 260 | 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] | 261 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 262 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 263 | return -1; |
| 264 | } |
| 265 | break; |
| 266 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 267 | WRN(NULL, "SAN IP address in an unknown format (length is %d).", ip->length); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 272 | |
Vitaly Kuzin | a3b6632 | 2021-08-26 12:33:43 +0300 | [diff] [blame] | 273 | if (i == san_count) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 274 | switch (map_type) { |
| 275 | case NC_TLS_CTN_SAN_RFC822_NAME: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 276 | WRN(NULL, "Certificate does not include the SAN rfc822Name field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 277 | break; |
| 278 | case NC_TLS_CTN_SAN_DNS_NAME: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 279 | WRN(NULL, "Certificate does not include the SAN dNSName field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 280 | break; |
| 281 | case NC_TLS_CTN_SAN_IP_ADDRESS: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 282 | WRN(NULL, "Certificate does not include the SAN iPAddress field."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 283 | break; |
| 284 | case NC_TLS_CTN_SAN_ANY: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 285 | WRN(NULL, "Certificate does not include any relevant SAN fields."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 286 | break; |
| 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | return 1; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* return: 0 - OK, 1 - no match, -1 - error */ |
| 298 | static int |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 299 | nc_tls_cert_to_name(struct nc_session *session, struct nc_ctn *ctn_first, X509 *cert) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 300 | { |
| 301 | char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL; |
| 302 | char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 303 | unsigned char *buf = malloc(64); |
| 304 | unsigned int buf_len = 64; |
| 305 | int ret = 0; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 306 | struct nc_ctn *ctn; |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 307 | NC_TLS_CTN_MAPTYPE map_type; |
| 308 | char *username = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 309 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 310 | if (!buf) { |
| 311 | ERRMEM; |
| 312 | return -1; |
| 313 | } |
| 314 | |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 315 | if (!session || !ctn_first || !cert) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 316 | free(buf); |
| 317 | return -1; |
| 318 | } |
| 319 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 320 | for (ctn = ctn_first; ctn; ctn = ctn->next) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 321 | /* reset map_type */ |
| 322 | map_type = NC_TLS_CTN_UNKNOWN; |
| 323 | |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 324 | /* first make sure the entry is valid */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 325 | if (!ctn->map_type || ((ctn->map_type == NC_TLS_CTN_SPECIFIED) && !ctn->name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 326 | VRB(NULL, "Cert verify CTN: entry with id %u not valid, skipping.", ctn->id); |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 327 | continue; |
| 328 | } |
| 329 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 330 | /* if ctn has no fingerprint, it will match any certificate */ |
| 331 | if (!ctn->fingerprint) { |
| 332 | map_type = ctn->map_type; |
| 333 | |
| 334 | /* MD5 */ |
| 335 | } else if (!strncmp(ctn->fingerprint, "01", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 336 | if (!digest_md5) { |
| 337 | if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 338 | ERR(NULL, "Calculating MD5 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 339 | ret = -1; |
| 340 | goto cleanup; |
| 341 | } |
| 342 | digest_to_str(buf, buf_len, &digest_md5); |
| 343 | } |
| 344 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 345 | if (!strcasecmp(ctn->fingerprint + 3, digest_md5)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 346 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 347 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 348 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 349 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 350 | free(digest_md5); |
| 351 | digest_md5 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 352 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 353 | /* SHA-1 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 354 | } else if (!strncmp(ctn->fingerprint, "02", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 355 | if (!digest_sha1) { |
| 356 | if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 357 | ERR(NULL, "Calculating SHA-1 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 358 | ret = -1; |
| 359 | goto cleanup; |
| 360 | } |
| 361 | digest_to_str(buf, buf_len, &digest_sha1); |
| 362 | } |
| 363 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 364 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha1)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 365 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 366 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 367 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 368 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 369 | free(digest_sha1); |
| 370 | digest_sha1 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 371 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 372 | /* SHA-224 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 373 | } else if (!strncmp(ctn->fingerprint, "03", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 374 | if (!digest_sha224) { |
| 375 | if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 376 | ERR(NULL, "Calculating SHA-224 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 377 | ret = -1; |
| 378 | goto cleanup; |
| 379 | } |
| 380 | digest_to_str(buf, buf_len, &digest_sha224); |
| 381 | } |
| 382 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 383 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha224)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 384 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 385 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 386 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 387 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 388 | free(digest_sha224); |
| 389 | digest_sha224 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 390 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 391 | /* SHA-256 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 392 | } else if (!strncmp(ctn->fingerprint, "04", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 393 | if (!digest_sha256) { |
| 394 | if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 395 | ERR(NULL, "Calculating SHA-256 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 396 | ret = -1; |
| 397 | goto cleanup; |
| 398 | } |
| 399 | digest_to_str(buf, buf_len, &digest_sha256); |
| 400 | } |
| 401 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 402 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha256)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 403 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 404 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 405 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 406 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 407 | free(digest_sha256); |
| 408 | digest_sha256 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 409 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 410 | /* SHA-384 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 411 | } else if (!strncmp(ctn->fingerprint, "05", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 412 | if (!digest_sha384) { |
| 413 | if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 414 | ERR(NULL, "Calculating SHA-384 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 415 | ret = -1; |
| 416 | goto cleanup; |
| 417 | } |
| 418 | digest_to_str(buf, buf_len, &digest_sha384); |
| 419 | } |
| 420 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 421 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha384)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 422 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 423 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 424 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 425 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 426 | free(digest_sha384); |
| 427 | digest_sha384 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 428 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 429 | /* SHA-512 */ |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 430 | } else if (!strncmp(ctn->fingerprint, "06", 2)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 431 | if (!digest_sha512) { |
| 432 | if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 433 | ERR(NULL, "Calculating SHA-512 digest failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 434 | ret = -1; |
| 435 | goto cleanup; |
| 436 | } |
| 437 | digest_to_str(buf, buf_len, &digest_sha512); |
| 438 | } |
| 439 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 440 | if (!strcasecmp(ctn->fingerprint + 3, digest_sha512)) { |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 441 | /* we got ourselves a potential winner! */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 442 | VRB(NULL, "Cert verify CTN: entry with a matching fingerprint found."); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 443 | map_type = ctn->map_type; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 444 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 445 | free(digest_sha512); |
| 446 | digest_sha512 = NULL; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 447 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 448 | /* unknown */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 449 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 450 | WRN(NULL, "Unknown fingerprint algorithm used (%s), skipping.", ctn->fingerprint); |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | |
| 454 | if (map_type != NC_TLS_CTN_UNKNOWN) { |
| 455 | /* found a fingerprint match */ |
| 456 | if (map_type == NC_TLS_CTN_SPECIFIED) { |
| 457 | /* specified -> get username from the ctn entry */ |
| 458 | session->username = strdup(ctn->name); |
| 459 | if (!session->username) { |
| 460 | ERRMEM; |
| 461 | ret = -1; |
| 462 | goto cleanup; |
| 463 | } |
| 464 | } else { |
| 465 | /* try to get the username from the cert with this ctn's map type */ |
| 466 | ret = nc_tls_ctn_get_username_from_cert(session->opts.server.client_cert, map_type, &username); |
| 467 | if (ret == -1) { |
| 468 | /* fatal error */ |
| 469 | goto cleanup; |
| 470 | } else if (ret) { |
| 471 | /* didn't get username, try next ctn entry */ |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | /* success */ |
| 476 | session->username = username; |
| 477 | } |
| 478 | |
| 479 | /* matching fingerprint found and username obtained, success */ |
| 480 | ret = 0; |
| 481 | goto cleanup; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 485 | if (!ctn) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 486 | ret = 1; |
| 487 | } |
| 488 | |
| 489 | cleanup: |
| 490 | free(digest_md5); |
| 491 | free(digest_sha1); |
| 492 | free(digest_sha224); |
| 493 | free(digest_sha256); |
| 494 | free(digest_sha384); |
| 495 | free(digest_sha512); |
| 496 | free(buf); |
| 497 | return ret; |
| 498 | } |
| 499 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 500 | static int |
| 501 | nc_server_tls_check_crl(X509_STORE *crl_store, X509_STORE_CTX *x509_ctx, X509 *cert, |
| 502 | const X509_NAME *subject, const X509_NAME *issuer) |
| 503 | { |
| 504 | int n, i, ret = 0; |
| 505 | X509_STORE_CTX *store_ctx = NULL; |
| 506 | X509_OBJECT *obj = NULL; |
| 507 | X509_CRL *crl; |
| 508 | X509_REVOKED *revoked; |
| 509 | EVP_PKEY *pubkey; |
| 510 | const ASN1_INTEGER *serial; |
| 511 | const ASN1_TIME *last_update = NULL, *next_update = NULL; |
| 512 | char *cp; |
| 513 | |
| 514 | store_ctx = X509_STORE_CTX_new(); |
| 515 | if (!store_ctx) { |
| 516 | ERRMEM; |
| 517 | ret = -1; |
| 518 | goto cleanup; |
| 519 | } |
| 520 | |
| 521 | /* init store context */ |
| 522 | ret = X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL); |
| 523 | if (!ret) { |
| 524 | ERR(NULL, "Initializing x509 store ctx failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 525 | ret = -1; |
| 526 | goto cleanup; |
| 527 | } |
| 528 | ret = 0; |
| 529 | |
| 530 | /* try to find a CRL entry that corresponds to the current certificate in question */ |
| 531 | obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, subject); |
| 532 | crl = X509_OBJECT_get0_X509_CRL(obj); |
| 533 | X509_OBJECT_free(obj); |
| 534 | if (crl) { |
| 535 | /* found it */ |
| 536 | cp = X509_NAME_oneline(subject, NULL, 0); |
| 537 | VRB(NULL, "Cert verify CRL: issuer: %s.", cp); |
| 538 | OPENSSL_free(cp); |
| 539 | |
| 540 | last_update = X509_CRL_get0_lastUpdate(crl); |
| 541 | next_update = X509_CRL_get0_nextUpdate(crl); |
| 542 | cp = asn1time_to_str(last_update); |
| 543 | VRB(NULL, "Cert verify CRL: last update: %s.", cp); |
| 544 | free(cp); |
| 545 | cp = asn1time_to_str(next_update); |
| 546 | VRB(NULL, "Cert verify CRL: next update: %s.", cp); |
| 547 | free(cp); |
| 548 | |
| 549 | /* verify the signature on this CRL */ |
| 550 | pubkey = X509_get0_pubkey(cert); |
| 551 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
| 552 | ERR(NULL, "Cert verify CRL: invalid signature."); |
| 553 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 554 | ret = -1; |
| 555 | goto cleanup; |
| 556 | } |
| 557 | |
| 558 | /* check date of CRL to make sure it's not expired */ |
| 559 | if (!next_update) { |
| 560 | ERR(NULL, "Cert verify CRL: invalid nextUpdate field."); |
| 561 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 562 | ret = -1; |
| 563 | goto cleanup; |
| 564 | } |
| 565 | |
| 566 | if (X509_cmp_current_time(next_update) < 0) { |
| 567 | ERR(NULL, "Cert verify CRL: expired - revoking all certificates."); |
| 568 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 569 | ret = -1; |
| 570 | goto cleanup; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* try to retrieve a CRL corresponding to the _issuer_ of |
| 575 | * the current certificate in order to check for revocation */ |
| 576 | obj = X509_STORE_CTX_get_obj_by_subject(store_ctx, X509_LU_CRL, issuer); |
| 577 | crl = X509_OBJECT_get0_X509_CRL(obj); |
| 578 | if (crl) { |
| 579 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 580 | for (i = 0; i < n; i++) { |
| 581 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 582 | serial = X509_REVOKED_get0_serialNumber(revoked); |
| 583 | if (ASN1_INTEGER_cmp(serial, X509_get_serialNumber(cert)) == 0) { |
| 584 | cp = X509_NAME_oneline(issuer, NULL, 0); |
| 585 | ERR(NULL, "Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s.", |
| 586 | serial, serial, cp); |
| 587 | OPENSSL_free(cp); |
| 588 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 589 | ret = -1; |
| 590 | goto cleanup; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | cleanup: |
| 596 | X509_STORE_CTX_free(store_ctx); |
| 597 | X509_OBJECT_free(obj); |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | static int |
| 602 | nc_server_tls_ts_ref_get_certs(const char *referenced_name, struct nc_certificate **certs, uint16_t *cert_count) |
| 603 | { |
| 604 | uint16_t i; |
| 605 | struct nc_truststore *ts = &server_opts.truststore; |
| 606 | |
| 607 | *certs = NULL; |
| 608 | *cert_count = 0; |
| 609 | |
| 610 | /* lookup name */ |
| 611 | for (i = 0; i < ts->cert_bag_count; i++) { |
| 612 | if (!strcmp(referenced_name, ts->cert_bags[i].name)) { |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (i == ts->cert_bag_count) { |
| 618 | ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name); |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | *certs = ts->cert_bags[i].certs; |
| 623 | *cert_count = ts->cert_bags[i].cert_count; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* In case a CA chain verification failed an end-entity certificate must match. |
| 628 | * The meaning of local_or_referenced is that it states, which end-entity certificates to check |
| 629 | * (1 = current endpoint's, 2 = referenced endpoint's). |
| 630 | */ |
| 631 | static int |
| 632 | nc_server_tls_do_preverify(struct nc_session *session, X509_STORE_CTX *x509_ctx, int local_or_referenced) |
| 633 | { |
| 634 | X509_STORE *store; |
| 635 | struct nc_cert_grouping *ee_certs; |
| 636 | int i, ret; |
| 637 | X509 *cert; |
| 638 | struct nc_certificate *certs; |
| 639 | uint16_t cert_count; |
| 640 | |
| 641 | store = X509_STORE_CTX_get0_store(x509_ctx); |
| 642 | if (!store) { |
| 643 | ERR(session, "Error getting store from context (%s).", ERR_reason_error_string(ERR_get_error())); |
| 644 | return -1; |
| 645 | } |
| 646 | |
| 647 | /* get the data from the store */ |
| 648 | ee_certs = X509_STORE_get_ex_data(store, local_or_referenced); |
| 649 | if (!ee_certs) { |
| 650 | ERR(session, "Error getting data from store (%s).", ERR_reason_error_string(ERR_get_error())); |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | if (ee_certs->store == NC_STORE_LOCAL) { |
| 655 | /* local definition */ |
| 656 | certs = ee_certs->certs; |
| 657 | cert_count = ee_certs->cert_count; |
| 658 | } else { |
| 659 | /* truststore reference */ |
| 660 | if (nc_server_tls_ts_ref_get_certs(ee_certs->ts_ref, &certs, &cert_count)) { |
| 661 | ERR(NULL, "Error getting end-entity certificates from the truststore reference \"%s\".", ee_certs->ts_ref); |
| 662 | return -1; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | for (i = 0; i < cert_count; i++) { |
| 667 | cert = base64der_to_cert(certs[i].data); |
| 668 | ret = cert_pubkey_match(session->opts.server.client_cert, cert); |
| 669 | X509_free(cert); |
| 670 | if (ret) { |
| 671 | /* we are just overriding the failed standard certificate verification (preverify_ok == 0), |
| 672 | * this callback will be called again with the same current certificate and preverify_ok == 1 */ |
| 673 | VRB(session, "Cert verify: fail (%s), but the end-entity certificate is trusted, continuing.", |
| 674 | X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
| 675 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
| 676 | return 1; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return 0; |
| 681 | } |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 682 | |
| 683 | static int |
| 684 | nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 685 | { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 686 | X509_NAME *subject; |
| 687 | X509_NAME *issuer; |
| 688 | X509 *cert; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 689 | char *cp; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 690 | |
| 691 | STACK_OF(X509) * cert_stack; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 692 | struct nc_session *session; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 693 | struct nc_server_tls_opts *opts; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 694 | int rc, depth; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 695 | |
| 696 | /* get the thread session */ |
| 697 | session = pthread_getspecific(verify_key); |
| 698 | if (!session) { |
| 699 | ERRINT; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | opts = session->data; |
| 704 | |
| 705 | /* get the last certificate, that is the peer (client) certificate */ |
| 706 | if (!session->opts.server.client_cert) { |
| 707 | cert_stack = X509_STORE_CTX_get1_chain(x509_ctx); |
Andrew Langefeld | 62bc671 | 2018-08-28 16:17:16 -0500 | [diff] [blame] | 708 | session->opts.server.client_cert = sk_X509_value(cert_stack, 0); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 709 | X509_up_ref(session->opts.server.client_cert); |
| 710 | sk_X509_pop_free(cert_stack, X509_free); |
| 711 | } |
| 712 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 713 | /* standard certificate verification failed, so an end-entity client cert must match to continue */ |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 714 | if (!preverify_ok) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 715 | /* check current endpoint's end-entity certs */ |
| 716 | rc = nc_server_tls_do_preverify(session, x509_ctx, 1); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 717 | if (rc == -1) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 718 | return 0; |
| 719 | } else if (rc == 1) { |
| 720 | return 1; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 721 | } |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 722 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 723 | /* no match, continue */ |
| 724 | if (opts->endpt_client_ref) { |
| 725 | /* check referenced endpoint's end-entity certs */ |
| 726 | rc = nc_server_tls_do_preverify(session, x509_ctx, 2); |
| 727 | if (rc == -1) { |
| 728 | return 0; |
| 729 | } else if (rc == 1) { |
| 730 | return 1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 731 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 732 | } |
| 733 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 734 | /* no match, fail */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 735 | ERR(session, "Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | /* print cert verify info */ |
| 740 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 741 | VRB(session, "Cert verify: depth %d.", depth); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 742 | |
| 743 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 744 | subject = X509_get_subject_name(cert); |
| 745 | issuer = X509_get_issuer_name(cert); |
| 746 | |
| 747 | cp = X509_NAME_oneline(subject, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 748 | VRB(session, "Cert verify: subject: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 749 | OPENSSL_free(cp); |
| 750 | cp = X509_NAME_oneline(issuer, NULL, 0); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 751 | VRB(session, "Cert verify: issuer: %s.", cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 752 | OPENSSL_free(cp); |
| 753 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 754 | /* check if the current certificate is revoked if CRL is set */ |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 755 | if (opts->crl_store) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 756 | rc = nc_server_tls_check_crl(opts->crl_store, x509_ctx, cert, subject, issuer); |
| 757 | if (rc) { |
| 758 | return 0; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | /* cert-to-name already successful */ |
| 763 | if (session->username) { |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | /* cert-to-name */ |
Roytak | be39192 | 2023-07-27 19:59:24 +0200 | [diff] [blame] | 768 | rc = nc_tls_cert_to_name(session, opts->ctn, cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 769 | if (rc == -1) { |
| 770 | /* fatal error */ |
| 771 | depth = 0; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 772 | goto fail; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 773 | } else if ((rc == 1) && !opts->endpt_client_ref) { |
| 774 | /* no match found and no referenced endpoint */ |
| 775 | goto fail; |
| 776 | } else if ((rc == 1) && opts->endpt_client_ref) { |
| 777 | /* no match found, but has a referenced endpoint so try it */ |
| 778 | rc = nc_tls_cert_to_name(session, opts->endpt_client_ref->opts.tls->ctn, cert); |
| 779 | if (rc) { |
| 780 | if (rc == -1) { |
| 781 | /* fatal error */ |
| 782 | depth = 0; |
| 783 | } |
| 784 | /* rc == 1 is a normal CTN fail (no match found) */ |
| 785 | goto fail; |
| 786 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 789 | VRB(session, "Cert verify CTN: new client username recognized as \"%s\".", session->username); |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 790 | |
| 791 | if (server_opts.user_verify_clb && !server_opts.user_verify_clb(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 792 | VRB(session, "Cert verify: user verify callback revoked authorization."); |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 793 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 794 | return 0; |
| 795 | } |
| 796 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 797 | return 1; |
| 798 | |
| 799 | fail: |
| 800 | if (depth > 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 801 | VRB(session, "Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 802 | return 1; |
| 803 | } |
| 804 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 805 | VRB(session, "Cert-to-name unsuccessful, dropping the new client."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 806 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 807 | return 0; |
| 808 | } |
| 809 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 810 | static int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 811 | 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] | 812 | struct nc_server_tls_opts *opts) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 813 | { |
| 814 | struct nc_ctn *ctn; |
| 815 | int ret = -1; |
| 816 | |
| 817 | for (ctn = opts->ctn; ctn; ctn = ctn->next) { |
| 818 | if (id && *id && (*id != ctn->id)) { |
| 819 | continue; |
| 820 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 821 | if (fingerprint && *fingerprint && (!ctn->fingerprint || strcmp(*fingerprint, ctn->fingerprint))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 822 | continue; |
| 823 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 824 | 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] | 825 | continue; |
| 826 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 827 | if (name && *name && (!ctn->name || strcmp(*name, ctn->name))) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 828 | continue; |
| 829 | } |
| 830 | |
| 831 | /* first match, good enough */ |
| 832 | if (id && !(*id)) { |
| 833 | *id = ctn->id; |
| 834 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 835 | if (fingerprint && !(*fingerprint) && ctn->fingerprint) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 836 | *fingerprint = strdup(ctn->fingerprint); |
| 837 | } |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 838 | if (map_type && !(*map_type) && ctn->map_type) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 839 | *map_type = ctn->map_type; |
| 840 | } |
Adam Richter | d44680e | 2019-06-15 13:16:16 -0700 | [diff] [blame] | 841 | if (name && !(*name) && ctn->name) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 842 | *name = strdup(ctn->name); |
| 843 | } |
| 844 | |
| 845 | ret = 0; |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | API int |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 853 | 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] | 854 | char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 855 | { |
| 856 | int ret; |
| 857 | struct nc_endpt *endpt; |
| 858 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 859 | NC_CHECK_ARG_RET(NULL, endpt_name, -1); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 860 | |
| 861 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 862 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_OPENSSL, NULL); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 863 | if (!endpt) { |
| 864 | return -1; |
| 865 | } |
| 866 | ret = nc_server_tls_get_ctn(id, fingerprint, map_type, name, endpt->opts.tls); |
| 867 | /* UNLOCK */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 868 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 869 | |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 874 | nc_server_tls_ch_client_endpt_get_ctn(const char *client_name, const char *endpt_name, uint32_t *id, char **fingerprint, |
| 875 | NC_TLS_CTN_MAPTYPE *map_type, char **name) |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 876 | { |
| 877 | int ret; |
| 878 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 879 | struct nc_ch_endpt *endpt; |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 880 | |
| 881 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 882 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_OPENSSL, &client); |
| 883 | if (!endpt) { |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 884 | return -1; |
| 885 | } |
| 886 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 887 | 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] | 888 | |
| 889 | /* UNLOCK */ |
| 890 | nc_server_ch_client_unlock(client); |
| 891 | |
| 892 | return ret; |
| 893 | } |
| 894 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 895 | API const X509 * |
| 896 | nc_session_get_client_cert(const struct nc_session *session) |
| 897 | { |
| 898 | if (!session || (session->side != NC_SERVER)) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 899 | ERRARG(session, "session"); |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 900 | return NULL; |
| 901 | } |
| 902 | |
| 903 | return session->opts.server.client_cert; |
| 904 | } |
| 905 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 906 | API void |
| 907 | nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)) |
| 908 | { |
| 909 | server_opts.user_verify_clb = verify_clb; |
| 910 | } |
| 911 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 912 | static void |
| 913 | nc_tls_make_verify_key(void) |
| 914 | { |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 915 | pthread_key_create(&verify_key, NULL); |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 916 | } |
| 917 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 918 | static int |
| 919 | nc_server_tls_ks_ref_get_cert_key(const char *referenced_key_name, const char *referenced_cert_name, |
| 920 | char **privkey_data, NC_PRIVKEY_FORMAT *privkey_type, char **cert_data) |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 921 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 922 | uint16_t i, j; |
| 923 | struct nc_keystore *ks = &server_opts.keystore; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 924 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 925 | *privkey_data = NULL; |
| 926 | *cert_data = NULL; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 927 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 928 | /* lookup name */ |
| 929 | for (i = 0; i < ks->asym_key_count; i++) { |
| 930 | if (!strcmp(referenced_key_name, ks->asym_keys[i].name)) { |
| 931 | break; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 932 | } |
| 933 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 934 | if (i == ks->asym_key_count) { |
| 935 | ERR(NULL, "Keystore entry \"%s\" not found.", referenced_key_name); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 936 | return -1; |
| 937 | } |
| 938 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 939 | for (j = 0; j < ks->asym_keys[i].cert_count; j++) { |
| 940 | if (!strcmp(referenced_cert_name, ks->asym_keys[i].certs[i].name)) { |
| 941 | break; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 942 | } |
| 943 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 944 | if (j == ks->asym_keys[i].cert_count) { |
| 945 | ERR(NULL, "Keystore certificate entry \"%s\" associated with the key \"%s\" not found.", |
| 946 | referenced_cert_name, referenced_key_name); |
| 947 | return -1; |
| 948 | } |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 949 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 950 | *privkey_data = ks->asym_keys[i].privkey_data; |
| 951 | *privkey_type = ks->asym_keys[i].privkey_type; |
| 952 | *cert_data = ks->asym_keys[i].certs[j].data; |
| 953 | return 0; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 954 | } |
| 955 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 956 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 957 | nc_tls_ctx_set_server_cert_key(SSL_CTX *tls_ctx, struct nc_server_tls_opts *opts) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 958 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 959 | char *privkey_data = NULL, *cert_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 960 | int ret = 0; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 961 | NC_PRIVKEY_FORMAT privkey_type; |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 962 | X509 *cert = NULL; |
| 963 | EVP_PKEY *pkey = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 964 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 965 | NC_CHECK_ARG_RET(NULL, tls_ctx, opts, -1); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 966 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 967 | /* get data needed for setting the server cert */ |
| 968 | if (opts->store == NC_STORE_LOCAL) { |
| 969 | /* local definition */ |
| 970 | cert_data = opts->cert_data; |
| 971 | privkey_data = opts->privkey_data; |
| 972 | privkey_type = opts->privkey_type; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 973 | } else { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 974 | /* keystore */ |
| 975 | ret = nc_server_tls_ks_ref_get_cert_key(opts->key_ref, opts->cert_ref, &privkey_data, &privkey_type, &cert_data); |
| 976 | if (ret) { |
| 977 | ERR(NULL, "Getting server certificate from the keystore reference \"%s\" failed.", opts->key_ref); |
| 978 | return -1; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 982 | /* load the cert */ |
| 983 | cert = base64der_to_cert(cert_data); |
| 984 | if (!cert) { |
| 985 | ERR(NULL, "Converting certificate data to certificate format failed."); |
| 986 | ret = -1; |
| 987 | goto cleanup; |
| 988 | } |
| 989 | |
| 990 | /* set server cert */ |
| 991 | ret = SSL_CTX_use_certificate(tls_ctx, cert); |
| 992 | if (ret != 1) { |
| 993 | ERR(NULL, "Loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 994 | ret = -1; |
| 995 | goto cleanup; |
| 996 | } |
| 997 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 998 | /* load the private key */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 999 | pkey = base64der_to_privatekey(privkey_data, nc_privkey_format_to_str(privkey_type)); |
| 1000 | if (!pkey) { |
| 1001 | ERR(NULL, "Converting private key data to private key format failed."); |
| 1002 | ret = -1; |
| 1003 | goto cleanup; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1006 | /* set server key */ |
| 1007 | ret = SSL_CTX_use_PrivateKey(tls_ctx, pkey); |
| 1008 | if (ret != 1) { |
| 1009 | ERR(NULL, "Loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1010 | ret = -1; |
| 1011 | goto cleanup; |
| 1012 | } |
| 1013 | |
| 1014 | ret = 0; |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 1015 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1016 | cleanup: |
Michal Vasko | 6e08cb7 | 2017-02-02 11:15:29 +0100 | [diff] [blame] | 1017 | X509_free(cert); |
| 1018 | EVP_PKEY_free(pkey); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1019 | return ret; |
| 1020 | } |
| 1021 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1022 | static int |
| 1023 | tls_store_add_trusted_cert(X509_STORE *cert_store, const char *cert_data) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1024 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1025 | X509 *cert; |
| 1026 | |
| 1027 | cert = base64der_to_cert(cert_data); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1028 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1029 | if (!cert) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1030 | ERR(NULL, "Loading a trusted certificate (data \"%s\") failed (%s).", cert_data, |
| 1031 | ERR_reason_error_string(ERR_get_error())); |
| 1032 | return -1; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* add the trusted certificate */ |
| 1036 | if (X509_STORE_add_cert(cert_store, cert) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1037 | ERR(NULL, "Adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1038 | X509_free(cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1039 | return -1; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1040 | } |
| 1041 | X509_free(cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1042 | |
| 1043 | return 0; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1047 | nc_tls_store_set_trusted_certs(X509_STORE *cert_store, struct nc_cert_grouping *ca_certs) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1048 | { |
| 1049 | uint16_t i; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1050 | struct nc_certificate *certs; |
| 1051 | uint16_t cert_count; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1052 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1053 | if (ca_certs->store == NC_STORE_LOCAL) { |
| 1054 | /* local definition */ |
| 1055 | certs = ca_certs->certs; |
| 1056 | cert_count = ca_certs->cert_count; |
| 1057 | } else { |
| 1058 | /* truststore */ |
| 1059 | if (nc_server_tls_ts_ref_get_certs(ca_certs->ts_ref, &certs, &cert_count)) { |
| 1060 | ERR(NULL, "Error getting certificate-authority certificates from the truststore reference \"%s\".", ca_certs->ts_ref); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1061 | return -1; |
| 1062 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1063 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1064 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1065 | for (i = 0; i < cert_count; i++) { |
| 1066 | if (tls_store_add_trusted_cert(cert_store, certs[i].data)) { |
| 1067 | return -1; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1068 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1074 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1075 | nc_server_tls_crl_path(struct nc_session *session, const char *crl_path, X509_STORE *store) |
| 1076 | { |
| 1077 | int ret = 0; |
| 1078 | X509_CRL *crl = NULL; |
| 1079 | FILE *f; |
| 1080 | |
| 1081 | f = fopen(crl_path, "r"); |
| 1082 | if (!f) { |
| 1083 | ERR(session, "Unable to open CRL file \"%s\".", crl_path); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | /* try DER first */ |
| 1088 | crl = d2i_X509_CRL_fp(f, NULL); |
| 1089 | if (crl) { |
| 1090 | /* success */ |
| 1091 | goto ok; |
| 1092 | } |
| 1093 | |
| 1094 | /* DER failed, try PEM */ |
| 1095 | rewind(f); |
| 1096 | crl = PEM_read_X509_CRL(f, NULL, NULL, NULL); |
| 1097 | if (!crl) { |
| 1098 | ERR(session, "Reading CRL from file \"%s\" failed.", crl_path); |
| 1099 | ret = -1; |
| 1100 | goto cleanup; |
| 1101 | } |
| 1102 | |
| 1103 | ok: |
| 1104 | ret = X509_STORE_add_crl(store, crl); |
| 1105 | if (!ret) { |
| 1106 | ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1107 | ret = -1; |
| 1108 | goto cleanup; |
| 1109 | } |
| 1110 | /* ok */ |
| 1111 | ret = 0; |
| 1112 | |
| 1113 | cleanup: |
| 1114 | fclose(f); |
| 1115 | X509_CRL_free(crl); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | static size_t |
| 1120 | nc_server_tls_curl_cb(char *ptr, size_t size, size_t nmemb, void *userdata) |
| 1121 | { |
| 1122 | struct nc_curl_data *data; |
| 1123 | |
| 1124 | size = nmemb; |
| 1125 | |
| 1126 | data = (struct nc_curl_data *)userdata; |
| 1127 | |
| 1128 | data->data = nc_realloc(data->data, data->size + size); |
| 1129 | if (!data->data) { |
| 1130 | ERRMEM; |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | memcpy(&data->data[data->size], ptr, size); |
| 1135 | data->size += size; |
| 1136 | |
| 1137 | return size; |
| 1138 | } |
| 1139 | |
| 1140 | static int |
| 1141 | nc_server_tls_curl_init(struct nc_session *session, CURL **handle, struct nc_curl_data *data) |
| 1142 | { |
| 1143 | NC_CHECK_ARG_RET(session, handle, data, -1); |
| 1144 | |
| 1145 | *handle = NULL; |
| 1146 | |
| 1147 | *handle = curl_easy_init(); |
| 1148 | if (!*handle) { |
| 1149 | ERR(session, "Initializing CURL failed."); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | if (curl_easy_setopt(*handle, CURLOPT_WRITEFUNCTION, nc_server_tls_curl_cb)) { |
| 1154 | ERR(session, "Setting curl callback failed."); |
| 1155 | return -1; |
| 1156 | } |
| 1157 | |
| 1158 | if (curl_easy_setopt(*handle, CURLOPT_WRITEDATA, data)) { |
| 1159 | ERR(session, "Setting curl callback data failed."); |
| 1160 | return -1; |
| 1161 | } |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | static int |
| 1167 | nc_server_tls_curl_fetch(struct nc_session *session, CURL *handle, const char *url) |
| 1168 | { |
| 1169 | char err_buf[CURL_ERROR_SIZE]; |
| 1170 | |
| 1171 | /* set uri */ |
| 1172 | if (curl_easy_setopt(handle, CURLOPT_URL, url)) { |
| 1173 | ERR(session, "Setting URI \"%s\" to download CRL from failed.", url); |
| 1174 | return -1; |
| 1175 | } |
| 1176 | |
| 1177 | /* set err buf */ |
| 1178 | curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, err_buf); |
| 1179 | |
| 1180 | /* download */ |
| 1181 | if (curl_easy_perform(handle)) { |
| 1182 | ERR(session, "Downloading CRL from \"%s\" failed (%s).", url, err_buf); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static int |
| 1190 | nc_server_tls_add_crl_to_store(struct nc_session *session, struct nc_curl_data *downloaded, X509_STORE *store) |
| 1191 | { |
| 1192 | int ret = 0; |
| 1193 | X509_CRL *crl = NULL; |
| 1194 | BIO *bio = NULL; |
| 1195 | |
| 1196 | /* try DER first */ |
| 1197 | crl = d2i_X509_CRL(NULL, (const unsigned char **) &downloaded->data, downloaded->size); |
| 1198 | if (crl) { |
| 1199 | /* it was DER */ |
| 1200 | goto ok; |
| 1201 | } |
| 1202 | |
| 1203 | /* DER failed, try PEM next */ |
| 1204 | bio = BIO_new_mem_buf(downloaded->data, downloaded->size); |
| 1205 | if (!bio) { |
| 1206 | ERR(session, "Creating new bio failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1207 | ret = -1; |
| 1208 | goto cleanup; |
| 1209 | } |
| 1210 | |
| 1211 | /* try to parse PEM from the downloaded data */ |
| 1212 | crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL); |
| 1213 | if (!crl) { |
| 1214 | ERR(session, "Reading downloaded CRL failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1215 | ret = -1; |
| 1216 | goto cleanup; |
| 1217 | } |
| 1218 | |
| 1219 | ok: |
| 1220 | /* we obtained the CRL, now add it to the CRL store */ |
| 1221 | ret = X509_STORE_add_crl(store, crl); |
| 1222 | if (!ret) { |
| 1223 | ERR(session, "Error adding CRL to store (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1224 | ret = -1; |
| 1225 | goto cleanup; |
| 1226 | } |
| 1227 | /* ok */ |
| 1228 | ret = 0; |
| 1229 | |
| 1230 | cleanup: |
| 1231 | X509_CRL_free(crl); |
| 1232 | BIO_free(bio); |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
| 1236 | static int |
| 1237 | nc_server_tls_crl_url(struct nc_session *session, const char *url, X509_STORE *store) |
| 1238 | { |
| 1239 | int ret = 0; |
| 1240 | CURL *handle = NULL; |
| 1241 | struct nc_curl_data downloaded = {0}; |
| 1242 | |
| 1243 | /* init curl */ |
| 1244 | ret = nc_server_tls_curl_init(session, &handle, &downloaded); |
| 1245 | if (ret) { |
| 1246 | goto cleanup; |
| 1247 | } |
| 1248 | |
| 1249 | VRB(session, "Downloading CRL from \"%s\".", url); |
| 1250 | |
| 1251 | /* download the CRL */ |
| 1252 | ret = nc_server_tls_curl_fetch(session, handle, url); |
| 1253 | if (ret) { |
| 1254 | goto cleanup; |
| 1255 | } |
| 1256 | |
| 1257 | /* convert the downloaded data to CRL and add it to the store */ |
| 1258 | ret = nc_server_tls_add_crl_to_store(session, &downloaded, store); |
| 1259 | if (ret) { |
| 1260 | goto cleanup; |
| 1261 | } |
| 1262 | |
| 1263 | cleanup: |
| 1264 | curl_easy_cleanup(handle); |
| 1265 | return ret; |
| 1266 | } |
| 1267 | |
| 1268 | static int |
| 1269 | nc_server_tls_crl_cert_ext(struct nc_session *session, X509_STORE *cert_store, X509_STORE *crl_store) |
| 1270 | { |
| 1271 | int ret = 0, i, j, k, gtype; |
| 1272 | CURL *handle = NULL; |
| 1273 | struct nc_curl_data downloaded = {0}; |
| 1274 | |
| 1275 | STACK_OF(X509_OBJECT) * objs; |
| 1276 | X509_OBJECT *obj; |
| 1277 | X509 *cert; |
| 1278 | |
| 1279 | STACK_OF(DIST_POINT) * dist_points; |
| 1280 | DIST_POINT *dist_point; |
| 1281 | GENERAL_NAMES *general_names; |
| 1282 | GENERAL_NAME *general_name; |
| 1283 | ASN1_STRING *asn_string_uri; |
| 1284 | const char *crl_distpoint_uri; |
| 1285 | |
| 1286 | /* init curl */ |
| 1287 | ret = nc_server_tls_curl_init(session, &handle, &downloaded); |
| 1288 | if (ret) { |
| 1289 | goto cleanup; |
| 1290 | } |
| 1291 | |
| 1292 | /* treat all entries in the cert_store as X509_OBJECTs */ |
| 1293 | objs = X509_STORE_get0_objects(cert_store); |
| 1294 | if (!objs) { |
| 1295 | ERR(session, "Getting certificates from store failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1296 | ret = -1; |
| 1297 | goto cleanup; |
| 1298 | } |
| 1299 | |
| 1300 | /* iterate over all the CAs */ |
| 1301 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 1302 | obj = sk_X509_OBJECT_value(objs, i); |
| 1303 | cert = X509_OBJECT_get0_X509(obj); |
| 1304 | if (!cert) { |
| 1305 | /* the object on this index was not a certificate */ |
| 1306 | continue; |
| 1307 | } |
| 1308 | |
| 1309 | /* get all the distribution points for this CA */ |
| 1310 | dist_points = X509_get_ext_d2i(cert, NID_crl_distribution_points, NULL, NULL); |
| 1311 | |
| 1312 | /* iterate over all the dist points (there can be multiple for a single cert) */ |
| 1313 | for (j = 0; j < sk_DIST_POINT_num(dist_points); j++) { |
| 1314 | dist_point = sk_DIST_POINT_value(dist_points, j); |
| 1315 | if (!dist_point) { |
| 1316 | continue; |
| 1317 | } |
| 1318 | general_names = dist_point->distpoint->name.fullname; |
| 1319 | |
| 1320 | /* iterate over all the GeneralesNames in the distribution point */ |
| 1321 | for (k = 0; k < sk_GENERAL_NAME_num(general_names); k++) { |
| 1322 | general_name = sk_GENERAL_NAME_value(general_names, k); |
| 1323 | asn_string_uri = GENERAL_NAME_get0_value(general_name, >ype); |
| 1324 | |
| 1325 | /* check if the general name is a URI and has a valid length */ |
| 1326 | if ((gtype != GEN_URI) || (ASN1_STRING_length(asn_string_uri) <= 6)) { |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
| 1330 | crl_distpoint_uri = (const char *) ASN1_STRING_get0_data(asn_string_uri); |
| 1331 | |
| 1332 | VRB(session, "Downloading CRL from \"%s\".", crl_distpoint_uri); |
| 1333 | |
| 1334 | /* download the CRL */ |
| 1335 | ret = nc_server_tls_curl_fetch(session, handle, crl_distpoint_uri); |
| 1336 | if (ret) { |
| 1337 | /* failed to download the CRL from this entry, try th next */ |
| 1338 | continue; |
| 1339 | } |
| 1340 | |
| 1341 | /* convert the downloaded data to CRL and add it to the store */ |
| 1342 | ret = nc_server_tls_add_crl_to_store(session, &downloaded, crl_store); |
| 1343 | if (ret) { |
| 1344 | goto cleanup; |
| 1345 | } |
| 1346 | |
| 1347 | /* the CRL was downloaded, no need to download it again using different protocol */ |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | cleanup: |
| 1354 | curl_easy_cleanup(handle); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
| 1358 | static int |
| 1359 | nc_tls_store_set_crl(struct nc_session *session, struct nc_server_tls_opts *opts, X509_STORE *store) |
| 1360 | { |
| 1361 | if (!opts->crl_store) { |
| 1362 | /* first call on this endpoint */ |
| 1363 | opts->crl_store = X509_STORE_new(); |
| 1364 | if (!opts->crl_store) { |
| 1365 | ERRMEM; |
| 1366 | goto fail; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | if (opts->crl_path) { |
| 1371 | if (nc_server_tls_crl_path(session, opts->crl_path, opts->crl_store)) { |
| 1372 | goto fail; |
| 1373 | } |
| 1374 | } else if (opts->crl_url) { |
| 1375 | if (nc_server_tls_crl_url(session, opts->crl_url, opts->crl_store)) { |
| 1376 | goto fail; |
| 1377 | } |
| 1378 | } else { |
| 1379 | if (nc_server_tls_crl_cert_ext(session, store, opts->crl_store)) { |
| 1380 | goto fail; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | return 0; |
| 1385 | |
| 1386 | fail: |
| 1387 | return -1; |
| 1388 | } |
| 1389 | |
| 1390 | static int |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1391 | nc_server_tls_accept_check(int accept_ret, struct nc_session *session) |
| 1392 | { |
| 1393 | int verify; |
| 1394 | |
| 1395 | /* check certificate verification result */ |
| 1396 | verify = SSL_get_verify_result(session->ti.tls); |
| 1397 | switch (verify) { |
| 1398 | case X509_V_OK: |
| 1399 | if (accept_ret == 1) { |
| 1400 | VRB(session, "Client certificate verified."); |
| 1401 | } |
| 1402 | break; |
| 1403 | default: |
| 1404 | ERR(session, "Client certificate error (%s).", X509_verify_cert_error_string(verify)); |
| 1405 | } |
| 1406 | |
| 1407 | if (accept_ret != 1) { |
| 1408 | switch (SSL_get_error(session->ti.tls, accept_ret)) { |
| 1409 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1410 | ERR(session, "SSL accept failed (%s).", strerror(errno)); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1411 | break; |
| 1412 | case SSL_ERROR_SSL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1413 | ERR(session, "SSL accept failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | default: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1416 | ERR(session, "SSL accept failed."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1417 | break; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | return accept_ret; |
| 1422 | } |
| 1423 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1424 | int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1425 | nc_accept_tls_session(struct nc_session *session, struct nc_server_tls_opts *opts, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1426 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1427 | X509_STORE *cert_store; |
| 1428 | SSL_CTX *tls_ctx; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1429 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1430 | struct timespec ts_timeout; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1431 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1432 | /* SSL_CTX */ |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 1433 | tls_ctx = SSL_CTX_new(TLS_server_method()); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1434 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1435 | if (!tls_ctx) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1436 | ERR(session, "Failed to create TLS context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1437 | goto error; |
| 1438 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1439 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1440 | SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1441 | if (nc_tls_ctx_set_server_cert_key(tls_ctx, opts)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1442 | goto error; |
| 1443 | } |
| 1444 | |
| 1445 | /* X509_STORE, managed (freed) with the context */ |
| 1446 | cert_store = X509_STORE_new(); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1447 | if (!cert_store) { |
| 1448 | ERR(session, "Creating certificate store failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1449 | goto error; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1452 | /* set end-entity certs as cert store data, retrieve them if verification fails later */ |
| 1453 | ret = X509_STORE_set_ex_data(cert_store, 1, &opts->ee_certs); |
| 1454 | if (!ret) { |
| 1455 | ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 1456 | goto error; |
| 1457 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1458 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1459 | /* do the same for referenced endpoint's end entity certs */ |
| 1460 | if (opts->endpt_client_ref) { |
| 1461 | ret = X509_STORE_set_ex_data(cert_store, 2, &opts->endpt_client_ref->opts.tls->ee_certs); |
| 1462 | if (!ret) { |
| 1463 | ERR(session, "Setting certificate store data failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1464 | goto error; |
| 1465 | } |
| 1466 | } |
| 1467 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1468 | /* set store to the context */ |
| 1469 | SSL_CTX_set_cert_store(tls_ctx, cert_store); |
| 1470 | |
| 1471 | /* set certificate authority certs */ |
| 1472 | if (nc_tls_store_set_trusted_certs(cert_store, &opts->ca_certs)) { |
| 1473 | goto error; |
| 1474 | } |
| 1475 | |
| 1476 | /* set referenced endpoint's CA certs if set */ |
| 1477 | if (opts->endpt_client_ref) { |
| 1478 | if (nc_tls_store_set_trusted_certs(cert_store, &opts->endpt_client_ref->opts.tls->ca_certs)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1479 | goto error; |
| 1480 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1481 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1482 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1483 | /* set Certificate Revocation List if configured */ |
| 1484 | if (opts->crl_path || opts->crl_url || opts->crl_cert_ext) { |
| 1485 | if (nc_tls_store_set_crl(session, opts, cert_store)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1486 | goto error; |
| 1487 | } |
| 1488 | } |
| 1489 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1490 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1491 | session->ti.tls = SSL_new(tls_ctx); |
| 1492 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1493 | /* context can be freed already, trusted certs must be freed manually */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1494 | SSL_CTX_free(tls_ctx); |
| 1495 | tls_ctx = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1496 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1497 | if (!session->ti.tls) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1498 | ERR(session, "Failed to create TLS structure from context."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1499 | goto error; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1500 | } |
| 1501 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1502 | /* set TLS versions for the current SSL session */ |
| 1503 | if (opts->tls_versions) { |
| 1504 | if (!(opts->tls_versions & NC_TLS_VERSION_10)) { |
| 1505 | SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1); |
| 1506 | } |
| 1507 | if (!(opts->tls_versions & NC_TLS_VERSION_11)) { |
| 1508 | SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_1); |
| 1509 | } |
| 1510 | if (!(opts->tls_versions & NC_TLS_VERSION_12)) { |
| 1511 | SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_2); |
| 1512 | } |
| 1513 | if (!(opts->tls_versions & NC_TLS_VERSION_13)) { |
| 1514 | SSL_set_options(session->ti.tls, SSL_OP_NO_TLSv1_3); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | /* set TLS cipher suites */ |
| 1519 | if (opts->ciphers) { |
| 1520 | /* set for TLS1.2 and lower */ |
| 1521 | SSL_set_cipher_list(session->ti.tls, opts->ciphers); |
| 1522 | /* set for TLS1.3 */ |
| 1523 | SSL_set_ciphersuites(session->ti.tls, opts->ciphers); |
| 1524 | } |
| 1525 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1526 | SSL_set_fd(session->ti.tls, sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1527 | sock = -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1528 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 1529 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1530 | /* store session on per-thread basis */ |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1531 | pthread_once(&verify_once, nc_tls_make_verify_key); |
| 1532 | pthread_setspecific(verify_key, session); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1533 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1534 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1535 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1536 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1537 | while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) { |
| 1538 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1539 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 1540 | ERR(session, "SSL accept timeout."); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1541 | return 0; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1542 | } |
| 1543 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 1544 | if (nc_server_tls_accept_check(ret, session) != 1) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1545 | return -1; |
| 1546 | } |
| 1547 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1548 | return 1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1549 | |
| 1550 | error: |
| 1551 | if (sock > -1) { |
| 1552 | close(sock); |
| 1553 | } |
| 1554 | SSL_CTX_free(tls_ctx); |
| 1555 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1556 | } |