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