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