Add GnuTLS version check
[exim.git] / src / src / tls-openssl.c
CommitLineData
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
f5d78688
JH
8/* Portions Copyright (c) The OpenSSL Project 1999 */
9
059ec3d9
PH
10/* This module provides the TLS (aka SSL) support for Exim using the OpenSSL
11library. It is #included into the tls.c file when that library is used. The
12code herein is based on a patch that was originally contributed by Steve
13Haslam. It was adapted from stunnel, a GPL program by Michal Trojnara.
14
15No cryptographic code is included in Exim. All this module does is to call
16functions from the OpenSSL library. */
17
18
19/* Heading stuff */
20
21#include <openssl/lhash.h>
22#include <openssl/ssl.h>
23#include <openssl/err.h>
24#include <openssl/rand.h>
f2de3a33 25#ifndef DISABLE_OCSP
e51c7be2 26# include <openssl/ocsp.h>
3f7eeb86
PP
27#endif
28
f2de3a33
JH
29#ifndef DISABLE_OCSP
30# define EXIM_OCSP_SKEW_SECONDS (300L)
31# define EXIM_OCSP_MAX_AGE (-1L)
3f7eeb86 32#endif
059ec3d9 33
3bcbbbe2 34#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
e51c7be2 35# define EXIM_HAVE_OPENSSL_TLSEXT
3bcbbbe2
PP
36#endif
37
059ec3d9
PH
38/* Structure for collecting random data for seeding. */
39
40typedef struct randstuff {
9e3331ea
TK
41 struct timeval tv;
42 pid_t p;
059ec3d9
PH
43} randstuff;
44
45/* Local static variables */
46
a2ff477a
JH
47static BOOL client_verify_callback_called = FALSE;
48static BOOL server_verify_callback_called = FALSE;
059ec3d9
PH
49static const uschar *sid_ctx = US"exim";
50
d4f09789
PP
51/* We have three different contexts to care about.
52
53Simple case: client, `client_ctx`
54 As a client, we can be doing a callout or cut-through delivery while receiving
55 a message. So we have a client context, which should have options initialised
56 from the SMTP Transport.
57
58Server:
59 There are two cases: with and without ServerNameIndication from the client.
60 Given TLS SNI, we can be using different keys, certs and various other
61 configuration settings, because they're re-expanded with $tls_sni set. This
62 allows vhosting with TLS. This SNI is sent in the handshake.
63 A client might not send SNI, so we need a fallback, and an initial setup too.
64 So as a server, we start out using `server_ctx`.
65 If SNI is sent by the client, then we as server, mid-negotiation, try to clone
66 `server_sni` from `server_ctx` and then initialise settings by re-expanding
67 configuration.
68*/
69
817d9f57
JH
70static SSL_CTX *client_ctx = NULL;
71static SSL_CTX *server_ctx = NULL;
72static SSL *client_ssl = NULL;
73static SSL *server_ssl = NULL;
389ca47a 74
35731706 75#ifdef EXIM_HAVE_OPENSSL_TLSEXT
817d9f57 76static SSL_CTX *server_sni = NULL;
35731706 77#endif
059ec3d9
PH
78
79static char ssl_errstring[256];
80
81static int ssl_session_timeout = 200;
a2ff477a
JH
82static BOOL client_verify_optional = FALSE;
83static BOOL server_verify_optional = FALSE;
059ec3d9 84
f5d78688 85static BOOL reexpand_tls_files_for_sni = FALSE;
059ec3d9
PH
86
87
7be682ca
PP
88typedef struct tls_ext_ctx_cb {
89 uschar *certificate;
90 uschar *privatekey;
f2de3a33 91#ifndef DISABLE_OCSP
f5d78688
JH
92 BOOL is_server;
93 union {
94 struct {
95 uschar *file;
96 uschar *file_expanded;
97 OCSP_RESPONSE *response;
98 } server;
99 struct {
44662487
JH
100 X509_STORE *verify_store; /* non-null if status requested */
101 BOOL verify_required;
f5d78688
JH
102 } client;
103 } u_ocsp;
3f7eeb86 104#endif
7be682ca
PP
105 uschar *dhparam;
106 /* these are cached from first expand */
107 uschar *server_cipher_list;
108 /* only passed down to tls_error: */
109 host_item *host;
e51c7be2
JH
110
111#ifdef EXPERIMENTAL_CERTNAMES
112 uschar * verify_cert_hostnames;
113#endif
7be682ca
PP
114} tls_ext_ctx_cb;
115
116/* should figure out a cleanup of API to handle state preserved per
117implementation, for various reasons, which can be void * in the APIs.
118For now, we hack around it. */
817d9f57
JH
119tls_ext_ctx_cb *client_static_cbinfo = NULL;
120tls_ext_ctx_cb *server_static_cbinfo = NULL;
7be682ca
PP
121
122static int
983207c1
JH
123setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host, BOOL optional,
124 int (*cert_vfy_cb)(int, X509_STORE_CTX *) );
059ec3d9 125
3f7eeb86 126/* Callbacks */
3bcbbbe2 127#ifdef EXIM_HAVE_OPENSSL_TLSEXT
3f7eeb86 128static int tls_servername_cb(SSL *s, int *ad ARG_UNUSED, void *arg);
3bcbbbe2 129#endif
f2de3a33 130#ifndef DISABLE_OCSP
f5d78688 131static int tls_server_stapling_cb(SSL *s, void *arg);
3f7eeb86
PP
132#endif
133
059ec3d9
PH
134
135/*************************************************
136* Handle TLS error *
137*************************************************/
138
139/* Called from lots of places when errors occur before actually starting to do
140the TLS handshake, that is, while the session is still in clear. Always returns
141DEFER for a server and FAIL for a client so that most calls can use "return
142tls_error(...)" to do this processing and then give an appropriate return. A
143single function is used for both server and client, because it is called from
144some shared functions.
145
146Argument:
147 prefix text to include in the logged error
148 host NULL if setting up a server;
149 the connected host if setting up a client
7199e1ee 150 msg error message or NULL if we should ask OpenSSL
059ec3d9
PH
151
152Returns: OK/DEFER/FAIL
153*/
154
155static int
7199e1ee 156tls_error(uschar *prefix, host_item *host, uschar *msg)
059ec3d9 157{
7199e1ee
TF
158if (msg == NULL)
159 {
160 ERR_error_string(ERR_get_error(), ssl_errstring);
5ca6d115 161 msg = (uschar *)ssl_errstring;
7199e1ee
TF
162 }
163
059ec3d9
PH
164if (host == NULL)
165 {
7199e1ee 166 uschar *conn_info = smtp_get_connection_info();
5ca6d115 167 if (Ustrncmp(conn_info, US"SMTP ", 5) == 0)
7199e1ee
TF
168 conn_info += 5;
169 log_write(0, LOG_MAIN, "TLS error on %s (%s): %s",
170 conn_info, prefix, msg);
059ec3d9
PH
171 return DEFER;
172 }
173else
174 {
175 log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s): %s",
7199e1ee 176 host->name, host->address, prefix, msg);
059ec3d9
PH
177 return FAIL;
178 }
179}
180
181
182
183/*************************************************
184* Callback to generate RSA key *
185*************************************************/
186
187/*
188Arguments:
189 s SSL connection
190 export not used
191 keylength keylength
192
193Returns: pointer to generated key
194*/
195
196static RSA *
197rsa_callback(SSL *s, int export, int keylength)
198{
199RSA *rsa_key;
200export = export; /* Shut picky compilers up */
201DEBUG(D_tls) debug_printf("Generating %d bit RSA key...\n", keylength);
202rsa_key = RSA_generate_key(keylength, RSA_F4, NULL, NULL);
203if (rsa_key == NULL)
204 {
205 ERR_error_string(ERR_get_error(), ssl_errstring);
206 log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (RSA_generate_key): %s",
207 ssl_errstring);
208 return NULL;
209 }
210return rsa_key;
211}
212
213
214
f5d78688 215/* Extreme debug
f2de3a33 216#ifndef DISABLE_OCSP
f5d78688
JH
217void
218x509_store_dump_cert_s_names(X509_STORE * store)
219{
220STACK_OF(X509_OBJECT) * roots= store->objs;
221int i;
222static uschar name[256];
223
224for(i= 0; i<sk_X509_OBJECT_num(roots); i++)
225 {
226 X509_OBJECT * tmp_obj= sk_X509_OBJECT_value(roots, i);
227 if(tmp_obj->type == X509_LU_X509)
228 {
229 X509 * current_cert= tmp_obj->data.x509;
230 X509_NAME_oneline(X509_get_subject_name(current_cert), CS name, sizeof(name));
231 debug_printf(" %s\n", name);
232 }
233 }
234}
235#endif
236*/
237
059ec3d9
PH
238
239/*************************************************
240* Callback for verification *
241*************************************************/
242
243/* The SSL library does certificate verification if set up to do so. This
244callback has the current yes/no state is in "state". If verification succeeded,
245we set up the tls_peerdn string. If verification failed, what happens depends
246on whether the client is required to present a verifiable certificate or not.
247
248If verification is optional, we change the state to yes, but still log the
249verification error. For some reason (it really would help to have proper
250documentation of OpenSSL), this callback function then gets called again, this
251time with state = 1. In fact, that's useful, because we can set up the peerdn
252value, but we must take care not to set the private verified flag on the second
253time through.
254
255Note: this function is not called if the client fails to present a certificate
256when asked. We get here only if a certificate has been received. Handling of
257optional verification for this case is done when requesting SSL to verify, by
258setting SSL_VERIFY_FAIL_IF_NO_PEER_CERT in the non-optional case.
259
260Arguments:
261 state current yes/no state as 1/0
262 x509ctx certificate information.
a2ff477a 263 client TRUE for client startup, FALSE for server startup
059ec3d9
PH
264
265Returns: 1 if verified, 0 if not
266*/
267
268static int
421aff85
JH
269verify_callback(int state, X509_STORE_CTX *x509ctx,
270 tls_support *tlsp, BOOL *calledp, BOOL *optionalp)
059ec3d9 271{
421aff85 272X509 * cert = X509_STORE_CTX_get_current_cert(x509ctx);
059ec3d9
PH
273static uschar txt[256];
274
e51c7be2 275X509_NAME_oneline(X509_get_subject_name(cert), CS txt, sizeof(txt));
059ec3d9
PH
276
277if (state == 0)
278 {
279 log_write(0, LOG_MAIN, "SSL verify error: depth=%d error=%s cert=%s",
421aff85
JH
280 X509_STORE_CTX_get_error_depth(x509ctx),
281 X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509ctx)),
059ec3d9 282 txt);
a2ff477a
JH
283 tlsp->certificate_verified = FALSE;
284 *calledp = TRUE;
9d1c15ef
JH
285 if (!*optionalp)
286 {
421aff85 287 tlsp->peercert = X509_dup(cert);
9d1c15ef
JH
288 return 0; /* reject */
289 }
059ec3d9
PH
290 DEBUG(D_tls) debug_printf("SSL verify failure overridden (host in "
291 "tls_try_verify_hosts)\n");
059ec3d9
PH
292 }
293
421aff85 294else if (X509_STORE_CTX_get_error_depth(x509ctx) != 0)
059ec3d9 295 {
93dcb1c2 296 DEBUG(D_tls) debug_printf("SSL verify ok: depth=%d SN=%s\n",
421aff85 297 X509_STORE_CTX_get_error_depth(x509ctx), txt);
f2de3a33 298#ifndef DISABLE_OCSP
f5d78688
JH
299 if (tlsp == &tls_out && client_static_cbinfo->u_ocsp.client.verify_store)
300 { /* client, wanting stapling */
301 /* Add the server cert's signing chain as the one
302 for the verification of the OCSP stapled information. */
303
304 if (!X509_STORE_add_cert(client_static_cbinfo->u_ocsp.client.verify_store,
421aff85 305 cert))
f5d78688
JH
306 ERR_clear_error();
307 }
308#endif
059ec3d9
PH
309 }
310else
311 {
e51c7be2
JH
312#ifdef EXPERIMENTAL_CERTNAMES
313 uschar * verify_cert_hostnames;
314#endif
315
a2ff477a 316 tlsp->peerdn = txt;
421aff85 317 tlsp->peercert = X509_dup(cert);
e51c7be2
JH
318
319#ifdef EXPERIMENTAL_CERTNAMES
320 if ( tlsp == &tls_out
321 && ((verify_cert_hostnames = client_static_cbinfo->verify_cert_hostnames)))
322 /* client, wanting hostname check */
323
324# if OPENSSL_VERSION_NUMBER >= 0x010100000L || OPENSSL_VERSION_NUMBER >= 0x010002000L
325 {
326 int sep = 0;
327 uschar * list = verify_cert_hostnames;
328 uschar * name;
329 while (name = string_nextinlist(&list, &sep, NULL, 0))
330 if (X509_check_host(cert, name, 0, 0))
331 break;
332 if (!name)
333 {
334 log_write(0, LOG_MAIN,
335 "SSL verify error: certificate name mismatch: \"%s\"\n", txt);
336 return 0; /* reject */
337 }
338 }
339# else
340 if (!tls_is_name_for_cert(verify_cert_hostnames, cert))
341 {
342 log_write(0, LOG_MAIN,
343 "SSL verify error: certificate name mismatch: \"%s\"\n", txt);
344 return 0; /* reject */
345 }
346# endif
347#endif
348
93dcb1c2
JH
349 DEBUG(D_tls) debug_printf("SSL%s verify ok: depth=0 SN=%s\n",
350 *calledp ? "" : " authenticated", txt);
351 if (!*calledp) tlsp->certificate_verified = TRUE;
352 *calledp = TRUE;
059ec3d9
PH
353 }
354
059ec3d9
PH
355return 1; /* accept */
356}
357
a2ff477a
JH
358static int
359verify_callback_client(int state, X509_STORE_CTX *x509ctx)
360{
f5d78688 361return verify_callback(state, x509ctx, &tls_out, &client_verify_callback_called, &client_verify_optional);
a2ff477a
JH
362}
363
364static int
365verify_callback_server(int state, X509_STORE_CTX *x509ctx)
366{
f5d78688 367return verify_callback(state, x509ctx, &tls_in, &server_verify_callback_called, &server_verify_optional);
a2ff477a
JH
368}
369
059ec3d9
PH
370
371
372/*************************************************
373* Information callback *
374*************************************************/
375
376/* The SSL library functions call this from time to time to indicate what they
7be682ca
PP
377are doing. We copy the string to the debugging output when TLS debugging has
378been requested.
059ec3d9
PH
379
380Arguments:
381 s the SSL connection
382 where
383 ret
384
385Returns: nothing
386*/
387
388static void
389info_callback(SSL *s, int where, int ret)
390{
391where = where;
392ret = ret;
393DEBUG(D_tls) debug_printf("SSL info: %s\n", SSL_state_string_long(s));
394}
395
396
397
398/*************************************************
399* Initialize for DH *
400*************************************************/
401
402/* If dhparam is set, expand it, and load up the parameters for DH encryption.
403
404Arguments:
a799883d 405 dhparam DH parameter file or fixed parameter identity string
7199e1ee 406 host connected host, if client; NULL if server
059ec3d9
PH
407
408Returns: TRUE if OK (nothing to set up, or setup worked)
409*/
410
411static BOOL
a799883d 412init_dh(SSL_CTX *sctx, uschar *dhparam, host_item *host)
059ec3d9 413{
059ec3d9
PH
414BIO *bio;
415DH *dh;
416uschar *dhexpanded;
a799883d 417const char *pem;
059ec3d9
PH
418
419if (!expand_check(dhparam, US"tls_dhparam", &dhexpanded))
420 return FALSE;
421
a799883d 422if (dhexpanded == NULL || *dhexpanded == '\0')
059ec3d9 423 {
a799883d 424 bio = BIO_new_mem_buf(CS std_dh_prime_default(), -1);
059ec3d9 425 }
a799883d 426else if (dhexpanded[0] == '/')
059ec3d9 427 {
a799883d
PP
428 bio = BIO_new_file(CS dhexpanded, "r");
429 if (bio == NULL)
059ec3d9 430 {
7199e1ee 431 tls_error(string_sprintf("could not read dhparams file %s", dhexpanded),
a799883d
PP
432 host, US strerror(errno));
433 return FALSE;
059ec3d9 434 }
a799883d
PP
435 }
436else
437 {
438 if (Ustrcmp(dhexpanded, "none") == 0)
059ec3d9 439 {
a799883d
PP
440 DEBUG(D_tls) debug_printf("Requested no DH parameters.\n");
441 return TRUE;
059ec3d9 442 }
a799883d
PP
443
444 pem = std_dh_prime_named(dhexpanded);
445 if (!pem)
446 {
447 tls_error(string_sprintf("Unknown standard DH prime \"%s\"", dhexpanded),
448 host, US strerror(errno));
449 return FALSE;
450 }
451 bio = BIO_new_mem_buf(CS pem, -1);
452 }
453
454dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
455if (dh == NULL)
456 {
059ec3d9 457 BIO_free(bio);
a799883d
PP
458 tls_error(string_sprintf("Could not read tls_dhparams \"%s\"", dhexpanded),
459 host, NULL);
460 return FALSE;
461 }
462
463/* Even if it is larger, we silently return success rather than cause things
464 * to fail out, so that a too-large DH will not knock out all TLS; it's a
465 * debatable choice. */
466if ((8*DH_size(dh)) > tls_dh_max_bits)
467 {
468 DEBUG(D_tls)
469 debug_printf("dhparams file %d bits, is > tls_dh_max_bits limit of %d",
470 8*DH_size(dh), tls_dh_max_bits);
471 }
472else
473 {
474 SSL_CTX_set_tmp_dh(sctx, dh);
475 DEBUG(D_tls)
476 debug_printf("Diffie-Hellman initialized from %s with %d-bit prime\n",
477 dhexpanded ? dhexpanded : US"default", 8*DH_size(dh));
059ec3d9
PH
478 }
479
a799883d
PP
480DH_free(dh);
481BIO_free(bio);
482
483return TRUE;
059ec3d9
PH
484}
485
486
487
488
f2de3a33 489#ifndef DISABLE_OCSP
3f7eeb86
PP
490/*************************************************
491* Load OCSP information into state *
492*************************************************/
493
f5d78688 494/* Called to load the server OCSP response from the given file into memory, once
3f7eeb86
PP
495caller has determined this is needed. Checks validity. Debugs a message
496if invalid.
497
498ASSUMES: single response, for single cert.
499
500Arguments:
501 sctx the SSL_CTX* to update
502 cbinfo various parts of session state
503 expanded the filename putatively holding an OCSP response
504
505*/
506
507static void
f5d78688 508ocsp_load_response(SSL_CTX *sctx, tls_ext_ctx_cb *cbinfo, const uschar *expanded)
3f7eeb86
PP
509{
510BIO *bio;
511OCSP_RESPONSE *resp;
512OCSP_BASICRESP *basic_response;
513OCSP_SINGLERESP *single_response;
514ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
515X509_STORE *store;
516unsigned long verify_flags;
517int status, reason, i;
518
f5d78688
JH
519cbinfo->u_ocsp.server.file_expanded = string_copy(expanded);
520if (cbinfo->u_ocsp.server.response)
3f7eeb86 521 {
f5d78688
JH
522 OCSP_RESPONSE_free(cbinfo->u_ocsp.server.response);
523 cbinfo->u_ocsp.server.response = NULL;
3f7eeb86
PP
524 }
525
f5d78688 526bio = BIO_new_file(CS cbinfo->u_ocsp.server.file_expanded, "rb");
3f7eeb86
PP
527if (!bio)
528 {
529 DEBUG(D_tls) debug_printf("Failed to open OCSP response file \"%s\"\n",
f5d78688 530 cbinfo->u_ocsp.server.file_expanded);
3f7eeb86
PP
531 return;
532 }
533
534resp = d2i_OCSP_RESPONSE_bio(bio, NULL);
535BIO_free(bio);
536if (!resp)
537 {
538 DEBUG(D_tls) debug_printf("Error reading OCSP response.\n");
539 return;
540 }
541
542status = OCSP_response_status(resp);
543if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL)
544 {
545 DEBUG(D_tls) debug_printf("OCSP response not valid: %s (%d)\n",
546 OCSP_response_status_str(status), status);
f5d78688 547 goto bad;
3f7eeb86
PP
548 }
549
550basic_response = OCSP_response_get1_basic(resp);
551if (!basic_response)
552 {
553 DEBUG(D_tls)
554 debug_printf("OCSP response parse error: unable to extract basic response.\n");
f5d78688 555 goto bad;
3f7eeb86
PP
556 }
557
558store = SSL_CTX_get_cert_store(sctx);
559verify_flags = OCSP_NOVERIFY; /* check sigs, but not purpose */
560
561/* May need to expose ability to adjust those flags?
562OCSP_NOSIGS OCSP_NOVERIFY OCSP_NOCHAIN OCSP_NOCHECKS OCSP_NOEXPLICIT
563OCSP_TRUSTOTHER OCSP_NOINTERN */
564
565i = OCSP_basic_verify(basic_response, NULL, store, verify_flags);
566if (i <= 0)
567 {
568 DEBUG(D_tls) {
569 ERR_error_string(ERR_get_error(), ssl_errstring);
570 debug_printf("OCSP response verify failure: %s\n", US ssl_errstring);
f5d78688
JH
571 }
572 goto bad;
3f7eeb86
PP
573 }
574
575/* Here's the simplifying assumption: there's only one response, for the
576one certificate we use, and nothing for anything else in a chain. If this
577proves false, we need to extract a cert id from our issued cert
578(tls_certificate) and use that for OCSP_resp_find_status() (which finds the
579right cert in the stack and then calls OCSP_single_get0_status()).
580
581I'm hoping to avoid reworking a bunch more of how we handle state here. */
582single_response = OCSP_resp_get0(basic_response, 0);
583if (!single_response)
584 {
585 DEBUG(D_tls)
586 debug_printf("Unable to get first response from OCSP basic response.\n");
f5d78688 587 goto bad;
3f7eeb86
PP
588 }
589
590status = OCSP_single_get0_status(single_response, &reason, &rev, &thisupd, &nextupd);
f5d78688 591if (status != V_OCSP_CERTSTATUS_GOOD)
3f7eeb86 592 {
f5d78688
JH
593 DEBUG(D_tls) debug_printf("OCSP response bad cert status: %s (%d) %s (%d)\n",
594 OCSP_cert_status_str(status), status,
595 OCSP_crl_reason_str(reason), reason);
596 goto bad;
3f7eeb86
PP
597 }
598
599if (!OCSP_check_validity(thisupd, nextupd, EXIM_OCSP_SKEW_SECONDS, EXIM_OCSP_MAX_AGE))
600 {
601 DEBUG(D_tls) debug_printf("OCSP status invalid times.\n");
f5d78688 602 goto bad;
3f7eeb86
PP
603 }
604
f5d78688 605supply_response:
018058b2 606 cbinfo->u_ocsp.server.response = resp;
f5d78688
JH
607return;
608
609bad:
018058b2
JH
610 if (running_in_test_harness)
611 {
612 extern char ** environ;
613 uschar ** p;
614 for (p = USS environ; *p != NULL; p++)
615 if (Ustrncmp(*p, "EXIM_TESTHARNESS_DISABLE_OCSPVALIDITYCHECK", 42) == 0)
616 {
617 DEBUG(D_tls) debug_printf("Supplying known bad OCSP response\n");
618 goto supply_response;
619 }
620 }
f5d78688 621return;
3f7eeb86 622}
f2de3a33 623#endif /*!DISABLE_OCSP*/
3f7eeb86
PP
624
625
626
627
7be682ca
PP
628/*************************************************
629* Expand key and cert file specs *
630*************************************************/
631
f5d78688 632/* Called once during tls_init and possibly again during TLS setup, for a
7be682ca
PP
633new context, if Server Name Indication was used and tls_sni was seen in
634the certificate string.
635
636Arguments:
637 sctx the SSL_CTX* to update
638 cbinfo various parts of session state
639
640Returns: OK/DEFER/FAIL
641*/
642
643static int
3f7eeb86 644tls_expand_session_files(SSL_CTX *sctx, tls_ext_ctx_cb *cbinfo)
7be682ca
PP
645{
646uschar *expanded;
647
648if (cbinfo->certificate == NULL)
649 return OK;
650
d9b2312b
JH
651if (Ustrstr(cbinfo->certificate, US"tls_sni") ||
652 Ustrstr(cbinfo->certificate, US"tls_in_sni") ||
653 Ustrstr(cbinfo->certificate, US"tls_out_sni")
654 )
7be682ca
PP
655 reexpand_tls_files_for_sni = TRUE;
656
657if (!expand_check(cbinfo->certificate, US"tls_certificate", &expanded))
658 return DEFER;
659
660if (expanded != NULL)
661 {
662 DEBUG(D_tls) debug_printf("tls_certificate file %s\n", expanded);
663 if (!SSL_CTX_use_certificate_chain_file(sctx, CS expanded))
664 return tls_error(string_sprintf(
665 "SSL_CTX_use_certificate_chain_file file=%s", expanded),
666 cbinfo->host, NULL);
667 }
668
669if (cbinfo->privatekey != NULL &&
670 !expand_check(cbinfo->privatekey, US"tls_privatekey", &expanded))
671 return DEFER;
672
673/* If expansion was forced to fail, key_expanded will be NULL. If the result
674of the expansion is an empty string, ignore it also, and assume the private
675key is in the same file as the certificate. */
676
677if (expanded != NULL && *expanded != 0)
678 {
679 DEBUG(D_tls) debug_printf("tls_privatekey file %s\n", expanded);
680 if (!SSL_CTX_use_PrivateKey_file(sctx, CS expanded, SSL_FILETYPE_PEM))
681 return tls_error(string_sprintf(
682 "SSL_CTX_use_PrivateKey_file file=%s", expanded), cbinfo->host, NULL);
683 }
684
f2de3a33 685#ifndef DISABLE_OCSP
f5d78688 686if (cbinfo->is_server && cbinfo->u_ocsp.server.file != NULL)
3f7eeb86 687 {
f5d78688 688 if (!expand_check(cbinfo->u_ocsp.server.file, US"tls_ocsp_file", &expanded))
3f7eeb86
PP
689 return DEFER;
690
691 if (expanded != NULL && *expanded != 0)
692 {
693 DEBUG(D_tls) debug_printf("tls_ocsp_file %s\n", expanded);
f5d78688
JH
694 if (cbinfo->u_ocsp.server.file_expanded &&
695 (Ustrcmp(expanded, cbinfo->u_ocsp.server.file_expanded) == 0))
3f7eeb86
PP
696 {
697 DEBUG(D_tls)
698 debug_printf("tls_ocsp_file value unchanged, using existing values.\n");
699 } else {
700 ocsp_load_response(sctx, cbinfo, expanded);
701 }
702 }
703 }
704#endif
705
7be682ca
PP
706return OK;
707}
708
709
710
711
712/*************************************************
713* Callback to handle SNI *
714*************************************************/
715
716/* Called when acting as server during the TLS session setup if a Server Name
717Indication extension was sent by the client.
718
719API documentation is OpenSSL s_server.c implementation.
720
721Arguments:
722 s SSL* of the current session
723 ad unknown (part of OpenSSL API) (unused)
724 arg Callback of "our" registered data
725
726Returns: SSL_TLSEXT_ERR_{OK,ALERT_WARNING,ALERT_FATAL,NOACK}
727*/
728
3bcbbbe2 729#ifdef EXIM_HAVE_OPENSSL_TLSEXT
7be682ca
PP
730static int
731tls_servername_cb(SSL *s, int *ad ARG_UNUSED, void *arg)
732{
733const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
3f7eeb86 734tls_ext_ctx_cb *cbinfo = (tls_ext_ctx_cb *) arg;
7be682ca 735int rc;
3f0945ff 736int old_pool = store_pool;
7be682ca
PP
737
738if (!servername)
739 return SSL_TLSEXT_ERR_OK;
740
3f0945ff 741DEBUG(D_tls) debug_printf("Received TLS SNI \"%s\"%s\n", servername,
7be682ca
PP
742 reexpand_tls_files_for_sni ? "" : " (unused for certificate selection)");
743
744/* Make the extension value available for expansion */
3f0945ff 745store_pool = POOL_PERM;
817d9f57 746tls_in.sni = string_copy(US servername);
3f0945ff 747store_pool = old_pool;
7be682ca
PP
748
749if (!reexpand_tls_files_for_sni)
750 return SSL_TLSEXT_ERR_OK;
751
752/* Can't find an SSL_CTX_clone() or equivalent, so we do it manually;
753not confident that memcpy wouldn't break some internal reference counting.
754Especially since there's a references struct member, which would be off. */
755
817d9f57
JH
756server_sni = SSL_CTX_new(SSLv23_server_method());
757if (!server_sni)
7be682ca
PP
758 {
759 ERR_error_string(ERR_get_error(), ssl_errstring);
760 DEBUG(D_tls) debug_printf("SSL_CTX_new() failed: %s\n", ssl_errstring);
761 return SSL_TLSEXT_ERR_NOACK;
762 }
763
764/* Not sure how many of these are actually needed, since SSL object
765already exists. Might even need this selfsame callback, for reneg? */
766
817d9f57
JH
767SSL_CTX_set_info_callback(server_sni, SSL_CTX_get_info_callback(server_ctx));
768SSL_CTX_set_mode(server_sni, SSL_CTX_get_mode(server_ctx));
769SSL_CTX_set_options(server_sni, SSL_CTX_get_options(server_ctx));
770SSL_CTX_set_timeout(server_sni, SSL_CTX_get_timeout(server_ctx));
771SSL_CTX_set_tlsext_servername_callback(server_sni, tls_servername_cb);
772SSL_CTX_set_tlsext_servername_arg(server_sni, cbinfo);
7be682ca 773if (cbinfo->server_cipher_list)
817d9f57 774 SSL_CTX_set_cipher_list(server_sni, CS cbinfo->server_cipher_list);
f2de3a33 775#ifndef DISABLE_OCSP
f5d78688 776if (cbinfo->u_ocsp.server.file)
3f7eeb86 777 {
f5d78688 778 SSL_CTX_set_tlsext_status_cb(server_sni, tls_server_stapling_cb);
14c7b357 779 SSL_CTX_set_tlsext_status_arg(server_sni, cbinfo);
3f7eeb86
PP
780 }
781#endif
7be682ca 782
983207c1 783rc = setup_certs(server_sni, tls_verify_certificates, tls_crl, NULL, FALSE, verify_callback_server);
7be682ca
PP
784if (rc != OK) return SSL_TLSEXT_ERR_NOACK;
785
3f7eeb86
PP
786/* do this after setup_certs, because this can require the certs for verifying
787OCSP information. */
817d9f57 788rc = tls_expand_session_files(server_sni, cbinfo);
7be682ca
PP
789if (rc != OK) return SSL_TLSEXT_ERR_NOACK;
790
389ca47a 791rc = init_dh(server_sni, cbinfo->dhparam, NULL);
a799883d
PP
792if (rc != OK) return SSL_TLSEXT_ERR_NOACK;
793
7be682ca 794DEBUG(D_tls) debug_printf("Switching SSL context.\n");
817d9f57 795SSL_set_SSL_CTX(s, server_sni);
7be682ca
PP
796
797return SSL_TLSEXT_ERR_OK;
798}
3bcbbbe2 799#endif /* EXIM_HAVE_OPENSSL_TLSEXT */
7be682ca
PP
800
801
802
803
f2de3a33 804#ifndef DISABLE_OCSP
f5d78688 805
3f7eeb86
PP
806/*************************************************
807* Callback to handle OCSP Stapling *
808*************************************************/
809
810/* Called when acting as server during the TLS session setup if the client
811requests OCSP information with a Certificate Status Request.
812
813Documentation via openssl s_server.c and the Apache patch from the OpenSSL
814project.
815
816*/
817
818static int
f5d78688 819tls_server_stapling_cb(SSL *s, void *arg)
3f7eeb86
PP
820{
821const tls_ext_ctx_cb *cbinfo = (tls_ext_ctx_cb *) arg;
822uschar *response_der;
823int response_der_len;
824
af4a1bca
JH
825DEBUG(D_tls)
826 debug_printf("Received TLS status request (OCSP stapling); %s response.",
f5d78688
JH
827 cbinfo->u_ocsp.server.response ? "have" : "lack");
828
44662487 829tls_in.ocsp = OCSP_NOT_RESP;
f5d78688 830if (!cbinfo->u_ocsp.server.response)
3f7eeb86
PP
831 return SSL_TLSEXT_ERR_NOACK;
832
833response_der = NULL;
44662487
JH
834response_der_len = i2d_OCSP_RESPONSE(cbinfo->u_ocsp.server.response,
835 &response_der);
3f7eeb86
PP
836if (response_der_len <= 0)
837 return SSL_TLSEXT_ERR_NOACK;
838
5e55c7a9 839SSL_set_tlsext_status_ocsp_resp(server_ssl, response_der, response_der_len);
44662487 840tls_in.ocsp = OCSP_VFIED;
3f7eeb86
PP
841return SSL_TLSEXT_ERR_OK;
842}
843
3f7eeb86 844
f5d78688
JH
845static void
846time_print(BIO * bp, const char * str, ASN1_GENERALIZEDTIME * time)
847{
848BIO_printf(bp, "\t%s: ", str);
849ASN1_GENERALIZEDTIME_print(bp, time);
850BIO_puts(bp, "\n");
851}
852
853static int
854tls_client_stapling_cb(SSL *s, void *arg)
855{
856tls_ext_ctx_cb * cbinfo = arg;
857const unsigned char * p;
858int len;
859OCSP_RESPONSE * rsp;
860OCSP_BASICRESP * bs;
861int i;
862
863DEBUG(D_tls) debug_printf("Received TLS status response (OCSP stapling):");
864len = SSL_get_tlsext_status_ocsp_resp(s, &p);
865if(!p)
866 {
44662487
JH
867 /* Expect this when we requested ocsp but got none */
868 if ( cbinfo->u_ocsp.client.verify_required
869 && log_extra_selector & LX_tls_cipher)
870 log_write(0, LOG_MAIN, "Received TLS status callback, null content");
f5d78688
JH
871 else
872 DEBUG(D_tls) debug_printf(" null\n");
44662487 873 return cbinfo->u_ocsp.client.verify_required ? 0 : 1;
f5d78688 874 }
018058b2 875
f5d78688
JH
876if(!(rsp = d2i_OCSP_RESPONSE(NULL, &p, len)))
877 {
018058b2 878 tls_out.ocsp = OCSP_FAILED;
f5d78688
JH
879 if (log_extra_selector & LX_tls_cipher)
880 log_write(0, LOG_MAIN, "Received TLS status response, parse error");
881 else
882 DEBUG(D_tls) debug_printf(" parse error\n");
883 return 0;
884 }
885
886if(!(bs = OCSP_response_get1_basic(rsp)))
887 {
018058b2 888 tls_out.ocsp = OCSP_FAILED;
f5d78688
JH
889 if (log_extra_selector & LX_tls_cipher)
890 log_write(0, LOG_MAIN, "Received TLS status response, error parsing response");
891 else
892 DEBUG(D_tls) debug_printf(" error parsing response\n");
893 OCSP_RESPONSE_free(rsp);
894 return 0;
895 }
896
897/* We'd check the nonce here if we'd put one in the request. */
898/* However that would defeat cacheability on the server so we don't. */
899
f5d78688
JH
900/* This section of code reworked from OpenSSL apps source;
901 The OpenSSL Project retains copyright:
902 Copyright (c) 1999 The OpenSSL Project. All rights reserved.
903*/
904 {
905 BIO * bp = NULL;
f5d78688
JH
906 int status, reason;
907 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
908
909 DEBUG(D_tls) bp = BIO_new_fp(stderr, BIO_NOCLOSE);
910
911 /*OCSP_RESPONSE_print(bp, rsp, 0); extreme debug: stapling content */
912
913 /* Use the chain that verified the server cert to verify the stapled info */
914 /* DEBUG(D_tls) x509_store_dump_cert_s_names(cbinfo->u_ocsp.client.verify_store); */
915
44662487
JH
916 if ((i = OCSP_basic_verify(bs, NULL,
917 cbinfo->u_ocsp.client.verify_store, 0)) <= 0)
f5d78688 918 {
018058b2 919 tls_out.ocsp = OCSP_FAILED;
f5d78688
JH
920 BIO_printf(bp, "OCSP response verify failure\n");
921 ERR_print_errors(bp);
44662487 922 i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
f5d78688
JH
923 goto out;
924 }
925
926 BIO_printf(bp, "OCSP response well-formed and signed OK\n");
927
928 {
929 STACK_OF(OCSP_SINGLERESP) * sresp = bs->tbsResponseData->responses;
930 OCSP_SINGLERESP * single;
931
932 if (sk_OCSP_SINGLERESP_num(sresp) != 1)
933 {
018058b2 934 tls_out.ocsp = OCSP_FAILED;
44662487
JH
935 log_write(0, LOG_MAIN, "OCSP stapling "
936 "with multiple responses not handled");
937 i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
f5d78688
JH
938 goto out;
939 }
940 single = OCSP_resp_get0(bs, 0);
44662487
JH
941 status = OCSP_single_get0_status(single, &reason, &rev,
942 &thisupd, &nextupd);
f5d78688
JH
943 }
944
f5d78688
JH
945 DEBUG(D_tls) time_print(bp, "This OCSP Update", thisupd);
946 DEBUG(D_tls) if(nextupd) time_print(bp, "Next OCSP Update", nextupd);
44662487
JH
947 if (!OCSP_check_validity(thisupd, nextupd,
948 EXIM_OCSP_SKEW_SECONDS, EXIM_OCSP_MAX_AGE))
f5d78688 949 {
018058b2 950 tls_out.ocsp = OCSP_FAILED;
f5d78688
JH
951 DEBUG(D_tls) ERR_print_errors(bp);
952 log_write(0, LOG_MAIN, "Server OSCP dates invalid");
44662487 953 i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
f5d78688 954 }
44662487 955 else
f5d78688 956 {
44662487
JH
957 DEBUG(D_tls) BIO_printf(bp, "Certificate status: %s\n",
958 OCSP_cert_status_str(status));
959 switch(status)
960 {
961 case V_OCSP_CERTSTATUS_GOOD:
44662487 962 tls_out.ocsp = OCSP_VFIED;
018058b2 963 i = 1;
44662487
JH
964 break;
965 case V_OCSP_CERTSTATUS_REVOKED:
018058b2 966 tls_out.ocsp = OCSP_FAILED;
44662487
JH
967 log_write(0, LOG_MAIN, "Server certificate revoked%s%s",
968 reason != -1 ? "; reason: " : "",
969 reason != -1 ? OCSP_crl_reason_str(reason) : "");
970 DEBUG(D_tls) time_print(bp, "Revocation Time", rev);
971 i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
972 break;
973 default:
018058b2 974 tls_out.ocsp = OCSP_FAILED;
44662487
JH
975 log_write(0, LOG_MAIN,
976 "Server certificate status unknown, in OCSP stapling");
977 i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
978 break;
979 }
f5d78688
JH
980 }
981 out:
982 BIO_free(bp);
983 }
984
985OCSP_RESPONSE_free(rsp);
986return i;
987}
f2de3a33 988#endif /*!DISABLE_OCSP*/
3f7eeb86
PP
989
990
991
059ec3d9
PH
992/*************************************************
993* Initialize for TLS *
994*************************************************/
995
e51c7be2
JH
996/* Called from both server and client code, to do preliminary initialization
997of the library. We allocate and return a context structure.
059ec3d9
PH
998
999Arguments:
1000 host connected host, if client; NULL if server
1001 dhparam DH parameter file
1002 certificate certificate file
1003 privatekey private key
f5d78688 1004 ocsp_file file of stapling info (server); flag for require ocsp (client)
059ec3d9 1005 addr address if client; NULL if server (for some randomness)
e51c7be2 1006 cbp place to put allocated context
059ec3d9
PH
1007
1008Returns: OK/DEFER/FAIL
1009*/
1010
1011static int
817d9f57 1012tls_init(SSL_CTX **ctxp, host_item *host, uschar *dhparam, uschar *certificate,
3f7eeb86 1013 uschar *privatekey,
f2de3a33 1014#ifndef DISABLE_OCSP
3f7eeb86
PP
1015 uschar *ocsp_file,
1016#endif
817d9f57 1017 address_item *addr, tls_ext_ctx_cb ** cbp)
059ec3d9 1018{
77bb000f 1019long init_options;
7be682ca 1020int rc;
77bb000f 1021BOOL okay;
7be682ca
PP
1022tls_ext_ctx_cb *cbinfo;
1023
1024cbinfo = store_malloc(sizeof(tls_ext_ctx_cb));
1025cbinfo->certificate = certificate;
1026cbinfo->privatekey = privatekey;
f2de3a33 1027#ifndef DISABLE_OCSP
f5d78688
JH
1028if ((cbinfo->is_server = host==NULL))
1029 {
1030 cbinfo->u_ocsp.server.file = ocsp_file;
1031 cbinfo->u_ocsp.server.file_expanded = NULL;
1032 cbinfo->u_ocsp.server.response = NULL;
1033 }
1034else
1035 cbinfo->u_ocsp.client.verify_store = NULL;
3f7eeb86 1036#endif
7be682ca
PP
1037cbinfo->dhparam = dhparam;
1038cbinfo->host = host;
77bb000f 1039
059ec3d9
PH
1040SSL_load_error_strings(); /* basic set up */
1041OpenSSL_add_ssl_algorithms();
1042
388d6564 1043#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
77bb000f 1044/* SHA256 is becoming ever more popular. This makes sure it gets added to the
a0475b69
TK
1045list of available digests. */
1046EVP_add_digest(EVP_sha256());
cf1ef1a9 1047#endif
a0475b69 1048
f0f5a555
PP
1049/* Create a context.
1050The OpenSSL docs in 1.0.1b have not been updated to clarify TLS variant
1051negotiation in the different methods; as far as I can tell, the only
1052*_{server,client}_method which allows negotiation is SSLv23, which exists even
1053when OpenSSL is built without SSLv2 support.
1054By disabling with openssl_options, we can let admins re-enable with the
1055existing knob. */
059ec3d9 1056
817d9f57 1057*ctxp = SSL_CTX_new((host == NULL)?
059ec3d9
PH
1058 SSLv23_server_method() : SSLv23_client_method());
1059
817d9f57 1060if (*ctxp == NULL) return tls_error(US"SSL_CTX_new", host, NULL);
059ec3d9
PH
1061
1062/* It turns out that we need to seed the random number generator this early in
1063order to get the full complement of ciphers to work. It took me roughly a day
1064of work to discover this by experiment.
1065
1066On systems that have /dev/urandom, SSL may automatically seed itself from
1067there. Otherwise, we have to make something up as best we can. Double check
1068afterwards. */
1069
1070if (!RAND_status())
1071 {
1072 randstuff r;
9e3331ea 1073 gettimeofday(&r.tv, NULL);
059ec3d9
PH
1074 r.p = getpid();
1075
1076 RAND_seed((uschar *)(&r), sizeof(r));
1077 RAND_seed((uschar *)big_buffer, big_buffer_size);
1078 if (addr != NULL) RAND_seed((uschar *)addr, sizeof(addr));
1079
1080 if (!RAND_status())
7199e1ee 1081 return tls_error(US"RAND_status", host,
5ca6d115 1082 US"unable to seed random number generator");
059ec3d9
PH
1083 }
1084
1085/* Set up the information callback, which outputs if debugging is at a suitable
1086level. */
1087
817d9f57 1088SSL_CTX_set_info_callback(*ctxp, (void (*)())info_callback);
059ec3d9 1089
c80c5570 1090/* Automatically re-try reads/writes after renegotiation. */
817d9f57 1091(void) SSL_CTX_set_mode(*ctxp, SSL_MODE_AUTO_RETRY);
c80c5570 1092
77bb000f
PP
1093/* Apply administrator-supplied work-arounds.
1094Historically we applied just one requested option,
1095SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS, but when bug 994 requested a second, we
1096moved to an administrator-controlled list of options to specify and
1097grandfathered in the first one as the default value for "openssl_options".
059ec3d9 1098
77bb000f
PP
1099No OpenSSL version number checks: the options we accept depend upon the
1100availability of the option value macros from OpenSSL. */
059ec3d9 1101
77bb000f
PP
1102okay = tls_openssl_options_parse(openssl_options, &init_options);
1103if (!okay)
73a46702 1104 return tls_error(US"openssl_options parsing failed", host, NULL);
77bb000f
PP
1105
1106if (init_options)
1107 {
1108 DEBUG(D_tls) debug_printf("setting SSL CTX options: %#lx\n", init_options);
817d9f57 1109 if (!(SSL_CTX_set_options(*ctxp, init_options)))
77bb000f
PP
1110 return tls_error(string_sprintf(
1111 "SSL_CTX_set_option(%#lx)", init_options), host, NULL);
1112 }
1113else
1114 DEBUG(D_tls) debug_printf("no SSL CTX options to set\n");
059ec3d9
PH
1115
1116/* Initialize with DH parameters if supplied */
1117
817d9f57 1118if (!init_dh(*ctxp, dhparam, host)) return DEFER;
059ec3d9 1119
3f7eeb86 1120/* Set up certificate and key (and perhaps OCSP info) */
059ec3d9 1121
817d9f57 1122rc = tls_expand_session_files(*ctxp, cbinfo);
7be682ca 1123if (rc != OK) return rc;
c91535f3 1124
7be682ca 1125/* If we need to handle SNI, do so */
3bcbbbe2 1126#ifdef EXIM_HAVE_OPENSSL_TLSEXT
f5d78688 1127if (host == NULL) /* server */
3f0945ff 1128 {
f2de3a33 1129# ifndef DISABLE_OCSP
f5d78688 1130 /* We check u_ocsp.server.file, not server.response, because we care about if
3f7eeb86
PP
1131 the option exists, not what the current expansion might be, as SNI might
1132 change the certificate and OCSP file in use between now and the time the
1133 callback is invoked. */
f5d78688 1134 if (cbinfo->u_ocsp.server.file)
3f7eeb86 1135 {
f5d78688 1136 SSL_CTX_set_tlsext_status_cb(server_ctx, tls_server_stapling_cb);
5e55c7a9 1137 SSL_CTX_set_tlsext_status_arg(server_ctx, cbinfo);
3f7eeb86 1138 }
f5d78688 1139# endif
3f0945ff
PP
1140 /* We always do this, so that $tls_sni is available even if not used in
1141 tls_certificate */
817d9f57
JH
1142 SSL_CTX_set_tlsext_servername_callback(*ctxp, tls_servername_cb);
1143 SSL_CTX_set_tlsext_servername_arg(*ctxp, cbinfo);
3f0945ff 1144 }
f2de3a33 1145# ifndef DISABLE_OCSP
f5d78688
JH
1146else /* client */
1147 if(ocsp_file) /* wanting stapling */
1148 {
1149 if (!(cbinfo->u_ocsp.client.verify_store = X509_STORE_new()))
1150 {
1151 DEBUG(D_tls) debug_printf("failed to create store for stapling verify\n");
1152 return FAIL;
1153 }
1154 SSL_CTX_set_tlsext_status_cb(*ctxp, tls_client_stapling_cb);
1155 SSL_CTX_set_tlsext_status_arg(*ctxp, cbinfo);
1156 }
1157# endif
7be682ca 1158#endif
059ec3d9 1159
e51c7be2
JH
1160#ifdef EXPERIMENTAL_CERTNAMES
1161cbinfo->verify_cert_hostnames = NULL;
1162#endif
1163
059ec3d9
PH
1164/* Set up the RSA callback */
1165
817d9f57 1166SSL_CTX_set_tmp_rsa_callback(*ctxp, rsa_callback);
059ec3d9
PH
1167
1168/* Finally, set the timeout, and we are done */
1169
817d9f57 1170SSL_CTX_set_timeout(*ctxp, ssl_session_timeout);
059ec3d9 1171DEBUG(D_tls) debug_printf("Initialized TLS\n");
7be682ca 1172
817d9f57 1173*cbp = cbinfo;
7be682ca 1174
059ec3d9
PH
1175return OK;
1176}
1177
1178
1179
1180
1181/*************************************************
1182* Get name of cipher in use *
1183*************************************************/
1184
817d9f57 1185/*
059ec3d9 1186Argument: pointer to an SSL structure for the connection
817d9f57
JH
1187 buffer to use for answer
1188 size of buffer
1189 pointer to number of bits for cipher
059ec3d9
PH
1190Returns: nothing
1191*/
1192
1193static void
817d9f57 1194construct_cipher_name(SSL *ssl, uschar *cipherbuf, int bsize, int *bits)
059ec3d9 1195{
57b3a7f5
PP
1196/* With OpenSSL 1.0.0a, this needs to be const but the documentation doesn't
1197yet reflect that. It should be a safe change anyway, even 0.9.8 versions have
1198the accessor functions use const in the prototype. */
1199const SSL_CIPHER *c;
d9784128 1200const uschar *ver;
059ec3d9 1201
d9784128 1202ver = (const uschar *)SSL_get_version(ssl);
059ec3d9 1203
57b3a7f5 1204c = (const SSL_CIPHER *) SSL_get_current_cipher(ssl);
817d9f57 1205SSL_CIPHER_get_bits(c, bits);
059ec3d9 1206
817d9f57
JH
1207string_format(cipherbuf, bsize, "%s:%s:%u", ver,
1208 SSL_CIPHER_get_name(c), *bits);
059ec3d9
PH
1209
1210DEBUG(D_tls) debug_printf("Cipher: %s\n", cipherbuf);
1211}
1212
1213
1214
1215
1216
1217/*************************************************
1218* Set up for verifying certificates *
1219*************************************************/
1220
1221/* Called by both client and server startup
1222
1223Arguments:
7be682ca 1224 sctx SSL_CTX* to initialise
059ec3d9
PH
1225 certs certs file or NULL
1226 crl CRL file or NULL
1227 host NULL in a server; the remote host in a client
1228 optional TRUE if called from a server for a host in tls_try_verify_hosts;
1229 otherwise passed as FALSE
983207c1 1230 cert_vfy_cb Callback function for certificate verification
059ec3d9
PH
1231
1232Returns: OK/DEFER/FAIL
1233*/
1234
1235static int
983207c1
JH
1236setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host, BOOL optional,
1237 int (*cert_vfy_cb)(int, X509_STORE_CTX *) )
059ec3d9
PH
1238{
1239uschar *expcerts, *expcrl;
1240
1241if (!expand_check(certs, US"tls_verify_certificates", &expcerts))
1242 return DEFER;
1243
26e72755 1244if (expcerts != NULL && *expcerts != '\0')
059ec3d9
PH
1245 {
1246 struct stat statbuf;
7be682ca 1247 if (!SSL_CTX_set_default_verify_paths(sctx))
7199e1ee 1248 return tls_error(US"SSL_CTX_set_default_verify_paths", host, NULL);
059ec3d9
PH
1249
1250 if (Ustat(expcerts, &statbuf) < 0)
1251 {
1252 log_write(0, LOG_MAIN|LOG_PANIC,
1253 "failed to stat %s for certificates", expcerts);
1254 return DEFER;
1255 }
1256 else
1257 {
1258 uschar *file, *dir;
1259 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1260 { file = NULL; dir = expcerts; }
1261 else
1262 { file = expcerts; dir = NULL; }
1263
1264 /* If a certificate file is empty, the next function fails with an
1265 unhelpful error message. If we skip it, we get the correct behaviour (no
1266 certificates are recognized, but the error message is still misleading (it
1267 says no certificate was supplied.) But this is better. */
1268
1269 if ((file == NULL || statbuf.st_size > 0) &&
7be682ca 1270 !SSL_CTX_load_verify_locations(sctx, CS file, CS dir))
7199e1ee 1271 return tls_error(US"SSL_CTX_load_verify_locations", host, NULL);
059ec3d9
PH
1272
1273 if (file != NULL)
1274 {
7be682ca 1275 SSL_CTX_set_client_CA_list(sctx, SSL_load_client_CA_file(CS file));
059ec3d9
PH
1276 }
1277 }
1278
1279 /* Handle a certificate revocation list. */
1280
1281 #if OPENSSL_VERSION_NUMBER > 0x00907000L
1282
8b417f2c
PH
1283 /* This bit of code is now the version supplied by Lars Mainka. (I have
1284 * merely reformatted it into the Exim code style.)
1285
1286 * "From here I changed the code to add support for multiple crl's
1287 * in pem format in one file or to support hashed directory entries in
1288 * pem format instead of a file. This method now uses the library function
1289 * X509_STORE_load_locations to add the CRL location to the SSL context.
1290 * OpenSSL will then handle the verify against CA certs and CRLs by
1291 * itself in the verify callback." */
1292
059ec3d9
PH
1293 if (!expand_check(crl, US"tls_crl", &expcrl)) return DEFER;
1294 if (expcrl != NULL && *expcrl != 0)
1295 {
8b417f2c
PH
1296 struct stat statbufcrl;
1297 if (Ustat(expcrl, &statbufcrl) < 0)
1298 {
1299 log_write(0, LOG_MAIN|LOG_PANIC,
1300 "failed to stat %s for certificates revocation lists", expcrl);
1301 return DEFER;
1302 }
1303 else
059ec3d9 1304 {
8b417f2c
PH
1305 /* is it a file or directory? */
1306 uschar *file, *dir;
7be682ca 1307 X509_STORE *cvstore = SSL_CTX_get_cert_store(sctx);
8b417f2c 1308 if ((statbufcrl.st_mode & S_IFMT) == S_IFDIR)
059ec3d9 1309 {
8b417f2c
PH
1310 file = NULL;
1311 dir = expcrl;
1312 DEBUG(D_tls) debug_printf("SSL CRL value is a directory %s\n", dir);
059ec3d9
PH
1313 }
1314 else
1315 {
8b417f2c
PH
1316 file = expcrl;
1317 dir = NULL;
1318 DEBUG(D_tls) debug_printf("SSL CRL value is a file %s\n", file);
059ec3d9 1319 }
8b417f2c 1320 if (X509_STORE_load_locations(cvstore, CS file, CS dir) == 0)
7199e1ee 1321 return tls_error(US"X509_STORE_load_locations", host, NULL);
8b417f2c
PH
1322
1323 /* setting the flags to check against the complete crl chain */
1324
1325 X509_STORE_set_flags(cvstore,
1326 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
059ec3d9 1327 }
059ec3d9
PH
1328 }
1329
1330 #endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1331
1332 /* If verification is optional, don't fail if no certificate */
1333
7be682ca 1334 SSL_CTX_set_verify(sctx,
059ec3d9 1335 SSL_VERIFY_PEER | (optional? 0 : SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
983207c1 1336 cert_vfy_cb);
059ec3d9
PH
1337 }
1338
1339return OK;
1340}
1341
1342
1343
1344/*************************************************
1345* Start a TLS session in a server *
1346*************************************************/
1347
1348/* This is called when Exim is running as a server, after having received
1349the STARTTLS command. It must respond to that command, and then negotiate
1350a TLS session.
1351
1352Arguments:
1353 require_ciphers allowed ciphers
1354
1355Returns: OK on success
1356 DEFER for errors before the start of the negotiation
1357 FAIL for errors during the negotation; the server can't
1358 continue running.
1359*/
1360
1361int
17c76198 1362tls_server_start(const uschar *require_ciphers)
059ec3d9
PH
1363{
1364int rc;
1365uschar *expciphers;
7be682ca 1366tls_ext_ctx_cb *cbinfo;
817d9f57 1367static uschar cipherbuf[256];
059ec3d9
PH
1368
1369/* Check for previous activation */
1370
817d9f57 1371if (tls_in.active >= 0)
059ec3d9 1372 {
5ca6d115 1373 tls_error(US"STARTTLS received after TLS started", NULL, US"");
059ec3d9
PH
1374 smtp_printf("554 Already in TLS\r\n");
1375 return FAIL;
1376 }
1377
1378/* Initialize the SSL library. If it fails, it will already have logged
1379the error. */
1380
817d9f57 1381rc = tls_init(&server_ctx, NULL, tls_dhparam, tls_certificate, tls_privatekey,
f2de3a33 1382#ifndef DISABLE_OCSP
3f7eeb86
PP
1383 tls_ocsp_file,
1384#endif
817d9f57 1385 NULL, &server_static_cbinfo);
059ec3d9 1386if (rc != OK) return rc;
817d9f57 1387cbinfo = server_static_cbinfo;
059ec3d9
PH
1388
1389if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers))
1390 return FAIL;
1391
1392/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
17c76198
PP
1393were historically separated by underscores. So that I can use either form in my
1394tests, and also for general convenience, we turn underscores into hyphens here.
1395*/
059ec3d9
PH
1396
1397if (expciphers != NULL)
1398 {
1399 uschar *s = expciphers;
1400 while (*s != 0) { if (*s == '_') *s = '-'; s++; }
1401 DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
817d9f57 1402 if (!SSL_CTX_set_cipher_list(server_ctx, CS expciphers))
7199e1ee 1403 return tls_error(US"SSL_CTX_set_cipher_list", NULL, NULL);
7be682ca 1404 cbinfo->server_cipher_list = expciphers;
059ec3d9
PH
1405 }
1406
1407/* If this is a host for which certificate verification is mandatory or
1408optional, set up appropriately. */
1409
817d9f57 1410tls_in.certificate_verified = FALSE;
a2ff477a 1411server_verify_callback_called = FALSE;
059ec3d9
PH
1412
1413if (verify_check_host(&tls_verify_hosts) == OK)
1414 {
983207c1
JH
1415 rc = setup_certs(server_ctx, tls_verify_certificates, tls_crl, NULL,
1416 FALSE, verify_callback_server);
059ec3d9 1417 if (rc != OK) return rc;
a2ff477a 1418 server_verify_optional = FALSE;
059ec3d9
PH
1419 }
1420else if (verify_check_host(&tls_try_verify_hosts) == OK)
1421 {
983207c1
JH
1422 rc = setup_certs(server_ctx, tls_verify_certificates, tls_crl, NULL,
1423 TRUE, verify_callback_server);
059ec3d9 1424 if (rc != OK) return rc;
a2ff477a 1425 server_verify_optional = TRUE;
059ec3d9
PH
1426 }
1427
1428/* Prepare for new connection */
1429
817d9f57 1430if ((server_ssl = SSL_new(server_ctx)) == NULL) return tls_error(US"SSL_new", NULL, NULL);
da3ad30d
PP
1431
1432/* Warning: we used to SSL_clear(ssl) here, it was removed.
1433 *
1434 * With the SSL_clear(), we get strange interoperability bugs with
1435 * OpenSSL 1.0.1b and TLS1.1/1.2. It looks as though this may be a bug in
1436 * OpenSSL itself, as a clear should not lead to inability to follow protocols.
1437 *
1438 * The SSL_clear() call is to let an existing SSL* be reused, typically after
1439 * session shutdown. In this case, we have a brand new object and there's no
1440 * obvious reason to immediately clear it. I'm guessing that this was
1441 * originally added because of incomplete initialisation which the clear fixed,
1442 * in some historic release.
1443 */
059ec3d9
PH
1444
1445/* Set context and tell client to go ahead, except in the case of TLS startup
1446on connection, where outputting anything now upsets the clients and tends to
1447make them disconnect. We need to have an explicit fflush() here, to force out
1448the response. Other smtp_printf() calls do not need it, because in non-TLS
1449mode, the fflush() happens when smtp_getc() is called. */
1450
817d9f57
JH
1451SSL_set_session_id_context(server_ssl, sid_ctx, Ustrlen(sid_ctx));
1452if (!tls_in.on_connect)
059ec3d9
PH
1453 {
1454 smtp_printf("220 TLS go ahead\r\n");
1455 fflush(smtp_out);
1456 }
1457
1458/* Now negotiate the TLS session. We put our own timer on it, since it seems
1459that the OpenSSL library doesn't. */
1460
817d9f57
JH
1461SSL_set_wfd(server_ssl, fileno(smtp_out));
1462SSL_set_rfd(server_ssl, fileno(smtp_in));
1463SSL_set_accept_state(server_ssl);
059ec3d9
PH
1464
1465DEBUG(D_tls) debug_printf("Calling SSL_accept\n");
1466
1467sigalrm_seen = FALSE;
1468if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
817d9f57 1469rc = SSL_accept(server_ssl);
059ec3d9
PH
1470alarm(0);
1471
1472if (rc <= 0)
1473 {
7199e1ee 1474 tls_error(US"SSL_accept", NULL, sigalrm_seen ? US"timed out" : NULL);
77bb000f
PP
1475 if (ERR_get_error() == 0)
1476 log_write(0, LOG_MAIN,
a053d125 1477 "TLS client disconnected cleanly (rejected our certificate?)");
059ec3d9
PH
1478 return FAIL;
1479 }
1480
1481DEBUG(D_tls) debug_printf("SSL_accept was successful\n");
1482
1483/* TLS has been set up. Adjust the input functions to read via TLS,
1484and initialize things. */
1485
817d9f57
JH
1486construct_cipher_name(server_ssl, cipherbuf, sizeof(cipherbuf), &tls_in.bits);
1487tls_in.cipher = cipherbuf;
059ec3d9
PH
1488
1489DEBUG(D_tls)
1490 {
1491 uschar buf[2048];
817d9f57 1492 if (SSL_get_shared_ciphers(server_ssl, CS buf, sizeof(buf)) != NULL)
059ec3d9
PH
1493 debug_printf("Shared ciphers: %s\n", buf);
1494 }
1495
9d1c15ef
JH
1496/* Record the certificate we presented */
1497 {
1498 X509 * crt = SSL_get_certificate(server_ssl);
1499 tls_in.ourcert = crt ? X509_dup(crt) : NULL;
1500 }
059ec3d9 1501
817d9f57
JH
1502/* Only used by the server-side tls (tls_in), including tls_getc.
1503 Client-side (tls_out) reads (seem to?) go via
1504 smtp_read_response()/ip_recv().
1505 Hence no need to duplicate for _in and _out.
1506 */
059ec3d9
PH
1507ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size);
1508ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0;
1509ssl_xfer_eof = ssl_xfer_error = 0;
1510
1511receive_getc = tls_getc;
1512receive_ungetc = tls_ungetc;
1513receive_feof = tls_feof;
1514receive_ferror = tls_ferror;
58eb016e 1515receive_smtp_buffered = tls_smtp_buffered;
059ec3d9 1516
817d9f57 1517tls_in.active = fileno(smtp_out);
059ec3d9
PH
1518return OK;
1519}
1520
1521
1522
1523
1524
1525/*************************************************
1526* Start a TLS session in a client *
1527*************************************************/
1528
1529/* Called from the smtp transport after STARTTLS has been accepted.
1530
1531Argument:
1532 fd the fd of the connection
1533 host connected host (for messages)
83da1223 1534 addr the first address
65867078 1535 ob smtp transport options
059ec3d9
PH
1536
1537Returns: OK on success
1538 FAIL otherwise - note that tls_error() will not give DEFER
1539 because this is not a server
1540*/
1541
1542int
f5d78688 1543tls_client_start(int fd, host_item *host, address_item *addr,
65867078 1544 void *v_ob)
059ec3d9 1545{
65867078 1546smtp_transport_options_block * ob = v_ob;
059ec3d9
PH
1547static uschar txt[256];
1548uschar *expciphers;
1549X509* server_cert;
1550int rc;
817d9f57 1551static uschar cipherbuf[256];
f2de3a33 1552#ifndef DISABLE_OCSP
65867078 1553BOOL require_ocsp = verify_check_this_host(&ob->hosts_require_ocsp,
f5d78688 1554 NULL, host->name, host->address, NULL) == OK;
44662487
JH
1555BOOL request_ocsp = require_ocsp ? TRUE
1556 : verify_check_this_host(&ob->hosts_request_ocsp,
1557 NULL, host->name, host->address, NULL) == OK;
f5d78688 1558#endif
059ec3d9 1559
65867078
JH
1560rc = tls_init(&client_ctx, host, NULL,
1561 ob->tls_certificate, ob->tls_privatekey,
f2de3a33 1562#ifndef DISABLE_OCSP
44662487 1563 (void *)(long)request_ocsp,
3f7eeb86 1564#endif
817d9f57 1565 addr, &client_static_cbinfo);
059ec3d9
PH
1566if (rc != OK) return rc;
1567
817d9f57 1568tls_out.certificate_verified = FALSE;
a2ff477a 1569client_verify_callback_called = FALSE;
059ec3d9 1570
65867078
JH
1571if (!expand_check(ob->tls_require_ciphers, US"tls_require_ciphers",
1572 &expciphers))
059ec3d9
PH
1573 return FAIL;
1574
1575/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
1576are separated by underscores. So that I can use either form in my tests, and
1577also for general convenience, we turn underscores into hyphens here. */
1578
1579if (expciphers != NULL)
1580 {
1581 uschar *s = expciphers;
1582 while (*s != 0) { if (*s == '_') *s = '-'; s++; }
1583 DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
817d9f57 1584 if (!SSL_CTX_set_cipher_list(client_ctx, CS expciphers))
7199e1ee 1585 return tls_error(US"SSL_CTX_set_cipher_list", host, NULL);
059ec3d9
PH
1586 }
1587
a63be306 1588/* stick to the old behaviour for compatibility if tls_verify_certificates is
65867078 1589 set but both tls_verify_hosts and tls_try_verify_hosts is not set. Check only
a63be306 1590 the specified host patterns if one of them is defined */
e51c7be2 1591
65867078
JH
1592if ((!ob->tls_verify_hosts && !ob->tls_try_verify_hosts) ||
1593 (verify_check_host(&ob->tls_verify_hosts) == OK))
a63be306 1594 {
65867078
JH
1595 if ((rc = setup_certs(client_ctx, ob->tls_verify_certificates,
1596 ob->tls_crl, host, FALSE, verify_callback_client)) != OK)
1597 return rc;
a63be306 1598 client_verify_optional = FALSE;
e51c7be2
JH
1599
1600#ifdef EXPERIMENTAL_CERTNAMES
1601 if (ob->tls_verify_cert_hostnames)
1602 {
1603 if (!expand_check(ob->tls_verify_cert_hostnames,
1604 US"tls_verify_cert_hostnames",
1605 &client_static_cbinfo->verify_cert_hostnames))
1606 return FAIL;
1607 if (client_static_cbinfo->verify_cert_hostnames)
1608 DEBUG(D_tls) debug_printf("Cert hostname to check: \"%s\"\n",
1609 client_static_cbinfo->verify_cert_hostnames);
1610 }
1611#endif
a63be306 1612 }
65867078 1613else if (verify_check_host(&ob->tls_try_verify_hosts) == OK)
a63be306 1614 {
65867078
JH
1615 if ((rc = setup_certs(client_ctx, ob->tls_verify_certificates,
1616 ob->tls_crl, host, TRUE, verify_callback_client)) != OK)
1617 return rc;
a63be306
WB
1618 client_verify_optional = TRUE;
1619 }
059ec3d9 1620
65867078
JH
1621if ((client_ssl = SSL_new(client_ctx)) == NULL)
1622 return tls_error(US"SSL_new", host, NULL);
817d9f57
JH
1623SSL_set_session_id_context(client_ssl, sid_ctx, Ustrlen(sid_ctx));
1624SSL_set_fd(client_ssl, fd);
1625SSL_set_connect_state(client_ssl);
059ec3d9 1626
65867078 1627if (ob->tls_sni)
3f0945ff 1628 {
65867078 1629 if (!expand_check(ob->tls_sni, US"tls_sni", &tls_out.sni))
3f0945ff 1630 return FAIL;
ec4b68e5 1631 if (tls_out.sni == NULL)
2c9a0e86
PP
1632 {
1633 DEBUG(D_tls) debug_printf("Setting TLS SNI forced to fail, not sending\n");
1634 }
ec4b68e5 1635 else if (!Ustrlen(tls_out.sni))
817d9f57 1636 tls_out.sni = NULL;
3f0945ff
PP
1637 else
1638 {
35731706 1639#ifdef EXIM_HAVE_OPENSSL_TLSEXT
817d9f57
JH
1640 DEBUG(D_tls) debug_printf("Setting TLS SNI \"%s\"\n", tls_out.sni);
1641 SSL_set_tlsext_host_name(client_ssl, tls_out.sni);
35731706
PP
1642#else
1643 DEBUG(D_tls)
1644 debug_printf("OpenSSL at build-time lacked SNI support, ignoring \"%s\"\n",
02d9264f 1645 tls_out.sni);
35731706 1646#endif
3f0945ff
PP
1647 }
1648 }
1649
f2de3a33 1650#ifndef DISABLE_OCSP
f5d78688
JH
1651/* Request certificate status at connection-time. If the server
1652does OCSP stapling we will get the callback (set in tls_init()) */
44662487
JH
1653if (request_ocsp)
1654 {
f5d78688 1655 SSL_set_tlsext_status_type(client_ssl, TLSEXT_STATUSTYPE_ocsp);
44662487
JH
1656 client_static_cbinfo->u_ocsp.client.verify_required = require_ocsp;
1657 tls_out.ocsp = OCSP_NOT_RESP;
1658 }
f5d78688
JH
1659#endif
1660
059ec3d9
PH
1661/* There doesn't seem to be a built-in timeout on connection. */
1662
1663DEBUG(D_tls) debug_printf("Calling SSL_connect\n");
1664sigalrm_seen = FALSE;
65867078 1665alarm(ob->command_timeout);
817d9f57 1666rc = SSL_connect(client_ssl);
059ec3d9
PH
1667alarm(0);
1668
1669if (rc <= 0)
7199e1ee 1670 return tls_error(US"SSL_connect", host, sigalrm_seen ? US"timed out" : NULL);
059ec3d9
PH
1671
1672DEBUG(D_tls) debug_printf("SSL_connect succeeded\n");
1673
453a6645 1674/* Beware anonymous ciphers which lead to server_cert being NULL */
9d1c15ef 1675/*XXX server_cert is never freed... use X509_free() */
817d9f57 1676server_cert = SSL_get_peer_certificate (client_ssl);
453a6645
PP
1677if (server_cert)
1678 {
817d9f57 1679 tls_out.peerdn = US X509_NAME_oneline(X509_get_subject_name(server_cert),
453a6645 1680 CS txt, sizeof(txt));
9d1c15ef 1681 tls_out.peerdn = txt; /*XXX a static buffer... */
453a6645
PP
1682 }
1683else
817d9f57 1684 tls_out.peerdn = NULL;
059ec3d9 1685
817d9f57
JH
1686construct_cipher_name(client_ssl, cipherbuf, sizeof(cipherbuf), &tls_out.bits);
1687tls_out.cipher = cipherbuf;
059ec3d9 1688
9d1c15ef
JH
1689/* Record the certificate we presented */
1690 {
1691 X509 * crt = SSL_get_certificate(client_ssl);
1692 tls_out.ourcert = crt ? X509_dup(crt) : NULL;
1693 }
1694
817d9f57 1695tls_out.active = fd;
059ec3d9
PH
1696return OK;
1697}
1698
1699
1700
1701
1702
1703/*************************************************
1704* TLS version of getc *
1705*************************************************/
1706
1707/* This gets the next byte from the TLS input buffer. If the buffer is empty,
1708it refills the buffer via the SSL reading function.
1709
1710Arguments: none
1711Returns: the next character or EOF
817d9f57
JH
1712
1713Only used by the server-side TLS.
059ec3d9
PH
1714*/
1715
1716int
1717tls_getc(void)
1718{
1719if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
1720 {
1721 int error;
1722 int inbytes;
1723
817d9f57 1724 DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", server_ssl,
c80c5570 1725 ssl_xfer_buffer, ssl_xfer_buffer_size);
059ec3d9
PH
1726
1727 if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
817d9f57
JH
1728 inbytes = SSL_read(server_ssl, CS ssl_xfer_buffer, ssl_xfer_buffer_size);
1729 error = SSL_get_error(server_ssl, inbytes);
059ec3d9
PH
1730 alarm(0);
1731
1732 /* SSL_ERROR_ZERO_RETURN appears to mean that the SSL session has been
1733 closed down, not that the socket itself has been closed down. Revert to
1734 non-SSL handling. */
1735
1736 if (error == SSL_ERROR_ZERO_RETURN)
1737 {
1738 DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
1739
1740 receive_getc = smtp_getc;
1741 receive_ungetc = smtp_ungetc;
1742 receive_feof = smtp_feof;
1743 receive_ferror = smtp_ferror;
58eb016e 1744 receive_smtp_buffered = smtp_buffered;
059ec3d9 1745
817d9f57
JH
1746 SSL_free(server_ssl);
1747 server_ssl = NULL;
1748 tls_in.active = -1;
1749 tls_in.bits = 0;
1750 tls_in.cipher = NULL;
1751 tls_in.peerdn = NULL;
1752 tls_in.sni = NULL;
059ec3d9
PH
1753
1754 return smtp_getc();
1755 }
1756
1757 /* Handle genuine errors */
1758
ba084640
PP
1759 else if (error == SSL_ERROR_SSL)
1760 {
1761 ERR_error_string(ERR_get_error(), ssl_errstring);
89dd51cd 1762 log_write(0, LOG_MAIN, "TLS error (SSL_read): %s", ssl_errstring);
ba084640
PP
1763 ssl_xfer_error = 1;
1764 return EOF;
1765 }
1766
059ec3d9
PH
1767 else if (error != SSL_ERROR_NONE)
1768 {
1769 DEBUG(D_tls) debug_printf("Got SSL error %d\n", error);
1770 ssl_xfer_error = 1;
1771 return EOF;
1772 }
c80c5570 1773
80a47a2c
TK
1774#ifndef DISABLE_DKIM
1775 dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
1776#endif
059ec3d9
PH
1777 ssl_xfer_buffer_hwm = inbytes;
1778 ssl_xfer_buffer_lwm = 0;
1779 }
1780
1781/* Something in the buffer; return next uschar */
1782
1783return ssl_xfer_buffer[ssl_xfer_buffer_lwm++];
1784}
1785
1786
1787
1788/*************************************************
1789* Read bytes from TLS channel *
1790*************************************************/
1791
1792/*
1793Arguments:
1794 buff buffer of data
1795 len size of buffer
1796
1797Returns: the number of bytes read
1798 -1 after a failed read
817d9f57
JH
1799
1800Only used by the client-side TLS.
059ec3d9
PH
1801*/
1802
1803int
389ca47a 1804tls_read(BOOL is_server, uschar *buff, size_t len)
059ec3d9 1805{
389ca47a 1806SSL *ssl = is_server ? server_ssl : client_ssl;
059ec3d9
PH
1807int inbytes;
1808int error;
1809
389ca47a 1810DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
c80c5570 1811 buff, (unsigned int)len);
059ec3d9 1812
389ca47a
JH
1813inbytes = SSL_read(ssl, CS buff, len);
1814error = SSL_get_error(ssl, inbytes);
059ec3d9
PH
1815
1816if (error == SSL_ERROR_ZERO_RETURN)
1817 {
1818 DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
1819 return -1;
1820 }
1821else if (error != SSL_ERROR_NONE)
1822 {
1823 return -1;
1824 }
1825
1826return inbytes;
1827}
1828
1829
1830
1831
1832
1833/*************************************************
1834* Write bytes down TLS channel *
1835*************************************************/
1836
1837/*
1838Arguments:
817d9f57 1839 is_server channel specifier
059ec3d9
PH
1840 buff buffer of data
1841 len number of bytes
1842
1843Returns: the number of bytes after a successful write,
1844 -1 after a failed write
817d9f57
JH
1845
1846Used by both server-side and client-side TLS.
059ec3d9
PH
1847*/
1848
1849int
817d9f57 1850tls_write(BOOL is_server, const uschar *buff, size_t len)
059ec3d9
PH
1851{
1852int outbytes;
1853int error;
1854int left = len;
817d9f57 1855SSL *ssl = is_server ? server_ssl : client_ssl;
059ec3d9 1856
c80c5570 1857DEBUG(D_tls) debug_printf("tls_do_write(%p, %d)\n", buff, left);
059ec3d9
PH
1858while (left > 0)
1859 {
c80c5570 1860 DEBUG(D_tls) debug_printf("SSL_write(SSL, %p, %d)\n", buff, left);
059ec3d9
PH
1861 outbytes = SSL_write(ssl, CS buff, left);
1862 error = SSL_get_error(ssl, outbytes);
1863 DEBUG(D_tls) debug_printf("outbytes=%d error=%d\n", outbytes, error);
1864 switch (error)
1865 {
1866 case SSL_ERROR_SSL:
1867 ERR_error_string(ERR_get_error(), ssl_errstring);
1868 log_write(0, LOG_MAIN, "TLS error (SSL_write): %s", ssl_errstring);
1869 return -1;
1870
1871 case SSL_ERROR_NONE:
1872 left -= outbytes;
1873 buff += outbytes;
1874 break;
1875
1876 case SSL_ERROR_ZERO_RETURN:
1877 log_write(0, LOG_MAIN, "SSL channel closed on write");
1878 return -1;
1879
817d9f57
JH
1880 case SSL_ERROR_SYSCALL:
1881 log_write(0, LOG_MAIN, "SSL_write: (from %s) syscall: %s",
1882 sender_fullhost ? sender_fullhost : US"<unknown>",
1883 strerror(errno));
1884
059ec3d9
PH
1885 default:
1886 log_write(0, LOG_MAIN, "SSL_write error %d", error);
1887 return -1;
1888 }
1889 }
1890return len;
1891}
1892
1893
1894
1895/*************************************************
1896* Close down a TLS session *
1897*************************************************/
1898
1899/* This is also called from within a delivery subprocess forked from the
1900daemon, to shut down the TLS library, without actually doing a shutdown (which
1901would tamper with the SSL session in the parent process).
1902
1903Arguments: TRUE if SSL_shutdown is to be called
1904Returns: nothing
817d9f57
JH
1905
1906Used by both server-side and client-side TLS.
059ec3d9
PH
1907*/
1908
1909void
817d9f57 1910tls_close(BOOL is_server, BOOL shutdown)
059ec3d9 1911{
817d9f57 1912SSL **sslp = is_server ? &server_ssl : &client_ssl;
389ca47a 1913int *fdp = is_server ? &tls_in.active : &tls_out.active;
817d9f57
JH
1914
1915if (*fdp < 0) return; /* TLS was not active */
059ec3d9
PH
1916
1917if (shutdown)
1918 {
1919 DEBUG(D_tls) debug_printf("tls_close(): shutting down SSL\n");
817d9f57 1920 SSL_shutdown(*sslp);
059ec3d9
PH
1921 }
1922
817d9f57
JH
1923SSL_free(*sslp);
1924*sslp = NULL;
059ec3d9 1925
817d9f57 1926*fdp = -1;
059ec3d9
PH
1927}
1928
36f12725
NM
1929
1930
1931
3375e053
PP
1932/*************************************************
1933* Let tls_require_ciphers be checked at startup *
1934*************************************************/
1935
1936/* The tls_require_ciphers option, if set, must be something which the
1937library can parse.
1938
1939Returns: NULL on success, or error message
1940*/
1941
1942uschar *
1943tls_validate_require_cipher(void)
1944{
1945SSL_CTX *ctx;
1946uschar *s, *expciphers, *err;
1947
1948/* this duplicates from tls_init(), we need a better "init just global
1949state, for no specific purpose" singleton function of our own */
1950
1951SSL_load_error_strings();
1952OpenSSL_add_ssl_algorithms();
1953#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
1954/* SHA256 is becoming ever more popular. This makes sure it gets added to the
1955list of available digests. */
1956EVP_add_digest(EVP_sha256());
1957#endif
1958
1959if (!(tls_require_ciphers && *tls_require_ciphers))
1960 return NULL;
1961
1962if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers))
1963 return US"failed to expand tls_require_ciphers";
1964
1965if (!(expciphers && *expciphers))
1966 return NULL;
1967
1968/* normalisation ripped from above */
1969s = expciphers;
1970while (*s != 0) { if (*s == '_') *s = '-'; s++; }
1971
1972err = NULL;
1973
1974ctx = SSL_CTX_new(SSLv23_server_method());
1975if (!ctx)
1976 {
1977 ERR_error_string(ERR_get_error(), ssl_errstring);
1978 return string_sprintf("SSL_CTX_new() failed: %s", ssl_errstring);
1979 }
1980
1981DEBUG(D_tls)
1982 debug_printf("tls_require_ciphers expands to \"%s\"\n", expciphers);
1983
1984if (!SSL_CTX_set_cipher_list(ctx, CS expciphers))
1985 {
1986 ERR_error_string(ERR_get_error(), ssl_errstring);
1987 err = string_sprintf("SSL_CTX_set_cipher_list(%s) failed", expciphers);
1988 }
1989
1990SSL_CTX_free(ctx);
1991
1992return err;
1993}
1994
1995
1996
1997
36f12725
NM
1998/*************************************************
1999* Report the library versions. *
2000*************************************************/
2001
2002/* There have historically been some issues with binary compatibility in
2003OpenSSL libraries; if Exim (like many other applications) is built against
2004one version of OpenSSL but the run-time linker picks up another version,
2005it can result in serious failures, including crashing with a SIGSEGV. So
2006report the version found by the compiler and the run-time version.
2007
f64a1e23
PP
2008Note: some OS vendors backport security fixes without changing the version
2009number/string, and the version date remains unchanged. The _build_ date
2010will change, so we can more usefully assist with version diagnosis by also
2011reporting the build date.
2012
36f12725
NM
2013Arguments: a FILE* to print the results to
2014Returns: nothing
2015*/
2016
2017void
2018tls_version_report(FILE *f)
2019{
754a0503 2020fprintf(f, "Library version: OpenSSL: Compile: %s\n"
f64a1e23
PP
2021 " Runtime: %s\n"
2022 " : %s\n",
754a0503 2023 OPENSSL_VERSION_TEXT,
f64a1e23
PP
2024 SSLeay_version(SSLEAY_VERSION),
2025 SSLeay_version(SSLEAY_BUILT_ON));
2026/* third line is 38 characters for the %s and the line is 73 chars long;
2027the OpenSSL output includes a "built on: " prefix already. */
36f12725
NM
2028}
2029
9e3331ea
TK
2030
2031
2032
2033/*************************************************
17c76198 2034* Random number generation *
9e3331ea
TK
2035*************************************************/
2036
2037/* Pseudo-random number generation. The result is not expected to be
2038cryptographically strong but not so weak that someone will shoot themselves
2039in the foot using it as a nonce in input in some email header scheme or
2040whatever weirdness they'll twist this into. The result should handle fork()
2041and avoid repeating sequences. OpenSSL handles that for us.
2042
2043Arguments:
2044 max range maximum
2045Returns a random number in range [0, max-1]
2046*/
2047
2048int
17c76198 2049vaguely_random_number(int max)
9e3331ea
TK
2050{
2051unsigned int r;
2052int i, needed_len;
de6135a0
PP
2053static pid_t pidlast = 0;
2054pid_t pidnow;
9e3331ea
TK
2055uschar *p;
2056uschar smallbuf[sizeof(r)];
2057
2058if (max <= 1)
2059 return 0;
2060
de6135a0
PP
2061pidnow = getpid();
2062if (pidnow != pidlast)
2063 {
2064 /* Although OpenSSL documents that "OpenSSL makes sure that the PRNG state
2065 is unique for each thread", this doesn't apparently apply across processes,
2066 so our own warning from vaguely_random_number_fallback() applies here too.
2067 Fix per PostgreSQL. */
2068 if (pidlast != 0)
2069 RAND_cleanup();
2070 pidlast = pidnow;
2071 }
2072
9e3331ea
TK
2073/* OpenSSL auto-seeds from /dev/random, etc, but this a double-check. */
2074if (!RAND_status())
2075 {
2076 randstuff r;
2077 gettimeofday(&r.tv, NULL);
2078 r.p = getpid();
2079
2080 RAND_seed((uschar *)(&r), sizeof(r));
2081 }
2082/* We're after pseudo-random, not random; if we still don't have enough data
2083in the internal PRNG then our options are limited. We could sleep and hope
2084for entropy to come along (prayer technique) but if the system is so depleted
2085in the first place then something is likely to just keep taking it. Instead,
2086we'll just take whatever little bit of pseudo-random we can still manage to
2087get. */
2088
2089needed_len = sizeof(r);
2090/* Don't take 8 times more entropy than needed if int is 8 octets and we were
2091asked for a number less than 10. */
2092for (r = max, i = 0; r; ++i)
2093 r >>= 1;
2094i = (i + 7) / 8;
2095if (i < needed_len)
2096 needed_len = i;
2097
2098/* We do not care if crypto-strong */
17c76198
PP
2099i = RAND_pseudo_bytes(smallbuf, needed_len);
2100if (i < 0)
2101 {
2102 DEBUG(D_all)
2103 debug_printf("OpenSSL RAND_pseudo_bytes() not supported by RAND method, using fallback.\n");
2104 return vaguely_random_number_fallback(max);
2105 }
2106
9e3331ea
TK
2107r = 0;
2108for (p = smallbuf; needed_len; --needed_len, ++p)
2109 {
2110 r *= 256;
2111 r += *p;
2112 }
2113
2114/* We don't particularly care about weighted results; if someone wants
2115smooth distribution and cares enough then they should submit a patch then. */
2116return r % max;
2117}
2118
77bb000f
PP
2119
2120
2121
2122/*************************************************
2123* OpenSSL option parse *
2124*************************************************/
2125
2126/* Parse one option for tls_openssl_options_parse below
2127
2128Arguments:
2129 name one option name
2130 value place to store a value for it
2131Returns success or failure in parsing
2132*/
2133
2134struct exim_openssl_option {
2135 uschar *name;
2136 long value;
2137};
2138/* We could use a macro to expand, but we need the ifdef and not all the
2139options document which version they were introduced in. Policylet: include
2140all options unless explicitly for DTLS, let the administrator choose which
2141to apply.
2142
2143This list is current as of:
e2fbf4a2
PP
2144 ==> 1.0.1b <==
2145Plus SSL_OP_SAFARI_ECDHE_ECDSA_BUG from 2013-June patch/discussion on openssl-dev
2146*/
77bb000f
PP
2147static struct exim_openssl_option exim_openssl_options[] = {
2148/* KEEP SORTED ALPHABETICALLY! */
2149#ifdef SSL_OP_ALL
73a46702 2150 { US"all", SSL_OP_ALL },
77bb000f
PP
2151#endif
2152#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
73a46702 2153 { US"allow_unsafe_legacy_renegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION },
77bb000f
PP
2154#endif
2155#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
73a46702 2156 { US"cipher_server_preference", SSL_OP_CIPHER_SERVER_PREFERENCE },
77bb000f
PP
2157#endif
2158#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
73a46702 2159 { US"dont_insert_empty_fragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS },
77bb000f
PP
2160#endif
2161#ifdef SSL_OP_EPHEMERAL_RSA
73a46702 2162 { US"ephemeral_rsa", SSL_OP_EPHEMERAL_RSA },
77bb000f
PP
2163#endif
2164#ifdef SSL_OP_LEGACY_SERVER_CONNECT
73a46702 2165 { US"legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT },
77bb000f
PP
2166#endif
2167#ifdef SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
73a46702 2168 { US"microsoft_big_sslv3_buffer", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER },
77bb000f
PP
2169#endif
2170#ifdef SSL_OP_MICROSOFT_SESS_ID_BUG
73a46702 2171 { US"microsoft_sess_id_bug", SSL_OP_MICROSOFT_SESS_ID_BUG },
77bb000f
PP
2172#endif
2173#ifdef SSL_OP_MSIE_SSLV2_RSA_PADDING
73a46702 2174 { US"msie_sslv2_rsa_padding", SSL_OP_MSIE_SSLV2_RSA_PADDING },
77bb000f
PP
2175#endif
2176#ifdef SSL_OP_NETSCAPE_CHALLENGE_BUG
73a46702 2177 { US"netscape_challenge_bug", SSL_OP_NETSCAPE_CHALLENGE_BUG },
77bb000f
PP
2178#endif
2179#ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
73a46702 2180 { US"netscape_reuse_cipher_change_bug", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG },
77bb000f 2181#endif
c80c5570
PP
2182#ifdef SSL_OP_NO_COMPRESSION
2183 { US"no_compression", SSL_OP_NO_COMPRESSION },
2184#endif
77bb000f 2185#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
73a46702 2186 { US"no_session_resumption_on_renegotiation", SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION },
77bb000f 2187#endif
c0c7b2da
PP
2188#ifdef SSL_OP_NO_SSLv2
2189 { US"no_sslv2", SSL_OP_NO_SSLv2 },
2190#endif
2191#ifdef SSL_OP_NO_SSLv3
2192 { US"no_sslv3", SSL_OP_NO_SSLv3 },
2193#endif
2194#ifdef SSL_OP_NO_TICKET
2195 { US"no_ticket", SSL_OP_NO_TICKET },
2196#endif
2197#ifdef SSL_OP_NO_TLSv1
2198 { US"no_tlsv1", SSL_OP_NO_TLSv1 },
2199#endif
c80c5570
PP
2200#ifdef SSL_OP_NO_TLSv1_1
2201#if SSL_OP_NO_TLSv1_1 == 0x00000400L
2202 /* Error in chosen value in 1.0.1a; see first item in CHANGES for 1.0.1b */
2203#warning OpenSSL 1.0.1a uses a bad value for SSL_OP_NO_TLSv1_1, ignoring
2204#else
2205 { US"no_tlsv1_1", SSL_OP_NO_TLSv1_1 },
2206#endif
2207#endif
2208#ifdef SSL_OP_NO_TLSv1_2
2209 { US"no_tlsv1_2", SSL_OP_NO_TLSv1_2 },
2210#endif
e2fbf4a2
PP
2211#ifdef SSL_OP_SAFARI_ECDHE_ECDSA_BUG
2212 { US"safari_ecdhe_ecdsa_bug", SSL_OP_SAFARI_ECDHE_ECDSA_BUG },
2213#endif
77bb000f 2214#ifdef SSL_OP_SINGLE_DH_USE
73a46702 2215 { US"single_dh_use", SSL_OP_SINGLE_DH_USE },
77bb000f
PP
2216#endif
2217#ifdef SSL_OP_SINGLE_ECDH_USE
73a46702 2218 { US"single_ecdh_use", SSL_OP_SINGLE_ECDH_USE },
77bb000f
PP
2219#endif
2220#ifdef SSL_OP_SSLEAY_080_CLIENT_DH_BUG
73a46702 2221 { US"ssleay_080_client_dh_bug", SSL_OP_SSLEAY_080_CLIENT_DH_BUG },
77bb000f
PP
2222#endif
2223#ifdef SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
73a46702 2224 { US"sslref2_reuse_cert_type_bug", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG },
77bb000f
PP
2225#endif
2226#ifdef SSL_OP_TLS_BLOCK_PADDING_BUG
73a46702 2227 { US"tls_block_padding_bug", SSL_OP_TLS_BLOCK_PADDING_BUG },
77bb000f
PP
2228#endif
2229#ifdef SSL_OP_TLS_D5_BUG
73a46702 2230 { US"tls_d5_bug", SSL_OP_TLS_D5_BUG },
77bb000f
PP
2231#endif
2232#ifdef SSL_OP_TLS_ROLLBACK_BUG
73a46702 2233 { US"tls_rollback_bug", SSL_OP_TLS_ROLLBACK_BUG },
77bb000f
PP
2234#endif
2235};
2236static int exim_openssl_options_size =
2237 sizeof(exim_openssl_options)/sizeof(struct exim_openssl_option);
2238
c80c5570 2239
77bb000f
PP
2240static BOOL
2241tls_openssl_one_option_parse(uschar *name, long *value)
2242{
2243int first = 0;
2244int last = exim_openssl_options_size;
2245while (last > first)
2246 {
2247 int middle = (first + last)/2;
2248 int c = Ustrcmp(name, exim_openssl_options[middle].name);
2249 if (c == 0)
2250 {
2251 *value = exim_openssl_options[middle].value;
2252 return TRUE;
2253 }
2254 else if (c > 0)
2255 first = middle + 1;
2256 else
2257 last = middle;
2258 }
2259return FALSE;
2260}
2261
2262
2263
2264
2265/*************************************************
2266* OpenSSL option parsing logic *
2267*************************************************/
2268
2269/* OpenSSL has a number of compatibility options which an administrator might
2270reasonably wish to set. Interpret a list similarly to decode_bits(), so that
2271we look like log_selector.
2272
2273Arguments:
2274 option_spec the administrator-supplied string of options
2275 results ptr to long storage for the options bitmap
2276Returns success or failure
2277*/
2278
2279BOOL
2280tls_openssl_options_parse(uschar *option_spec, long *results)
2281{
2282long result, item;
2283uschar *s, *end;
2284uschar keep_c;
2285BOOL adding, item_parsed;
2286
0e944a0d 2287result = 0L;
b1770b6e 2288/* Prior to 4.80 we or'd in SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; removed
da3ad30d 2289 * from default because it increases BEAST susceptibility. */
f0f5a555
PP
2290#ifdef SSL_OP_NO_SSLv2
2291result |= SSL_OP_NO_SSLv2;
2292#endif
77bb000f
PP
2293
2294if (option_spec == NULL)
2295 {
2296 *results = result;
2297 return TRUE;
2298 }
2299
2300for (s=option_spec; *s != '\0'; /**/)
2301 {
2302 while (isspace(*s)) ++s;
2303 if (*s == '\0')
2304 break;
2305 if (*s != '+' && *s != '-')
2306 {
2307 DEBUG(D_tls) debug_printf("malformed openssl option setting: "
0e944a0d 2308 "+ or - expected but found \"%s\"\n", s);
77bb000f
PP
2309 return FALSE;
2310 }
2311 adding = *s++ == '+';
2312 for (end = s; (*end != '\0') && !isspace(*end); ++end) /**/ ;
2313 keep_c = *end;
2314 *end = '\0';
2315 item_parsed = tls_openssl_one_option_parse(s, &item);
2316 if (!item_parsed)
2317 {
0e944a0d 2318 DEBUG(D_tls) debug_printf("openssl option setting unrecognised: \"%s\"\n", s);
77bb000f
PP
2319 return FALSE;
2320 }
2321 DEBUG(D_tls) debug_printf("openssl option, %s from %lx: %lx (%s)\n",
2322 adding ? "adding" : "removing", result, item, s);
2323 if (adding)
2324 result |= item;
2325 else
2326 result &= ~item;
2327 *end = keep_c;
2328 s = end;
2329 }
2330
2331*results = result;
2332return TRUE;
2333}
2334
9d1c15ef
JH
2335/* vi: aw ai sw=2
2336*/
059ec3d9 2337/* End of tls-openssl.c */