1 /* 2 * Copyright (c) 2000-2006, 2008, 2009, 2011, 2013 Proofpoint, Inc. and its suppliers. 3 * All rights reserved. 4 * 5 * By using this file, you agree to the terms and conditions set 6 * forth in the LICENSE file which can be found at the top level of 7 * the sendmail distribution. 8 * 9 */ 10 11 #include <sendmail.h> 12 13 SM_RCSID("@(#)$Id: tls.c,v 8.127 2013-11-27 02:51:11 gshapiro Exp $") 14 15 #if STARTTLS 16 # include <openssl/err.h> 17 # include <openssl/bio.h> 18 # include <openssl/pem.h> 19 # if !NO_DH 20 # include <openssl/dh.h> 21 # endif /* !NO_DH */ 22 # ifndef HASURANDOMDEV 23 # include <openssl/rand.h> 24 # endif /* ! HASURANDOMDEV */ 25 # if !TLS_NO_RSA 26 static RSA *rsa_tmp = NULL; /* temporary RSA key */ 27 static RSA *tmp_rsa_key __P((SSL *, int, int)); 28 # endif /* !TLS_NO_RSA */ 29 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L 30 static int tls_verify_cb __P((X509_STORE_CTX *)); 31 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 32 static int tls_verify_cb __P((X509_STORE_CTX *, void *)); 33 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 34 35 # if OPENSSL_VERSION_NUMBER > 0x00907000L 36 static int x509_verify_cb __P((int, X509_STORE_CTX *)); 37 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 38 39 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L 40 # define CONST097 41 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 42 # define CONST097 const 43 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 44 static void apps_ssl_info_cb __P((CONST097 SSL *, int , int)); 45 static bool tls_ok_f __P((char *, char *, int)); 46 static bool tls_safe_f __P((char *, long, bool)); 47 static int tls_verify_log __P((int, X509_STORE_CTX *, const char *)); 48 49 # if !NO_DH 50 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100001L || \ 51 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L) 52 static int 53 DH_set0_pqg(dh, p, q, g) 54 DH *dh; 55 BIGNUM *p; 56 BIGNUM *q; 57 BIGNUM *g; 58 { 59 dh->p=p; 60 if (q != NULL) 61 dh->q=q; 62 dh->g=g; 63 return 1; /* success */ 64 } 65 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 66 67 static DH *get_dh512 __P((void)); 68 69 static unsigned char dh512_p[] = 70 { 71 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75, 72 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F, 73 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3, 74 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12, 75 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C, 76 0x47,0x74,0xE8,0x33 77 }; 78 static unsigned char dh512_g[] = 79 { 80 0x02 81 }; 82 83 static DH * 84 get_dh512() 85 { 86 DH *dh = NULL; 87 BIGNUM *dhp_bn, *dhg_bn; 88 89 if ((dh = DH_new()) == NULL) 90 return NULL; 91 dhp_bn = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL); 92 dhg_bn = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL); 93 if ((dhp_bn == NULL) || (dhg_bn == NULL) || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) 94 { 95 DH_free(dh); 96 BN_free(dhp_bn); 97 BN_free(dhg_bn); 98 return(NULL); 99 } 100 return dh; 101 } 102 103 # if 0 104 105 This is the data from which the C code has been generated: 106 107 -----BEGIN DH PARAMETERS----- 108 MIIBCAKCAQEArDcgcLpxEksQHPlolRKCUJ2szKRziseWV9cUSQNZGxoGw7KkROz4 109 HF9QSbg5axyNIG+QbZYtx0jp3l6/GWq1dLOj27yZkgYgaYgFrvKPiZ2jJ5xETQVH 110 UpZwbjRcyjyWkWYJVsx1aF4F/iY4kT0n/+iGEoimI3C9V3KXTJ2S6jIkyJ6M/CrN 111 EtrDynMlUMGlc7S1ouXVOTrtKeqy3S2L9eBLxVI+sChEijGIfELupdVeXihK006p 112 MgnABPDbkTx6OOtYmSZaGQX+OLW2FPmwvcrzgCz9t9cAsuUcBZv1LeHEqZZttyLU 113 oK0jjSXgFyeU4/NfyA+zuNeWzUL6bHmigwIBAg== 114 -----END DH PARAMETERS----- 115 # endif /* 0 */ 116 117 static DH * 118 get_dh2048() 119 { 120 static unsigned char dh2048_p[]={ 121 0xAC,0x37,0x20,0x70,0xBA,0x71,0x12,0x4B,0x10,0x1C,0xF9,0x68, 122 0x95,0x12,0x82,0x50,0x9D,0xAC,0xCC,0xA4,0x73,0x8A,0xC7,0x96, 123 0x57,0xD7,0x14,0x49,0x03,0x59,0x1B,0x1A,0x06,0xC3,0xB2,0xA4, 124 0x44,0xEC,0xF8,0x1C,0x5F,0x50,0x49,0xB8,0x39,0x6B,0x1C,0x8D, 125 0x20,0x6F,0x90,0x6D,0x96,0x2D,0xC7,0x48,0xE9,0xDE,0x5E,0xBF, 126 0x19,0x6A,0xB5,0x74,0xB3,0xA3,0xDB,0xBC,0x99,0x92,0x06,0x20, 127 0x69,0x88,0x05,0xAE,0xF2,0x8F,0x89,0x9D,0xA3,0x27,0x9C,0x44, 128 0x4D,0x05,0x47,0x52,0x96,0x70,0x6E,0x34,0x5C,0xCA,0x3C,0x96, 129 0x91,0x66,0x09,0x56,0xCC,0x75,0x68,0x5E,0x05,0xFE,0x26,0x38, 130 0x91,0x3D,0x27,0xFF,0xE8,0x86,0x12,0x88,0xA6,0x23,0x70,0xBD, 131 0x57,0x72,0x97,0x4C,0x9D,0x92,0xEA,0x32,0x24,0xC8,0x9E,0x8C, 132 0xFC,0x2A,0xCD,0x12,0xDA,0xC3,0xCA,0x73,0x25,0x50,0xC1,0xA5, 133 0x73,0xB4,0xB5,0xA2,0xE5,0xD5,0x39,0x3A,0xED,0x29,0xEA,0xB2, 134 0xDD,0x2D,0x8B,0xF5,0xE0,0x4B,0xC5,0x52,0x3E,0xB0,0x28,0x44, 135 0x8A,0x31,0x88,0x7C,0x42,0xEE,0xA5,0xD5,0x5E,0x5E,0x28,0x4A, 136 0xD3,0x4E,0xA9,0x32,0x09,0xC0,0x04,0xF0,0xDB,0x91,0x3C,0x7A, 137 0x38,0xEB,0x58,0x99,0x26,0x5A,0x19,0x05,0xFE,0x38,0xB5,0xB6, 138 0x14,0xF9,0xB0,0xBD,0xCA,0xF3,0x80,0x2C,0xFD,0xB7,0xD7,0x00, 139 0xB2,0xE5,0x1C,0x05,0x9B,0xF5,0x2D,0xE1,0xC4,0xA9,0x96,0x6D, 140 0xB7,0x22,0xD4,0xA0,0xAD,0x23,0x8D,0x25,0xE0,0x17,0x27,0x94, 141 0xE3,0xF3,0x5F,0xC8,0x0F,0xB3,0xB8,0xD7,0x96,0xCD,0x42,0xFA, 142 0x6C,0x79,0xA2,0x83, 143 }; 144 static unsigned char dh2048_g[]={ 0x02, }; 145 DH *dh; 146 BIGNUM *dhp_bn, *dhg_bn; 147 148 if ((dh=DH_new()) == NULL) 149 return(NULL); 150 dhp_bn = BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL); 151 dhg_bn = BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL); 152 if ((dhp_bn == NULL) || (dhg_bn == NULL) || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) 153 { 154 DH_free(dh); 155 BN_free(dhp_bn); 156 BN_free(dhg_bn); 157 return(NULL); 158 } 159 return(dh); 160 } 161 # endif /* !NO_DH */ 162 163 164 /* 165 ** TLS_RAND_INIT -- initialize STARTTLS random generator 166 ** 167 ** Parameters: 168 ** randfile -- name of file with random data 169 ** logl -- loglevel 170 ** 171 ** Returns: 172 ** success/failure 173 ** 174 ** Side Effects: 175 ** initializes PRNG for tls library. 176 */ 177 178 # define MIN_RAND_BYTES 128 /* 1024 bits */ 179 180 # define RF_OK 0 /* randfile OK */ 181 # define RF_MISS 1 /* randfile == NULL || *randfile == '\0' */ 182 # define RF_UNKNOWN 2 /* unknown prefix for randfile */ 183 184 # define RI_NONE 0 /* no init yet */ 185 # define RI_SUCCESS 1 /* init was successful */ 186 # define RI_FAIL 2 /* init failed */ 187 188 static bool tls_rand_init __P((char *, int)); 189 190 static bool 191 tls_rand_init(randfile, logl) 192 char *randfile; 193 int logl; 194 { 195 # ifndef HASURANDOMDEV 196 /* not required if /dev/urandom exists, OpenSSL does it internally */ 197 198 bool ok; 199 int randdef; 200 static int done = RI_NONE; 201 202 /* 203 ** initialize PRNG 204 */ 205 206 /* did we try this before? if yes: return old value */ 207 if (done != RI_NONE) 208 return done == RI_SUCCESS; 209 210 /* set default values */ 211 ok = false; 212 done = RI_FAIL; 213 randdef = (randfile == NULL || *randfile == '\0') ? RF_MISS : RF_OK; 214 # if EGD 215 if (randdef == RF_OK && sm_strncasecmp(randfile, "egd:", 4) == 0) 216 { 217 randfile += 4; 218 if (RAND_egd(randfile) < 0) 219 { 220 sm_syslog(LOG_WARNING, NOQID, 221 "STARTTLS: RAND_egd(%s) failed: random number generator not seeded", 222 randfile); 223 } 224 else 225 ok = true; 226 } 227 else 228 # endif /* EGD */ 229 if (randdef == RF_OK && sm_strncasecmp(randfile, "file:", 5) == 0) 230 { 231 int fd; 232 long sff; 233 struct stat st; 234 235 randfile += 5; 236 sff = SFF_SAFEDIRPATH | SFF_NOWLINK 237 | SFF_NOGWFILES | SFF_NOWWFILES 238 | SFF_NOGRFILES | SFF_NOWRFILES 239 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT; 240 if (DontLockReadFiles) 241 sff |= SFF_NOLOCK; 242 if ((fd = safeopen(randfile, O_RDONLY, 0, sff)) >= 0) 243 { 244 if (fstat(fd, &st) < 0) 245 { 246 if (LogLevel > logl) 247 sm_syslog(LOG_ERR, NOQID, 248 "STARTTLS: can't fstat(%s)", 249 randfile); 250 } 251 else 252 { 253 bool use, problem; 254 255 use = true; 256 problem = false; 257 258 /* max. age of file: 10 minutes */ 259 if (st.st_mtime + 600 < curtime()) 260 { 261 use = bitnset(DBS_INSUFFICIENTENTROPY, 262 DontBlameSendmail); 263 problem = true; 264 if (LogLevel > logl) 265 sm_syslog(LOG_ERR, NOQID, 266 "STARTTLS: RandFile %s too old: %s", 267 randfile, 268 use ? "unsafe" : 269 "unusable"); 270 } 271 if (use && st.st_size < MIN_RAND_BYTES) 272 { 273 use = bitnset(DBS_INSUFFICIENTENTROPY, 274 DontBlameSendmail); 275 problem = true; 276 if (LogLevel > logl) 277 sm_syslog(LOG_ERR, NOQID, 278 "STARTTLS: size(%s) < %d: %s", 279 randfile, 280 MIN_RAND_BYTES, 281 use ? "unsafe" : 282 "unusable"); 283 } 284 if (use) 285 ok = RAND_load_file(randfile, -1) >= 286 MIN_RAND_BYTES; 287 if (use && !ok) 288 { 289 if (LogLevel > logl) 290 sm_syslog(LOG_WARNING, NOQID, 291 "STARTTLS: RAND_load_file(%s) failed: random number generator not seeded", 292 randfile); 293 } 294 if (problem) 295 ok = false; 296 } 297 if (ok || bitnset(DBS_INSUFFICIENTENTROPY, 298 DontBlameSendmail)) 299 { 300 /* add this even if fstat() failed */ 301 RAND_seed((void *) &st, sizeof(st)); 302 } 303 (void) close(fd); 304 } 305 else 306 { 307 if (LogLevel > logl) 308 sm_syslog(LOG_WARNING, NOQID, 309 "STARTTLS: Warning: safeopen(%s) failed", 310 randfile); 311 } 312 } 313 else if (randdef == RF_OK) 314 { 315 if (LogLevel > logl) 316 sm_syslog(LOG_WARNING, NOQID, 317 "STARTTLS: Error: no proper random file definition %s", 318 randfile); 319 randdef = RF_UNKNOWN; 320 } 321 if (randdef == RF_MISS) 322 { 323 if (LogLevel > logl) 324 sm_syslog(LOG_WARNING, NOQID, 325 "STARTTLS: Error: missing random file definition"); 326 } 327 if (!ok && bitnset(DBS_INSUFFICIENTENTROPY, DontBlameSendmail)) 328 { 329 int i; 330 long r; 331 unsigned char buf[MIN_RAND_BYTES]; 332 333 /* assert((MIN_RAND_BYTES % sizeof(long)) == 0); */ 334 for (i = 0; i <= sizeof(buf) - sizeof(long); i += sizeof(long)) 335 { 336 r = get_random(); 337 (void) memcpy(buf + i, (void *) &r, sizeof(long)); 338 } 339 RAND_seed(buf, sizeof(buf)); 340 if (LogLevel > logl) 341 sm_syslog(LOG_WARNING, NOQID, 342 "STARTTLS: Warning: random number generator not properly seeded"); 343 ok = true; 344 } 345 done = ok ? RI_SUCCESS : RI_FAIL; 346 return ok; 347 # else /* ! HASURANDOMDEV */ 348 return true; 349 # endif /* ! HASURANDOMDEV */ 350 } 351 /* 352 ** INIT_TLS_LIBRARY -- Calls functions which setup TLS library for global use. 353 ** 354 ** Parameters: 355 ** fipsmode -- use FIPS? 356 ** 357 ** Returns: 358 ** succeeded? 359 */ 360 361 bool 362 init_tls_library(fipsmode) 363 bool fipsmode; 364 { 365 bool bv; 366 367 /* basic TLS initialization, ignore result for now */ 368 SSL_library_init(); 369 SSL_load_error_strings(); 370 OpenSSL_add_all_algorithms(); 371 # if 0 372 /* this is currently a macro for SSL_library_init */ 373 SSLeay_add_ssl_algorithms(); 374 # endif /* 0 */ 375 376 bv = tls_rand_init(RandFile, 7); 377 # if _FFR_FIPSMODE 378 if (bv && fipsmode) 379 { 380 if (!FIPS_mode_set(1)) 381 { 382 unsigned long err; 383 384 err = ERR_get_error(); 385 if (LogLevel > 0) 386 sm_syslog(LOG_ERR, NOQID, 387 "STARTTLS=init, FIPSMode=%s", 388 ERR_error_string(err, NULL)); 389 return false; 390 } 391 else 392 { 393 if (LogLevel > 9) 394 sm_syslog(LOG_INFO, NOQID, 395 "STARTTLS=init, FIPSMode=ok"); 396 } 397 } 398 #endif /* _FFR_FIPSMODE */ 399 if (bv && CertFingerprintAlgorithm != NULL) 400 { 401 const EVP_MD *md; 402 403 md = EVP_get_digestbyname(CertFingerprintAlgorithm); 404 if (NULL == md) 405 { 406 bv = false; 407 if (LogLevel > 0) 408 sm_syslog(LOG_ERR, NOQID, 409 "STARTTLS=init, CertFingerprintAlgorithm=%s, status=invalid" 410 , CertFingerprintAlgorithm); 411 } 412 else 413 EVP_digest = md; 414 } 415 return bv; 416 } 417 418 /* 419 ** TLS_SET_VERIFY -- request client certificate? 420 ** 421 ** Parameters: 422 ** ctx -- TLS context 423 ** ssl -- TLS structure 424 ** vrfy -- request certificate? 425 ** 426 ** Returns: 427 ** none. 428 ** 429 ** Side Effects: 430 ** Sets verification state for TLS 431 ** 432 # if TLS_VRFY_PER_CTX 433 ** Notice: 434 ** This is per TLS context, not per TLS structure; 435 ** the former is global, the latter per connection. 436 ** It would be nice to do this per connection, but this 437 ** doesn't work in the current TLS libraries :-( 438 # endif * TLS_VRFY_PER_CTX * 439 */ 440 441 void 442 tls_set_verify(ctx, ssl, vrfy) 443 SSL_CTX *ctx; 444 SSL *ssl; 445 bool vrfy; 446 { 447 # if !TLS_VRFY_PER_CTX 448 SSL_set_verify(ssl, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL); 449 # else /* !TLS_VRFY_PER_CTX */ 450 SSL_CTX_set_verify(ctx, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, 451 NULL); 452 # endif /* !TLS_VRFY_PER_CTX */ 453 } 454 455 /* 456 ** status in initialization 457 ** these flags keep track of the status of the initialization 458 ** i.e., whether a file exists (_EX) and whether it can be used (_OK) 459 ** [due to permissions] 460 */ 461 462 # define TLS_S_NONE 0x00000000 /* none yet */ 463 # define TLS_S_CERT_EX 0x00000001 /* cert file exists */ 464 # define TLS_S_CERT_OK 0x00000002 /* cert file is ok */ 465 # define TLS_S_KEY_EX 0x00000004 /* key file exists */ 466 # define TLS_S_KEY_OK 0x00000008 /* key file is ok */ 467 # define TLS_S_CERTP_EX 0x00000010 /* CA cert path exists */ 468 # define TLS_S_CERTP_OK 0x00000020 /* CA cert path is ok */ 469 # define TLS_S_CERTF_EX 0x00000040 /* CA cert file exists */ 470 # define TLS_S_CERTF_OK 0x00000080 /* CA cert file is ok */ 471 # define TLS_S_CRLF_EX 0x00000100 /* CRL file exists */ 472 # define TLS_S_CRLF_OK 0x00000200 /* CRL file is ok */ 473 474 # define TLS_S_CERT2_EX 0x00001000 /* 2nd cert file exists */ 475 # define TLS_S_CERT2_OK 0x00002000 /* 2nd cert file is ok */ 476 # define TLS_S_KEY2_EX 0x00004000 /* 2nd key file exists */ 477 # define TLS_S_KEY2_OK 0x00008000 /* 2nd key file is ok */ 478 479 # define TLS_S_DH_OK 0x00200000 /* DH cert is ok */ 480 # define TLS_S_DHPAR_EX 0x00400000 /* DH param file exists */ 481 # define TLS_S_DHPAR_OK 0x00800000 /* DH param file is ok to use */ 482 483 /* Type of variable */ 484 # define TLS_T_OTHER 0 485 # define TLS_T_SRV 1 486 # define TLS_T_CLT 2 487 488 /* 489 ** TLS_OK_F -- can var be an absolute filename? 490 ** 491 ** Parameters: 492 ** var -- filename 493 ** fn -- what is the filename used for? 494 ** type -- type of variable 495 ** 496 ** Returns: 497 ** ok? 498 */ 499 500 static bool 501 tls_ok_f(var, fn, type) 502 char *var; 503 char *fn; 504 int type; 505 { 506 /* must be absolute pathname */ 507 if (var != NULL && *var == '/') 508 return true; 509 if (LogLevel > 12) 510 sm_syslog(LOG_WARNING, NOQID, "STARTTLS: %s%s missing", 511 type == TLS_T_SRV ? "Server" : 512 (type == TLS_T_CLT ? "Client" : ""), fn); 513 return false; 514 } 515 /* 516 ** TLS_SAFE_F -- is a file safe to use? 517 ** 518 ** Parameters: 519 ** var -- filename 520 ** sff -- flags for safefile() 521 ** srv -- server side? 522 ** 523 ** Returns: 524 ** ok? 525 */ 526 527 static bool 528 tls_safe_f(var, sff, srv) 529 char *var; 530 long sff; 531 bool srv; 532 { 533 int ret; 534 535 if ((ret = safefile(var, RunAsUid, RunAsGid, RunAsUserName, sff, 536 S_IRUSR, NULL)) == 0) 537 return true; 538 if (LogLevel > 7) 539 sm_syslog(LOG_WARNING, NOQID, "STARTTLS=%s: file %s unsafe: %s", 540 srv ? "server" : "client", var, sm_errstring(ret)); 541 return false; 542 } 543 544 /* 545 ** TLS_OK_F -- macro to simplify calls to tls_ok_f 546 ** 547 ** Parameters: 548 ** var -- filename 549 ** fn -- what is the filename used for? 550 ** req -- is the file required? 551 ** st -- status bit to set if ok 552 ** type -- type of variable 553 ** 554 ** Side Effects: 555 ** uses r, ok; may change ok and status. 556 ** 557 */ 558 559 # define TLS_OK_F(var, fn, req, st, type) if (ok) \ 560 { \ 561 r = tls_ok_f(var, fn, type); \ 562 if (r) \ 563 status |= st; \ 564 else if (req) \ 565 ok = false; \ 566 } 567 568 /* 569 ** TLS_UNR -- macro to return whether a file should be unreadable 570 ** 571 ** Parameters: 572 ** bit -- flag to test 573 ** req -- flags 574 ** 575 ** Returns: 576 ** 0/SFF_NORFILES 577 */ 578 # define TLS_UNR(bit, req) (bitset(bit, req) ? SFF_NORFILES : 0) 579 # define TLS_OUNR(bit, req) (bitset(bit, req) ? SFF_NOWRFILES : 0) 580 # define TLS_KEYSFF(req) \ 581 (bitnset(DBS_GROUPREADABLEKEYFILE, DontBlameSendmail) ? \ 582 TLS_OUNR(TLS_I_KEY_OUNR, req) : \ 583 TLS_UNR(TLS_I_KEY_UNR, req)) 584 585 /* 586 ** TLS_SAFE_F -- macro to simplify calls to tls_safe_f 587 ** 588 ** Parameters: 589 ** var -- filename 590 ** sff -- flags for safefile() 591 ** req -- is the file required? 592 ** ex -- does the file exist? 593 ** st -- status bit to set if ok 594 ** srv -- server side? 595 ** 596 ** Side Effects: 597 ** uses r, ok, ex; may change ok and status. 598 ** 599 */ 600 601 # define TLS_SAFE_F(var, sff, req, ex, st, srv) if (ex && ok) \ 602 { \ 603 r = tls_safe_f(var, sff, srv); \ 604 if (r) \ 605 status |= st; \ 606 else if (req) \ 607 ok = false; \ 608 } 609 610 # if _FFR_TLS_SE_OPTS 611 /* 612 ** LOAD_CERTKEY -- load cert/key for TLS session 613 ** 614 ** Parameters: 615 ** ssl -- TLS session context 616 ** certfile -- filename of certificate 617 ** keyfile -- filename of private key 618 ** 619 ** Returns: 620 ** succeeded? 621 */ 622 623 bool 624 load_certkey(ssl, srv, certfile, keyfile) 625 SSL *ssl; 626 bool srv; 627 char *certfile; 628 char *keyfile; 629 { 630 bool ok; 631 int r; 632 long sff, status; 633 unsigned long req; 634 char *who; 635 636 ok = true; 637 who = srv ? "server" : "client"; 638 status = TLS_S_NONE; 639 req = TLS_I_CERT_EX|TLS_I_KEY_EX; 640 TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req), 641 TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT); 642 TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req), 643 TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT); 644 645 /* certfile etc. must be "safe". */ 646 sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK 647 | SFF_NOGWFILES | SFF_NOWWFILES 648 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT; 649 if (DontLockReadFiles) 650 sff |= SFF_NOLOCK; 651 652 TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req), 653 bitset(TLS_I_CERT_EX, req), 654 bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv); 655 TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req), 656 bitset(TLS_I_KEY_EX, req), 657 bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv); 658 659 # define SSL_use_cert(ssl, certfile) \ 660 SSL_use_certificate_file(ssl, certfile, SSL_FILETYPE_PEM) 661 # define SSL_USE_CERT "SSL_use_certificate_file" 662 663 if (bitset(TLS_S_CERT_OK, status) && 664 SSL_use_cert(ssl, certfile) <= 0) 665 { 666 if (LogLevel > 7) 667 { 668 sm_syslog(LOG_WARNING, NOQID, 669 "STARTTLS=%s, error: %s(%s) failed", 670 who, SSL_USE_CERT, certfile); 671 if (LogLevel > 9) 672 tlslogerr(LOG_WARNING, who); 673 } 674 if (bitset(TLS_I_USE_CERT, req)) 675 return false; 676 } 677 if (bitset(TLS_S_KEY_OK, status) && 678 SSL_use_PrivateKey_file(ssl, keyfile, SSL_FILETYPE_PEM) <= 0) 679 { 680 if (LogLevel > 7) 681 { 682 sm_syslog(LOG_WARNING, NOQID, 683 "STARTTLS=%s, error: SSL_use_PrivateKey_file(%s) failed", 684 who, keyfile); 685 if (LogLevel > 9) 686 tlslogerr(LOG_WARNING, who); 687 } 688 if (bitset(TLS_I_USE_KEY, req)) 689 return false; 690 } 691 692 /* check the private key */ 693 if (bitset(TLS_S_KEY_OK, status) && 694 (r = SSL_check_private_key(ssl)) <= 0) 695 { 696 /* Private key does not match the certificate public key */ 697 if (LogLevel > 5) 698 { 699 sm_syslog(LOG_WARNING, NOQID, 700 "STARTTLS=%s, error: SSL_check_private_key failed(%s): %d", 701 who, keyfile, r); 702 if (LogLevel > 9) 703 tlslogerr(LOG_WARNING, who); 704 } 705 if (bitset(TLS_I_USE_KEY, req)) 706 return false; 707 } 708 709 return true; 710 } 711 # endif /* _FFR_TLS_SE_OPTS */ 712 713 /* 714 ** INITTLS -- initialize TLS 715 ** 716 ** Parameters: 717 ** ctx -- pointer to context 718 ** req -- requirements for initialization (see sendmail.h) 719 ** options -- options 720 ** srv -- server side? 721 ** certfile -- filename of certificate 722 ** keyfile -- filename of private key 723 ** cacertpath -- path to CAs 724 ** cacertfile -- file with CA(s) 725 ** dhparam -- parameters for DH 726 ** 727 ** Returns: 728 ** succeeded? 729 */ 730 731 /* 732 ** The session_id_context identifies the service that created a session. 733 ** This information is used to distinguish between multiple TLS-based 734 ** servers running on the same server. We use the name of the mail system. 735 ** Note: the session cache is not persistent. 736 */ 737 738 static char server_session_id_context[] = "sendmail8"; 739 740 # if !TLS_NO_RSA 741 static RSA * 742 sm_RSA_generate_key(num, e) 743 int num; 744 unsigned long e; 745 { 746 RSA *rsa = NULL; 747 BIGNUM *bn_rsa_r4; 748 int rc; 749 750 bn_rsa_r4 = BN_new(); 751 rc = BN_set_word(bn_rsa_r4, RSA_F4); 752 if ((bn_rsa_r4 != NULL) && BN_set_word(bn_rsa_r4, RSA_F4) && (rsa = RSA_new()) != NULL) 753 { 754 if (!RSA_generate_key_ex(rsa, RSA_KEYLENGTH, bn_rsa_r4, NULL)) 755 { 756 RSA_free(rsa); 757 rsa = NULL; 758 } 759 return NULL; 760 } 761 BN_free(bn_rsa_r4); 762 return rsa; 763 } 764 # endif /* !TLS_NO_RSA */ 765 766 /* 0.9.8a and b have a problem with SSL_OP_TLS_BLOCK_PADDING_BUG */ 767 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) 768 # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 1 769 #else 770 # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 0 771 #endif 772 773 bool 774 inittls(ctx, req, options, srv, certfile, keyfile, cacertpath, cacertfile, dhparam) 775 SSL_CTX **ctx; 776 unsigned long req; 777 unsigned long options; 778 bool srv; 779 char *certfile, *keyfile, *cacertpath, *cacertfile, *dhparam; 780 { 781 # if !NO_DH 782 static DH *dh = NULL; 783 # endif /* !NO_DH */ 784 int r; 785 bool ok; 786 long sff, status; 787 char *who; 788 char *cf2, *kf2; 789 # if SM_CONF_SHM 790 extern int ShmId; 791 # endif /* SM_CONF_SHM */ 792 # if OPENSSL_VERSION_NUMBER > 0x00907000L 793 BIO *crl_file; 794 X509_CRL *crl; 795 X509_STORE *store; 796 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 797 #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG 798 long rt_version; 799 STACK_OF(SSL_COMP) *comp_methods; 800 #endif 801 802 status = TLS_S_NONE; 803 who = srv ? "server" : "client"; 804 if (ctx == NULL) 805 { 806 syserr("STARTTLS=%s, inittls: ctx == NULL", who); 807 /* NOTREACHED */ 808 SM_ASSERT(ctx != NULL); 809 } 810 811 /* already initialized? (we could re-init...) */ 812 if (*ctx != NULL) 813 return true; 814 ok = true; 815 816 /* 817 ** look for a second filename: it must be separated by a ',' 818 ** no blanks allowed (they won't be skipped). 819 ** we change a global variable here! this change will be undone 820 ** before return from the function but only if it returns true. 821 ** this isn't a problem since in a failure case this function 822 ** won't be called again with the same (overwritten) values. 823 ** otherwise each return must be replaced with a goto endinittls. 824 */ 825 826 cf2 = NULL; 827 kf2 = NULL; 828 if (certfile != NULL && (cf2 = strchr(certfile, ',')) != NULL) 829 { 830 *cf2++ = '\0'; 831 if (keyfile != NULL && (kf2 = strchr(keyfile, ',')) != NULL) 832 *kf2++ = '\0'; 833 } 834 835 /* 836 ** Check whether files/paths are defined 837 */ 838 839 TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req), 840 TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT); 841 TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req), 842 TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT); 843 TLS_OK_F(cacertpath, "CACertPath", bitset(TLS_I_CERTP_EX, req), 844 TLS_S_CERTP_EX, TLS_T_OTHER); 845 TLS_OK_F(cacertfile, "CACertFile", bitset(TLS_I_CERTF_EX, req), 846 TLS_S_CERTF_EX, TLS_T_OTHER); 847 848 # if OPENSSL_VERSION_NUMBER > 0x00907000L 849 TLS_OK_F(CRLFile, "CRLFile", bitset(TLS_I_CRLF_EX, req), 850 TLS_S_CRLF_EX, TLS_T_OTHER); 851 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 852 853 /* 854 ** if the second file is specified it must exist 855 ** XXX: it is possible here to define only one of those files 856 */ 857 858 if (cf2 != NULL) 859 { 860 TLS_OK_F(cf2, "CertFile", bitset(TLS_I_CERT_EX, req), 861 TLS_S_CERT2_EX, srv ? TLS_T_SRV : TLS_T_CLT); 862 } 863 if (kf2 != NULL) 864 { 865 TLS_OK_F(kf2, "KeyFile", bitset(TLS_I_KEY_EX, req), 866 TLS_S_KEY2_EX, srv ? TLS_T_SRV : TLS_T_CLT); 867 } 868 869 /* 870 ** valid values for dhparam are (only the first char is checked) 871 ** none no parameters: don't use DH 872 ** i use precomputed 2048 bit parameters 873 ** 512 use precomputed 512 bit parameters 874 ** 1024 generate 1024 bit parameters 875 ** 2048 generate 2048 bit parameters 876 ** /file/name read parameters from /file/name 877 */ 878 879 #define SET_DH_DFL \ 880 do { \ 881 dhparam = "I"; \ 882 req |= TLS_I_DHFIXED; \ 883 } while (0) 884 885 if (bitset(TLS_I_TRY_DH, req)) 886 { 887 if (dhparam != NULL) 888 { 889 char c = *dhparam; 890 891 if (c == '1') 892 req |= TLS_I_DH1024; 893 else if (c == 'I' || c == 'i') 894 req |= TLS_I_DHFIXED; 895 else if (c == '2') 896 req |= TLS_I_DH2048; 897 else if (c == '5') 898 req |= TLS_I_DH512; 899 else if (c == 'n' || c == 'N') 900 req &= ~TLS_I_TRY_DH; 901 else if (c != '/') 902 { 903 if (LogLevel > 12) 904 sm_syslog(LOG_WARNING, NOQID, 905 "STARTTLS=%s, error: illegal value '%s' for DHParameters", 906 who, dhparam); 907 dhparam = NULL; 908 } 909 } 910 if (dhparam == NULL) 911 SET_DH_DFL; 912 else if (*dhparam == '/') 913 { 914 TLS_OK_F(dhparam, "DHParameters", 915 bitset(TLS_I_DHPAR_EX, req), 916 TLS_S_DHPAR_EX, TLS_T_OTHER); 917 } 918 } 919 if (!ok) 920 return ok; 921 922 /* certfile etc. must be "safe". */ 923 sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK 924 | SFF_NOGWFILES | SFF_NOWWFILES 925 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT; 926 if (DontLockReadFiles) 927 sff |= SFF_NOLOCK; 928 929 TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req), 930 bitset(TLS_I_CERT_EX, req), 931 bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv); 932 TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req), 933 bitset(TLS_I_KEY_EX, req), 934 bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv); 935 TLS_SAFE_F(cacertfile, sff | TLS_UNR(TLS_I_CERTF_UNR, req), 936 bitset(TLS_I_CERTF_EX, req), 937 bitset(TLS_S_CERTF_EX, status), TLS_S_CERTF_OK, srv); 938 if (dhparam != NULL && *dhparam == '/') 939 { 940 TLS_SAFE_F(dhparam, sff | TLS_UNR(TLS_I_DHPAR_UNR, req), 941 bitset(TLS_I_DHPAR_EX, req), 942 bitset(TLS_S_DHPAR_EX, status), TLS_S_DHPAR_OK, srv); 943 if (!bitset(TLS_S_DHPAR_OK, status)) 944 SET_DH_DFL; 945 } 946 # if OPENSSL_VERSION_NUMBER > 0x00907000L 947 TLS_SAFE_F(CRLFile, sff | TLS_UNR(TLS_I_CRLF_UNR, req), 948 bitset(TLS_I_CRLF_EX, req), 949 bitset(TLS_S_CRLF_EX, status), TLS_S_CRLF_OK, srv); 950 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 951 if (!ok) 952 return ok; 953 if (cf2 != NULL) 954 { 955 TLS_SAFE_F(cf2, sff | TLS_UNR(TLS_I_CERT_UNR, req), 956 bitset(TLS_I_CERT_EX, req), 957 bitset(TLS_S_CERT2_EX, status), TLS_S_CERT2_OK, srv); 958 } 959 if (kf2 != NULL) 960 { 961 TLS_SAFE_F(kf2, sff | TLS_KEYSFF(req), 962 bitset(TLS_I_KEY_EX, req), 963 bitset(TLS_S_KEY2_EX, status), TLS_S_KEY2_OK, srv); 964 } 965 966 /* create a method and a new context */ 967 if ((*ctx = SSL_CTX_new(srv ? SSLv23_server_method() : 968 SSLv23_client_method())) == NULL) 969 { 970 if (LogLevel > 7) 971 sm_syslog(LOG_WARNING, NOQID, 972 "STARTTLS=%s, error: SSL_CTX_new(SSLv23_%s_method()) failed", 973 who, who); 974 if (LogLevel > 9) 975 tlslogerr(LOG_WARNING, who); 976 return false; 977 } 978 979 # if OPENSSL_VERSION_NUMBER > 0x00907000L 980 if (CRLFile != NULL) 981 { 982 /* get a pointer to the current certificate validation store */ 983 store = SSL_CTX_get_cert_store(*ctx); /* does not fail */ 984 crl_file = BIO_new(BIO_s_file()); 985 if (crl_file != NULL) 986 { 987 if (BIO_read_filename(crl_file, CRLFile) >= 0) 988 { 989 crl = PEM_read_bio_X509_CRL(crl_file, NULL, 990 NULL, NULL); 991 BIO_free(crl_file); 992 X509_STORE_add_crl(store, crl); 993 X509_CRL_free(crl); 994 X509_STORE_set_flags(store, 995 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); 996 X509_STORE_set_verify_cb_func(store, 997 x509_verify_cb); 998 } 999 else 1000 { 1001 if (LogLevel > 9) 1002 { 1003 sm_syslog(LOG_WARNING, NOQID, 1004 "STARTTLS=%s, error: PEM_read_bio_X509_CRL(%s)=failed", 1005 who, CRLFile); 1006 } 1007 1008 /* avoid memory leaks */ 1009 BIO_free(crl_file); 1010 return false; 1011 } 1012 1013 } 1014 else if (LogLevel > 9) 1015 sm_syslog(LOG_WARNING, NOQID, 1016 "STARTTLS=%s, error: BIO_new=failed", who); 1017 } 1018 else 1019 store = NULL; 1020 # if _FFR_CRLPATH 1021 if (CRLPath != NULL && store != NULL) 1022 { 1023 X509_LOOKUP *lookup; 1024 1025 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); 1026 if (lookup == NULL) 1027 { 1028 if (LogLevel > 9) 1029 { 1030 sm_syslog(LOG_WARNING, NOQID, 1031 "STARTTLS=%s, error: X509_STORE_add_lookup(hash)=failed", 1032 who, CRLFile); 1033 } 1034 return false; 1035 } 1036 X509_LOOKUP_add_dir(lookup, CRLPath, X509_FILETYPE_PEM); 1037 X509_STORE_set_flags(store, 1038 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); 1039 } 1040 # endif /* _FFR_CRLPATH */ 1041 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 1042 1043 # if TLS_NO_RSA 1044 /* turn off backward compatibility, required for no-rsa */ 1045 SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2); 1046 # endif /* TLS_NO_RSA */ 1047 1048 1049 # if !TLS_NO_RSA 1050 /* 1051 ** Create a temporary RSA key 1052 ** XXX Maybe we shouldn't create this always (even though it 1053 ** is only at startup). 1054 ** It is a time-consuming operation and it is not always necessary. 1055 ** maybe we should do it only on demand... 1056 */ 1057 1058 if (bitset(TLS_I_RSA_TMP, req) 1059 # if SM_CONF_SHM 1060 && ShmId != SM_SHM_NO_ID && 1061 (rsa_tmp = sm_RSA_generate_key(RSA_KEYLENGTH, RSA_F4)) == NULL 1062 # else /* SM_CONF_SHM */ 1063 && 0 /* no shared memory: no need to generate key now */ 1064 # endif /* SM_CONF_SHM */ 1065 ) 1066 { 1067 if (LogLevel > 7) 1068 { 1069 sm_syslog(LOG_WARNING, NOQID, 1070 "STARTTLS=%s, error: RSA_generate_key failed", 1071 who); 1072 if (LogLevel > 9) 1073 tlslogerr(LOG_WARNING, who); 1074 } 1075 return false; 1076 } 1077 # endif /* !TLS_NO_RSA */ 1078 1079 /* 1080 ** load private key 1081 ** XXX change this for DSA-only version 1082 */ 1083 1084 if (bitset(TLS_S_KEY_OK, status) && 1085 SSL_CTX_use_PrivateKey_file(*ctx, keyfile, 1086 SSL_FILETYPE_PEM) <= 0) 1087 { 1088 if (LogLevel > 7) 1089 { 1090 sm_syslog(LOG_WARNING, NOQID, 1091 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed", 1092 who, keyfile); 1093 if (LogLevel > 9) 1094 tlslogerr(LOG_WARNING, who); 1095 } 1096 if (bitset(TLS_I_USE_KEY, req)) 1097 return false; 1098 } 1099 1100 #if _FFR_TLS_USE_CERTIFICATE_CHAIN_FILE 1101 # define SSL_CTX_use_cert(ssl_ctx, certfile) \ 1102 SSL_CTX_use_certificate_chain_file(ssl_ctx, certfile) 1103 # define SSL_CTX_USE_CERT "SSL_CTX_use_certificate_chain_file" 1104 #else 1105 # define SSL_CTX_use_cert(ssl_ctx, certfile) \ 1106 SSL_CTX_use_certificate_file(ssl_ctx, certfile, SSL_FILETYPE_PEM) 1107 # define SSL_CTX_USE_CERT "SSL_CTX_use_certificate_file" 1108 #endif 1109 1110 /* get the certificate file */ 1111 if (bitset(TLS_S_CERT_OK, status) && 1112 SSL_CTX_use_cert(*ctx, certfile) <= 0) 1113 { 1114 if (LogLevel > 7) 1115 { 1116 sm_syslog(LOG_WARNING, NOQID, 1117 "STARTTLS=%s, error: %s(%s) failed", 1118 who, SSL_CTX_USE_CERT, certfile); 1119 if (LogLevel > 9) 1120 tlslogerr(LOG_WARNING, who); 1121 } 1122 if (bitset(TLS_I_USE_CERT, req)) 1123 return false; 1124 } 1125 1126 /* check the private key */ 1127 if (bitset(TLS_S_KEY_OK, status) && 1128 (r = SSL_CTX_check_private_key(*ctx)) <= 0) 1129 { 1130 /* Private key does not match the certificate public key */ 1131 if (LogLevel > 5) 1132 { 1133 sm_syslog(LOG_WARNING, NOQID, 1134 "STARTTLS=%s, error: SSL_CTX_check_private_key failed(%s): %d", 1135 who, keyfile, r); 1136 if (LogLevel > 9) 1137 tlslogerr(LOG_WARNING, who); 1138 } 1139 if (bitset(TLS_I_USE_KEY, req)) 1140 return false; 1141 } 1142 1143 /* XXX this code is pretty much duplicated from above! */ 1144 1145 /* load private key */ 1146 if (bitset(TLS_S_KEY2_OK, status) && 1147 SSL_CTX_use_PrivateKey_file(*ctx, kf2, SSL_FILETYPE_PEM) <= 0) 1148 { 1149 if (LogLevel > 7) 1150 { 1151 sm_syslog(LOG_WARNING, NOQID, 1152 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed", 1153 who, kf2); 1154 if (LogLevel > 9) 1155 tlslogerr(LOG_WARNING, who); 1156 } 1157 } 1158 1159 /* get the certificate file */ 1160 if (bitset(TLS_S_CERT2_OK, status) && 1161 SSL_CTX_use_cert(*ctx, cf2) <= 0) 1162 { 1163 if (LogLevel > 7) 1164 { 1165 sm_syslog(LOG_WARNING, NOQID, 1166 "STARTTLS=%s, error: %s(%s) failed", 1167 who, SSL_CTX_USE_CERT, cf2); 1168 if (LogLevel > 9) 1169 tlslogerr(LOG_WARNING, who); 1170 } 1171 } 1172 1173 /* also check the private key */ 1174 if (bitset(TLS_S_KEY2_OK, status) && 1175 (r = SSL_CTX_check_private_key(*ctx)) <= 0) 1176 { 1177 /* Private key does not match the certificate public key */ 1178 if (LogLevel > 5) 1179 { 1180 sm_syslog(LOG_WARNING, NOQID, 1181 "STARTTLS=%s, error: SSL_CTX_check_private_key 2 failed: %d", 1182 who, r); 1183 if (LogLevel > 9) 1184 tlslogerr(LOG_WARNING, who); 1185 } 1186 } 1187 1188 /* SSL_CTX_set_quiet_shutdown(*ctx, 1); violation of standard? */ 1189 1190 #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG 1191 1192 /* 1193 ** In OpenSSL 0.9.8[ab], enabling zlib compression breaks the 1194 ** padding bug work-around, leading to false positives and 1195 ** failed connections. We may not interoperate with systems 1196 ** with the bug, but this is better than breaking on all 0.9.8[ab] 1197 ** systems that have zlib support enabled. 1198 ** Note: this checks the runtime version of the library, not 1199 ** just the compile time version. 1200 */ 1201 1202 rt_version = SSLeay(); 1203 if (rt_version >= 0x00908000L && rt_version <= 0x0090802fL) 1204 { 1205 comp_methods = SSL_COMP_get_compression_methods(); 1206 if (comp_methods != NULL && sk_SSL_COMP_num(comp_methods) > 0) 1207 options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG; 1208 } 1209 #endif 1210 SSL_CTX_set_options(*ctx, (long) options); 1211 1212 # if !NO_DH 1213 /* Diffie-Hellman initialization */ 1214 if (bitset(TLS_I_TRY_DH, req)) 1215 { 1216 #if _FFR_TLS_EC 1217 EC_KEY *ecdh; 1218 #endif /* _FFR_TLS_EC */ 1219 1220 if (tTd(96, 8)) 1221 sm_dprintf("inittls: req=%#lx, status=%#lx\n", 1222 req, status); 1223 if (bitset(TLS_S_DHPAR_OK, status)) 1224 { 1225 BIO *bio; 1226 1227 if ((bio = BIO_new_file(dhparam, "r")) != NULL) 1228 { 1229 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); 1230 BIO_free(bio); 1231 if (dh == NULL && LogLevel > 7) 1232 { 1233 unsigned long err; 1234 1235 err = ERR_get_error(); 1236 sm_syslog(LOG_WARNING, NOQID, 1237 "STARTTLS=%s, error: cannot read DH parameters(%s): %s", 1238 who, dhparam, 1239 ERR_error_string(err, NULL)); 1240 if (LogLevel > 9) 1241 tlslogerr(LOG_WARNING, who); 1242 SET_DH_DFL; 1243 } 1244 } 1245 else 1246 { 1247 if (LogLevel > 5) 1248 { 1249 sm_syslog(LOG_WARNING, NOQID, 1250 "STARTTLS=%s, error: BIO_new_file(%s) failed", 1251 who, dhparam); 1252 if (LogLevel > 9) 1253 tlslogerr(LOG_WARNING, who); 1254 } 1255 } 1256 } 1257 if (dh == NULL && bitset(TLS_I_DH1024|TLS_I_DH2048, req)) 1258 { 1259 int bits; 1260 DSA *dsa; 1261 1262 bits = bitset(TLS_I_DH2048, req) ? 2048 : 1024; 1263 if (tTd(96, 2)) 1264 sm_dprintf("inittls: Generating %d bit DH parameters\n", bits); 1265 1266 dsa=DSA_new(); 1267 /* this takes a while! */ 1268 (void)DSA_generate_parameters_ex(dsa, bits, NULL, 0, 1269 NULL, NULL, NULL); 1270 dh = DSA_dup_DH(dsa); 1271 DSA_free(dsa); 1272 } 1273 else if (dh == NULL && bitset(TLS_I_DHFIXED, req)) 1274 { 1275 if (tTd(96, 2)) 1276 sm_dprintf("inittls: Using precomputed 2048 bit DH parameters\n"); 1277 dh = get_dh2048(); 1278 } 1279 else if (dh == NULL && bitset(TLS_I_DH512, req)) 1280 { 1281 if (tTd(96, 2)) 1282 sm_dprintf("inittls: Using precomputed 512 bit DH parameters\n"); 1283 dh = get_dh512(); 1284 } 1285 1286 if (dh == NULL) 1287 { 1288 if (LogLevel > 9) 1289 { 1290 unsigned long err; 1291 1292 err = ERR_get_error(); 1293 sm_syslog(LOG_WARNING, NOQID, 1294 "STARTTLS=%s, error: cannot read or set DH parameters(%s): %s", 1295 who, dhparam, 1296 ERR_error_string(err, NULL)); 1297 } 1298 if (bitset(TLS_I_REQ_DH, req)) 1299 return false; 1300 } 1301 else 1302 { 1303 /* important to avoid small subgroup attacks */ 1304 SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_DH_USE); 1305 1306 SSL_CTX_set_tmp_dh(*ctx, dh); 1307 if (LogLevel > 13) 1308 sm_syslog(LOG_INFO, NOQID, 1309 "STARTTLS=%s, Diffie-Hellman init, key=%d bit (%c)", 1310 who, 8 * DH_size(dh), *dhparam); 1311 DH_free(dh); 1312 } 1313 1314 #if _FFR_TLS_EC 1315 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 1316 if (ecdh != NULL) 1317 { 1318 SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_ECDH_USE); 1319 SSL_CTX_set_tmp_ecdh(*ctx, ecdh); 1320 EC_KEY_free(ecdh); 1321 } 1322 #endif /* _FFR_TLS_EC */ 1323 1324 } 1325 # endif /* !NO_DH */ 1326 1327 1328 /* XXX do we need this cache here? */ 1329 if (bitset(TLS_I_CACHE, req)) 1330 { 1331 SSL_CTX_sess_set_cache_size(*ctx, 1); 1332 SSL_CTX_set_timeout(*ctx, 1); 1333 SSL_CTX_set_session_id_context(*ctx, 1334 (void *) &server_session_id_context, 1335 sizeof(server_session_id_context)); 1336 (void) SSL_CTX_set_session_cache_mode(*ctx, 1337 SSL_SESS_CACHE_SERVER); 1338 } 1339 else 1340 { 1341 (void) SSL_CTX_set_session_cache_mode(*ctx, 1342 SSL_SESS_CACHE_OFF); 1343 } 1344 1345 /* load certificate locations and default CA paths */ 1346 if (bitset(TLS_S_CERTP_EX, status) && bitset(TLS_S_CERTF_EX, status)) 1347 { 1348 if ((r = SSL_CTX_load_verify_locations(*ctx, cacertfile, 1349 cacertpath)) == 1) 1350 { 1351 # if !TLS_NO_RSA 1352 if (bitset(TLS_I_RSA_TMP, req)) 1353 SSL_CTX_set_tmp_rsa_callback(*ctx, tmp_rsa_key); 1354 # endif /* !TLS_NO_RSA */ 1355 1356 /* 1357 ** We have to install our own verify callback: 1358 ** SSL_VERIFY_PEER requests a client cert but even 1359 ** though *FAIL_IF* isn't set, the connection 1360 ** will be aborted if the client presents a cert 1361 ** that is not "liked" (can't be verified?) by 1362 ** the TLS library :-( 1363 */ 1364 1365 /* 1366 ** XXX currently we could call tls_set_verify() 1367 ** but we hope that that function will later on 1368 ** only set the mode per connection. 1369 */ 1370 SSL_CTX_set_verify(*ctx, 1371 bitset(TLS_I_NO_VRFY, req) ? SSL_VERIFY_NONE 1372 : SSL_VERIFY_PEER, 1373 NULL); 1374 1375 /* install verify callback */ 1376 SSL_CTX_set_cert_verify_callback(*ctx, tls_verify_cb, 1377 NULL); 1378 SSL_CTX_set_client_CA_list(*ctx, 1379 SSL_load_client_CA_file(cacertfile)); 1380 } 1381 else 1382 { 1383 /* 1384 ** can't load CA data; do we care? 1385 ** the data is necessary to authenticate the client, 1386 ** which in turn would be necessary 1387 ** if we want to allow relaying based on it. 1388 */ 1389 if (LogLevel > 5) 1390 { 1391 sm_syslog(LOG_WARNING, NOQID, 1392 "STARTTLS=%s, error: load verify locs %s, %s failed: %d", 1393 who, cacertpath, cacertfile, r); 1394 if (LogLevel > 9) 1395 tlslogerr(LOG_WARNING, who); 1396 } 1397 if (bitset(TLS_I_VRFY_LOC, req)) 1398 return false; 1399 } 1400 } 1401 1402 /* XXX: make this dependent on an option? */ 1403 if (tTd(96, 9)) 1404 SSL_CTX_set_info_callback(*ctx, apps_ssl_info_cb); 1405 1406 /* install our own cipher list */ 1407 if (CipherList != NULL && *CipherList != '\0') 1408 { 1409 if (SSL_CTX_set_cipher_list(*ctx, CipherList) <= 0) 1410 { 1411 if (LogLevel > 7) 1412 { 1413 sm_syslog(LOG_WARNING, NOQID, 1414 "STARTTLS=%s, error: SSL_CTX_set_cipher_list(%s) failed, list ignored", 1415 who, CipherList); 1416 1417 if (LogLevel > 9) 1418 tlslogerr(LOG_WARNING, who); 1419 } 1420 /* failure if setting to this list is required? */ 1421 } 1422 } 1423 1424 if (LogLevel > 12) 1425 sm_syslog(LOG_INFO, NOQID, "STARTTLS=%s, init=%d", who, ok); 1426 1427 # if 0 1428 /* 1429 ** this label is required if we want to have a "clean" exit 1430 ** see the comments above at the initialization of cf2 1431 */ 1432 1433 endinittls: 1434 # endif /* 0 */ 1435 1436 /* undo damage to global variables */ 1437 if (cf2 != NULL) 1438 *--cf2 = ','; 1439 if (kf2 != NULL) 1440 *--kf2 = ','; 1441 1442 return ok; 1443 } 1444 1445 /* 1446 ** CERT_FP -- get cert fingerprint 1447 ** 1448 ** Parameters: 1449 ** cert -- TLS cert 1450 ** mac -- macro storage 1451 ** macro -- where to store cert fp 1452 ** 1453 ** Returns: 1454 ** <=0: cert fp calculation failed 1455 ** >0: cert fp calculation ok 1456 */ 1457 1458 static int 1459 cert_fp(cert, evp_digest, mac, macro) 1460 X509 *cert; 1461 const EVP_MD *evp_digest; 1462 MACROS_T *mac; 1463 char *macro; 1464 { 1465 unsigned int n; 1466 int r; 1467 unsigned char md[EVP_MAX_MD_SIZE]; 1468 char md5h[EVP_MAX_MD_SIZE * 3]; 1469 static const char hexcodes[] = "0123456789ABCDEF"; 1470 1471 n = 0; 1472 if (X509_digest(cert, EVP_digest, md, &n) == 0 || n <= 0) 1473 { 1474 macdefine(mac, A_TEMP, macid(macro), ""); 1475 return 0; 1476 } 1477 1478 SM_ASSERT((n * 3) + 2 < sizeof(md5h)); 1479 for (r = 0; r < (int) n; r++) 1480 { 1481 md5h[r * 3] = hexcodes[(md[r] & 0xf0) >> 4]; 1482 md5h[(r * 3) + 1] = hexcodes[(md[r] & 0x0f)]; 1483 md5h[(r * 3) + 2] = ':'; 1484 } 1485 md5h[(n * 3) - 1] = '\0'; 1486 macdefine(mac, A_TEMP, macid(macro), md5h); 1487 return 1; 1488 } 1489 1490 /* 1491 ** TLS_GET_INFO -- get information about TLS connection 1492 ** 1493 ** Parameters: 1494 ** ssl -- TLS connection structure 1495 ** srv -- server or client 1496 ** host -- hostname of other side 1497 ** mac -- macro storage 1498 ** certreq -- did we ask for a cert? 1499 ** 1500 ** Returns: 1501 ** result of authentication. 1502 ** 1503 ** Side Effects: 1504 ** sets various TLS related macros. 1505 */ 1506 1507 int 1508 tls_get_info(ssl, srv, host, mac, certreq) 1509 SSL *ssl; 1510 bool srv; 1511 char *host; 1512 MACROS_T *mac; 1513 bool certreq; 1514 { 1515 const SSL_CIPHER *c; 1516 int b, r; 1517 long verifyok; 1518 char *s, *who; 1519 char bitstr[16]; 1520 X509 *cert; 1521 1522 c = SSL_get_current_cipher(ssl); 1523 1524 /* cast is just workaround for compiler warning */ 1525 macdefine(mac, A_TEMP, macid("{cipher}"), 1526 (char *) SSL_CIPHER_get_name(c)); 1527 b = SSL_CIPHER_get_bits(c, &r); 1528 (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", b); 1529 macdefine(mac, A_TEMP, macid("{cipher_bits}"), bitstr); 1530 (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", r); 1531 macdefine(mac, A_TEMP, macid("{alg_bits}"), bitstr); 1532 s = (char *) SSL_get_version(ssl); 1533 if (s == NULL) 1534 s = "UNKNOWN"; 1535 macdefine(mac, A_TEMP, macid("{tls_version}"), s); 1536 1537 who = srv ? "server" : "client"; 1538 cert = SSL_get_peer_certificate(ssl); 1539 verifyok = SSL_get_verify_result(ssl); 1540 if (LogLevel > 14) 1541 sm_syslog(LOG_INFO, NOQID, 1542 "STARTTLS=%s, get_verify: %ld get_peer: 0x%lx", 1543 who, verifyok, (unsigned long) cert); 1544 if (cert != NULL) 1545 { 1546 X509_NAME *subj, *issuer; 1547 char buf[MAXNAME]; 1548 1549 subj = X509_get_subject_name(cert); 1550 issuer = X509_get_issuer_name(cert); 1551 X509_NAME_oneline(subj, buf, sizeof(buf)); 1552 macdefine(mac, A_TEMP, macid("{cert_subject}"), 1553 xtextify(buf, "<>\")")); 1554 X509_NAME_oneline(issuer, buf, sizeof(buf)); 1555 macdefine(mac, A_TEMP, macid("{cert_issuer}"), 1556 xtextify(buf, "<>\")")); 1557 1558 # define LL_BADCERT 8 1559 1560 #define CERTFPMACRO (CertFingerprintAlgorithm != NULL ? "{cert_fp}" : "{cert_md5}") 1561 1562 #define CHECK_X509_NAME(which) \ 1563 do { \ 1564 if (r == -1) \ 1565 { \ 1566 sm_strlcpy(buf, "BadCertificateUnknown", sizeof(buf)); \ 1567 if (LogLevel > LL_BADCERT) \ 1568 sm_syslog(LOG_INFO, NOQID, \ 1569 "STARTTLS=%s, relay=%.100s, field=%s, status=failed to extract CN", \ 1570 who, \ 1571 host == NULL ? "local" : host, \ 1572 which); \ 1573 } \ 1574 else if ((size_t)r >= sizeof(buf) - 1) \ 1575 { \ 1576 sm_strlcpy(buf, "BadCertificateTooLong", sizeof(buf)); \ 1577 if (LogLevel > 7) \ 1578 sm_syslog(LOG_INFO, NOQID, \ 1579 "STARTTLS=%s, relay=%.100s, field=%s, status=CN too long", \ 1580 who, \ 1581 host == NULL ? "local" : host, \ 1582 which); \ 1583 } \ 1584 else if ((size_t)r > strlen(buf)) \ 1585 { \ 1586 sm_strlcpy(buf, "BadCertificateContainsNUL", \ 1587 sizeof(buf)); \ 1588 if (LogLevel > 7) \ 1589 sm_syslog(LOG_INFO, NOQID, \ 1590 "STARTTLS=%s, relay=%.100s, field=%s, status=CN contains NUL", \ 1591 who, \ 1592 host == NULL ? "local" : host, \ 1593 which); \ 1594 } \ 1595 } while (0) 1596 1597 r = X509_NAME_get_text_by_NID(subj, NID_commonName, buf, 1598 sizeof buf); 1599 CHECK_X509_NAME("cn_subject"); 1600 macdefine(mac, A_TEMP, macid("{cn_subject}"), 1601 xtextify(buf, "<>\")")); 1602 r = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf, 1603 sizeof buf); 1604 CHECK_X509_NAME("cn_issuer"); 1605 macdefine(mac, A_TEMP, macid("{cn_issuer}"), 1606 xtextify(buf, "<>\")")); 1607 (void) cert_fp(cert, EVP_digest, mac, CERTFPMACRO); 1608 } 1609 else 1610 { 1611 macdefine(mac, A_PERM, macid("{cert_subject}"), ""); 1612 macdefine(mac, A_PERM, macid("{cert_issuer}"), ""); 1613 macdefine(mac, A_PERM, macid("{cn_subject}"), ""); 1614 macdefine(mac, A_PERM, macid("{cn_issuer}"), ""); 1615 macdefine(mac, A_TEMP, macid(CERTFPMACRO), ""); 1616 } 1617 switch (verifyok) 1618 { 1619 case X509_V_OK: 1620 if (cert != NULL) 1621 { 1622 s = "OK"; 1623 r = TLS_AUTH_OK; 1624 } 1625 else 1626 { 1627 s = certreq ? "NO" : "NOT", 1628 r = TLS_AUTH_NO; 1629 } 1630 break; 1631 default: 1632 s = "FAIL"; 1633 r = TLS_AUTH_FAIL; 1634 break; 1635 } 1636 macdefine(mac, A_PERM, macid("{verify}"), s); 1637 if (cert != NULL) 1638 X509_free(cert); 1639 1640 /* do some logging */ 1641 if (LogLevel > 8) 1642 { 1643 char *vers, *s1, *s2, *cbits, *algbits; 1644 1645 vers = macget(mac, macid("{tls_version}")); 1646 cbits = macget(mac, macid("{cipher_bits}")); 1647 algbits = macget(mac, macid("{alg_bits}")); 1648 s1 = macget(mac, macid("{verify}")); 1649 s2 = macget(mac, macid("{cipher}")); 1650 1651 /* XXX: maybe cut off ident info? */ 1652 sm_syslog(LOG_INFO, NOQID, 1653 "STARTTLS=%s, relay=%.100s, version=%.16s, verify=%.16s, cipher=%.64s, bits=%.6s/%.6s", 1654 who, 1655 host == NULL ? "local" : host, 1656 vers, s1, s2, /* sm_snprintf() can deal with NULL */ 1657 algbits == NULL ? "0" : algbits, 1658 cbits == NULL ? "0" : cbits); 1659 if (LogLevel > 11) 1660 { 1661 /* 1662 ** Maybe run xuntextify on the strings? 1663 ** That is easier to read but makes it maybe a bit 1664 ** more complicated to figure out the right values 1665 ** for the access map... 1666 */ 1667 1668 s1 = macget(mac, macid("{cert_subject}")); 1669 s2 = macget(mac, macid("{cert_issuer}")); 1670 sm_syslog(LOG_INFO, NOQID, 1671 "STARTTLS=%s, cert-subject=%.256s, cert-issuer=%.256s, verifymsg=%s", 1672 who, s1, s2, 1673 X509_verify_cert_error_string(verifyok)); 1674 } 1675 } 1676 return r; 1677 } 1678 /* 1679 ** ENDTLS -- shutdown secure connection 1680 ** 1681 ** Parameters: 1682 ** ssl -- SSL connection information. 1683 ** side -- server/client (for logging). 1684 ** 1685 ** Returns: 1686 ** success? (EX_* code) 1687 */ 1688 1689 int 1690 endtls(ssl, side) 1691 SSL *ssl; 1692 char *side; 1693 { 1694 int ret = EX_OK; 1695 1696 if (ssl != NULL) 1697 { 1698 int r; 1699 1700 if ((r = SSL_shutdown(ssl)) < 0) 1701 { 1702 if (LogLevel > 11) 1703 { 1704 sm_syslog(LOG_WARNING, NOQID, 1705 "STARTTLS=%s, SSL_shutdown failed: %d", 1706 side, r); 1707 tlslogerr(LOG_WARNING, side); 1708 } 1709 ret = EX_SOFTWARE; 1710 } 1711 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL 1712 1713 /* 1714 ** Bug in OpenSSL (at least up to 0.9.6b): 1715 ** From: Lutz.Jaenicke@aet.TU-Cottbus.DE 1716 ** Message-ID: <20010723152244.A13122@serv01.aet.tu-cottbus.de> 1717 ** To: openssl-users@openssl.org 1718 ** Subject: Re: SSL_shutdown() woes (fwd) 1719 ** 1720 ** The side sending the shutdown alert first will 1721 ** not care about the answer of the peer but will 1722 ** immediately return with a return value of "0" 1723 ** (ssl/s3_lib.c:ssl3_shutdown()). SSL_get_error will evaluate 1724 ** the value of "0" and as the shutdown alert of the peer was 1725 ** not received (actually, the program did not even wait for 1726 ** the answer), an SSL_ERROR_SYSCALL is flagged, because this 1727 ** is the default rule in case everything else does not apply. 1728 ** 1729 ** For your server the problem is different, because it 1730 ** receives the shutdown first (setting SSL_RECEIVED_SHUTDOWN), 1731 ** then sends its response (SSL_SENT_SHUTDOWN), so for the 1732 ** server the shutdown was successfull. 1733 ** 1734 ** As is by know, you would have to call SSL_shutdown() once 1735 ** and ignore an SSL_ERROR_SYSCALL returned. Then call 1736 ** SSL_shutdown() again to actually get the server's response. 1737 ** 1738 ** In the last discussion, Bodo Moeller concluded that a 1739 ** rewrite of the shutdown code would be necessary, but 1740 ** probably with another API, as the change would not be 1741 ** compatible to the way it is now. Things do not become 1742 ** easier as other programs do not follow the shutdown 1743 ** guidelines anyway, so that a lot error conditions and 1744 ** compitibility issues would have to be caught. 1745 ** 1746 ** For now the recommondation is to ignore the error message. 1747 */ 1748 1749 else if (r == 0) 1750 { 1751 if (LogLevel > 15) 1752 { 1753 sm_syslog(LOG_WARNING, NOQID, 1754 "STARTTLS=%s, SSL_shutdown not done", 1755 side); 1756 tlslogerr(LOG_WARNING, side); 1757 } 1758 ret = EX_SOFTWARE; 1759 } 1760 # endif /* !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL */ 1761 SSL_free(ssl); 1762 ssl = NULL; 1763 } 1764 return ret; 1765 } 1766 1767 # if !TLS_NO_RSA 1768 /* 1769 ** TMP_RSA_KEY -- return temporary RSA key 1770 ** 1771 ** Parameters: 1772 ** s -- TLS connection structure 1773 ** export -- 1774 ** keylength -- 1775 ** 1776 ** Returns: 1777 ** temporary RSA key. 1778 */ 1779 1780 # ifndef MAX_RSA_TMP_CNT 1781 # define MAX_RSA_TMP_CNT 1000 /* XXX better value? */ 1782 # endif /* ! MAX_RSA_TMP_CNT */ 1783 1784 /* ARGUSED0 */ 1785 static RSA * 1786 tmp_rsa_key(s, export, keylength) 1787 SSL *s; 1788 int export; 1789 int keylength; 1790 { 1791 # if SM_CONF_SHM 1792 extern int ShmId; 1793 extern int *PRSATmpCnt; 1794 1795 if (ShmId != SM_SHM_NO_ID && rsa_tmp != NULL && 1796 ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT) 1797 return rsa_tmp; 1798 # endif /* SM_CONF_SHM */ 1799 1800 if (rsa_tmp != NULL) 1801 RSA_free(rsa_tmp); 1802 rsa_tmp = sm_RSA_generate_key(RSA_KEYLENGTH, RSA_F4); 1803 if (rsa_tmp == NULL) 1804 { 1805 if (LogLevel > 0) 1806 sm_syslog(LOG_ERR, NOQID, 1807 "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!"); 1808 } 1809 else 1810 { 1811 # if SM_CONF_SHM 1812 # if 0 1813 /* 1814 ** XXX we can't (yet) share the new key... 1815 ** The RSA structure contains pointers hence it can't be 1816 ** easily kept in shared memory. It must be transformed 1817 ** into a continous memory region first, then stored, 1818 ** and later read out again (each time re-transformed). 1819 */ 1820 1821 if (ShmId != SM_SHM_NO_ID) 1822 *PRSATmpCnt = 0; 1823 # endif /* 0 */ 1824 # endif /* SM_CONF_SHM */ 1825 if (LogLevel > 9) 1826 sm_syslog(LOG_ERR, NOQID, 1827 "STARTTLS=server, tmp_rsa_key: new temp RSA key"); 1828 } 1829 return rsa_tmp; 1830 } 1831 # endif /* !TLS_NO_RSA */ 1832 /* 1833 ** APPS_SSL_INFO_CB -- info callback for TLS connections 1834 ** 1835 ** Parameters: 1836 ** s -- TLS connection structure 1837 ** where -- state in handshake 1838 ** ret -- return code of last operation 1839 ** 1840 ** Returns: 1841 ** none. 1842 */ 1843 1844 static void 1845 apps_ssl_info_cb(s, where, ret) 1846 CONST097 SSL *s; 1847 int where; 1848 int ret; 1849 { 1850 int w; 1851 char *str; 1852 BIO *bio_err = NULL; 1853 1854 if (LogLevel > 14) 1855 sm_syslog(LOG_INFO, NOQID, 1856 "STARTTLS: info_callback where=0x%x, ret=%d", 1857 where, ret); 1858 1859 w = where & ~SSL_ST_MASK; 1860 if (bio_err == NULL) 1861 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); 1862 1863 if (bitset(SSL_ST_CONNECT, w)) 1864 str = "SSL_connect"; 1865 else if (bitset(SSL_ST_ACCEPT, w)) 1866 str = "SSL_accept"; 1867 else 1868 str = "undefined"; 1869 1870 if (bitset(SSL_CB_LOOP, where)) 1871 { 1872 if (LogLevel > 12) 1873 sm_syslog(LOG_NOTICE, NOQID, 1874 "STARTTLS: %s:%s", 1875 str, SSL_state_string_long(s)); 1876 } 1877 else if (bitset(SSL_CB_ALERT, where)) 1878 { 1879 str = bitset(SSL_CB_READ, where) ? "read" : "write"; 1880 if (LogLevel > 12) 1881 sm_syslog(LOG_NOTICE, NOQID, 1882 "STARTTLS: SSL3 alert %s:%s:%s", 1883 str, SSL_alert_type_string_long(ret), 1884 SSL_alert_desc_string_long(ret)); 1885 } 1886 else if (bitset(SSL_CB_EXIT, where)) 1887 { 1888 if (ret == 0) 1889 { 1890 if (LogLevel > 7) 1891 sm_syslog(LOG_WARNING, NOQID, 1892 "STARTTLS: %s:failed in %s", 1893 str, SSL_state_string_long(s)); 1894 } 1895 else if (ret < 0) 1896 { 1897 if (LogLevel > 7) 1898 sm_syslog(LOG_WARNING, NOQID, 1899 "STARTTLS: %s:error in %s", 1900 str, SSL_state_string_long(s)); 1901 } 1902 } 1903 } 1904 /* 1905 ** TLS_VERIFY_LOG -- log verify error for TLS certificates 1906 ** 1907 ** Parameters: 1908 ** ok -- verify ok? 1909 ** ctx -- x509 context 1910 ** name -- from where is this called? 1911 ** 1912 ** Returns: 1913 ** 1 -- ok 1914 */ 1915 1916 static int 1917 tls_verify_log(ok, ctx, name) 1918 int ok; 1919 X509_STORE_CTX *ctx; 1920 const char *name; 1921 { 1922 X509 *cert; 1923 int reason, depth; 1924 char buf[512]; 1925 1926 cert = X509_STORE_CTX_get_current_cert(ctx); 1927 reason = X509_STORE_CTX_get_error(ctx); 1928 depth = X509_STORE_CTX_get_error_depth(ctx); 1929 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf)); 1930 sm_syslog(LOG_INFO, NOQID, 1931 "STARTTLS: %s cert verify: depth=%d %s, state=%d, reason=%s", 1932 name, depth, buf, ok, X509_verify_cert_error_string(reason)); 1933 return 1; 1934 } 1935 1936 /* 1937 ** TLS_VERIFY_CB -- verify callback for TLS certificates 1938 ** 1939 ** Parameters: 1940 ** ctx -- x509 context 1941 ** 1942 ** Returns: 1943 ** accept connection? 1944 ** currently: always yes. 1945 */ 1946 1947 static int 1948 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L 1949 tls_verify_cb(ctx) 1950 X509_STORE_CTX *ctx; 1951 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 1952 tls_verify_cb(ctx, unused) 1953 X509_STORE_CTX *ctx; 1954 void *unused; 1955 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */ 1956 { 1957 int ok; 1958 1959 /* 1960 ** man SSL_CTX_set_cert_verify_callback(): 1961 ** callback should return 1 to indicate verification success 1962 ** and 0 to indicate verification failure. 1963 */ 1964 1965 ok = X509_verify_cert(ctx); 1966 if (ok <= 0) 1967 { 1968 if (LogLevel > 13) 1969 return tls_verify_log(ok, ctx, "TLS"); 1970 } 1971 return 1; 1972 } 1973 /* 1974 ** TLSLOGERR -- log the errors from the TLS error stack 1975 ** 1976 ** Parameters: 1977 ** level -- syslog level 1978 ** who -- server/client (for logging). 1979 ** 1980 ** Returns: 1981 ** none. 1982 */ 1983 1984 void 1985 tlslogerr(level, who) 1986 int level; 1987 const char *who; 1988 { 1989 unsigned long l; 1990 int line, flags; 1991 unsigned long es; 1992 char *file, *data; 1993 char buf[256]; 1994 1995 es = CRYPTO_thread_id(); 1996 while ((l = ERR_get_error_line_data((const char **) &file, &line, 1997 (const char **) &data, &flags)) 1998 != 0) 1999 { 2000 sm_syslog(level, NOQID, 2001 "STARTTLS=%s: %lu:%s:%s:%d:%s", who, es, 2002 ERR_error_string(l, buf), 2003 file, line, 2004 bitset(ERR_TXT_STRING, flags) ? data : ""); 2005 } 2006 } 2007 2008 # if OPENSSL_VERSION_NUMBER > 0x00907000L 2009 /* 2010 ** X509_VERIFY_CB -- verify callback 2011 ** 2012 ** Parameters: 2013 ** ctx -- x509 context 2014 ** 2015 ** Returns: 2016 ** accept connection? 2017 ** currently: always yes. 2018 */ 2019 2020 static int 2021 x509_verify_cb(ok, ctx) 2022 int ok; 2023 X509_STORE_CTX *ctx; 2024 { 2025 if (ok == 0) 2026 { 2027 if (LogLevel > 13) 2028 tls_verify_log(ok, ctx, "x509"); 2029 if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_CRL) 2030 { 2031 X509_STORE_CTX_set_error(ctx, 0); 2032 return 1; /* override it */ 2033 } 2034 } 2035 return ok; 2036 } 2037 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ 2038 #endif /* STARTTLS */ 2039