1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is
9 based on a patch that was originally contributed by Steve Haslam. It was
10 adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
11 based on a patch contributed by Nikos Mavrogiannopoulos. Because these packages
12 are so very different, the functions for each are kept in separate files. The
13 relevant file is #included as required, after any any common functions.
15 No cryptographic code is included in Exim. All this module does is to call
16 functions from the OpenSSL or GNU TLS libraries. */
20 #include "transports/smtp.h"
22 #if !defined(DISABLE_TLS) && !defined(USE_OPENSSL) && !defined(USE_GNUTLS)
23 # error One of USE_OPENSSL or USE_GNUTLS must be defined for a TLS build
27 #if defined(MACRO_PREDEF) && !defined(DISABLE_TLS)
28 # include "macro_predef.h"
32 # include "tls-openssl.c"
38 /* This module is compiled only when it is specifically requested in the
39 build-time configuration. However, some compilers don't like compiling empty
40 modules, so keep them happy with a dummy when skipping the rest. Make it
41 reference itself to stop picky compilers complaining that it is unused, and put
42 in a dummy argument to stop even pickier compilers complaining about infinite
46 static void dummy(int x) { dummy(x-1); }
49 /* Static variables that are used for buffering data by both sets of
50 functions and the common functions below.
52 We're moving away from this; GnuTLS is already using a state, which
53 can switch, so we can do TLS callouts during ACLs. */
55 static const int ssl_xfer_buffer_size = 4096;
57 static uschar *ssl_xfer_buffer = NULL;
58 static int ssl_xfer_buffer_lwm = 0;
59 static int ssl_xfer_buffer_hwm = 0;
60 static int ssl_xfer_eof = FALSE;
61 static BOOL ssl_xfer_error = FALSE;
64 uschar *tls_channelbinding_b64 = NULL;
67 /*************************************************
68 * Expand string; give error on failure *
69 *************************************************/
71 /* If expansion is forced to fail, set the result NULL and return TRUE.
72 Other failures return FALSE. For a server, an SMTP response is given.
75 s the string to expand; if NULL just return TRUE
76 name name of string being expanded (for error)
77 result where to put the result
79 Returns: TRUE if OK; result may still be NULL after forced failure
83 expand_check(const uschar *s, const uschar *name, uschar **result, uschar ** errstr)
87 else if ( !(*result = expand_string(US s)) /* need to clean up const more */
88 && !f.expand_string_forcedfail
91 *errstr = US"Internal error";
92 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
93 expand_string_message);
100 /*************************************************
101 * Timezone environment flipping *
102 *************************************************/
107 uschar * old = US getenv("TZ");
108 (void) setenv("TZ", CCS tz, 1);
114 restore_tz(uschar * tz)
117 (void) setenv("TZ", CCS tz, 1);
119 (void) os_unsetenv(US"TZ");
123 /*************************************************
124 * Many functions are package-specific *
125 *************************************************/
128 # include "tls-gnu.c"
129 # include "tlscert-gnu.c"
130 # define ssl_xfer_buffer (state_server.xfer_buffer)
131 # define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
132 # define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
133 # define ssl_xfer_eof (state_server.xfer_eof)
134 # define ssl_xfer_error (state_server.xfer_error)
138 # include "tls-openssl.c"
139 # include "tlscert-openssl.c"
144 /*************************************************
145 * TLS version of ungetc *
146 *************************************************/
148 /* Puts a character back in the input buffer. Only ever
150 Only used by the server-side TLS.
155 Returns: the character
161 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
167 /*************************************************
168 * TLS version of feof *
169 *************************************************/
171 /* Tests for a previous EOF
172 Only used by the server-side TLS.
175 Returns: non-zero if the eof flag is set
181 return (int)ssl_xfer_eof;
186 /*************************************************
187 * TLS version of ferror *
188 *************************************************/
190 /* Tests for a previous read error, and returns with errno
191 restored to what it was when the error was detected.
192 Only used by the server-side TLS.
194 >>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
197 Returns: non-zero if the error flag is set
203 return (int)ssl_xfer_error;
207 /*************************************************
208 * TLS version of smtp_buffered *
209 *************************************************/
211 /* Tests for unused chars in the TLS input buffer.
212 Only used by the server-side TLS.
219 tls_smtp_buffered(void)
221 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
225 #endif /*DISABLE_TLS*/
228 tls_modify_variables(tls_support * dest_tsp)
230 modify_variable(US"tls_bits", &dest_tsp->bits);
231 modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
232 modify_variable(US"tls_cipher", &dest_tsp->cipher);
233 modify_variable(US"tls_peerdn", &dest_tsp->peerdn);
235 modify_variable(US"tls_sni", &dest_tsp->sni);
241 /************************************************
242 * TLS certificate name operations *
243 ************************************************/
245 /* Convert an rfc4514 DN to an exim comma-sep list.
246 Backslashed commas need to be replaced by doublecomma
247 for Exim's list quoting. We modify the given string
252 dn_to_list(uschar * dn)
254 for (uschar * cp = dn; *cp; cp++)
255 if (cp[0] == '\\' && cp[1] == ',')
260 /* Extract fields of a given type from an RFC4514-
261 format Distinguished Name. Return an Exim list.
262 NOTE: We modify the supplied dn string during operation.
265 dn Distinguished Name string
266 mod list containing optional output list-sep and
267 field selector match, comma-separated
269 allocated string with list of matching fields,
274 tls_field_from_dn(uschar * dn, const uschar * mod)
277 uschar outsep = '\n';
279 uschar * match = NULL;
281 gstring * list = NULL;
283 while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
285 match = ele; /* field tag to match */
287 outsep = ele[1]; /* nondefault output separator */
291 len = match ? Ustrlen(match) : -1;
292 while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
294 || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
296 list = string_append_listele(list, outsep, ele+len+1);
297 return string_from_gstring(list);
301 /* Compare a domain name with a possibly-wildcarded name. Wildcards
302 are restricted to a single one, as the first element of patterns
303 having at least three dot-separated elements. Case-independent.
304 Return TRUE for a match
307 is_name_match(const uschar * name, const uschar * pat)
310 return *pat == '*' /* possible wildcard match */
311 ? *++pat == '.' /* starts star, dot */
312 && !Ustrchr(++pat, '*') /* has no more stars */
313 && Ustrchr(pat, '.') /* and has another dot. */
314 && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
315 && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
316 : !Ustrchr(pat+1, '*')
317 && strcmpic(name, pat) == 0;
320 /* Compare a list of names with the dnsname elements
321 of the Subject Alternate Name, if any, and the
325 namelist names to compare
333 tls_is_name_for_cert(const uschar * namelist, void * cert)
335 uschar * altnames = tls_cert_subject_altname(cert, US"dns");
341 if ((altnames = tls_cert_subject_altname(cert, US"dns")))
344 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
346 const uschar * an = altnames;
347 while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
348 if (is_name_match(cmpname, certname))
353 else if ((subjdn = tls_cert_subject(cert, NULL)))
358 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
360 const uschar * sn = subjdn;
361 while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
362 if ( *certname++ == 'C'
363 && *certname++ == 'N'
364 && *certname++ == '='
365 && is_name_match(cmpname, certname)
375 /*************************************************
376 * Drop privs for checking TLS config *
377 *************************************************/
379 /* We want to validate TLS options during readconf, but do not want to be
380 root when we call into the TLS library, in case of library linkage errors
381 which cause segfaults; before this check, those were always done as the Exim
382 runtime user and it makes sense to continue with that.
384 Assumes: tls_require_ciphers has been set, if it will be
385 exim_user has been set, if it will be
386 exim_group has been set, if it will be
388 Returns: bool for "okay"; false will cause caller to immediately exit.
392 tls_dropprivs_validate_require_cipher(BOOL nowarn)
394 const uschar *errmsg;
397 void (*oldsignal)(int);
399 /* If TLS will never be used, no point checking ciphers */
401 if ( !tls_advertise_hosts
402 || !*tls_advertise_hosts
403 || Ustrcmp(tls_advertise_hosts, ":") == 0
406 else if (!nowarn && !tls_certificate)
407 log_write(0, LOG_MAIN,
408 "Warning: No server certificate defined; will use a selfsigned one.\n"
409 " Suggested action: either install a certificate or change tls_advertise_hosts option");
411 oldsignal = signal(SIGCHLD, SIG_DFL);
414 if ((pid = fork()) < 0)
415 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for TLS check");
419 /* in some modes, will have dropped privilege already */
421 exim_setugid(exim_uid, exim_gid, FALSE,
422 US"calling tls_validate_require_cipher");
424 if ((errmsg = tls_validate_require_cipher()))
425 log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
426 "tls_require_ciphers invalid: %s", errmsg);
428 exim_underbar_exit(0);
432 rc = waitpid(pid, &status, 0);
433 } while (rc < 0 && errno == EINTR);
436 debug_printf("tls_validate_require_cipher child %d ended: status=0x%x\n",
439 signal(SIGCHLD, oldsignal);
447 #endif /*!DISABLE_TLS*/
448 #endif /*!MACRO_PREDEF*/