| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) Jeremy Harris 2014 - 2019 */ |
| 6 | |
| 7 | /* This module provides TLS (aka SSL) support for Exim using the OpenSSL |
| 8 | library. It is #included into the tls.c file when that library is used. |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | /* Heading stuff */ |
| 13 | |
| 14 | #include <openssl/lhash.h> |
| 15 | #include <openssl/ssl.h> |
| 16 | #include <openssl/err.h> |
| 17 | #include <openssl/rand.h> |
| 18 | #include <openssl/x509v3.h> |
| 19 | |
| 20 | #ifdef LIBRESSL_VERSION_NUMBER /* LibreSSL */ |
| 21 | # if LIBRESSL_VERSION_NUMBER >= 0x2090000fL |
| 22 | # define EXIM_HAVE_ASN1_MACROS |
| 23 | # endif |
| 24 | #else /* OpenSSL */ |
| 25 | # if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 26 | # define EXIM_HAVE_ASN1_MACROS |
| 27 | # endif |
| 28 | #endif |
| 29 | |
| 30 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 31 | # define ASN1_STRING_get0_data ASN1_STRING_data |
| 32 | #endif |
| 33 | |
| 34 | /***************************************************** |
| 35 | * Export/import a certificate, binary/printable |
| 36 | *****************************************************/ |
| 37 | int |
| 38 | tls_export_cert(uschar * buf, size_t buflen, void * cert) |
| 39 | { |
| 40 | BIO * bp = BIO_new(BIO_s_mem()); |
| 41 | int fail; |
| 42 | |
| 43 | if ((fail = PEM_write_bio_X509(bp, (X509 *)cert) ? 0 : 1)) |
| 44 | log_write(0, LOG_MAIN, "TLS error in certificate export: %s", |
| 45 | ERR_error_string(ERR_get_error(), NULL)); |
| 46 | else |
| 47 | { |
| 48 | char * cp = CS buf; |
| 49 | int n; |
| 50 | buflen -= 2; |
| 51 | for(;;) |
| 52 | { |
| 53 | if ((n = BIO_gets(bp, cp, (int)buflen)) <= 0) break; |
| 54 | cp += n+1; |
| 55 | buflen -= n+1; |
| 56 | cp[-2] = '\\'; cp[-1] = 'n'; /* newline->"\n" */ |
| 57 | } /* compat with string_printing() */ |
| 58 | *cp = '\0'; |
| 59 | } |
| 60 | |
| 61 | BIO_free(bp); |
| 62 | return fail; |
| 63 | } |
| 64 | |
| 65 | int |
| 66 | tls_import_cert(const uschar * buf, void ** cert) |
| 67 | { |
| 68 | void * reset_point = store_get(0); |
| 69 | const uschar * cp = string_unprinting(US buf); |
| 70 | BIO * bp; |
| 71 | X509 * x = *(X509 **)cert; |
| 72 | int fail = 0; |
| 73 | |
| 74 | if (x) X509_free(x); |
| 75 | |
| 76 | bp = BIO_new_mem_buf(US cp, -1); |
| 77 | if (!(x = PEM_read_bio_X509(bp, NULL, 0, NULL))) |
| 78 | { |
| 79 | log_write(0, LOG_MAIN, "TLS error in certificate import: %s", |
| 80 | ERR_error_string(ERR_get_error(), NULL)); |
| 81 | fail = 1; |
| 82 | } |
| 83 | else |
| 84 | *cert = (void *)x; |
| 85 | BIO_free(bp); |
| 86 | store_reset(reset_point); |
| 87 | return fail; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | tls_free_cert(void ** cert) |
| 92 | { |
| 93 | X509 * x = *(X509 **)cert; |
| 94 | if (x) |
| 95 | { |
| 96 | X509_free(x); |
| 97 | *cert = NULL; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /***************************************************** |
| 103 | * Certificate field extraction routines |
| 104 | *****************************************************/ |
| 105 | |
| 106 | /* First, some internal service functions */ |
| 107 | |
| 108 | static uschar * |
| 109 | badalloc(void) |
| 110 | { |
| 111 | expand_string_message = US"allocation failure"; |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | static uschar * |
| 116 | bio_string_copy(BIO * bp, int len) |
| 117 | { |
| 118 | uschar * cp = US""; |
| 119 | len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0; |
| 120 | cp = string_copyn(cp, len); |
| 121 | BIO_free(bp); |
| 122 | return cp; |
| 123 | } |
| 124 | |
| 125 | static uschar * |
| 126 | asn1_time_copy(const ASN1_TIME * asntime, uschar * mod) |
| 127 | { |
| 128 | uschar * s = NULL; |
| 129 | BIO * bp = BIO_new(BIO_s_mem()); |
| 130 | int len; |
| 131 | |
| 132 | if (!bp) |
| 133 | return badalloc(); |
| 134 | len = ASN1_TIME_print(bp, asntime); |
| 135 | len = len > 0 ? (int) BIO_get_mem_data(bp, CSS &s) : 0; |
| 136 | |
| 137 | if (mod && Ustrcmp(mod, "raw") == 0) /* native ASN */ |
| 138 | s = string_copyn(s, len); |
| 139 | else |
| 140 | { |
| 141 | struct tm tm; |
| 142 | struct tm * tm_p = &tm; |
| 143 | BOOL mod_tz = TRUE; |
| 144 | uschar * tz = to_tz(US"GMT0"); /* need to call strptime with baseline TZ */ |
| 145 | |
| 146 | /* Parse OpenSSL ASN1_TIME_print output. A shame there seems to |
| 147 | be no other interface for the times. |
| 148 | */ |
| 149 | |
| 150 | /*XXX %Z might be glibc-specific? Solaris has it, at least*/ |
| 151 | /*XXX should we switch to POSIX locale for this? */ |
| 152 | tm.tm_isdst = 0; |
| 153 | if (!len || !strptime(CCS s, "%b %e %T %Y %Z", &tm)) |
| 154 | expand_string_message = US"failed time conversion"; |
| 155 | |
| 156 | else |
| 157 | { |
| 158 | time_t t = mktime(&tm); /* make the tm self-consistent */ |
| 159 | |
| 160 | if (mod && Ustrcmp(mod, "int") == 0) /* seconds since epoch */ |
| 161 | s = string_sprintf(TIME_T_FMT, t); |
| 162 | |
| 163 | else |
| 164 | { |
| 165 | if (!f.timestamps_utc) /* decoded string in local TZ */ |
| 166 | { /* shift to local TZ */ |
| 167 | restore_tz(tz); |
| 168 | mod_tz = FALSE; |
| 169 | tm_p = localtime(&t); |
| 170 | } |
| 171 | /* "utc" is default, and rfc5280 says cert times should be Zulu */ |
| 172 | |
| 173 | /* convert to string in our format */ |
| 174 | len = 32; |
| 175 | s = store_get(len); |
| 176 | strftime(CS s, (size_t)len, "%b %e %T %Y %z", tm_p); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (mod_tz) |
| 181 | restore_tz(tz); |
| 182 | } |
| 183 | BIO_free(bp); |
| 184 | return s; |
| 185 | } |
| 186 | |
| 187 | static uschar * |
| 188 | x509_name_copy(X509_NAME * name) |
| 189 | { |
| 190 | BIO * bp = BIO_new(BIO_s_mem()); |
| 191 | int len_good; |
| 192 | |
| 193 | if (!bp) return badalloc(); |
| 194 | |
| 195 | len_good = |
| 196 | X509_NAME_print_ex(bp, name, 0, XN_FLAG_RFC2253) >= 0 |
| 197 | ? 1 : 0; |
| 198 | return bio_string_copy(bp, len_good); |
| 199 | } |
| 200 | |
| 201 | /**/ |
| 202 | /* Now the extractors, called from expand.c |
| 203 | Arguments: |
| 204 | cert The certificate |
| 205 | mod Optional modifiers for the operator |
| 206 | |
| 207 | Return: |
| 208 | Allocated string with extracted value |
| 209 | */ |
| 210 | |
| 211 | uschar * |
| 212 | tls_cert_issuer(void * cert, uschar * mod) |
| 213 | { |
| 214 | uschar * cp = x509_name_copy(X509_get_issuer_name((X509 *)cert)); |
| 215 | return mod ? tls_field_from_dn(cp, mod) : cp; |
| 216 | } |
| 217 | |
| 218 | uschar * |
| 219 | tls_cert_not_before(void * cert, uschar * mod) |
| 220 | { |
| 221 | return asn1_time_copy(X509_get_notBefore((X509 *)cert), mod); |
| 222 | } |
| 223 | |
| 224 | uschar * |
| 225 | tls_cert_not_after(void * cert, uschar * mod) |
| 226 | { |
| 227 | return asn1_time_copy(X509_get_notAfter((X509 *)cert), mod); |
| 228 | } |
| 229 | |
| 230 | uschar * |
| 231 | tls_cert_serial_number(void * cert, uschar * mod) |
| 232 | { |
| 233 | uschar txt[256]; |
| 234 | BIO * bp = BIO_new(BIO_s_mem()); |
| 235 | int len; |
| 236 | |
| 237 | if (!bp) return badalloc(); |
| 238 | |
| 239 | len = i2a_ASN1_INTEGER(bp, X509_get_serialNumber((X509 *)cert)); |
| 240 | if (len < sizeof(txt)) |
| 241 | BIO_read(bp, txt, len); |
| 242 | else |
| 243 | len = 0; |
| 244 | BIO_free(bp); |
| 245 | return string_copynlc(txt, len); /* lowercase */ |
| 246 | } |
| 247 | |
| 248 | uschar * |
| 249 | tls_cert_signature(void * cert, uschar * mod) |
| 250 | { |
| 251 | uschar * cp = NULL; |
| 252 | BIO * bp = BIO_new(BIO_s_mem()); |
| 253 | |
| 254 | if (!bp) return badalloc(); |
| 255 | |
| 256 | if (X509_print_ex(bp, (X509 *)cert, 0, |
| 257 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL | |
| 258 | X509_FLAG_NO_SIGNAME | X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY | |
| 259 | X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS | |
| 260 | /* X509_FLAG_NO_SIGDUMP is the missing one */ |
| 261 | X509_FLAG_NO_AUX) == 1) |
| 262 | { |
| 263 | long len = BIO_get_mem_data(bp, &cp); |
| 264 | |
| 265 | /* Strip leading "Signature Algorithm" line */ |
| 266 | while (*cp && *cp != '\n') { cp++; len--; } |
| 267 | |
| 268 | cp = string_copyn(cp+1, len-1); |
| 269 | } |
| 270 | BIO_free(bp); |
| 271 | return cp; |
| 272 | } |
| 273 | |
| 274 | uschar * |
| 275 | tls_cert_signature_algorithm(void * cert, uschar * mod) |
| 276 | { |
| 277 | uschar * cp = NULL; |
| 278 | BIO * bp = BIO_new(BIO_s_mem()); |
| 279 | |
| 280 | if (!bp) return badalloc(); |
| 281 | |
| 282 | if (X509_print_ex(bp, (X509 *)cert, 0, |
| 283 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL | |
| 284 | /* X509_FLAG_NO_SIGNAME is the missing one */ |
| 285 | X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY | |
| 286 | X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS | |
| 287 | X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_AUX) == 1) |
| 288 | { |
| 289 | long len = BIO_get_mem_data(bp, &cp); |
| 290 | |
| 291 | /* Strip leading " Signature Algorithm: " and trailing newline */ |
| 292 | while (*cp && *cp != ':') { cp++; len--; } |
| 293 | do { cp++; len--; } while (*cp && *cp == ' '); |
| 294 | if (cp[len-1] == '\n') len--; |
| 295 | |
| 296 | cp = string_copyn(cp, len); |
| 297 | } |
| 298 | BIO_free(bp); |
| 299 | return cp; |
| 300 | } |
| 301 | |
| 302 | uschar * |
| 303 | tls_cert_subject(void * cert, uschar * mod) |
| 304 | { |
| 305 | uschar * cp = x509_name_copy(X509_get_subject_name((X509 *)cert)); |
| 306 | return mod ? tls_field_from_dn(cp, mod) : cp; |
| 307 | } |
| 308 | |
| 309 | uschar * |
| 310 | tls_cert_version(void * cert, uschar * mod) |
| 311 | { |
| 312 | return string_sprintf("%ld", X509_get_version((X509 *)cert)); |
| 313 | } |
| 314 | |
| 315 | uschar * |
| 316 | tls_cert_ext_by_oid(void * cert, uschar * oid, int idx) |
| 317 | { |
| 318 | int nid = OBJ_create(CS oid, "", ""); |
| 319 | int nidx = X509_get_ext_by_NID((X509 *)cert, nid, idx); |
| 320 | X509_EXTENSION * ex = X509_get_ext((X509 *)cert, nidx); |
| 321 | ASN1_OCTET_STRING * adata = X509_EXTENSION_get_data(ex); |
| 322 | BIO * bp = BIO_new(BIO_s_mem()); |
| 323 | long len; |
| 324 | uschar * cp1; |
| 325 | uschar * cp2; |
| 326 | uschar * cp3; |
| 327 | |
| 328 | if (!bp) return badalloc(); |
| 329 | |
| 330 | #ifdef EXIM_HAVE_ASN1_MACROS |
| 331 | ASN1_STRING_print(bp, adata); |
| 332 | #else |
| 333 | M_ASN1_OCTET_STRING_print(bp, adata); |
| 334 | #endif |
| 335 | |
| 336 | /* binary data, DER encoded */ |
| 337 | /* just dump for now */ |
| 338 | len = BIO_get_mem_data(bp, &cp1); |
| 339 | cp3 = cp2 = store_get(len*3+1); |
| 340 | |
| 341 | while(len) |
| 342 | { |
| 343 | cp2 += sprintf(CS cp2, "%.2x ", *cp1++); |
| 344 | len--; |
| 345 | } |
| 346 | cp2[-1] = '\0'; |
| 347 | |
| 348 | return cp3; |
| 349 | } |
| 350 | |
| 351 | uschar * |
| 352 | tls_cert_subject_altname(void * cert, uschar * mod) |
| 353 | { |
| 354 | gstring * list = NULL; |
| 355 | STACK_OF(GENERAL_NAME) * san = (STACK_OF(GENERAL_NAME) *) |
| 356 | X509_get_ext_d2i((X509 *)cert, NID_subject_alt_name, NULL, NULL); |
| 357 | uschar osep = '\n'; |
| 358 | uschar * tag = US""; |
| 359 | uschar * ele; |
| 360 | int match = -1; |
| 361 | int len; |
| 362 | |
| 363 | if (!san) return NULL; |
| 364 | |
| 365 | while (mod && *mod) |
| 366 | { |
| 367 | if (*mod == '>' && *++mod) osep = *mod++; |
| 368 | else if (Ustrncmp(mod,"dns",3)==0) { match = GEN_DNS; mod += 3; } |
| 369 | else if (Ustrncmp(mod,"uri",3)==0) { match = GEN_URI; mod += 3; } |
| 370 | else if (Ustrncmp(mod,"mail",4)==0) { match = GEN_EMAIL; mod += 4; } |
| 371 | else mod++; |
| 372 | |
| 373 | if (*mod == ',') mod++; |
| 374 | } |
| 375 | |
| 376 | while (sk_GENERAL_NAME_num(san) > 0) |
| 377 | { |
| 378 | GENERAL_NAME * namePart = sk_GENERAL_NAME_pop(san); |
| 379 | if (match != -1 && match != namePart->type) |
| 380 | continue; |
| 381 | switch (namePart->type) |
| 382 | { |
| 383 | case GEN_DNS: |
| 384 | tag = US"DNS"; |
| 385 | ele = US ASN1_STRING_get0_data(namePart->d.dNSName); |
| 386 | len = ASN1_STRING_length(namePart->d.dNSName); |
| 387 | break; |
| 388 | case GEN_URI: |
| 389 | tag = US"URI"; |
| 390 | ele = US ASN1_STRING_get0_data(namePart->d.uniformResourceIdentifier); |
| 391 | len = ASN1_STRING_length(namePart->d.uniformResourceIdentifier); |
| 392 | break; |
| 393 | case GEN_EMAIL: |
| 394 | tag = US"MAIL"; |
| 395 | ele = US ASN1_STRING_get0_data(namePart->d.rfc822Name); |
| 396 | len = ASN1_STRING_length(namePart->d.rfc822Name); |
| 397 | break; |
| 398 | default: |
| 399 | continue; /* ignore unrecognised types */ |
| 400 | } |
| 401 | if (ele[len]) /* not nul-terminated */ |
| 402 | ele = string_copyn(ele, len); |
| 403 | |
| 404 | if (Ustrlen(ele) == len) /* ignore any with embedded nul */ |
| 405 | list = string_append_listele(list, osep, |
| 406 | match == -1 ? string_sprintf("%s=%s", tag, ele) : ele); |
| 407 | } |
| 408 | |
| 409 | sk_GENERAL_NAME_free(san); |
| 410 | return string_from_gstring(list); |
| 411 | } |
| 412 | |
| 413 | uschar * |
| 414 | tls_cert_ocsp_uri(void * cert, uschar * mod) |
| 415 | { |
| 416 | STACK_OF(ACCESS_DESCRIPTION) * ads = (STACK_OF(ACCESS_DESCRIPTION) *) |
| 417 | X509_get_ext_d2i((X509 *)cert, NID_info_access, NULL, NULL); |
| 418 | int adsnum = sk_ACCESS_DESCRIPTION_num(ads); |
| 419 | uschar sep = '\n'; |
| 420 | gstring * list = NULL; |
| 421 | |
| 422 | if (mod) |
| 423 | if (*mod == '>' && *++mod) sep = *mod++; |
| 424 | |
| 425 | for (int i = 0; i < adsnum; i++) |
| 426 | { |
| 427 | ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(ads, i); |
| 428 | |
| 429 | if (ad && OBJ_obj2nid(ad->method) == NID_ad_OCSP) |
| 430 | list = string_append_listele_n(list, sep, |
| 431 | US ASN1_STRING_get0_data(ad->location->d.ia5), |
| 432 | ASN1_STRING_length(ad->location->d.ia5)); |
| 433 | } |
| 434 | sk_ACCESS_DESCRIPTION_free(ads); |
| 435 | return string_from_gstring(list); |
| 436 | } |
| 437 | |
| 438 | uschar * |
| 439 | tls_cert_crl_uri(void * cert, uschar * mod) |
| 440 | { |
| 441 | STACK_OF(DIST_POINT) * dps = (STACK_OF(DIST_POINT) *) |
| 442 | X509_get_ext_d2i((X509 *)cert, NID_crl_distribution_points, |
| 443 | NULL, NULL); |
| 444 | DIST_POINT * dp; |
| 445 | uschar sep = '\n'; |
| 446 | gstring * list = NULL; |
| 447 | |
| 448 | if (mod) |
| 449 | if (*mod == '>' && *++mod) sep = *mod++; |
| 450 | |
| 451 | if (dps) for (int i = 0, dpsnum = sk_DIST_POINT_num(dps); i < dpsnum; i++) |
| 452 | if ((dp = sk_DIST_POINT_value(dps, i))) |
| 453 | { |
| 454 | STACK_OF(GENERAL_NAME) * names = dp->distpoint->name.fullname; |
| 455 | GENERAL_NAME * np; |
| 456 | |
| 457 | for (int j = 0, nnum = sk_GENERAL_NAME_num(names); j < nnum; j++) |
| 458 | if ( (np = sk_GENERAL_NAME_value(names, j)) |
| 459 | && np->type == GEN_URI |
| 460 | ) |
| 461 | list = string_append_listele_n(list, sep, |
| 462 | US ASN1_STRING_get0_data(np->d.uniformResourceIdentifier), |
| 463 | ASN1_STRING_length(np->d.uniformResourceIdentifier)); |
| 464 | } |
| 465 | sk_DIST_POINT_free(dps); |
| 466 | return string_from_gstring(list); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | /***************************************************** |
| 472 | * Certificate operator routines |
| 473 | *****************************************************/ |
| 474 | uschar * |
| 475 | tls_cert_der_b64(void * cert) |
| 476 | { |
| 477 | BIO * bp = BIO_new(BIO_s_mem()); |
| 478 | uschar * cp = NULL; |
| 479 | |
| 480 | if (!i2d_X509_bio(bp, (X509 *)cert)) |
| 481 | log_write(0, LOG_MAIN, "TLS error in certificate export: %s", |
| 482 | ERR_error_string(ERR_get_error(), NULL)); |
| 483 | else |
| 484 | { |
| 485 | long len = BIO_get_mem_data(bp, &cp); |
| 486 | cp = b64encode(CUS cp, (int)len); |
| 487 | } |
| 488 | |
| 489 | BIO_free(bp); |
| 490 | return cp; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | static uschar * |
| 495 | fingerprint(X509 * cert, const EVP_MD * fdig) |
| 496 | { |
| 497 | unsigned int n; |
| 498 | uschar md[EVP_MAX_MD_SIZE]; |
| 499 | uschar * cp; |
| 500 | |
| 501 | if (!X509_digest(cert,fdig,md,&n)) |
| 502 | { |
| 503 | expand_string_message = US"tls_cert_fprt: out of mem\n"; |
| 504 | return NULL; |
| 505 | } |
| 506 | cp = store_get(n*2+1); |
| 507 | for (int j = 0; j < (int)n; j++) sprintf(CS cp+2*j, "%02X", md[j]); |
| 508 | return(cp); |
| 509 | } |
| 510 | |
| 511 | uschar * |
| 512 | tls_cert_fprt_md5(void * cert) |
| 513 | { |
| 514 | return fingerprint((X509 *)cert, EVP_md5()); |
| 515 | } |
| 516 | |
| 517 | uschar * |
| 518 | tls_cert_fprt_sha1(void * cert) |
| 519 | { |
| 520 | return fingerprint((X509 *)cert, EVP_sha1()); |
| 521 | } |
| 522 | |
| 523 | uschar * |
| 524 | tls_cert_fprt_sha256(void * cert) |
| 525 | { |
| 526 | return fingerprint((X509 *)cert, EVP_sha256()); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /* vi: aw ai sw=2 |
| 531 | */ |
| 532 | /* End of tlscert-openssl.c */ |