]> git.ipfire.org Git - people/ms/dma.git/blame - crypto.c
As a safety measure, don't install dma with setuid bit until it has undergone
[people/ms/dma.git] / crypto.c
CommitLineData
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.
34 *
bf28fcc6 35 * $DragonFly: src/libexec/dma/crypto.c,v 1.3 2008/09/02 15:11:49 matthias Exp $
86e4d161
MS
36 */
37
38#ifdef HAVE_CRYPTO
39
40#include <openssl/x509.h>
bf28fcc6 41#include <openssl/md5.h>
86e4d161
MS
42#include <openssl/ssl.h>
43#include <openssl/err.h>
44#include <openssl/pem.h>
45#include <openssl/rand.h>
46
47#include <syslog.h>
48
49#include "dma.h"
50
51extern struct config *config;
52
53static int
54init_cert_file(struct qitem *it, SSL_CTX *ctx, const char *path)
55{
56 int error;
57
58 /* Load certificate into ctx */
59 error = SSL_CTX_use_certificate_chain_file(ctx, path);
60 if (error < 1) {
61 syslog(LOG_ERR, "%s: SSL: Cannot load certificate: %s",
62 it->queueid, path);
63 return (-1);
64 }
65
66 /* Add private key to ctx */
67 error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
68 if (error < 1) {
69 syslog(LOG_ERR, "%s: SSL: Cannot load private key: %s",
70 it->queueid, path);
71 return (-1);
72 }
73
74 /*
75 * Check the consistency of a private key with the corresponding
76 * certificate
77 */
78 error = SSL_CTX_check_private_key(ctx);
79 if (error < 1) {
80 syslog(LOG_ERR, "%s: SSL: Cannot check private key: %s",
81 it->queueid, path);
82 return (-1);
83 }
84
85 return (0);
86}
87
88int
89smtp_init_crypto(struct qitem *it, int fd, int feature)
90{
91 SSL_CTX *ctx = NULL;
92 SSL_METHOD *meth = NULL;
93 X509 *cert;
94 char buf[2048];
95 int error;
96
97 /* Init SSL library */
98 SSL_library_init();
99
100 meth = TLSv1_client_method();
101
102 ctx = SSL_CTX_new(meth);
103 if (ctx == NULL) {
104 syslog(LOG_ERR, "%s: remote delivery deferred:"
105 " SSL init failed: %m", it->queueid);
106 return (2);
107 }
108
109 /* User supplied a certificate */
110 if (config->certfile != NULL)
111 init_cert_file(it, ctx, config->certfile);
112
113 /*
114 * If the user wants STARTTLS, we have to send EHLO here
115 */
116 if (((feature & SECURETRANS) != 0) &&
117 (feature & STARTTLS) != 0) {
118 /* TLS init phase, disable SSL_write */
50c8e68a 119 config->features |= NOSSL;
86e4d161
MS
120
121 send_remote_command(fd, "EHLO %s", hostname());
bf28fcc6 122 if (read_remote(fd, 0, NULL) == 2) {
86e4d161 123 send_remote_command(fd, "STARTTLS");
bf28fcc6 124 if (read_remote(fd, 0, NULL) != 2) {
86e4d161
MS
125 syslog(LOG_ERR, "%s: remote delivery failed:"
126 " STARTTLS not available: %m", it->queueid);
50c8e68a 127 config->features &= ~NOSSL;
86e4d161
MS
128 return (-1);
129 }
130 }
131 /* End of TLS init phase, enable SSL_write/read */
50c8e68a 132 config->features &= ~NOSSL;
86e4d161
MS
133 }
134
135 config->ssl = SSL_new(ctx);
136 if (config->ssl == NULL) {
137 syslog(LOG_ERR, "%s: remote delivery deferred:"
138 " SSL struct creation failed:", it->queueid);
139 return (2);
140 }
141
142 /* Set ssl to work in client mode */
143 SSL_set_connect_state(config->ssl);
144
145 /* Set fd for SSL in/output */
146 error = SSL_set_fd(config->ssl, fd);
147 if (error == 0) {
148 error = SSL_get_error(config->ssl, error);
149 syslog(LOG_ERR, "%s: remote delivery deferred:"
150 " SSL set fd failed (%d): %m", it->queueid, error);
151 return (2);
152 }
153
154 /* Open SSL connection */
155 error = SSL_connect(config->ssl);
156 if (error < 0) {
157 syslog(LOG_ERR, "%s: remote delivery failed:"
158 " SSL handshake fataly failed: %m", it->queueid);
159 return (-1);
160 }
161
162 /* Get peer certificate */
163 cert = SSL_get_peer_certificate(config->ssl);
164 if (cert == NULL) {
165 syslog(LOG_ERR, "%s: remote delivery deferred:"
166 " Peer did not provied certificate: %m", it->queueid);
167 }
168 X509_free(cert);
169
170 return (0);
171}
172
bf28fcc6
MS
173/*
174 * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk,
175 * M. Bellare and R. Canetti.
176 */
177void
178hmac_md5(text, text_len, key, key_len, digest)
179unsigned char* text; /* pointer to data stream */
180int text_len; /* length of data stream */
181unsigned char* key; /* pointer to authentication key */
182int key_len; /* length of authentication key */
183caddr_t digest; /* caller digest to be filled in */
184
185{
186 MD5_CTX context;
187 unsigned char k_ipad[65]; /* inner padding -
188 * key XORd with ipad
189 */
190 unsigned char k_opad[65]; /* outer padding -
191 * key XORd with opad
192 */
193 unsigned char tk[16];
194 int i;
195 /* if key is longer than 64 bytes reset it to key=MD5(key) */
196 if (key_len > 64) {
197
198 MD5_CTX tctx;
199
200 MD5_Init(&tctx);
201 MD5_Update(&tctx, key, key_len);
202 MD5_Final(tk, &tctx);
203
204 key = tk;
205 key_len = 16;
206 }
207
208 /*
209 * the HMAC_MD5 transform looks like:
210 *
211 * MD5(K XOR opad, MD5(K XOR ipad, text))
212 *
213 * where K is an n byte key
214 * ipad is the byte 0x36 repeated 64 times
215 *
216 * opad is the byte 0x5c repeated 64 times
217 * and text is the data being protected
218 */
219
220 /* start out by storing key in pads */
221 bzero( k_ipad, sizeof k_ipad);
222 bzero( k_opad, sizeof k_opad);
223 bcopy( key, k_ipad, key_len);
224 bcopy( key, k_opad, key_len);
225
226 /* XOR key with ipad and opad values */
227 for (i=0; i<64; i++) {
228 k_ipad[i] ^= 0x36;
229 k_opad[i] ^= 0x5c;
230 }
231 /*
232 * perform inner MD5
233 */
234 MD5_Init(&context); /* init context for 1st
235 * pass */
236 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
237 MD5_Update(&context, text, text_len); /* then text of datagram */
238 MD5_Final(digest, &context); /* finish up 1st pass */
239 /*
240 * perform outer MD5
241 */
242 MD5_Init(&context); /* init context for 2nd
243 * pass */
244 MD5_Update(&context, k_opad, 64); /* start with outer pad */
245 MD5_Update(&context, digest, 16); /* then results of 1st
246 * hash */
247 MD5_Final(digest, &context); /* finish up 2nd pass */
248}
249
86e4d161
MS
250/*
251 * CRAM-MD5 authentication
86e4d161
MS
252 */
253int
bf28fcc6 254smtp_auth_md5(struct qitem *it, int fd, char *login, char *password)
86e4d161 255{
bf28fcc6
MS
256 unsigned char buffer[BUF_SIZE], digest[BUF_SIZE], ascii_digest[33];
257 char *temp;
258 int len, i;
259 static char hextab[] = "0123456789abcdef";
260
261 temp = calloc(BUF_SIZE, 1);
262 memset(buffer, 0, sizeof(buffer));
263 memset(digest, 0, sizeof(digest));
264 memset(ascii_digest, 0, sizeof(ascii_digest));
265
266 /* Send AUTH command according to RFC 2554 */
267 send_remote_command(fd, "AUTH CRAM-MD5");
268 if (read_remote(fd, sizeof(buffer), buffer) != 3) {
269 syslog(LOG_ERR, "%s: smarthost authentification:"
270 " AUTH cram-md5 not available: %m", it->queueid);
271 /* if cram-md5 is not available */
272 return (-1);
273 }
274
275 /* skip 3 char status + 1 char space */
276 base64_decode(buffer + 4, temp);
277 hmac_md5(temp, strlen(temp), password, strlen(password), digest);
278
279 ascii_digest[32] = 0;
280 for (i = 0; i < 16; i++) {
281 ascii_digest[2*i] = hextab[digest[i] >> 4];
282 ascii_digest[2*i+1] = hextab[digest[i] & 15];
283 }
284
285 /* prepare answer */
286 snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
287
288 /* temp will be allocated inside base64_encode again */
289 free(temp);
290 /* encode answer */
291 len = base64_encode(buffer, strlen(buffer), &temp);
292 if (len <= 0)
293 return (-1);
294
295 /* send answer */
296 send_remote_command(fd, "%s", temp);
297 if (read_remote(fd, 0, NULL) != 2) {
298 syslog(LOG_ERR, "%s: remote delivery deferred:"
299 " AUTH cram-md5 failed: %m", it->queueid);
300 return (-2);
301 }
302
303 return (0);
86e4d161 304}
86e4d161
MS
305
306#endif /* HAVE_CRYPTO */