Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
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 | |
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 | |
6aa6fc9c | 11 | based on a patch contributed by Nikos Mavrogiannopoulos. Because these packages |
059ec3d9 PH |
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. | |
14 | ||
15 | No cryptographic code is included in Exim. All this module does is to call | |
16 | functions from the OpenSSL or GNU TLS libraries. */ | |
17 | ||
18 | ||
19 | #include "exim.h" | |
65867078 | 20 | #include "transports/smtp.h" |
059ec3d9 | 21 | |
8442641e JH |
22 | #if defined(MACRO_PREDEF) && defined(SUPPORT_TLS) |
23 | # ifndef USE_GNUTLS | |
24 | # include "macro_predef.h" | |
25 | # include "tls-openssl.c" | |
26 | # endif | |
27 | #endif | |
28 | ||
29 | #ifndef MACRO_PREDEF | |
30 | ||
059ec3d9 PH |
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 | |
36 | loops. */ | |
37 | ||
38 | #ifndef SUPPORT_TLS | |
39 | static void dummy(int x) { dummy(x-1); } | |
40 | #else | |
41 | ||
42 | /* Static variables that are used for buffering data by both sets of | |
4fe99a6c | 43 | functions and the common functions below. |
059ec3d9 | 44 | |
4fe99a6c PP |
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. */ | |
059ec3d9 | 47 | |
17c76198 | 48 | static const int ssl_xfer_buffer_size = 4096; |
4fe99a6c PP |
49 | #ifndef USE_GNUTLS |
50 | static uschar *ssl_xfer_buffer = NULL; | |
059ec3d9 PH |
51 | static int ssl_xfer_buffer_lwm = 0; |
52 | static int ssl_xfer_buffer_hwm = 0; | |
8b77d27a JH |
53 | static int ssl_xfer_eof = FALSE; |
54 | static BOOL ssl_xfer_error = FALSE; | |
4fe99a6c | 55 | #endif |
059ec3d9 | 56 | |
44bbabb5 | 57 | uschar *tls_channelbinding_b64 = NULL; |
059ec3d9 PH |
58 | |
59 | ||
60 | /************************************************* | |
61 | * Expand string; give error on failure * | |
62 | *************************************************/ | |
63 | ||
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. | |
66 | ||
67 | Arguments: | |
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 | |
71 | ||
72 | Returns: TRUE if OK; result may still be NULL after forced failure | |
73 | */ | |
74 | ||
75 | static BOOL | |
cf0c6164 | 76 | expand_check(const uschar *s, const uschar *name, uschar **result, uschar ** errstr) |
059ec3d9 | 77 | { |
cf0c6164 JH |
78 | if (!s) |
79 | *result = NULL; | |
80 | else if ( !(*result = expand_string(US s)) /* need to clean up const more */ | |
8768d548 | 81 | && !f.expand_string_forcedfail |
cf0c6164 | 82 | ) |
059ec3d9 | 83 | { |
cf0c6164 JH |
84 | *errstr = US"Internal error"; |
85 | log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name, | |
86 | expand_string_message); | |
87 | return FALSE; | |
059ec3d9 PH |
88 | } |
89 | return TRUE; | |
90 | } | |
91 | ||
92 | ||
e9477a08 JH |
93 | /************************************************* |
94 | * Timezone environment flipping * | |
95 | *************************************************/ | |
96 | ||
97 | static uschar * | |
98 | to_tz(uschar * tz) | |
99 | { | |
dfe7d917 JH |
100 | uschar * old = US getenv("TZ"); |
101 | (void) setenv("TZ", CCS tz, 1); | |
271019bd | 102 | tzset(); |
dfe7d917 | 103 | return old; |
e9477a08 | 104 | } |
dfe7d917 | 105 | |
e9477a08 JH |
106 | static void |
107 | restore_tz(uschar * tz) | |
108 | { | |
dfe7d917 JH |
109 | if (tz) |
110 | (void) setenv("TZ", CCS tz, 1); | |
111 | else | |
93a680e4 | 112 | (void) os_unsetenv(US"TZ"); |
271019bd | 113 | tzset(); |
e9477a08 JH |
114 | } |
115 | ||
059ec3d9 PH |
116 | /************************************************* |
117 | * Many functions are package-specific * | |
118 | *************************************************/ | |
119 | ||
120 | #ifdef USE_GNUTLS | |
27f19eb4 JH |
121 | # include "tls-gnu.c" |
122 | # include "tlscert-gnu.c" | |
4fe99a6c | 123 | |
27f19eb4 JH |
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) | |
4fe99a6c | 129 | |
059ec3d9 | 130 | #else |
27f19eb4 JH |
131 | # include "tls-openssl.c" |
132 | # include "tlscert-openssl.c" | |
059ec3d9 PH |
133 | #endif |
134 | ||
135 | ||
136 | ||
137 | /************************************************* | |
138 | * TLS version of ungetc * | |
139 | *************************************************/ | |
140 | ||
141 | /* Puts a character back in the input buffer. Only ever | |
142 | called once. | |
389ca47a | 143 | Only used by the server-side TLS. |
059ec3d9 PH |
144 | |
145 | Arguments: | |
146 | ch the character | |
147 | ||
148 | Returns: the character | |
149 | */ | |
150 | ||
151 | int | |
152 | tls_ungetc(int ch) | |
153 | { | |
154 | ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch; | |
155 | return ch; | |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | /************************************************* | |
161 | * TLS version of feof * | |
162 | *************************************************/ | |
163 | ||
164 | /* Tests for a previous EOF | |
389ca47a | 165 | Only used by the server-side TLS. |
059ec3d9 PH |
166 | |
167 | Arguments: none | |
168 | Returns: non-zero if the eof flag is set | |
169 | */ | |
170 | ||
171 | int | |
172 | tls_feof(void) | |
173 | { | |
8b77d27a | 174 | return (int)ssl_xfer_eof; |
059ec3d9 PH |
175 | } |
176 | ||
177 | ||
178 | ||
179 | /************************************************* | |
180 | * TLS version of ferror * | |
181 | *************************************************/ | |
182 | ||
183 | /* Tests for a previous read error, and returns with errno | |
184 | restored to what it was when the error was detected. | |
389ca47a | 185 | Only used by the server-side TLS. |
059ec3d9 PH |
186 | |
187 | >>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>> | |
188 | ||
189 | Arguments: none | |
190 | Returns: non-zero if the error flag is set | |
191 | */ | |
192 | ||
193 | int | |
194 | tls_ferror(void) | |
195 | { | |
8b77d27a | 196 | return (int)ssl_xfer_error; |
059ec3d9 PH |
197 | } |
198 | ||
58eb016e PH |
199 | |
200 | /************************************************* | |
201 | * TLS version of smtp_buffered * | |
202 | *************************************************/ | |
203 | ||
204 | /* Tests for unused chars in the TLS input buffer. | |
389ca47a | 205 | Only used by the server-side TLS. |
58eb016e PH |
206 | |
207 | Arguments: none | |
208 | Returns: TRUE/FALSE | |
209 | */ | |
210 | ||
211 | BOOL | |
212 | tls_smtp_buffered(void) | |
213 | { | |
214 | return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm; | |
215 | } | |
216 | ||
217 | ||
059ec3d9 PH |
218 | #endif /* SUPPORT_TLS */ |
219 | ||
35aba663 JH |
220 | void |
221 | tls_modify_variables(tls_support * dest_tsp) | |
222 | { | |
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); | |
229 | #endif | |
230 | } | |
231 | ||
417bfce4 JH |
232 | |
233 | #ifdef SUPPORT_TLS | |
812a6045 JH |
234 | /************************************************ |
235 | * TLS certificate name operations * | |
236 | ************************************************/ | |
237 | ||
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 | |
241 | inplace. | |
242 | */ | |
243 | ||
244 | static void | |
245 | dn_to_list(uschar * dn) | |
246 | { | |
d7978c0f | 247 | for (uschar * cp = dn; *cp; cp++) |
812a6045 JH |
248 | if (cp[0] == '\\' && cp[1] == ',') |
249 | *cp++ = ','; | |
250 | } | |
251 | ||
252 | ||
253 | /* Extract fields of a given type from an RFC4514- | |
254 | format Distinguished Name. Return an Exim list. | |
255 | NOTE: We modify the supplied dn string during operation. | |
256 | ||
257 | Arguments: | |
258 | dn Distinguished Name string | |
bfbad1dd | 259 | mod list containing optional output list-sep and |
812a6045 JH |
260 | field selector match, comma-separated |
261 | Return: | |
262 | allocated string with list of matching fields, | |
263 | field type stripped | |
264 | */ | |
265 | ||
266 | uschar * | |
55414b25 | 267 | tls_field_from_dn(uschar * dn, const uschar * mod) |
812a6045 JH |
268 | { |
269 | int insep = ','; | |
270 | uschar outsep = '\n'; | |
271 | uschar * ele; | |
272 | uschar * match = NULL; | |
273 | int len; | |
acec9514 | 274 | gstring * list = NULL; |
812a6045 | 275 | |
7437665e | 276 | while ((ele = string_nextinlist(&mod, &insep, NULL, 0))) |
812a6045 JH |
277 | if (ele[0] != '>') |
278 | match = ele; /* field tag to match */ | |
279 | else if (ele[1]) | |
bfbad1dd | 280 | outsep = ele[1]; /* nondefault output separator */ |
812a6045 JH |
281 | |
282 | dn_to_list(dn); | |
283 | insep = ','; | |
bfbad1dd | 284 | len = match ? Ustrlen(match) : -1; |
55414b25 | 285 | while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0))) |
bfbad1dd JH |
286 | if ( !match |
287 | || Ustrncmp(ele, match, len) == 0 && ele[len] == '=' | |
288 | ) | |
acec9514 JH |
289 | list = string_append_listele(list, outsep, ele+len+1); |
290 | return string_from_gstring(list); | |
812a6045 JH |
291 | } |
292 | ||
293 | ||
e51c7be2 JH |
294 | /* Compare a domain name with a possibly-wildcarded name. Wildcards |
295 | are restricted to a single one, as the first element of patterns | |
296 | having at least three dot-separated elements. Case-independent. | |
297 | Return TRUE for a match | |
298 | */ | |
299 | static BOOL | |
300 | is_name_match(const uschar * name, const uschar * pat) | |
301 | { | |
302 | uschar * cp; | |
303 | return *pat == '*' /* possible wildcard match */ | |
304 | ? *++pat == '.' /* starts star, dot */ | |
305 | && !Ustrchr(++pat, '*') /* has no more stars */ | |
306 | && Ustrchr(pat, '.') /* and has another dot. */ | |
307 | && (cp = Ustrchr(name, '.'))/* The name has at least one dot */ | |
308 | && strcmpic(++cp, pat) == 0 /* and we only compare after it. */ | |
309 | : !Ustrchr(pat+1, '*') | |
310 | && strcmpic(name, pat) == 0; | |
311 | } | |
312 | ||
313 | /* Compare a list of names with the dnsname elements | |
314 | of the Subject Alternate Name, if any, and the | |
315 | Subject otherwise. | |
316 | ||
317 | Arguments: | |
318 | namelist names to compare | |
319 | cert certificate | |
320 | ||
321 | Returns: | |
322 | TRUE/FALSE | |
323 | */ | |
324 | ||
325 | BOOL | |
55414b25 | 326 | tls_is_name_for_cert(const uschar * namelist, void * cert) |
e51c7be2 JH |
327 | { |
328 | uschar * altnames = tls_cert_subject_altname(cert, US"dns"); | |
329 | uschar * subjdn; | |
330 | uschar * certname; | |
331 | int cmp_sep = 0; | |
332 | uschar * cmpname; | |
333 | ||
334 | if ((altnames = tls_cert_subject_altname(cert, US"dns"))) | |
335 | { | |
336 | int alt_sep = '\n'; | |
7437665e | 337 | while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0))) |
e51c7be2 | 338 | { |
55414b25 | 339 | const uschar * an = altnames; |
7437665e | 340 | while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0))) |
e51c7be2 JH |
341 | if (is_name_match(cmpname, certname)) |
342 | return TRUE; | |
343 | } | |
344 | } | |
345 | ||
346 | else if ((subjdn = tls_cert_subject(cert, NULL))) | |
347 | { | |
348 | int sn_sep = ','; | |
e51c7be2 JH |
349 | |
350 | dn_to_list(subjdn); | |
67791ce4 | 351 | while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0))) |
e51c7be2 | 352 | { |
55414b25 | 353 | const uschar * sn = subjdn; |
7437665e | 354 | while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0))) |
e51c7be2 JH |
355 | if ( *certname++ == 'C' |
356 | && *certname++ == 'N' | |
357 | && *certname++ == '=' | |
358 | && is_name_match(cmpname, certname) | |
359 | ) | |
360 | return TRUE; | |
361 | } | |
362 | } | |
363 | return FALSE; | |
364 | } | |
6c1c3d1d | 365 | #endif /*SUPPORT_TLS*/ |
8442641e | 366 | #endif /*!MACRO_PREDEF*/ |
812a6045 JH |
367 | |
368 | /* vi: aw ai sw=2 | |
369 | */ | |
059ec3d9 | 370 | /* End of tls.c */ |