]>
Commit | Line | Data |
---|---|---|
86e4d161 MS |
1 | /* |
2 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. | |
3 | * | |
4 | * This code is derived from software contributed to The DragonFly Project | |
5 | * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg, | |
6 | * Germany. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific, prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
86e4d161 MS |
34 | */ |
35 | ||
86e4d161 | 36 | #include <openssl/x509.h> |
bf28fcc6 | 37 | #include <openssl/md5.h> |
86e4d161 MS |
38 | #include <openssl/ssl.h> |
39 | #include <openssl/err.h> | |
40 | #include <openssl/pem.h> | |
41 | #include <openssl/rand.h> | |
42 | ||
43 | #include <syslog.h> | |
44 | ||
45 | #include "dma.h" | |
46 | ||
86e4d161 | 47 | static int |
4d5af2b0 | 48 | init_cert_file(SSL_CTX *ctx, const char *path) |
86e4d161 MS |
49 | { |
50 | int error; | |
51 | ||
52 | /* Load certificate into ctx */ | |
53 | error = SSL_CTX_use_certificate_chain_file(ctx, path); | |
54 | if (error < 1) { | |
4d5af2b0 | 55 | syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr()); |
86e4d161 MS |
56 | return (-1); |
57 | } | |
58 | ||
59 | /* Add private key to ctx */ | |
60 | error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM); | |
61 | if (error < 1) { | |
4d5af2b0 | 62 | syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr()); |
86e4d161 MS |
63 | return (-1); |
64 | } | |
65 | ||
66 | /* | |
67 | * Check the consistency of a private key with the corresponding | |
68 | * certificate | |
69 | */ | |
70 | error = SSL_CTX_check_private_key(ctx); | |
71 | if (error < 1) { | |
4d5af2b0 | 72 | syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr()); |
86e4d161 MS |
73 | return (-1); |
74 | } | |
75 | ||
76 | return (0); | |
77 | } | |
78 | ||
79 | int | |
4d5af2b0 | 80 | smtp_init_crypto(int fd, int feature) |
86e4d161 MS |
81 | { |
82 | SSL_CTX *ctx = NULL; | |
83 | SSL_METHOD *meth = NULL; | |
84 | X509 *cert; | |
86e4d161 MS |
85 | int error; |
86 | ||
8aea09c9 | 87 | /* XXX clean up on error/close */ |
86e4d161 MS |
88 | /* Init SSL library */ |
89 | SSL_library_init(); | |
53885e5a | 90 | SSL_load_error_strings(); |
86e4d161 MS |
91 | |
92 | meth = TLSv1_client_method(); | |
93 | ||
94 | ctx = SSL_CTX_new(meth); | |
95 | if (ctx == NULL) { | |
4d5af2b0 | 96 | syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr()); |
53885e5a | 97 | return (1); |
86e4d161 MS |
98 | } |
99 | ||
100 | /* User supplied a certificate */ | |
a0c4afa6 SS |
101 | if (config.certfile != NULL) { |
102 | error = init_cert_file(ctx, config.certfile); | |
53885e5a | 103 | if (error) { |
4d5af2b0 | 104 | syslog(LOG_WARNING, "remote delivery deferred"); |
53885e5a SS |
105 | return (1); |
106 | } | |
107 | } | |
86e4d161 MS |
108 | |
109 | /* | |
110 | * If the user wants STARTTLS, we have to send EHLO here | |
111 | */ | |
112 | if (((feature & SECURETRANS) != 0) && | |
113 | (feature & STARTTLS) != 0) { | |
114 | /* TLS init phase, disable SSL_write */ | |
a0c4afa6 | 115 | config.features |= NOSSL; |
86e4d161 MS |
116 | |
117 | send_remote_command(fd, "EHLO %s", hostname()); | |
bf28fcc6 | 118 | if (read_remote(fd, 0, NULL) == 2) { |
86e4d161 | 119 | send_remote_command(fd, "STARTTLS"); |
bf28fcc6 | 120 | if (read_remote(fd, 0, NULL) != 2) { |
4d5af2b0 SS |
121 | syslog(LOG_ERR, "remote delivery deferred:" |
122 | " STARTTLS not available: %s", neterr); | |
53885e5a | 123 | return (1); |
86e4d161 MS |
124 | } |
125 | } | |
126 | /* End of TLS init phase, enable SSL_write/read */ | |
a0c4afa6 | 127 | config.features &= ~NOSSL; |
86e4d161 MS |
128 | } |
129 | ||
a0c4afa6 SS |
130 | config.ssl = SSL_new(ctx); |
131 | if (config.ssl == NULL) { | |
4d5af2b0 SS |
132 | syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s", |
133 | ssl_errstr()); | |
53885e5a | 134 | return (1); |
86e4d161 MS |
135 | } |
136 | ||
137 | /* Set ssl to work in client mode */ | |
a0c4afa6 | 138 | SSL_set_connect_state(config.ssl); |
86e4d161 MS |
139 | |
140 | /* Set fd for SSL in/output */ | |
a0c4afa6 | 141 | error = SSL_set_fd(config.ssl, fd); |
86e4d161 | 142 | if (error == 0) { |
4d5af2b0 SS |
143 | syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s", |
144 | ssl_errstr()); | |
53885e5a | 145 | return (1); |
86e4d161 MS |
146 | } |
147 | ||
148 | /* Open SSL connection */ | |
a0c4afa6 | 149 | error = SSL_connect(config.ssl); |
86e4d161 | 150 | if (error < 0) { |
4d5af2b0 SS |
151 | syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s", |
152 | ssl_errstr()); | |
53885e5a | 153 | return (1); |
86e4d161 MS |
154 | } |
155 | ||
156 | /* Get peer certificate */ | |
a0c4afa6 | 157 | cert = SSL_get_peer_certificate(config.ssl); |
86e4d161 | 158 | if (cert == NULL) { |
4d5af2b0 SS |
159 | syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s", |
160 | ssl_errstr()); | |
86e4d161 MS |
161 | } |
162 | X509_free(cert); | |
163 | ||
164 | return (0); | |
165 | } | |
166 | ||
bf28fcc6 MS |
167 | /* |
168 | * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk, | |
169 | * M. Bellare and R. Canetti. | |
c507d897 SW |
170 | * |
171 | * text pointer to data stream | |
172 | * text_len length of data stream | |
173 | * key pointer to authentication key | |
174 | * key_len length of authentication key | |
175 | * digest caller digest to be filled int | |
176 | */ | |
bf28fcc6 | 177 | void |
c507d897 | 178 | hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, |
9b921550 | 179 | unsigned char* digest) |
bf28fcc6 MS |
180 | { |
181 | MD5_CTX context; | |
182 | unsigned char k_ipad[65]; /* inner padding - | |
183 | * key XORd with ipad | |
184 | */ | |
185 | unsigned char k_opad[65]; /* outer padding - | |
186 | * key XORd with opad | |
187 | */ | |
188 | unsigned char tk[16]; | |
189 | int i; | |
190 | /* if key is longer than 64 bytes reset it to key=MD5(key) */ | |
191 | if (key_len > 64) { | |
192 | ||
193 | MD5_CTX tctx; | |
194 | ||
195 | MD5_Init(&tctx); | |
196 | MD5_Update(&tctx, key, key_len); | |
197 | MD5_Final(tk, &tctx); | |
198 | ||
199 | key = tk; | |
200 | key_len = 16; | |
201 | } | |
202 | ||
203 | /* | |
204 | * the HMAC_MD5 transform looks like: | |
205 | * | |
206 | * MD5(K XOR opad, MD5(K XOR ipad, text)) | |
207 | * | |
208 | * where K is an n byte key | |
209 | * ipad is the byte 0x36 repeated 64 times | |
210 | * | |
211 | * opad is the byte 0x5c repeated 64 times | |
212 | * and text is the data being protected | |
213 | */ | |
214 | ||
215 | /* start out by storing key in pads */ | |
216 | bzero( k_ipad, sizeof k_ipad); | |
217 | bzero( k_opad, sizeof k_opad); | |
218 | bcopy( key, k_ipad, key_len); | |
219 | bcopy( key, k_opad, key_len); | |
220 | ||
221 | /* XOR key with ipad and opad values */ | |
222 | for (i=0; i<64; i++) { | |
223 | k_ipad[i] ^= 0x36; | |
224 | k_opad[i] ^= 0x5c; | |
225 | } | |
226 | /* | |
227 | * perform inner MD5 | |
228 | */ | |
229 | MD5_Init(&context); /* init context for 1st | |
230 | * pass */ | |
231 | MD5_Update(&context, k_ipad, 64); /* start with inner pad */ | |
232 | MD5_Update(&context, text, text_len); /* then text of datagram */ | |
233 | MD5_Final(digest, &context); /* finish up 1st pass */ | |
234 | /* | |
235 | * perform outer MD5 | |
236 | */ | |
237 | MD5_Init(&context); /* init context for 2nd | |
238 | * pass */ | |
239 | MD5_Update(&context, k_opad, 64); /* start with outer pad */ | |
240 | MD5_Update(&context, digest, 16); /* then results of 1st | |
241 | * hash */ | |
242 | MD5_Final(digest, &context); /* finish up 2nd pass */ | |
243 | } | |
244 | ||
86e4d161 MS |
245 | /* |
246 | * CRAM-MD5 authentication | |
86e4d161 MS |
247 | */ |
248 | int | |
4d5af2b0 | 249 | smtp_auth_md5(int fd, char *login, char *password) |
86e4d161 | 250 | { |
9b921550 SS |
251 | unsigned char digest[BUF_SIZE]; |
252 | char buffer[BUF_SIZE], ascii_digest[33]; | |
bf28fcc6 MS |
253 | char *temp; |
254 | int len, i; | |
255 | static char hextab[] = "0123456789abcdef"; | |
256 | ||
257 | temp = calloc(BUF_SIZE, 1); | |
258 | memset(buffer, 0, sizeof(buffer)); | |
259 | memset(digest, 0, sizeof(digest)); | |
260 | memset(ascii_digest, 0, sizeof(ascii_digest)); | |
261 | ||
262 | /* Send AUTH command according to RFC 2554 */ | |
263 | send_remote_command(fd, "AUTH CRAM-MD5"); | |
264 | if (read_remote(fd, sizeof(buffer), buffer) != 3) { | |
904bbe47 | 265 | syslog(LOG_DEBUG, "smarthost authentication:" |
4d5af2b0 | 266 | " AUTH cram-md5 not available: %s", neterr); |
bf28fcc6 MS |
267 | /* if cram-md5 is not available */ |
268 | return (-1); | |
269 | } | |
270 | ||
271 | /* skip 3 char status + 1 char space */ | |
272 | base64_decode(buffer + 4, temp); | |
9b921550 SS |
273 | hmac_md5((unsigned char *)temp, strlen(temp), |
274 | (unsigned char *)password, strlen(password), digest); | |
196b1b32 | 275 | free(temp); |
bf28fcc6 MS |
276 | |
277 | ascii_digest[32] = 0; | |
278 | for (i = 0; i < 16; i++) { | |
279 | ascii_digest[2*i] = hextab[digest[i] >> 4]; | |
280 | ascii_digest[2*i+1] = hextab[digest[i] & 15]; | |
281 | } | |
282 | ||
283 | /* prepare answer */ | |
284 | snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest); | |
285 | ||
bf28fcc6 MS |
286 | /* encode answer */ |
287 | len = base64_encode(buffer, strlen(buffer), &temp); | |
196b1b32 | 288 | if (len < 0) { |
4d5af2b0 | 289 | syslog(LOG_ERR, "can not encode auth reply: %m"); |
bf28fcc6 | 290 | return (-1); |
196b1b32 | 291 | } |
bf28fcc6 MS |
292 | |
293 | /* send answer */ | |
294 | send_remote_command(fd, "%s", temp); | |
196b1b32 | 295 | free(temp); |
bf28fcc6 | 296 | if (read_remote(fd, 0, NULL) != 2) { |
4d5af2b0 SS |
297 | syslog(LOG_WARNING, "remote delivery deferred:" |
298 | " AUTH cram-md5 failed: %s", neterr); | |
bf28fcc6 MS |
299 | return (-2); |
300 | } | |
301 | ||
302 | return (0); | |
86e4d161 | 303 | } |