2 * Copyright (c) 2000-2006, 2008, 2009, 2011, 2013 Proofpoint, Inc. and its suppliers.
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.
13 SM_RCSID("@(#)$Id: tls.c,v 8.127 2013-11-27 02:51:11 gshapiro Exp $")
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 */
23 static RSA *rsa_tmp = NULL; /* temporary RSA key */
24 static RSA *tmp_rsa_key __P((SSL *, int, int));
25 # endif /* !TLS_NO_RSA */
26 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
27 static int tls_verify_cb __P((X509_STORE_CTX *));
28 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
29 static int tls_verify_cb __P((X509_STORE_CTX *, void *));
30 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
32 # if OPENSSL_VERSION_NUMBER > 0x00907000L
33 static int x509_verify_cb __P((int, X509_STORE_CTX *));
34 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
36 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
38 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
39 # define CONST097 const
40 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
41 static void apps_ssl_info_cb __P((CONST097 SSL *, int , int));
42 static bool tls_ok_f __P((char *, char *, int));
43 static bool tls_safe_f __P((char *, long, bool));
44 static int tls_verify_log __P((int, X509_STORE_CTX *, const char *));
47 static DH *get_dh512 __P((void));
49 static unsigned char dh512_p[] =
51 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
52 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
53 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
54 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
55 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
58 static unsigned char dh512_g[] =
68 if ((dh = DH_new()) == NULL)
70 dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
71 dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
72 if ((dh->p == NULL) || (dh->g == NULL))
79 This is the data from which the C code has been generated:
81 -----BEGIN DH PARAMETERS-----
82 MIIBCAKCAQEArDcgcLpxEksQHPlolRKCUJ2szKRziseWV9cUSQNZGxoGw7KkROz4
83 HF9QSbg5axyNIG+QbZYtx0jp3l6/GWq1dLOj27yZkgYgaYgFrvKPiZ2jJ5xETQVH
84 UpZwbjRcyjyWkWYJVsx1aF4F/iY4kT0n/+iGEoimI3C9V3KXTJ2S6jIkyJ6M/CrN
85 EtrDynMlUMGlc7S1ouXVOTrtKeqy3S2L9eBLxVI+sChEijGIfELupdVeXihK006p
86 MgnABPDbkTx6OOtYmSZaGQX+OLW2FPmwvcrzgCz9t9cAsuUcBZv1LeHEqZZttyLU
87 oK0jjSXgFyeU4/NfyA+zuNeWzUL6bHmigwIBAg==
88 -----END DH PARAMETERS-----
94 static unsigned char dh2048_p[]={
95 0xAC,0x37,0x20,0x70,0xBA,0x71,0x12,0x4B,0x10,0x1C,0xF9,0x68,
96 0x95,0x12,0x82,0x50,0x9D,0xAC,0xCC,0xA4,0x73,0x8A,0xC7,0x96,
97 0x57,0xD7,0x14,0x49,0x03,0x59,0x1B,0x1A,0x06,0xC3,0xB2,0xA4,
98 0x44,0xEC,0xF8,0x1C,0x5F,0x50,0x49,0xB8,0x39,0x6B,0x1C,0x8D,
99 0x20,0x6F,0x90,0x6D,0x96,0x2D,0xC7,0x48,0xE9,0xDE,0x5E,0xBF,
100 0x19,0x6A,0xB5,0x74,0xB3,0xA3,0xDB,0xBC,0x99,0x92,0x06,0x20,
101 0x69,0x88,0x05,0xAE,0xF2,0x8F,0x89,0x9D,0xA3,0x27,0x9C,0x44,
102 0x4D,0x05,0x47,0x52,0x96,0x70,0x6E,0x34,0x5C,0xCA,0x3C,0x96,
103 0x91,0x66,0x09,0x56,0xCC,0x75,0x68,0x5E,0x05,0xFE,0x26,0x38,
104 0x91,0x3D,0x27,0xFF,0xE8,0x86,0x12,0x88,0xA6,0x23,0x70,0xBD,
105 0x57,0x72,0x97,0x4C,0x9D,0x92,0xEA,0x32,0x24,0xC8,0x9E,0x8C,
106 0xFC,0x2A,0xCD,0x12,0xDA,0xC3,0xCA,0x73,0x25,0x50,0xC1,0xA5,
107 0x73,0xB4,0xB5,0xA2,0xE5,0xD5,0x39,0x3A,0xED,0x29,0xEA,0xB2,
108 0xDD,0x2D,0x8B,0xF5,0xE0,0x4B,0xC5,0x52,0x3E,0xB0,0x28,0x44,
109 0x8A,0x31,0x88,0x7C,0x42,0xEE,0xA5,0xD5,0x5E,0x5E,0x28,0x4A,
110 0xD3,0x4E,0xA9,0x32,0x09,0xC0,0x04,0xF0,0xDB,0x91,0x3C,0x7A,
111 0x38,0xEB,0x58,0x99,0x26,0x5A,0x19,0x05,0xFE,0x38,0xB5,0xB6,
112 0x14,0xF9,0xB0,0xBD,0xCA,0xF3,0x80,0x2C,0xFD,0xB7,0xD7,0x00,
113 0xB2,0xE5,0x1C,0x05,0x9B,0xF5,0x2D,0xE1,0xC4,0xA9,0x96,0x6D,
114 0xB7,0x22,0xD4,0xA0,0xAD,0x23,0x8D,0x25,0xE0,0x17,0x27,0x94,
115 0xE3,0xF3,0x5F,0xC8,0x0F,0xB3,0xB8,0xD7,0x96,0xCD,0x42,0xFA,
118 static unsigned char dh2048_g[]={ 0x02, };
121 if ((dh=DH_new()) == NULL)
123 dh->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
124 dh->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
125 if ((dh->p == NULL) || (dh->g == NULL))
136 ** TLS_RAND_INIT -- initialize STARTTLS random generator
139 ** randfile -- name of file with random data
146 ** initializes PRNG for tls library.
149 # define MIN_RAND_BYTES 128 /* 1024 bits */
151 # define RF_OK 0 /* randfile OK */
152 # define RF_MISS 1 /* randfile == NULL || *randfile == '\0' */
153 # define RF_UNKNOWN 2 /* unknown prefix for randfile */
155 # define RI_NONE 0 /* no init yet */
156 # define RI_SUCCESS 1 /* init was successful */
157 # define RI_FAIL 2 /* init failed */
159 static bool tls_rand_init __P((char *, int));
162 tls_rand_init(randfile, logl)
166 # ifndef HASURANDOMDEV
167 /* not required if /dev/urandom exists, OpenSSL does it internally */
171 static int done = RI_NONE;
177 /* did we try this before? if yes: return old value */
179 return done == RI_SUCCESS;
181 /* set default values */
184 randdef = (randfile == NULL || *randfile == '\0') ? RF_MISS : RF_OK;
186 if (randdef == RF_OK && sm_strncasecmp(randfile, "egd:", 4) == 0)
189 if (RAND_egd(randfile) < 0)
191 sm_syslog(LOG_WARNING, NOQID,
192 "STARTTLS: RAND_egd(%s) failed: random number generator not seeded",
200 if (randdef == RF_OK && sm_strncasecmp(randfile, "file:", 5) == 0)
207 sff = SFF_SAFEDIRPATH | SFF_NOWLINK
208 | SFF_NOGWFILES | SFF_NOWWFILES
209 | SFF_NOGRFILES | SFF_NOWRFILES
210 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
211 if (DontLockReadFiles)
213 if ((fd = safeopen(randfile, O_RDONLY, 0, sff)) >= 0)
215 if (fstat(fd, &st) < 0)
218 sm_syslog(LOG_ERR, NOQID,
219 "STARTTLS: can't fstat(%s)",
229 /* max. age of file: 10 minutes */
230 if (st.st_mtime + 600 < curtime())
232 use = bitnset(DBS_INSUFFICIENTENTROPY,
236 sm_syslog(LOG_ERR, NOQID,
237 "STARTTLS: RandFile %s too old: %s",
242 if (use && st.st_size < MIN_RAND_BYTES)
244 use = bitnset(DBS_INSUFFICIENTENTROPY,
248 sm_syslog(LOG_ERR, NOQID,
249 "STARTTLS: size(%s) < %d: %s",
256 ok = RAND_load_file(randfile, -1) >=
261 sm_syslog(LOG_WARNING, NOQID,
262 "STARTTLS: RAND_load_file(%s) failed: random number generator not seeded",
268 if (ok || bitnset(DBS_INSUFFICIENTENTROPY,
271 /* add this even if fstat() failed */
272 RAND_seed((void *) &st, sizeof(st));
279 sm_syslog(LOG_WARNING, NOQID,
280 "STARTTLS: Warning: safeopen(%s) failed",
284 else if (randdef == RF_OK)
287 sm_syslog(LOG_WARNING, NOQID,
288 "STARTTLS: Error: no proper random file definition %s",
290 randdef = RF_UNKNOWN;
292 if (randdef == RF_MISS)
295 sm_syslog(LOG_WARNING, NOQID,
296 "STARTTLS: Error: missing random file definition");
298 if (!ok && bitnset(DBS_INSUFFICIENTENTROPY, DontBlameSendmail))
302 unsigned char buf[MIN_RAND_BYTES];
304 /* assert((MIN_RAND_BYTES % sizeof(long)) == 0); */
305 for (i = 0; i <= sizeof(buf) - sizeof(long); i += sizeof(long))
308 (void) memcpy(buf + i, (void *) &r, sizeof(long));
310 RAND_seed(buf, sizeof(buf));
312 sm_syslog(LOG_WARNING, NOQID,
313 "STARTTLS: Warning: random number generator not properly seeded");
316 done = ok ? RI_SUCCESS : RI_FAIL;
318 # else /* ! HASURANDOMDEV */
320 # endif /* ! HASURANDOMDEV */
323 ** INIT_TLS_LIBRARY -- Calls functions which setup TLS library for global use.
326 ** fipsmode -- use FIPS?
333 init_tls_library(fipsmode)
338 /* basic TLS initialization, ignore result for now */
340 SSL_load_error_strings();
341 OpenSSL_add_all_algorithms();
343 /* this is currently a macro for SSL_library_init */
344 SSLeay_add_ssl_algorithms();
347 bv = tls_rand_init(RandFile, 7);
351 if (!FIPS_mode_set(1))
355 err = ERR_get_error();
357 sm_syslog(LOG_ERR, NOQID,
358 "STARTTLS=init, FIPSMode=%s",
359 ERR_error_string(err, NULL));
365 sm_syslog(LOG_INFO, NOQID,
366 "STARTTLS=init, FIPSMode=ok");
369 #endif /* _FFR_FIPSMODE */
370 if (bv && CertFingerprintAlgorithm != NULL)
374 md = EVP_get_digestbyname(CertFingerprintAlgorithm);
379 sm_syslog(LOG_ERR, NOQID,
380 "STARTTLS=init, CertFingerprintAlgorithm=%s, status=invalid"
381 , CertFingerprintAlgorithm);
390 ** TLS_SET_VERIFY -- request client certificate?
393 ** ctx -- TLS context
394 ** ssl -- TLS structure
395 ** vrfy -- request certificate?
401 ** Sets verification state for TLS
403 # if TLS_VRFY_PER_CTX
405 ** This is per TLS context, not per TLS structure;
406 ** the former is global, the latter per connection.
407 ** It would be nice to do this per connection, but this
408 ** doesn't work in the current TLS libraries :-(
409 # endif * TLS_VRFY_PER_CTX *
413 tls_set_verify(ctx, ssl, vrfy)
418 # if !TLS_VRFY_PER_CTX
419 SSL_set_verify(ssl, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
420 # else /* !TLS_VRFY_PER_CTX */
421 SSL_CTX_set_verify(ctx, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
423 # endif /* !TLS_VRFY_PER_CTX */
427 ** status in initialization
428 ** these flags keep track of the status of the initialization
429 ** i.e., whether a file exists (_EX) and whether it can be used (_OK)
430 ** [due to permissions]
433 # define TLS_S_NONE 0x00000000 /* none yet */
434 # define TLS_S_CERT_EX 0x00000001 /* cert file exists */
435 # define TLS_S_CERT_OK 0x00000002 /* cert file is ok */
436 # define TLS_S_KEY_EX 0x00000004 /* key file exists */
437 # define TLS_S_KEY_OK 0x00000008 /* key file is ok */
438 # define TLS_S_CERTP_EX 0x00000010 /* CA cert path exists */
439 # define TLS_S_CERTP_OK 0x00000020 /* CA cert path is ok */
440 # define TLS_S_CERTF_EX 0x00000040 /* CA cert file exists */
441 # define TLS_S_CERTF_OK 0x00000080 /* CA cert file is ok */
442 # define TLS_S_CRLF_EX 0x00000100 /* CRL file exists */
443 # define TLS_S_CRLF_OK 0x00000200 /* CRL file is ok */
445 # define TLS_S_CERT2_EX 0x00001000 /* 2nd cert file exists */
446 # define TLS_S_CERT2_OK 0x00002000 /* 2nd cert file is ok */
447 # define TLS_S_KEY2_EX 0x00004000 /* 2nd key file exists */
448 # define TLS_S_KEY2_OK 0x00008000 /* 2nd key file is ok */
450 # define TLS_S_DH_OK 0x00200000 /* DH cert is ok */
451 # define TLS_S_DHPAR_EX 0x00400000 /* DH param file exists */
452 # define TLS_S_DHPAR_OK 0x00800000 /* DH param file is ok to use */
454 /* Type of variable */
455 # define TLS_T_OTHER 0
460 ** TLS_OK_F -- can var be an absolute filename?
464 ** fn -- what is the filename used for?
465 ** type -- type of variable
472 tls_ok_f(var, fn, type)
477 /* must be absolute pathname */
478 if (var != NULL && *var == '/')
481 sm_syslog(LOG_WARNING, NOQID, "STARTTLS: %s%s missing",
482 type == TLS_T_SRV ? "Server" :
483 (type == TLS_T_CLT ? "Client" : ""), fn);
487 ** TLS_SAFE_F -- is a file safe to use?
491 ** sff -- flags for safefile()
492 ** srv -- server side?
499 tls_safe_f(var, sff, srv)
506 if ((ret = safefile(var, RunAsUid, RunAsGid, RunAsUserName, sff,
507 S_IRUSR, NULL)) == 0)
510 sm_syslog(LOG_WARNING, NOQID, "STARTTLS=%s: file %s unsafe: %s",
511 srv ? "server" : "client", var, sm_errstring(ret));
516 ** TLS_OK_F -- macro to simplify calls to tls_ok_f
520 ** fn -- what is the filename used for?
521 ** req -- is the file required?
522 ** st -- status bit to set if ok
523 ** type -- type of variable
526 ** uses r, ok; may change ok and status.
530 # define TLS_OK_F(var, fn, req, st, type) if (ok) \
532 r = tls_ok_f(var, fn, type); \
540 ** TLS_UNR -- macro to return whether a file should be unreadable
543 ** bit -- flag to test
549 # define TLS_UNR(bit, req) (bitset(bit, req) ? SFF_NORFILES : 0)
550 # define TLS_OUNR(bit, req) (bitset(bit, req) ? SFF_NOWRFILES : 0)
551 # define TLS_KEYSFF(req) \
552 (bitnset(DBS_GROUPREADABLEKEYFILE, DontBlameSendmail) ? \
553 TLS_OUNR(TLS_I_KEY_OUNR, req) : \
554 TLS_UNR(TLS_I_KEY_UNR, req))
557 ** TLS_SAFE_F -- macro to simplify calls to tls_safe_f
561 ** sff -- flags for safefile()
562 ** req -- is the file required?
563 ** ex -- does the file exist?
564 ** st -- status bit to set if ok
565 ** srv -- server side?
568 ** uses r, ok, ex; may change ok and status.
572 # define TLS_SAFE_F(var, sff, req, ex, st, srv) if (ex && ok) \
574 r = tls_safe_f(var, sff, srv); \
581 # if _FFR_TLS_SE_OPTS
583 ** LOAD_CERTKEY -- load cert/key for TLS session
586 ** ssl -- TLS session context
587 ** certfile -- filename of certificate
588 ** keyfile -- filename of private key
595 load_certkey(ssl, srv, certfile, keyfile)
608 who = srv ? "server" : "client";
610 req = TLS_I_CERT_EX|TLS_I_KEY_EX;
611 TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req),
612 TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT);
613 TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req),
614 TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT);
616 /* certfile etc. must be "safe". */
617 sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK
618 | SFF_NOGWFILES | SFF_NOWWFILES
619 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
620 if (DontLockReadFiles)
623 TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req),
624 bitset(TLS_I_CERT_EX, req),
625 bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv);
626 TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req),
627 bitset(TLS_I_KEY_EX, req),
628 bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv);
630 # define SSL_use_cert(ssl, certfile) \
631 SSL_use_certificate_file(ssl, certfile, SSL_FILETYPE_PEM)
632 # define SSL_USE_CERT "SSL_use_certificate_file"
634 if (bitset(TLS_S_CERT_OK, status) &&
635 SSL_use_cert(ssl, certfile) <= 0)
639 sm_syslog(LOG_WARNING, NOQID,
640 "STARTTLS=%s, error: %s(%s) failed",
641 who, SSL_USE_CERT, certfile);
643 tlslogerr(LOG_WARNING, who);
645 if (bitset(TLS_I_USE_CERT, req))
648 if (bitset(TLS_S_KEY_OK, status) &&
649 SSL_use_PrivateKey_file(ssl, keyfile, SSL_FILETYPE_PEM) <= 0)
653 sm_syslog(LOG_WARNING, NOQID,
654 "STARTTLS=%s, error: SSL_use_PrivateKey_file(%s) failed",
657 tlslogerr(LOG_WARNING, who);
659 if (bitset(TLS_I_USE_KEY, req))
663 /* check the private key */
664 if (bitset(TLS_S_KEY_OK, status) &&
665 (r = SSL_check_private_key(ssl)) <= 0)
667 /* Private key does not match the certificate public key */
670 sm_syslog(LOG_WARNING, NOQID,
671 "STARTTLS=%s, error: SSL_check_private_key failed(%s): %d",
674 tlslogerr(LOG_WARNING, who);
676 if (bitset(TLS_I_USE_KEY, req))
682 # endif /* _FFR_TLS_SE_OPTS */
685 ** INITTLS -- initialize TLS
688 ** ctx -- pointer to context
689 ** req -- requirements for initialization (see sendmail.h)
690 ** options -- options
691 ** srv -- server side?
692 ** certfile -- filename of certificate
693 ** keyfile -- filename of private key
694 ** cacertpath -- path to CAs
695 ** cacertfile -- file with CA(s)
696 ** dhparam -- parameters for DH
703 ** The session_id_context identifies the service that created a session.
704 ** This information is used to distinguish between multiple TLS-based
705 ** servers running on the same server. We use the name of the mail system.
706 ** Note: the session cache is not persistent.
709 static char server_session_id_context[] = "sendmail8";
711 /* 0.9.8a and b have a problem with SSL_OP_TLS_BLOCK_PADDING_BUG */
712 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
713 # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 1
715 # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 0
719 inittls(ctx, req, options, srv, certfile, keyfile, cacertpath, cacertfile, dhparam)
722 unsigned long options;
724 char *certfile, *keyfile, *cacertpath, *cacertfile, *dhparam;
727 static DH *dh = NULL;
736 # endif /* SM_CONF_SHM */
737 # if OPENSSL_VERSION_NUMBER > 0x00907000L
741 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
742 #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
744 STACK_OF(SSL_COMP) *comp_methods;
748 who = srv ? "server" : "client";
751 syserr("STARTTLS=%s, inittls: ctx == NULL", who);
753 SM_ASSERT(ctx != NULL);
756 /* already initialized? (we could re-init...) */
762 ** look for a second filename: it must be separated by a ','
763 ** no blanks allowed (they won't be skipped).
764 ** we change a global variable here! this change will be undone
765 ** before return from the function but only if it returns true.
766 ** this isn't a problem since in a failure case this function
767 ** won't be called again with the same (overwritten) values.
768 ** otherwise each return must be replaced with a goto endinittls.
773 if (certfile != NULL && (cf2 = strchr(certfile, ',')) != NULL)
776 if (keyfile != NULL && (kf2 = strchr(keyfile, ',')) != NULL)
781 ** Check whether files/paths are defined
784 TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req),
785 TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT);
786 TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req),
787 TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT);
788 TLS_OK_F(cacertpath, "CACertPath", bitset(TLS_I_CERTP_EX, req),
789 TLS_S_CERTP_EX, TLS_T_OTHER);
790 TLS_OK_F(cacertfile, "CACertFile", bitset(TLS_I_CERTF_EX, req),
791 TLS_S_CERTF_EX, TLS_T_OTHER);
793 # if OPENSSL_VERSION_NUMBER > 0x00907000L
794 TLS_OK_F(CRLFile, "CRLFile", bitset(TLS_I_CRLF_EX, req),
795 TLS_S_CRLF_EX, TLS_T_OTHER);
796 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
799 ** if the second file is specified it must exist
800 ** XXX: it is possible here to define only one of those files
805 TLS_OK_F(cf2, "CertFile", bitset(TLS_I_CERT_EX, req),
806 TLS_S_CERT2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
810 TLS_OK_F(kf2, "KeyFile", bitset(TLS_I_KEY_EX, req),
811 TLS_S_KEY2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
815 ** valid values for dhparam are (only the first char is checked)
816 ** none no parameters: don't use DH
817 ** i use precomputed 2048 bit parameters
818 ** 512 use precomputed 512 bit parameters
819 ** 1024 generate 1024 bit parameters
820 ** 2048 generate 2048 bit parameters
821 ** /file/name read parameters from /file/name
827 req |= TLS_I_DHFIXED; \
830 if (bitset(TLS_I_TRY_DH, req))
838 else if (c == 'I' || c == 'i')
839 req |= TLS_I_DHFIXED;
844 else if (c == 'n' || c == 'N')
845 req &= ~TLS_I_TRY_DH;
849 sm_syslog(LOG_WARNING, NOQID,
850 "STARTTLS=%s, error: illegal value '%s' for DHParameters",
857 else if (*dhparam == '/')
859 TLS_OK_F(dhparam, "DHParameters",
860 bitset(TLS_I_DHPAR_EX, req),
861 TLS_S_DHPAR_EX, TLS_T_OTHER);
867 /* certfile etc. must be "safe". */
868 sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK
869 | SFF_NOGWFILES | SFF_NOWWFILES
870 | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
871 if (DontLockReadFiles)
874 TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req),
875 bitset(TLS_I_CERT_EX, req),
876 bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv);
877 TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req),
878 bitset(TLS_I_KEY_EX, req),
879 bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv);
880 TLS_SAFE_F(cacertfile, sff | TLS_UNR(TLS_I_CERTF_UNR, req),
881 bitset(TLS_I_CERTF_EX, req),
882 bitset(TLS_S_CERTF_EX, status), TLS_S_CERTF_OK, srv);
883 if (dhparam != NULL && *dhparam == '/')
885 TLS_SAFE_F(dhparam, sff | TLS_UNR(TLS_I_DHPAR_UNR, req),
886 bitset(TLS_I_DHPAR_EX, req),
887 bitset(TLS_S_DHPAR_EX, status), TLS_S_DHPAR_OK, srv);
888 if (!bitset(TLS_S_DHPAR_OK, status))
891 # if OPENSSL_VERSION_NUMBER > 0x00907000L
892 TLS_SAFE_F(CRLFile, sff | TLS_UNR(TLS_I_CRLF_UNR, req),
893 bitset(TLS_I_CRLF_EX, req),
894 bitset(TLS_S_CRLF_EX, status), TLS_S_CRLF_OK, srv);
895 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
900 TLS_SAFE_F(cf2, sff | TLS_UNR(TLS_I_CERT_UNR, req),
901 bitset(TLS_I_CERT_EX, req),
902 bitset(TLS_S_CERT2_EX, status), TLS_S_CERT2_OK, srv);
906 TLS_SAFE_F(kf2, sff | TLS_KEYSFF(req),
907 bitset(TLS_I_KEY_EX, req),
908 bitset(TLS_S_KEY2_EX, status), TLS_S_KEY2_OK, srv);
911 /* create a method and a new context */
912 if ((*ctx = SSL_CTX_new(srv ? SSLv23_server_method() :
913 SSLv23_client_method())) == NULL)
916 sm_syslog(LOG_WARNING, NOQID,
917 "STARTTLS=%s, error: SSL_CTX_new(SSLv23_%s_method()) failed",
920 tlslogerr(LOG_WARNING, who);
924 # if OPENSSL_VERSION_NUMBER > 0x00907000L
927 /* get a pointer to the current certificate validation store */
928 store = SSL_CTX_get_cert_store(*ctx); /* does not fail */
929 crl_file = BIO_new(BIO_s_file_internal());
930 if (crl_file != NULL)
932 if (BIO_read_filename(crl_file, CRLFile) >= 0)
934 crl = PEM_read_bio_X509_CRL(crl_file, NULL,
937 X509_STORE_add_crl(store, crl);
939 X509_STORE_set_flags(store,
940 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
941 X509_STORE_set_verify_cb_func(store,
948 sm_syslog(LOG_WARNING, NOQID,
949 "STARTTLS=%s, error: PEM_read_bio_X509_CRL(%s)=failed",
953 /* avoid memory leaks */
959 else if (LogLevel > 9)
960 sm_syslog(LOG_WARNING, NOQID,
961 "STARTTLS=%s, error: BIO_new=failed", who);
966 if (CRLPath != NULL && store != NULL)
970 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
975 sm_syslog(LOG_WARNING, NOQID,
976 "STARTTLS=%s, error: X509_STORE_add_lookup(hash)=failed",
981 X509_LOOKUP_add_dir(lookup, CRLPath, X509_FILETYPE_PEM);
982 X509_STORE_set_flags(store,
983 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
985 # endif /* _FFR_CRLPATH */
986 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
989 /* turn off backward compatibility, required for no-rsa */
990 SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2);
991 # endif /* TLS_NO_RSA */
996 ** Create a temporary RSA key
997 ** XXX Maybe we shouldn't create this always (even though it
998 ** is only at startup).
999 ** It is a time-consuming operation and it is not always necessary.
1000 ** maybe we should do it only on demand...
1003 if (bitset(TLS_I_RSA_TMP, req)
1005 && ShmId != SM_SHM_NO_ID &&
1006 (rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL,
1008 # else /* SM_CONF_SHM */
1009 && 0 /* no shared memory: no need to generate key now */
1010 # endif /* SM_CONF_SHM */
1015 sm_syslog(LOG_WARNING, NOQID,
1016 "STARTTLS=%s, error: RSA_generate_key failed",
1019 tlslogerr(LOG_WARNING, who);
1023 # endif /* !TLS_NO_RSA */
1027 ** XXX change this for DSA-only version
1030 if (bitset(TLS_S_KEY_OK, status) &&
1031 SSL_CTX_use_PrivateKey_file(*ctx, keyfile,
1032 SSL_FILETYPE_PEM) <= 0)
1036 sm_syslog(LOG_WARNING, NOQID,
1037 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
1040 tlslogerr(LOG_WARNING, who);
1042 if (bitset(TLS_I_USE_KEY, req))
1046 #if _FFR_TLS_USE_CERTIFICATE_CHAIN_FILE
1047 # define SSL_CTX_use_cert(ssl_ctx, certfile) \
1048 SSL_CTX_use_certificate_chain_file(ssl_ctx, certfile)
1049 # define SSL_CTX_USE_CERT "SSL_CTX_use_certificate_chain_file"
1051 # define SSL_CTX_use_cert(ssl_ctx, certfile) \
1052 SSL_CTX_use_certificate_file(ssl_ctx, certfile, SSL_FILETYPE_PEM)
1053 # define SSL_CTX_USE_CERT "SSL_CTX_use_certificate_file"
1056 /* get the certificate file */
1057 if (bitset(TLS_S_CERT_OK, status) &&
1058 SSL_CTX_use_cert(*ctx, certfile) <= 0)
1062 sm_syslog(LOG_WARNING, NOQID,
1063 "STARTTLS=%s, error: %s(%s) failed",
1064 who, SSL_CTX_USE_CERT, certfile);
1066 tlslogerr(LOG_WARNING, who);
1068 if (bitset(TLS_I_USE_CERT, req))
1072 /* check the private key */
1073 if (bitset(TLS_S_KEY_OK, status) &&
1074 (r = SSL_CTX_check_private_key(*ctx)) <= 0)
1076 /* Private key does not match the certificate public key */
1079 sm_syslog(LOG_WARNING, NOQID,
1080 "STARTTLS=%s, error: SSL_CTX_check_private_key failed(%s): %d",
1083 tlslogerr(LOG_WARNING, who);
1085 if (bitset(TLS_I_USE_KEY, req))
1089 /* XXX this code is pretty much duplicated from above! */
1091 /* load private key */
1092 if (bitset(TLS_S_KEY2_OK, status) &&
1093 SSL_CTX_use_PrivateKey_file(*ctx, kf2, SSL_FILETYPE_PEM) <= 0)
1097 sm_syslog(LOG_WARNING, NOQID,
1098 "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
1101 tlslogerr(LOG_WARNING, who);
1105 /* get the certificate file */
1106 if (bitset(TLS_S_CERT2_OK, status) &&
1107 SSL_CTX_use_cert(*ctx, cf2) <= 0)
1111 sm_syslog(LOG_WARNING, NOQID,
1112 "STARTTLS=%s, error: %s(%s) failed",
1113 who, SSL_CTX_USE_CERT, cf2);
1115 tlslogerr(LOG_WARNING, who);
1119 /* also check the private key */
1120 if (bitset(TLS_S_KEY2_OK, status) &&
1121 (r = SSL_CTX_check_private_key(*ctx)) <= 0)
1123 /* Private key does not match the certificate public key */
1126 sm_syslog(LOG_WARNING, NOQID,
1127 "STARTTLS=%s, error: SSL_CTX_check_private_key 2 failed: %d",
1130 tlslogerr(LOG_WARNING, who);
1134 /* SSL_CTX_set_quiet_shutdown(*ctx, 1); violation of standard? */
1136 #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
1139 ** In OpenSSL 0.9.8[ab], enabling zlib compression breaks the
1140 ** padding bug work-around, leading to false positives and
1141 ** failed connections. We may not interoperate with systems
1142 ** with the bug, but this is better than breaking on all 0.9.8[ab]
1143 ** systems that have zlib support enabled.
1144 ** Note: this checks the runtime version of the library, not
1145 ** just the compile time version.
1148 rt_version = SSLeay();
1149 if (rt_version >= 0x00908000L && rt_version <= 0x0090802fL)
1151 comp_methods = SSL_COMP_get_compression_methods();
1152 if (comp_methods != NULL && sk_SSL_COMP_num(comp_methods) > 0)
1153 options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG;
1156 SSL_CTX_set_options(*ctx, (long) options);
1159 /* Diffie-Hellman initialization */
1160 if (bitset(TLS_I_TRY_DH, req))
1164 #endif /* _FFR_TLS_EC */
1167 sm_dprintf("inittls: req=%#lx, status=%#lx\n",
1169 if (bitset(TLS_S_DHPAR_OK, status))
1173 if ((bio = BIO_new_file(dhparam, "r")) != NULL)
1175 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1177 if (dh == NULL && LogLevel > 7)
1181 err = ERR_get_error();
1182 sm_syslog(LOG_WARNING, NOQID,
1183 "STARTTLS=%s, error: cannot read DH parameters(%s): %s",
1185 ERR_error_string(err, NULL));
1187 tlslogerr(LOG_WARNING, who);
1195 sm_syslog(LOG_WARNING, NOQID,
1196 "STARTTLS=%s, error: BIO_new_file(%s) failed",
1199 tlslogerr(LOG_WARNING, who);
1203 if (dh == NULL && bitset(TLS_I_DH1024|TLS_I_DH2048, req))
1208 bits = bitset(TLS_I_DH2048, req) ? 2048 : 1024;
1210 sm_dprintf("inittls: Generating %d bit DH parameters\n", bits);
1212 /* this takes a while! */
1213 dsa = DSA_generate_parameters(bits, NULL, 0, NULL,
1215 dh = DSA_dup_DH(dsa);
1218 else if (dh == NULL && bitset(TLS_I_DHFIXED, req))
1221 sm_dprintf("inittls: Using precomputed 2048 bit DH parameters\n");
1224 else if (dh == NULL && bitset(TLS_I_DH512, req))
1227 sm_dprintf("inittls: Using precomputed 512 bit DH parameters\n");
1237 err = ERR_get_error();
1238 sm_syslog(LOG_WARNING, NOQID,
1239 "STARTTLS=%s, error: cannot read or set DH parameters(%s): %s",
1241 ERR_error_string(err, NULL));
1243 if (bitset(TLS_I_REQ_DH, req))
1248 /* important to avoid small subgroup attacks */
1249 SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_DH_USE);
1251 SSL_CTX_set_tmp_dh(*ctx, dh);
1253 sm_syslog(LOG_INFO, NOQID,
1254 "STARTTLS=%s, Diffie-Hellman init, key=%d bit (%c)",
1255 who, 8 * DH_size(dh), *dhparam);
1260 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1263 SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_ECDH_USE);
1264 SSL_CTX_set_tmp_ecdh(*ctx, ecdh);
1267 #endif /* _FFR_TLS_EC */
1270 # endif /* !NO_DH */
1273 /* XXX do we need this cache here? */
1274 if (bitset(TLS_I_CACHE, req))
1276 SSL_CTX_sess_set_cache_size(*ctx, 1);
1277 SSL_CTX_set_timeout(*ctx, 1);
1278 SSL_CTX_set_session_id_context(*ctx,
1279 (void *) &server_session_id_context,
1280 sizeof(server_session_id_context));
1281 (void) SSL_CTX_set_session_cache_mode(*ctx,
1282 SSL_SESS_CACHE_SERVER);
1286 (void) SSL_CTX_set_session_cache_mode(*ctx,
1287 SSL_SESS_CACHE_OFF);
1290 /* load certificate locations and default CA paths */
1291 if (bitset(TLS_S_CERTP_EX, status) && bitset(TLS_S_CERTF_EX, status))
1293 if ((r = SSL_CTX_load_verify_locations(*ctx, cacertfile,
1297 if (bitset(TLS_I_RSA_TMP, req))
1298 SSL_CTX_set_tmp_rsa_callback(*ctx, tmp_rsa_key);
1299 # endif /* !TLS_NO_RSA */
1302 ** We have to install our own verify callback:
1303 ** SSL_VERIFY_PEER requests a client cert but even
1304 ** though *FAIL_IF* isn't set, the connection
1305 ** will be aborted if the client presents a cert
1306 ** that is not "liked" (can't be verified?) by
1307 ** the TLS library :-(
1311 ** XXX currently we could call tls_set_verify()
1312 ** but we hope that that function will later on
1313 ** only set the mode per connection.
1315 SSL_CTX_set_verify(*ctx,
1316 bitset(TLS_I_NO_VRFY, req) ? SSL_VERIFY_NONE
1320 /* install verify callback */
1321 SSL_CTX_set_cert_verify_callback(*ctx, tls_verify_cb,
1323 SSL_CTX_set_client_CA_list(*ctx,
1324 SSL_load_client_CA_file(cacertfile));
1329 ** can't load CA data; do we care?
1330 ** the data is necessary to authenticate the client,
1331 ** which in turn would be necessary
1332 ** if we want to allow relaying based on it.
1336 sm_syslog(LOG_WARNING, NOQID,
1337 "STARTTLS=%s, error: load verify locs %s, %s failed: %d",
1338 who, cacertpath, cacertfile, r);
1340 tlslogerr(LOG_WARNING, who);
1342 if (bitset(TLS_I_VRFY_LOC, req))
1347 /* XXX: make this dependent on an option? */
1349 SSL_CTX_set_info_callback(*ctx, apps_ssl_info_cb);
1351 /* install our own cipher list */
1352 if (CipherList != NULL && *CipherList != '\0')
1354 if (SSL_CTX_set_cipher_list(*ctx, CipherList) <= 0)
1358 sm_syslog(LOG_WARNING, NOQID,
1359 "STARTTLS=%s, error: SSL_CTX_set_cipher_list(%s) failed, list ignored",
1363 tlslogerr(LOG_WARNING, who);
1365 /* failure if setting to this list is required? */
1370 sm_syslog(LOG_INFO, NOQID, "STARTTLS=%s, init=%d", who, ok);
1374 ** this label is required if we want to have a "clean" exit
1375 ** see the comments above at the initialization of cf2
1381 /* undo damage to global variables */
1391 ** CERT_FP -- get cert fingerprint
1395 ** mac -- macro storage
1396 ** macro -- where to store cert fp
1399 ** <=0: cert fp calculation failed
1400 ** >0: cert fp calculation ok
1404 cert_fp(cert, evp_digest, mac, macro)
1406 const EVP_MD *evp_digest;
1412 unsigned char md[EVP_MAX_MD_SIZE];
1413 char md5h[EVP_MAX_MD_SIZE * 3];
1414 static const char hexcodes[] = "0123456789ABCDEF";
1417 if (X509_digest(cert, EVP_digest, md, &n) == 0 || n <= 0)
1419 macdefine(mac, A_TEMP, macid(macro), "");
1423 SM_ASSERT((n * 3) + 2 < sizeof(md5h));
1424 for (r = 0; r < (int) n; r++)
1426 md5h[r * 3] = hexcodes[(md[r] & 0xf0) >> 4];
1427 md5h[(r * 3) + 1] = hexcodes[(md[r] & 0x0f)];
1428 md5h[(r * 3) + 2] = ':';
1430 md5h[(n * 3) - 1] = '\0';
1431 macdefine(mac, A_TEMP, macid(macro), md5h);
1436 ** TLS_GET_INFO -- get information about TLS connection
1439 ** ssl -- TLS connection structure
1440 ** srv -- server or client
1441 ** host -- hostname of other side
1442 ** mac -- macro storage
1443 ** certreq -- did we ask for a cert?
1446 ** result of authentication.
1449 ** sets various TLS related macros.
1453 tls_get_info(ssl, srv, host, mac, certreq)
1460 const SSL_CIPHER *c;
1467 c = SSL_get_current_cipher(ssl);
1469 /* cast is just workaround for compiler warning */
1470 macdefine(mac, A_TEMP, macid("{cipher}"),
1471 (char *) SSL_CIPHER_get_name(c));
1472 b = SSL_CIPHER_get_bits(c, &r);
1473 (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", b);
1474 macdefine(mac, A_TEMP, macid("{cipher_bits}"), bitstr);
1475 (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", r);
1476 macdefine(mac, A_TEMP, macid("{alg_bits}"), bitstr);
1477 s = (char *) SSL_get_version(ssl);
1480 macdefine(mac, A_TEMP, macid("{tls_version}"), s);
1482 who = srv ? "server" : "client";
1483 cert = SSL_get_peer_certificate(ssl);
1484 verifyok = SSL_get_verify_result(ssl);
1486 sm_syslog(LOG_INFO, NOQID,
1487 "STARTTLS=%s, get_verify: %ld get_peer: 0x%lx",
1488 who, verifyok, (unsigned long) cert);
1491 X509_NAME *subj, *issuer;
1494 subj = X509_get_subject_name(cert);
1495 issuer = X509_get_issuer_name(cert);
1496 X509_NAME_oneline(subj, buf, sizeof(buf));
1497 macdefine(mac, A_TEMP, macid("{cert_subject}"),
1498 xtextify(buf, "<>\")"));
1499 X509_NAME_oneline(issuer, buf, sizeof(buf));
1500 macdefine(mac, A_TEMP, macid("{cert_issuer}"),
1501 xtextify(buf, "<>\")"));
1503 # define LL_BADCERT 8
1505 #define CERTFPMACRO (CertFingerprintAlgorithm != NULL ? "{cert_fp}" : "{cert_md5}")
1507 #define CHECK_X509_NAME(which) \
1511 sm_strlcpy(buf, "BadCertificateUnknown", sizeof(buf)); \
1512 if (LogLevel > LL_BADCERT) \
1513 sm_syslog(LOG_INFO, NOQID, \
1514 "STARTTLS=%s, relay=%.100s, field=%s, status=failed to extract CN", \
1516 host == NULL ? "local" : host, \
1519 else if ((size_t)r >= sizeof(buf) - 1) \
1521 sm_strlcpy(buf, "BadCertificateTooLong", sizeof(buf)); \
1523 sm_syslog(LOG_INFO, NOQID, \
1524 "STARTTLS=%s, relay=%.100s, field=%s, status=CN too long", \
1526 host == NULL ? "local" : host, \
1529 else if ((size_t)r > strlen(buf)) \
1531 sm_strlcpy(buf, "BadCertificateContainsNUL", \
1534 sm_syslog(LOG_INFO, NOQID, \
1535 "STARTTLS=%s, relay=%.100s, field=%s, status=CN contains NUL", \
1537 host == NULL ? "local" : host, \
1542 r = X509_NAME_get_text_by_NID(subj, NID_commonName, buf,
1544 CHECK_X509_NAME("cn_subject");
1545 macdefine(mac, A_TEMP, macid("{cn_subject}"),
1546 xtextify(buf, "<>\")"));
1547 r = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf,
1549 CHECK_X509_NAME("cn_issuer");
1550 macdefine(mac, A_TEMP, macid("{cn_issuer}"),
1551 xtextify(buf, "<>\")"));
1552 (void) cert_fp(cert, EVP_digest, mac, CERTFPMACRO);
1556 macdefine(mac, A_PERM, macid("{cert_subject}"), "");
1557 macdefine(mac, A_PERM, macid("{cert_issuer}"), "");
1558 macdefine(mac, A_PERM, macid("{cn_subject}"), "");
1559 macdefine(mac, A_PERM, macid("{cn_issuer}"), "");
1560 macdefine(mac, A_TEMP, macid(CERTFPMACRO), "");
1572 s = certreq ? "NO" : "NOT",
1581 macdefine(mac, A_PERM, macid("{verify}"), s);
1585 /* do some logging */
1588 char *vers, *s1, *s2, *cbits, *algbits;
1590 vers = macget(mac, macid("{tls_version}"));
1591 cbits = macget(mac, macid("{cipher_bits}"));
1592 algbits = macget(mac, macid("{alg_bits}"));
1593 s1 = macget(mac, macid("{verify}"));
1594 s2 = macget(mac, macid("{cipher}"));
1596 /* XXX: maybe cut off ident info? */
1597 sm_syslog(LOG_INFO, NOQID,
1598 "STARTTLS=%s, relay=%.100s, version=%.16s, verify=%.16s, cipher=%.64s, bits=%.6s/%.6s",
1600 host == NULL ? "local" : host,
1601 vers, s1, s2, /* sm_snprintf() can deal with NULL */
1602 algbits == NULL ? "0" : algbits,
1603 cbits == NULL ? "0" : cbits);
1607 ** Maybe run xuntextify on the strings?
1608 ** That is easier to read but makes it maybe a bit
1609 ** more complicated to figure out the right values
1610 ** for the access map...
1613 s1 = macget(mac, macid("{cert_subject}"));
1614 s2 = macget(mac, macid("{cert_issuer}"));
1615 sm_syslog(LOG_INFO, NOQID,
1616 "STARTTLS=%s, cert-subject=%.256s, cert-issuer=%.256s, verifymsg=%s",
1618 X509_verify_cert_error_string(verifyok));
1624 ** ENDTLS -- shutdown secure connection
1627 ** ssl -- SSL connection information.
1628 ** side -- server/client (for logging).
1631 ** success? (EX_* code)
1645 if ((r = SSL_shutdown(ssl)) < 0)
1649 sm_syslog(LOG_WARNING, NOQID,
1650 "STARTTLS=%s, SSL_shutdown failed: %d",
1652 tlslogerr(LOG_WARNING, side);
1656 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL
1659 ** Bug in OpenSSL (at least up to 0.9.6b):
1660 ** From: Lutz.Jaenicke@aet.TU-Cottbus.DE
1661 ** Message-ID: <20010723152244.A13122@serv01.aet.tu-cottbus.de>
1662 ** To: openssl-users@openssl.org
1663 ** Subject: Re: SSL_shutdown() woes (fwd)
1665 ** The side sending the shutdown alert first will
1666 ** not care about the answer of the peer but will
1667 ** immediately return with a return value of "0"
1668 ** (ssl/s3_lib.c:ssl3_shutdown()). SSL_get_error will evaluate
1669 ** the value of "0" and as the shutdown alert of the peer was
1670 ** not received (actually, the program did not even wait for
1671 ** the answer), an SSL_ERROR_SYSCALL is flagged, because this
1672 ** is the default rule in case everything else does not apply.
1674 ** For your server the problem is different, because it
1675 ** receives the shutdown first (setting SSL_RECEIVED_SHUTDOWN),
1676 ** then sends its response (SSL_SENT_SHUTDOWN), so for the
1677 ** server the shutdown was successfull.
1679 ** As is by know, you would have to call SSL_shutdown() once
1680 ** and ignore an SSL_ERROR_SYSCALL returned. Then call
1681 ** SSL_shutdown() again to actually get the server's response.
1683 ** In the last discussion, Bodo Moeller concluded that a
1684 ** rewrite of the shutdown code would be necessary, but
1685 ** probably with another API, as the change would not be
1686 ** compatible to the way it is now. Things do not become
1687 ** easier as other programs do not follow the shutdown
1688 ** guidelines anyway, so that a lot error conditions and
1689 ** compitibility issues would have to be caught.
1691 ** For now the recommondation is to ignore the error message.
1698 sm_syslog(LOG_WARNING, NOQID,
1699 "STARTTLS=%s, SSL_shutdown not done",
1701 tlslogerr(LOG_WARNING, side);
1705 # endif /* !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL */
1714 ** TMP_RSA_KEY -- return temporary RSA key
1717 ** s -- TLS connection structure
1722 ** temporary RSA key.
1725 # ifndef MAX_RSA_TMP_CNT
1726 # define MAX_RSA_TMP_CNT 1000 /* XXX better value? */
1727 # endif /* ! MAX_RSA_TMP_CNT */
1731 tmp_rsa_key(s, export, keylength)
1738 extern int *PRSATmpCnt;
1740 if (ShmId != SM_SHM_NO_ID && rsa_tmp != NULL &&
1741 ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT)
1743 # endif /* SM_CONF_SHM */
1745 if (rsa_tmp != NULL)
1747 rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, NULL);
1748 if (rsa_tmp == NULL)
1751 sm_syslog(LOG_ERR, NOQID,
1752 "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!");
1759 ** XXX we can't (yet) share the new key...
1760 ** The RSA structure contains pointers hence it can't be
1761 ** easily kept in shared memory. It must be transformed
1762 ** into a continous memory region first, then stored,
1763 ** and later read out again (each time re-transformed).
1766 if (ShmId != SM_SHM_NO_ID)
1769 # endif /* SM_CONF_SHM */
1771 sm_syslog(LOG_ERR, NOQID,
1772 "STARTTLS=server, tmp_rsa_key: new temp RSA key");
1776 # endif /* !TLS_NO_RSA */
1778 ** APPS_SSL_INFO_CB -- info callback for TLS connections
1781 ** s -- TLS connection structure
1782 ** where -- state in handshake
1783 ** ret -- return code of last operation
1790 apps_ssl_info_cb(s, where, ret)
1797 BIO *bio_err = NULL;
1800 sm_syslog(LOG_INFO, NOQID,
1801 "STARTTLS: info_callback where=0x%x, ret=%d",
1804 w = where & ~SSL_ST_MASK;
1805 if (bio_err == NULL)
1806 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1808 if (bitset(SSL_ST_CONNECT, w))
1809 str = "SSL_connect";
1810 else if (bitset(SSL_ST_ACCEPT, w))
1815 if (bitset(SSL_CB_LOOP, where))
1818 sm_syslog(LOG_NOTICE, NOQID,
1820 str, SSL_state_string_long(s));
1822 else if (bitset(SSL_CB_ALERT, where))
1824 str = bitset(SSL_CB_READ, where) ? "read" : "write";
1826 sm_syslog(LOG_NOTICE, NOQID,
1827 "STARTTLS: SSL3 alert %s:%s:%s",
1828 str, SSL_alert_type_string_long(ret),
1829 SSL_alert_desc_string_long(ret));
1831 else if (bitset(SSL_CB_EXIT, where))
1836 sm_syslog(LOG_WARNING, NOQID,
1837 "STARTTLS: %s:failed in %s",
1838 str, SSL_state_string_long(s));
1843 sm_syslog(LOG_WARNING, NOQID,
1844 "STARTTLS: %s:error in %s",
1845 str, SSL_state_string_long(s));
1850 ** TLS_VERIFY_LOG -- log verify error for TLS certificates
1854 ** ctx -- x509 context
1855 ** name -- from where is this called?
1862 tls_verify_log(ok, ctx, name)
1864 X509_STORE_CTX *ctx;
1871 cert = X509_STORE_CTX_get_current_cert(ctx);
1872 reason = X509_STORE_CTX_get_error(ctx);
1873 depth = X509_STORE_CTX_get_error_depth(ctx);
1874 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
1875 sm_syslog(LOG_INFO, NOQID,
1876 "STARTTLS: %s cert verify: depth=%d %s, state=%d, reason=%s",
1877 name, depth, buf, ok, X509_verify_cert_error_string(reason));
1882 ** TLS_VERIFY_CB -- verify callback for TLS certificates
1885 ** ctx -- x509 context
1888 ** accept connection?
1889 ** currently: always yes.
1893 # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
1895 X509_STORE_CTX *ctx;
1896 # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1897 tls_verify_cb(ctx, unused)
1898 X509_STORE_CTX *ctx;
1900 # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1905 ** man SSL_CTX_set_cert_verify_callback():
1906 ** callback should return 1 to indicate verification success
1907 ** and 0 to indicate verification failure.
1910 ok = X509_verify_cert(ctx);
1914 return tls_verify_log(ok, ctx, "TLS");
1919 ** TLSLOGERR -- log the errors from the TLS error stack
1922 ** level -- syslog level
1923 ** who -- server/client (for logging).
1930 tlslogerr(level, who)
1940 es = CRYPTO_thread_id();
1941 while ((l = ERR_get_error_line_data((const char **) &file, &line,
1942 (const char **) &data, &flags))
1945 sm_syslog(level, NOQID,
1946 "STARTTLS=%s: %lu:%s:%s:%d:%s", who, es,
1947 ERR_error_string(l, buf),
1949 bitset(ERR_TXT_STRING, flags) ? data : "");
1953 # if OPENSSL_VERSION_NUMBER > 0x00907000L
1955 ** X509_VERIFY_CB -- verify callback
1958 ** ctx -- x509 context
1961 ** accept connection?
1962 ** currently: always yes.
1966 x509_verify_cb(ok, ctx)
1968 X509_STORE_CTX *ctx;
1973 tls_verify_log(ok, ctx, "x509");
1974 if (ctx->error == X509_V_ERR_UNABLE_TO_GET_CRL)
1977 return 1; /* override it */
1982 # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1983 #endif /* STARTTLS */