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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 23 | #define _GNU_SOURCE |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <poll.h> |
| 27 | |
| 28 | #include <openssl/ssl.h> |
| 29 | #include <openssl/evp.h> |
| 30 | #include <openssl/err.h> |
| 31 | #include <openssl/x509v3.h> |
| 32 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 33 | #include "libnetconf.h" |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 34 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 35 | extern struct nc_server_opts server_opts; |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 36 | struct nc_tls_server_opts tls_opts = { |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 37 | .tls_ctx_lock = PTHREAD_MUTEX_INITIALIZER, |
| 38 | .crl_lock = PTHREAD_MUTEX_INITIALIZER, |
| 39 | .ctn_lock = PTHREAD_MUTEX_INITIALIZER, |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 40 | .verify_once = PTHREAD_ONCE_INIT |
| 41 | }; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 42 | |
| 43 | static char * |
| 44 | asn1time_to_str(ASN1_TIME *t) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 46 | char *cp; |
| 47 | BIO *bio; |
| 48 | int n; |
Radek Krejci | 5da708a | 2015-09-01 17:33:23 +0200 | [diff] [blame] | 49 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 50 | if (!t) { |
| 51 | return NULL; |
| 52 | } |
| 53 | bio = BIO_new(BIO_s_mem()); |
| 54 | if (!bio) { |
| 55 | return NULL; |
| 56 | } |
| 57 | ASN1_TIME_print(bio, t); |
| 58 | n = BIO_pending(bio); |
| 59 | cp = malloc(n + 1); |
| 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); |
| 77 | for (i = 0; i < dig_len - 1; ++i) { |
| 78 | sprintf((*str) + (i * 3), "%02x:", digest[i]); |
| 79 | } |
| 80 | sprintf((*str) + (i * 3), "%02x", digest[i]); |
| 81 | } |
| 82 | |
| 83 | /* return NULL - SSL error can be retrieved */ |
| 84 | static X509 * |
| 85 | base64der_to_cert(const char *in) |
| 86 | { |
| 87 | X509 *out; |
| 88 | char *buf; |
| 89 | BIO *bio; |
| 90 | |
| 91 | if (in == NULL) { |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | if (asprintf(&buf, "%s%s%s", "-----BEGIN CERTIFICATE-----\n", in, "\n-----END CERTIFICATE-----") == -1) { |
| 96 | return NULL; |
| 97 | } |
| 98 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 99 | if (!bio) { |
| 100 | free(buf); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | out = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 105 | if (!out) { |
| 106 | free(buf); |
| 107 | BIO_free(bio); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | free(buf); |
| 112 | BIO_free(bio); |
| 113 | return out; |
| 114 | } |
| 115 | |
| 116 | /* return NULL - either errno or SSL error */ |
| 117 | static X509 * |
| 118 | pem_to_cert(const char *path) |
| 119 | { |
| 120 | FILE *fp; |
| 121 | X509 *out; |
| 122 | |
| 123 | fp = fopen(path, "r"); |
| 124 | if (!fp) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | out = PEM_read_X509(fp, NULL, NULL, NULL); |
| 129 | fclose(fp); |
| 130 | return out; |
| 131 | } |
| 132 | |
| 133 | static EVP_PKEY * |
| 134 | base64der_to_privatekey(const char *in, int rsa) |
| 135 | { |
| 136 | EVP_PKEY *out; |
| 137 | char *buf; |
| 138 | BIO *bio; |
| 139 | |
| 140 | if (in == NULL) { |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | 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) { |
| 145 | return NULL; |
| 146 | } |
| 147 | bio = BIO_new_mem_buf(buf, strlen(buf)); |
| 148 | if (!bio) { |
| 149 | free(buf); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | out = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 154 | if (!out) { |
| 155 | free(buf); |
| 156 | BIO_free(bio); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | free(buf); |
| 161 | BIO_free(bio); |
| 162 | return out; |
| 163 | } |
| 164 | |
| 165 | static int |
| 166 | cert_pubkey_match(X509 *cert1, X509 *cert2) |
| 167 | { |
| 168 | ASN1_BIT_STRING *bitstr1, *bitstr2; |
| 169 | |
| 170 | bitstr1 = X509_get0_pubkey_bitstr(cert1); |
| 171 | bitstr2 = X509_get0_pubkey_bitstr(cert2); |
| 172 | |
| 173 | if (!bitstr1 || !bitstr2 || (bitstr1->length != bitstr2->length) || |
| 174 | memcmp(bitstr1->data, bitstr2->data, bitstr1->length)) { |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | nc_tls_ctn_get_username_from_cert(X509 *client_cert, NC_TLS_CTN_MAPTYPE map_type, char **username) |
| 183 | { |
| 184 | STACK_OF(GENERAL_NAME) *san_names; |
| 185 | GENERAL_NAME *san_name; |
| 186 | ASN1_OCTET_STRING *ip; |
| 187 | int i, san_count; |
| 188 | char *subject, *common_name; |
| 189 | |
| 190 | if (map_type == NC_TLS_CTN_COMMON_NAME) { |
| 191 | subject = X509_NAME_oneline(X509_get_subject_name(client_cert), NULL, 0); |
| 192 | common_name = strstr(subject, "CN="); |
| 193 | if (!common_name) { |
| 194 | WRN("%s: cert does not include the commonName field", __func__); |
| 195 | free(subject); |
| 196 | return 1; |
| 197 | } |
| 198 | common_name += 3; |
| 199 | if (strchr(common_name, '/')) { |
| 200 | *strchr(common_name, '/') = '\0'; |
| 201 | } |
| 202 | *username = strdup(common_name); |
| 203 | free(subject); |
| 204 | } else { |
| 205 | /* retrieve subjectAltName's rfc822Name (email), dNSName and iPAddress values */ |
| 206 | san_names = X509_get_ext_d2i(client_cert, NID_subject_alt_name, NULL, NULL); |
| 207 | if (!san_names) { |
| 208 | WRN("%s: cert has no SANs or failed to retrieve them", __func__); |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | san_count = sk_GENERAL_NAME_num(san_names); |
| 213 | for (i = 0; i < san_count; ++i) { |
| 214 | san_name = sk_GENERAL_NAME_value(san_names, i); |
| 215 | |
| 216 | /* rfc822Name (email) */ |
| 217 | if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_RFC822_NAME) && |
| 218 | san_name->type == GEN_EMAIL) { |
| 219 | *username = strdup((char *)ASN1_STRING_data(san_name->d.rfc822Name)); |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | /* dNSName */ |
| 224 | if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_DNS_NAME) && |
| 225 | san_name->type == GEN_DNS) { |
| 226 | *username = strdup((char *)ASN1_STRING_data(san_name->d.dNSName)); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | /* iPAddress */ |
| 231 | if ((map_type == NC_TLS_CTN_SAN_ANY || map_type == NC_TLS_CTN_SAN_IP_ADDRESS) && |
| 232 | san_name->type == GEN_IPADD) { |
| 233 | ip = san_name->d.iPAddress; |
| 234 | if (ip->length == 4) { |
| 235 | if (asprintf(username, "%d.%d.%d.%d", ip->data[0], ip->data[1], ip->data[2], ip->data[3]) == -1) { |
| 236 | ERR("%s: asprintf() failed", __func__); |
| 237 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 238 | return -1; |
| 239 | } |
| 240 | break; |
| 241 | } else if (ip->length == 16) { |
| 242 | if (asprintf(username, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
| 243 | ip->data[0], ip->data[1], ip->data[2], ip->data[3], ip->data[4], ip->data[5], |
| 244 | ip->data[6], ip->data[7], ip->data[8], ip->data[9], ip->data[10], ip->data[11], |
| 245 | ip->data[12], ip->data[13], ip->data[14], ip->data[15]) == -1) { |
| 246 | ERR("%s: asprintf() failed", __func__); |
| 247 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 248 | return -1; |
| 249 | } |
| 250 | break; |
| 251 | } else { |
| 252 | WRN("%s: SAN IP address in an unknown format (length is %d)", __func__, ip->length); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); |
| 257 | |
| 258 | if (i < san_count) { |
| 259 | switch (map_type) { |
| 260 | case NC_TLS_CTN_SAN_RFC822_NAME: |
| 261 | WRN("%s: cert does not include the SAN rfc822Name field", __func__); |
| 262 | break; |
| 263 | case NC_TLS_CTN_SAN_DNS_NAME: |
| 264 | WRN("%s: cert does not include the SAN dNSName field", __func__); |
| 265 | break; |
| 266 | case NC_TLS_CTN_SAN_IP_ADDRESS: |
| 267 | WRN("%s: cert does not include the SAN iPAddress field", __func__); |
| 268 | break; |
| 269 | case NC_TLS_CTN_SAN_ANY: |
| 270 | WRN("%s: cert does not include any relevant SAN fields", __func__); |
| 271 | break; |
| 272 | default: |
| 273 | break; |
| 274 | } |
| 275 | return 1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* return: 0 - OK, 1 - no match, -1 - error */ |
| 283 | static int |
| 284 | nc_tls_cert_to_name(X509 *cert, NC_TLS_CTN_MAPTYPE *map_type, const char **name) |
| 285 | { |
| 286 | char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL; |
| 287 | char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL; |
| 288 | uint16_t i; |
| 289 | unsigned char *buf = malloc(64); |
| 290 | unsigned int buf_len = 64; |
| 291 | int ret = 0; |
| 292 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 293 | if (!cert || !map_type || !name) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 294 | free(buf); |
| 295 | return -1; |
| 296 | } |
| 297 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 298 | /* LOCK */ |
| 299 | pthread_mutex_lock(&tls_opts.ctn_lock); |
| 300 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 301 | for (i = 0; i < tls_opts.ctn_count; ++i) { |
| 302 | /* MD5 */ |
| 303 | if (!strncmp(tls_opts.ctn[i].fingerprint, "01", 2)) { |
| 304 | if (!digest_md5) { |
| 305 | if (X509_digest(cert, EVP_md5(), buf, &buf_len) != 1) { |
| 306 | ERR("%s: calculating MD5 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 307 | ret = -1; |
| 308 | goto cleanup; |
| 309 | } |
| 310 | digest_to_str(buf, buf_len, &digest_md5); |
| 311 | } |
| 312 | |
| 313 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_md5)) { |
| 314 | /* we got ourselves a winner! */ |
| 315 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 316 | *map_type = tls_opts.ctn[i].map_type; |
| 317 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 318 | *name = tls_opts.ctn[i].name; |
| 319 | } |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | /* SHA-1 */ |
| 324 | } else if (!strncmp(tls_opts.ctn[i].fingerprint, "02", 2)) { |
| 325 | if (!digest_sha1) { |
| 326 | if (X509_digest(cert, EVP_sha1(), buf, &buf_len) != 1) { |
| 327 | ERR("%s: calculating SHA-1 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 328 | ret = -1; |
| 329 | goto cleanup; |
| 330 | } |
| 331 | digest_to_str(buf, buf_len, &digest_sha1); |
| 332 | } |
| 333 | |
| 334 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha1)) { |
| 335 | /* we got ourselves a winner! */ |
| 336 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 337 | *map_type = tls_opts.ctn[i].map_type; |
| 338 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 339 | *name = tls_opts.ctn[i].name; |
| 340 | } |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | /* SHA-224 */ |
| 345 | } else if (!strncmp(tls_opts.ctn[i].fingerprint, "03", 2)) { |
| 346 | if (!digest_sha224) { |
| 347 | if (X509_digest(cert, EVP_sha224(), buf, &buf_len) != 1) { |
| 348 | ERR("%s: calculating SHA-224 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 349 | ret = -1; |
| 350 | goto cleanup; |
| 351 | } |
| 352 | digest_to_str(buf, buf_len, &digest_sha224); |
| 353 | } |
| 354 | |
| 355 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha224)) { |
| 356 | /* we got ourselves a winner! */ |
| 357 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 358 | *map_type = tls_opts.ctn[i].map_type; |
| 359 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 360 | *name = tls_opts.ctn[i].name; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | /* SHA-256 */ |
| 366 | } else if (!strncmp(tls_opts.ctn[i].fingerprint, "04", 2)) { |
| 367 | if (!digest_sha256) { |
| 368 | if (X509_digest(cert, EVP_sha256(), buf, &buf_len) != 1) { |
| 369 | ERR("%s: calculating SHA-256 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 370 | ret = -1; |
| 371 | goto cleanup; |
| 372 | } |
| 373 | digest_to_str(buf, buf_len, &digest_sha256); |
| 374 | } |
| 375 | |
| 376 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha256)) { |
| 377 | /* we got ourselves a winner! */ |
| 378 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 379 | *map_type = tls_opts.ctn[i].map_type; |
| 380 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 381 | *name = tls_opts.ctn[i].name; |
| 382 | } |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | /* SHA-384 */ |
| 387 | } else if (!strncmp(tls_opts.ctn[i].fingerprint, "05", 2)) { |
| 388 | if (!digest_sha384) { |
| 389 | if (X509_digest(cert, EVP_sha384(), buf, &buf_len) != 1) { |
| 390 | ERR("%s: calculating SHA-384 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 391 | ret = -1; |
| 392 | goto cleanup; |
| 393 | } |
| 394 | digest_to_str(buf, buf_len, &digest_sha384); |
| 395 | } |
| 396 | |
| 397 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha384)) { |
| 398 | /* we got ourselves a winner! */ |
| 399 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 400 | *map_type = tls_opts.ctn[i].map_type; |
| 401 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 402 | *name = tls_opts.ctn[i].name; |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | /* SHA-512 */ |
| 408 | } else if (!strncmp(tls_opts.ctn[i].fingerprint, "06", 2)) { |
| 409 | if (!digest_sha512) { |
| 410 | if (X509_digest(cert, EVP_sha512(), buf, &buf_len) != 1) { |
| 411 | ERR("%s: calculating SHA-512 digest: %s", __func__, ERR_reason_error_string(ERR_get_error())); |
| 412 | ret = -1; |
| 413 | goto cleanup; |
| 414 | } |
| 415 | digest_to_str(buf, buf_len, &digest_sha512); |
| 416 | } |
| 417 | |
| 418 | if (!strcasecmp(tls_opts.ctn[i].fingerprint + 3, digest_sha512)) { |
| 419 | /* we got ourselves a winner! */ |
| 420 | VRB("Cert verify CTN: entry with a matching fingerprint found"); |
| 421 | *map_type = tls_opts.ctn[i].map_type; |
| 422 | if (tls_opts.ctn[i].map_type == NC_TLS_CTN_SPECIFIED) { |
| 423 | *name = tls_opts.ctn[i].name; |
| 424 | } |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | /* unknown */ |
| 429 | } else { |
| 430 | WRN("%s: unknown fingerprint algorithm used (%s), skipping", __func__, tls_opts.ctn[i].fingerprint); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (i == tls_opts.ctn_count) { |
| 435 | ret = 1; |
| 436 | } |
| 437 | |
| 438 | cleanup: |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 439 | /* UNLOCK */ |
| 440 | pthread_mutex_unlock(&tls_opts.ctn_lock); |
| 441 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 442 | free(digest_md5); |
| 443 | free(digest_sha1); |
| 444 | free(digest_sha224); |
| 445 | free(digest_sha256); |
| 446 | free(digest_sha384); |
| 447 | free(digest_sha512); |
| 448 | free(buf); |
| 449 | return ret; |
| 450 | } |
| 451 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 452 | static int |
| 453 | nc_tlsclb_verify(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 454 | { |
| 455 | X509_STORE_CTX store_ctx; |
| 456 | X509_OBJECT obj; |
| 457 | X509_NAME *subject; |
| 458 | X509_NAME *issuer; |
| 459 | X509 *cert; |
| 460 | X509_CRL *crl; |
| 461 | X509_REVOKED *revoked; |
| 462 | STACK_OF(X509) *cert_stack; |
| 463 | EVP_PKEY *pubkey; |
| 464 | struct nc_session* session; |
| 465 | long serial; |
| 466 | int i, n, rc, depth; |
| 467 | char *cp; |
| 468 | const char *username = NULL; |
| 469 | NC_TLS_CTN_MAPTYPE map_type = 0; |
| 470 | ASN1_TIME *last_update = NULL, *next_update = NULL; |
| 471 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 472 | /* get the thread session */ |
| 473 | session = pthread_getspecific(tls_opts.verify_key); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 474 | |
| 475 | /* get the last certificate, that is the peer (client) certificate */ |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 476 | if (!session->tls_cert) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 477 | cert_stack = X509_STORE_CTX_get1_chain(x509_ctx); |
| 478 | /* TODO all that is needed, but function X509_up_ref not present in older OpenSSL versions |
| 479 | session->cert = sk_X509_value(cert_stack, sk_X509_num(cert_stack) - 1); |
| 480 | X509_up_ref(session->cert); |
| 481 | sk_X509_pop_free(cert_stack, X509_free); */ |
| 482 | while ((cert = sk_X509_pop(cert_stack))) { |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 483 | X509_free(session->tls_cert); |
| 484 | session->tls_cert = cert; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 485 | } |
| 486 | sk_X509_pop_free(cert_stack, X509_free); |
| 487 | } |
| 488 | |
| 489 | /* standard certificate verification failed, so a trusted client cert must match to continue */ |
| 490 | if (!preverify_ok) { |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 491 | subject = X509_get_subject_name(session->tls_cert); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 492 | cert_stack = X509_STORE_get1_certs(x509_ctx, subject); |
| 493 | if (cert_stack) { |
| 494 | for (i = 0; i < sk_X509_num(cert_stack); ++i) { |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 495 | if (cert_pubkey_match(session->tls_cert, sk_X509_value(cert_stack, i))) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 496 | /* we are just overriding the failed standard certificate verification (preverify_ok == 0), |
| 497 | * 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] | 498 | VRB("Cert verify: fail (%s), but the client certificate is trusted, continuing.", |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 499 | X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
| 500 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
| 501 | sk_X509_pop_free(cert_stack, X509_free); |
| 502 | return 1; |
| 503 | } |
| 504 | } |
| 505 | sk_X509_pop_free(cert_stack, X509_free); |
| 506 | } |
| 507 | |
| 508 | ERR("Cert verify: fail (%s).", X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* print cert verify info */ |
| 513 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
| 514 | VRB("Cert verify: depth %d", depth); |
| 515 | |
| 516 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 517 | subject = X509_get_subject_name(cert); |
| 518 | issuer = X509_get_issuer_name(cert); |
| 519 | |
| 520 | cp = X509_NAME_oneline(subject, NULL, 0); |
| 521 | VRB("Cert verify: subject: %s", cp); |
| 522 | OPENSSL_free(cp); |
| 523 | cp = X509_NAME_oneline(issuer, NULL, 0); |
| 524 | VRB("Cert verify: issuer: %s", cp); |
| 525 | OPENSSL_free(cp); |
| 526 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 527 | /* LOCK */ |
| 528 | pthread_mutex_lock(&tls_opts.crl_lock); |
| 529 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 530 | /* check for revocation if set */ |
| 531 | if (tls_opts.crl_store) { |
| 532 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 533 | * the current certificate in order to verify it's integrity */ |
| 534 | memset((char *)&obj, 0, sizeof(obj)); |
| 535 | X509_STORE_CTX_init(&store_ctx, tls_opts.crl_store, NULL, NULL); |
| 536 | rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj); |
| 537 | X509_STORE_CTX_cleanup(&store_ctx); |
| 538 | crl = obj.data.crl; |
| 539 | if (rc > 0 && crl) { |
| 540 | cp = X509_NAME_oneline(subject, NULL, 0); |
| 541 | VRB("Cert verify CRL: issuer: %s", cp); |
| 542 | OPENSSL_free(cp); |
| 543 | |
| 544 | last_update = X509_CRL_get_lastUpdate(crl); |
| 545 | next_update = X509_CRL_get_nextUpdate(crl); |
| 546 | cp = asn1time_to_str(last_update); |
| 547 | VRB("Cert verify CRL: last update: %s", cp); |
| 548 | free(cp); |
| 549 | cp = asn1time_to_str(next_update); |
| 550 | VRB("Cert verify CRL: next update: %s", cp); |
| 551 | free(cp); |
| 552 | |
| 553 | /* verify the signature on this CRL */ |
| 554 | pubkey = X509_get_pubkey(cert); |
| 555 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
| 556 | ERR("Cert verify CRL: invalid signature."); |
| 557 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 558 | X509_OBJECT_free_contents(&obj); |
| 559 | if (pubkey) { |
| 560 | EVP_PKEY_free(pubkey); |
| 561 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 562 | /* UNLOCK */ |
| 563 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | if (pubkey) { |
| 567 | EVP_PKEY_free(pubkey); |
| 568 | } |
| 569 | |
| 570 | /* check date of CRL to make sure it's not expired */ |
| 571 | if (!next_update) { |
| 572 | ERR("Cert verify CRL: invalid nextUpdate field."); |
| 573 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 574 | X509_OBJECT_free_contents(&obj); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 575 | /* UNLOCK */ |
| 576 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | if (X509_cmp_current_time(next_update) < 0) { |
| 580 | ERR("Cert verify CRL: expired - revoking all certificates."); |
| 581 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 582 | X509_OBJECT_free_contents(&obj); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 583 | /* UNLOCK */ |
| 584 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | X509_OBJECT_free_contents(&obj); |
| 588 | } |
| 589 | |
| 590 | /* try to retrieve a CRL corresponding to the _issuer_ of |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 591 | * the current certificate in order to check for revocation */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 592 | memset((char *)&obj, 0, sizeof(obj)); |
| 593 | X509_STORE_CTX_init(&store_ctx, tls_opts.crl_store, NULL, NULL); |
| 594 | rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj); |
| 595 | X509_STORE_CTX_cleanup(&store_ctx); |
| 596 | crl = obj.data.crl; |
| 597 | if (rc > 0 && crl) { |
| 598 | /* check if the current certificate is revoked by this CRL */ |
| 599 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 600 | for (i = 0; i < n; i++) { |
| 601 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 602 | if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) { |
| 603 | serial = ASN1_INTEGER_get(revoked->serialNumber); |
| 604 | cp = X509_NAME_oneline(issuer, NULL, 0); |
| 605 | ERR("Cert verify CRL: certificate with serial %ld (0x%lX) revoked per CRL from issuer %s", serial, serial, cp); |
| 606 | OPENSSL_free(cp); |
| 607 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 608 | X509_OBJECT_free_contents(&obj); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 609 | /* UNLOCK */ |
| 610 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | } |
| 614 | X509_OBJECT_free_contents(&obj); |
| 615 | } |
| 616 | } |
| 617 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 618 | /* UNLOCK */ |
| 619 | pthread_mutex_unlock(&tls_opts.crl_lock); |
| 620 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 621 | /* cert-to-name already successful */ |
| 622 | if (session->username) { |
| 623 | return 1; |
| 624 | } |
| 625 | |
| 626 | /* cert-to-name */ |
| 627 | rc = nc_tls_cert_to_name(cert, &map_type, &username); |
| 628 | if (rc) { |
| 629 | if (rc == -1) { |
| 630 | /* fatal error */ |
| 631 | depth = 0; |
| 632 | } |
| 633 | /* rc == 1 is a normal CTN fail (no match found) */ |
| 634 | goto fail; |
| 635 | } |
| 636 | |
| 637 | /* cert-to-name match, now to extract the specific field from the peer cert */ |
| 638 | if (map_type == NC_TLS_CTN_SPECIFIED) { |
| 639 | session->username = lydict_insert(server_opts.ctx, username, 0); |
| 640 | } else { |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 641 | rc = nc_tls_ctn_get_username_from_cert(session->tls_cert, map_type, &cp); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 642 | if (rc) { |
| 643 | if (rc == -1) { |
| 644 | depth = 0; |
| 645 | } |
| 646 | goto fail; |
| 647 | } |
| 648 | session->username = lydict_insert_zc(server_opts.ctx, cp); |
| 649 | } |
| 650 | |
| 651 | VRB("Cert verify CTN: new client username recognized as \"%s\".", session->username); |
| 652 | return 1; |
| 653 | |
| 654 | fail: |
| 655 | if (depth > 0) { |
| 656 | VRB("Cert verify CTN: cert fail, cert-to-name will continue on the next cert in chain."); |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | VRB("Cert-to-name unsuccessful, dropping the new client."); |
| 661 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | API int |
| 666 | nc_tls_server_set_cert(const char *cert) |
| 667 | { |
| 668 | X509 *x509_cert; |
| 669 | |
| 670 | if (!cert) { |
| 671 | ERRARG; |
| 672 | return -1; |
| 673 | } |
| 674 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 675 | /* LOCK */ |
| 676 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 677 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 678 | if (!tls_opts.tls_ctx) { |
| 679 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 680 | if (!tls_opts.tls_ctx) { |
| 681 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 682 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 683 | } |
| 684 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 685 | } |
| 686 | |
| 687 | x509_cert = base64der_to_cert(cert); |
| 688 | if (!x509_cert || (SSL_CTX_use_certificate(tls_opts.tls_ctx, x509_cert) != 1)) { |
| 689 | ERR("%s: loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 690 | X509_free(x509_cert); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 691 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 692 | } |
| 693 | X509_free(x509_cert); |
| 694 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 695 | /* UNLOCK */ |
| 696 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 697 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 698 | |
| 699 | fail: |
| 700 | /* UNLOCK */ |
| 701 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 702 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 705 | API int |
| 706 | nc_tls_server_set_cert_path(const char *cert_path) |
| 707 | { |
| 708 | if (!cert_path) { |
| 709 | ERRARG; |
| 710 | return -1; |
| 711 | } |
| 712 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 713 | /* LOCK */ |
| 714 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 715 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 716 | if (!tls_opts.tls_ctx) { |
| 717 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 718 | if (!tls_opts.tls_ctx) { |
| 719 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 720 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 721 | } |
| 722 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 723 | } |
| 724 | |
| 725 | if (SSL_CTX_use_certificate_file(tls_opts.tls_ctx, cert_path, SSL_FILETYPE_PEM) != 1) { |
| 726 | ERR("%s: loading the server certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 727 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 730 | /* UNLOCK */ |
| 731 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 732 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 733 | |
| 734 | fail: |
| 735 | /* UNLOCK */ |
| 736 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 737 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | API int |
| 741 | nc_tls_server_set_key(const char *privkey, int is_rsa) |
| 742 | { |
| 743 | EVP_PKEY *key;; |
| 744 | |
| 745 | if (!privkey) { |
| 746 | ERRARG; |
| 747 | return -1; |
| 748 | } |
| 749 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 750 | /* LOCK */ |
| 751 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 752 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 753 | if (!tls_opts.tls_ctx) { |
| 754 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 755 | if (!tls_opts.tls_ctx) { |
| 756 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 757 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 758 | } |
| 759 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 760 | } |
| 761 | |
| 762 | key = base64der_to_privatekey(privkey, is_rsa); |
| 763 | if (!key || (SSL_CTX_use_PrivateKey(tls_opts.tls_ctx, key) != 1)) { |
| 764 | ERR("%s: loading the server private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 765 | EVP_PKEY_free(key); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 766 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 767 | } |
| 768 | EVP_PKEY_free(key); |
| 769 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 770 | /* UNLOCK */ |
| 771 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 772 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 773 | |
| 774 | fail: |
| 775 | /* UNLOCK */ |
| 776 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 777 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 780 | API int |
| 781 | nc_tls_server_set_key_path(const char *privkey_path) |
| 782 | { |
| 783 | if (!privkey_path) { |
| 784 | ERRARG; |
| 785 | return -1; |
| 786 | } |
| 787 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 788 | /* LOCK */ |
| 789 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 790 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 791 | if (!tls_opts.tls_ctx) { |
| 792 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 793 | if (!tls_opts.tls_ctx) { |
| 794 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 795 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 796 | } |
| 797 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 798 | } |
| 799 | |
| 800 | if (SSL_CTX_use_PrivateKey_file(tls_opts.tls_ctx, privkey_path, SSL_FILETYPE_PEM) != 1) { |
| 801 | ERR("%s: loading the server priavte key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 802 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 805 | /* UNLOCK */ |
| 806 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 807 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 808 | |
| 809 | fail: |
| 810 | /* UNLOCK */ |
| 811 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 812 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | API int |
| 816 | nc_tls_server_add_trusted_cert(const char *cert) |
| 817 | { |
| 818 | X509_STORE *cert_store; |
| 819 | X509 *x509_cert; |
| 820 | |
| 821 | if (!cert) { |
| 822 | ERRARG; |
| 823 | return -1; |
| 824 | } |
| 825 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 826 | /* LOCK */ |
| 827 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 828 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 829 | if (!tls_opts.tls_ctx) { |
| 830 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 831 | if (!tls_opts.tls_ctx) { |
| 832 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 833 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 834 | } |
| 835 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 836 | } |
| 837 | |
| 838 | cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx); |
| 839 | if (!cert_store) { |
| 840 | cert_store = X509_STORE_new(); |
| 841 | SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store); |
| 842 | } |
| 843 | |
| 844 | x509_cert = base64der_to_cert(cert); |
| 845 | if (!x509_cert || (X509_STORE_add_cert(cert_store, x509_cert) != 1)) { |
| 846 | ERR("%s: adding a trusted certificate failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 847 | X509_free(x509_cert); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 848 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 849 | } |
| 850 | X509_free(x509_cert); |
| 851 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 852 | /* UNLOCK */ |
| 853 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 854 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 855 | |
| 856 | fail: |
| 857 | /* UNLOCK */ |
| 858 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 859 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | API int |
| 863 | nc_tls_server_add_trusted_cert_path(const char *cert_path) |
| 864 | { |
| 865 | X509_STORE *cert_store; |
| 866 | X509 *x509_cert; |
| 867 | |
| 868 | if (!cert_path) { |
| 869 | ERRARG; |
| 870 | return -1; |
| 871 | } |
| 872 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 873 | /* LOCK */ |
| 874 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 875 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 876 | if (!tls_opts.tls_ctx) { |
| 877 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 878 | if (!tls_opts.tls_ctx) { |
| 879 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 880 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 881 | } |
| 882 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 883 | } |
| 884 | |
| 885 | cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx); |
| 886 | if (!cert_store) { |
| 887 | cert_store = X509_STORE_new(); |
| 888 | SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store); |
| 889 | } |
| 890 | |
| 891 | errno = 0; |
| 892 | x509_cert = pem_to_cert(cert_path); |
| 893 | if (!x509_cert || (X509_STORE_add_cert(cert_store, x509_cert) != 1)) { |
| 894 | ERR("%s: adding a trusted certificate failed (%s).", |
| 895 | (errno ? strerror(errno) : ERR_reason_error_string(ERR_get_error()))); |
| 896 | X509_free(x509_cert); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 897 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 898 | } |
| 899 | X509_free(x509_cert); |
| 900 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 901 | /* UNLOCK */ |
| 902 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 903 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 904 | |
| 905 | fail: |
| 906 | /* UNLOCK */ |
| 907 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 908 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | API int |
| 912 | nc_tls_server_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path) |
| 913 | { |
| 914 | X509_STORE *cert_store; |
| 915 | X509_LOOKUP *lookup; |
| 916 | |
| 917 | if (!cacert_file_path && !cacert_dir_path) { |
| 918 | ERRARG; |
| 919 | return -1; |
| 920 | } |
| 921 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 922 | /* LOCK */ |
| 923 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 924 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 925 | if (!tls_opts.tls_ctx) { |
| 926 | tls_opts.tls_ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 927 | if (!tls_opts.tls_ctx) { |
| 928 | ERR("%s: failed to create TLS context.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 929 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 930 | } |
| 931 | SSL_CTX_set_verify(tls_opts.tls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nc_tlsclb_verify); |
| 932 | } |
| 933 | |
| 934 | cert_store = SSL_CTX_get_cert_store(tls_opts.tls_ctx); |
| 935 | if (!cert_store) { |
| 936 | cert_store = X509_STORE_new(); |
| 937 | SSL_CTX_set_cert_store(tls_opts.tls_ctx, cert_store); |
| 938 | } |
| 939 | |
| 940 | if (cacert_file_path) { |
| 941 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file()); |
| 942 | if (!lookup) { |
| 943 | ERR("%s: failed to add lookup method.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 944 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | if (X509_LOOKUP_load_file(lookup, cacert_file_path, X509_FILETYPE_PEM) != 1) { |
| 948 | ERR("%s: failed to add trusted cert file (%s).", __func__, ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 949 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
| 953 | if (cacert_dir_path) { |
| 954 | lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir()); |
| 955 | if (!lookup) { |
| 956 | ERR("%s: failed to add lookup method.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 957 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | if (X509_LOOKUP_add_dir(lookup, cacert_dir_path, X509_FILETYPE_PEM) != 1) { |
| 961 | ERR("%s: failed to add trusted cert directory (%s).", __func__, ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 962 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 966 | /* UNLOCK */ |
| 967 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 968 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 969 | |
| 970 | fail: |
| 971 | /* UNLOCK */ |
| 972 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
| 973 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | API void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 977 | nc_tls_server_destroy_certs(void) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 978 | { |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 979 | /* LOCK */ |
| 980 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
| 981 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 982 | if (!tls_opts.tls_ctx) { |
| 983 | return; |
| 984 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 985 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 986 | SSL_CTX_free(tls_opts.tls_ctx); |
| 987 | tls_opts.tls_ctx = NULL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 988 | |
| 989 | /* UNLOCK */ |
| 990 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 993 | API int |
| 994 | nc_tls_server_set_crl_locations(const char *crl_file_path, const char *crl_dir_path) |
| 995 | { |
| 996 | X509_LOOKUP *lookup; |
| 997 | |
| 998 | if (!crl_file_path && !crl_dir_path) { |
| 999 | ERRARG; |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1003 | /* LOCK */ |
| 1004 | pthread_mutex_lock(&tls_opts.crl_lock); |
| 1005 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1006 | if (!tls_opts.crl_store) { |
| 1007 | tls_opts.crl_store = X509_STORE_new(); |
| 1008 | } |
| 1009 | |
| 1010 | if (crl_file_path) { |
| 1011 | lookup = X509_STORE_add_lookup(tls_opts.crl_store, X509_LOOKUP_file()); |
| 1012 | if (!lookup) { |
| 1013 | ERR("%s: failed to add lookup method.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1014 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | if (X509_LOOKUP_load_file(lookup, crl_file_path, X509_FILETYPE_PEM) != 1) { |
| 1018 | ERR("%s: failed to add revocation lookup file (%s).", __func__, ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1019 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | if (crl_dir_path) { |
| 1024 | lookup = X509_STORE_add_lookup(tls_opts.crl_store, X509_LOOKUP_hash_dir()); |
| 1025 | if (!lookup) { |
| 1026 | ERR("%s: failed to add lookup method.", __func__); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1027 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (X509_LOOKUP_add_dir(lookup, crl_dir_path, X509_FILETYPE_PEM) != 1) { |
| 1031 | ERR("%s: failed to add revocation lookup directory (%s).", __func__, ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1032 | goto fail; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1036 | /* UNLOCK */ |
| 1037 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1038 | return 0; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1039 | |
| 1040 | fail: |
| 1041 | /* UNLOCK */ |
| 1042 | pthread_mutex_unlock(&tls_opts.crl_lock); |
| 1043 | return -1; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | API void |
| 1047 | nc_tls_server_destroy_crls(void) |
| 1048 | { |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1049 | /* LOCK */ |
| 1050 | pthread_mutex_lock(&tls_opts.crl_lock); |
| 1051 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1052 | if (!tls_opts.crl_store) { |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | X509_STORE_free(tls_opts.crl_store); |
| 1057 | tls_opts.crl_store = NULL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1058 | |
| 1059 | /* UNLOCK */ |
| 1060 | pthread_mutex_unlock(&tls_opts.crl_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | API int |
| 1064 | nc_tls_server_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name) |
| 1065 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1066 | if (!fingerprint || !map_type || ((map_type == NC_TLS_CTN_SPECIFIED) && !name) |
| 1067 | || ((map_type != NC_TLS_CTN_SPECIFIED) && name)) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1068 | ERRARG; |
| 1069 | return -1; |
| 1070 | } |
| 1071 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1072 | /* LOCK */ |
| 1073 | pthread_mutex_lock(&tls_opts.ctn_lock); |
| 1074 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1075 | ++tls_opts.ctn_count; |
| 1076 | tls_opts.ctn = realloc(tls_opts.ctn, tls_opts.ctn_count * sizeof *tls_opts.ctn); |
| 1077 | |
| 1078 | tls_opts.ctn[tls_opts.ctn_count - 1].id = id; |
| 1079 | tls_opts.ctn[tls_opts.ctn_count - 1].fingerprint = lydict_insert(server_opts.ctx, fingerprint, 0); |
| 1080 | tls_opts.ctn[tls_opts.ctn_count - 1].map_type = map_type; |
| 1081 | tls_opts.ctn[tls_opts.ctn_count - 1].name = lydict_insert(server_opts.ctx, name, 0); |
| 1082 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1083 | /* UNLOCK */ |
| 1084 | pthread_mutex_unlock(&tls_opts.ctn_lock); |
| 1085 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | API int |
| 1090 | nc_tls_server_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name) |
| 1091 | { |
| 1092 | uint16_t i; |
| 1093 | int ret = -1; |
| 1094 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1095 | /* LOCK */ |
| 1096 | pthread_mutex_lock(&tls_opts.ctn_lock); |
| 1097 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1098 | if ((id < 0) && !fingerprint && !map_type && !name) { |
| 1099 | for (i = 0; i < tls_opts.ctn_count; ++i) { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1100 | lydict_remove(server_opts.ctx, tls_opts.ctn[i].fingerprint); |
| 1101 | lydict_remove(server_opts.ctx, tls_opts.ctn[i].name); |
| 1102 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1103 | ret = 0; |
| 1104 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1105 | free(tls_opts.ctn); |
| 1106 | tls_opts.ctn = NULL; |
| 1107 | tls_opts.ctn_count = 0; |
| 1108 | } else { |
| 1109 | for (i = 0; i < tls_opts.ctn_count; ++i) { |
| 1110 | if (((id < 0) || (tls_opts.ctn[i].id == id)) |
| 1111 | && (!fingerprint || !strcmp(tls_opts.ctn[i].fingerprint, fingerprint)) |
| 1112 | && (!map_type || (tls_opts.ctn[i].map_type == map_type)) |
| 1113 | && (!name || (tls_opts.ctn[i].name && !strcmp(tls_opts.ctn[i].name, name)))) { |
| 1114 | lydict_remove(server_opts.ctx, tls_opts.ctn[i].fingerprint); |
| 1115 | lydict_remove(server_opts.ctx, tls_opts.ctn[i].name); |
| 1116 | |
| 1117 | --tls_opts.ctn_count; |
| 1118 | memmove(&tls_opts.ctn[i], &tls_opts.ctn[i + 1], (tls_opts.ctn_count - i) * sizeof *tls_opts.ctn); |
| 1119 | |
| 1120 | ret = 0; |
| 1121 | } |
| 1122 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1125 | /* UNLOCK */ |
| 1126 | pthread_mutex_unlock(&tls_opts.ctn_lock); |
| 1127 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | API void |
| 1132 | nc_tls_server_free_opts(void) |
| 1133 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1134 | nc_tls_server_destroy_certs(); |
| 1135 | nc_tls_server_destroy_crls(); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1136 | nc_tls_server_del_ctn(-1, NULL, 0, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1137 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1138 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1139 | static void |
| 1140 | nc_tls_make_verify_key(void) |
| 1141 | { |
| 1142 | pthread_key_create(&tls_opts.verify_key, NULL); |
| 1143 | } |
| 1144 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1145 | int |
| 1146 | nc_accept_tls_session(struct nc_session *session, int sock, int timeout) |
| 1147 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1148 | int ret; |
| 1149 | struct pollfd pfd; |
| 1150 | |
| 1151 | pfd.fd = sock; |
| 1152 | pfd.events = POLLIN; |
| 1153 | pfd.revents = 0; |
| 1154 | |
| 1155 | /* poll for a new connection */ |
| 1156 | errno = 0; |
| 1157 | ret = poll(&pfd, 1, timeout); |
| 1158 | if (!ret) { |
| 1159 | /* we timeouted */ |
| 1160 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1161 | return 0; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1162 | } else if (ret == -1) { |
| 1163 | ERR("%s: poll failed (%s).", __func__, strerror(errno)); |
| 1164 | close(sock); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1168 | /* data waiting, prepare session */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1169 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1170 | /* LOCK */ |
| 1171 | pthread_mutex_lock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1172 | session->ti.tls = SSL_new(tls_opts.tls_ctx); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1173 | /* UNLOCK */ |
| 1174 | pthread_mutex_unlock(&tls_opts.tls_ctx_lock); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1175 | if (!session->ti.tls) { |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1176 | ERR("%s: failed to create TLS structure from context.", __func__); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1177 | close(sock); |
| 1178 | return -1; |
| 1179 | } |
| 1180 | |
| 1181 | SSL_set_fd(session->ti.tls, sock); |
| 1182 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 1183 | |
Michal Vasko | 6d29299 | 2016-01-18 09:42:38 +0100 | [diff] [blame] | 1184 | /* store session on per-thread basis */ |
| 1185 | pthread_once(&tls_opts.verify_once, nc_tls_make_verify_key); |
| 1186 | pthread_setspecific(tls_opts.verify_key, session); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1187 | |
| 1188 | ret = SSL_accept(session->ti.tls); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame^] | 1189 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1190 | if (ret != 1) { |
| 1191 | switch (SSL_get_error(session->ti.tls, ret)) { |
| 1192 | case SSL_ERROR_SYSCALL: |
| 1193 | ERR("%s: SSL_accept failed (%s).", __func__, strerror(errno)); |
| 1194 | break; |
| 1195 | case SSL_ERROR_SSL: |
| 1196 | ERR("%s: SSL_accept failed (%s).", __func__, ERR_reason_error_string(ERR_get_error())); |
| 1197 | break; |
| 1198 | default: |
| 1199 | ERR("%s: SSL_accept failed.", __func__); |
| 1200 | break; |
| 1201 | } |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1205 | return 1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1206 | } |