1 /* 2 * Copyright (c) 2000-2001 Sendmail, 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.75 2001/09/11 04:05:17 gshapiro Exp $") 14 15 #if STARTTLS 16 # include <openssl/err.h> 17 # include <openssl/bio.h> 18 # include <openssl/pem.h> 19 # ifndef HASURANDOMDEV 20 # include <openssl/rand.h> 21 # endif /* ! HASURANDOMDEV */ 22 # if SM_CONF_SHM 23 # include <sm/shm.h> 24 # endif /* SM_CONF_SHM */ 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 static int tls_verify_cb __P((X509_STORE_CTX *)); 30 31 static void apps_ssl_info_cb __P((SSL *, int , int)); 32 33 # if !NO_DH 34 static DH *get_dh512 __P((void)); 35 36 static unsigned char dh512_p[] = 37 { 38 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75, 39 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F, 40 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3, 41 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12, 42 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C, 43 0x47,0x74,0xE8,0x33 44 }; 45 static unsigned char dh512_g[] = 46 { 47 0x02 48 }; 49 50 static DH * 51 get_dh512() 52 { 53 DH *dh = NULL; 54 55 if ((dh = DH_new()) == NULL) 56 return NULL; 57 dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL); 58 dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL); 59 if ((dh->p == NULL) || (dh->g == NULL)) 60 return NULL; 61 return dh; 62 } 63 # endif /* !NO_DH */ 64 65 66 /* 67 ** TLS_RAND_INIT -- initialize STARTTLS random generator 68 ** 69 ** Parameters: 70 ** randfile -- name of file with random data 71 ** logl -- loglevel 72 ** 73 ** Returns: 74 ** success/failure 75 ** 76 ** Side Effects: 77 ** initializes PRNG for tls library. 78 */ 79 80 # define MIN_RAND_BYTES 128 /* 1024 bits */ 81 82 # define RF_OK 0 /* randfile OK */ 83 # define RF_MISS 1 /* randfile == NULL || *randfile == '\0' */ 84 # define RF_UNKNOWN 2 /* unknown prefix for randfile */ 85 86 # define RI_NONE 0 /* no init yet */ 87 # define RI_SUCCESS 1 /* init was successful */ 88 # define RI_FAIL 2 /* init failed */ 89 90 static bool tls_rand_init __P((char *, int)); 91 92 static bool 93 tls_rand_init(randfile, logl) 94 char *randfile; 95 int logl; 96 { 97 # ifndef HASURANDOMDEV 98 /* not required if /dev/urandom exists, OpenSSL does it internally */ 99 100 bool ok; 101 int randdef; 102 static int done = RI_NONE; 103 104 /* 105 ** initialize PRNG 106 */ 107 108 /* did we try this before? if yes: return old value */ 109 if (done != RI_NONE) 110 return done == RI_SUCCESS; 111 112 /* set default values */ 113 ok = false; 114 done = RI_FAIL; 115 randdef = (randfile == NULL || *randfile == '\0') ? RF_MISS : RF_OK; 116 # if EGD 117 if (randdef == RF_OK && sm_strncasecmp(randfile, "egd:", 4) == 0) 118 { 119 randfile += 4; 120 if (RAND_egd(randfile) < 0) 121 { 122 sm_syslog(LOG_WARNING, NOQID, 123 "STARTTLS: RAND_egd(%s) failed: random number generator not seeded", 124 randfile); 125 } 126 else 127 ok = true; 128 } 129 else 130 # endif /* EGD */ 131 if (randdef == RF_OK && sm_strncasecmp(randfile, "file:", 5) == 0) 132 { 133 int fd; 134 long sff; 135 struct stat st; 136 137 randfile += 5; 138 sff = SFF_SAFEDIRPATH | SFF_NOWLINK 139 | SFF_NOGWFILES | SFF_NOWWFILES 140 | SFF_NOGRFILES | SFF_NOWRFILES 141 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT; 142 if ((fd = safeopen(randfile, O_RDONLY, 0, sff)) >= 0) 143 { 144 if (fstat(fd, &st) < 0) 145 { 146 if (LogLevel > logl) 147 sm_syslog(LOG_ERR, NOQID, 148 "STARTTLS: can't fstat(%s)", 149 randfile); 150 } 151 else 152 { 153 bool use, problem; 154 155 use = true; 156 problem = false; 157 158 /* max. age of file: 10 minutes */ 159 if (st.st_mtime + 600 < curtime()) 160 { 161 use = bitnset(DBS_INSUFFICIENTENTROPY, 162 DontBlameSendmail); 163 problem = true; 164 if (LogLevel > logl) 165 sm_syslog(LOG_ERR, NOQID, 166 "STARTTLS: RandFile %s too old: %s", 167 randfile, 168 use ? "unsafe" : 169 "unusable"); 170 } 171 if (use && st.st_size < MIN_RAND_BYTES) 172 { 173 use = bitnset(DBS_INSUFFICIENTENTROPY, 174 DontBlameSendmail); 175 problem = true; 176 if (LogLevel > logl) 177 sm_syslog(LOG_ERR, NOQID, 178 "STARTTLS: size(%s) < %d: %s", 179 randfile, 180 MIN_RAND_BYTES, 181 use ? "unsafe" : 182 "unusable"); 183 } 184 if (use) 185 ok = RAND_load_file(randfile, -1) >= 186 MIN_RAND_BYTES; 187 if (use && !ok) 188 { 189 if (LogLevel > logl) 190 sm_syslog(LOG_WARNING, NOQID, 191 "STARTTLS: RAND_load_file(%s) failed: random number generator not seeded", 192 randfile); 193 } 194 if (problem) 195 ok = false; 196 } 197 if (ok || bitnset(DBS_INSUFFICIENTENTROPY, 198 DontBlameSendmail)) 199 { 200 /* add this even if fstat() failed */ 201 RAND_seed((void *) &st, sizeof st); 202 } 203 (void) close(fd); 204 } 205 else 206 { 207 if (LogLevel > logl) 208 sm_syslog(LOG_WARNING, NOQID, 209 "STARTTLS: Warning: safeopen(%s) failed", 210 randfile); 211 } 212 } 213 else if (randdef == RF_OK) 214 { 215 if (LogLevel > logl) 216 sm_syslog(LOG_WARNING, NOQID, 217 "STARTTLS: Error: no proper random file definition %s", 218 randfile); 219 randdef = RF_UNKNOWN; 220 } 221 if (randdef == RF_MISS) 222 { 223 if (LogLevel > logl) 224 sm_syslog(LOG_WARNING, NOQID, 225 "STARTTLS: Error: missing random file definition"); 226 } 227 if (!ok && bitnset(DBS_INSUFFICIENTENTROPY, DontBlameSendmail)) 228 { 229 int i; 230 long r; 231 unsigned char buf[MIN_RAND_BYTES]; 232 233 /* assert((MIN_RAND_BYTES % sizeof(long)) == 0); */ 234 for (i = 0; i <= sizeof(buf) - sizeof(long); i += sizeof(long)) 235 { 236 r = get_random(); 237 (void) memcpy(buf + i, (void *) &r, sizeof(long)); 238 } 239 RAND_seed(buf, sizeof buf); 240 if (LogLevel > logl) 241 sm_syslog(LOG_WARNING, NOQID, 242 "STARTTLS: Warning: random number generator not properly seeded"); 243 ok = true; 244 } 245 done = ok ? RI_SUCCESS : RI_FAIL; 246 return ok; 247 # else /* ! HASURANDOMDEV */ 248 return true; 249 # endif /* ! HASURANDOMDEV */ 250 } 251 /* 252 ** INIT_TLS_LIBRARY -- Calls functions which setup TLS library for global use. 253 ** 254 ** Parameters: 255 ** none. 256 ** 257 ** Returns: 258 ** succeeded? 259 */ 260 261 bool 262 init_tls_library() 263 { 264 /* basic TLS initialization, ignore result for now */ 265 SSL_library_init(); 266 SSL_load_error_strings(); 267 # if 0 268 /* this is currently a macro for SSL_library_init */ 269 SSLeay_add_ssl_algorithms(); 270 # endif /* 0 */ 271 272 return tls_rand_init(RandFile, 7); 273 } 274 /* 275 ** TLS_SET_VERIFY -- request client certificate? 276 ** 277 ** Parameters: 278 ** ctx -- TLS context 279 ** ssl -- TLS structure 280 ** vrfy -- require certificate? 281 ** 282 ** Returns: 283 ** none. 284 ** 285 ** Side Effects: 286 ** Sets verification state for TLS 287 ** 288 # if TLS_VRFY_PER_CTX 289 ** Notice: 290 ** This is per TLS context, not per TLS structure; 291 ** the former is global, the latter per connection. 292 ** It would be nice to do this per connection, but this 293 ** doesn't work in the current TLS libraries :-( 294 # endif * TLS_VRFY_PER_CTX * 295 */ 296 297 void 298 tls_set_verify(ctx, ssl, vrfy) 299 SSL_CTX *ctx; 300 SSL *ssl; 301 bool vrfy; 302 { 303 # if !TLS_VRFY_PER_CTX 304 SSL_set_verify(ssl, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL); 305 # else /* !TLS_VRFY_PER_CTX */ 306 SSL_CTX_set_verify(ctx, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, 307 NULL); 308 # endif /* !TLS_VRFY_PER_CTX */ 309 } 310 311 /* 312 ** status in initialization 313 ** these flags keep track of the status of the initialization 314 ** i.e., whether a file exists (_EX) and whether it can be used (_OK) 315 ** [due to permissions] 316 */ 317 318 # define TLS_S_NONE 0x00000000 /* none yet */ 319 # define TLS_S_CERT_EX 0x00000001 /* CERT file exists */ 320 # define TLS_S_CERT_OK 0x00000002 /* CERT file is ok */ 321 # define TLS_S_KEY_EX 0x00000004 /* KEY file exists */ 322 # define TLS_S_KEY_OK 0x00000008 /* KEY file is ok */ 323 # define TLS_S_CERTP_EX 0x00000010 /* CA CERT PATH exists */ 324 # define TLS_S_CERTP_OK 0x00000020 /* CA CERT PATH is ok */ 325 # define TLS_S_CERTF_EX 0x00000040 /* CA CERT FILE exists */ 326 # define TLS_S_CERTF_OK 0x00000080 /* CA CERT FILE is ok */ 327 328 # if _FFR_TLS_1 329 # define TLS_S_CERT2_EX 0x00001000 /* 2nd CERT file exists */ 330 # define TLS_S_CERT2_OK 0x00002000 /* 2nd CERT file is ok */ 331 # define TLS_S_KEY2_EX 0x00004000 /* 2nd KEY file exists */ 332 # define TLS_S_KEY2_OK 0x00008000 /* 2nd KEY file is ok */ 333 # endif /* _FFR_TLS_1 */ 334 335 # define TLS_S_DH_OK 0x00200000 /* DH cert is ok */ 336 # define TLS_S_DHPAR_EX 0x00400000 /* DH param file exists */ 337 # define TLS_S_DHPAR_OK 0x00800000 /* DH param file is ok to use */ 338 339 /* 340 ** TLS_OK_F -- can var be an absolute filename? 341 ** 342 ** Parameters: 343 ** var -- filename 344 ** fn -- what is the filename used for? 345 ** srv -- server side? 346 ** 347 ** Returns: 348 ** ok? 349 */ 350 351 static bool 352 tls_ok_f(var, fn, srv) 353 char *var; 354 char *fn; 355 bool srv; 356 { 357 /* must be absolute pathname */ 358 if (var != NULL && *var == '/') 359 return true; 360 if (LogLevel > 12) 361 sm_syslog(LOG_WARNING, NOQID, "STARTTLS: %s%s missing", 362 srv ? "Server" : "Client", fn); 363 return false; 364 } 365 /* 366 ** TLS_SAFE_F -- is a file safe to use? 367 ** 368 ** Parameters: 369 ** var -- filename 370 ** sff -- flags for safefile() 371 ** srv -- server side? 372 ** 373 ** Returns: 374 ** ok? 375 */ 376 377 static bool 378 tls_safe_f(var, sff, srv) 379 char *var; 380 long sff; 381 bool srv; 382 { 383 int ret; 384 385 if ((ret = safefile(var, RunAsUid, RunAsGid, RunAsUserName, sff, 386 S_IRUSR, NULL)) == 0) 387 return true; 388 if (LogLevel > 7) 389 sm_syslog(LOG_WARNING, NOQID, "STARTTLS=%s: file %s unsafe: %s", 390 srv ? "server" : "client", var, sm_errstring(ret)); 391 return false; 392 } 393 394 /* 395 ** TLS_OK_F -- macro to simplify calls to tls_ok_f 396 ** 397 ** Parameters: 398 ** var -- filename 399 ** fn -- what is the filename used for? 400 ** req -- is the file required? 401 ** st -- status bit to set if ok 402 ** srv -- server side? 403 ** 404 ** Side Effects: 405 ** uses r, ok; may change ok and status. 406 ** 407 */ 408 409 # define TLS_OK_F(var, fn, req, st, srv) if (ok) \ 410 { \ 411 r = tls_ok_f(var, fn, srv); \ 412 if (r) \ 413 status |= st; \ 414 else if (req) \ 415 ok = false; \ 416 } 417 418 /* 419 ** TLS_UNR -- macro to return whether a file should be unreadable 420 ** 421 ** Parameters: 422 ** bit -- flag to test 423 ** req -- flags 424 ** 425 ** Returns: 426 ** 0/SFF_NORFILES 427 */ 428 # define TLS_UNR(bit, req) (bitset(bit, req) ? SFF_NORFILES : 0) 429 # define TLS_OUNR(bit, req) (bitset(bit, req) ? SFF_NOWRFILES : 0) 430 # define TLS_KEYSFF(req) \ 431 (bitnset(DBS_GROUPREADABLEKEYFILE, DontBlameSendmail) ? \ 432 TLS_OUNR(TLS_I_KEY_OUNR, req) : \ 433 TLS_UNR(TLS_I_KEY_UNR, req)) 434 435 /* 436 ** TLS_SAFE_F -- macro to simplify calls to tls_safe_f 437 ** 438 ** Parameters: 439 ** var -- filename 440 ** sff -- flags for safefile() 441 ** req -- is the file required? 442 ** ex -- does the file exist? 443 ** st -- status bit to set if ok 444 ** srv -- server side? 445 ** 446 ** Side Effects: 447 ** uses r, ok, ex; may change ok and status. 448 ** 449 */ 450 451 # define TLS_SAFE_F(var, sff, req, ex, st, srv) if (ex && ok) \ 452 { \ 453 r = tls_safe_f(var, sff, srv); \ 454 if (r) \ 455 status |= st; \ 456 else if (req) \ 457 ok = false; \ 458 } 459 460 /* 461 ** INITTLS -- initialize TLS 462 ** 463 ** Parameters: 464 ** ctx -- pointer to context 465 ** req -- requirements for initialization (see sendmail.h) 466 ** srv -- server side? 467 ** certfile -- filename of certificate 468 ** keyfile -- filename of private key 469 ** cacertpath -- path to CAs 470 ** cacertfile -- file with CA(s) 471 ** dhparam -- parameters for DH 472 ** 473 ** Returns: 474 ** succeeded? 475 */ 476 477 bool 478 inittls(ctx, req, srv, certfile, keyfile, cacertpath, cacertfile, dhparam) 479 SSL_CTX **ctx; 480 unsigned long req; 481 bool srv; 482 char *certfile, *keyfile, *cacertpath, *cacertfile, *dhparam; 483 { 484 # if !NO_DH 485 static DH *dh = NULL; 486 # endif /* !NO_DH */ 487 int r; 488 bool ok; 489 long sff, status; 490 char *who; 491 # if _FFR_TLS_1 492 char *cf2, *kf2; 493 # endif /* _FFR_TLS_1 */ 494 # if SM_CONF_SHM 495 extern int ShmId; 496 # endif /* SM_CONF_SHM */ 497 498 status = TLS_S_NONE; 499 who = srv ? "server" : "client"; 500 if (ctx == NULL) 501 syserr("STARTTLS=%s, inittls: ctx == NULL", who); 502 503 /* already initialized? (we could re-init...) */ 504 if (*ctx != NULL) 505 return true; 506 ok = true; 507 508 # if _FFR_TLS_1 509 /* 510 ** look for a second filename: it must be separated by a ',' 511 ** no blanks allowed (they won't be skipped). 512 ** we change a global variable here! this change will be undone 513 ** before return from the function but only if it returns true. 514 ** this isn't a problem since in a failure case this function 515 ** won't be called again with the same (overwritten) values. 516 ** otherwise each return must be replaced with a goto endinittls. 517 */ 518 519 cf2 = NULL; 520 kf2 = NULL; 521 if (certfile != NULL && (cf2 = strchr(certfile, ',')) != NULL) 522 { 523 *cf2++ = '\0'; 524 if (keyfile != NULL && (kf2 = strchr(keyfile, ',')) != NULL) 525 *kf2++ = '\0'; 526 } 527 # endif /* _FFR_TLS_1 */ 528 529 /* 530 ** Check whether files/paths are defined 531 */ 532 533 TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req), 534 TLS_S_CERT_EX, srv); 535 TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req), 536 TLS_S_KEY_EX, srv); 537 TLS_OK_F(cacertpath, "CACERTPath", bitset(TLS_I_CERTP_EX, req), 538 TLS_S_CERTP_EX, srv); 539 TLS_OK_F(cacertfile, "CACERTFile", bitset(TLS_I_CERTF_EX, req), 540 TLS_S_CERTF_EX, srv); 541 542 # if _FFR_TLS_1 543 /* 544 ** if the second file is specified it must exist 545 ** XXX: it is possible here to define only one of those files 546 */ 547 548 if (cf2 != NULL) 549 { 550 TLS_OK_F(cf2, "CertFile", bitset(TLS_I_CERT_EX, req), 551 TLS_S_CERT2_EX, srv); 552 } 553 if (kf2 != NULL) 554 { 555 TLS_OK_F(kf2, "KeyFile", bitset(TLS_I_KEY_EX, req), 556 TLS_S_KEY2_EX, srv); 557 } 558 # endif /* _FFR_TLS_1 */ 559 560 /* 561 ** valid values for dhparam are (only the first char is checked) 562 ** none no parameters: don't use DH 563 ** 512 generate 512 bit parameters (fixed) 564 ** 1024 generate 1024 bit parameters 565 ** /file/name read parameters from /file/name 566 ** default is: 1024 for server, 512 for client (OK? XXX) 567 */ 568 569 if (bitset(TLS_I_TRY_DH, req)) 570 { 571 if (dhparam != NULL) 572 { 573 char c = *dhparam; 574 575 if (c == '1') 576 req |= TLS_I_DH1024; 577 else if (c == '5') 578 req |= TLS_I_DH512; 579 else if (c != 'n' && c != 'N' && c != '/') 580 { 581 if (LogLevel > 12) 582 sm_syslog(LOG_WARNING, NOQID, 583 "STARTTLS=%s, error: illegal value '%s' for DHParam", 584 who, dhparam); 585 dhparam = NULL; 586 } 587 } 588 if (dhparam == NULL) 589 dhparam = srv ? "1" : "5"; 590 else if (*dhparam == '/') 591 { 592 TLS_OK_F(dhparam, "DHParameters", 593 bitset(TLS_I_DHPAR_EX, req), 594 TLS_S_DHPAR_EX, srv); 595 } 596 } 597 if (!ok) 598 return ok; 599 600 /* certfile etc. must be "safe". */ 601 sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK 602 | SFF_NOGWFILES | SFF_NOWWFILES 603 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT; 604 if (DontLockReadFiles) 605 sff |= SFF_NOLOCK; 606 607 TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req), 608 bitset(TLS_I_CERT_EX, req), 609 bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv); 610 TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req), 611 bitset(TLS_I_KEY_EX, req), 612 bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv); 613 TLS_SAFE_F(cacertfile, sff | TLS_UNR(TLS_I_CERTF_UNR, req), 614 bitset(TLS_I_CERTF_EX, req), 615 bitset(TLS_S_CERTF_EX, status), TLS_S_CERTF_OK, srv); 616 TLS_SAFE_F(dhparam, sff | TLS_UNR(TLS_I_DHPAR_UNR, req), 617 bitset(TLS_I_DHPAR_EX, req), 618 bitset(TLS_S_DHPAR_EX, status), TLS_S_DHPAR_OK, srv); 619 if (!ok) 620 return ok; 621 # if _FFR_TLS_1 622 if (cf2 != NULL) 623 { 624 TLS_SAFE_F(cf2, sff | TLS_UNR(TLS_I_CERT_UNR, req), 625 bitset(TLS_I_CERT_EX, req), 626 bitset(TLS_S_CERT2_EX, status), TLS_S_CERT2_OK, srv); 627 } 628 if (kf2 != NULL) 629 { 630 TLS_SAFE_F(kf2, sff | TLS_KEYSFF(req), 631 bitset(TLS_I_KEY_EX, req), 632 bitset(TLS_S_KEY2_EX, status), TLS_S_KEY2_OK, srv); 633 } 634 # endif /* _FFR_TLS_1 */ 635 636 /* create a method and a new context */ 637 if ((*ctx = SSL_CTX_new(srv ? SSLv23_server_method() : 638 SSLv23_client_method())) == NULL) 639 { 640 if (LogLevel > 7) 641 sm_syslog(LOG_WARNING, NOQID, 642 "STARTTLS=%s, error: SSL_CTX_new(SSLv23_%s_method()) failed", 643 who, who); 644 if (LogLevel > 9) 645 tlslogerr(who); 646 return false; 647 } 648 649 # if TLS_NO_RSA 650 /* turn off backward compatibility, required for no-rsa */ 651 SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2); 652 # endif /* TLS_NO_RSA */ 653 654 655 # if !TLS_NO_RSA 656 /* 657 ** Create a temporary RSA key 658 ** XXX Maybe we shouldn't create this always (even though it 659 ** is only at startup). 660 ** It is a time-consuming operation and it is not always necessary. 661 ** maybe we should do it only on demand... 662 */ 663 664 if (bitset(TLS_I_RSA_TMP, req) 665 # if SM_CONF_SHM 666 && ShmId != SM_SHM_NO_ID && 667 (rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, 668 NULL)) == NULL 669 # else /* SM_CONF_SHM */ 670 && 0 /* no shared memory: no need to generate key now */ 671 # endif /* SM_CONF_SHM */ 672 ) 673 { 674 if (LogLevel > 7) 675 { 676 sm_syslog(LOG_WARNING, NOQID, 677 "STARTTLS=%s, error: RSA_generate_key failed", 678 who); 679 if (LogLevel > 9) 680 tlslogerr(who); 681 } 682 return false; 683 } 684 # endif /* !TLS_NO_RSA */ 685 686 /* 687 ** load private key 688 ** XXX change this for DSA-only version 689 */ 690 691 if (bitset(TLS_S_KEY_OK, status) && 692 SSL_CTX_use_PrivateKey_file(*ctx, keyfile, 693 SSL_FILETYPE_PEM) <= 0) 694 { 695 if (LogLevel > 7) 696 { 697 sm_syslog(LOG_WARNING, NOQID, 698 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed", 699 who, keyfile); 700 if (LogLevel > 9) 701 tlslogerr(who); 702 } 703 if (bitset(TLS_I_USE_KEY, req)) 704 return false; 705 } 706 707 /* get the certificate file */ 708 if (bitset(TLS_S_CERT_OK, status) && 709 SSL_CTX_use_certificate_file(*ctx, certfile, 710 SSL_FILETYPE_PEM) <= 0) 711 { 712 if (LogLevel > 7) 713 { 714 sm_syslog(LOG_WARNING, NOQID, 715 "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed", 716 who, certfile); 717 if (LogLevel > 9) 718 tlslogerr(who); 719 } 720 if (bitset(TLS_I_USE_CERT, req)) 721 return false; 722 } 723 724 /* check the private key */ 725 if (bitset(TLS_S_KEY_OK, status) && 726 (r = SSL_CTX_check_private_key(*ctx)) <= 0) 727 { 728 /* Private key does not match the certificate public key */ 729 if (LogLevel > 5) 730 { 731 sm_syslog(LOG_WARNING, NOQID, 732 "STARTTLS=%s, error: SSL_CTX_check_private_key failed(%s): %d", 733 who, keyfile, r); 734 if (LogLevel > 9) 735 tlslogerr(who); 736 } 737 if (bitset(TLS_I_USE_KEY, req)) 738 return false; 739 } 740 741 # if _FFR_TLS_1 742 /* XXX this code is pretty much duplicated from above! */ 743 744 /* load private key */ 745 if (bitset(TLS_S_KEY2_OK, status) && 746 SSL_CTX_use_PrivateKey_file(*ctx, kf2, SSL_FILETYPE_PEM) <= 0) 747 { 748 if (LogLevel > 7) 749 { 750 sm_syslog(LOG_WARNING, NOQID, 751 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed", 752 who, kf2); 753 if (LogLevel > 9) 754 tlslogerr(who); 755 } 756 } 757 758 /* get the certificate file */ 759 if (bitset(TLS_S_CERT2_OK, status) && 760 SSL_CTX_use_certificate_file(*ctx, cf2, SSL_FILETYPE_PEM) <= 0) 761 { 762 if (LogLevel > 7) 763 { 764 sm_syslog(LOG_WARNING, NOQID, 765 "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed", 766 who, cf2); 767 if (LogLevel > 9) 768 tlslogerr(who); 769 } 770 } 771 772 /* also check the private key */ 773 if (bitset(TLS_S_KEY2_OK, status) && 774 (r = SSL_CTX_check_private_key(*ctx)) <= 0) 775 { 776 /* Private key does not match the certificate public key */ 777 if (LogLevel > 5) 778 { 779 sm_syslog(LOG_WARNING, NOQID, 780 "STARTTLS=%s, error: SSL_CTX_check_private_key 2 failed: %d", 781 who, r); 782 if (LogLevel > 9) 783 tlslogerr(who); 784 } 785 } 786 # endif /* _FFR_TLS_1 */ 787 788 /* SSL_CTX_set_quiet_shutdown(*ctx, 1); violation of standard? */ 789 SSL_CTX_set_options(*ctx, SSL_OP_ALL); /* XXX bug compatibility? */ 790 791 # if !NO_DH 792 /* Diffie-Hellman initialization */ 793 if (bitset(TLS_I_TRY_DH, req)) 794 { 795 if (bitset(TLS_S_DHPAR_OK, status)) 796 { 797 BIO *bio; 798 799 if ((bio = BIO_new_file(dhparam, "r")) != NULL) 800 { 801 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); 802 BIO_free(bio); 803 if (dh == NULL && LogLevel > 7) 804 { 805 unsigned long err; 806 807 err = ERR_get_error(); 808 sm_syslog(LOG_WARNING, NOQID, 809 "STARTTLS=%s, error: cannot read DH parameters(%s): %s", 810 who, dhparam, 811 ERR_error_string(err, NULL)); 812 if (LogLevel > 9) 813 tlslogerr(who); 814 } 815 } 816 else 817 { 818 if (LogLevel > 5) 819 { 820 sm_syslog(LOG_WARNING, NOQID, 821 "STARTTLS=%s, error: BIO_new_file(%s) failed", 822 who, dhparam); 823 if (LogLevel > 9) 824 tlslogerr(who); 825 } 826 } 827 } 828 if (dh == NULL && bitset(TLS_I_DH1024, req)) 829 { 830 DSA *dsa; 831 832 /* this takes a while! (7-130s on a 450MHz AMD K6-2) */ 833 dsa = DSA_generate_parameters(1024, NULL, 0, NULL, 834 NULL, 0, NULL); 835 dh = DSA_dup_DH(dsa); 836 DSA_free(dsa); 837 } 838 else 839 if (dh == NULL && bitset(TLS_I_DH512, req)) 840 dh = get_dh512(); 841 842 if (dh == NULL) 843 { 844 if (LogLevel > 9) 845 { 846 unsigned long err; 847 848 err = ERR_get_error(); 849 sm_syslog(LOG_WARNING, NOQID, 850 "STARTTLS=%s, error: cannot read or set DH parameters(%s): %s", 851 who, dhparam, 852 ERR_error_string(err, NULL)); 853 } 854 if (bitset(TLS_I_REQ_DH, req)) 855 return false; 856 } 857 else 858 { 859 SSL_CTX_set_tmp_dh(*ctx, dh); 860 861 /* important to avoid small subgroup attacks */ 862 SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_DH_USE); 863 if (LogLevel > 13) 864 sm_syslog(LOG_INFO, NOQID, 865 "STARTTLS=%s, Diffie-Hellman init, key=%d bit (%c)", 866 who, 8 * DH_size(dh), *dhparam); 867 DH_free(dh); 868 } 869 } 870 # endif /* !NO_DH */ 871 872 873 /* XXX do we need this cache here? */ 874 if (bitset(TLS_I_CACHE, req)) 875 SSL_CTX_sess_set_cache_size(*ctx, 128); 876 /* timeout? SSL_CTX_set_timeout(*ctx, TimeOut...); */ 877 878 /* load certificate locations and default CA paths */ 879 if (bitset(TLS_S_CERTP_EX, status) && bitset(TLS_S_CERTF_EX, status)) 880 { 881 if ((r = SSL_CTX_load_verify_locations(*ctx, cacertfile, 882 cacertpath)) == 1) 883 { 884 # if !TLS_NO_RSA 885 if (bitset(TLS_I_RSA_TMP, req)) 886 SSL_CTX_set_tmp_rsa_callback(*ctx, tmp_rsa_key); 887 # endif /* !TLS_NO_RSA */ 888 889 /* 890 ** We have to install our own verify callback: 891 ** SSL_VERIFY_PEER requests a client cert but even 892 ** though *FAIL_IF* isn't set, the connection 893 ** will be aborted if the client presents a cert 894 ** that is not "liked" (can't be verified?) by 895 ** the TLS library :-( 896 */ 897 898 /* 899 ** XXX currently we could call tls_set_verify() 900 ** but we hope that that function will later on 901 ** only set the mode per connection. 902 */ 903 SSL_CTX_set_verify(*ctx, 904 bitset(TLS_I_NO_VRFY, req) ? SSL_VERIFY_NONE 905 : SSL_VERIFY_PEER, 906 NULL); 907 908 /* install verify callback */ 909 SSL_CTX_set_cert_verify_callback(*ctx, tls_verify_cb, 910 NULL); 911 SSL_CTX_set_client_CA_list(*ctx, 912 SSL_load_client_CA_file(cacertfile)); 913 } 914 else 915 { 916 /* 917 ** can't load CA data; do we care? 918 ** the data is necessary to authenticate the client, 919 ** which in turn would be necessary 920 ** if we want to allow relaying based on it. 921 */ 922 if (LogLevel > 5) 923 { 924 sm_syslog(LOG_WARNING, NOQID, 925 "STARTTLS=%s, error: load verify locs %s, %s failed: %d", 926 who, cacertpath, cacertfile, r); 927 if (LogLevel > 9) 928 tlslogerr(who); 929 } 930 if (bitset(TLS_I_VRFY_LOC, req)) 931 return false; 932 } 933 } 934 935 /* XXX: make this dependent on an option? */ 936 if (tTd(96, 9)) 937 SSL_CTX_set_info_callback(*ctx, apps_ssl_info_cb); 938 939 # if _FFR_TLS_1 940 /* install our own cipher list */ 941 if (CipherList != NULL && *CipherList != '\0') 942 { 943 if (SSL_CTX_set_cipher_list(*ctx, CipherList) <= 0) 944 { 945 if (LogLevel > 7) 946 { 947 sm_syslog(LOG_WARNING, NOQID, 948 "STARTTLS=%s, error: SSL_CTX_set_cipher_list(%s) failed, list ignored", 949 who, CipherList); 950 951 if (LogLevel > 9) 952 tlslogerr(who); 953 } 954 /* failure if setting to this list is required? */ 955 } 956 } 957 # endif /* _FFR_TLS_1 */ 958 if (LogLevel > 12) 959 sm_syslog(LOG_INFO, NOQID, "STARTTLS=%s, init=%d", who, ok); 960 961 # if _FFR_TLS_1 962 # if 0 963 /* 964 ** this label is required if we want to have a "clean" exit 965 ** see the comments above at the initialization of cf2 966 */ 967 968 endinittls: 969 # endif /* 0 */ 970 971 /* undo damage to global variables */ 972 if (cf2 != NULL) 973 *--cf2 = ','; 974 if (kf2 != NULL) 975 *--kf2 = ','; 976 # endif /* _FFR_TLS_1 */ 977 978 return ok; 979 } 980 /* 981 ** TLS_GET_INFO -- get information about TLS connection 982 ** 983 ** Parameters: 984 ** ssl -- TLS connection structure 985 ** srv -- server or client 986 ** host -- hostname of other side 987 ** mac -- macro storage 988 ** certreq -- did we ask for a cert? 989 ** 990 ** Returns: 991 ** result of authentication. 992 ** 993 ** Side Effects: 994 ** sets macros: {cipher}, {tls_version}, {verify}, 995 ** {cipher_bits}, {alg_bits}, {cert}, {cert_subject}, 996 ** {cert_issuer}, {cn_subject}, {cn_issuer} 997 */ 998 999 int 1000 tls_get_info(ssl, srv, host, mac, certreq) 1001 SSL *ssl; 1002 bool srv; 1003 char *host; 1004 MACROS_T *mac; 1005 bool certreq; 1006 { 1007 SSL_CIPHER *c; 1008 int b, r; 1009 char *s, *who; 1010 char bitstr[16]; 1011 X509 *cert; 1012 1013 c = SSL_get_current_cipher(ssl); 1014 1015 /* cast is just workaround for compiler warning */ 1016 macdefine(mac, A_TEMP, macid("{cipher}"), 1017 (char *) SSL_CIPHER_get_name(c)); 1018 b = SSL_CIPHER_get_bits(c, &r); 1019 (void) sm_snprintf(bitstr, sizeof bitstr, "%d", b); 1020 macdefine(mac, A_TEMP, macid("{cipher_bits}"), bitstr); 1021 (void) sm_snprintf(bitstr, sizeof bitstr, "%d", r); 1022 macdefine(mac, A_TEMP, macid("{alg_bits}"), bitstr); 1023 s = SSL_CIPHER_get_version(c); 1024 if (s == NULL) 1025 s = "UNKNOWN"; 1026 macdefine(mac, A_TEMP, macid("{tls_version}"), s); 1027 1028 who = srv ? "server" : "client"; 1029 cert = SSL_get_peer_certificate(ssl); 1030 if (LogLevel > 14) 1031 sm_syslog(LOG_INFO, NOQID, 1032 "STARTTLS=%s, get_verify: %ld get_peer: 0x%lx", 1033 who, SSL_get_verify_result(ssl), 1034 (unsigned long) cert); 1035 if (cert != NULL) 1036 { 1037 unsigned int n; 1038 unsigned char md[EVP_MAX_MD_SIZE]; 1039 char buf[MAXNAME]; 1040 1041 X509_NAME_oneline(X509_get_subject_name(cert), 1042 buf, sizeof buf); 1043 macdefine(mac, A_TEMP, macid("{cert_subject}"), 1044 xtextify(buf, "<>\")")); 1045 X509_NAME_oneline(X509_get_issuer_name(cert), 1046 buf, sizeof buf); 1047 macdefine(mac, A_TEMP, macid("{cert_issuer}"), 1048 xtextify(buf, "<>\")")); 1049 X509_NAME_get_text_by_NID(X509_get_subject_name(cert), 1050 NID_commonName, buf, sizeof buf); 1051 macdefine(mac, A_TEMP, macid("{cn_subject}"), 1052 xtextify(buf, "<>\")")); 1053 X509_NAME_get_text_by_NID(X509_get_issuer_name(cert), 1054 NID_commonName, buf, sizeof buf); 1055 macdefine(mac, A_TEMP, macid("{cn_issuer}"), 1056 xtextify(buf, "<>\")")); 1057 if (X509_digest(cert, EVP_md5(), md, &n)) 1058 { 1059 char md5h[EVP_MAX_MD_SIZE * 3]; 1060 static const char hexcodes[] = "0123456789ABCDEF"; 1061 1062 SM_ASSERT((n * 3) + 2 < sizeof(md5h)); 1063 for (r = 0; r < (int) n; r++) 1064 { 1065 md5h[r * 3] = hexcodes[(md[r] & 0xf0) >> 4]; 1066 md5h[(r * 3) + 1] = hexcodes[(md[r] & 0x0f)]; 1067 md5h[(r * 3) + 2] = ':'; 1068 } 1069 md5h[(n * 3) - 1] = '\0'; 1070 macdefine(mac, A_TEMP, macid("{cert_md5}"), md5h); 1071 } 1072 else 1073 macdefine(mac, A_TEMP, macid("{cert_md5}"), ""); 1074 } 1075 else 1076 { 1077 macdefine(mac, A_PERM, macid("{cert_subject}"), ""); 1078 macdefine(mac, A_PERM, macid("{cert_issuer}"), ""); 1079 macdefine(mac, A_PERM, macid("{cn_subject}"), ""); 1080 macdefine(mac, A_PERM, macid("{cn_issuer}"), ""); 1081 macdefine(mac, A_TEMP, macid("{cert_md5}"), ""); 1082 } 1083 switch (SSL_get_verify_result(ssl)) 1084 { 1085 case X509_V_OK: 1086 if (cert != NULL) 1087 { 1088 s = "OK"; 1089 r = TLS_AUTH_OK; 1090 } 1091 else 1092 { 1093 s = certreq ? "NO" : "NOT", 1094 r = TLS_AUTH_NO; 1095 } 1096 break; 1097 default: 1098 s = "FAIL"; 1099 r = TLS_AUTH_FAIL; 1100 break; 1101 } 1102 macdefine(mac, A_PERM, macid("{verify}"), s); 1103 if (cert != NULL) 1104 X509_free(cert); 1105 1106 /* do some logging */ 1107 if (LogLevel > 8) 1108 { 1109 char *vers, *s1, *s2, *cbits, *algbits; 1110 1111 vers = macget(mac, macid("{tls_version}")); 1112 cbits = macget(mac, macid("{cipher_bits}")); 1113 algbits = macget(mac, macid("{alg_bits}")); 1114 s1 = macget(mac, macid("{verify}")); 1115 s2 = macget(mac, macid("{cipher}")); 1116 1117 /* XXX: maybe cut off ident info? */ 1118 sm_syslog(LOG_INFO, NOQID, 1119 "STARTTLS=%s, relay=%.100s, version=%.16s, verify=%.16s, cipher=%.64s, bits=%.6s/%.6s", 1120 who, 1121 host == NULL ? "local" : host, 1122 vers, s1, s2, /* sm_snprintf() can deal with NULL */ 1123 algbits == NULL ? "0" : algbits, 1124 cbits == NULL ? "0" : cbits); 1125 if (LogLevel > 11) 1126 { 1127 /* 1128 ** Maybe run xuntextify on the strings? 1129 ** That is easier to read but makes it maybe a bit 1130 ** more complicated to figure out the right values 1131 ** for the access map... 1132 */ 1133 1134 s1 = macget(mac, macid("{cert_subject}")); 1135 s2 = macget(mac, macid("{cert_issuer}")); 1136 sm_syslog(LOG_INFO, NOQID, 1137 "STARTTLS=%s, cert-subject=%.128s, cert-issuer=%.128s", 1138 who, s1, s2); 1139 } 1140 } 1141 return r; 1142 } 1143 /* 1144 ** ENDTLS -- shutdown secure connection 1145 ** 1146 ** Parameters: 1147 ** ssl -- SSL connection information. 1148 ** side -- server/client (for logging). 1149 ** 1150 ** Returns: 1151 ** success? (EX_* code) 1152 */ 1153 1154 int 1155 endtls(ssl, side) 1156 SSL *ssl; 1157 char *side; 1158 { 1159 int ret = EX_OK; 1160 1161 if (ssl != NULL) 1162 { 1163 int r; 1164 1165 if ((r = SSL_shutdown(ssl)) < 0) 1166 { 1167 if (LogLevel > 11) 1168 { 1169 sm_syslog(LOG_WARNING, NOQID, 1170 "STARTTLS=%s, SSL_shutdown failed: %d", 1171 side, r); 1172 tlslogerr(side); 1173 } 1174 ret = EX_SOFTWARE; 1175 } 1176 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL 1177 1178 /* 1179 ** Bug in OpenSSL (at least up to 0.9.6b): 1180 ** From: Lutz.Jaenicke@aet.TU-Cottbus.DE 1181 ** Message-ID: <20010723152244.A13122@serv01.aet.tu-cottbus.de> 1182 ** To: openssl-users@openssl.org 1183 ** Subject: Re: SSL_shutdown() woes (fwd) 1184 ** 1185 ** The side sending the shutdown alert first will 1186 ** not care about the answer of the peer but will 1187 ** immediately return with a return value of "0" 1188 ** (ssl/s3_lib.c:ssl3_shutdown()). SSL_get_error will evaluate 1189 ** the value of "0" and as the shutdown alert of the peer was 1190 ** not received (actually, the program did not even wait for 1191 ** the answer), an SSL_ERROR_SYSCALL is flagged, because this 1192 ** is the default rule in case everything else does not apply. 1193 ** 1194 ** For your server the problem is different, because it 1195 ** receives the shutdown first (setting SSL_RECEIVED_SHUTDOWN), 1196 ** then sends its response (SSL_SENT_SHUTDOWN), so for the 1197 ** server the shutdown was successfull. 1198 ** 1199 ** As is by know, you would have to call SSL_shutdown() once 1200 ** and ignore an SSL_ERROR_SYSCALL returned. Then call 1201 ** SSL_shutdown() again to actually get the server's response. 1202 ** 1203 ** In the last discussion, Bodo Moeller concluded that a 1204 ** rewrite of the shutdown code would be necessary, but 1205 ** probably with another API, as the change would not be 1206 ** compatible to the way it is now. Things do not become 1207 ** easier as other programs do not follow the shutdown 1208 ** guidelines anyway, so that a lot error conditions and 1209 ** compitibility issues would have to be caught. 1210 ** 1211 ** For now the recommondation is to ignore the error message. 1212 */ 1213 1214 else if (r == 0) 1215 { 1216 if (LogLevel > 15) 1217 { 1218 sm_syslog(LOG_WARNING, NOQID, 1219 "STARTTLS=%s, SSL_shutdown not done", 1220 side); 1221 tlslogerr(side); 1222 } 1223 ret = EX_SOFTWARE; 1224 } 1225 # endif /* !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL */ 1226 SSL_free(ssl); 1227 ssl = NULL; 1228 } 1229 return ret; 1230 } 1231 1232 # if !TLS_NO_RSA 1233 /* 1234 ** TMP_RSA_KEY -- return temporary RSA key 1235 ** 1236 ** Parameters: 1237 ** s -- TLS connection structure 1238 ** export -- 1239 ** keylength -- 1240 ** 1241 ** Returns: 1242 ** temporary RSA key. 1243 */ 1244 1245 # ifndef MAX_RSA_TMP_CNT 1246 # define MAX_RSA_TMP_CNT 1000 /* XXX better value? */ 1247 # endif /* ! MAX_RSA_TMP_CNT */ 1248 1249 /* ARGUSED0 */ 1250 static RSA * 1251 tmp_rsa_key(s, export, keylength) 1252 SSL *s; 1253 int export; 1254 int keylength; 1255 { 1256 # if SM_CONF_SHM 1257 extern int ShmId; 1258 extern int *PRSATmpCnt; 1259 1260 if (ShmId != SM_SHM_NO_ID && rsa_tmp != NULL && 1261 ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT) 1262 return rsa_tmp; 1263 # endif /* SM_CONF_SHM */ 1264 1265 if (rsa_tmp != NULL) 1266 RSA_free(rsa_tmp); 1267 rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, NULL); 1268 if (rsa_tmp == NULL) 1269 { 1270 if (LogLevel > 0) 1271 sm_syslog(LOG_ERR, NOQID, 1272 "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!"); 1273 } 1274 else 1275 { 1276 # if SM_CONF_SHM 1277 # if 0 1278 /* 1279 ** XXX we can't (yet) share the new key... 1280 ** The RSA structure contains pointers hence it can't be 1281 ** easily kept in shared memory. It must be transformed 1282 ** into a continous memory region first, then stored, 1283 ** and later read out again (each time re-transformed). 1284 */ 1285 1286 if (ShmId != SM_SHM_NO_ID) 1287 *PRSATmpCnt = 0; 1288 # endif /* 0 */ 1289 # endif /* SM_CONF_SHM */ 1290 if (LogLevel > 9) 1291 sm_syslog(LOG_ERR, NOQID, 1292 "STARTTLS=server, tmp_rsa_key: new temp RSA key"); 1293 } 1294 return rsa_tmp; 1295 } 1296 # endif /* !TLS_NO_RSA */ 1297 /* 1298 ** APPS_SSL_INFO_CB -- info callback for TLS connections 1299 ** 1300 ** Parameters: 1301 ** s -- TLS connection structure 1302 ** where -- state in handshake 1303 ** ret -- return code of last operation 1304 ** 1305 ** Returns: 1306 ** none. 1307 */ 1308 1309 static void 1310 apps_ssl_info_cb(s, where, ret) 1311 SSL *s; 1312 int where; 1313 int ret; 1314 { 1315 int w; 1316 char *str; 1317 BIO *bio_err = NULL; 1318 1319 if (LogLevel > 14) 1320 sm_syslog(LOG_INFO, NOQID, 1321 "STARTTLS: info_callback where=0x%x, ret=%d", 1322 where, ret); 1323 1324 w = where & ~SSL_ST_MASK; 1325 if (bio_err == NULL) 1326 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); 1327 1328 if (bitset(SSL_ST_CONNECT, w)) 1329 str = "SSL_connect"; 1330 else if (bitset(SSL_ST_ACCEPT, w)) 1331 str = "SSL_accept"; 1332 else 1333 str = "undefined"; 1334 1335 if (bitset(SSL_CB_LOOP, where)) 1336 { 1337 if (LogLevel > 12) 1338 sm_syslog(LOG_NOTICE, NOQID, 1339 "STARTTLS: %s:%s", 1340 str, SSL_state_string_long(s)); 1341 } 1342 else if (bitset(SSL_CB_ALERT, where)) 1343 { 1344 str = bitset(SSL_CB_READ, where) ? "read" : "write"; 1345 if (LogLevel > 12) 1346 sm_syslog(LOG_NOTICE, NOQID, 1347 "STARTTLS: SSL3 alert %s:%s:%s", 1348 str, SSL_alert_type_string_long(ret), 1349 SSL_alert_desc_string_long(ret)); 1350 } 1351 else if (bitset(SSL_CB_EXIT, where)) 1352 { 1353 if (ret == 0) 1354 { 1355 if (LogLevel > 7) 1356 sm_syslog(LOG_WARNING, NOQID, 1357 "STARTTLS: %s:failed in %s", 1358 str, SSL_state_string_long(s)); 1359 } 1360 else if (ret < 0) 1361 { 1362 if (LogLevel > 7) 1363 sm_syslog(LOG_WARNING, NOQID, 1364 "STARTTLS: %s:error in %s", 1365 str, SSL_state_string_long(s)); 1366 } 1367 } 1368 } 1369 /* 1370 ** TLS_VERIFY_LOG -- log verify error for TLS certificates 1371 ** 1372 ** Parameters: 1373 ** ok -- verify ok? 1374 ** ctx -- x509 context 1375 ** 1376 ** Returns: 1377 ** 0 -- fatal error 1378 ** 1 -- ok 1379 */ 1380 1381 static int 1382 tls_verify_log(ok, ctx) 1383 int ok; 1384 X509_STORE_CTX *ctx; 1385 { 1386 SSL *ssl; 1387 X509 *cert; 1388 int reason, depth; 1389 char buf[512]; 1390 1391 cert = X509_STORE_CTX_get_current_cert(ctx); 1392 reason = X509_STORE_CTX_get_error(ctx); 1393 depth = X509_STORE_CTX_get_error_depth(ctx); 1394 ssl = (SSL *) X509_STORE_CTX_get_ex_data(ctx, 1395 SSL_get_ex_data_X509_STORE_CTX_idx()); 1396 1397 if (ssl == NULL) 1398 { 1399 /* internal error */ 1400 sm_syslog(LOG_ERR, NOQID, 1401 "STARTTLS: internal error: tls_verify_cb: ssl == NULL"); 1402 return 0; 1403 } 1404 1405 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof buf); 1406 sm_syslog(LOG_INFO, NOQID, 1407 "STARTTLS: cert verify: depth=%d %s, state=%d, reason=%s", 1408 depth, buf, ok, X509_verify_cert_error_string(reason)); 1409 return 1; 1410 } 1411 /* 1412 ** TLS_VERIFY_CB -- verify callback for TLS certificates 1413 ** 1414 ** Parameters: 1415 ** ctx -- x509 context 1416 ** 1417 ** Returns: 1418 ** accept connection? 1419 ** currently: always yes. 1420 */ 1421 1422 static int 1423 tls_verify_cb(ctx) 1424 X509_STORE_CTX *ctx; 1425 { 1426 int ok; 1427 1428 ok = X509_verify_cert(ctx); 1429 if (ok == 0) 1430 { 1431 if (LogLevel > 13) 1432 return tls_verify_log(ok, ctx); 1433 return 1; /* override it */ 1434 } 1435 return ok; 1436 } 1437 /* 1438 ** TLSLOGERR -- log the errors from the TLS error stack 1439 ** 1440 ** Parameters: 1441 ** who -- server/client (for logging). 1442 ** 1443 ** Returns: 1444 ** none. 1445 */ 1446 1447 void 1448 tlslogerr(who) 1449 char *who; 1450 { 1451 unsigned long l; 1452 int line, flags; 1453 unsigned long es; 1454 char *file, *data; 1455 char buf[256]; 1456 # define CP (const char **) 1457 1458 es = CRYPTO_thread_id(); 1459 while ((l = ERR_get_error_line_data(CP &file, &line, CP &data, &flags)) 1460 != 0) 1461 { 1462 sm_syslog(LOG_WARNING, NOQID, 1463 "STARTTLS=%s: %lu:%s:%s:%d:%s", who, es, 1464 ERR_error_string(l, buf), 1465 file, line, 1466 bitset(ERR_TXT_STRING, flags) ? data : ""); 1467 } 1468 } 1469 #endif /* STARTTLS */ 1470