dma: restructure logging
[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
f67beddd 49static int
405f48ee 50init_cert_file(SSL_CTX *ctx, const char *path)
f67beddd
MS
51{
52 int error;
53
54 /* Load certificate into ctx */
55 error = SSL_CTX_use_certificate_chain_file(ctx, path);
56 if (error < 1) {
405f48ee 57 syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr());
f67beddd
MS
58 return (-1);
59 }
60
61 /* Add private key to ctx */
62 error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
63 if (error < 1) {
405f48ee 64 syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr());
f67beddd
MS
65 return (-1);
66 }
67
68 /*
69 * Check the consistency of a private key with the corresponding
70 * certificate
71 */
72 error = SSL_CTX_check_private_key(ctx);
73 if (error < 1) {
405f48ee 74 syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr());
f67beddd
MS
75 return (-1);
76 }
77
78 return (0);
79}
80
81int
405f48ee 82smtp_init_crypto(int fd, int feature)
f67beddd
MS
83{
84 SSL_CTX *ctx = NULL;
85 SSL_METHOD *meth = NULL;
86 X509 *cert;
f67beddd
MS
87 int error;
88
ea921663 89 /* XXX clean up on error/close */
f67beddd
MS
90 /* Init SSL library */
91 SSL_library_init();
e2c88018 92 SSL_load_error_strings();
f67beddd
MS
93
94 meth = TLSv1_client_method();
95
96 ctx = SSL_CTX_new(meth);
97 if (ctx == NULL) {
405f48ee 98 syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
e2c88018 99 return (1);
f67beddd
MS
100 }
101
102 /* User supplied a certificate */
e2c88018 103 if (config->certfile != NULL) {
405f48ee 104 error = init_cert_file(ctx, config->certfile);
e2c88018 105 if (error) {
405f48ee 106 syslog(LOG_WARNING, "remote delivery deferred");
e2c88018
SS
107 return (1);
108 }
109 }
f67beddd
MS
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) {
405f48ee
SS
123 syslog(LOG_ERR, "remote delivery deferred:"
124 " STARTTLS not available: %s", neterr);
6ef9fe01 125 config->features &= ~NOSSL;
e2c88018 126 return (1);
f67beddd
MS
127 }
128 }
129 /* End of TLS init phase, enable SSL_write/read */
6ef9fe01 130 config->features &= ~NOSSL;
f67beddd
MS
131 }
132
133 config->ssl = SSL_new(ctx);
134 if (config->ssl == NULL) {
405f48ee
SS
135 syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s",
136 ssl_errstr());
e2c88018 137 return (1);
f67beddd
MS
138 }
139
140 /* Set ssl to work in client mode */
141 SSL_set_connect_state(config->ssl);
142
143 /* Set fd for SSL in/output */
144 error = SSL_set_fd(config->ssl, fd);
145 if (error == 0) {
405f48ee
SS
146 syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s",
147 ssl_errstr());
e2c88018 148 return (1);
f67beddd
MS
149 }
150
151 /* Open SSL connection */
152 error = SSL_connect(config->ssl);
153 if (error < 0) {
405f48ee
SS
154 syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s",
155 ssl_errstr());
e2c88018 156 return (1);
f67beddd
MS
157 }
158
159 /* Get peer certificate */
160 cert = SSL_get_peer_certificate(config->ssl);
161 if (cert == NULL) {
405f48ee
SS
162 syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s",
163 ssl_errstr());
f67beddd
MS
164 }
165 X509_free(cert);
166
167 return (0);
168}
169
7b68d8ae
MS
170/*
171 * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk,
172 * M. Bellare and R. Canetti.
bc7baf1d
SW
173 *
174 * text pointer to data stream
175 * text_len length of data stream
176 * key pointer to authentication key
177 * key_len length of authentication key
178 * digest caller digest to be filled int
179 */
7b68d8ae 180void
bc7baf1d
SW
181hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
182 caddr_t digest)
7b68d8ae
MS
183{
184 MD5_CTX context;
185 unsigned char k_ipad[65]; /* inner padding -
186 * key XORd with ipad
187 */
188 unsigned char k_opad[65]; /* outer padding -
189 * key XORd with opad
190 */
191 unsigned char tk[16];
192 int i;
193 /* if key is longer than 64 bytes reset it to key=MD5(key) */
194 if (key_len > 64) {
195
196 MD5_CTX tctx;
197
198 MD5_Init(&tctx);
199 MD5_Update(&tctx, key, key_len);
200 MD5_Final(tk, &tctx);
201
202 key = tk;
203 key_len = 16;
204 }
205
206 /*
207 * the HMAC_MD5 transform looks like:
208 *
209 * MD5(K XOR opad, MD5(K XOR ipad, text))
210 *
211 * where K is an n byte key
212 * ipad is the byte 0x36 repeated 64 times
213 *
214 * opad is the byte 0x5c repeated 64 times
215 * and text is the data being protected
216 */
217
218 /* start out by storing key in pads */
219 bzero( k_ipad, sizeof k_ipad);
220 bzero( k_opad, sizeof k_opad);
221 bcopy( key, k_ipad, key_len);
222 bcopy( key, k_opad, key_len);
223
224 /* XOR key with ipad and opad values */
225 for (i=0; i<64; i++) {
226 k_ipad[i] ^= 0x36;
227 k_opad[i] ^= 0x5c;
228 }
229 /*
230 * perform inner MD5
231 */
232 MD5_Init(&context); /* init context for 1st
233 * pass */
234 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
235 MD5_Update(&context, text, text_len); /* then text of datagram */
236 MD5_Final(digest, &context); /* finish up 1st pass */
237 /*
238 * perform outer MD5
239 */
240 MD5_Init(&context); /* init context for 2nd
241 * pass */
242 MD5_Update(&context, k_opad, 64); /* start with outer pad */
243 MD5_Update(&context, digest, 16); /* then results of 1st
244 * hash */
245 MD5_Final(digest, &context); /* finish up 2nd pass */
246}
247
f67beddd
MS
248/*
249 * CRAM-MD5 authentication
f67beddd
MS
250 */
251int
405f48ee 252smtp_auth_md5(int fd, char *login, char *password)
f67beddd 253{
7b68d8ae
MS
254 unsigned char buffer[BUF_SIZE], digest[BUF_SIZE], ascii_digest[33];
255 char *temp;
256 int len, i;
257 static char hextab[] = "0123456789abcdef";
258
259 temp = calloc(BUF_SIZE, 1);
260 memset(buffer, 0, sizeof(buffer));
261 memset(digest, 0, sizeof(digest));
262 memset(ascii_digest, 0, sizeof(ascii_digest));
263
264 /* Send AUTH command according to RFC 2554 */
265 send_remote_command(fd, "AUTH CRAM-MD5");
266 if (read_remote(fd, sizeof(buffer), buffer) != 3) {
405f48ee
SS
267 syslog(LOG_DEBUG, "smarthost authentification:"
268 " AUTH cram-md5 not available: %s", neterr);
7b68d8ae
MS
269 /* if cram-md5 is not available */
270 return (-1);
271 }
272
273 /* skip 3 char status + 1 char space */
274 base64_decode(buffer + 4, temp);
275 hmac_md5(temp, strlen(temp), password, strlen(password), digest);
5d7fe8bb 276 free(temp);
7b68d8ae
MS
277
278 ascii_digest[32] = 0;
279 for (i = 0; i < 16; i++) {
280 ascii_digest[2*i] = hextab[digest[i] >> 4];
281 ascii_digest[2*i+1] = hextab[digest[i] & 15];
282 }
283
284 /* prepare answer */
285 snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
286
7b68d8ae
MS
287 /* encode answer */
288 len = base64_encode(buffer, strlen(buffer), &temp);
5d7fe8bb 289 if (len < 0) {
405f48ee 290 syslog(LOG_ERR, "can not encode auth reply: %m");
7b68d8ae 291 return (-1);
5d7fe8bb 292 }
7b68d8ae
MS
293
294 /* send answer */
295 send_remote_command(fd, "%s", temp);
5d7fe8bb 296 free(temp);
7b68d8ae 297 if (read_remote(fd, 0, NULL) != 2) {
405f48ee
SS
298 syslog(LOG_WARNING, "remote delivery deferred:"
299 " AUTH cram-md5 failed: %s", neterr);
7b68d8ae
MS
300 return (-2);
301 }
302
303 return (0);
f67beddd 304}