155 #define STR(x) vstring_str(x)
156 #define LEN(x) VSTRING_LEN(x)
165 static const char server_session_id_context[] =
"Postfix/TLS";
167 #if OPENSSL_VERSION_NUMBER >= 0x1000000fL
168 #define GET_SID(s, v, lptr) ((v) = SSL_SESSION_get_id((s), (lptr)))
171 #define GET_SID(s, v, lptr) \
172 do { (v) = (s)->session_id; *(lptr) = (s)->session_id_length; } while (0)
177 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
178 typedef const unsigned char *session_id_t;
181 typedef unsigned char *session_id_t;
187 static SSL_SESSION *get_server_session_cb(SSL *ssl, session_id_t session_id,
188 int session_id_length,
191 const char *myname =
"get_server_session_cb";
192 TLS_SESS_STATE *TLScontext;
195 SSL_SESSION *session = 0;
197 if ((TLScontext = SSL_get_ex_data(ssl, TLScontext_index)) == 0)
198 msg_panic(
"%s: null TLScontext in session lookup callback", myname);
200 #define GEN_CACHE_ID(buf, id, len, service) \
202 buf = vstring_alloc(2 * (len + strlen(service))); \
203 hex_encode(buf, (char *) (id), (len)); \
204 vstring_sprintf_append(buf, "&s=%s", (service)); \
205 vstring_sprintf_append(buf, "&l=%ld", (long) OpenSSL_version_num()); \
209 GEN_CACHE_ID(cache_id, session_id, session_id_length, TLScontext->serverid);
211 if (TLScontext->log_mask & TLS_LOG_CACHE)
212 msg_info(
"%s: looking up session %s in %s cache", TLScontext->namaddr,
213 STR(cache_id), TLScontext->cache_type);
220 session = tls_session_activate(
STR(session_data),
LEN(session_data));
221 if (session && (TLScontext->log_mask & TLS_LOG_CACHE))
222 msg_info(
"%s: reloaded session %s from %s cache",
223 TLScontext->namaddr,
STR(cache_id),
224 TLScontext->cache_type);
238 static void uncache_session(SSL_CTX *ctx, TLS_SESS_STATE *TLScontext)
241 SSL_SESSION *session = SSL_get_session(TLScontext->con);
242 const unsigned char *sid;
243 unsigned int sid_length;
245 SSL_CTX_remove_session(ctx, session);
247 if (TLScontext->cache_type == 0)
250 GET_SID(session, sid, &sid_length);
251 GEN_CACHE_ID(cache_id, sid, sid_length, TLScontext->serverid);
253 if (TLScontext->log_mask & TLS_LOG_CACHE)
254 msg_info(
"%s: remove session %s from %s cache", TLScontext->namaddr,
255 STR(cache_id), TLScontext->cache_type);
263 static int new_server_session_cb(SSL *ssl, SSL_SESSION *session)
265 const char *myname =
"new_server_session_cb";
267 TLS_SESS_STATE *TLScontext;
269 const unsigned char *sid;
270 unsigned int sid_length;
272 if ((TLScontext = SSL_get_ex_data(ssl, TLScontext_index)) == 0)
273 msg_panic(
"%s: null TLScontext in new session callback", myname);
275 GET_SID(session, sid, &sid_length);
276 GEN_CACHE_ID(cache_id, sid, sid_length, TLScontext->serverid);
278 if (TLScontext->log_mask & TLS_LOG_CACHE)
279 msg_info(
"%s: save session %s to %s cache", TLScontext->namaddr,
280 STR(cache_id), TLScontext->cache_type);
285 session_data = tls_session_passivate(session);
288 STR(session_data),
LEN(session_data));
296 SSL_SESSION_free(session);
301 #define NOENGINE ((ENGINE *) 0)
302 #define TLS_TKT_NOKEYS -1
303 #define TLS_TKT_STALE 0
304 #define TLS_TKT_ACCEPT 1
305 #define TLS_TKT_REISSUE 2
309 #if defined(SSL_OP_NO_TICKET) \
310 && !defined(OPENSSL_NO_TLSEXT) \
311 && OPENSSL_VERSION_NUMBER >= 0x0090808fL
313 static int ticket_cb(SSL *con,
unsigned char name[],
unsigned char iv[],
314 EVP_CIPHER_CTX * ctx, HMAC_CTX * hctx,
int create)
316 static const EVP_MD *sha256;
317 static const EVP_CIPHER *ciph;
319 TLS_SESS_STATE *TLScontext = SSL_get_ex_data(con, TLScontext_index);
320 int timeout = ((
int) SSL_CTX_get_timeout(SSL_get_SSL_CTX(con))) / 2;
322 if ((!sha256 && (sha256 = EVP_sha256()) == 0)
324 || (key =
tls_mgr_key(create ? 0 : name, timeout)) == 0
326 return (create ? TLS_TKT_NOKEYS : TLS_TKT_STALE);
331 EVP_EncryptInit_ex(ctx, ciph, NOENGINE, key->
bits, iv);
333 if (TLScontext->log_mask & TLS_LOG_CACHE)
334 msg_info(
"%s: Issuing session ticket, key expiration: %ld",
335 TLScontext->namaddr, (
long) key->
tout);
337 EVP_DecryptInit_ex(ctx, ciph, NOENGINE, key->
bits, iv);
338 if (TLScontext->log_mask & TLS_LOG_CACHE)
339 msg_info(
"%s: Decrypting session ticket, key expiration: %ld",
340 TLScontext->namaddr, (
long) key->
tout);
342 TLScontext->ticketed = 1;
343 return (TLS_TKT_ACCEPT);
350 TLS_APPL_STATE *tls_server_init(
const TLS_SERVER_INIT_PROPS *props)
354 int verify_flags = SSL_VERIFY_NONE;
359 TLS_APPL_STATE *app_ctx;
365 log_mask = tls_log_mask(props->log_param, props->log_level);
367 if (log_mask & TLS_LOG_VERBOSE)
368 msg_info(
"initializing the server-side TLS engine");
380 #if OPENSSL_VERSION_NUMBER < 0x10100000L
387 SSL_load_error_strings();
388 OpenSSL_add_ssl_algorithms();
394 protomask = tls_protocol_mask(props->protocols);
395 if (protomask == TLS_PROTOCOL_INVALID) {
397 msg_warn(
"Invalid TLS protocol list \"%s\": disabling TLS support",
407 if (TLScontext_index < 0) {
408 if ((TLScontext_index = SSL_get_ex_new_index(0, 0, 0, 0, 0)) < 0) {
409 msg_warn(
"Cannot allocate SSL application data index: "
410 "disabling TLS support");
419 if (!tls_validate_digest(props->mdalg)) {
430 msg_warn(
"no entropy for TLS key generation: disabling TLS support");
446 server_ctx = SSL_CTX_new(TLS_server_method());
447 if (server_ctx == 0) {
448 msg_warn(
"cannot allocate server SSL_CTX: disabling TLS support");
452 #ifdef SSL_SECOP_PEER
454 SSL_CTX_set_security_level(server_ctx, 0);
460 SSL_CTX_set_verify_depth(server_ctx, props->verifydepth + 1);
475 if (scache_timeout <= 0)
481 off |= tls_bug_bits();
488 #ifdef SSL_OP_NO_TICKET
489 #if !defined(OPENSSL_NO_TLSEXT) && OPENSSL_VERSION_NUMBER >= 0x0090808fL
491 && !(off & SSL_OP_NO_TICKET));
493 const EVP_CIPHER *ciph;
496 || EVP_CIPHER_mode(ciph) != EVP_CIPH_CBC_MODE
500 msg_warn(
"%s: invalid value: %s; session tickets disabled",
506 SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, ticket_cb);
509 off |= SSL_OP_NO_TICKET;
512 SSL_CTX_set_options(server_ctx, off);
518 SSL_CTX_set_options(server_ctx, TLS_SSL_OP_PROTOMASK(protomask));
530 SSL_CTX_set_options(server_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
535 if (log_mask & TLS_LOG_DEBUG)
536 SSL_CTX_set_info_callback(server_ctx, tls_info_callback);
549 if (tls_set_ca_certificate_info(server_ctx,
550 props->CAfile, props->CApath) < 0) {
552 SSL_CTX_free(server_ctx);
568 if (tls_set_my_certificate_key_info(server_ctx,
574 props->eckey_file) < 0) {
576 SSL_CTX_free(server_ctx);
583 #if OPENSSL_VERSION_NUMBER < 0x10100000L
590 SSL_CTX_set_tmp_rsa_callback(server_ctx, tls_tmp_rsa_cb);
601 SSL_CTX_set_tmp_dh_callback(server_ctx, tls_tmp_dh_cb);
602 if (*props->dh1024_param_file != 0)
603 tls_set_dh_from_file(props->dh1024_param_file, 1024);
604 if (*props->dh512_param_file != 0)
605 tls_set_dh_from_file(props->dh512_param_file, 512);
611 tls_set_eecdh_curve(server_ctx, props->eecdh_grade);
633 if (props->ask_ccert)
634 verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
635 SSL_CTX_set_verify(server_ctx, verify_flags,
636 tls_verify_certificate_callback);
638 SSL_CTX_set_client_CA_list(server_ctx,
639 SSL_load_client_CA_file(props->CAfile));
645 app_ctx = tls_alloc_app_context(server_ctx, log_mask);
647 if (cachable || ticketable || props->set_sessid) {
667 SSL_CTX_sess_set_cache_size(server_ctx, 1);
668 SSL_CTX_set_session_id_context(server_ctx,
669 (
void *) &server_session_id_context,
670 sizeof(server_session_id_context));
671 SSL_CTX_set_session_cache_mode(server_ctx,
672 SSL_SESS_CACHE_SERVER |
673 SSL_SESS_CACHE_NO_AUTO_CLEAR);
675 app_ctx->cache_type =
mystrdup(props->cache_type);
677 SSL_CTX_sess_set_get_cb(server_ctx, get_server_session_cb);
678 SSL_CTX_sess_set_new_cb(server_ctx, new_server_session_cb);
691 SSL_CTX_set_timeout(server_ctx, 2 * scache_timeout);
699 SSL_CTX_set_session_cache_mode(server_ctx, SSL_SESS_CACHE_OFF);
710 TLS_SESS_STATE *tls_server_start(
const TLS_SERVER_START_PROPS *props)
713 TLS_SESS_STATE *TLScontext;
714 const char *cipher_list;
715 TLS_APPL_STATE *app_ctx = props->ctx;
716 int log_mask = app_ctx->log_mask;
722 if (props->requirecert)
723 log_mask |= TLS_LOG_UNTRUSTED;
725 if (log_mask & TLS_LOG_VERBOSE)
726 msg_info(
"setting up TLS connection from %s", props->namaddr);
728 cipher_list = tls_set_ciphers(app_ctx,
"TLS", props->cipher_grade,
729 props->cipher_exclusions);
730 if (cipher_list == 0) {
731 msg_warn(
"%s: %s: aborting TLS session", props->namaddr,
735 if (log_mask & TLS_LOG_VERBOSE)
736 msg_info(
"%s: TLS cipher list \"%s\"", props->namaddr, cipher_list);
743 TLScontext = tls_alloc_sess_context(log_mask, props->namaddr);
744 TLScontext->cache_type = app_ctx->cache_type;
746 TLScontext->serverid =
mystrdup(props->serverid);
747 TLScontext->am_server = 1;
748 TLScontext->stream = props->stream;
749 TLScontext->mdalg = props->mdalg;
752 if ((TLScontext->con = (SSL *) SSL_new(app_ctx->ssl_ctx)) == 0) {
753 msg_warn(
"Could not allocate 'TLScontext->con' with SSL_new()");
755 tls_free_context(TLScontext);
758 if (!SSL_set_ex_data(TLScontext->con, TLScontext_index, TLScontext)) {
759 msg_warn(
"Could not set application data for 'TLScontext->con'");
761 tls_free_context(TLScontext);
764 #ifdef SSL_SECOP_PEER
766 if (props->requirecert)
767 SSL_set_security_level(TLScontext->con, 1);
782 SSL_set_accept_state(TLScontext->con);
787 if (SSL_set_fd(TLScontext->con, props->stream == 0 ? props->fd :
789 msg_info(
"SSL_set_fd error to %s", props->namaddr);
791 uncache_session(app_ctx->ssl_ctx, TLScontext);
792 tls_free_context(TLScontext);
805 if (log_mask & TLS_LOG_TLSPKTS)
806 BIO_set_callback(SSL_get_rbio(TLScontext->con), tls_bio_dump_cb);
812 if (props->stream == 0)
828 sts = tls_bio_accept(
vstream_fileno(props->stream), props->timeout,
831 if (ERR_peek_error() != 0) {
832 msg_info(
"SSL_accept error from %s: %d", props->namaddr, sts);
834 }
else if (errno != 0) {
835 msg_info(
"SSL_accept error from %s: %m", props->namaddr);
837 msg_info(
"SSL_accept error from %s: lost connection",
840 tls_free_context(TLScontext);
843 return (tls_server_post_accept(TLScontext));
848 TLS_SESS_STATE *tls_server_post_accept(TLS_SESS_STATE *TLScontext)
850 SSL_CIPHER_const SSL_CIPHER *cipher;
852 char buf[CCERT_BUFSIZ];
855 if ((TLScontext->log_mask & TLS_LOG_ALLPKTS) == 0)
856 BIO_set_callback(SSL_get_rbio(TLScontext->con), 0);
862 TLScontext->session_reused = SSL_session_reused(TLScontext->con);
863 if ((TLScontext->log_mask & TLS_LOG_CACHE) && TLScontext->session_reused)
864 msg_info(
"%s: Reusing old session%s", TLScontext->namaddr,
865 TLScontext->ticketed ?
" (RFC 5077 session ticket)" :
"");
871 peer = SSL_get_peer_certificate(TLScontext->con);
873 TLScontext->peer_status |= TLS_CERT_FLAG_PRESENT;
874 if (SSL_get_verify_result(TLScontext->con) == X509_V_OK)
875 TLScontext->peer_status |= TLS_CERT_FLAG_TRUSTED;
877 if (TLScontext->log_mask & TLS_LOG_VERBOSE) {
878 X509_NAME_oneline(X509_get_subject_name(peer),
881 X509_NAME_oneline(X509_get_issuer_name(peer),
885 TLScontext->peer_CN = tls_peer_CN(peer, TLScontext);
886 TLScontext->issuer_CN = tls_issuer_CN(peer, TLScontext);
887 TLScontext->peer_cert_fprint = tls_cert_fprint(peer, TLScontext->mdalg);
888 TLScontext->peer_pkey_fprint = tls_pkey_fprint(peer, TLScontext->mdalg);
890 if (TLScontext->log_mask & (TLS_LOG_VERBOSE | TLS_LOG_PEERCERT)) {
891 msg_info(
"%s: subject_CN=%s, issuer=%s, fingerprint=%s"
892 ", pkey_fingerprint=%s",
894 TLScontext->peer_CN, TLScontext->issuer_CN,
895 TLScontext->peer_cert_fprint,
896 TLScontext->peer_pkey_fprint);
906 if (!TLS_CERT_IS_TRUSTED(TLScontext)
907 && (TLScontext->log_mask & TLS_LOG_UNTRUSTED)) {
908 if (TLScontext->session_reused == 0)
909 tls_log_verify_error(TLScontext);
911 msg_info(
"%s: re-using session with untrusted certificate, "
912 "look for details earlier in the log",
913 TLScontext->namaddr);
917 TLScontext->issuer_CN =
mystrdup(
"");
918 TLScontext->peer_cert_fprint =
mystrdup(
"");
919 TLScontext->peer_pkey_fprint =
mystrdup(
"");
925 TLScontext->protocol = SSL_get_version(TLScontext->con);
926 cipher = SSL_get_current_cipher(TLScontext->con);
927 TLScontext->cipher_name = SSL_CIPHER_get_name(cipher);
928 TLScontext->cipher_usebits = SSL_CIPHER_get_bits(cipher,
929 &(TLScontext->cipher_algbits));
937 if (TLScontext->stream != 0)
938 tls_stream_start(TLScontext->stream, TLScontext);
943 if (TLScontext->log_mask & TLS_LOG_SUMMARY)
944 msg_info(
"%s TLS connection established from %s: %s with cipher %s "
945 "(%d/%d bits)", !TLS_CERT_IS_PRESENT(TLScontext) ?
"Anonymous"
946 : TLS_CERT_IS_TRUSTED(TLScontext) ?
"Trusted" :
"Untrusted",
947 TLScontext->namaddr, TLScontext->protocol, TLScontext->cipher_name,
948 TLScontext->cipher_usebits, TLScontext->cipher_algbits);
char * mystrdup(const char *str)
char * var_tls_tkt_cipher
#define TLS_TICKET_NAMELEN
NORETURN msg_panic(const char *fmt,...)
int tls_mgr_lookup(const char *, const char *, VSTRING *)
int tls_mgr_update(const char *, const char *, const char *, ssize_t)
unsigned char hmac[TLS_TICKET_MACLEN]
unsigned char name[TLS_TICKET_NAMELEN]
void msg_warn(const char *fmt,...)
VSTRING * vstring_alloc(ssize_t len)
int var_tls_daemon_rand_bytes
#define VAR_TLS_TKT_CIPHER
int tls_mgr_delete(const char *, const char *)
TLS_TICKET_KEY * tls_mgr_key(unsigned char *, int)
int tls_mgr_policy(const char *, int *, int *)
unsigned char bits[TLS_TICKET_KEYLEN]
bool var_tls_preempt_clist
int non_blocking(int, int)
VSTRING * vstring_free(VSTRING *vp)
#define TLS_TICKET_MACLEN
#define vstream_fileno(vp)
#define TLS_TICKET_KEYLEN
char * printable(char *string, int replacement)
void msg_info(const char *fmt,...)