xref: /freebsd/contrib/dma/crypto.c (revision 692b19255c5d53a5f42ea49ad569284316315577)
1a9e8641dSBaptiste Daroussin /*
2a9e8641dSBaptiste Daroussin  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3a9e8641dSBaptiste Daroussin  *
4a9e8641dSBaptiste Daroussin  * This code is derived from software contributed to The DragonFly Project
5a9e8641dSBaptiste Daroussin  * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
6a9e8641dSBaptiste Daroussin  * Germany.
7a9e8641dSBaptiste Daroussin  *
8a9e8641dSBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
9a9e8641dSBaptiste Daroussin  * modification, are permitted provided that the following conditions
10a9e8641dSBaptiste Daroussin  * are met:
11a9e8641dSBaptiste Daroussin  *
12a9e8641dSBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
13a9e8641dSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
14a9e8641dSBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
15a9e8641dSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in
16a9e8641dSBaptiste Daroussin  *    the documentation and/or other materials provided with the
17a9e8641dSBaptiste Daroussin  *    distribution.
18a9e8641dSBaptiste Daroussin  * 3. Neither the name of The DragonFly Project nor the names of its
19a9e8641dSBaptiste Daroussin  *    contributors may be used to endorse or promote products derived
20a9e8641dSBaptiste Daroussin  *    from this software without specific, prior written permission.
21a9e8641dSBaptiste Daroussin  *
22a9e8641dSBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23a9e8641dSBaptiste Daroussin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24a9e8641dSBaptiste Daroussin  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25a9e8641dSBaptiste Daroussin  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26a9e8641dSBaptiste Daroussin  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27a9e8641dSBaptiste Daroussin  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28a9e8641dSBaptiste Daroussin  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29a9e8641dSBaptiste Daroussin  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30a9e8641dSBaptiste Daroussin  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31a9e8641dSBaptiste Daroussin  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32a9e8641dSBaptiste Daroussin  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33a9e8641dSBaptiste Daroussin  * SUCH DAMAGE.
34a9e8641dSBaptiste Daroussin  */
35a9e8641dSBaptiste Daroussin 
36a9e8641dSBaptiste Daroussin #include <openssl/x509.h>
37a9e8641dSBaptiste Daroussin #include <openssl/md5.h>
38a9e8641dSBaptiste Daroussin #include <openssl/ssl.h>
39a9e8641dSBaptiste Daroussin #include <openssl/err.h>
40a9e8641dSBaptiste Daroussin #include <openssl/pem.h>
41a9e8641dSBaptiste Daroussin #include <openssl/rand.h>
42a9e8641dSBaptiste Daroussin 
43fbe95b88SBaptiste Daroussin #include <strings.h>
44b86d1398SJung-uk Kim #include <string.h>
45a9e8641dSBaptiste Daroussin #include <syslog.h>
46a9e8641dSBaptiste Daroussin 
47a9e8641dSBaptiste Daroussin #include "dma.h"
48a9e8641dSBaptiste Daroussin 
49a9e8641dSBaptiste Daroussin static int
init_cert_file(SSL_CTX * ctx,const char * path)50a9e8641dSBaptiste Daroussin init_cert_file(SSL_CTX *ctx, const char *path)
51a9e8641dSBaptiste Daroussin {
52a9e8641dSBaptiste Daroussin 	int error;
53a9e8641dSBaptiste Daroussin 
54a9e8641dSBaptiste Daroussin 	/* Load certificate into ctx */
55a9e8641dSBaptiste Daroussin 	error = SSL_CTX_use_certificate_chain_file(ctx, path);
56a9e8641dSBaptiste Daroussin 	if (error < 1) {
57a9e8641dSBaptiste Daroussin 		syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr());
58a9e8641dSBaptiste Daroussin 		return (-1);
59a9e8641dSBaptiste Daroussin 	}
60a9e8641dSBaptiste Daroussin 
61a9e8641dSBaptiste Daroussin 	/* Add private key to ctx */
62a9e8641dSBaptiste Daroussin 	error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
63a9e8641dSBaptiste Daroussin 	if (error < 1) {
64a9e8641dSBaptiste Daroussin 		syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr());
65a9e8641dSBaptiste Daroussin 		return (-1);
66a9e8641dSBaptiste Daroussin 	}
67a9e8641dSBaptiste Daroussin 
68a9e8641dSBaptiste Daroussin 	/*
69a9e8641dSBaptiste Daroussin 	 * Check the consistency of a private key with the corresponding
70a9e8641dSBaptiste Daroussin          * certificate
71a9e8641dSBaptiste Daroussin 	 */
72a9e8641dSBaptiste Daroussin 	error = SSL_CTX_check_private_key(ctx);
73a9e8641dSBaptiste Daroussin 	if (error < 1) {
74a9e8641dSBaptiste Daroussin 		syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr());
75a9e8641dSBaptiste Daroussin 		return (-1);
76a9e8641dSBaptiste Daroussin 	}
77a9e8641dSBaptiste Daroussin 
78a9e8641dSBaptiste Daroussin 	return (0);
79a9e8641dSBaptiste Daroussin }
80a9e8641dSBaptiste Daroussin 
81fbe95b88SBaptiste Daroussin static int
verify_server_fingerprint(const X509 * cert)82fbe95b88SBaptiste Daroussin verify_server_fingerprint(const X509 *cert)
83fbe95b88SBaptiste Daroussin {
84fbe95b88SBaptiste Daroussin 	unsigned char fingerprint[EVP_MAX_MD_SIZE] = {0};
85fbe95b88SBaptiste Daroussin 	unsigned int fingerprint_len = 0;
86fbe95b88SBaptiste Daroussin 	if(!X509_digest(cert, EVP_sha256(), fingerprint, &fingerprint_len)) {
87fbe95b88SBaptiste Daroussin 		syslog(LOG_WARNING, "failed to load fingerprint of server certicate: %s",
88fbe95b88SBaptiste Daroussin 			   ssl_errstr());
89fbe95b88SBaptiste Daroussin 		return (1);
90fbe95b88SBaptiste Daroussin 	}
91fbe95b88SBaptiste Daroussin 	if(fingerprint_len != SHA256_DIGEST_LENGTH) {
92fbe95b88SBaptiste Daroussin 		syslog(LOG_WARNING, "sha256 fingerprint has unexpected length of %d bytes",
93fbe95b88SBaptiste Daroussin 		       fingerprint_len);
94fbe95b88SBaptiste Daroussin 		return (1);
95fbe95b88SBaptiste Daroussin 	}
96fbe95b88SBaptiste Daroussin 	if(memcmp(fingerprint, config.fingerprint, SHA256_DIGEST_LENGTH) != 0) {
97fbe95b88SBaptiste Daroussin 		syslog(LOG_WARNING, "fingerprints do not match");
98fbe95b88SBaptiste Daroussin 		return (1);
99fbe95b88SBaptiste Daroussin 	}
100fbe95b88SBaptiste Daroussin 	syslog(LOG_DEBUG, "successfully verified server certificate fingerprint");
101fbe95b88SBaptiste Daroussin 	return (0);
102fbe95b88SBaptiste Daroussin }
103fbe95b88SBaptiste Daroussin 
104a9e8641dSBaptiste Daroussin int
smtp_init_crypto(int fd,int feature,struct smtp_features * features)105fbe95b88SBaptiste Daroussin smtp_init_crypto(int fd, int feature, struct smtp_features* features)
106a9e8641dSBaptiste Daroussin {
107a9e8641dSBaptiste Daroussin 	SSL_CTX *ctx = NULL;
108a9e8641dSBaptiste Daroussin #if (OPENSSL_VERSION_NUMBER >= 0x00909000L)
109a9e8641dSBaptiste Daroussin 	const SSL_METHOD *meth = NULL;
110a9e8641dSBaptiste Daroussin #else
111a9e8641dSBaptiste Daroussin 	SSL_METHOD *meth = NULL;
112a9e8641dSBaptiste Daroussin #endif
113a9e8641dSBaptiste Daroussin 	X509 *cert;
114a9e8641dSBaptiste Daroussin 	int error;
115a9e8641dSBaptiste Daroussin 
116a9e8641dSBaptiste Daroussin 	/* XXX clean up on error/close */
117a9e8641dSBaptiste Daroussin 	/* Init SSL library */
118*692b1925SEd Maste 	OPENSSL_init_ssl(0, NULL);
119a9e8641dSBaptiste Daroussin 
120b86d1398SJung-uk Kim 	// Allow any possible version
121b86d1398SJung-uk Kim #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
122b86d1398SJung-uk Kim 	meth = TLS_client_method();
123b86d1398SJung-uk Kim #else
124b86d1398SJung-uk Kim 	meth = SSLv23_client_method();
125b86d1398SJung-uk Kim #endif
126a9e8641dSBaptiste Daroussin 
127a9e8641dSBaptiste Daroussin 	ctx = SSL_CTX_new(meth);
128a9e8641dSBaptiste Daroussin 	if (ctx == NULL) {
129a9e8641dSBaptiste Daroussin 		syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
130a9e8641dSBaptiste Daroussin 		return (1);
131a9e8641dSBaptiste Daroussin 	}
132a9e8641dSBaptiste Daroussin 
133a9e8641dSBaptiste Daroussin 	/* User supplied a certificate */
134a9e8641dSBaptiste Daroussin 	if (config.certfile != NULL) {
135a9e8641dSBaptiste Daroussin 		error = init_cert_file(ctx, config.certfile);
136a9e8641dSBaptiste Daroussin 		if (error) {
137a9e8641dSBaptiste Daroussin 			syslog(LOG_WARNING, "remote delivery deferred");
138a9e8641dSBaptiste Daroussin 			return (1);
139a9e8641dSBaptiste Daroussin 		}
140a9e8641dSBaptiste Daroussin 	}
141a9e8641dSBaptiste Daroussin 
142a9e8641dSBaptiste Daroussin 	/*
143a9e8641dSBaptiste Daroussin 	 * If the user wants STARTTLS, we have to send EHLO here
144a9e8641dSBaptiste Daroussin 	 */
145fbe95b88SBaptiste Daroussin 	if (((feature & SECURETRANSFER) != 0) &&
146a9e8641dSBaptiste Daroussin 	     (feature & STARTTLS) != 0) {
147a9e8641dSBaptiste Daroussin 		/* TLS init phase, disable SSL_write */
148a9e8641dSBaptiste Daroussin 		config.features |= NOSSL;
149a9e8641dSBaptiste Daroussin 
150fbe95b88SBaptiste Daroussin 		if (perform_server_greeting(fd, features) == 0) {
151a9e8641dSBaptiste Daroussin 			send_remote_command(fd, "STARTTLS");
152a9e8641dSBaptiste Daroussin 			if (read_remote(fd, 0, NULL) != 2) {
153a9e8641dSBaptiste Daroussin 				if ((feature & TLS_OPP) == 0) {
154a9e8641dSBaptiste Daroussin 					syslog(LOG_ERR, "remote delivery deferred: STARTTLS not available: %s", neterr);
155a9e8641dSBaptiste Daroussin 					return (1);
156a9e8641dSBaptiste Daroussin 				} else {
157a9e8641dSBaptiste Daroussin 					syslog(LOG_INFO, "in opportunistic TLS mode, STARTTLS not available: %s", neterr);
158a9e8641dSBaptiste Daroussin 					return (0);
159a9e8641dSBaptiste Daroussin 				}
160a9e8641dSBaptiste Daroussin 			}
161fbe95b88SBaptiste Daroussin 		} else {
162fbe95b88SBaptiste Daroussin 			syslog(LOG_ERR, "remote delivery deferred: could not perform server greeting: %s",
163fbe95b88SBaptiste Daroussin 				neterr);
164fbe95b88SBaptiste Daroussin 			return (1);
165a9e8641dSBaptiste Daroussin 		}
166fbe95b88SBaptiste Daroussin 
167a9e8641dSBaptiste Daroussin 		/* End of TLS init phase, enable SSL_write/read */
168a9e8641dSBaptiste Daroussin 		config.features &= ~NOSSL;
169a9e8641dSBaptiste Daroussin 	}
170a9e8641dSBaptiste Daroussin 
171a9e8641dSBaptiste Daroussin 	config.ssl = SSL_new(ctx);
172a9e8641dSBaptiste Daroussin 	if (config.ssl == NULL) {
173a9e8641dSBaptiste Daroussin 		syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s",
174a9e8641dSBaptiste Daroussin 		       ssl_errstr());
175a9e8641dSBaptiste Daroussin 		return (1);
176a9e8641dSBaptiste Daroussin 	}
177a9e8641dSBaptiste Daroussin 
178a9e8641dSBaptiste Daroussin 	/* Set ssl to work in client mode */
179a9e8641dSBaptiste Daroussin 	SSL_set_connect_state(config.ssl);
180a9e8641dSBaptiste Daroussin 
181a9e8641dSBaptiste Daroussin 	/* Set fd for SSL in/output */
182a9e8641dSBaptiste Daroussin 	error = SSL_set_fd(config.ssl, fd);
183a9e8641dSBaptiste Daroussin 	if (error == 0) {
184a9e8641dSBaptiste Daroussin 		syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s",
185a9e8641dSBaptiste Daroussin 		       ssl_errstr());
186a9e8641dSBaptiste Daroussin 		return (1);
187a9e8641dSBaptiste Daroussin 	}
188a9e8641dSBaptiste Daroussin 
189a9e8641dSBaptiste Daroussin 	/* Open SSL connection */
190a9e8641dSBaptiste Daroussin 	error = SSL_connect(config.ssl);
191fbe95b88SBaptiste Daroussin 	if (error != 1) {
192a9e8641dSBaptiste Daroussin 		syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s",
193a9e8641dSBaptiste Daroussin 		       ssl_errstr());
194a9e8641dSBaptiste Daroussin 		return (1);
195a9e8641dSBaptiste Daroussin 	}
196a9e8641dSBaptiste Daroussin 
197a9e8641dSBaptiste Daroussin 	/* Get peer certificate */
198a9e8641dSBaptiste Daroussin 	cert = SSL_get_peer_certificate(config.ssl);
199a9e8641dSBaptiste Daroussin 	if (cert == NULL) {
200a9e8641dSBaptiste Daroussin 		syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s",
201a9e8641dSBaptiste Daroussin 		       ssl_errstr());
202fbe95b88SBaptiste Daroussin 		return (1);
203fbe95b88SBaptiste Daroussin 	}
204fbe95b88SBaptiste Daroussin 	if(config.fingerprint != NULL && verify_server_fingerprint(cert)) {
205fbe95b88SBaptiste Daroussin 		X509_free(cert);
206fbe95b88SBaptiste Daroussin 		return (1);
207a9e8641dSBaptiste Daroussin 	}
208a9e8641dSBaptiste Daroussin 	X509_free(cert);
209a9e8641dSBaptiste Daroussin 
210a9e8641dSBaptiste Daroussin 	return (0);
211a9e8641dSBaptiste Daroussin }
212a9e8641dSBaptiste Daroussin 
213a9e8641dSBaptiste Daroussin /*
214a9e8641dSBaptiste Daroussin  * hmac_md5() taken out of RFC 2104.  This RFC was written by H. Krawczyk,
215a9e8641dSBaptiste Daroussin  * M. Bellare and R. Canetti.
216a9e8641dSBaptiste Daroussin  *
217a9e8641dSBaptiste Daroussin  * text      pointer to data stream
218a9e8641dSBaptiste Daroussin  * text_len  length of data stream
219a9e8641dSBaptiste Daroussin  * key       pointer to authentication key
220a9e8641dSBaptiste Daroussin  * key_len   length of authentication key
221a9e8641dSBaptiste Daroussin  * digest    caller digest to be filled int
222a9e8641dSBaptiste Daroussin  */
223a9e8641dSBaptiste Daroussin void
hmac_md5(unsigned char * text,int text_len,unsigned char * key,int key_len,unsigned char * digest)224a9e8641dSBaptiste Daroussin hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
225a9e8641dSBaptiste Daroussin     unsigned char* digest)
226a9e8641dSBaptiste Daroussin {
227a9e8641dSBaptiste Daroussin         MD5_CTX context;
228a9e8641dSBaptiste Daroussin         unsigned char k_ipad[65];    /* inner padding -
229a9e8641dSBaptiste Daroussin                                       * key XORd with ipad
230a9e8641dSBaptiste Daroussin                                       */
231a9e8641dSBaptiste Daroussin         unsigned char k_opad[65];    /* outer padding -
232a9e8641dSBaptiste Daroussin                                       * key XORd with opad
233a9e8641dSBaptiste Daroussin                                       */
234a9e8641dSBaptiste Daroussin         unsigned char tk[16];
235a9e8641dSBaptiste Daroussin         int i;
236a9e8641dSBaptiste Daroussin         /* if key is longer than 64 bytes reset it to key=MD5(key) */
237a9e8641dSBaptiste Daroussin         if (key_len > 64) {
238a9e8641dSBaptiste Daroussin 
239a9e8641dSBaptiste Daroussin                 MD5_CTX      tctx;
240a9e8641dSBaptiste Daroussin 
241a9e8641dSBaptiste Daroussin                 MD5_Init(&tctx);
242a9e8641dSBaptiste Daroussin                 MD5_Update(&tctx, key, key_len);
243a9e8641dSBaptiste Daroussin                 MD5_Final(tk, &tctx);
244a9e8641dSBaptiste Daroussin 
245a9e8641dSBaptiste Daroussin                 key = tk;
246a9e8641dSBaptiste Daroussin                 key_len = 16;
247a9e8641dSBaptiste Daroussin         }
248a9e8641dSBaptiste Daroussin 
249a9e8641dSBaptiste Daroussin         /*
250a9e8641dSBaptiste Daroussin          * the HMAC_MD5 transform looks like:
251a9e8641dSBaptiste Daroussin          *
252a9e8641dSBaptiste Daroussin          * MD5(K XOR opad, MD5(K XOR ipad, text))
253a9e8641dSBaptiste Daroussin          *
254a9e8641dSBaptiste Daroussin          * where K is an n byte key
255a9e8641dSBaptiste Daroussin          * ipad is the byte 0x36 repeated 64 times
256a9e8641dSBaptiste Daroussin 	 *
257a9e8641dSBaptiste Daroussin          * opad is the byte 0x5c repeated 64 times
258a9e8641dSBaptiste Daroussin          * and text is the data being protected
259a9e8641dSBaptiste Daroussin          */
260a9e8641dSBaptiste Daroussin 
261a9e8641dSBaptiste Daroussin         /* start out by storing key in pads */
262a9e8641dSBaptiste Daroussin         bzero( k_ipad, sizeof k_ipad);
263a9e8641dSBaptiste Daroussin         bzero( k_opad, sizeof k_opad);
264a9e8641dSBaptiste Daroussin         bcopy( key, k_ipad, key_len);
265a9e8641dSBaptiste Daroussin         bcopy( key, k_opad, key_len);
266a9e8641dSBaptiste Daroussin 
267a9e8641dSBaptiste Daroussin         /* XOR key with ipad and opad values */
268a9e8641dSBaptiste Daroussin         for (i=0; i<64; i++) {
269a9e8641dSBaptiste Daroussin                 k_ipad[i] ^= 0x36;
270a9e8641dSBaptiste Daroussin                 k_opad[i] ^= 0x5c;
271a9e8641dSBaptiste Daroussin         }
272a9e8641dSBaptiste Daroussin         /*
273a9e8641dSBaptiste Daroussin          * perform inner MD5
274a9e8641dSBaptiste Daroussin          */
275a9e8641dSBaptiste Daroussin         MD5_Init(&context);                   /* init context for 1st
276a9e8641dSBaptiste Daroussin                                               * pass */
277a9e8641dSBaptiste Daroussin         MD5_Update(&context, k_ipad, 64);     /* start with inner pad */
278a9e8641dSBaptiste Daroussin         MD5_Update(&context, text, text_len); /* then text of datagram */
279a9e8641dSBaptiste Daroussin         MD5_Final(digest, &context);          /* finish up 1st pass */
280a9e8641dSBaptiste Daroussin         /*
281a9e8641dSBaptiste Daroussin          * perform outer MD5
282a9e8641dSBaptiste Daroussin          */
283a9e8641dSBaptiste Daroussin         MD5_Init(&context);                   /* init context for 2nd
284a9e8641dSBaptiste Daroussin                                               * pass */
285a9e8641dSBaptiste Daroussin         MD5_Update(&context, k_opad, 64);     /* start with outer pad */
286a9e8641dSBaptiste Daroussin         MD5_Update(&context, digest, 16);     /* then results of 1st
287a9e8641dSBaptiste Daroussin                                               * hash */
288a9e8641dSBaptiste Daroussin         MD5_Final(digest, &context);          /* finish up 2nd pass */
289a9e8641dSBaptiste Daroussin }
290a9e8641dSBaptiste Daroussin 
291a9e8641dSBaptiste Daroussin /*
292a9e8641dSBaptiste Daroussin  * CRAM-MD5 authentication
293a9e8641dSBaptiste Daroussin  */
294a9e8641dSBaptiste Daroussin int
smtp_auth_md5(int fd,char * login,char * password)295a9e8641dSBaptiste Daroussin smtp_auth_md5(int fd, char *login, char *password)
296a9e8641dSBaptiste Daroussin {
297a9e8641dSBaptiste Daroussin 	unsigned char digest[BUF_SIZE];
298a9e8641dSBaptiste Daroussin 	char buffer[BUF_SIZE], ascii_digest[33];
299a9e8641dSBaptiste Daroussin 	char *temp;
300a9e8641dSBaptiste Daroussin 	int len, i;
301a9e8641dSBaptiste Daroussin 	static char hextab[] = "0123456789abcdef";
302a9e8641dSBaptiste Daroussin 
303a9e8641dSBaptiste Daroussin 	temp = calloc(BUF_SIZE, 1);
304a9e8641dSBaptiste Daroussin 	memset(buffer, 0, sizeof(buffer));
305a9e8641dSBaptiste Daroussin 	memset(digest, 0, sizeof(digest));
306a9e8641dSBaptiste Daroussin 	memset(ascii_digest, 0, sizeof(ascii_digest));
307a9e8641dSBaptiste Daroussin 
308a9e8641dSBaptiste Daroussin 	/* Send AUTH command according to RFC 2554 */
309a9e8641dSBaptiste Daroussin 	send_remote_command(fd, "AUTH CRAM-MD5");
310a9e8641dSBaptiste Daroussin 	if (read_remote(fd, sizeof(buffer), buffer) != 3) {
311a9e8641dSBaptiste Daroussin 		syslog(LOG_DEBUG, "smarthost authentication:"
312a9e8641dSBaptiste Daroussin 		       " AUTH cram-md5 not available: %s", neterr);
313a9e8641dSBaptiste Daroussin 		/* if cram-md5 is not available */
314a9e8641dSBaptiste Daroussin 		free(temp);
315a9e8641dSBaptiste Daroussin 		return (-1);
316a9e8641dSBaptiste Daroussin 	}
317a9e8641dSBaptiste Daroussin 
318a9e8641dSBaptiste Daroussin 	/* skip 3 char status + 1 char space */
319a9e8641dSBaptiste Daroussin 	base64_decode(buffer + 4, temp);
320a9e8641dSBaptiste Daroussin 	hmac_md5((unsigned char *)temp, strlen(temp),
321a9e8641dSBaptiste Daroussin 		 (unsigned char *)password, strlen(password), digest);
322a9e8641dSBaptiste Daroussin 	free(temp);
323a9e8641dSBaptiste Daroussin 
324a9e8641dSBaptiste Daroussin 	ascii_digest[32] = 0;
325a9e8641dSBaptiste Daroussin 	for (i = 0; i < 16; i++) {
326a9e8641dSBaptiste Daroussin 		ascii_digest[2*i] = hextab[digest[i] >> 4];
327a9e8641dSBaptiste Daroussin 		ascii_digest[2*i+1] = hextab[digest[i] & 15];
328a9e8641dSBaptiste Daroussin 	}
329a9e8641dSBaptiste Daroussin 
330a9e8641dSBaptiste Daroussin 	/* prepare answer */
331a9e8641dSBaptiste Daroussin 	snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
332a9e8641dSBaptiste Daroussin 
333a9e8641dSBaptiste Daroussin 	/* encode answer */
334a9e8641dSBaptiste Daroussin 	len = base64_encode(buffer, strlen(buffer), &temp);
335a9e8641dSBaptiste Daroussin 	if (len < 0) {
336a9e8641dSBaptiste Daroussin 		syslog(LOG_ERR, "can not encode auth reply: %m");
337a9e8641dSBaptiste Daroussin 		return (-1);
338a9e8641dSBaptiste Daroussin 	}
339a9e8641dSBaptiste Daroussin 
340a9e8641dSBaptiste Daroussin 	/* send answer */
341a9e8641dSBaptiste Daroussin 	send_remote_command(fd, "%s", temp);
342a9e8641dSBaptiste Daroussin 	free(temp);
343a9e8641dSBaptiste Daroussin 	if (read_remote(fd, 0, NULL) != 2) {
344a9e8641dSBaptiste Daroussin 		syslog(LOG_WARNING, "remote delivery deferred:"
345a9e8641dSBaptiste Daroussin 				" AUTH cram-md5 failed: %s", neterr);
346a9e8641dSBaptiste Daroussin 		return (-2);
347a9e8641dSBaptiste Daroussin 	}
348a9e8641dSBaptiste Daroussin 
349a9e8641dSBaptiste Daroussin 	return (0);
350a9e8641dSBaptiste Daroussin }
351