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