2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
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>
50 init_cert_file(SSL_CTX *ctx, const char *path)
54 /* Load certificate into ctx */
55 error = SSL_CTX_use_certificate_chain_file(ctx, path);
57 syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr());
61 /* Add private key to ctx */
62 error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
64 syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr());
69 * Check the consistency of a private key with the corresponding
72 error = SSL_CTX_check_private_key(ctx);
74 syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr());
82 verify_server_fingerprint(const X509 *cert)
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",
91 if(fingerprint_len != SHA256_DIGEST_LENGTH) {
92 syslog(LOG_WARNING, "sha256 fingerprint has unexpected length of %d bytes",
96 if(memcmp(fingerprint, config.fingerprint, SHA256_DIGEST_LENGTH) != 0) {
97 syslog(LOG_WARNING, "fingerprints do not match");
100 syslog(LOG_DEBUG, "successfully verified server certificate fingerprint");
105 smtp_init_crypto(int fd, int feature, struct smtp_features* features)
108 #if (OPENSSL_VERSION_NUMBER >= 0x00909000L)
109 const SSL_METHOD *meth = NULL;
111 SSL_METHOD *meth = NULL;
116 /* XXX clean up on error/close */
117 /* Init SSL library */
118 OPENSSL_init_ssl(0, NULL);
120 // Allow any possible version
121 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
122 meth = TLS_client_method();
124 meth = SSLv23_client_method();
127 ctx = SSL_CTX_new(meth);
129 syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
133 /* User supplied a certificate */
134 if (config.certfile != NULL) {
135 error = init_cert_file(ctx, config.certfile);
137 syslog(LOG_WARNING, "remote delivery deferred");
143 * If the user wants STARTTLS, we have to send EHLO here
145 if (((feature & SECURETRANSFER) != 0) &&
146 (feature & STARTTLS) != 0) {
147 /* TLS init phase, disable SSL_write */
148 config.features |= NOSSL;
150 if (perform_server_greeting(fd, features) == 0) {
151 send_remote_command(fd, "STARTTLS");
152 if (read_remote(fd, 0, NULL) != 2) {
153 if ((feature & TLS_OPP) == 0) {
154 syslog(LOG_ERR, "remote delivery deferred: STARTTLS not available: %s", neterr);
157 syslog(LOG_INFO, "in opportunistic TLS mode, STARTTLS not available: %s", neterr);
162 syslog(LOG_ERR, "remote delivery deferred: could not perform server greeting: %s",
167 /* End of TLS init phase, enable SSL_write/read */
168 config.features &= ~NOSSL;
171 config.ssl = SSL_new(ctx);
172 if (config.ssl == NULL) {
173 syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s",
178 /* Set ssl to work in client mode */
179 SSL_set_connect_state(config.ssl);
181 /* Set fd for SSL in/output */
182 error = SSL_set_fd(config.ssl, fd);
184 syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s",
189 /* Open SSL connection */
190 error = SSL_connect(config.ssl);
192 syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s",
197 /* Get peer certificate */
198 cert = SSL_get_peer_certificate(config.ssl);
200 syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s",
204 if(config.fingerprint != NULL && verify_server_fingerprint(cert)) {
214 * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk,
215 * M. Bellare and R. Canetti.
217 * text pointer to data stream
218 * text_len length of data stream
219 * key pointer to authentication key
220 * key_len length of authentication key
221 * digest caller digest to be filled int
224 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
225 unsigned char* digest)
228 unsigned char k_ipad[65]; /* inner padding -
231 unsigned char k_opad[65]; /* outer padding -
234 unsigned char tk[16];
236 /* if key is longer than 64 bytes reset it to key=MD5(key) */
242 MD5_Update(&tctx, key, key_len);
243 MD5_Final(tk, &tctx);
250 * the HMAC_MD5 transform looks like:
252 * MD5(K XOR opad, MD5(K XOR ipad, text))
254 * where K is an n byte key
255 * ipad is the byte 0x36 repeated 64 times
257 * opad is the byte 0x5c repeated 64 times
258 * and text is the data being protected
261 /* start out by storing key in pads */
262 bzero( k_ipad, sizeof k_ipad);
263 bzero( k_opad, sizeof k_opad);
264 bcopy( key, k_ipad, key_len);
265 bcopy( key, k_opad, key_len);
267 /* XOR key with ipad and opad values */
268 for (i=0; i<64; i++) {
275 MD5_Init(&context); /* init context for 1st
277 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
278 MD5_Update(&context, text, text_len); /* then text of datagram */
279 MD5_Final(digest, &context); /* finish up 1st pass */
283 MD5_Init(&context); /* init context for 2nd
285 MD5_Update(&context, k_opad, 64); /* start with outer pad */
286 MD5_Update(&context, digest, 16); /* then results of 1st
288 MD5_Final(digest, &context); /* finish up 2nd pass */
292 * CRAM-MD5 authentication
295 smtp_auth_md5(int fd, char *login, char *password)
297 unsigned char digest[BUF_SIZE];
298 char buffer[BUF_SIZE], ascii_digest[33];
301 static char hextab[] = "0123456789abcdef";
303 temp = calloc(BUF_SIZE, 1);
304 memset(buffer, 0, sizeof(buffer));
305 memset(digest, 0, sizeof(digest));
306 memset(ascii_digest, 0, sizeof(ascii_digest));
308 /* Send AUTH command according to RFC 2554 */
309 send_remote_command(fd, "AUTH CRAM-MD5");
310 if (read_remote(fd, sizeof(buffer), buffer) != 3) {
311 syslog(LOG_DEBUG, "smarthost authentication:"
312 " AUTH cram-md5 not available: %s", neterr);
313 /* if cram-md5 is not available */
318 /* skip 3 char status + 1 char space */
319 base64_decode(buffer + 4, temp);
320 hmac_md5((unsigned char *)temp, strlen(temp),
321 (unsigned char *)password, strlen(password), digest);
324 ascii_digest[32] = 0;
325 for (i = 0; i < 16; i++) {
326 ascii_digest[2*i] = hextab[digest[i] >> 4];
327 ascii_digest[2*i+1] = hextab[digest[i] & 15];
331 snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
334 len = base64_encode(buffer, strlen(buffer), &temp);
336 syslog(LOG_ERR, "can not encode auth reply: %m");
341 send_remote_command(fd, "%s", temp);
343 if (read_remote(fd, 0, NULL) != 2) {
344 syslog(LOG_WARNING, "remote delivery deferred:"
345 " AUTH cram-md5 failed: %s", neterr);