Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
5a66c31b | 5 | /* Copyright (c) University of Cambridge 1995 - 2014 */ |
059ec3d9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
17c76198 | 8 | /* Copyright (c) Phil Pennock 2012 */ |
059ec3d9 | 9 | |
17c76198 PP |
10 | /* This file provides TLS/SSL support for Exim using the GnuTLS library, |
11 | one of the available supported implementations. This file is #included into | |
12 | tls.c when USE_GNUTLS has been set. | |
059ec3d9 | 13 | |
17c76198 PP |
14 | The code herein is a revamp of GnuTLS integration using the current APIs; the |
15 | original tls-gnu.c was based on a patch which was contributed by Nikos | |
16 | Mavroyanopoulos. The revamp is partially a rewrite, partially cut&paste as | |
17 | appropriate. | |
059ec3d9 | 18 | |
17c76198 PP |
19 | APIs current as of GnuTLS 2.12.18; note that the GnuTLS manual is for GnuTLS 3, |
20 | which is not widely deployed by OS vendors. Will note issues below, which may | |
21 | assist in updating the code in the future. Another sources of hints is | |
22 | mod_gnutls for Apache (SNI callback registration and handling). | |
059ec3d9 | 23 | |
17c76198 PP |
24 | Keeping client and server variables more split than before and is currently |
25 | the norm, in anticipation of TLS in ACL callouts. | |
059ec3d9 | 26 | |
17c76198 PP |
27 | I wanted to switch to gnutls_certificate_set_verify_function() so that |
28 | certificate rejection could happen during handshake where it belongs, rather | |
29 | than being dropped afterwards, but that was introduced in 2.10.0 and Debian | |
30 | (6.0.5) is still on 2.8.6. So for now we have to stick with sub-par behaviour. | |
059ec3d9 | 31 | |
17c76198 PP |
32 | (I wasn't looking for libraries quite that old, when updating to get rid of |
33 | compiler warnings of deprecated APIs. If it turns out that a lot of the rest | |
34 | require current GnuTLS, then we'll drop support for the ancient libraries). | |
35 | */ | |
b5aea5e1 | 36 | |
17c76198 PP |
37 | #include <gnutls/gnutls.h> |
38 | /* needed for cert checks in verification and DN extraction: */ | |
39 | #include <gnutls/x509.h> | |
40 | /* man-page is incorrect, gnutls_rnd() is not in gnutls.h: */ | |
41 | #include <gnutls/crypto.h> | |
a5f239e4 PP |
42 | /* needed to disable PKCS11 autoload unless requested */ |
43 | #if GNUTLS_VERSION_NUMBER >= 0x020c00 | |
44 | # include <gnutls/pkcs11.h> | |
45 | #endif | |
2b4a568d JH |
46 | #ifdef EXPERIMENTAL_OCSP |
47 | # include <gnutls/ocsp.h> | |
48 | #endif | |
059ec3d9 | 49 | |
17c76198 | 50 | /* GnuTLS 2 vs 3 |
059ec3d9 | 51 | |
17c76198 PP |
52 | GnuTLS 3 only: |
53 | gnutls_global_set_audit_log_function() | |
059ec3d9 | 54 | |
17c76198 PP |
55 | Changes: |
56 | gnutls_certificate_verify_peers2(): is new, drop the 2 for old version | |
57 | */ | |
059ec3d9 | 58 | |
17c76198 | 59 | /* Local static variables for GnuTLS */ |
059ec3d9 | 60 | |
17c76198 | 61 | /* Values for verify_requirement */ |
059ec3d9 | 62 | |
17c76198 | 63 | enum peer_verify_requirement { VERIFY_NONE, VERIFY_OPTIONAL, VERIFY_REQUIRED }; |
059ec3d9 | 64 | |
17c76198 PP |
65 | /* This holds most state for server or client; with this, we can set up an |
66 | outbound TLS-enabled connection in an ACL callout, while not stomping all | |
67 | over the TLS variables available for expansion. | |
059ec3d9 | 68 | |
17c76198 PP |
69 | Some of these correspond to variables in globals.c; those variables will |
70 | be set to point to content in one of these instances, as appropriate for | |
71 | the stage of the process lifetime. | |
059ec3d9 | 72 | |
389ca47a | 73 | Not handled here: global tls_channelbinding_b64. |
17c76198 | 74 | */ |
059ec3d9 | 75 | |
17c76198 | 76 | typedef struct exim_gnutls_state { |
9d1c15ef | 77 | gnutls_session_t session; |
17c76198 | 78 | gnutls_certificate_credentials_t x509_cred; |
9d1c15ef | 79 | gnutls_priority_t priority_cache; |
17c76198 | 80 | enum peer_verify_requirement verify_requirement; |
9d1c15ef JH |
81 | int fd_in; |
82 | int fd_out; | |
83 | BOOL peer_cert_verified; | |
84 | BOOL trigger_sni_changes; | |
85 | BOOL have_set_peerdn; | |
17c76198 | 86 | const struct host_item *host; |
9d1c15ef JH |
87 | gnutls_x509_crt_t peercert; |
88 | uschar *peerdn; | |
89 | uschar *ciphersuite; | |
90 | uschar *received_sni; | |
17c76198 PP |
91 | |
92 | const uschar *tls_certificate; | |
93 | const uschar *tls_privatekey; | |
94 | const uschar *tls_sni; /* client send only, not received */ | |
95 | const uschar *tls_verify_certificates; | |
96 | const uschar *tls_crl; | |
97 | const uschar *tls_require_ciphers; | |
98 | uschar *exp_tls_certificate; | |
99 | uschar *exp_tls_privatekey; | |
100 | uschar *exp_tls_sni; | |
101 | uschar *exp_tls_verify_certificates; | |
102 | uschar *exp_tls_crl; | |
103 | uschar *exp_tls_require_ciphers; | |
104 | ||
389ca47a | 105 | tls_support *tlsp; /* set in tls_init() */ |
817d9f57 | 106 | |
17c76198 PP |
107 | uschar *xfer_buffer; |
108 | int xfer_buffer_lwm; | |
109 | int xfer_buffer_hwm; | |
110 | int xfer_eof; | |
111 | int xfer_error; | |
17c76198 PP |
112 | } exim_gnutls_state_st; |
113 | ||
114 | static const exim_gnutls_state_st exim_gnutls_state_init = { | |
4fe99a6c | 115 | NULL, NULL, NULL, VERIFY_NONE, -1, -1, FALSE, FALSE, FALSE, |
75fe387d | 116 | NULL, NULL, NULL, NULL, |
17c76198 PP |
117 | NULL, NULL, NULL, NULL, NULL, NULL, |
118 | NULL, NULL, NULL, NULL, NULL, NULL, | |
817d9f57 | 119 | NULL, |
17c76198 | 120 | NULL, 0, 0, 0, 0, |
17c76198 | 121 | }; |
83da1223 | 122 | |
17c76198 PP |
123 | /* Not only do we have our own APIs which don't pass around state, assuming |
124 | it's held in globals, GnuTLS doesn't appear to let us register callback data | |
125 | for callbacks, or as part of the session, so we have to keep a "this is the | |
126 | context we're currently dealing with" pointer and rely upon being | |
127 | single-threaded to keep from processing data on an inbound TLS connection while | |
128 | talking to another TLS connection for an outbound check. This does mean that | |
129 | there's no way for heart-beats to be responded to, for the duration of the | |
130 | second connection. */ | |
059ec3d9 | 131 | |
17c76198 | 132 | static exim_gnutls_state_st state_server, state_client; |
059ec3d9 | 133 | |
17c76198 PP |
134 | /* dh_params are initialised once within the lifetime of a process using TLS; |
135 | if we used TLS in a long-lived daemon, we'd have to reconsider this. But we | |
136 | don't want to repeat this. */ | |
83da1223 | 137 | |
17c76198 | 138 | static gnutls_dh_params_t dh_server_params = NULL; |
059ec3d9 | 139 | |
17c76198 | 140 | /* No idea how this value was chosen; preserving it. Default is 3600. */ |
059ec3d9 | 141 | |
17c76198 | 142 | static const int ssl_session_timeout = 200; |
059ec3d9 | 143 | |
17c76198 | 144 | static const char * const exim_default_gnutls_priority = "NORMAL"; |
83da1223 | 145 | |
17c76198 | 146 | /* Guard library core initialisation */ |
83da1223 | 147 | |
17c76198 | 148 | static BOOL exim_gnutls_base_init_done = FALSE; |
059ec3d9 | 149 | |
059ec3d9 | 150 | |
17c76198 PP |
151 | /* ------------------------------------------------------------------------ */ |
152 | /* macros */ | |
83da1223 | 153 | |
17c76198 | 154 | #define MAX_HOST_LEN 255 |
83da1223 | 155 | |
17c76198 PP |
156 | /* Set this to control gnutls_global_set_log_level(); values 0 to 9 will setup |
157 | the library logging; a value less than 0 disables the calls to set up logging | |
158 | callbacks. */ | |
2c17bb02 | 159 | #ifndef EXIM_GNUTLS_LIBRARY_LOG_LEVEL |
17c76198 | 160 | #define EXIM_GNUTLS_LIBRARY_LOG_LEVEL -1 |
2c17bb02 | 161 | #endif |
83da1223 | 162 | |
2c17bb02 | 163 | #ifndef EXIM_CLIENT_DH_MIN_BITS |
bba74fc6 | 164 | #define EXIM_CLIENT_DH_MIN_BITS 1024 |
2c17bb02 | 165 | #endif |
83da1223 | 166 | |
af3498d6 PP |
167 | /* With GnuTLS 2.12.x+ we have gnutls_sec_param_to_pk_bits() with which we |
168 | can ask for a bit-strength. Without that, we stick to the constant we had | |
169 | before, for now. */ | |
2c17bb02 | 170 | #ifndef EXIM_SERVER_DH_BITS_PRE2_12 |
af3498d6 | 171 | #define EXIM_SERVER_DH_BITS_PRE2_12 1024 |
2c17bb02 | 172 | #endif |
af3498d6 | 173 | |
17c76198 PP |
174 | #define exim_gnutls_err_check(Label) do { \ |
175 | if (rc != GNUTLS_E_SUCCESS) { return tls_error((Label), gnutls_strerror(rc), host); } } while (0) | |
059ec3d9 | 176 | |
17c76198 | 177 | #define expand_check_tlsvar(Varname) expand_check(state->Varname, US #Varname, &state->exp_##Varname) |
83da1223 | 178 | |
17c76198 PP |
179 | #if GNUTLS_VERSION_NUMBER >= 0x020c00 |
180 | #define HAVE_GNUTLS_SESSION_CHANNEL_BINDING | |
af3498d6 PP |
181 | #define HAVE_GNUTLS_SEC_PARAM_CONSTANTS |
182 | #define HAVE_GNUTLS_RND | |
2519e60d TL |
183 | /* The security fix we provide with the gnutls_allow_auto_pkcs11 option |
184 | * (4.82 PP/09) introduces a compatibility regression. The symbol simply | |
185 | * isn't available sometimes, so this needs to become a conditional | |
186 | * compilation; the sanest way to deal with this being a problem on | |
187 | * older OSes is to block it in the Local/Makefile with this compiler | |
188 | * definition */ | |
189 | #ifndef AVOID_GNUTLS_PKCS11 | |
a5f239e4 | 190 | #define HAVE_GNUTLS_PKCS11 |
2519e60d | 191 | #endif /* AVOID_GNUTLS_PKCS11 */ |
17c76198 | 192 | #endif |
83da1223 | 193 | |
af3498d6 PP |
194 | |
195 | ||
196 | ||
197 | /* ------------------------------------------------------------------------ */ | |
198 | /* Callback declarations */ | |
199 | ||
200 | #if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0 | |
201 | static void exim_gnutls_logger_cb(int level, const char *message); | |
202 | #endif | |
203 | ||
204 | static int exim_sni_handling_cb(gnutls_session_t session); | |
205 | ||
206 | ||
207 | ||
208 | ||
17c76198 PP |
209 | /* ------------------------------------------------------------------------ */ |
210 | /* Static functions */ | |
059ec3d9 PH |
211 | |
212 | /************************************************* | |
213 | * Handle TLS error * | |
214 | *************************************************/ | |
215 | ||
216 | /* Called from lots of places when errors occur before actually starting to do | |
217 | the TLS handshake, that is, while the session is still in clear. Always returns | |
218 | DEFER for a server and FAIL for a client so that most calls can use "return | |
219 | tls_error(...)" to do this processing and then give an appropriate return. A | |
220 | single function is used for both server and client, because it is called from | |
221 | some shared functions. | |
222 | ||
223 | Argument: | |
224 | prefix text to include in the logged error | |
7199e1ee TF |
225 | msg additional error string (may be NULL) |
226 | usually obtained from gnutls_strerror() | |
17c76198 PP |
227 | host NULL if setting up a server; |
228 | the connected host if setting up a client | |
059ec3d9 PH |
229 | |
230 | Returns: OK/DEFER/FAIL | |
231 | */ | |
232 | ||
233 | static int | |
17c76198 | 234 | tls_error(const uschar *prefix, const char *msg, const host_item *host) |
059ec3d9 | 235 | { |
17c76198 PP |
236 | if (host) |
237 | { | |
238 | log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s)%s%s", | |
239 | host->name, host->address, prefix, msg ? ": " : "", msg ? msg : ""); | |
240 | return FAIL; | |
241 | } | |
242 | else | |
059ec3d9 | 243 | { |
7199e1ee | 244 | uschar *conn_info = smtp_get_connection_info(); |
17c76198 | 245 | if (Ustrncmp(conn_info, US"SMTP ", 5) == 0) |
7199e1ee TF |
246 | conn_info += 5; |
247 | log_write(0, LOG_MAIN, "TLS error on %s (%s)%s%s", | |
17c76198 | 248 | conn_info, prefix, msg ? ": " : "", msg ? msg : ""); |
059ec3d9 PH |
249 | return DEFER; |
250 | } | |
059ec3d9 PH |
251 | } |
252 | ||
253 | ||
254 | ||
17c76198 | 255 | |
059ec3d9 | 256 | /************************************************* |
17c76198 | 257 | * Deal with logging errors during I/O * |
059ec3d9 PH |
258 | *************************************************/ |
259 | ||
17c76198 | 260 | /* We have to get the identity of the peer from saved data. |
059ec3d9 | 261 | |
17c76198 PP |
262 | Argument: |
263 | state the current GnuTLS exim state container | |
264 | rc the GnuTLS error code, or 0 if it's a local error | |
265 | when text identifying read or write | |
266 | text local error text when ec is 0 | |
059ec3d9 | 267 | |
17c76198 | 268 | Returns: nothing |
059ec3d9 PH |
269 | */ |
270 | ||
17c76198 PP |
271 | static void |
272 | record_io_error(exim_gnutls_state_st *state, int rc, uschar *when, uschar *text) | |
059ec3d9 | 273 | { |
17c76198 | 274 | const char *msg; |
059ec3d9 | 275 | |
17c76198 PP |
276 | if (rc == GNUTLS_E_FATAL_ALERT_RECEIVED) |
277 | msg = CS string_sprintf("%s: %s", US gnutls_strerror(rc), | |
278 | US gnutls_alert_get_name(gnutls_alert_get(state->session))); | |
279 | else | |
280 | msg = gnutls_strerror(rc); | |
059ec3d9 | 281 | |
17c76198 PP |
282 | tls_error(when, msg, state->host); |
283 | } | |
059ec3d9 | 284 | |
059ec3d9 | 285 | |
059ec3d9 | 286 | |
059ec3d9 | 287 | |
17c76198 PP |
288 | /************************************************* |
289 | * Set various Exim expansion vars * | |
290 | *************************************************/ | |
059ec3d9 | 291 | |
9d1c15ef JH |
292 | #define exim_gnutls_cert_err(Label) do { \ |
293 | if (rc != GNUTLS_E_SUCCESS) { \ | |
294 | DEBUG(D_tls) debug_printf("TLS: cert problem: %s: %s\n", (Label), gnutls_strerror(rc)); \ | |
295 | return rc; } } while (0) | |
296 | ||
297 | static int | |
298 | import_cert(const gnutls_datum * cert, gnutls_x509_crt_t * crtp) | |
299 | { | |
300 | int rc; | |
301 | ||
302 | rc = gnutls_x509_crt_init(crtp); | |
303 | exim_gnutls_cert_err(US"gnutls_x509_crt_init (crt)"); | |
304 | ||
305 | rc = gnutls_x509_crt_import(*crtp, cert, GNUTLS_X509_FMT_DER); | |
306 | exim_gnutls_cert_err(US"failed to import certificate [gnutls_x509_crt_import(cert)]"); | |
307 | ||
308 | return rc; | |
309 | } | |
310 | ||
311 | #undef exim_gnutls_cert_err | |
312 | ||
313 | ||
17c76198 PP |
314 | /* We set various Exim global variables from the state, once a session has |
315 | been established. With TLS callouts, may need to change this to stack | |
316 | variables, or just re-call it with the server state after client callout | |
317 | has finished. | |
059ec3d9 | 318 | |
9d1c15ef | 319 | Make sure anything set here is unset in tls_getc(). |
17c76198 PP |
320 | |
321 | Sets: | |
322 | tls_active fd | |
323 | tls_bits strength indicator | |
324 | tls_certificate_verified bool indicator | |
325 | tls_channelbinding_b64 for some SASL mechanisms | |
326 | tls_cipher a string | |
9d1c15ef | 327 | tls_peercert pointer to library internal |
17c76198 PP |
328 | tls_peerdn a string |
329 | tls_sni a (UTF-8) string | |
9d1c15ef | 330 | tls_ourcert pointer to library internal |
17c76198 PP |
331 | |
332 | Argument: | |
333 | state the relevant exim_gnutls_state_st * | |
334 | */ | |
335 | ||
336 | static void | |
9d1c15ef | 337 | extract_exim_vars_from_tls_state(exim_gnutls_state_st * state) |
17c76198 | 338 | { |
17c76198 | 339 | gnutls_cipher_algorithm_t cipher; |
17c76198 PP |
340 | #ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING |
341 | int old_pool; | |
342 | int rc; | |
343 | gnutls_datum_t channel; | |
344 | #endif | |
9d1c15ef | 345 | tls_support * tlsp = state->tlsp; |
17c76198 | 346 | |
9d1c15ef | 347 | tlsp->active = state->fd_out; |
17c76198 PP |
348 | |
349 | cipher = gnutls_cipher_get(state->session); | |
350 | /* returns size in "bytes" */ | |
9d1c15ef | 351 | tlsp->bits = gnutls_cipher_get_key_size(cipher) * 8; |
17c76198 | 352 | |
9d1c15ef | 353 | tlsp->cipher = state->ciphersuite; |
17c76198 | 354 | |
817d9f57 | 355 | DEBUG(D_tls) debug_printf("cipher: %s\n", state->ciphersuite); |
17c76198 | 356 | |
9d1c15ef | 357 | tlsp->certificate_verified = state->peer_cert_verified; |
059ec3d9 | 358 | |
17c76198 PP |
359 | /* note that tls_channelbinding_b64 is not saved to the spool file, since it's |
360 | only available for use for authenticators while this TLS session is running. */ | |
361 | ||
362 | tls_channelbinding_b64 = NULL; | |
363 | #ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING | |
364 | channel.data = NULL; | |
365 | channel.size = 0; | |
366 | rc = gnutls_session_channel_binding(state->session, GNUTLS_CB_TLS_UNIQUE, &channel); | |
367 | if (rc) { | |
368 | DEBUG(D_tls) debug_printf("Channel binding error: %s\n", gnutls_strerror(rc)); | |
369 | } else { | |
370 | old_pool = store_pool; | |
371 | store_pool = POOL_PERM; | |
372 | tls_channelbinding_b64 = auth_b64encode(channel.data, (int)channel.size); | |
373 | store_pool = old_pool; | |
374 | DEBUG(D_tls) debug_printf("Have channel bindings cached for possible auth usage.\n"); | |
375 | } | |
376 | #endif | |
377 | ||
9d1c15ef JH |
378 | /* peercert is set in peer_status() */ |
379 | tlsp->peerdn = state->peerdn; | |
380 | tlsp->sni = state->received_sni; | |
381 | ||
382 | /* record our certificate */ | |
383 | { | |
384 | const gnutls_datum * cert = gnutls_certificate_get_ours(state->session); | |
385 | gnutls_x509_crt_t crt; | |
386 | ||
387 | tlsp->ourcert = cert && import_cert(cert, &crt)==0 ? crt : NULL; | |
388 | } | |
059ec3d9 PH |
389 | } |
390 | ||
391 | ||
392 | ||
17c76198 | 393 | |
059ec3d9 | 394 | /************************************************* |
575643cd | 395 | * Setup up DH parameters * |
059ec3d9 PH |
396 | *************************************************/ |
397 | ||
575643cd | 398 | /* Generating the D-H parameters may take a long time. They only need to |
059ec3d9 PH |
399 | be re-generated every so often, depending on security policy. What we do is to |
400 | keep these parameters in a file in the spool directory. If the file does not | |
401 | exist, we generate them. This means that it is easy to cause a regeneration. | |
402 | ||
403 | The new file is written as a temporary file and renamed, so that an incomplete | |
404 | file is never present. If two processes both compute some new parameters, you | |
405 | waste a bit of effort, but it doesn't seem worth messing around with locking to | |
406 | prevent this. | |
407 | ||
059ec3d9 PH |
408 | Returns: OK/DEFER/FAIL |
409 | */ | |
410 | ||
411 | static int | |
17c76198 | 412 | init_server_dh(void) |
059ec3d9 | 413 | { |
17c76198 PP |
414 | int fd, rc; |
415 | unsigned int dh_bits; | |
b5aea5e1 | 416 | gnutls_datum m; |
a799883d PP |
417 | uschar filename_buf[PATH_MAX]; |
418 | uschar *filename = NULL; | |
17c76198 | 419 | size_t sz; |
a799883d PP |
420 | uschar *exp_tls_dhparam; |
421 | BOOL use_file_in_spool = FALSE; | |
422 | BOOL use_fixed_file = FALSE; | |
17c76198 | 423 | host_item *host = NULL; /* dummy for macros */ |
059ec3d9 | 424 | |
17c76198 | 425 | DEBUG(D_tls) debug_printf("Initialising GnuTLS server params.\n"); |
059ec3d9 | 426 | |
17c76198 PP |
427 | rc = gnutls_dh_params_init(&dh_server_params); |
428 | exim_gnutls_err_check(US"gnutls_dh_params_init"); | |
059ec3d9 | 429 | |
a799883d PP |
430 | m.data = NULL; |
431 | m.size = 0; | |
432 | ||
433 | if (!expand_check(tls_dhparam, US"tls_dhparam", &exp_tls_dhparam)) | |
434 | return DEFER; | |
435 | ||
436 | if (!exp_tls_dhparam) | |
437 | { | |
438 | DEBUG(D_tls) debug_printf("Loading default hard-coded DH params\n"); | |
439 | m.data = US std_dh_prime_default(); | |
440 | m.size = Ustrlen(m.data); | |
441 | } | |
442 | else if (Ustrcmp(exp_tls_dhparam, "historic") == 0) | |
443 | use_file_in_spool = TRUE; | |
444 | else if (Ustrcmp(exp_tls_dhparam, "none") == 0) | |
445 | { | |
446 | DEBUG(D_tls) debug_printf("Requested no DH parameters.\n"); | |
447 | return OK; | |
448 | } | |
449 | else if (exp_tls_dhparam[0] != '/') | |
450 | { | |
451 | m.data = US std_dh_prime_named(exp_tls_dhparam); | |
452 | if (m.data == NULL) | |
453 | return tls_error(US"No standard prime named", CS exp_tls_dhparam, NULL); | |
454 | m.size = Ustrlen(m.data); | |
455 | } | |
456 | else | |
457 | { | |
458 | use_fixed_file = TRUE; | |
459 | filename = exp_tls_dhparam; | |
460 | } | |
461 | ||
462 | if (m.data) | |
463 | { | |
464 | rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM); | |
465 | exim_gnutls_err_check(US"gnutls_dh_params_import_pkcs3"); | |
466 | DEBUG(D_tls) debug_printf("Loaded fixed standard D-H parameters\n"); | |
467 | return OK; | |
468 | } | |
469 | ||
af3498d6 PP |
470 | #ifdef HAVE_GNUTLS_SEC_PARAM_CONSTANTS |
471 | /* If you change this constant, also change dh_param_fn_ext so that we can use a | |
17c76198 PP |
472 | different filename and ensure we have sufficient bits. */ |
473 | dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_NORMAL); | |
474 | if (!dh_bits) | |
475 | return tls_error(US"gnutls_sec_param_to_pk_bits() failed", NULL, NULL); | |
af3498d6 | 476 | DEBUG(D_tls) |
b34fc30c | 477 | debug_printf("GnuTLS tells us that for D-H PK, NORMAL is %d bits.\n", |
af3498d6 PP |
478 | dh_bits); |
479 | #else | |
480 | dh_bits = EXIM_SERVER_DH_BITS_PRE2_12; | |
481 | DEBUG(D_tls) | |
482 | debug_printf("GnuTLS lacks gnutls_sec_param_to_pk_bits(), using %d bits.\n", | |
483 | dh_bits); | |
484 | #endif | |
059ec3d9 | 485 | |
3375e053 PP |
486 | /* Some clients have hard-coded limits. */ |
487 | if (dh_bits > tls_dh_max_bits) | |
488 | { | |
489 | DEBUG(D_tls) | |
490 | debug_printf("tls_dh_max_bits clamping override, using %d bits instead.\n", | |
491 | tls_dh_max_bits); | |
492 | dh_bits = tls_dh_max_bits; | |
493 | } | |
494 | ||
a799883d PP |
495 | if (use_file_in_spool) |
496 | { | |
497 | if (!string_format(filename_buf, sizeof(filename_buf), | |
498 | "%s/gnutls-params-%d", spool_directory, dh_bits)) | |
499 | return tls_error(US"overlong filename", NULL, NULL); | |
500 | filename = filename_buf; | |
501 | } | |
059ec3d9 | 502 | |
b5aea5e1 | 503 | /* Open the cache file for reading and if successful, read it and set up the |
575643cd | 504 | parameters. */ |
059ec3d9 PH |
505 | |
506 | fd = Uopen(filename, O_RDONLY, 0); | |
b5aea5e1 | 507 | if (fd >= 0) |
059ec3d9 | 508 | { |
b5aea5e1 | 509 | struct stat statbuf; |
17c76198 PP |
510 | FILE *fp; |
511 | int saved_errno; | |
512 | ||
513 | if (fstat(fd, &statbuf) < 0) /* EIO */ | |
514 | { | |
515 | saved_errno = errno; | |
516 | (void)close(fd); | |
517 | return tls_error(US"TLS cache stat failed", strerror(saved_errno), NULL); | |
518 | } | |
519 | if (!S_ISREG(statbuf.st_mode)) | |
b5aea5e1 PH |
520 | { |
521 | (void)close(fd); | |
17c76198 PP |
522 | return tls_error(US"TLS cache not a file", NULL, NULL); |
523 | } | |
524 | fp = fdopen(fd, "rb"); | |
525 | if (!fp) | |
526 | { | |
527 | saved_errno = errno; | |
528 | (void)close(fd); | |
529 | return tls_error(US"fdopen(TLS cache stat fd) failed", | |
530 | strerror(saved_errno), NULL); | |
b5aea5e1 | 531 | } |
059ec3d9 | 532 | |
b5aea5e1 PH |
533 | m.size = statbuf.st_size; |
534 | m.data = malloc(m.size); | |
535 | if (m.data == NULL) | |
17c76198 PP |
536 | { |
537 | fclose(fp); | |
538 | return tls_error(US"malloc failed", strerror(errno), NULL); | |
539 | } | |
540 | sz = fread(m.data, m.size, 1, fp); | |
541 | if (!sz) | |
542 | { | |
543 | saved_errno = errno; | |
544 | fclose(fp); | |
545 | free(m.data); | |
546 | return tls_error(US"fread failed", strerror(saved_errno), NULL); | |
547 | } | |
548 | fclose(fp); | |
b5aea5e1 | 549 | |
17c76198 | 550 | rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM); |
b5aea5e1 | 551 | free(m.data); |
17c76198 PP |
552 | exim_gnutls_err_check(US"gnutls_dh_params_import_pkcs3"); |
553 | DEBUG(D_tls) debug_printf("read D-H parameters from file \"%s\"\n", filename); | |
b5aea5e1 PH |
554 | } |
555 | ||
556 | /* If the file does not exist, fall through to compute new data and cache it. | |
557 | If there was any other opening error, it is serious. */ | |
558 | ||
182ad5cf PH |
559 | else if (errno == ENOENT) |
560 | { | |
17c76198 | 561 | rc = -1; |
182ad5cf | 562 | DEBUG(D_tls) |
17c76198 | 563 | debug_printf("D-H parameter cache file \"%s\" does not exist\n", filename); |
182ad5cf PH |
564 | } |
565 | else | |
17c76198 PP |
566 | return tls_error(string_open_failed(errno, "\"%s\" for reading", filename), |
567 | NULL, NULL); | |
b5aea5e1 PH |
568 | |
569 | /* If ret < 0, either the cache file does not exist, or the data it contains | |
570 | is not useful. One particular case of this is when upgrading from an older | |
571 | release of Exim in which the data was stored in a different format. We don't | |
572 | try to be clever and support both formats; we just regenerate new data in this | |
573 | case. */ | |
574 | ||
17c76198 | 575 | if (rc < 0) |
b5aea5e1 | 576 | { |
17c76198 | 577 | uschar *temp_fn; |
201f5254 | 578 | unsigned int dh_bits_gen = dh_bits; |
059ec3d9 | 579 | |
17c76198 PP |
580 | if ((PATH_MAX - Ustrlen(filename)) < 10) |
581 | return tls_error(US"Filename too long to generate replacement", | |
582 | CS filename, NULL); | |
059ec3d9 | 583 | |
17c76198 PP |
584 | temp_fn = string_copy(US "%s.XXXXXXX"); |
585 | fd = mkstemp(CS temp_fn); /* modifies temp_fn */ | |
059ec3d9 | 586 | if (fd < 0) |
17c76198 | 587 | return tls_error(US"Unable to open temp file", strerror(errno), NULL); |
059ec3d9 PH |
588 | (void)fchown(fd, exim_uid, exim_gid); /* Probably not necessary */ |
589 | ||
201f5254 PP |
590 | /* GnuTLS overshoots! |
591 | * If we ask for 2236, we might get 2237 or more. | |
592 | * But there's no way to ask GnuTLS how many bits there really are. | |
593 | * We can ask how many bits were used in a TLS session, but that's it! | |
594 | * The prime itself is hidden behind too much abstraction. | |
595 | * So we ask for less, and proceed on a wing and a prayer. | |
596 | * First attempt, subtracted 3 for 2233 and got 2240. | |
597 | */ | |
cae6e576 | 598 | if (dh_bits >= EXIM_CLIENT_DH_MIN_BITS + 10) |
201f5254 PP |
599 | { |
600 | dh_bits_gen = dh_bits - 10; | |
601 | DEBUG(D_tls) | |
602 | debug_printf("being paranoid about DH generation, make it '%d' bits'\n", | |
603 | dh_bits_gen); | |
604 | } | |
605 | ||
606 | DEBUG(D_tls) | |
607 | debug_printf("requesting generation of %d bit Diffie-Hellman prime ...\n", | |
608 | dh_bits_gen); | |
609 | rc = gnutls_dh_params_generate2(dh_server_params, dh_bits_gen); | |
17c76198 PP |
610 | exim_gnutls_err_check(US"gnutls_dh_params_generate2"); |
611 | ||
612 | /* gnutls_dh_params_export_pkcs3() will tell us the exact size, every time, | |
613 | and I confirmed that a NULL call to get the size first is how the GnuTLS | |
614 | sample apps handle this. */ | |
615 | ||
616 | sz = 0; | |
617 | m.data = NULL; | |
618 | rc = gnutls_dh_params_export_pkcs3(dh_server_params, GNUTLS_X509_FMT_PEM, | |
619 | m.data, &sz); | |
620 | if (rc != GNUTLS_E_SHORT_MEMORY_BUFFER) | |
621 | exim_gnutls_err_check(US"gnutls_dh_params_export_pkcs3(NULL) sizing"); | |
622 | m.size = sz; | |
b5aea5e1 PH |
623 | m.data = malloc(m.size); |
624 | if (m.data == NULL) | |
17c76198 | 625 | return tls_error(US"memory allocation failed", strerror(errno), NULL); |
1f00591e | 626 | /* this will return a size 1 less than the allocation size above */ |
17c76198 | 627 | rc = gnutls_dh_params_export_pkcs3(dh_server_params, GNUTLS_X509_FMT_PEM, |
1f00591e | 628 | m.data, &sz); |
17c76198 PP |
629 | if (rc != GNUTLS_E_SUCCESS) |
630 | { | |
631 | free(m.data); | |
632 | exim_gnutls_err_check(US"gnutls_dh_params_export_pkcs3() real"); | |
633 | } | |
1f00591e | 634 | m.size = sz; /* shrink by 1, probably */ |
059ec3d9 | 635 | |
17c76198 PP |
636 | sz = write_to_fd_buf(fd, m.data, (size_t) m.size); |
637 | if (sz != m.size) | |
638 | { | |
639 | free(m.data); | |
640 | return tls_error(US"TLS cache write D-H params failed", | |
641 | strerror(errno), NULL); | |
642 | } | |
b5aea5e1 | 643 | free(m.data); |
17c76198 PP |
644 | sz = write_to_fd_buf(fd, US"\n", 1); |
645 | if (sz != 1) | |
646 | return tls_error(US"TLS cache write D-H params final newline failed", | |
647 | strerror(errno), NULL); | |
648 | ||
649 | rc = close(fd); | |
650 | if (rc) | |
651 | return tls_error(US"TLS cache write close() failed", | |
652 | strerror(errno), NULL); | |
059ec3d9 | 653 | |
17c76198 PP |
654 | if (Urename(temp_fn, filename) < 0) |
655 | return tls_error(string_sprintf("failed to rename \"%s\" as \"%s\"", | |
656 | temp_fn, filename), strerror(errno), NULL); | |
059ec3d9 | 657 | |
17c76198 | 658 | DEBUG(D_tls) debug_printf("wrote D-H parameters to file \"%s\"\n", filename); |
059ec3d9 PH |
659 | } |
660 | ||
17c76198 | 661 | DEBUG(D_tls) debug_printf("initialized server D-H parameters\n"); |
059ec3d9 PH |
662 | return OK; |
663 | } | |
664 | ||
665 | ||
666 | ||
667 | ||
668 | /************************************************* | |
17c76198 | 669 | * Variables re-expanded post-SNI * |
059ec3d9 PH |
670 | *************************************************/ |
671 | ||
17c76198 PP |
672 | /* Called from both server and client code, via tls_init(), and also from |
673 | the SNI callback after receiving an SNI, if tls_certificate includes "tls_sni". | |
674 | ||
675 | We can tell the two apart by state->received_sni being non-NULL in callback. | |
676 | ||
677 | The callback should not call us unless state->trigger_sni_changes is true, | |
678 | which we are responsible for setting on the first pass through. | |
059ec3d9 PH |
679 | |
680 | Arguments: | |
17c76198 | 681 | state exim_gnutls_state_st * |
059ec3d9 PH |
682 | |
683 | Returns: OK/DEFER/FAIL | |
684 | */ | |
685 | ||
686 | static int | |
17c76198 | 687 | tls_expand_session_files(exim_gnutls_state_st *state) |
059ec3d9 | 688 | { |
1365611d | 689 | struct stat statbuf; |
059ec3d9 | 690 | int rc; |
17c76198 PP |
691 | const host_item *host = state->host; /* macro should be reconsidered? */ |
692 | uschar *saved_tls_certificate = NULL; | |
693 | uschar *saved_tls_privatekey = NULL; | |
694 | uschar *saved_tls_verify_certificates = NULL; | |
695 | uschar *saved_tls_crl = NULL; | |
696 | int cert_count; | |
697 | ||
698 | /* We check for tls_sni *before* expansion. */ | |
2b4a568d | 699 | if (!host) /* server */ |
17c76198 PP |
700 | { |
701 | if (!state->received_sni) | |
702 | { | |
d9b2312b JH |
703 | if (state->tls_certificate && |
704 | (Ustrstr(state->tls_certificate, US"tls_sni") || | |
705 | Ustrstr(state->tls_certificate, US"tls_in_sni") || | |
706 | Ustrstr(state->tls_certificate, US"tls_out_sni") | |
707 | )) | |
17c76198 PP |
708 | { |
709 | DEBUG(D_tls) debug_printf("We will re-expand TLS session files if we receive SNI.\n"); | |
710 | state->trigger_sni_changes = TRUE; | |
711 | } | |
712 | } | |
713 | else | |
714 | { | |
1365611d | 715 | /* useful for debugging */ |
17c76198 PP |
716 | saved_tls_certificate = state->exp_tls_certificate; |
717 | saved_tls_privatekey = state->exp_tls_privatekey; | |
718 | saved_tls_verify_certificates = state->exp_tls_verify_certificates; | |
719 | saved_tls_crl = state->exp_tls_crl; | |
720 | } | |
721 | } | |
059ec3d9 | 722 | |
1365611d PP |
723 | rc = gnutls_certificate_allocate_credentials(&state->x509_cred); |
724 | exim_gnutls_err_check(US"gnutls_certificate_allocate_credentials"); | |
725 | ||
17c76198 PP |
726 | /* remember: expand_check_tlsvar() is expand_check() but fiddling with |
727 | state members, assuming consistent naming; and expand_check() returns | |
728 | false if expansion failed, unless expansion was forced to fail. */ | |
059ec3d9 | 729 | |
17c76198 PP |
730 | /* check if we at least have a certificate, before doing expensive |
731 | D-H generation. */ | |
059ec3d9 | 732 | |
17c76198 PP |
733 | if (!expand_check_tlsvar(tls_certificate)) |
734 | return DEFER; | |
059ec3d9 | 735 | |
17c76198 | 736 | /* certificate is mandatory in server, optional in client */ |
059ec3d9 | 737 | |
17c76198 PP |
738 | if ((state->exp_tls_certificate == NULL) || |
739 | (*state->exp_tls_certificate == '\0')) | |
740 | { | |
2b4a568d | 741 | if (!host) |
17c76198 PP |
742 | return tls_error(US"no TLS server certificate is specified", NULL, NULL); |
743 | else | |
744 | DEBUG(D_tls) debug_printf("TLS: no client certificate specified; okay\n"); | |
745 | } | |
059ec3d9 | 746 | |
17c76198 | 747 | if (state->tls_privatekey && !expand_check_tlsvar(tls_privatekey)) |
059ec3d9 PH |
748 | return DEFER; |
749 | ||
17c76198 PP |
750 | /* tls_privatekey is optional, defaulting to same file as certificate */ |
751 | ||
752 | if (state->tls_privatekey == NULL || *state->tls_privatekey == '\0') | |
059ec3d9 | 753 | { |
17c76198 PP |
754 | state->tls_privatekey = state->tls_certificate; |
755 | state->exp_tls_privatekey = state->exp_tls_certificate; | |
059ec3d9 | 756 | } |
c91535f3 | 757 | |
059ec3d9 | 758 | |
17c76198 | 759 | if (state->exp_tls_certificate && *state->exp_tls_certificate) |
059ec3d9 PH |
760 | { |
761 | DEBUG(D_tls) debug_printf("certificate file = %s\nkey file = %s\n", | |
17c76198 PP |
762 | state->exp_tls_certificate, state->exp_tls_privatekey); |
763 | ||
764 | if (state->received_sni) | |
de365ded | 765 | { |
17c76198 PP |
766 | if ((Ustrcmp(state->exp_tls_certificate, saved_tls_certificate) == 0) && |
767 | (Ustrcmp(state->exp_tls_privatekey, saved_tls_privatekey) == 0)) | |
768 | { | |
b34fc30c | 769 | DEBUG(D_tls) debug_printf("TLS SNI: cert and key unchanged\n"); |
17c76198 PP |
770 | } |
771 | else | |
772 | { | |
b34fc30c | 773 | DEBUG(D_tls) debug_printf("TLS SNI: have a changed cert/key pair.\n"); |
17c76198 | 774 | } |
8e669ac1 | 775 | } |
059ec3d9 | 776 | |
1365611d PP |
777 | rc = gnutls_certificate_set_x509_key_file(state->x509_cred, |
778 | CS state->exp_tls_certificate, CS state->exp_tls_privatekey, | |
779 | GNUTLS_X509_FMT_PEM); | |
780 | exim_gnutls_err_check( | |
781 | string_sprintf("cert/key setup: cert=%s key=%s", | |
782 | state->exp_tls_certificate, state->exp_tls_privatekey)); | |
783 | DEBUG(D_tls) debug_printf("TLS: cert/key registered\n"); | |
b34fc30c | 784 | } /* tls_certificate */ |
059ec3d9 | 785 | |
2b4a568d JH |
786 | |
787 | /* Set the OCSP stapling server info */ | |
788 | ||
789 | #ifdef EXPERIMENTAL_OCSP | |
790 | if ( !host /* server */ | |
791 | && tls_ocsp_file | |
792 | ) | |
793 | { | |
794 | uschar * expanded; | |
795 | int rc; | |
796 | ||
797 | if (!expand_check(tls_ocsp_file, US"tls_ocsp_file", &expanded)) | |
798 | return DEFER; | |
799 | ||
800 | /* Lazy way; would like callback to emit debug on actual response */ | |
801 | ||
802 | rc = gnutls_certificate_set_ocsp_status_request_file(state->x509_cred, | |
803 | expanded, 0); | |
804 | exim_gnutls_err_check(US"gnutls_certificate_set_ocsp_status_request_file"); | |
805 | DEBUG(D_tls) debug_printf("Set OCSP response file %s\n", expanded); | |
806 | } | |
807 | #endif | |
808 | ||
809 | ||
059ec3d9 PH |
810 | /* Set the trusted CAs file if one is provided, and then add the CRL if one is |
811 | provided. Experiment shows that, if the certificate file is empty, an unhelpful | |
812 | error message is provided. However, if we just refrain from setting anything up | |
813 | in that case, certificate verification fails, which seems to be the correct | |
814 | behaviour. */ | |
815 | ||
17c76198 | 816 | if (state->tls_verify_certificates && *state->tls_verify_certificates) |
059ec3d9 | 817 | { |
17c76198 | 818 | if (!expand_check_tlsvar(tls_verify_certificates)) |
059ec3d9 | 819 | return DEFER; |
17c76198 PP |
820 | if (state->tls_crl && *state->tls_crl) |
821 | if (!expand_check_tlsvar(tls_crl)) | |
822 | return DEFER; | |
059ec3d9 | 823 | |
1365611d PP |
824 | if (!(state->exp_tls_verify_certificates && |
825 | *state->exp_tls_verify_certificates)) | |
b34fc30c PP |
826 | { |
827 | DEBUG(D_tls) | |
1365611d PP |
828 | debug_printf("TLS: tls_verify_certificates expanded empty, ignoring\n"); |
829 | /* With no tls_verify_certificates, we ignore tls_crl too */ | |
17c76198 | 830 | return OK; |
b34fc30c | 831 | } |
1365611d | 832 | } |
83e2f8a2 PP |
833 | else |
834 | { | |
835 | DEBUG(D_tls) | |
836 | debug_printf("TLS: tls_verify_certificates not set or empty, ignoring\n"); | |
837 | return OK; | |
838 | } | |
17c76198 | 839 | |
1365611d PP |
840 | if (Ustat(state->exp_tls_verify_certificates, &statbuf) < 0) |
841 | { | |
842 | log_write(0, LOG_MAIN|LOG_PANIC, "could not stat %s " | |
843 | "(tls_verify_certificates): %s", state->exp_tls_verify_certificates, | |
844 | strerror(errno)); | |
845 | return DEFER; | |
846 | } | |
17c76198 | 847 | |
619b2b25 PP |
848 | /* The test suite passes in /dev/null; we could check for that path explicitly, |
849 | but who knows if someone has some weird FIFO which always dumps some certs, or | |
850 | other weirdness. The thing we really want to check is that it's not a | |
851 | directory, since while OpenSSL supports that, GnuTLS does not. | |
852 | So s/!S_ISREG/S_ISDIR/ and change some messsaging ... */ | |
853 | if (S_ISDIR(statbuf.st_mode)) | |
1365611d PP |
854 | { |
855 | DEBUG(D_tls) | |
619b2b25 PP |
856 | debug_printf("verify certificates path is a dir: \"%s\"\n", |
857 | state->exp_tls_verify_certificates); | |
1365611d | 858 | log_write(0, LOG_MAIN|LOG_PANIC, |
619b2b25 | 859 | "tls_verify_certificates \"%s\" is a directory", |
1365611d PP |
860 | state->exp_tls_verify_certificates); |
861 | return DEFER; | |
862 | } | |
059ec3d9 | 863 | |
1365611d PP |
864 | DEBUG(D_tls) debug_printf("verify certificates = %s size=" OFF_T_FMT "\n", |
865 | state->exp_tls_verify_certificates, statbuf.st_size); | |
059ec3d9 | 866 | |
1365611d PP |
867 | if (statbuf.st_size == 0) |
868 | { | |
869 | DEBUG(D_tls) | |
870 | debug_printf("cert file empty, no certs, no verification, ignoring any CRL\n"); | |
871 | return OK; | |
872 | } | |
059ec3d9 | 873 | |
1365611d PP |
874 | cert_count = gnutls_certificate_set_x509_trust_file(state->x509_cred, |
875 | CS state->exp_tls_verify_certificates, GNUTLS_X509_FMT_PEM); | |
876 | if (cert_count < 0) | |
877 | { | |
878 | rc = cert_count; | |
879 | exim_gnutls_err_check(US"gnutls_certificate_set_x509_trust_file"); | |
880 | } | |
881 | DEBUG(D_tls) debug_printf("Added %d certificate authorities.\n", cert_count); | |
059ec3d9 | 882 | |
5c8cda3a PP |
883 | if (state->tls_crl && *state->tls_crl && |
884 | state->exp_tls_crl && *state->exp_tls_crl) | |
1365611d | 885 | { |
5c8cda3a PP |
886 | DEBUG(D_tls) debug_printf("loading CRL file = %s\n", state->exp_tls_crl); |
887 | cert_count = gnutls_certificate_set_x509_crl_file(state->x509_cred, | |
888 | CS state->exp_tls_crl, GNUTLS_X509_FMT_PEM); | |
889 | if (cert_count < 0) | |
1365611d | 890 | { |
5c8cda3a | 891 | rc = cert_count; |
1365611d PP |
892 | exim_gnutls_err_check(US"gnutls_certificate_set_x509_crl_file"); |
893 | } | |
5c8cda3a | 894 | DEBUG(D_tls) debug_printf("Processed %d CRLs.\n", cert_count); |
1365611d | 895 | } |
059ec3d9 | 896 | |
059ec3d9 PH |
897 | return OK; |
898 | } | |
899 | ||
900 | ||
901 | ||
902 | ||
1365611d PP |
903 | /************************************************* |
904 | * Set X.509 state variables * | |
905 | *************************************************/ | |
906 | ||
907 | /* In GnuTLS, the registered cert/key are not replaced by a later | |
908 | set of a cert/key, so for SNI support we need a whole new x509_cred | |
909 | structure. Which means various other non-re-expanded pieces of state | |
910 | need to be re-set in the new struct, so the setting logic is pulled | |
911 | out to this. | |
912 | ||
913 | Arguments: | |
914 | state exim_gnutls_state_st * | |
915 | ||
916 | Returns: OK/DEFER/FAIL | |
917 | */ | |
918 | ||
919 | static int | |
920 | tls_set_remaining_x509(exim_gnutls_state_st *state) | |
921 | { | |
922 | int rc; | |
923 | const host_item *host = state->host; /* macro should be reconsidered? */ | |
924 | ||
925 | /* Create D-H parameters, or read them from the cache file. This function does | |
926 | its own SMTP error messaging. This only happens for the server, TLS D-H ignores | |
927 | client-side params. */ | |
928 | ||
929 | if (!state->host) | |
930 | { | |
931 | if (!dh_server_params) | |
932 | { | |
933 | rc = init_server_dh(); | |
934 | if (rc != OK) return rc; | |
935 | } | |
936 | gnutls_certificate_set_dh_params(state->x509_cred, dh_server_params); | |
937 | } | |
938 | ||
939 | /* Link the credentials to the session. */ | |
940 | ||
941 | rc = gnutls_credentials_set(state->session, GNUTLS_CRD_CERTIFICATE, state->x509_cred); | |
942 | exim_gnutls_err_check(US"gnutls_credentials_set"); | |
943 | ||
944 | return OK; | |
945 | } | |
946 | ||
059ec3d9 | 947 | /************************************************* |
17c76198 | 948 | * Initialize for GnuTLS * |
059ec3d9 PH |
949 | *************************************************/ |
950 | ||
17c76198 PP |
951 | /* Called from both server and client code. In the case of a server, errors |
952 | before actual TLS negotiation return DEFER. | |
059ec3d9 PH |
953 | |
954 | Arguments: | |
17c76198 PP |
955 | host connected host, if client; NULL if server |
956 | certificate certificate file | |
957 | privatekey private key file | |
958 | sni TLS SNI to send, sometimes when client; else NULL | |
959 | cas CA certs file | |
960 | crl CRL file | |
961 | require_ciphers tls_require_ciphers setting | |
817d9f57 | 962 | caller_state returned state-info structure |
059ec3d9 | 963 | |
17c76198 | 964 | Returns: OK/DEFER/FAIL |
059ec3d9 PH |
965 | */ |
966 | ||
17c76198 PP |
967 | static int |
968 | tls_init( | |
969 | const host_item *host, | |
970 | const uschar *certificate, | |
971 | const uschar *privatekey, | |
972 | const uschar *sni, | |
973 | const uschar *cas, | |
974 | const uschar *crl, | |
975 | const uschar *require_ciphers, | |
976 | exim_gnutls_state_st **caller_state) | |
059ec3d9 | 977 | { |
17c76198 PP |
978 | exim_gnutls_state_st *state; |
979 | int rc; | |
980 | size_t sz; | |
981 | const char *errpos; | |
982 | uschar *p; | |
983 | BOOL want_default_priorities; | |
984 | ||
985 | if (!exim_gnutls_base_init_done) | |
059ec3d9 | 986 | { |
17c76198 PP |
987 | DEBUG(D_tls) debug_printf("GnuTLS global init required.\n"); |
988 | ||
a5f239e4 PP |
989 | #ifdef HAVE_GNUTLS_PKCS11 |
990 | /* By default, gnutls_global_init will init PKCS11 support in auto mode, | |
991 | which loads modules from a config file, which sounds good and may be wanted | |
992 | by some sysadmin, but also means in common configurations that GNOME keyring | |
993 | environment variables are used and so breaks for users calling mailq. | |
994 | To prevent this, we init PKCS11 first, which is the documented approach. */ | |
2519e60d | 995 | if (!gnutls_allow_auto_pkcs11) |
a5f239e4 PP |
996 | { |
997 | rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL); | |
998 | exim_gnutls_err_check(US"gnutls_pkcs11_init"); | |
999 | } | |
1000 | #endif | |
1001 | ||
17c76198 PP |
1002 | rc = gnutls_global_init(); |
1003 | exim_gnutls_err_check(US"gnutls_global_init"); | |
1004 | ||
1005 | #if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0 | |
1006 | DEBUG(D_tls) | |
059ec3d9 | 1007 | { |
17c76198 PP |
1008 | gnutls_global_set_log_function(exim_gnutls_logger_cb); |
1009 | /* arbitrarily chosen level; bump upto 9 for more */ | |
1010 | gnutls_global_set_log_level(EXIM_GNUTLS_LIBRARY_LOG_LEVEL); | |
059ec3d9 | 1011 | } |
17c76198 PP |
1012 | #endif |
1013 | ||
1014 | exim_gnutls_base_init_done = TRUE; | |
059ec3d9 | 1015 | } |
059ec3d9 | 1016 | |
17c76198 PP |
1017 | if (host) |
1018 | { | |
1019 | state = &state_client; | |
1020 | memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init)); | |
817d9f57 | 1021 | state->tlsp = &tls_out; |
17c76198 PP |
1022 | DEBUG(D_tls) debug_printf("initialising GnuTLS client session\n"); |
1023 | rc = gnutls_init(&state->session, GNUTLS_CLIENT); | |
1024 | } | |
1025 | else | |
1026 | { | |
1027 | state = &state_server; | |
1028 | memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init)); | |
817d9f57 | 1029 | state->tlsp = &tls_in; |
17c76198 PP |
1030 | DEBUG(D_tls) debug_printf("initialising GnuTLS server session\n"); |
1031 | rc = gnutls_init(&state->session, GNUTLS_SERVER); | |
1032 | } | |
1033 | exim_gnutls_err_check(US"gnutls_init"); | |
059ec3d9 | 1034 | |
17c76198 | 1035 | state->host = host; |
059ec3d9 | 1036 | |
17c76198 PP |
1037 | state->tls_certificate = certificate; |
1038 | state->tls_privatekey = privatekey; | |
5779e6aa | 1039 | state->tls_require_ciphers = require_ciphers; |
17c76198 PP |
1040 | state->tls_sni = sni; |
1041 | state->tls_verify_certificates = cas; | |
1042 | state->tls_crl = crl; | |
059ec3d9 | 1043 | |
17c76198 PP |
1044 | /* This handles the variables that might get re-expanded after TLS SNI; |
1045 | that's tls_certificate, tls_privatekey, tls_verify_certificates, tls_crl */ | |
059ec3d9 | 1046 | |
17c76198 PP |
1047 | DEBUG(D_tls) |
1048 | debug_printf("Expanding various TLS configuration options for session credentials.\n"); | |
1049 | rc = tls_expand_session_files(state); | |
1050 | if (rc != OK) return rc; | |
059ec3d9 | 1051 | |
1365611d PP |
1052 | /* These are all other parts of the x509_cred handling, since SNI in GnuTLS |
1053 | requires a new structure afterwards. */ | |
83da1223 | 1054 | |
1365611d PP |
1055 | rc = tls_set_remaining_x509(state); |
1056 | if (rc != OK) return rc; | |
83da1223 | 1057 | |
17c76198 PP |
1058 | /* set SNI in client, only */ |
1059 | if (host) | |
1060 | { | |
a5f239e4 | 1061 | if (!expand_check(state->tlsp->sni, US"tls_out_sni", &state->exp_tls_sni)) |
17c76198 PP |
1062 | return DEFER; |
1063 | if (state->exp_tls_sni && *state->exp_tls_sni) | |
1064 | { | |
1065 | DEBUG(D_tls) | |
1066 | debug_printf("Setting TLS client SNI to \"%s\"\n", state->exp_tls_sni); | |
1067 | sz = Ustrlen(state->exp_tls_sni); | |
1068 | rc = gnutls_server_name_set(state->session, | |
1069 | GNUTLS_NAME_DNS, state->exp_tls_sni, sz); | |
1070 | exim_gnutls_err_check(US"gnutls_server_name_set"); | |
1071 | } | |
1072 | } | |
1073 | else if (state->tls_sni) | |
1074 | DEBUG(D_tls) debug_printf("*** PROBABLY A BUG *** " \ | |
1075 | "have an SNI set for a client [%s]\n", state->tls_sni); | |
83da1223 | 1076 | |
17c76198 | 1077 | /* This is the priority string support, |
42bfef1e | 1078 | http://www.gnutls.org/manual/html_node/Priority-Strings.html |
17c76198 PP |
1079 | and replaces gnutls_require_kx, gnutls_require_mac & gnutls_require_protocols. |
1080 | This was backwards incompatible, but means Exim no longer needs to track | |
1081 | all algorithms and provide string forms for them. */ | |
83da1223 | 1082 | |
17c76198 | 1083 | want_default_priorities = TRUE; |
83da1223 | 1084 | |
17c76198 | 1085 | if (state->tls_require_ciphers && *state->tls_require_ciphers) |
83da1223 | 1086 | { |
17c76198 PP |
1087 | if (!expand_check_tlsvar(tls_require_ciphers)) |
1088 | return DEFER; | |
1089 | if (state->exp_tls_require_ciphers && *state->exp_tls_require_ciphers) | |
83da1223 | 1090 | { |
17c76198 PP |
1091 | DEBUG(D_tls) debug_printf("GnuTLS session cipher/priority \"%s\"\n", |
1092 | state->exp_tls_require_ciphers); | |
1093 | ||
1094 | rc = gnutls_priority_init(&state->priority_cache, | |
1095 | CS state->exp_tls_require_ciphers, &errpos); | |
1096 | want_default_priorities = FALSE; | |
1097 | p = state->exp_tls_require_ciphers; | |
83da1223 PH |
1098 | } |
1099 | } | |
17c76198 PP |
1100 | if (want_default_priorities) |
1101 | { | |
83e2f8a2 PP |
1102 | DEBUG(D_tls) |
1103 | debug_printf("GnuTLS using default session cipher/priority \"%s\"\n", | |
1104 | exim_default_gnutls_priority); | |
17c76198 PP |
1105 | rc = gnutls_priority_init(&state->priority_cache, |
1106 | exim_default_gnutls_priority, &errpos); | |
1107 | p = US exim_default_gnutls_priority; | |
1108 | } | |
83da1223 | 1109 | |
17c76198 PP |
1110 | exim_gnutls_err_check(string_sprintf( |
1111 | "gnutls_priority_init(%s) failed at offset %ld, \"%.6s..\"", | |
1112 | p, errpos - CS p, errpos)); | |
1113 | ||
1114 | rc = gnutls_priority_set(state->session, state->priority_cache); | |
1115 | exim_gnutls_err_check(US"gnutls_priority_set"); | |
1116 | ||
1117 | gnutls_db_set_cache_expiration(state->session, ssl_session_timeout); | |
1118 | ||
1119 | /* Reduce security in favour of increased compatibility, if the admin | |
1120 | decides to make that trade-off. */ | |
1121 | if (gnutls_compat_mode) | |
83da1223 | 1122 | { |
17c76198 PP |
1123 | #if LIBGNUTLS_VERSION_NUMBER >= 0x020104 |
1124 | DEBUG(D_tls) debug_printf("lowering GnuTLS security, compatibility mode\n"); | |
1125 | gnutls_session_enable_compatibility_mode(state->session); | |
1126 | #else | |
1127 | DEBUG(D_tls) debug_printf("Unable to set gnutls_compat_mode - GnuTLS version too old\n"); | |
1128 | #endif | |
83da1223 PH |
1129 | } |
1130 | ||
17c76198 | 1131 | *caller_state = state; |
17c76198 | 1132 | return OK; |
83da1223 PH |
1133 | } |
1134 | ||
1135 | ||
1136 | ||
059ec3d9 | 1137 | /************************************************* |
17c76198 | 1138 | * Extract peer information * |
059ec3d9 PH |
1139 | *************************************************/ |
1140 | ||
17c76198 | 1141 | /* Called from both server and client code. |
4fe99a6c PP |
1142 | Only this is allowed to set state->peerdn and state->have_set_peerdn |
1143 | and we use that to detect double-calls. | |
059ec3d9 | 1144 | |
75fe387d PP |
1145 | NOTE: the state blocks last while the TLS connection is up, which is fine |
1146 | for logging in the server side, but for the client side, we log after teardown | |
1147 | in src/deliver.c. While the session is up, we can twist about states and | |
1148 | repoint tls_* globals, but those variables used for logging or other variable | |
1149 | expansion that happens _after_ delivery need to have a longer life-time. | |
1150 | ||
1151 | So for those, we get the data from POOL_PERM; the re-invoke guard keeps us from | |
1152 | doing this more than once per generation of a state context. We set them in | |
1153 | the state context, and repoint tls_* to them. After the state goes away, the | |
1154 | tls_* copies of the pointers remain valid and client delivery logging is happy. | |
1155 | ||
1156 | tls_certificate_verified is a BOOL, so the tls_peerdn and tls_cipher issues | |
1157 | don't apply. | |
1158 | ||
059ec3d9 | 1159 | Arguments: |
17c76198 | 1160 | state exim_gnutls_state_st * |
059ec3d9 | 1161 | |
17c76198 | 1162 | Returns: OK/DEFER/FAIL |
059ec3d9 PH |
1163 | */ |
1164 | ||
17c76198 PP |
1165 | static int |
1166 | peer_status(exim_gnutls_state_st *state) | |
059ec3d9 | 1167 | { |
75fe387d | 1168 | uschar cipherbuf[256]; |
17c76198 | 1169 | const gnutls_datum *cert_list; |
75fe387d | 1170 | int old_pool, rc; |
17c76198 | 1171 | unsigned int cert_list_size = 0; |
4fe99a6c PP |
1172 | gnutls_protocol_t protocol; |
1173 | gnutls_cipher_algorithm_t cipher; | |
1174 | gnutls_kx_algorithm_t kx; | |
1175 | gnutls_mac_algorithm_t mac; | |
17c76198 PP |
1176 | gnutls_certificate_type_t ct; |
1177 | gnutls_x509_crt_t crt; | |
4fe99a6c | 1178 | uschar *p, *dn_buf; |
17c76198 | 1179 | size_t sz; |
059ec3d9 | 1180 | |
4fe99a6c | 1181 | if (state->have_set_peerdn) |
17c76198 | 1182 | return OK; |
4fe99a6c | 1183 | state->have_set_peerdn = TRUE; |
059ec3d9 | 1184 | |
4fe99a6c | 1185 | state->peerdn = NULL; |
059ec3d9 | 1186 | |
4fe99a6c PP |
1187 | /* tls_cipher */ |
1188 | cipher = gnutls_cipher_get(state->session); | |
1189 | protocol = gnutls_protocol_get_version(state->session); | |
1190 | mac = gnutls_mac_get(state->session); | |
1191 | kx = gnutls_kx_get(state->session); | |
1192 | ||
75fe387d | 1193 | string_format(cipherbuf, sizeof(cipherbuf), |
4fe99a6c PP |
1194 | "%s:%s:%d", |
1195 | gnutls_protocol_get_name(protocol), | |
1196 | gnutls_cipher_suite_get_name(kx, cipher, mac), | |
1197 | (int) gnutls_cipher_get_key_size(cipher) * 8); | |
1198 | ||
1199 | /* I don't see a way that spaces could occur, in the current GnuTLS | |
1200 | code base, but it was a concern in the old code and perhaps older GnuTLS | |
1201 | releases did return "TLS 1.0"; play it safe, just in case. */ | |
75fe387d | 1202 | for (p = cipherbuf; *p != '\0'; ++p) |
4fe99a6c PP |
1203 | if (isspace(*p)) |
1204 | *p = '-'; | |
75fe387d PP |
1205 | old_pool = store_pool; |
1206 | store_pool = POOL_PERM; | |
1207 | state->ciphersuite = string_copy(cipherbuf); | |
1208 | store_pool = old_pool; | |
817d9f57 | 1209 | state->tlsp->cipher = state->ciphersuite; |
4fe99a6c PP |
1210 | |
1211 | /* tls_peerdn */ | |
17c76198 | 1212 | cert_list = gnutls_certificate_get_peers(state->session, &cert_list_size); |
83da1223 | 1213 | |
17c76198 PP |
1214 | if (cert_list == NULL || cert_list_size == 0) |
1215 | { | |
17c76198 PP |
1216 | DEBUG(D_tls) debug_printf("TLS: no certificate from peer (%p & %d)\n", |
1217 | cert_list, cert_list_size); | |
1218 | if (state->verify_requirement == VERIFY_REQUIRED) | |
1219 | return tls_error(US"certificate verification failed", | |
1220 | "no certificate received from peer", state->host); | |
1221 | return OK; | |
1222 | } | |
059ec3d9 | 1223 | |
17c76198 PP |
1224 | ct = gnutls_certificate_type_get(state->session); |
1225 | if (ct != GNUTLS_CRT_X509) | |
059ec3d9 | 1226 | { |
17c76198 | 1227 | const char *ctn = gnutls_certificate_type_get_name(ct); |
17c76198 PP |
1228 | DEBUG(D_tls) |
1229 | debug_printf("TLS: peer cert not X.509 but instead \"%s\"\n", ctn); | |
1230 | if (state->verify_requirement == VERIFY_REQUIRED) | |
1231 | return tls_error(US"certificate verification not possible, unhandled type", | |
1232 | ctn, state->host); | |
1233 | return OK; | |
83da1223 | 1234 | } |
059ec3d9 | 1235 | |
17c76198 PP |
1236 | #define exim_gnutls_peer_err(Label) do { \ |
1237 | if (rc != GNUTLS_E_SUCCESS) { \ | |
1238 | DEBUG(D_tls) debug_printf("TLS: peer cert problem: %s: %s\n", (Label), gnutls_strerror(rc)); \ | |
1239 | if (state->verify_requirement == VERIFY_REQUIRED) { return tls_error((Label), gnutls_strerror(rc), state->host); } \ | |
1240 | return OK; } } while (0) | |
1241 | ||
9d1c15ef JH |
1242 | rc = import_cert(&cert_list[0], &crt); |
1243 | exim_gnutls_peer_err(US"cert 0"); | |
1244 | ||
1245 | state->tlsp->peercert = state->peercert = crt; | |
17c76198 | 1246 | |
17c76198 PP |
1247 | sz = 0; |
1248 | rc = gnutls_x509_crt_get_dn(crt, NULL, &sz); | |
1249 | if (rc != GNUTLS_E_SHORT_MEMORY_BUFFER) | |
83da1223 | 1250 | { |
17c76198 PP |
1251 | exim_gnutls_peer_err(US"getting size for cert DN failed"); |
1252 | return FAIL; /* should not happen */ | |
059ec3d9 | 1253 | } |
17c76198 PP |
1254 | dn_buf = store_get_perm(sz); |
1255 | rc = gnutls_x509_crt_get_dn(crt, CS dn_buf, &sz); | |
1256 | exim_gnutls_peer_err(US"failed to extract certificate DN [gnutls_x509_crt_get_dn(cert 0)]"); | |
9d1c15ef | 1257 | |
17c76198 PP |
1258 | state->peerdn = dn_buf; |
1259 | ||
1260 | return OK; | |
1261 | #undef exim_gnutls_peer_err | |
1262 | } | |
059ec3d9 | 1263 | |
059ec3d9 | 1264 | |
059ec3d9 | 1265 | |
059ec3d9 | 1266 | |
17c76198 PP |
1267 | /************************************************* |
1268 | * Verify peer certificate * | |
1269 | *************************************************/ | |
059ec3d9 | 1270 | |
17c76198 PP |
1271 | /* Called from both server and client code. |
1272 | *Should* be using a callback registered with | |
1273 | gnutls_certificate_set_verify_function() to fail the handshake if we dislike | |
1274 | the peer information, but that's too new for some OSes. | |
059ec3d9 | 1275 | |
17c76198 PP |
1276 | Arguments: |
1277 | state exim_gnutls_state_st * | |
1278 | error where to put an error message | |
059ec3d9 | 1279 | |
17c76198 PP |
1280 | Returns: |
1281 | FALSE if the session should be rejected | |
1282 | TRUE if the cert is okay or we just don't care | |
1283 | */ | |
059ec3d9 | 1284 | |
17c76198 PP |
1285 | static BOOL |
1286 | verify_certificate(exim_gnutls_state_st *state, const char **error) | |
1287 | { | |
1288 | int rc; | |
1289 | unsigned int verify; | |
1290 | ||
1291 | *error = NULL; | |
1292 | ||
0a92f87f | 1293 | if ((rc = peer_status(state)) != OK) |
e6060e2c | 1294 | { |
17c76198 | 1295 | verify = GNUTLS_CERT_INVALID; |
0a92f87f | 1296 | *error = "certificate not supplied"; |
17c76198 PP |
1297 | } |
1298 | else | |
17c76198 | 1299 | rc = gnutls_certificate_verify_peers2(state->session, &verify); |
e6060e2c | 1300 | |
17c76198 PP |
1301 | /* Handle the result of verification. INVALID seems to be set as well |
1302 | as REVOKED, but leave the test for both. */ | |
059ec3d9 | 1303 | |
0a92f87f | 1304 | if (rc < 0 || verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED)) |
17c76198 PP |
1305 | { |
1306 | state->peer_cert_verified = FALSE; | |
0a92f87f JH |
1307 | if (!*error) |
1308 | *error = verify & GNUTLS_CERT_REVOKED | |
1309 | ? "certificate revoked" : "certificate invalid"; | |
059ec3d9 | 1310 | |
17c76198 PP |
1311 | DEBUG(D_tls) |
1312 | debug_printf("TLS certificate verification failed (%s): peerdn=%s\n", | |
4fe99a6c | 1313 | *error, state->peerdn ? state->peerdn : US"<unset>"); |
059ec3d9 | 1314 | |
17c76198 PP |
1315 | if (state->verify_requirement == VERIFY_REQUIRED) |
1316 | { | |
1317 | gnutls_alert_send(state->session, GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE); | |
1318 | return FALSE; | |
1319 | } | |
1320 | DEBUG(D_tls) | |
4789da3a | 1321 | debug_printf("TLS verify failure overridden (host in tls_try_verify_hosts)\n"); |
17c76198 PP |
1322 | } |
1323 | else | |
1324 | { | |
1325 | state->peer_cert_verified = TRUE; | |
4fe99a6c PP |
1326 | DEBUG(D_tls) debug_printf("TLS certificate verified: peerdn=%s\n", |
1327 | state->peerdn ? state->peerdn : US"<unset>"); | |
17c76198 | 1328 | } |
059ec3d9 | 1329 | |
817d9f57 | 1330 | state->tlsp->peerdn = state->peerdn; |
059ec3d9 | 1331 | |
17c76198 PP |
1332 | return TRUE; |
1333 | } | |
059ec3d9 | 1334 | |
17c76198 PP |
1335 | |
1336 | ||
1337 | ||
1338 | /* ------------------------------------------------------------------------ */ | |
1339 | /* Callbacks */ | |
1340 | ||
1341 | /* Logging function which can be registered with | |
1342 | * gnutls_global_set_log_function() | |
1343 | * gnutls_global_set_log_level() 0..9 | |
1344 | */ | |
af3498d6 | 1345 | #if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0 |
059ec3d9 | 1346 | static void |
17c76198 | 1347 | exim_gnutls_logger_cb(int level, const char *message) |
059ec3d9 | 1348 | { |
8c79eebf PP |
1349 | size_t len = strlen(message); |
1350 | if (len < 1) | |
1351 | { | |
1352 | DEBUG(D_tls) debug_printf("GnuTLS<%d> empty debug message\n", level); | |
1353 | return; | |
1354 | } | |
1355 | DEBUG(D_tls) debug_printf("GnuTLS<%d>: %s%s", level, message, | |
1356 | message[len-1] == '\n' ? "" : "\n"); | |
17c76198 | 1357 | } |
af3498d6 | 1358 | #endif |
059ec3d9 | 1359 | |
059ec3d9 | 1360 | |
17c76198 PP |
1361 | /* Called after client hello, should handle SNI work. |
1362 | This will always set tls_sni (state->received_sni) if available, | |
1363 | and may trigger presenting different certificates, | |
1364 | if state->trigger_sni_changes is TRUE. | |
059ec3d9 | 1365 | |
17c76198 PP |
1366 | Should be registered with |
1367 | gnutls_handshake_set_post_client_hello_function() | |
059ec3d9 | 1368 | |
17c76198 PP |
1369 | "This callback must return 0 on success or a gnutls error code to terminate the |
1370 | handshake.". | |
059ec3d9 | 1371 | |
17c76198 PP |
1372 | For inability to get SNI information, we return 0. |
1373 | We only return non-zero if re-setup failed. | |
817d9f57 | 1374 | Only used for server-side TLS. |
17c76198 | 1375 | */ |
44bbabb5 | 1376 | |
17c76198 PP |
1377 | static int |
1378 | exim_sni_handling_cb(gnutls_session_t session) | |
1379 | { | |
1380 | char sni_name[MAX_HOST_LEN]; | |
1381 | size_t data_len = MAX_HOST_LEN; | |
817d9f57 | 1382 | exim_gnutls_state_st *state = &state_server; |
17c76198 PP |
1383 | unsigned int sni_type; |
1384 | int rc, old_pool; | |
1385 | ||
1386 | rc = gnutls_server_name_get(session, sni_name, &data_len, &sni_type, 0); | |
b34fc30c PP |
1387 | if (rc != GNUTLS_E_SUCCESS) |
1388 | { | |
1389 | DEBUG(D_tls) { | |
1390 | if (rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) | |
1391 | debug_printf("TLS: no SNI presented in handshake.\n"); | |
1392 | else | |
1393 | debug_printf("TLS failure: gnutls_server_name_get(): %s [%d]\n", | |
1394 | gnutls_strerror(rc), rc); | |
1395 | }; | |
1396 | return 0; | |
1397 | } | |
1398 | ||
17c76198 PP |
1399 | if (sni_type != GNUTLS_NAME_DNS) |
1400 | { | |
1401 | DEBUG(D_tls) debug_printf("TLS: ignoring SNI of unhandled type %u\n", sni_type); | |
1402 | return 0; | |
1403 | } | |
44bbabb5 | 1404 | |
17c76198 PP |
1405 | /* We now have a UTF-8 string in sni_name */ |
1406 | old_pool = store_pool; | |
1407 | store_pool = POOL_PERM; | |
1408 | state->received_sni = string_copyn(US sni_name, data_len); | |
1409 | store_pool = old_pool; | |
1410 | ||
1411 | /* We set this one now so that variable expansions below will work */ | |
817d9f57 | 1412 | state->tlsp->sni = state->received_sni; |
17c76198 PP |
1413 | |
1414 | DEBUG(D_tls) debug_printf("Received TLS SNI \"%s\"%s\n", sni_name, | |
1415 | state->trigger_sni_changes ? "" : " (unused for certificate selection)"); | |
1416 | ||
1417 | if (!state->trigger_sni_changes) | |
1418 | return 0; | |
1419 | ||
1420 | rc = tls_expand_session_files(state); | |
1421 | if (rc != OK) | |
1422 | { | |
1423 | /* If the setup of certs/etc failed before handshake, TLS would not have | |
1424 | been offered. The best we can do now is abort. */ | |
1425 | return GNUTLS_E_APPLICATION_ERROR_MIN; | |
1426 | } | |
1427 | ||
1365611d PP |
1428 | rc = tls_set_remaining_x509(state); |
1429 | if (rc != OK) return GNUTLS_E_APPLICATION_ERROR_MIN; | |
1430 | ||
1431 | return 0; | |
059ec3d9 PH |
1432 | } |
1433 | ||
1434 | ||
1435 | ||
17c76198 PP |
1436 | |
1437 | /* ------------------------------------------------------------------------ */ | |
1438 | /* Exported functions */ | |
1439 | ||
1440 | ||
1441 | ||
1442 | ||
059ec3d9 PH |
1443 | /************************************************* |
1444 | * Start a TLS session in a server * | |
1445 | *************************************************/ | |
1446 | ||
1447 | /* This is called when Exim is running as a server, after having received | |
1448 | the STARTTLS command. It must respond to that command, and then negotiate | |
1449 | a TLS session. | |
1450 | ||
1451 | Arguments: | |
83da1223 | 1452 | require_ciphers list of allowed ciphers or NULL |
059ec3d9 PH |
1453 | |
1454 | Returns: OK on success | |
1455 | DEFER for errors before the start of the negotiation | |
1456 | FAIL for errors during the negotation; the server can't | |
1457 | continue running. | |
1458 | */ | |
1459 | ||
1460 | int | |
17c76198 | 1461 | tls_server_start(const uschar *require_ciphers) |
059ec3d9 PH |
1462 | { |
1463 | int rc; | |
7199e1ee | 1464 | const char *error; |
17c76198 | 1465 | exim_gnutls_state_st *state = NULL; |
059ec3d9 PH |
1466 | |
1467 | /* Check for previous activation */ | |
817d9f57 | 1468 | if (tls_in.active >= 0) |
059ec3d9 | 1469 | { |
17c76198 | 1470 | tls_error(US"STARTTLS received after TLS started", "", NULL); |
059ec3d9 PH |
1471 | smtp_printf("554 Already in TLS\r\n"); |
1472 | return FAIL; | |
1473 | } | |
1474 | ||
1475 | /* Initialize the library. If it fails, it will already have logged the error | |
1476 | and sent an SMTP response. */ | |
1477 | ||
17c76198 | 1478 | DEBUG(D_tls) debug_printf("initialising GnuTLS as a server\n"); |
059ec3d9 | 1479 | |
17c76198 PP |
1480 | rc = tls_init(NULL, tls_certificate, tls_privatekey, |
1481 | NULL, tls_verify_certificates, tls_crl, | |
1482 | require_ciphers, &state); | |
059ec3d9 PH |
1483 | if (rc != OK) return rc; |
1484 | ||
059ec3d9 PH |
1485 | /* If this is a host for which certificate verification is mandatory or |
1486 | optional, set up appropriately. */ | |
1487 | ||
059ec3d9 | 1488 | if (verify_check_host(&tls_verify_hosts) == OK) |
17c76198 PP |
1489 | { |
1490 | DEBUG(D_tls) debug_printf("TLS: a client certificate will be required.\n"); | |
1491 | state->verify_requirement = VERIFY_REQUIRED; | |
1492 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUIRE); | |
1493 | } | |
059ec3d9 | 1494 | else if (verify_check_host(&tls_try_verify_hosts) == OK) |
17c76198 PP |
1495 | { |
1496 | DEBUG(D_tls) debug_printf("TLS: a client certificate will be requested but not required.\n"); | |
1497 | state->verify_requirement = VERIFY_OPTIONAL; | |
1498 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUEST); | |
1499 | } | |
1500 | else | |
1501 | { | |
1502 | DEBUG(D_tls) debug_printf("TLS: a client certificate will not be requested.\n"); | |
1503 | state->verify_requirement = VERIFY_NONE; | |
1504 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_IGNORE); | |
1505 | } | |
059ec3d9 | 1506 | |
17c76198 PP |
1507 | /* Register SNI handling; always, even if not in tls_certificate, so that the |
1508 | expansion variable $tls_sni is always available. */ | |
059ec3d9 | 1509 | |
17c76198 PP |
1510 | gnutls_handshake_set_post_client_hello_function(state->session, |
1511 | exim_sni_handling_cb); | |
059ec3d9 PH |
1512 | |
1513 | /* Set context and tell client to go ahead, except in the case of TLS startup | |
1514 | on connection, where outputting anything now upsets the clients and tends to | |
1515 | make them disconnect. We need to have an explicit fflush() here, to force out | |
1516 | the response. Other smtp_printf() calls do not need it, because in non-TLS | |
1517 | mode, the fflush() happens when smtp_getc() is called. */ | |
1518 | ||
817d9f57 | 1519 | if (!state->tlsp->on_connect) |
059ec3d9 PH |
1520 | { |
1521 | smtp_printf("220 TLS go ahead\r\n"); | |
9d1c15ef | 1522 | fflush(smtp_out); |
059ec3d9 PH |
1523 | } |
1524 | ||
1525 | /* Now negotiate the TLS session. We put our own timer on it, since it seems | |
1526 | that the GnuTLS library doesn't. */ | |
1527 | ||
17c76198 PP |
1528 | gnutls_transport_set_ptr2(state->session, |
1529 | (gnutls_transport_ptr)fileno(smtp_in), | |
1530 | (gnutls_transport_ptr)fileno(smtp_out)); | |
1531 | state->fd_in = fileno(smtp_in); | |
1532 | state->fd_out = fileno(smtp_out); | |
059ec3d9 PH |
1533 | |
1534 | sigalrm_seen = FALSE; | |
1535 | if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); | |
17c76198 PP |
1536 | do |
1537 | { | |
1538 | rc = gnutls_handshake(state->session); | |
619b2b25 PP |
1539 | } while ((rc == GNUTLS_E_AGAIN) || |
1540 | (rc == GNUTLS_E_INTERRUPTED && !sigalrm_seen)); | |
059ec3d9 PH |
1541 | alarm(0); |
1542 | ||
17c76198 | 1543 | if (rc != GNUTLS_E_SUCCESS) |
059ec3d9 | 1544 | { |
17c76198 PP |
1545 | tls_error(US"gnutls_handshake", |
1546 | sigalrm_seen ? "timed out" : gnutls_strerror(rc), NULL); | |
059ec3d9 PH |
1547 | /* It seems that, except in the case of a timeout, we have to close the |
1548 | connection right here; otherwise if the other end is running OpenSSL it hangs | |
1549 | until the server times out. */ | |
1550 | ||
1551 | if (!sigalrm_seen) | |
1552 | { | |
f1e894f3 PH |
1553 | (void)fclose(smtp_out); |
1554 | (void)fclose(smtp_in); | |
059ec3d9 PH |
1555 | } |
1556 | ||
1557 | return FAIL; | |
1558 | } | |
1559 | ||
1560 | DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n"); | |
1561 | ||
17c76198 PP |
1562 | /* Verify after the fact */ |
1563 | ||
9d1c15ef JH |
1564 | if ( state->verify_requirement != VERIFY_NONE |
1565 | && !verify_certificate(state, &error)) | |
059ec3d9 | 1566 | { |
9d1c15ef | 1567 | if (state->verify_requirement != VERIFY_OPTIONAL) |
17c76198 | 1568 | { |
9d1c15ef JH |
1569 | tls_error(US"certificate verification failed", error, NULL); |
1570 | return FAIL; | |
17c76198 | 1571 | } |
9d1c15ef JH |
1572 | DEBUG(D_tls) |
1573 | debug_printf("TLS: continuing on only because verification was optional, after: %s\n", | |
1574 | error); | |
059ec3d9 PH |
1575 | } |
1576 | ||
17c76198 PP |
1577 | /* Figure out peer DN, and if authenticated, etc. */ |
1578 | ||
1579 | rc = peer_status(state); | |
1580 | if (rc != OK) return rc; | |
1581 | ||
1582 | /* Sets various Exim expansion variables; always safe within server */ | |
1583 | ||
9d1c15ef | 1584 | extract_exim_vars_from_tls_state(state); |
059ec3d9 PH |
1585 | |
1586 | /* TLS has been set up. Adjust the input functions to read via TLS, | |
1587 | and initialize appropriately. */ | |
1588 | ||
17c76198 | 1589 | state->xfer_buffer = store_malloc(ssl_xfer_buffer_size); |
059ec3d9 PH |
1590 | |
1591 | receive_getc = tls_getc; | |
1592 | receive_ungetc = tls_ungetc; | |
1593 | receive_feof = tls_feof; | |
1594 | receive_ferror = tls_ferror; | |
58eb016e | 1595 | receive_smtp_buffered = tls_smtp_buffered; |
059ec3d9 | 1596 | |
059ec3d9 PH |
1597 | return OK; |
1598 | } | |
1599 | ||
1600 | ||
1601 | ||
1602 | ||
1603 | /************************************************* | |
1604 | * Start a TLS session in a client * | |
1605 | *************************************************/ | |
1606 | ||
1607 | /* Called from the smtp transport after STARTTLS has been accepted. | |
1608 | ||
1609 | Arguments: | |
1610 | fd the fd of the connection | |
1611 | host connected host (for messages) | |
83da1223 | 1612 | addr the first address (not used) |
059ec3d9 PH |
1613 | certificate certificate file |
1614 | privatekey private key file | |
3f0945ff | 1615 | sni TLS SNI to send to remote host |
059ec3d9 PH |
1616 | verify_certs file for certificate verify |
1617 | verify_crl CRL for verify | |
83da1223 | 1618 | require_ciphers list of allowed ciphers or NULL |
2b4a568d | 1619 | hosts_require_ocsp hosts for which to request certificate-status (OCSP) |
54c90be1 | 1620 | dh_min_bits minimum number of bits acceptable in server's DH prime |
059ec3d9 | 1621 | timeout startup timeout |
2b4a568d JH |
1622 | verify_hosts mandatory client verification |
1623 | try_verify_hosts optional client verification | |
059ec3d9 PH |
1624 | |
1625 | Returns: OK/DEFER/FAIL (because using common functions), | |
1626 | but for a client, DEFER and FAIL have the same meaning | |
1627 | */ | |
1628 | ||
1629 | int | |
17c76198 | 1630 | tls_client_start(int fd, host_item *host, |
f5d78688 | 1631 | address_item *addr ARG_UNUSED, |
17c76198 PP |
1632 | uschar *certificate, uschar *privatekey, uschar *sni, |
1633 | uschar *verify_certs, uschar *verify_crl, | |
f5d78688 JH |
1634 | uschar *require_ciphers, |
1635 | #ifdef EXPERIMENTAL_OCSP | |
2b4a568d | 1636 | uschar *hosts_require_ocsp, |
f5d78688 | 1637 | #endif |
a63be306 WB |
1638 | int dh_min_bits, int timeout, |
1639 | uschar *verify_hosts, uschar *try_verify_hosts) | |
059ec3d9 | 1640 | { |
059ec3d9 | 1641 | int rc; |
17c76198 PP |
1642 | const char *error; |
1643 | exim_gnutls_state_st *state = NULL; | |
2b4a568d JH |
1644 | #ifdef EXPERIMENTAL_OCSP |
1645 | BOOL require_ocsp = verify_check_this_host(&hosts_require_ocsp, | |
1646 | NULL, host->name, host->address, NULL) == OK; | |
1647 | #endif | |
059ec3d9 | 1648 | |
17c76198 | 1649 | DEBUG(D_tls) debug_printf("initialising GnuTLS as a client on fd %d\n", fd); |
059ec3d9 | 1650 | |
2b4a568d JH |
1651 | if ((rc = tls_init(host, certificate, privatekey, |
1652 | sni, verify_certs, verify_crl, require_ciphers, &state)) != OK) | |
1653 | return rc; | |
059ec3d9 | 1654 | |
54c90be1 PP |
1655 | if (dh_min_bits < EXIM_CLIENT_DH_MIN_MIN_BITS) |
1656 | { | |
1657 | DEBUG(D_tls) | |
1658 | debug_printf("WARNING: tls_dh_min_bits far too low, clamping %d up to %d\n", | |
1659 | dh_min_bits, EXIM_CLIENT_DH_MIN_MIN_BITS); | |
1660 | dh_min_bits = EXIM_CLIENT_DH_MIN_MIN_BITS; | |
1661 | } | |
1662 | ||
1663 | DEBUG(D_tls) debug_printf("Setting D-H prime minimum acceptable bits to %d\n", | |
1664 | dh_min_bits); | |
1665 | gnutls_dh_set_prime_bits(state->session, dh_min_bits); | |
83da1223 | 1666 | |
2b4a568d JH |
1667 | /* Stick to the old behaviour for compatibility if tls_verify_certificates is |
1668 | set but both tls_verify_hosts and tls_try_verify_hosts are unset. Check only | |
1669 | the specified host patterns if one of them is defined */ | |
1670 | ||
1671 | if (( state->exp_tls_verify_certificates | |
1672 | && !verify_hosts | |
1673 | && !try_verify_hosts | |
1674 | ) | |
1675 | || | |
1676 | verify_check_host(&verify_hosts) == OK | |
1677 | ) | |
17c76198 | 1678 | { |
52f93eed WB |
1679 | DEBUG(D_tls) debug_printf("TLS: server certificate verification required.\n"); |
1680 | state->verify_requirement = VERIFY_REQUIRED; | |
1681 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUIRE); | |
1682 | } | |
1683 | else if (verify_check_host(&try_verify_hosts) == OK) | |
1684 | { | |
1685 | DEBUG(D_tls) debug_printf("TLS: server certificate verification optional.\n"); | |
1686 | state->verify_requirement = VERIFY_OPTIONAL; | |
17c76198 PP |
1687 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUEST); |
1688 | } | |
1689 | else | |
1690 | { | |
52f93eed WB |
1691 | DEBUG(D_tls) debug_printf("TLS: server certificate verification not required.\n"); |
1692 | state->verify_requirement = VERIFY_NONE; | |
1693 | gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_IGNORE); | |
17c76198 | 1694 | } |
059ec3d9 | 1695 | |
2b4a568d | 1696 | #ifdef EXPERIMENTAL_OCSP /* since GnuTLS 3.1.3 */ |
9d1c15ef JH |
1697 | if (require_ocsp) |
1698 | { | |
1699 | DEBUG(D_tls) debug_printf("TLS: will request OCSP stapling\n"); | |
1700 | rc = gnutls_ocsp_status_request_enable_client(state->session, | |
1701 | NULL, 0, NULL); | |
1702 | if (rc != OK) | |
1703 | return tls_error(US"cert-status-req", | |
1704 | gnutls_strerror(rc), state->host); | |
1705 | } | |
2b4a568d JH |
1706 | #endif |
1707 | ||
17c76198 PP |
1708 | gnutls_transport_set_ptr(state->session, (gnutls_transport_ptr)fd); |
1709 | state->fd_in = fd; | |
1710 | state->fd_out = fd; | |
059ec3d9 | 1711 | |
9d1c15ef | 1712 | DEBUG(D_tls) debug_printf("about to gnutls_handshake\n"); |
059ec3d9 PH |
1713 | /* There doesn't seem to be a built-in timeout on connection. */ |
1714 | ||
1715 | sigalrm_seen = FALSE; | |
1716 | alarm(timeout); | |
17c76198 PP |
1717 | do |
1718 | { | |
1719 | rc = gnutls_handshake(state->session); | |
619b2b25 PP |
1720 | } while ((rc == GNUTLS_E_AGAIN) || |
1721 | (rc == GNUTLS_E_INTERRUPTED && !sigalrm_seen)); | |
059ec3d9 PH |
1722 | alarm(0); |
1723 | ||
4fe99a6c PP |
1724 | if (rc != GNUTLS_E_SUCCESS) |
1725 | return tls_error(US"gnutls_handshake", | |
1726 | sigalrm_seen ? "timed out" : gnutls_strerror(rc), state->host); | |
1727 | ||
17c76198 | 1728 | DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n"); |
059ec3d9 | 1729 | |
17c76198 | 1730 | /* Verify late */ |
059ec3d9 | 1731 | |
17c76198 PP |
1732 | if (state->verify_requirement != VERIFY_NONE && |
1733 | !verify_certificate(state, &error)) | |
1734 | return tls_error(US"certificate verification failed", error, state->host); | |
059ec3d9 | 1735 | |
2b4a568d JH |
1736 | #ifdef EXPERIMENTAL_OCSP |
1737 | if (require_ocsp) | |
1738 | { | |
1739 | DEBUG(D_tls) | |
1740 | { | |
1741 | gnutls_datum_t stapling; | |
1742 | gnutls_ocsp_resp_t resp; | |
1743 | gnutls_datum_t printed; | |
1744 | if ( (rc= gnutls_ocsp_status_request_get(state->session, &stapling)) == 0 | |
1745 | && (rc= gnutls_ocsp_resp_init(&resp)) == 0 | |
1746 | && (rc= gnutls_ocsp_resp_import(resp, &stapling)) == 0 | |
1747 | && (rc= gnutls_ocsp_resp_print(resp, GNUTLS_OCSP_PRINT_FULL, &printed)) == 0 | |
1748 | ) | |
1749 | { | |
1750 | fprintf(stderr, "%.4096s", printed.data); | |
1751 | gnutls_free(printed.data); | |
1752 | } | |
1753 | else | |
1754 | (void) tls_error(US"ocsp decode", gnutls_strerror(rc), state->host); | |
1755 | } | |
1756 | ||
1757 | fprintf(stderr, "%s: checking ocsp\n", __FUNCTION__); | |
1758 | if (gnutls_ocsp_status_request_is_checked(state->session, 0) == 0) | |
1759 | return tls_error(US"certificate status check failed", NULL, state->host); | |
1760 | DEBUG(D_tls) debug_printf("Passed OCSP checking\n"); | |
1761 | } | |
1762 | #endif | |
1763 | ||
17c76198 | 1764 | /* Figure out peer DN, and if authenticated, etc. */ |
059ec3d9 | 1765 | |
2b4a568d JH |
1766 | if ((rc = peer_status(state)) != OK) |
1767 | return rc; | |
059ec3d9 | 1768 | |
4fe99a6c | 1769 | /* Sets various Exim expansion variables; may need to adjust for ACL callouts */ |
059ec3d9 | 1770 | |
9d1c15ef | 1771 | extract_exim_vars_from_tls_state(state); |
059ec3d9 | 1772 | |
059ec3d9 PH |
1773 | return OK; |
1774 | } | |
1775 | ||
1776 | ||
1777 | ||
17c76198 | 1778 | |
059ec3d9 | 1779 | /************************************************* |
17c76198 | 1780 | * Close down a TLS session * |
059ec3d9 PH |
1781 | *************************************************/ |
1782 | ||
17c76198 PP |
1783 | /* This is also called from within a delivery subprocess forked from the |
1784 | daemon, to shut down the TLS library, without actually doing a shutdown (which | |
1785 | would tamper with the TLS session in the parent process). | |
059ec3d9 | 1786 | |
17c76198 PP |
1787 | Arguments: TRUE if gnutls_bye is to be called |
1788 | Returns: nothing | |
059ec3d9 PH |
1789 | */ |
1790 | ||
17c76198 | 1791 | void |
817d9f57 | 1792 | tls_close(BOOL is_server, BOOL shutdown) |
059ec3d9 | 1793 | { |
817d9f57 | 1794 | exim_gnutls_state_st *state = is_server ? &state_server : &state_client; |
059ec3d9 | 1795 | |
389ca47a | 1796 | if (!state->tlsp || state->tlsp->active < 0) return; /* TLS was not active */ |
17c76198 PP |
1797 | |
1798 | if (shutdown) | |
1799 | { | |
1800 | DEBUG(D_tls) debug_printf("tls_close(): shutting down TLS\n"); | |
1801 | gnutls_bye(state->session, GNUTLS_SHUT_WR); | |
1802 | } | |
1803 | ||
1804 | gnutls_deinit(state->session); | |
1805 | ||
389ca47a | 1806 | state->tlsp->active = -1; |
17c76198 PP |
1807 | memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init)); |
1808 | ||
1809 | if ((state_server.session == NULL) && (state_client.session == NULL)) | |
1810 | { | |
1811 | gnutls_global_deinit(); | |
1812 | exim_gnutls_base_init_done = FALSE; | |
1813 | } | |
7199e1ee | 1814 | |
059ec3d9 PH |
1815 | } |
1816 | ||
1817 | ||
1818 | ||
17c76198 | 1819 | |
059ec3d9 PH |
1820 | /************************************************* |
1821 | * TLS version of getc * | |
1822 | *************************************************/ | |
1823 | ||
1824 | /* This gets the next byte from the TLS input buffer. If the buffer is empty, | |
1825 | it refills the buffer via the GnuTLS reading function. | |
817d9f57 | 1826 | Only used by the server-side TLS. |
059ec3d9 | 1827 | |
17c76198 PP |
1828 | This feeds DKIM and should be used for all message-body reads. |
1829 | ||
059ec3d9 PH |
1830 | Arguments: none |
1831 | Returns: the next character or EOF | |
1832 | */ | |
1833 | ||
1834 | int | |
1835 | tls_getc(void) | |
1836 | { | |
817d9f57 | 1837 | exim_gnutls_state_st *state = &state_server; |
17c76198 | 1838 | if (state->xfer_buffer_lwm >= state->xfer_buffer_hwm) |
059ec3d9 | 1839 | { |
17c76198 | 1840 | ssize_t inbytes; |
059ec3d9 | 1841 | |
17c76198 PP |
1842 | DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(%p, %p, %u)\n", |
1843 | state->session, state->xfer_buffer, ssl_xfer_buffer_size); | |
059ec3d9 PH |
1844 | |
1845 | if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); | |
17c76198 | 1846 | inbytes = gnutls_record_recv(state->session, state->xfer_buffer, |
059ec3d9 PH |
1847 | ssl_xfer_buffer_size); |
1848 | alarm(0); | |
1849 | ||
1850 | /* A zero-byte return appears to mean that the TLS session has been | |
1851 | closed down, not that the socket itself has been closed down. Revert to | |
1852 | non-TLS handling. */ | |
1853 | ||
1854 | if (inbytes == 0) | |
1855 | { | |
1856 | DEBUG(D_tls) debug_printf("Got TLS_EOF\n"); | |
1857 | ||
1858 | receive_getc = smtp_getc; | |
1859 | receive_ungetc = smtp_ungetc; | |
1860 | receive_feof = smtp_feof; | |
1861 | receive_ferror = smtp_ferror; | |
58eb016e | 1862 | receive_smtp_buffered = smtp_buffered; |
059ec3d9 | 1863 | |
17c76198 PP |
1864 | gnutls_deinit(state->session); |
1865 | state->session = NULL; | |
817d9f57 JH |
1866 | state->tlsp->active = -1; |
1867 | state->tlsp->bits = 0; | |
1868 | state->tlsp->certificate_verified = FALSE; | |
9d1c15ef | 1869 | tls_channelbinding_b64 = NULL; |
817d9f57 | 1870 | state->tlsp->cipher = NULL; |
9d1c15ef | 1871 | state->tlsp->peercert = NULL; |
817d9f57 | 1872 | state->tlsp->peerdn = NULL; |
059ec3d9 PH |
1873 | |
1874 | return smtp_getc(); | |
1875 | } | |
1876 | ||
1877 | /* Handle genuine errors */ | |
1878 | ||
1879 | else if (inbytes < 0) | |
1880 | { | |
17c76198 PP |
1881 | record_io_error(state, (int) inbytes, US"recv", NULL); |
1882 | state->xfer_error = 1; | |
059ec3d9 PH |
1883 | return EOF; |
1884 | } | |
80a47a2c | 1885 | #ifndef DISABLE_DKIM |
17c76198 | 1886 | dkim_exim_verify_feed(state->xfer_buffer, inbytes); |
80a47a2c | 1887 | #endif |
17c76198 PP |
1888 | state->xfer_buffer_hwm = (int) inbytes; |
1889 | state->xfer_buffer_lwm = 0; | |
059ec3d9 PH |
1890 | } |
1891 | ||
059ec3d9 PH |
1892 | /* Something in the buffer; return next uschar */ |
1893 | ||
17c76198 | 1894 | return state->xfer_buffer[state->xfer_buffer_lwm++]; |
059ec3d9 PH |
1895 | } |
1896 | ||
1897 | ||
1898 | ||
17c76198 | 1899 | |
059ec3d9 PH |
1900 | /************************************************* |
1901 | * Read bytes from TLS channel * | |
1902 | *************************************************/ | |
1903 | ||
17c76198 PP |
1904 | /* This does not feed DKIM, so if the caller uses this for reading message body, |
1905 | then the caller must feed DKIM. | |
817d9f57 | 1906 | |
059ec3d9 PH |
1907 | Arguments: |
1908 | buff buffer of data | |
1909 | len size of buffer | |
1910 | ||
1911 | Returns: the number of bytes read | |
1912 | -1 after a failed read | |
1913 | */ | |
1914 | ||
1915 | int | |
817d9f57 | 1916 | tls_read(BOOL is_server, uschar *buff, size_t len) |
059ec3d9 | 1917 | { |
817d9f57 | 1918 | exim_gnutls_state_st *state = is_server ? &state_server : &state_client; |
17c76198 | 1919 | ssize_t inbytes; |
059ec3d9 | 1920 | |
17c76198 PP |
1921 | if (len > INT_MAX) |
1922 | len = INT_MAX; | |
059ec3d9 | 1923 | |
17c76198 PP |
1924 | if (state->xfer_buffer_lwm < state->xfer_buffer_hwm) |
1925 | DEBUG(D_tls) | |
1926 | debug_printf("*** PROBABLY A BUG *** " \ | |
1927 | "tls_read() called with data in the tls_getc() buffer, %d ignored\n", | |
1928 | state->xfer_buffer_hwm - state->xfer_buffer_lwm); | |
1929 | ||
1930 | DEBUG(D_tls) | |
1931 | debug_printf("Calling gnutls_record_recv(%p, %p, " SIZE_T_FMT ")\n", | |
1932 | state->session, buff, len); | |
1933 | ||
1934 | inbytes = gnutls_record_recv(state->session, buff, len); | |
059ec3d9 PH |
1935 | if (inbytes > 0) return inbytes; |
1936 | if (inbytes == 0) | |
1937 | { | |
1938 | DEBUG(D_tls) debug_printf("Got TLS_EOF\n"); | |
1939 | } | |
17c76198 | 1940 | else record_io_error(state, (int)inbytes, US"recv", NULL); |
059ec3d9 PH |
1941 | |
1942 | return -1; | |
1943 | } | |
1944 | ||
1945 | ||
1946 | ||
17c76198 | 1947 | |
059ec3d9 PH |
1948 | /************************************************* |
1949 | * Write bytes down TLS channel * | |
1950 | *************************************************/ | |
1951 | ||
1952 | /* | |
1953 | Arguments: | |
817d9f57 | 1954 | is_server channel specifier |
059ec3d9 PH |
1955 | buff buffer of data |
1956 | len number of bytes | |
1957 | ||
1958 | Returns: the number of bytes after a successful write, | |
1959 | -1 after a failed write | |
1960 | */ | |
1961 | ||
1962 | int | |
817d9f57 | 1963 | tls_write(BOOL is_server, const uschar *buff, size_t len) |
059ec3d9 | 1964 | { |
17c76198 PP |
1965 | ssize_t outbytes; |
1966 | size_t left = len; | |
817d9f57 | 1967 | exim_gnutls_state_st *state = is_server ? &state_server : &state_client; |
059ec3d9 | 1968 | |
17c76198 | 1969 | DEBUG(D_tls) debug_printf("tls_do_write(%p, " SIZE_T_FMT ")\n", buff, left); |
059ec3d9 PH |
1970 | while (left > 0) |
1971 | { | |
17c76198 PP |
1972 | DEBUG(D_tls) debug_printf("gnutls_record_send(SSL, %p, " SIZE_T_FMT ")\n", |
1973 | buff, left); | |
1974 | outbytes = gnutls_record_send(state->session, buff, left); | |
059ec3d9 | 1975 | |
17c76198 | 1976 | DEBUG(D_tls) debug_printf("outbytes=" SSIZE_T_FMT "\n", outbytes); |
059ec3d9 PH |
1977 | if (outbytes < 0) |
1978 | { | |
17c76198 | 1979 | record_io_error(state, outbytes, US"send", NULL); |
059ec3d9 PH |
1980 | return -1; |
1981 | } | |
1982 | if (outbytes == 0) | |
1983 | { | |
17c76198 | 1984 | record_io_error(state, 0, US"send", US"TLS channel closed on write"); |
059ec3d9 PH |
1985 | return -1; |
1986 | } | |
1987 | ||
1988 | left -= outbytes; | |
1989 | buff += outbytes; | |
1990 | } | |
1991 | ||
17c76198 PP |
1992 | if (len > INT_MAX) |
1993 | { | |
1994 | DEBUG(D_tls) | |
1995 | debug_printf("Whoops! Wrote more bytes (" SIZE_T_FMT ") than INT_MAX\n", | |
1996 | len); | |
1997 | len = INT_MAX; | |
1998 | } | |
1999 | ||
2000 | return (int) len; | |
059ec3d9 PH |
2001 | } |
2002 | ||
2003 | ||
2004 | ||
17c76198 | 2005 | |
059ec3d9 | 2006 | /************************************************* |
17c76198 | 2007 | * Random number generation * |
059ec3d9 PH |
2008 | *************************************************/ |
2009 | ||
17c76198 PP |
2010 | /* Pseudo-random number generation. The result is not expected to be |
2011 | cryptographically strong but not so weak that someone will shoot themselves | |
2012 | in the foot using it as a nonce in input in some email header scheme or | |
2013 | whatever weirdness they'll twist this into. The result should handle fork() | |
2014 | and avoid repeating sequences. OpenSSL handles that for us. | |
059ec3d9 | 2015 | |
17c76198 PP |
2016 | Arguments: |
2017 | max range maximum | |
2018 | Returns a random number in range [0, max-1] | |
059ec3d9 PH |
2019 | */ |
2020 | ||
af3498d6 | 2021 | #ifdef HAVE_GNUTLS_RND |
17c76198 PP |
2022 | int |
2023 | vaguely_random_number(int max) | |
059ec3d9 | 2024 | { |
17c76198 PP |
2025 | unsigned int r; |
2026 | int i, needed_len; | |
2027 | uschar *p; | |
2028 | uschar smallbuf[sizeof(r)]; | |
2029 | ||
2030 | if (max <= 1) | |
2031 | return 0; | |
2032 | ||
2033 | needed_len = sizeof(r); | |
2034 | /* Don't take 8 times more entropy than needed if int is 8 octets and we were | |
2035 | * asked for a number less than 10. */ | |
2036 | for (r = max, i = 0; r; ++i) | |
2037 | r >>= 1; | |
2038 | i = (i + 7) / 8; | |
2039 | if (i < needed_len) | |
2040 | needed_len = i; | |
2041 | ||
2042 | i = gnutls_rnd(GNUTLS_RND_NONCE, smallbuf, needed_len); | |
2043 | if (i < 0) | |
059ec3d9 | 2044 | { |
17c76198 PP |
2045 | DEBUG(D_all) debug_printf("gnutls_rnd() failed, using fallback.\n"); |
2046 | return vaguely_random_number_fallback(max); | |
2047 | } | |
2048 | r = 0; | |
2049 | for (p = smallbuf; needed_len; --needed_len, ++p) | |
2050 | { | |
2051 | r *= 256; | |
2052 | r += *p; | |
059ec3d9 PH |
2053 | } |
2054 | ||
17c76198 PP |
2055 | /* We don't particularly care about weighted results; if someone wants |
2056 | * smooth distribution and cares enough then they should submit a patch then. */ | |
2057 | return r % max; | |
059ec3d9 | 2058 | } |
af3498d6 PP |
2059 | #else /* HAVE_GNUTLS_RND */ |
2060 | int | |
2061 | vaguely_random_number(int max) | |
2062 | { | |
2063 | return vaguely_random_number_fallback(max); | |
2064 | } | |
2065 | #endif /* HAVE_GNUTLS_RND */ | |
059ec3d9 | 2066 | |
36f12725 NM |
2067 | |
2068 | ||
2069 | ||
3375e053 PP |
2070 | /************************************************* |
2071 | * Let tls_require_ciphers be checked at startup * | |
2072 | *************************************************/ | |
2073 | ||
2074 | /* The tls_require_ciphers option, if set, must be something which the | |
2075 | library can parse. | |
2076 | ||
2077 | Returns: NULL on success, or error message | |
2078 | */ | |
2079 | ||
2080 | uschar * | |
2081 | tls_validate_require_cipher(void) | |
2082 | { | |
2083 | int rc; | |
2084 | uschar *expciphers = NULL; | |
2085 | gnutls_priority_t priority_cache; | |
2086 | const char *errpos; | |
2087 | ||
2088 | #define validate_check_rc(Label) do { \ | |
2089 | if (rc != GNUTLS_E_SUCCESS) { if (exim_gnutls_base_init_done) gnutls_global_deinit(); \ | |
2090 | return string_sprintf("%s failed: %s", (Label), gnutls_strerror(rc)); } } while (0) | |
2091 | #define return_deinit(Label) do { gnutls_global_deinit(); return (Label); } while (0) | |
2092 | ||
2093 | if (exim_gnutls_base_init_done) | |
2094 | log_write(0, LOG_MAIN|LOG_PANIC, | |
2095 | "already initialised GnuTLS, Exim developer bug"); | |
2096 | ||
a5f239e4 | 2097 | #ifdef HAVE_GNUTLS_PKCS11 |
2519e60d | 2098 | if (!gnutls_allow_auto_pkcs11) |
a5f239e4 PP |
2099 | { |
2100 | rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL); | |
2101 | validate_check_rc(US"gnutls_pkcs11_init"); | |
2102 | } | |
2103 | #endif | |
3375e053 PP |
2104 | rc = gnutls_global_init(); |
2105 | validate_check_rc(US"gnutls_global_init()"); | |
2106 | exim_gnutls_base_init_done = TRUE; | |
2107 | ||
2108 | if (!(tls_require_ciphers && *tls_require_ciphers)) | |
2109 | return_deinit(NULL); | |
2110 | ||
2111 | if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers)) | |
2112 | return_deinit(US"failed to expand tls_require_ciphers"); | |
2113 | ||
2114 | if (!(expciphers && *expciphers)) | |
2115 | return_deinit(NULL); | |
2116 | ||
2117 | DEBUG(D_tls) | |
2118 | debug_printf("tls_require_ciphers expands to \"%s\"\n", expciphers); | |
2119 | ||
2120 | rc = gnutls_priority_init(&priority_cache, CS expciphers, &errpos); | |
2121 | validate_check_rc(string_sprintf( | |
2122 | "gnutls_priority_init(%s) failed at offset %ld, \"%.8s..\"", | |
2123 | expciphers, errpos - CS expciphers, errpos)); | |
2124 | ||
2125 | #undef return_deinit | |
2126 | #undef validate_check_rc | |
2127 | gnutls_global_deinit(); | |
2128 | ||
2129 | return NULL; | |
2130 | } | |
2131 | ||
2132 | ||
2133 | ||
2134 | ||
36f12725 NM |
2135 | /************************************************* |
2136 | * Report the library versions. * | |
2137 | *************************************************/ | |
2138 | ||
2139 | /* See a description in tls-openssl.c for an explanation of why this exists. | |
2140 | ||
2141 | Arguments: a FILE* to print the results to | |
2142 | Returns: nothing | |
2143 | */ | |
2144 | ||
2145 | void | |
2146 | tls_version_report(FILE *f) | |
2147 | { | |
754a0503 PP |
2148 | fprintf(f, "Library version: GnuTLS: Compile: %s\n" |
2149 | " Runtime: %s\n", | |
2150 | LIBGNUTLS_VERSION, | |
2151 | gnutls_check_version(NULL)); | |
36f12725 NM |
2152 | } |
2153 | ||
2b4a568d JH |
2154 | /* vi: aw ai sw=2 |
2155 | */ | |
059ec3d9 | 2156 | /* End of tls-gnu.c */ |