Guard loadable module vars with LOOKUP_MODULE_DIR.
[exim.git] / src / src / tls-openssl.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
059ec3d9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* This module provides the TLS (aka SSL) support for Exim using the OpenSSL
9library. It is #included into the tls.c file when that library is used. The
10code herein is based on a patch that was originally contributed by Steve
11Haslam. It was adapted from stunnel, a GPL program by Michal Trojnara.
12
13No cryptographic code is included in Exim. All this module does is to call
14functions from the OpenSSL library. */
15
16
17/* Heading stuff */
18
19#include <openssl/lhash.h>
20#include <openssl/ssl.h>
21#include <openssl/err.h>
22#include <openssl/rand.h>
23
24/* Structure for collecting random data for seeding. */
25
26typedef struct randstuff {
9e3331ea
TK
27 struct timeval tv;
28 pid_t p;
059ec3d9
PH
29} randstuff;
30
31/* Local static variables */
32
33static BOOL verify_callback_called = FALSE;
34static const uschar *sid_ctx = US"exim";
35
36static SSL_CTX *ctx = NULL;
37static SSL *ssl = NULL;
38
39static char ssl_errstring[256];
40
41static int ssl_session_timeout = 200;
42static BOOL verify_optional = FALSE;
43
44
45
46
47
48/*************************************************
49* Handle TLS error *
50*************************************************/
51
52/* Called from lots of places when errors occur before actually starting to do
53the TLS handshake, that is, while the session is still in clear. Always returns
54DEFER for a server and FAIL for a client so that most calls can use "return
55tls_error(...)" to do this processing and then give an appropriate return. A
56single function is used for both server and client, because it is called from
57some shared functions.
58
59Argument:
60 prefix text to include in the logged error
61 host NULL if setting up a server;
62 the connected host if setting up a client
7199e1ee 63 msg error message or NULL if we should ask OpenSSL
059ec3d9
PH
64
65Returns: OK/DEFER/FAIL
66*/
67
68static int
7199e1ee 69tls_error(uschar *prefix, host_item *host, uschar *msg)
059ec3d9 70{
7199e1ee
TF
71if (msg == NULL)
72 {
73 ERR_error_string(ERR_get_error(), ssl_errstring);
5ca6d115 74 msg = (uschar *)ssl_errstring;
7199e1ee
TF
75 }
76
059ec3d9
PH
77if (host == NULL)
78 {
7199e1ee 79 uschar *conn_info = smtp_get_connection_info();
5ca6d115 80 if (Ustrncmp(conn_info, US"SMTP ", 5) == 0)
7199e1ee
TF
81 conn_info += 5;
82 log_write(0, LOG_MAIN, "TLS error on %s (%s): %s",
83 conn_info, prefix, msg);
059ec3d9
PH
84 return DEFER;
85 }
86else
87 {
88 log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s): %s",
7199e1ee 89 host->name, host->address, prefix, msg);
059ec3d9
PH
90 return FAIL;
91 }
92}
93
94
95
96/*************************************************
97* Callback to generate RSA key *
98*************************************************/
99
100/*
101Arguments:
102 s SSL connection
103 export not used
104 keylength keylength
105
106Returns: pointer to generated key
107*/
108
109static RSA *
110rsa_callback(SSL *s, int export, int keylength)
111{
112RSA *rsa_key;
113export = export; /* Shut picky compilers up */
114DEBUG(D_tls) debug_printf("Generating %d bit RSA key...\n", keylength);
115rsa_key = RSA_generate_key(keylength, RSA_F4, NULL, NULL);
116if (rsa_key == NULL)
117 {
118 ERR_error_string(ERR_get_error(), ssl_errstring);
119 log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (RSA_generate_key): %s",
120 ssl_errstring);
121 return NULL;
122 }
123return rsa_key;
124}
125
126
127
128
129/*************************************************
130* Callback for verification *
131*************************************************/
132
133/* The SSL library does certificate verification if set up to do so. This
134callback has the current yes/no state is in "state". If verification succeeded,
135we set up the tls_peerdn string. If verification failed, what happens depends
136on whether the client is required to present a verifiable certificate or not.
137
138If verification is optional, we change the state to yes, but still log the
139verification error. For some reason (it really would help to have proper
140documentation of OpenSSL), this callback function then gets called again, this
141time with state = 1. In fact, that's useful, because we can set up the peerdn
142value, but we must take care not to set the private verified flag on the second
143time through.
144
145Note: this function is not called if the client fails to present a certificate
146when asked. We get here only if a certificate has been received. Handling of
147optional verification for this case is done when requesting SSL to verify, by
148setting SSL_VERIFY_FAIL_IF_NO_PEER_CERT in the non-optional case.
149
150Arguments:
151 state current yes/no state as 1/0
152 x509ctx certificate information.
153
154Returns: 1 if verified, 0 if not
155*/
156
157static int
158verify_callback(int state, X509_STORE_CTX *x509ctx)
159{
160static uschar txt[256];
161
162X509_NAME_oneline(X509_get_subject_name(x509ctx->current_cert),
163 CS txt, sizeof(txt));
164
165if (state == 0)
166 {
167 log_write(0, LOG_MAIN, "SSL verify error: depth=%d error=%s cert=%s",
168 x509ctx->error_depth,
169 X509_verify_cert_error_string(x509ctx->error),
170 txt);
171 tls_certificate_verified = FALSE;
172 verify_callback_called = TRUE;
173 if (!verify_optional) return 0; /* reject */
174 DEBUG(D_tls) debug_printf("SSL verify failure overridden (host in "
175 "tls_try_verify_hosts)\n");
176 return 1; /* accept */
177 }
178
179if (x509ctx->error_depth != 0)
180 {
181 DEBUG(D_tls) debug_printf("SSL verify ok: depth=%d cert=%s\n",
182 x509ctx->error_depth, txt);
183 }
184else
185 {
186 DEBUG(D_tls) debug_printf("SSL%s peer: %s\n",
187 verify_callback_called? "" : " authenticated", txt);
188 tls_peerdn = txt;
189 }
190
059ec3d9
PH
191if (!verify_callback_called) tls_certificate_verified = TRUE;
192verify_callback_called = TRUE;
193
194return 1; /* accept */
195}
196
197
198
199/*************************************************
200* Information callback *
201*************************************************/
202
203/* The SSL library functions call this from time to time to indicate what they
204are doing. We copy the string to the debugging output when the level is high
205enough.
206
207Arguments:
208 s the SSL connection
209 where
210 ret
211
212Returns: nothing
213*/
214
215static void
216info_callback(SSL *s, int where, int ret)
217{
218where = where;
219ret = ret;
220DEBUG(D_tls) debug_printf("SSL info: %s\n", SSL_state_string_long(s));
221}
222
223
224
225/*************************************************
226* Initialize for DH *
227*************************************************/
228
229/* If dhparam is set, expand it, and load up the parameters for DH encryption.
230
231Arguments:
232 dhparam DH parameter file
7199e1ee 233 host connected host, if client; NULL if server
059ec3d9
PH
234
235Returns: TRUE if OK (nothing to set up, or setup worked)
236*/
237
238static BOOL
7199e1ee 239init_dh(uschar *dhparam, host_item *host)
059ec3d9
PH
240{
241BOOL yield = TRUE;
242BIO *bio;
243DH *dh;
244uschar *dhexpanded;
245
246if (!expand_check(dhparam, US"tls_dhparam", &dhexpanded))
247 return FALSE;
248
249if (dhexpanded == NULL) return TRUE;
250
251if ((bio = BIO_new_file(CS dhexpanded, "r")) == NULL)
252 {
7199e1ee 253 tls_error(string_sprintf("could not read dhparams file %s", dhexpanded),
5ca6d115 254 host, (uschar *)strerror(errno));
059ec3d9
PH
255 yield = FALSE;
256 }
257else
258 {
259 if ((dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL)) == NULL)
260 {
7199e1ee
TF
261 tls_error(string_sprintf("could not read dhparams file %s", dhexpanded),
262 host, NULL);
059ec3d9
PH
263 yield = FALSE;
264 }
265 else
266 {
267 SSL_CTX_set_tmp_dh(ctx, dh);
268 DEBUG(D_tls)
269 debug_printf("Diffie-Hellman initialized from %s with %d-bit key\n",
270 dhexpanded, 8*DH_size(dh));
271 DH_free(dh);
272 }
273 BIO_free(bio);
274 }
275
276return yield;
277}
278
279
280
281
282/*************************************************
283* Initialize for TLS *
284*************************************************/
285
286/* Called from both server and client code, to do preliminary initialization of
287the library.
288
289Arguments:
290 host connected host, if client; NULL if server
291 dhparam DH parameter file
292 certificate certificate file
293 privatekey private key
294 addr address if client; NULL if server (for some randomness)
295
296Returns: OK/DEFER/FAIL
297*/
298
299static int
c91535f3
PH
300tls_init(host_item *host, uschar *dhparam, uschar *certificate,
301 uschar *privatekey, address_item *addr)
059ec3d9 302{
77bb000f
PP
303long init_options;
304BOOL okay;
305
059ec3d9
PH
306SSL_load_error_strings(); /* basic set up */
307OpenSSL_add_ssl_algorithms();
308
388d6564 309#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
77bb000f 310/* SHA256 is becoming ever more popular. This makes sure it gets added to the
a0475b69
TK
311list of available digests. */
312EVP_add_digest(EVP_sha256());
cf1ef1a9 313#endif
a0475b69 314
059ec3d9
PH
315/* Create a context */
316
317ctx = SSL_CTX_new((host == NULL)?
318 SSLv23_server_method() : SSLv23_client_method());
319
7199e1ee 320if (ctx == NULL) return tls_error(US"SSL_CTX_new", host, NULL);
059ec3d9
PH
321
322/* It turns out that we need to seed the random number generator this early in
323order to get the full complement of ciphers to work. It took me roughly a day
324of work to discover this by experiment.
325
326On systems that have /dev/urandom, SSL may automatically seed itself from
327there. Otherwise, we have to make something up as best we can. Double check
328afterwards. */
329
330if (!RAND_status())
331 {
332 randstuff r;
9e3331ea 333 gettimeofday(&r.tv, NULL);
059ec3d9
PH
334 r.p = getpid();
335
336 RAND_seed((uschar *)(&r), sizeof(r));
337 RAND_seed((uschar *)big_buffer, big_buffer_size);
338 if (addr != NULL) RAND_seed((uschar *)addr, sizeof(addr));
339
340 if (!RAND_status())
7199e1ee 341 return tls_error(US"RAND_status", host,
5ca6d115 342 US"unable to seed random number generator");
059ec3d9
PH
343 }
344
345/* Set up the information callback, which outputs if debugging is at a suitable
346level. */
347
58c01c94 348SSL_CTX_set_info_callback(ctx, (void (*)())info_callback);
059ec3d9 349
c80c5570
PP
350/* Automatically re-try reads/writes after renegotiation. */
351(void) SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
352
77bb000f
PP
353/* Apply administrator-supplied work-arounds.
354Historically we applied just one requested option,
355SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS, but when bug 994 requested a second, we
356moved to an administrator-controlled list of options to specify and
357grandfathered in the first one as the default value for "openssl_options".
059ec3d9 358
77bb000f
PP
359No OpenSSL version number checks: the options we accept depend upon the
360availability of the option value macros from OpenSSL. */
059ec3d9 361
77bb000f
PP
362okay = tls_openssl_options_parse(openssl_options, &init_options);
363if (!okay)
73a46702 364 return tls_error(US"openssl_options parsing failed", host, NULL);
77bb000f
PP
365
366if (init_options)
367 {
368 DEBUG(D_tls) debug_printf("setting SSL CTX options: %#lx\n", init_options);
369 if (!(SSL_CTX_set_options(ctx, init_options)))
370 return tls_error(string_sprintf(
371 "SSL_CTX_set_option(%#lx)", init_options), host, NULL);
372 }
373else
374 DEBUG(D_tls) debug_printf("no SSL CTX options to set\n");
059ec3d9
PH
375
376/* Initialize with DH parameters if supplied */
377
7199e1ee 378if (!init_dh(dhparam, host)) return DEFER;
059ec3d9
PH
379
380/* Set up certificate and key */
381
382if (certificate != NULL)
383 {
384 uschar *expanded;
385 if (!expand_check(certificate, US"tls_certificate", &expanded))
386 return DEFER;
387
388 if (expanded != NULL)
389 {
390 DEBUG(D_tls) debug_printf("tls_certificate file %s\n", expanded);
391 if (!SSL_CTX_use_certificate_chain_file(ctx, CS expanded))
d6453af2 392 return tls_error(string_sprintf(
7199e1ee 393 "SSL_CTX_use_certificate_chain_file file=%s", expanded), host, NULL);
059ec3d9
PH
394 }
395
396 if (privatekey != NULL &&
397 !expand_check(privatekey, US"tls_privatekey", &expanded))
398 return DEFER;
399
c91535f3
PH
400 /* If expansion was forced to fail, key_expanded will be NULL. If the result
401 of the expansion is an empty string, ignore it also, and assume the private
402 key is in the same file as the certificate. */
403
404 if (expanded != NULL && *expanded != 0)
059ec3d9
PH
405 {
406 DEBUG(D_tls) debug_printf("tls_privatekey file %s\n", expanded);
407 if (!SSL_CTX_use_PrivateKey_file(ctx, CS expanded, SSL_FILETYPE_PEM))
d6453af2 408 return tls_error(string_sprintf(
7199e1ee 409 "SSL_CTX_use_PrivateKey_file file=%s", expanded), host, NULL);
059ec3d9
PH
410 }
411 }
412
413/* Set up the RSA callback */
414
415SSL_CTX_set_tmp_rsa_callback(ctx, rsa_callback);
416
417/* Finally, set the timeout, and we are done */
418
419SSL_CTX_set_timeout(ctx, ssl_session_timeout);
420DEBUG(D_tls) debug_printf("Initialized TLS\n");
421return OK;
422}
423
424
425
426
427/*************************************************
428* Get name of cipher in use *
429*************************************************/
430
431/* The answer is left in a static buffer, and tls_cipher is set to point
432to it.
433
434Argument: pointer to an SSL structure for the connection
435Returns: nothing
436*/
437
438static void
439construct_cipher_name(SSL *ssl)
440{
441static uschar cipherbuf[256];
57b3a7f5
PP
442/* With OpenSSL 1.0.0a, this needs to be const but the documentation doesn't
443yet reflect that. It should be a safe change anyway, even 0.9.8 versions have
444the accessor functions use const in the prototype. */
445const SSL_CIPHER *c;
059ec3d9 446uschar *ver;
059ec3d9
PH
447
448switch (ssl->session->ssl_version)
449 {
450 case SSL2_VERSION:
451 ver = US"SSLv2";
452 break;
453
454 case SSL3_VERSION:
455 ver = US"SSLv3";
456 break;
457
458 case TLS1_VERSION:
459 ver = US"TLSv1";
460 break;
461
c80c5570
PP
462#ifdef TLS1_1_VERSION
463 case TLS1_1_VERSION:
464 ver = US"TLSv1.1";
465 break;
466#endif
467
468#ifdef TLS1_2_VERSION
469 case TLS1_2_VERSION:
470 ver = US"TLSv1.2";
471 break;
472#endif
473
059ec3d9
PH
474 default:
475 ver = US"UNKNOWN";
476 }
477
57b3a7f5 478c = (const SSL_CIPHER *) SSL_get_current_cipher(ssl);
edc33b5f 479SSL_CIPHER_get_bits(c, &tls_bits);
059ec3d9
PH
480
481string_format(cipherbuf, sizeof(cipherbuf), "%s:%s:%u", ver,
edc33b5f 482 SSL_CIPHER_get_name(c), tls_bits);
059ec3d9
PH
483tls_cipher = cipherbuf;
484
485DEBUG(D_tls) debug_printf("Cipher: %s\n", cipherbuf);
486}
487
488
489
490
491
492/*************************************************
493* Set up for verifying certificates *
494*************************************************/
495
496/* Called by both client and server startup
497
498Arguments:
499 certs certs file or NULL
500 crl CRL file or NULL
501 host NULL in a server; the remote host in a client
502 optional TRUE if called from a server for a host in tls_try_verify_hosts;
503 otherwise passed as FALSE
504
505Returns: OK/DEFER/FAIL
506*/
507
508static int
509setup_certs(uschar *certs, uschar *crl, host_item *host, BOOL optional)
510{
511uschar *expcerts, *expcrl;
512
513if (!expand_check(certs, US"tls_verify_certificates", &expcerts))
514 return DEFER;
515
516if (expcerts != NULL)
517 {
518 struct stat statbuf;
519 if (!SSL_CTX_set_default_verify_paths(ctx))
7199e1ee 520 return tls_error(US"SSL_CTX_set_default_verify_paths", host, NULL);
059ec3d9
PH
521
522 if (Ustat(expcerts, &statbuf) < 0)
523 {
524 log_write(0, LOG_MAIN|LOG_PANIC,
525 "failed to stat %s for certificates", expcerts);
526 return DEFER;
527 }
528 else
529 {
530 uschar *file, *dir;
531 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
532 { file = NULL; dir = expcerts; }
533 else
534 { file = expcerts; dir = NULL; }
535
536 /* If a certificate file is empty, the next function fails with an
537 unhelpful error message. If we skip it, we get the correct behaviour (no
538 certificates are recognized, but the error message is still misleading (it
539 says no certificate was supplied.) But this is better. */
540
541 if ((file == NULL || statbuf.st_size > 0) &&
542 !SSL_CTX_load_verify_locations(ctx, CS file, CS dir))
7199e1ee 543 return tls_error(US"SSL_CTX_load_verify_locations", host, NULL);
059ec3d9
PH
544
545 if (file != NULL)
546 {
547 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CS file));
548 }
549 }
550
551 /* Handle a certificate revocation list. */
552
553 #if OPENSSL_VERSION_NUMBER > 0x00907000L
554
8b417f2c
PH
555 /* This bit of code is now the version supplied by Lars Mainka. (I have
556 * merely reformatted it into the Exim code style.)
557
558 * "From here I changed the code to add support for multiple crl's
559 * in pem format in one file or to support hashed directory entries in
560 * pem format instead of a file. This method now uses the library function
561 * X509_STORE_load_locations to add the CRL location to the SSL context.
562 * OpenSSL will then handle the verify against CA certs and CRLs by
563 * itself in the verify callback." */
564
059ec3d9
PH
565 if (!expand_check(crl, US"tls_crl", &expcrl)) return DEFER;
566 if (expcrl != NULL && *expcrl != 0)
567 {
8b417f2c
PH
568 struct stat statbufcrl;
569 if (Ustat(expcrl, &statbufcrl) < 0)
570 {
571 log_write(0, LOG_MAIN|LOG_PANIC,
572 "failed to stat %s for certificates revocation lists", expcrl);
573 return DEFER;
574 }
575 else
059ec3d9 576 {
8b417f2c
PH
577 /* is it a file or directory? */
578 uschar *file, *dir;
579 X509_STORE *cvstore = SSL_CTX_get_cert_store(ctx);
580 if ((statbufcrl.st_mode & S_IFMT) == S_IFDIR)
059ec3d9 581 {
8b417f2c
PH
582 file = NULL;
583 dir = expcrl;
584 DEBUG(D_tls) debug_printf("SSL CRL value is a directory %s\n", dir);
059ec3d9
PH
585 }
586 else
587 {
8b417f2c
PH
588 file = expcrl;
589 dir = NULL;
590 DEBUG(D_tls) debug_printf("SSL CRL value is a file %s\n", file);
059ec3d9 591 }
8b417f2c 592 if (X509_STORE_load_locations(cvstore, CS file, CS dir) == 0)
7199e1ee 593 return tls_error(US"X509_STORE_load_locations", host, NULL);
8b417f2c
PH
594
595 /* setting the flags to check against the complete crl chain */
596
597 X509_STORE_set_flags(cvstore,
598 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
059ec3d9 599 }
059ec3d9
PH
600 }
601
602 #endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
603
604 /* If verification is optional, don't fail if no certificate */
605
606 SSL_CTX_set_verify(ctx,
607 SSL_VERIFY_PEER | (optional? 0 : SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
608 verify_callback);
609 }
610
611return OK;
612}
613
614
615
616/*************************************************
617* Start a TLS session in a server *
618*************************************************/
619
620/* This is called when Exim is running as a server, after having received
621the STARTTLS command. It must respond to that command, and then negotiate
622a TLS session.
623
624Arguments:
625 require_ciphers allowed ciphers
83da1223
PH
626 ------------------------------------------------------
627 require_mac list of allowed MACs ) Not used
628 require_kx list of allowed key_exchange methods ) for
629 require_proto list of allowed protocols ) OpenSSL
630 ------------------------------------------------------
059ec3d9
PH
631
632Returns: OK on success
633 DEFER for errors before the start of the negotiation
634 FAIL for errors during the negotation; the server can't
635 continue running.
636*/
637
638int
83da1223
PH
639tls_server_start(uschar *require_ciphers, uschar *require_mac,
640 uschar *require_kx, uschar *require_proto)
059ec3d9
PH
641{
642int rc;
643uschar *expciphers;
644
645/* Check for previous activation */
646
647if (tls_active >= 0)
648 {
5ca6d115 649 tls_error(US"STARTTLS received after TLS started", NULL, US"");
059ec3d9
PH
650 smtp_printf("554 Already in TLS\r\n");
651 return FAIL;
652 }
653
654/* Initialize the SSL library. If it fails, it will already have logged
655the error. */
656
657rc = tls_init(NULL, tls_dhparam, tls_certificate, tls_privatekey, NULL);
658if (rc != OK) return rc;
659
660if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers))
661 return FAIL;
662
663/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
664are separated by underscores. So that I can use either form in my tests, and
665also for general convenience, we turn underscores into hyphens here. */
666
667if (expciphers != NULL)
668 {
669 uschar *s = expciphers;
670 while (*s != 0) { if (*s == '_') *s = '-'; s++; }
671 DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
672 if (!SSL_CTX_set_cipher_list(ctx, CS expciphers))
7199e1ee 673 return tls_error(US"SSL_CTX_set_cipher_list", NULL, NULL);
059ec3d9
PH
674 }
675
676/* If this is a host for which certificate verification is mandatory or
677optional, set up appropriately. */
678
679tls_certificate_verified = FALSE;
680verify_callback_called = FALSE;
681
682if (verify_check_host(&tls_verify_hosts) == OK)
683 {
684 rc = setup_certs(tls_verify_certificates, tls_crl, NULL, FALSE);
685 if (rc != OK) return rc;
686 verify_optional = FALSE;
687 }
688else if (verify_check_host(&tls_try_verify_hosts) == OK)
689 {
690 rc = setup_certs(tls_verify_certificates, tls_crl, NULL, TRUE);
691 if (rc != OK) return rc;
692 verify_optional = TRUE;
693 }
694
695/* Prepare for new connection */
696
7199e1ee 697if ((ssl = SSL_new(ctx)) == NULL) return tls_error(US"SSL_new", NULL, NULL);
da3ad30d
PP
698
699/* Warning: we used to SSL_clear(ssl) here, it was removed.
700 *
701 * With the SSL_clear(), we get strange interoperability bugs with
702 * OpenSSL 1.0.1b and TLS1.1/1.2. It looks as though this may be a bug in
703 * OpenSSL itself, as a clear should not lead to inability to follow protocols.
704 *
705 * The SSL_clear() call is to let an existing SSL* be reused, typically after
706 * session shutdown. In this case, we have a brand new object and there's no
707 * obvious reason to immediately clear it. I'm guessing that this was
708 * originally added because of incomplete initialisation which the clear fixed,
709 * in some historic release.
710 */
059ec3d9
PH
711
712/* Set context and tell client to go ahead, except in the case of TLS startup
713on connection, where outputting anything now upsets the clients and tends to
714make them disconnect. We need to have an explicit fflush() here, to force out
715the response. Other smtp_printf() calls do not need it, because in non-TLS
716mode, the fflush() happens when smtp_getc() is called. */
717
718SSL_set_session_id_context(ssl, sid_ctx, Ustrlen(sid_ctx));
719if (!tls_on_connect)
720 {
721 smtp_printf("220 TLS go ahead\r\n");
722 fflush(smtp_out);
723 }
724
725/* Now negotiate the TLS session. We put our own timer on it, since it seems
726that the OpenSSL library doesn't. */
727
56f5d9bd
PH
728SSL_set_wfd(ssl, fileno(smtp_out));
729SSL_set_rfd(ssl, fileno(smtp_in));
059ec3d9
PH
730SSL_set_accept_state(ssl);
731
732DEBUG(D_tls) debug_printf("Calling SSL_accept\n");
733
734sigalrm_seen = FALSE;
735if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
736rc = SSL_accept(ssl);
737alarm(0);
738
739if (rc <= 0)
740 {
7199e1ee 741 tls_error(US"SSL_accept", NULL, sigalrm_seen ? US"timed out" : NULL);
77bb000f
PP
742 if (ERR_get_error() == 0)
743 log_write(0, LOG_MAIN,
a053d125 744 "TLS client disconnected cleanly (rejected our certificate?)");
059ec3d9
PH
745 return FAIL;
746 }
747
748DEBUG(D_tls) debug_printf("SSL_accept was successful\n");
749
750/* TLS has been set up. Adjust the input functions to read via TLS,
751and initialize things. */
752
753construct_cipher_name(ssl);
754
755DEBUG(D_tls)
756 {
757 uschar buf[2048];
758 if (SSL_get_shared_ciphers(ssl, CS buf, sizeof(buf)) != NULL)
759 debug_printf("Shared ciphers: %s\n", buf);
760 }
761
762
763ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size);
764ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0;
765ssl_xfer_eof = ssl_xfer_error = 0;
766
767receive_getc = tls_getc;
768receive_ungetc = tls_ungetc;
769receive_feof = tls_feof;
770receive_ferror = tls_ferror;
58eb016e 771receive_smtp_buffered = tls_smtp_buffered;
059ec3d9
PH
772
773tls_active = fileno(smtp_out);
774return OK;
775}
776
777
778
779
780
781/*************************************************
782* Start a TLS session in a client *
783*************************************************/
784
785/* Called from the smtp transport after STARTTLS has been accepted.
786
787Argument:
788 fd the fd of the connection
789 host connected host (for messages)
83da1223 790 addr the first address
059ec3d9
PH
791 dhparam DH parameter file
792 certificate certificate file
793 privatekey private key file
794 verify_certs file for certificate verify
795 crl file containing CRL
796 require_ciphers list of allowed ciphers
83da1223
PH
797 ------------------------------------------------------
798 require_mac list of allowed MACs ) Not used
799 require_kx list of allowed key_exchange methods ) for
800 require_proto list of allowed protocols ) OpenSSL
801 ------------------------------------------------------
802 timeout startup timeout
059ec3d9
PH
803
804Returns: OK on success
805 FAIL otherwise - note that tls_error() will not give DEFER
806 because this is not a server
807*/
808
809int
810tls_client_start(int fd, host_item *host, address_item *addr, uschar *dhparam,
811 uschar *certificate, uschar *privatekey, uschar *verify_certs, uschar *crl,
83da1223
PH
812 uschar *require_ciphers, uschar *require_mac, uschar *require_kx,
813 uschar *require_proto, int timeout)
059ec3d9
PH
814{
815static uschar txt[256];
816uschar *expciphers;
817X509* server_cert;
818int rc;
819
820rc = tls_init(host, dhparam, certificate, privatekey, addr);
821if (rc != OK) return rc;
822
823tls_certificate_verified = FALSE;
824verify_callback_called = FALSE;
825
826if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers))
827 return FAIL;
828
829/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
830are separated by underscores. So that I can use either form in my tests, and
831also for general convenience, we turn underscores into hyphens here. */
832
833if (expciphers != NULL)
834 {
835 uschar *s = expciphers;
836 while (*s != 0) { if (*s == '_') *s = '-'; s++; }
837 DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
838 if (!SSL_CTX_set_cipher_list(ctx, CS expciphers))
7199e1ee 839 return tls_error(US"SSL_CTX_set_cipher_list", host, NULL);
059ec3d9
PH
840 }
841
842rc = setup_certs(verify_certs, crl, host, FALSE);
843if (rc != OK) return rc;
844
7199e1ee 845if ((ssl = SSL_new(ctx)) == NULL) return tls_error(US"SSL_new", host, NULL);
059ec3d9
PH
846SSL_set_session_id_context(ssl, sid_ctx, Ustrlen(sid_ctx));
847SSL_set_fd(ssl, fd);
848SSL_set_connect_state(ssl);
849
850/* There doesn't seem to be a built-in timeout on connection. */
851
852DEBUG(D_tls) debug_printf("Calling SSL_connect\n");
853sigalrm_seen = FALSE;
854alarm(timeout);
855rc = SSL_connect(ssl);
856alarm(0);
857
858if (rc <= 0)
7199e1ee 859 return tls_error(US"SSL_connect", host, sigalrm_seen ? US"timed out" : NULL);
059ec3d9
PH
860
861DEBUG(D_tls) debug_printf("SSL_connect succeeded\n");
862
453a6645 863/* Beware anonymous ciphers which lead to server_cert being NULL */
059ec3d9 864server_cert = SSL_get_peer_certificate (ssl);
453a6645
PP
865if (server_cert)
866 {
867 tls_peerdn = US X509_NAME_oneline(X509_get_subject_name(server_cert),
868 CS txt, sizeof(txt));
869 tls_peerdn = txt;
870 }
871else
872 tls_peerdn = NULL;
059ec3d9
PH
873
874construct_cipher_name(ssl); /* Sets tls_cipher */
875
876tls_active = fd;
877return OK;
878}
879
880
881
882
883
884/*************************************************
885* TLS version of getc *
886*************************************************/
887
888/* This gets the next byte from the TLS input buffer. If the buffer is empty,
889it refills the buffer via the SSL reading function.
890
891Arguments: none
892Returns: the next character or EOF
893*/
894
895int
896tls_getc(void)
897{
898if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
899 {
900 int error;
901 int inbytes;
902
c80c5570
PP
903 DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
904 ssl_xfer_buffer, ssl_xfer_buffer_size);
059ec3d9
PH
905
906 if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
907 inbytes = SSL_read(ssl, CS ssl_xfer_buffer, ssl_xfer_buffer_size);
908 error = SSL_get_error(ssl, inbytes);
909 alarm(0);
910
911 /* SSL_ERROR_ZERO_RETURN appears to mean that the SSL session has been
912 closed down, not that the socket itself has been closed down. Revert to
913 non-SSL handling. */
914
915 if (error == SSL_ERROR_ZERO_RETURN)
916 {
917 DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
918
919 receive_getc = smtp_getc;
920 receive_ungetc = smtp_ungetc;
921 receive_feof = smtp_feof;
922 receive_ferror = smtp_ferror;
58eb016e 923 receive_smtp_buffered = smtp_buffered;
059ec3d9
PH
924
925 SSL_free(ssl);
926 ssl = NULL;
927 tls_active = -1;
928 tls_cipher = NULL;
929 tls_peerdn = NULL;
930
931 return smtp_getc();
932 }
933
934 /* Handle genuine errors */
935
ba084640
PP
936 else if (error == SSL_ERROR_SSL)
937 {
938 ERR_error_string(ERR_get_error(), ssl_errstring);
89dd51cd 939 log_write(0, LOG_MAIN, "TLS error (SSL_read): %s", ssl_errstring);
ba084640
PP
940 ssl_xfer_error = 1;
941 return EOF;
942 }
943
059ec3d9
PH
944 else if (error != SSL_ERROR_NONE)
945 {
946 DEBUG(D_tls) debug_printf("Got SSL error %d\n", error);
947 ssl_xfer_error = 1;
948 return EOF;
949 }
c80c5570 950
80a47a2c
TK
951#ifndef DISABLE_DKIM
952 dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
953#endif
059ec3d9
PH
954 ssl_xfer_buffer_hwm = inbytes;
955 ssl_xfer_buffer_lwm = 0;
956 }
957
958/* Something in the buffer; return next uschar */
959
960return ssl_xfer_buffer[ssl_xfer_buffer_lwm++];
961}
962
963
964
965/*************************************************
966* Read bytes from TLS channel *
967*************************************************/
968
969/*
970Arguments:
971 buff buffer of data
972 len size of buffer
973
974Returns: the number of bytes read
975 -1 after a failed read
976*/
977
978int
979tls_read(uschar *buff, size_t len)
980{
981int inbytes;
982int error;
983
c80c5570
PP
984DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
985 buff, (unsigned int)len);
059ec3d9
PH
986
987inbytes = SSL_read(ssl, CS buff, len);
988error = SSL_get_error(ssl, inbytes);
989
990if (error == SSL_ERROR_ZERO_RETURN)
991 {
992 DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
993 return -1;
994 }
995else if (error != SSL_ERROR_NONE)
996 {
997 return -1;
998 }
999
1000return inbytes;
1001}
1002
1003
1004
1005
1006
1007/*************************************************
1008* Write bytes down TLS channel *
1009*************************************************/
1010
1011/*
1012Arguments:
1013 buff buffer of data
1014 len number of bytes
1015
1016Returns: the number of bytes after a successful write,
1017 -1 after a failed write
1018*/
1019
1020int
1021tls_write(const uschar *buff, size_t len)
1022{
1023int outbytes;
1024int error;
1025int left = len;
1026
c80c5570 1027DEBUG(D_tls) debug_printf("tls_do_write(%p, %d)\n", buff, left);
059ec3d9
PH
1028while (left > 0)
1029 {
c80c5570 1030 DEBUG(D_tls) debug_printf("SSL_write(SSL, %p, %d)\n", buff, left);
059ec3d9
PH
1031 outbytes = SSL_write(ssl, CS buff, left);
1032 error = SSL_get_error(ssl, outbytes);
1033 DEBUG(D_tls) debug_printf("outbytes=%d error=%d\n", outbytes, error);
1034 switch (error)
1035 {
1036 case SSL_ERROR_SSL:
1037 ERR_error_string(ERR_get_error(), ssl_errstring);
1038 log_write(0, LOG_MAIN, "TLS error (SSL_write): %s", ssl_errstring);
1039 return -1;
1040
1041 case SSL_ERROR_NONE:
1042 left -= outbytes;
1043 buff += outbytes;
1044 break;
1045
1046 case SSL_ERROR_ZERO_RETURN:
1047 log_write(0, LOG_MAIN, "SSL channel closed on write");
1048 return -1;
1049
1050 default:
1051 log_write(0, LOG_MAIN, "SSL_write error %d", error);
1052 return -1;
1053 }
1054 }
1055return len;
1056}
1057
1058
1059
1060/*************************************************
1061* Close down a TLS session *
1062*************************************************/
1063
1064/* This is also called from within a delivery subprocess forked from the
1065daemon, to shut down the TLS library, without actually doing a shutdown (which
1066would tamper with the SSL session in the parent process).
1067
1068Arguments: TRUE if SSL_shutdown is to be called
1069Returns: nothing
1070*/
1071
1072void
1073tls_close(BOOL shutdown)
1074{
1075if (tls_active < 0) return; /* TLS was not active */
1076
1077if (shutdown)
1078 {
1079 DEBUG(D_tls) debug_printf("tls_close(): shutting down SSL\n");
1080 SSL_shutdown(ssl);
1081 }
1082
1083SSL_free(ssl);
1084ssl = NULL;
1085
1086tls_active = -1;
1087}
1088
36f12725
NM
1089
1090
1091
1092/*************************************************
1093* Report the library versions. *
1094*************************************************/
1095
1096/* There have historically been some issues with binary compatibility in
1097OpenSSL libraries; if Exim (like many other applications) is built against
1098one version of OpenSSL but the run-time linker picks up another version,
1099it can result in serious failures, including crashing with a SIGSEGV. So
1100report the version found by the compiler and the run-time version.
1101
1102Arguments: a FILE* to print the results to
1103Returns: nothing
1104*/
1105
1106void
1107tls_version_report(FILE *f)
1108{
754a0503
PP
1109fprintf(f, "Library version: OpenSSL: Compile: %s\n"
1110 " Runtime: %s\n",
1111 OPENSSL_VERSION_TEXT,
1112 SSLeay_version(SSLEAY_VERSION));
36f12725
NM
1113}
1114
9e3331ea
TK
1115
1116
1117
1118/*************************************************
1119* Pseudo-random number generation *
1120*************************************************/
1121
1122/* Pseudo-random number generation. The result is not expected to be
1123cryptographically strong but not so weak that someone will shoot themselves
1124in the foot using it as a nonce in input in some email header scheme or
1125whatever weirdness they'll twist this into. The result should handle fork()
1126and avoid repeating sequences. OpenSSL handles that for us.
1127
1128Arguments:
1129 max range maximum
1130Returns a random number in range [0, max-1]
1131*/
1132
1133int
1134pseudo_random_number(int max)
1135{
1136unsigned int r;
1137int i, needed_len;
1138uschar *p;
1139uschar smallbuf[sizeof(r)];
1140
1141if (max <= 1)
1142 return 0;
1143
1144/* OpenSSL auto-seeds from /dev/random, etc, but this a double-check. */
1145if (!RAND_status())
1146 {
1147 randstuff r;
1148 gettimeofday(&r.tv, NULL);
1149 r.p = getpid();
1150
1151 RAND_seed((uschar *)(&r), sizeof(r));
1152 }
1153/* We're after pseudo-random, not random; if we still don't have enough data
1154in the internal PRNG then our options are limited. We could sleep and hope
1155for entropy to come along (prayer technique) but if the system is so depleted
1156in the first place then something is likely to just keep taking it. Instead,
1157we'll just take whatever little bit of pseudo-random we can still manage to
1158get. */
1159
1160needed_len = sizeof(r);
1161/* Don't take 8 times more entropy than needed if int is 8 octets and we were
1162asked for a number less than 10. */
1163for (r = max, i = 0; r; ++i)
1164 r >>= 1;
1165i = (i + 7) / 8;
1166if (i < needed_len)
1167 needed_len = i;
1168
1169/* We do not care if crypto-strong */
1170(void) RAND_pseudo_bytes(smallbuf, needed_len);
1171r = 0;
1172for (p = smallbuf; needed_len; --needed_len, ++p)
1173 {
1174 r *= 256;
1175 r += *p;
1176 }
1177
1178/* We don't particularly care about weighted results; if someone wants
1179smooth distribution and cares enough then they should submit a patch then. */
1180return r % max;
1181}
1182
77bb000f
PP
1183
1184
1185
1186/*************************************************
1187* OpenSSL option parse *
1188*************************************************/
1189
1190/* Parse one option for tls_openssl_options_parse below
1191
1192Arguments:
1193 name one option name
1194 value place to store a value for it
1195Returns success or failure in parsing
1196*/
1197
1198struct exim_openssl_option {
1199 uschar *name;
1200 long value;
1201};
1202/* We could use a macro to expand, but we need the ifdef and not all the
1203options document which version they were introduced in. Policylet: include
1204all options unless explicitly for DTLS, let the administrator choose which
1205to apply.
1206
1207This list is current as of:
c80c5570 1208 ==> 1.0.1b <== */
77bb000f
PP
1209static struct exim_openssl_option exim_openssl_options[] = {
1210/* KEEP SORTED ALPHABETICALLY! */
1211#ifdef SSL_OP_ALL
73a46702 1212 { US"all", SSL_OP_ALL },
77bb000f
PP
1213#endif
1214#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
73a46702 1215 { US"allow_unsafe_legacy_renegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION },
77bb000f
PP
1216#endif
1217#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
73a46702 1218 { US"cipher_server_preference", SSL_OP_CIPHER_SERVER_PREFERENCE },
77bb000f
PP
1219#endif
1220#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
73a46702 1221 { US"dont_insert_empty_fragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS },
77bb000f
PP
1222#endif
1223#ifdef SSL_OP_EPHEMERAL_RSA
73a46702 1224 { US"ephemeral_rsa", SSL_OP_EPHEMERAL_RSA },
77bb000f
PP
1225#endif
1226#ifdef SSL_OP_LEGACY_SERVER_CONNECT
73a46702 1227 { US"legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT },
77bb000f
PP
1228#endif
1229#ifdef SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
73a46702 1230 { US"microsoft_big_sslv3_buffer", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER },
77bb000f
PP
1231#endif
1232#ifdef SSL_OP_MICROSOFT_SESS_ID_BUG
73a46702 1233 { US"microsoft_sess_id_bug", SSL_OP_MICROSOFT_SESS_ID_BUG },
77bb000f
PP
1234#endif
1235#ifdef SSL_OP_MSIE_SSLV2_RSA_PADDING
73a46702 1236 { US"msie_sslv2_rsa_padding", SSL_OP_MSIE_SSLV2_RSA_PADDING },
77bb000f
PP
1237#endif
1238#ifdef SSL_OP_NETSCAPE_CHALLENGE_BUG
73a46702 1239 { US"netscape_challenge_bug", SSL_OP_NETSCAPE_CHALLENGE_BUG },
77bb000f
PP
1240#endif
1241#ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
73a46702 1242 { US"netscape_reuse_cipher_change_bug", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG },
77bb000f 1243#endif
c80c5570
PP
1244#ifdef SSL_OP_NO_COMPRESSION
1245 { US"no_compression", SSL_OP_NO_COMPRESSION },
1246#endif
77bb000f 1247#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
73a46702 1248 { US"no_session_resumption_on_renegotiation", SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION },
77bb000f 1249#endif
c0c7b2da
PP
1250#ifdef SSL_OP_NO_SSLv2
1251 { US"no_sslv2", SSL_OP_NO_SSLv2 },
1252#endif
1253#ifdef SSL_OP_NO_SSLv3
1254 { US"no_sslv3", SSL_OP_NO_SSLv3 },
1255#endif
1256#ifdef SSL_OP_NO_TICKET
1257 { US"no_ticket", SSL_OP_NO_TICKET },
1258#endif
1259#ifdef SSL_OP_NO_TLSv1
1260 { US"no_tlsv1", SSL_OP_NO_TLSv1 },
1261#endif
c80c5570
PP
1262#ifdef SSL_OP_NO_TLSv1_1
1263#if SSL_OP_NO_TLSv1_1 == 0x00000400L
1264 /* Error in chosen value in 1.0.1a; see first item in CHANGES for 1.0.1b */
1265#warning OpenSSL 1.0.1a uses a bad value for SSL_OP_NO_TLSv1_1, ignoring
1266#else
1267 { US"no_tlsv1_1", SSL_OP_NO_TLSv1_1 },
1268#endif
1269#endif
1270#ifdef SSL_OP_NO_TLSv1_2
1271 { US"no_tlsv1_2", SSL_OP_NO_TLSv1_2 },
1272#endif
77bb000f 1273#ifdef SSL_OP_SINGLE_DH_USE
73a46702 1274 { US"single_dh_use", SSL_OP_SINGLE_DH_USE },
77bb000f
PP
1275#endif
1276#ifdef SSL_OP_SINGLE_ECDH_USE
73a46702 1277 { US"single_ecdh_use", SSL_OP_SINGLE_ECDH_USE },
77bb000f
PP
1278#endif
1279#ifdef SSL_OP_SSLEAY_080_CLIENT_DH_BUG
73a46702 1280 { US"ssleay_080_client_dh_bug", SSL_OP_SSLEAY_080_CLIENT_DH_BUG },
77bb000f
PP
1281#endif
1282#ifdef SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
73a46702 1283 { US"sslref2_reuse_cert_type_bug", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG },
77bb000f
PP
1284#endif
1285#ifdef SSL_OP_TLS_BLOCK_PADDING_BUG
73a46702 1286 { US"tls_block_padding_bug", SSL_OP_TLS_BLOCK_PADDING_BUG },
77bb000f
PP
1287#endif
1288#ifdef SSL_OP_TLS_D5_BUG
73a46702 1289 { US"tls_d5_bug", SSL_OP_TLS_D5_BUG },
77bb000f
PP
1290#endif
1291#ifdef SSL_OP_TLS_ROLLBACK_BUG
73a46702 1292 { US"tls_rollback_bug", SSL_OP_TLS_ROLLBACK_BUG },
77bb000f
PP
1293#endif
1294};
1295static int exim_openssl_options_size =
1296 sizeof(exim_openssl_options)/sizeof(struct exim_openssl_option);
1297
c80c5570 1298
77bb000f
PP
1299static BOOL
1300tls_openssl_one_option_parse(uschar *name, long *value)
1301{
1302int first = 0;
1303int last = exim_openssl_options_size;
1304while (last > first)
1305 {
1306 int middle = (first + last)/2;
1307 int c = Ustrcmp(name, exim_openssl_options[middle].name);
1308 if (c == 0)
1309 {
1310 *value = exim_openssl_options[middle].value;
1311 return TRUE;
1312 }
1313 else if (c > 0)
1314 first = middle + 1;
1315 else
1316 last = middle;
1317 }
1318return FALSE;
1319}
1320
1321
1322
1323
1324/*************************************************
1325* OpenSSL option parsing logic *
1326*************************************************/
1327
1328/* OpenSSL has a number of compatibility options which an administrator might
1329reasonably wish to set. Interpret a list similarly to decode_bits(), so that
1330we look like log_selector.
1331
1332Arguments:
1333 option_spec the administrator-supplied string of options
1334 results ptr to long storage for the options bitmap
1335Returns success or failure
1336*/
1337
1338BOOL
1339tls_openssl_options_parse(uschar *option_spec, long *results)
1340{
1341long result, item;
1342uschar *s, *end;
1343uschar keep_c;
1344BOOL adding, item_parsed;
1345
0e944a0d 1346result = 0L;
da3ad30d
PP
1347/* Prior to 4.78 we or'd in SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; removed
1348 * from default because it increases BEAST susceptibility. */
77bb000f
PP
1349
1350if (option_spec == NULL)
1351 {
1352 *results = result;
1353 return TRUE;
1354 }
1355
1356for (s=option_spec; *s != '\0'; /**/)
1357 {
1358 while (isspace(*s)) ++s;
1359 if (*s == '\0')
1360 break;
1361 if (*s != '+' && *s != '-')
1362 {
1363 DEBUG(D_tls) debug_printf("malformed openssl option setting: "
0e944a0d 1364 "+ or - expected but found \"%s\"\n", s);
77bb000f
PP
1365 return FALSE;
1366 }
1367 adding = *s++ == '+';
1368 for (end = s; (*end != '\0') && !isspace(*end); ++end) /**/ ;
1369 keep_c = *end;
1370 *end = '\0';
1371 item_parsed = tls_openssl_one_option_parse(s, &item);
1372 if (!item_parsed)
1373 {
0e944a0d 1374 DEBUG(D_tls) debug_printf("openssl option setting unrecognised: \"%s\"\n", s);
77bb000f
PP
1375 return FALSE;
1376 }
1377 DEBUG(D_tls) debug_printf("openssl option, %s from %lx: %lx (%s)\n",
1378 adding ? "adding" : "removing", result, item, s);
1379 if (adding)
1380 result |= item;
1381 else
1382 result &= ~item;
1383 *end = keep_c;
1384 s = end;
1385 }
1386
1387*results = result;
1388return TRUE;
1389}
1390
059ec3d9 1391/* End of tls-openssl.c */