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(MACRO_PREDEF) && defined(SUPPORT_TLS)
24 # include "macro_predef.h"
25 # include "tls-openssl.c"
31 /* This module is compiled only when it is specifically requested in the
32 build-time configuration. However, some compilers don't like compiling empty
33 modules, so keep them happy with a dummy when skipping the rest. Make it
34 reference itself to stop picky compilers complaining that it is unused, and put
35 in a dummy argument to stop even pickier compilers complaining about infinite
39 static void dummy(int x) { dummy(x-1); }
42 /* Static variables that are used for buffering data by both sets of
43 functions and the common functions below.
45 We're moving away from this; GnuTLS is already using a state, which
46 can switch, so we can do TLS callouts during ACLs. */
48 static const int ssl_xfer_buffer_size = 4096;
50 static uschar *ssl_xfer_buffer = NULL;
51 static int ssl_xfer_buffer_lwm = 0;
52 static int ssl_xfer_buffer_hwm = 0;
53 static int ssl_xfer_eof = FALSE;
54 static BOOL ssl_xfer_error = FALSE;
57 uschar *tls_channelbinding_b64 = NULL;
60 /*************************************************
61 * Expand string; give error on failure *
62 *************************************************/
64 /* If expansion is forced to fail, set the result NULL and return TRUE.
65 Other failures return FALSE. For a server, an SMTP response is given.
68 s the string to expand; if NULL just return TRUE
69 name name of string being expanded (for error)
70 result where to put the result
72 Returns: TRUE if OK; result may still be NULL after forced failure
76 expand_check(const uschar *s, const uschar *name, uschar **result, uschar ** errstr)
80 else if ( !(*result = expand_string(US s)) /* need to clean up const more */
81 && !f.expand_string_forcedfail
84 *errstr = US"Internal error";
85 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
86 expand_string_message);
93 /*************************************************
94 * Timezone environment flipping *
95 *************************************************/
100 uschar * old = US getenv("TZ");
101 (void) setenv("TZ", CCS tz, 1);
107 restore_tz(uschar * tz)
110 (void) setenv("TZ", CCS tz, 1);
112 (void) os_unsetenv(US"TZ");
116 /*************************************************
117 * Many functions are package-specific *
118 *************************************************/
121 # include "tls-gnu.c"
122 # include "tlscert-gnu.c"
124 # define ssl_xfer_buffer (state_server.xfer_buffer)
125 # define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
126 # define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
127 # define ssl_xfer_eof (state_server.xfer_eof)
128 # define ssl_xfer_error (state_server.xfer_error)
131 # include "tls-openssl.c"
132 # include "tlscert-openssl.c"
137 /*************************************************
138 * TLS version of ungetc *
139 *************************************************/
141 /* Puts a character back in the input buffer. Only ever
143 Only used by the server-side TLS.
148 Returns: the character
154 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
160 /*************************************************
161 * TLS version of feof *
162 *************************************************/
164 /* Tests for a previous EOF
165 Only used by the server-side TLS.
168 Returns: non-zero if the eof flag is set
174 return (int)ssl_xfer_eof;
179 /*************************************************
180 * TLS version of ferror *
181 *************************************************/
183 /* Tests for a previous read error, and returns with errno
184 restored to what it was when the error was detected.
185 Only used by the server-side TLS.
187 >>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
190 Returns: non-zero if the error flag is set
196 return (int)ssl_xfer_error;
200 /*************************************************
201 * TLS version of smtp_buffered *
202 *************************************************/
204 /* Tests for unused chars in the TLS input buffer.
205 Only used by the server-side TLS.
212 tls_smtp_buffered(void)
214 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
218 #endif /* SUPPORT_TLS */
221 tls_modify_variables(tls_support * dest_tsp)
223 modify_variable(US"tls_bits", &dest_tsp->bits);
224 modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
225 modify_variable(US"tls_cipher", &dest_tsp->cipher);
226 modify_variable(US"tls_peerdn", &dest_tsp->peerdn);
227 #if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
228 modify_variable(US"tls_sni", &dest_tsp->sni);
234 /************************************************
235 * TLS certificate name operations *
236 ************************************************/
238 /* Convert an rfc4514 DN to an exim comma-sep list.
239 Backslashed commas need to be replaced by doublecomma
240 for Exim's list quoting. We modify the given string
245 dn_to_list(uschar * dn)
248 for (cp = dn; *cp; cp++)
249 if (cp[0] == '\\' && cp[1] == ',')
254 /* Extract fields of a given type from an RFC4514-
255 format Distinguished Name. Return an Exim list.
256 NOTE: We modify the supplied dn string during operation.
259 dn Distinguished Name string
260 mod list containing optional output list-sep and
261 field selector match, comma-separated
263 allocated string with list of matching fields,
268 tls_field_from_dn(uschar * dn, const uschar * mod)
271 uschar outsep = '\n';
273 uschar * match = NULL;
275 gstring * list = NULL;
277 while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
279 match = ele; /* field tag to match */
281 outsep = ele[1]; /* nondefault output separator */
285 len = match ? Ustrlen(match) : -1;
286 while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
288 || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
290 list = string_append_listele(list, outsep, ele+len+1);
291 return string_from_gstring(list);
295 /* Compare a domain name with a possibly-wildcarded name. Wildcards
296 are restricted to a single one, as the first element of patterns
297 having at least three dot-separated elements. Case-independent.
298 Return TRUE for a match
301 is_name_match(const uschar * name, const uschar * pat)
304 return *pat == '*' /* possible wildcard match */
305 ? *++pat == '.' /* starts star, dot */
306 && !Ustrchr(++pat, '*') /* has no more stars */
307 && Ustrchr(pat, '.') /* and has another dot. */
308 && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
309 && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
310 : !Ustrchr(pat+1, '*')
311 && strcmpic(name, pat) == 0;
314 /* Compare a list of names with the dnsname elements
315 of the Subject Alternate Name, if any, and the
319 namelist names to compare
327 tls_is_name_for_cert(const uschar * namelist, void * cert)
329 uschar * altnames = tls_cert_subject_altname(cert, US"dns");
335 if ((altnames = tls_cert_subject_altname(cert, US"dns")))
338 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
340 const uschar * an = altnames;
341 while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
342 if (is_name_match(cmpname, certname))
347 else if ((subjdn = tls_cert_subject(cert, NULL)))
352 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
354 const uschar * sn = subjdn;
355 while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
356 if ( *certname++ == 'C'
357 && *certname++ == 'N'
358 && *certname++ == '='
359 && is_name_match(cmpname, certname)
366 #endif /*SUPPORT_TLS*/
367 #endif /*!MACRO_PREDEF*/