Overhaul of GnuTLS code.
[exim.git] / src / src / tls.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 TLS (aka SSL) support for Exim. The code for OpenSSL is
9based on a patch that was originally contributed by Steve Haslam. It was
10adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
11based on a patch contributed by Nikos Mavroyanopoulos. Because these packages
12are so very different, the functions for each are kept in separate files. The
13relevant file is #included as required, after any any common functions.
14
15No cryptographic code is included in Exim. All this module does is to call
16functions from the OpenSSL or GNU TLS libraries. */
17
18
19#include "exim.h"
20
21/* This module is compiled only when it is specifically requested in the
22build-time configuration. However, some compilers don't like compiling empty
23modules, so keep them happy with a dummy when skipping the rest. Make it
24reference itself to stop picky compilers complaining that it is unused, and put
25in a dummy argument to stop even pickier compilers complaining about infinite
26loops. */
27
28#ifndef SUPPORT_TLS
29static void dummy(int x) { dummy(x-1); }
30#else
31
32/* Static variables that are used for buffering data by both sets of
33functions and the common functions below. */
34
35
36static uschar *ssl_xfer_buffer = NULL;
17c76198 37static const int ssl_xfer_buffer_size = 4096;
059ec3d9
PH
38static int ssl_xfer_buffer_lwm = 0;
39static int ssl_xfer_buffer_hwm = 0;
40static int ssl_xfer_eof = 0;
41static int ssl_xfer_error = 0;
42
44bbabb5 43uschar *tls_channelbinding_b64 = NULL;
059ec3d9
PH
44
45
46/*************************************************
47* Expand string; give error on failure *
48*************************************************/
49
50/* If expansion is forced to fail, set the result NULL and return TRUE.
51Other failures return FALSE. For a server, an SMTP response is given.
52
53Arguments:
54 s the string to expand; if NULL just return TRUE
55 name name of string being expanded (for error)
56 result where to put the result
57
58Returns: TRUE if OK; result may still be NULL after forced failure
59*/
60
61static BOOL
17c76198 62expand_check(const uschar *s, const uschar *name, uschar **result)
059ec3d9
PH
63{
64if (s == NULL) *result = NULL; else
65 {
17c76198 66 *result = expand_string(US s); /* need to clean up const some more */
059ec3d9
PH
67 if (*result == NULL && !expand_string_forcedfail)
68 {
69 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
70 expand_string_message);
71 return FALSE;
72 }
73 }
74return TRUE;
75}
76
77
78/*************************************************
79* Many functions are package-specific *
80*************************************************/
81
82#ifdef USE_GNUTLS
83#include "tls-gnu.c"
84#else
85#include "tls-openssl.c"
86#endif
87
88
89
90/*************************************************
91* TLS version of ungetc *
92*************************************************/
93
94/* Puts a character back in the input buffer. Only ever
95called once.
96
97Arguments:
98 ch the character
99
100Returns: the character
101*/
102
103int
104tls_ungetc(int ch)
105{
106ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
107return ch;
108}
109
110
111
112/*************************************************
113* TLS version of feof *
114*************************************************/
115
116/* Tests for a previous EOF
117
118Arguments: none
119Returns: non-zero if the eof flag is set
120*/
121
122int
123tls_feof(void)
124{
125return ssl_xfer_eof;
126}
127
128
129
130/*************************************************
131* TLS version of ferror *
132*************************************************/
133
134/* Tests for a previous read error, and returns with errno
135restored to what it was when the error was detected.
136
137>>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
138
139Arguments: none
140Returns: non-zero if the error flag is set
141*/
142
143int
144tls_ferror(void)
145{
146return ssl_xfer_error;
147}
148
58eb016e
PH
149
150/*************************************************
151* TLS version of smtp_buffered *
152*************************************************/
153
154/* Tests for unused chars in the TLS input buffer.
155
156Arguments: none
157Returns: TRUE/FALSE
158*/
159
160BOOL
161tls_smtp_buffered(void)
162{
163return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
164}
165
166
059ec3d9
PH
167#endif /* SUPPORT_TLS */
168
169/* End of tls.c */