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