Fix cert-try-verify when denied by event action
[exim.git] / src / src / tls.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
c4ceed07 5/* Copyright (c) University of Cambridge 1995 - 2012 */
059ec3d9
PH
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 Mavroyanopoulos. 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"
65867078 20#include "transports/smtp.h"
059ec3d9
PH
21
22/* This module is compiled only when it is specifically requested in the
23build-time configuration. However, some compilers don't like compiling empty
24modules, so keep them happy with a dummy when skipping the rest. Make it
25reference itself to stop picky compilers complaining that it is unused, and put
26in a dummy argument to stop even pickier compilers complaining about infinite
27loops. */
28
29#ifndef SUPPORT_TLS
30static void dummy(int x) { dummy(x-1); }
31#else
32
33/* Static variables that are used for buffering data by both sets of
4fe99a6c 34functions and the common functions below.
059ec3d9 35
4fe99a6c
PP
36We're moving away from this; GnuTLS is already using a state, which
37can switch, so we can do TLS callouts during ACLs. */
059ec3d9 38
17c76198 39static const int ssl_xfer_buffer_size = 4096;
4fe99a6c
PP
40#ifndef USE_GNUTLS
41static uschar *ssl_xfer_buffer = NULL;
059ec3d9
PH
42static int ssl_xfer_buffer_lwm = 0;
43static int ssl_xfer_buffer_hwm = 0;
44static int ssl_xfer_eof = 0;
45static int ssl_xfer_error = 0;
4fe99a6c 46#endif
059ec3d9 47
44bbabb5 48uschar *tls_channelbinding_b64 = NULL;
059ec3d9
PH
49
50
51/*************************************************
52* Expand string; give error on failure *
53*************************************************/
54
55/* If expansion is forced to fail, set the result NULL and return TRUE.
56Other failures return FALSE. For a server, an SMTP response is given.
57
58Arguments:
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
62
63Returns: TRUE if OK; result may still be NULL after forced failure
64*/
65
66static BOOL
17c76198 67expand_check(const uschar *s, const uschar *name, uschar **result)
059ec3d9
PH
68{
69if (s == NULL) *result = NULL; else
70 {
17c76198 71 *result = expand_string(US s); /* need to clean up const some more */
059ec3d9
PH
72 if (*result == NULL && !expand_string_forcedfail)
73 {
74 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
75 expand_string_message);
76 return FALSE;
77 }
78 }
79return TRUE;
80}
81
82
83/*************************************************
84* Many functions are package-specific *
85*************************************************/
86
87#ifdef USE_GNUTLS
88#include "tls-gnu.c"
9d1c15ef 89#include "tlscert-gnu.c"
4fe99a6c 90
389ca47a
JH
91#define ssl_xfer_buffer (state_server.xfer_buffer)
92#define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
93#define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
94#define ssl_xfer_eof (state_server.xfer_eof)
95#define ssl_xfer_error (state_server.xfer_error)
4fe99a6c 96
059ec3d9
PH
97#else
98#include "tls-openssl.c"
9d1c15ef 99#include "tlscert-openssl.c"
059ec3d9
PH
100#endif
101
102
103
104/*************************************************
105* TLS version of ungetc *
106*************************************************/
107
108/* Puts a character back in the input buffer. Only ever
109called once.
389ca47a 110Only used by the server-side TLS.
059ec3d9
PH
111
112Arguments:
113 ch the character
114
115Returns: the character
116*/
117
118int
119tls_ungetc(int ch)
120{
121ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
122return ch;
123}
124
125
126
127/*************************************************
128* TLS version of feof *
129*************************************************/
130
131/* Tests for a previous EOF
389ca47a 132Only used by the server-side TLS.
059ec3d9
PH
133
134Arguments: none
135Returns: non-zero if the eof flag is set
136*/
137
138int
139tls_feof(void)
140{
141return ssl_xfer_eof;
142}
143
144
145
146/*************************************************
147* TLS version of ferror *
148*************************************************/
149
150/* Tests for a previous read error, and returns with errno
151restored to what it was when the error was detected.
389ca47a 152Only used by the server-side TLS.
059ec3d9
PH
153
154>>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
155
156Arguments: none
157Returns: non-zero if the error flag is set
158*/
159
160int
161tls_ferror(void)
162{
163return ssl_xfer_error;
164}
165
58eb016e
PH
166
167/*************************************************
168* TLS version of smtp_buffered *
169*************************************************/
170
171/* Tests for unused chars in the TLS input buffer.
389ca47a 172Only used by the server-side TLS.
58eb016e
PH
173
174Arguments: none
175Returns: TRUE/FALSE
176*/
177
178BOOL
179tls_smtp_buffered(void)
180{
181return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
182}
183
184
059ec3d9
PH
185#endif /* SUPPORT_TLS */
186
35aba663
JH
187void
188tls_modify_variables(tls_support * dest_tsp)
189{
190modify_variable(US"tls_bits", &dest_tsp->bits);
191modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
192modify_variable(US"tls_cipher", &dest_tsp->cipher);
193modify_variable(US"tls_peerdn", &dest_tsp->peerdn);
194#if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
195modify_variable(US"tls_sni", &dest_tsp->sni);
196#endif
197}
198
417bfce4
JH
199
200#ifdef SUPPORT_TLS
812a6045
JH
201/************************************************
202* TLS certificate name operations *
203************************************************/
204
205/* Convert an rfc4514 DN to an exim comma-sep list.
206Backslashed commas need to be replaced by doublecomma
207for Exim's list quoting. We modify the given string
208inplace.
209*/
210
211static void
212dn_to_list(uschar * dn)
213{
214uschar * cp;
215for (cp = dn; *cp; cp++)
216 if (cp[0] == '\\' && cp[1] == ',')
217 *cp++ = ',';
218}
219
220
221/* Extract fields of a given type from an RFC4514-
222format Distinguished Name. Return an Exim list.
223NOTE: We modify the supplied dn string during operation.
224
225Arguments:
226 dn Distinguished Name string
227 mod string containing optional list-sep and
228 field selector match, comma-separated
229Return:
230 allocated string with list of matching fields,
231 field type stripped
232*/
233
234uschar *
235tls_field_from_dn(uschar * dn, uschar * mod)
236{
237int insep = ',';
238uschar outsep = '\n';
239uschar * ele;
240uschar * match = NULL;
241int len;
242uschar * list = NULL;
243
7437665e 244while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
812a6045
JH
245 if (ele[0] != '>')
246 match = ele; /* field tag to match */
247 else if (ele[1])
248 outsep = ele[1]; /* nondefault separator */
249
250dn_to_list(dn);
251insep = ',';
252len = Ustrlen(match);
7437665e 253while ((ele = string_nextinlist(&dn, &insep, NULL, 0)))
812a6045
JH
254 if (Ustrncmp(ele, match, len) == 0 && ele[len] == '=')
255 list = string_append_listele(list, outsep, ele+len+1);
256return list;
257}
258
259
417bfce4 260# ifdef EXPERIMENTAL_CERTNAMES
e51c7be2
JH
261/* Compare a domain name with a possibly-wildcarded name. Wildcards
262are restricted to a single one, as the first element of patterns
263having at least three dot-separated elements. Case-independent.
264Return TRUE for a match
265*/
266static BOOL
267is_name_match(const uschar * name, const uschar * pat)
268{
269uschar * cp;
270return *pat == '*' /* possible wildcard match */
271 ? *++pat == '.' /* starts star, dot */
272 && !Ustrchr(++pat, '*') /* has no more stars */
273 && Ustrchr(pat, '.') /* and has another dot. */
274 && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
275 && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
276 : !Ustrchr(pat+1, '*')
277 && strcmpic(name, pat) == 0;
278}
279
280/* Compare a list of names with the dnsname elements
281of the Subject Alternate Name, if any, and the
282Subject otherwise.
283
284Arguments:
285 namelist names to compare
286 cert certificate
287
288Returns:
289 TRUE/FALSE
290*/
291
292BOOL
293tls_is_name_for_cert(uschar * namelist, void * cert)
294{
295uschar * altnames = tls_cert_subject_altname(cert, US"dns");
296uschar * subjdn;
297uschar * certname;
298int cmp_sep = 0;
299uschar * cmpname;
300
301if ((altnames = tls_cert_subject_altname(cert, US"dns")))
302 {
303 int alt_sep = '\n';
7437665e 304 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
e51c7be2
JH
305 {
306 uschar * an = altnames;
7437665e 307 while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
e51c7be2
JH
308 if (is_name_match(cmpname, certname))
309 return TRUE;
310 }
311 }
312
313else if ((subjdn = tls_cert_subject(cert, NULL)))
314 {
315 int sn_sep = ',';
e51c7be2
JH
316
317 dn_to_list(subjdn);
67791ce4 318 while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
e51c7be2
JH
319 {
320 uschar * sn = subjdn;
7437665e 321 while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
e51c7be2
JH
322 if ( *certname++ == 'C'
323 && *certname++ == 'N'
324 && *certname++ == '='
325 && is_name_match(cmpname, certname)
326 )
327 return TRUE;
328 }
329 }
330return FALSE;
331}
417bfce4 332# endif /*EXPERIMENTAL_CERTNAMES*/
6c1c3d1d 333#endif /*SUPPORT_TLS*/
812a6045
JH
334
335/* vi: aw ai sw=2
336*/
059ec3d9 337/* End of tls.c */