Dummies for Solaris build
[exim.git] / src / src / tls.c
... / ...
CommitLineData
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) University of Cambridge 1995 - 2018 */
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is
9based on a patch that was originally contributed by Steve Haslam. It was
10adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
11based on a patch contributed by Nikos Mavrogiannopoulos. Because these packages
12are so very different, the functions for each are kept in separate files. The
13relevant file is #included as required, after any any common functions.
14
15No cryptographic code is included in Exim. All this module does is to call
16functions from the OpenSSL or GNU TLS libraries. */
17
18
19#include "exim.h"
20#include "transports/smtp.h"
21
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
24#endif
25
26
27#if defined(MACRO_PREDEF) && !defined(DISABLE_TLS)
28# include "macro_predef.h"
29# ifdef USE_GNUTLS
30# include "tls-gnu.c"
31# else
32# include "tls-openssl.c"
33# endif
34#endif
35
36#ifndef MACRO_PREDEF
37
38/* This module is compiled only when it is specifically requested in the
39build-time configuration. However, some compilers don't like compiling empty
40modules, so keep them happy with a dummy when skipping the rest. Make it
41reference itself to stop picky compilers complaining that it is unused, and put
42in a dummy argument to stop even pickier compilers complaining about infinite
43loops. */
44
45#ifdef DISABLE_TLS
46static void dummy(int x) { dummy(x-1); }
47#else
48
49/* Static variables that are used for buffering data by both sets of
50functions and the common functions below.
51
52We're moving away from this; GnuTLS is already using a state, which
53can switch, so we can do TLS callouts during ACLs. */
54
55static const int ssl_xfer_buffer_size = 4096;
56#ifdef USE_OPENSSL
57static uschar *ssl_xfer_buffer = NULL;
58static int ssl_xfer_buffer_lwm = 0;
59static int ssl_xfer_buffer_hwm = 0;
60static int ssl_xfer_eof = FALSE;
61static BOOL ssl_xfer_error = FALSE;
62#endif
63
64uschar *tls_channelbinding_b64 = NULL;
65
66
67/*************************************************
68* Expand string; give error on failure *
69*************************************************/
70
71/* If expansion is forced to fail, set the result NULL and return TRUE.
72Other failures return FALSE. For a server, an SMTP response is given.
73
74Arguments:
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
78
79Returns: TRUE if OK; result may still be NULL after forced failure
80*/
81
82static BOOL
83expand_check(const uschar *s, const uschar *name, uschar **result, uschar ** errstr)
84{
85if (!s)
86 *result = NULL;
87else if ( !(*result = expand_string(US s)) /* need to clean up const more */
88 && !f.expand_string_forcedfail
89 )
90 {
91 *errstr = US"Internal error";
92 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
93 expand_string_message);
94 return FALSE;
95 }
96return TRUE;
97}
98
99
100/*************************************************
101* Timezone environment flipping *
102*************************************************/
103
104static uschar *
105to_tz(uschar * tz)
106{
107uschar * old = US getenv("TZ");
108(void) setenv("TZ", CCS tz, 1);
109tzset();
110return old;
111}
112
113static void
114restore_tz(uschar * tz)
115{
116if (tz)
117 (void) setenv("TZ", CCS tz, 1);
118else
119 (void) os_unsetenv(US"TZ");
120tzset();
121}
122
123/*************************************************
124* Many functions are package-specific *
125*************************************************/
126
127#ifdef USE_GNUTLS
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)
135#endif
136
137#ifdef USE_OPENSSL
138# include "tls-openssl.c"
139# include "tlscert-openssl.c"
140#endif
141
142
143
144/*************************************************
145* TLS version of ungetc *
146*************************************************/
147
148/* Puts a character back in the input buffer. Only ever
149called once.
150Only used by the server-side TLS.
151
152Arguments:
153 ch the character
154
155Returns: the character
156*/
157
158int
159tls_ungetc(int ch)
160{
161ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
162return ch;
163}
164
165
166
167/*************************************************
168* TLS version of feof *
169*************************************************/
170
171/* Tests for a previous EOF
172Only used by the server-side TLS.
173
174Arguments: none
175Returns: non-zero if the eof flag is set
176*/
177
178int
179tls_feof(void)
180{
181return (int)ssl_xfer_eof;
182}
183
184
185
186/*************************************************
187* TLS version of ferror *
188*************************************************/
189
190/* Tests for a previous read error, and returns with errno
191restored to what it was when the error was detected.
192Only used by the server-side TLS.
193
194>>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
195
196Arguments: none
197Returns: non-zero if the error flag is set
198*/
199
200int
201tls_ferror(void)
202{
203return (int)ssl_xfer_error;
204}
205
206
207/*************************************************
208* TLS version of smtp_buffered *
209*************************************************/
210
211/* Tests for unused chars in the TLS input buffer.
212Only used by the server-side TLS.
213
214Arguments: none
215Returns: TRUE/FALSE
216*/
217
218BOOL
219tls_smtp_buffered(void)
220{
221return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
222}
223
224
225#endif /*DISABLE_TLS*/
226
227void
228tls_modify_variables(tls_support * dest_tsp)
229{
230modify_variable(US"tls_bits", &dest_tsp->bits);
231modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
232modify_variable(US"tls_cipher", &dest_tsp->cipher);
233modify_variable(US"tls_peerdn", &dest_tsp->peerdn);
234#ifdef USE_OPENSSL
235modify_variable(US"tls_sni", &dest_tsp->sni);
236#endif
237}
238
239
240#ifndef DISABLE_TLS
241/************************************************
242* TLS certificate name operations *
243************************************************/
244
245/* Convert an rfc4514 DN to an exim comma-sep list.
246Backslashed commas need to be replaced by doublecomma
247for Exim's list quoting. We modify the given string
248inplace.
249*/
250
251static void
252dn_to_list(uschar * dn)
253{
254for (uschar * cp = dn; *cp; cp++)
255 if (cp[0] == '\\' && cp[1] == ',')
256 *cp++ = ',';
257}
258
259
260/* Extract fields of a given type from an RFC4514-
261format Distinguished Name. Return an Exim list.
262NOTE: We modify the supplied dn string during operation.
263
264Arguments:
265 dn Distinguished Name string
266 mod list containing optional output list-sep and
267 field selector match, comma-separated
268Return:
269 allocated string with list of matching fields,
270 field type stripped
271*/
272
273uschar *
274tls_field_from_dn(uschar * dn, const uschar * mod)
275{
276int insep = ',';
277uschar outsep = '\n';
278uschar * ele;
279uschar * match = NULL;
280int len;
281gstring * list = NULL;
282
283while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
284 if (ele[0] != '>')
285 match = ele; /* field tag to match */
286 else if (ele[1])
287 outsep = ele[1]; /* nondefault output separator */
288
289dn_to_list(dn);
290insep = ',';
291len = match ? Ustrlen(match) : -1;
292while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
293 if ( !match
294 || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
295 )
296 list = string_append_listele(list, outsep, ele+len+1);
297return string_from_gstring(list);
298}
299
300
301/* Compare a domain name with a possibly-wildcarded name. Wildcards
302are restricted to a single one, as the first element of patterns
303having at least three dot-separated elements. Case-independent.
304Return TRUE for a match
305*/
306static BOOL
307is_name_match(const uschar * name, const uschar * pat)
308{
309uschar * cp;
310return *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;
318}
319
320/* Compare a list of names with the dnsname elements
321of the Subject Alternate Name, if any, and the
322Subject otherwise.
323
324Arguments:
325 namelist names to compare
326 cert certificate
327
328Returns:
329 TRUE/FALSE
330*/
331
332BOOL
333tls_is_name_for_cert(const uschar * namelist, void * cert)
334{
335uschar * altnames = tls_cert_subject_altname(cert, US"dns");
336uschar * subjdn;
337uschar * certname;
338int cmp_sep = 0;
339uschar * cmpname;
340
341if ((altnames = tls_cert_subject_altname(cert, US"dns")))
342 {
343 int alt_sep = '\n';
344 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
345 {
346 const uschar * an = altnames;
347 while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
348 if (is_name_match(cmpname, certname))
349 return TRUE;
350 }
351 }
352
353else if ((subjdn = tls_cert_subject(cert, NULL)))
354 {
355 int sn_sep = ',';
356
357 dn_to_list(subjdn);
358 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
359 {
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)
366 )
367 return TRUE;
368 }
369 }
370return FALSE;
371}
372#endif /*!DISABLE_TLS*/
373#endif /*!MACRO_PREDEF*/
374
375/* vi: aw ai sw=2
376*/
377/* End of tls.c */