dma: clean up network code
[dragonfly.git] / libexec / dma / crypto.c
CommitLineData
f67beddd
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 *
bc7baf1d 35 * $DragonFly: src/libexec/dma/crypto.c,v 1.4 2008/09/30 17:47:21 swildner Exp $
f67beddd
MS
36 */
37
f67beddd 38#include <openssl/x509.h>
7b68d8ae 39#include <openssl/md5.h>
f67beddd
MS
40#include <openssl/ssl.h>
41#include <openssl/err.h>
42#include <openssl/pem.h>
43#include <openssl/rand.h>
44
45#include <syslog.h>
46
47#include "dma.h"
48
49extern struct config *config;
50
51static int
52init_cert_file(struct qitem *it, SSL_CTX *ctx, const char *path)
53{
54 int error;
55
56 /* Load certificate into ctx */
57 error = SSL_CTX_use_certificate_chain_file(ctx, path);
58 if (error < 1) {
59 syslog(LOG_ERR, "%s: SSL: Cannot load certificate: %s",
60 it->queueid, path);
61 return (-1);
62 }
63
64 /* Add private key to ctx */
65 error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
66 if (error < 1) {
67 syslog(LOG_ERR, "%s: SSL: Cannot load private key: %s",
68 it->queueid, path);
69 return (-1);
70 }
71
72 /*
73 * Check the consistency of a private key with the corresponding
74 * certificate
75 */
76 error = SSL_CTX_check_private_key(ctx);
77 if (error < 1) {
78 syslog(LOG_ERR, "%s: SSL: Cannot check private key: %s",
79 it->queueid, path);
80 return (-1);
81 }
82
83 return (0);
84}
85
86int
87smtp_init_crypto(struct qitem *it, int fd, int feature)
88{
89 SSL_CTX *ctx = NULL;
90 SSL_METHOD *meth = NULL;
91 X509 *cert;
f67beddd
MS
92 int error;
93
ea921663 94 /* XXX clean up on error/close */
f67beddd
MS
95 /* Init SSL library */
96 SSL_library_init();
97
98 meth = TLSv1_client_method();
99
100 ctx = SSL_CTX_new(meth);
101 if (ctx == NULL) {
99d6192f 102 syslog(LOG_WARNING, "%s: remote delivery deferred:"
f67beddd
MS
103 " SSL init failed: %m", it->queueid);
104 return (2);
105 }
106
107 /* User supplied a certificate */
108 if (config->certfile != NULL)
109 init_cert_file(it, ctx, config->certfile);
110
111 /*
112 * If the user wants STARTTLS, we have to send EHLO here
113 */
114 if (((feature & SECURETRANS) != 0) &&
115 (feature & STARTTLS) != 0) {
116 /* TLS init phase, disable SSL_write */
6ef9fe01 117 config->features |= NOSSL;
f67beddd
MS
118
119 send_remote_command(fd, "EHLO %s", hostname());
7b68d8ae 120 if (read_remote(fd, 0, NULL) == 2) {
f67beddd 121 send_remote_command(fd, "STARTTLS");
7b68d8ae 122 if (read_remote(fd, 0, NULL) != 2) {
f67beddd 123 syslog(LOG_ERR, "%s: remote delivery failed:"
a5a8a1a4
SS
124 " STARTTLS not available: %s", it->queueid,
125 neterr);
6ef9fe01 126 config->features &= ~NOSSL;
f67beddd
MS
127 return (-1);
128 }
129 }
130 /* End of TLS init phase, enable SSL_write/read */
6ef9fe01 131 config->features &= ~NOSSL;
f67beddd
MS
132 }
133
134 config->ssl = SSL_new(ctx);
135 if (config->ssl == NULL) {
99d6192f 136 syslog(LOG_NOTICE, "%s: remote delivery deferred:"
f67beddd
MS
137 " SSL struct creation failed:", it->queueid);
138 return (2);
139 }
140
141 /* Set ssl to work in client mode */
142 SSL_set_connect_state(config->ssl);
143
144 /* Set fd for SSL in/output */
145 error = SSL_set_fd(config->ssl, fd);
146 if (error == 0) {
147 error = SSL_get_error(config->ssl, error);
99d6192f 148 syslog(LOG_NOTICE, "%s: remote delivery deferred:"
f67beddd
MS
149 " SSL set fd failed (%d): %m", it->queueid, error);
150 return (2);
151 }
152
153 /* Open SSL connection */
154 error = SSL_connect(config->ssl);
155 if (error < 0) {
156 syslog(LOG_ERR, "%s: remote delivery failed:"
ce0c4a73 157 " SSL handshake failed fatally: %m", it->queueid);
f67beddd
MS
158 return (-1);
159 }
160
161 /* Get peer certificate */
162 cert = SSL_get_peer_certificate(config->ssl);
163 if (cert == NULL) {
99d6192f 164 syslog(LOG_WARNING, "%s: remote delivery deferred:"
ce0c4a73 165 " Peer did not provide certificate: %m", it->queueid);
f67beddd
MS
166 }
167 X509_free(cert);
168
169 return (0);
170}
171
7b68d8ae
MS
172/*
173 * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk,
174 * M. Bellare and R. Canetti.
bc7baf1d
SW
175 *
176 * text pointer to data stream
177 * text_len length of data stream
178 * key pointer to authentication key
179 * key_len length of authentication key
180 * digest caller digest to be filled int
181 */
7b68d8ae 182void
bc7baf1d
SW
183hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
184 caddr_t digest)
7b68d8ae
MS
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
f67beddd
MS
250/*
251 * CRAM-MD5 authentication
f67beddd
MS
252 */
253int
7b68d8ae 254smtp_auth_md5(struct qitem *it, int fd, char *login, char *password)
f67beddd 255{
7b68d8ae
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) {
5d7fe8bb 269 syslog(LOG_DEBUG, "%s: smarthost authentification:"
a5a8a1a4
SS
270 " AUTH cram-md5 not available: %s", it->queueid,
271 neterr);
7b68d8ae
MS
272 /* if cram-md5 is not available */
273 return (-1);
274 }
275
276 /* skip 3 char status + 1 char space */
277 base64_decode(buffer + 4, temp);
278 hmac_md5(temp, strlen(temp), password, strlen(password), digest);
5d7fe8bb 279 free(temp);
7b68d8ae
MS
280
281 ascii_digest[32] = 0;
282 for (i = 0; i < 16; i++) {
283 ascii_digest[2*i] = hextab[digest[i] >> 4];
284 ascii_digest[2*i+1] = hextab[digest[i] & 15];
285 }
286
287 /* prepare answer */
288 snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
289
7b68d8ae
MS
290 /* encode answer */
291 len = base64_encode(buffer, strlen(buffer), &temp);
5d7fe8bb
SS
292 if (len < 0) {
293 syslog(LOG_ERR, "%s: can not encode auth reply: %m",
294 it->queueid);
7b68d8ae 295 return (-1);
5d7fe8bb 296 }
7b68d8ae
MS
297
298 /* send answer */
299 send_remote_command(fd, "%s", temp);
5d7fe8bb 300 free(temp);
7b68d8ae 301 if (read_remote(fd, 0, NULL) != 2) {
99d6192f 302 syslog(LOG_WARNING, "%s: remote delivery deferred:"
a5a8a1a4
SS
303 " AUTH cram-md5 failed: %s", it->queueid,
304 neterr);
7b68d8ae
MS
305 return (-2);
306 }
307
308 return (0);
f67beddd 309}