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