xref: /dragonfly/libexec/dma/crypto.c (revision c8860c9a)
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  */
35 
36 #include <openssl/x509.h>
37 #include <openssl/md5.h>
38 #include <openssl/ssl.h>
39 #include <openssl/err.h>
40 #include <openssl/pem.h>
41 #include <openssl/rand.h>
42 
43 #include <strings.h>
44 #include <string.h>
45 #include <syslog.h>
46 
47 #include "dma.h"
48 
49 static int
50 init_cert_file(SSL_CTX *ctx, const char *path)
51 {
52 	int error;
53 
54 	/* Load certificate into ctx */
55 	error = SSL_CTX_use_certificate_chain_file(ctx, path);
56 	if (error < 1) {
57 		syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr());
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) {
64 		syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr());
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) {
74 		syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr());
75 		return (-1);
76 	}
77 
78 	return (0);
79 }
80 
81 static int
82 verify_server_fingerprint(const X509 *cert)
83 {
84 	unsigned char fingerprint[EVP_MAX_MD_SIZE] = {0};
85 	unsigned int fingerprint_len = 0;
86 	if(!X509_digest(cert, EVP_sha256(), fingerprint, &fingerprint_len)) {
87 		syslog(LOG_WARNING, "failed to load fingerprint of server certicate: %s",
88 			   ssl_errstr());
89 		return (1);
90 	}
91 	if(fingerprint_len != SHA256_DIGEST_LENGTH) {
92 		syslog(LOG_WARNING, "sha256 fingerprint has unexpected length of %d bytes",
93 		       fingerprint_len);
94 		return (1);
95 	}
96 	if(memcmp(fingerprint, config.fingerprint, SHA256_DIGEST_LENGTH) != 0) {
97 		syslog(LOG_WARNING, "fingerprints do not match");
98 		return (1);
99 	}
100 	syslog(LOG_DEBUG, "successfully verified server certificate fingerprint");
101 	return (0);
102 }
103 
104 int
105 smtp_init_crypto(int fd, int feature, struct smtp_features* features)
106 {
107 	SSL_CTX *ctx = NULL;
108 #if (OPENSSL_VERSION_NUMBER >= 0x00909000L)
109 	const SSL_METHOD *meth = NULL;
110 #else
111 	SSL_METHOD *meth = NULL;
112 #endif
113 	X509 *cert;
114 	int error;
115 
116 	/* XXX clean up on error/close */
117 	/* Init SSL library */
118 	SSL_library_init();
119 	SSL_load_error_strings();
120 
121 	// Allow any possible version
122 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
123 	meth = TLS_client_method();
124 #else
125 	meth = SSLv23_client_method();
126 #endif
127 
128 	ctx = SSL_CTX_new(meth);
129 	if (ctx == NULL) {
130 		syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
131 		return (1);
132 	}
133 
134 	/* User supplied a certificate */
135 	if (config.certfile != NULL) {
136 		error = init_cert_file(ctx, config.certfile);
137 		if (error) {
138 			syslog(LOG_WARNING, "remote delivery deferred");
139 			return (1);
140 		}
141 	}
142 
143 	/*
144 	 * If the user wants STARTTLS, we have to send EHLO here
145 	 */
146 	if (((feature & SECURETRANSFER) != 0) &&
147 	     (feature & STARTTLS) != 0) {
148 		/* TLS init phase, disable SSL_write */
149 		config.features |= NOSSL;
150 
151 		if (perform_server_greeting(fd, features) == 0) {
152 			send_remote_command(fd, "STARTTLS");
153 			if (read_remote(fd, 0, NULL) != 2) {
154 				if ((feature & TLS_OPP) == 0) {
155 					syslog(LOG_ERR, "remote delivery deferred: STARTTLS not available: %s", neterr);
156 					return (1);
157 				} else {
158 					syslog(LOG_INFO, "in opportunistic TLS mode, STARTTLS not available: %s", neterr);
159 					return (0);
160 				}
161 			}
162 		} else {
163 			syslog(LOG_ERR, "remote delivery deferred: could not perform server greeting: %s",
164 				neterr);
165 			return (1);
166 		}
167 
168 		/* End of TLS init phase, enable SSL_write/read */
169 		config.features &= ~NOSSL;
170 	}
171 
172 	config.ssl = SSL_new(ctx);
173 	if (config.ssl == NULL) {
174 		syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s",
175 		       ssl_errstr());
176 		return (1);
177 	}
178 
179 	/* Set ssl to work in client mode */
180 	SSL_set_connect_state(config.ssl);
181 
182 	/* Set fd for SSL in/output */
183 	error = SSL_set_fd(config.ssl, fd);
184 	if (error == 0) {
185 		syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s",
186 		       ssl_errstr());
187 		return (1);
188 	}
189 
190 	/* Open SSL connection */
191 	error = SSL_connect(config.ssl);
192 	if (error != 1) {
193 		syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s",
194 		       ssl_errstr());
195 		return (1);
196 	}
197 
198 	/* Get peer certificate */
199 	cert = SSL_get_peer_certificate(config.ssl);
200 	if (cert == NULL) {
201 		syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s",
202 		       ssl_errstr());
203 		return (1);
204 	}
205 	if(config.fingerprint != NULL && verify_server_fingerprint(cert)) {
206 		X509_free(cert);
207 		return (1);
208 	}
209 	X509_free(cert);
210 
211 	return (0);
212 }
213 
214 /*
215  * hmac_md5() taken out of RFC 2104.  This RFC was written by H. Krawczyk,
216  * M. Bellare and R. Canetti.
217  *
218  * text      pointer to data stream
219  * text_len  length of data stream
220  * key       pointer to authentication key
221  * key_len   length of authentication key
222  * digest    caller digest to be filled int
223  */
224 void
225 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
226     unsigned char* digest)
227 {
228         MD5_CTX context;
229         unsigned char k_ipad[65];    /* inner padding -
230                                       * key XORd with ipad
231                                       */
232         unsigned char k_opad[65];    /* outer padding -
233                                       * key XORd with opad
234                                       */
235         unsigned char tk[16];
236         int i;
237         /* if key is longer than 64 bytes reset it to key=MD5(key) */
238         if (key_len > 64) {
239 
240                 MD5_CTX      tctx;
241 
242                 MD5_Init(&tctx);
243                 MD5_Update(&tctx, key, key_len);
244                 MD5_Final(tk, &tctx);
245 
246                 key = tk;
247                 key_len = 16;
248         }
249 
250         /*
251          * the HMAC_MD5 transform looks like:
252          *
253          * MD5(K XOR opad, MD5(K XOR ipad, text))
254          *
255          * where K is an n byte key
256          * ipad is the byte 0x36 repeated 64 times
257 	 *
258          * opad is the byte 0x5c repeated 64 times
259          * and text is the data being protected
260          */
261 
262         /* start out by storing key in pads */
263         bzero( k_ipad, sizeof k_ipad);
264         bzero( k_opad, sizeof k_opad);
265         bcopy( key, k_ipad, key_len);
266         bcopy( key, k_opad, key_len);
267 
268         /* XOR key with ipad and opad values */
269         for (i=0; i<64; i++) {
270                 k_ipad[i] ^= 0x36;
271                 k_opad[i] ^= 0x5c;
272         }
273         /*
274          * perform inner MD5
275          */
276         MD5_Init(&context);                   /* init context for 1st
277                                               * pass */
278         MD5_Update(&context, k_ipad, 64);     /* start with inner pad */
279         MD5_Update(&context, text, text_len); /* then text of datagram */
280         MD5_Final(digest, &context);          /* finish up 1st pass */
281         /*
282          * perform outer MD5
283          */
284         MD5_Init(&context);                   /* init context for 2nd
285                                               * pass */
286         MD5_Update(&context, k_opad, 64);     /* start with outer pad */
287         MD5_Update(&context, digest, 16);     /* then results of 1st
288                                               * hash */
289         MD5_Final(digest, &context);          /* finish up 2nd pass */
290 }
291 
292 /*
293  * CRAM-MD5 authentication
294  */
295 int
296 smtp_auth_md5(int fd, char *login, char *password)
297 {
298 	unsigned char digest[BUF_SIZE];
299 	char buffer[BUF_SIZE], ascii_digest[33];
300 	char *temp;
301 	int len, i;
302 	static char hextab[] = "0123456789abcdef";
303 
304 	temp = calloc(BUF_SIZE, 1);
305 	memset(buffer, 0, sizeof(buffer));
306 	memset(digest, 0, sizeof(digest));
307 	memset(ascii_digest, 0, sizeof(ascii_digest));
308 
309 	/* Send AUTH command according to RFC 2554 */
310 	send_remote_command(fd, "AUTH CRAM-MD5");
311 	if (read_remote(fd, sizeof(buffer), buffer) != 3) {
312 		syslog(LOG_DEBUG, "smarthost authentication:"
313 		       " AUTH cram-md5 not available: %s", neterr);
314 		/* if cram-md5 is not available */
315 		free(temp);
316 		return (-1);
317 	}
318 
319 	/* skip 3 char status + 1 char space */
320 	base64_decode(buffer + 4, temp);
321 	hmac_md5((unsigned char *)temp, strlen(temp),
322 		 (unsigned char *)password, strlen(password), digest);
323 	free(temp);
324 
325 	ascii_digest[32] = 0;
326 	for (i = 0; i < 16; i++) {
327 		ascii_digest[2*i] = hextab[digest[i] >> 4];
328 		ascii_digest[2*i+1] = hextab[digest[i] & 15];
329 	}
330 
331 	/* prepare answer */
332 	snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
333 
334 	/* encode answer */
335 	len = base64_encode(buffer, strlen(buffer), &temp);
336 	if (len < 0) {
337 		syslog(LOG_ERR, "can not encode auth reply: %m");
338 		return (-1);
339 	}
340 
341 	/* send answer */
342 	send_remote_command(fd, "%s", temp);
343 	free(temp);
344 	if (read_remote(fd, 0, NULL) != 2) {
345 		syslog(LOG_WARNING, "remote delivery deferred:"
346 				" AUTH cram-md5 failed: %s", neterr);
347 		return (-2);
348 	}
349 
350 	return (0);
351 }
352