1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
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 Mavroyanopoulos. 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 /* This module is compiled only when it is specifically requested in the
23 build-time configuration. However, some compilers don't like compiling empty
24 modules, so keep them happy with a dummy when skipping the rest. Make it
25 reference itself to stop picky compilers complaining that it is unused, and put
26 in a dummy argument to stop even pickier compilers complaining about infinite
30 static void dummy(int x) { dummy(x-1); }
33 /* Static variables that are used for buffering data by both sets of
34 functions and the common functions below.
36 We're moving away from this; GnuTLS is already using a state, which
37 can switch, so we can do TLS callouts during ACLs. */
39 static const int ssl_xfer_buffer_size = 4096;
41 static uschar *ssl_xfer_buffer = NULL;
42 static int ssl_xfer_buffer_lwm = 0;
43 static int ssl_xfer_buffer_hwm = 0;
44 static int ssl_xfer_eof = 0;
45 static int ssl_xfer_error = 0;
48 uschar *tls_channelbinding_b64 = NULL;
51 /*************************************************
52 * Expand string; give error on failure *
53 *************************************************/
55 /* If expansion is forced to fail, set the result NULL and return TRUE.
56 Other failures return FALSE. For a server, an SMTP response is given.
59 s the string to expand; if NULL just return TRUE
60 name name of string being expanded (for error)
61 result where to put the result
63 Returns: TRUE if OK; result may still be NULL after forced failure
67 expand_check(const uschar *s, const uschar *name, uschar **result)
69 if (s == NULL) *result = NULL; else
71 *result = expand_string(US s); /* need to clean up const some more */
72 if (*result == NULL && !expand_string_forcedfail)
74 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
75 expand_string_message);
83 /*************************************************
84 * Timezone environment flipping *
85 *************************************************/
87 #ifdef MISSING_UNSETENV_3
94 uschar * old = US getenv("TZ");
95 (void) setenv("TZ", CCS tz, 1);
100 restore_tz(uschar * tz)
103 (void) setenv("TZ", CCS tz, 1);
105 (void) unsetenv("TZ");
109 /*************************************************
110 * Many functions are package-specific *
111 *************************************************/
114 # include "tls-gnu.c"
115 # include "tlscert-gnu.c"
117 # define ssl_xfer_buffer (state_server.xfer_buffer)
118 # define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
119 # define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
120 # define ssl_xfer_eof (state_server.xfer_eof)
121 # define ssl_xfer_error (state_server.xfer_error)
124 # include "tls-openssl.c"
125 # include "tlscert-openssl.c"
130 /*************************************************
131 * TLS version of ungetc *
132 *************************************************/
134 /* Puts a character back in the input buffer. Only ever
136 Only used by the server-side TLS.
141 Returns: the character
147 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
153 /*************************************************
154 * TLS version of feof *
155 *************************************************/
157 /* Tests for a previous EOF
158 Only used by the server-side TLS.
161 Returns: non-zero if the eof flag is set
172 /*************************************************
173 * TLS version of ferror *
174 *************************************************/
176 /* Tests for a previous read error, and returns with errno
177 restored to what it was when the error was detected.
178 Only used by the server-side TLS.
180 >>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
183 Returns: non-zero if the error flag is set
189 return ssl_xfer_error;
193 /*************************************************
194 * TLS version of smtp_buffered *
195 *************************************************/
197 /* Tests for unused chars in the TLS input buffer.
198 Only used by the server-side TLS.
205 tls_smtp_buffered(void)
207 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
211 #endif /* SUPPORT_TLS */
214 tls_modify_variables(tls_support * dest_tsp)
216 modify_variable(US"tls_bits", &dest_tsp->bits);
217 modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
218 modify_variable(US"tls_cipher", &dest_tsp->cipher);
219 modify_variable(US"tls_peerdn", &dest_tsp->peerdn);
220 #if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
221 modify_variable(US"tls_sni", &dest_tsp->sni);
227 /************************************************
228 * TLS certificate name operations *
229 ************************************************/
231 /* Convert an rfc4514 DN to an exim comma-sep list.
232 Backslashed commas need to be replaced by doublecomma
233 for Exim's list quoting. We modify the given string
238 dn_to_list(uschar * dn)
241 for (cp = dn; *cp; cp++)
242 if (cp[0] == '\\' && cp[1] == ',')
247 /* Extract fields of a given type from an RFC4514-
248 format Distinguished Name. Return an Exim list.
249 NOTE: We modify the supplied dn string during operation.
252 dn Distinguished Name string
253 mod list containing optional output list-sep and
254 field selector match, comma-separated
256 allocated string with list of matching fields,
261 tls_field_from_dn(uschar * dn, const uschar * mod)
264 uschar outsep = '\n';
266 uschar * match = NULL;
268 uschar * list = NULL;
270 while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
272 match = ele; /* field tag to match */
274 outsep = ele[1]; /* nondefault output separator */
278 len = match ? Ustrlen(match) : -1;
279 while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
281 || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
283 list = string_append_listele(list, outsep, ele+len+1);
288 /* Compare a domain name with a possibly-wildcarded name. Wildcards
289 are restricted to a single one, as the first element of patterns
290 having at least three dot-separated elements. Case-independent.
291 Return TRUE for a match
294 is_name_match(const uschar * name, const uschar * pat)
297 return *pat == '*' /* possible wildcard match */
298 ? *++pat == '.' /* starts star, dot */
299 && !Ustrchr(++pat, '*') /* has no more stars */
300 && Ustrchr(pat, '.') /* and has another dot. */
301 && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
302 && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
303 : !Ustrchr(pat+1, '*')
304 && strcmpic(name, pat) == 0;
307 /* Compare a list of names with the dnsname elements
308 of the Subject Alternate Name, if any, and the
312 namelist names to compare
320 tls_is_name_for_cert(const uschar * namelist, void * cert)
322 uschar * altnames = tls_cert_subject_altname(cert, US"dns");
328 if ((altnames = tls_cert_subject_altname(cert, US"dns")))
331 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
333 const uschar * an = altnames;
334 while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
335 if (is_name_match(cmpname, certname))
340 else if ((subjdn = tls_cert_subject(cert, NULL)))
345 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
347 const uschar * sn = subjdn;
348 while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
349 if ( *certname++ == 'C'
350 && *certname++ == 'N'
351 && *certname++ == '='
352 && is_name_match(cmpname, certname)
359 #endif /*SUPPORT_TLS*/