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