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