xref: /dragonfly/libexec/dma/crypto.c (revision 577b958f)
1f67bedddSMatthias Schmidt /*
2f67bedddSMatthias Schmidt  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3f67bedddSMatthias Schmidt  *
4f67bedddSMatthias Schmidt  * This code is derived from software contributed to The DragonFly Project
5f67bedddSMatthias Schmidt  * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
6f67bedddSMatthias Schmidt  * Germany.
7f67bedddSMatthias Schmidt  *
8f67bedddSMatthias Schmidt  * Redistribution and use in source and binary forms, with or without
9f67bedddSMatthias Schmidt  * modification, are permitted provided that the following conditions
10f67bedddSMatthias Schmidt  * are met:
11f67bedddSMatthias Schmidt  *
12f67bedddSMatthias Schmidt  * 1. Redistributions of source code must retain the above copyright
13f67bedddSMatthias Schmidt  *    notice, this list of conditions and the following disclaimer.
14f67bedddSMatthias Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
15f67bedddSMatthias Schmidt  *    notice, this list of conditions and the following disclaimer in
16f67bedddSMatthias Schmidt  *    the documentation and/or other materials provided with the
17f67bedddSMatthias Schmidt  *    distribution.
18f67bedddSMatthias Schmidt  * 3. Neither the name of The DragonFly Project nor the names of its
19f67bedddSMatthias Schmidt  *    contributors may be used to endorse or promote products derived
20f67bedddSMatthias Schmidt  *    from this software without specific, prior written permission.
21f67bedddSMatthias Schmidt  *
22f67bedddSMatthias Schmidt  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f67bedddSMatthias Schmidt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24f67bedddSMatthias Schmidt  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25f67bedddSMatthias Schmidt  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26f67bedddSMatthias Schmidt  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27f67bedddSMatthias Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28f67bedddSMatthias Schmidt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29f67bedddSMatthias Schmidt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30f67bedddSMatthias Schmidt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31f67bedddSMatthias Schmidt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32f67bedddSMatthias Schmidt  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f67bedddSMatthias Schmidt  * SUCH DAMAGE.
34f67bedddSMatthias Schmidt  */
35f67bedddSMatthias Schmidt 
36f67bedddSMatthias Schmidt #include <openssl/x509.h>
377b68d8aeSMatthias Schmidt #include <openssl/md5.h>
38f67bedddSMatthias Schmidt #include <openssl/ssl.h>
39f67bedddSMatthias Schmidt #include <openssl/err.h>
40f67bedddSMatthias Schmidt #include <openssl/pem.h>
41f67bedddSMatthias Schmidt #include <openssl/rand.h>
42f67bedddSMatthias Schmidt 
4392fe556dSDaniel Fojt #include <strings.h>
4492fe556dSDaniel Fojt #include <string.h>
45f67bedddSMatthias Schmidt #include <syslog.h>
46f67bedddSMatthias Schmidt 
47f67bedddSMatthias Schmidt #include "dma.h"
48f67bedddSMatthias Schmidt 
49f67bedddSMatthias Schmidt static int
init_cert_file(SSL_CTX * ctx,const char * path)50405f48eeSSimon Schubert init_cert_file(SSL_CTX *ctx, const char *path)
51f67bedddSMatthias Schmidt {
52f67bedddSMatthias Schmidt 	int error;
53f67bedddSMatthias Schmidt 
54f67bedddSMatthias Schmidt 	/* Load certificate into ctx */
55f67bedddSMatthias Schmidt 	error = SSL_CTX_use_certificate_chain_file(ctx, path);
56f67bedddSMatthias Schmidt 	if (error < 1) {
57405f48eeSSimon Schubert 		syslog(LOG_ERR, "SSL: Cannot load certificate `%s': %s", path, ssl_errstr());
58f67bedddSMatthias Schmidt 		return (-1);
59f67bedddSMatthias Schmidt 	}
60f67bedddSMatthias Schmidt 
61f67bedddSMatthias Schmidt 	/* Add private key to ctx */
62f67bedddSMatthias Schmidt 	error = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
63f67bedddSMatthias Schmidt 	if (error < 1) {
64405f48eeSSimon Schubert 		syslog(LOG_ERR, "SSL: Cannot load private key `%s': %s", path, ssl_errstr());
65f67bedddSMatthias Schmidt 		return (-1);
66f67bedddSMatthias Schmidt 	}
67f67bedddSMatthias Schmidt 
68f67bedddSMatthias Schmidt 	/*
69f67bedddSMatthias Schmidt 	 * Check the consistency of a private key with the corresponding
70f67bedddSMatthias Schmidt          * certificate
71f67bedddSMatthias Schmidt 	 */
72f67bedddSMatthias Schmidt 	error = SSL_CTX_check_private_key(ctx);
73f67bedddSMatthias Schmidt 	if (error < 1) {
74405f48eeSSimon Schubert 		syslog(LOG_ERR, "SSL: Cannot check private key: %s", ssl_errstr());
75f67bedddSMatthias Schmidt 		return (-1);
76f67bedddSMatthias Schmidt 	}
77f67bedddSMatthias Schmidt 
78f67bedddSMatthias Schmidt 	return (0);
79f67bedddSMatthias Schmidt }
80f67bedddSMatthias Schmidt 
81*577b958fSDaniel Fojt static int
verify_server_fingerprint(const X509 * cert)8292fe556dSDaniel Fojt verify_server_fingerprint(const X509 *cert)
8392fe556dSDaniel Fojt {
8492fe556dSDaniel Fojt 	unsigned char fingerprint[EVP_MAX_MD_SIZE] = {0};
8592fe556dSDaniel Fojt 	unsigned int fingerprint_len = 0;
8692fe556dSDaniel Fojt 	if(!X509_digest(cert, EVP_sha256(), fingerprint, &fingerprint_len)) {
8792fe556dSDaniel Fojt 		syslog(LOG_WARNING, "failed to load fingerprint of server certicate: %s",
8892fe556dSDaniel Fojt 			   ssl_errstr());
8992fe556dSDaniel Fojt 		return (1);
9092fe556dSDaniel Fojt 	}
9192fe556dSDaniel Fojt 	if(fingerprint_len != SHA256_DIGEST_LENGTH) {
9292fe556dSDaniel Fojt 		syslog(LOG_WARNING, "sha256 fingerprint has unexpected length of %d bytes",
9392fe556dSDaniel Fojt 		       fingerprint_len);
9492fe556dSDaniel Fojt 		return (1);
9592fe556dSDaniel Fojt 	}
9692fe556dSDaniel Fojt 	if(memcmp(fingerprint, config.fingerprint, SHA256_DIGEST_LENGTH) != 0) {
9792fe556dSDaniel Fojt 		syslog(LOG_WARNING, "fingerprints do not match");
9892fe556dSDaniel Fojt 		return (1);
9992fe556dSDaniel Fojt 	}
10092fe556dSDaniel Fojt 	syslog(LOG_DEBUG, "successfully verified server certificate fingerprint");
10192fe556dSDaniel Fojt 	return (0);
10292fe556dSDaniel Fojt }
10392fe556dSDaniel Fojt 
10492fe556dSDaniel Fojt int
smtp_init_crypto(int fd,int feature,struct smtp_features * features)10592fe556dSDaniel Fojt smtp_init_crypto(int fd, int feature, struct smtp_features* features)
106f67bedddSMatthias Schmidt {
107f67bedddSMatthias Schmidt 	SSL_CTX *ctx = NULL;
10814dfb991SJoris Giovannangeli #if (OPENSSL_VERSION_NUMBER >= 0x00909000L)
10901185282SPeter Avalos 	const SSL_METHOD *meth = NULL;
11014dfb991SJoris Giovannangeli #else
11114dfb991SJoris Giovannangeli 	SSL_METHOD *meth = NULL;
11214dfb991SJoris Giovannangeli #endif
113f67bedddSMatthias Schmidt 	X509 *cert;
114f67bedddSMatthias Schmidt 	int error;
115f67bedddSMatthias Schmidt 
116ea921663SSimon Schubert 	/* XXX clean up on error/close */
117f67bedddSMatthias Schmidt 	/* Init SSL library */
118f67bedddSMatthias Schmidt 	SSL_library_init();
119e2c88018SSimon Schubert 	SSL_load_error_strings();
120f67bedddSMatthias Schmidt 
12192fe556dSDaniel Fojt 	// Allow any possible version
12292fe556dSDaniel Fojt #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
12392fe556dSDaniel Fojt 	meth = TLS_client_method();
12492fe556dSDaniel Fojt #else
12592fe556dSDaniel Fojt 	meth = SSLv23_client_method();
12692fe556dSDaniel Fojt #endif
127f67bedddSMatthias Schmidt 
128f67bedddSMatthias Schmidt 	ctx = SSL_CTX_new(meth);
129f67bedddSMatthias Schmidt 	if (ctx == NULL) {
130405f48eeSSimon Schubert 		syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
131e2c88018SSimon Schubert 		return (1);
132f67bedddSMatthias Schmidt 	}
133f67bedddSMatthias Schmidt 
134f67bedddSMatthias Schmidt 	/* User supplied a certificate */
135ca259d14SSimon Schubert 	if (config.certfile != NULL) {
136ca259d14SSimon Schubert 		error = init_cert_file(ctx, config.certfile);
137e2c88018SSimon Schubert 		if (error) {
138405f48eeSSimon Schubert 			syslog(LOG_WARNING, "remote delivery deferred");
139e2c88018SSimon Schubert 			return (1);
140e2c88018SSimon Schubert 		}
141e2c88018SSimon Schubert 	}
142f67bedddSMatthias Schmidt 
143f67bedddSMatthias Schmidt 	/*
144f67bedddSMatthias Schmidt 	 * If the user wants STARTTLS, we have to send EHLO here
145f67bedddSMatthias Schmidt 	 */
14692fe556dSDaniel Fojt 	if (((feature & SECURETRANSFER) != 0) &&
147f67bedddSMatthias Schmidt 	     (feature & STARTTLS) != 0) {
148f67bedddSMatthias Schmidt 		/* TLS init phase, disable SSL_write */
149ca259d14SSimon Schubert 		config.features |= NOSSL;
150f67bedddSMatthias Schmidt 
15192fe556dSDaniel Fojt 		if (perform_server_greeting(fd, features) == 0) {
152f67bedddSMatthias Schmidt 			send_remote_command(fd, "STARTTLS");
1537b68d8aeSMatthias Schmidt 			if (read_remote(fd, 0, NULL) != 2) {
154c8b07ee5SSascha Wildner 				if ((feature & TLS_OPP) == 0) {
155c8b07ee5SSascha Wildner 					syslog(LOG_ERR, "remote delivery deferred: STARTTLS not available: %s", neterr);
156e2c88018SSimon Schubert 					return (1);
157c8b07ee5SSascha Wildner 				} else {
158c8b07ee5SSascha Wildner 					syslog(LOG_INFO, "in opportunistic TLS mode, STARTTLS not available: %s", neterr);
159c8b07ee5SSascha Wildner 					return (0);
160c8b07ee5SSascha Wildner 				}
161f67bedddSMatthias Schmidt 			}
1624e8d31bcSDaniel Fojt 		} else {
1634e8d31bcSDaniel Fojt 			syslog(LOG_ERR, "remote delivery deferred: could not perform server greeting: %s",
1644e8d31bcSDaniel Fojt 				neterr);
1654e8d31bcSDaniel Fojt 			return (1);
166f67bedddSMatthias Schmidt 		}
16792fe556dSDaniel Fojt 
168f67bedddSMatthias Schmidt 		/* End of TLS init phase, enable SSL_write/read */
169ca259d14SSimon Schubert 		config.features &= ~NOSSL;
170f67bedddSMatthias Schmidt 	}
171f67bedddSMatthias Schmidt 
172ca259d14SSimon Schubert 	config.ssl = SSL_new(ctx);
173ca259d14SSimon Schubert 	if (config.ssl == NULL) {
174405f48eeSSimon Schubert 		syslog(LOG_NOTICE, "remote delivery deferred: SSL struct creation failed: %s",
175405f48eeSSimon Schubert 		       ssl_errstr());
176e2c88018SSimon Schubert 		return (1);
177f67bedddSMatthias Schmidt 	}
178f67bedddSMatthias Schmidt 
179f67bedddSMatthias Schmidt 	/* Set ssl to work in client mode */
180ca259d14SSimon Schubert 	SSL_set_connect_state(config.ssl);
181f67bedddSMatthias Schmidt 
182f67bedddSMatthias Schmidt 	/* Set fd for SSL in/output */
183ca259d14SSimon Schubert 	error = SSL_set_fd(config.ssl, fd);
184f67bedddSMatthias Schmidt 	if (error == 0) {
185405f48eeSSimon Schubert 		syslog(LOG_NOTICE, "remote delivery deferred: SSL set fd failed: %s",
186405f48eeSSimon Schubert 		       ssl_errstr());
187e2c88018SSimon Schubert 		return (1);
188f67bedddSMatthias Schmidt 	}
189f67bedddSMatthias Schmidt 
190f67bedddSMatthias Schmidt 	/* Open SSL connection */
191ca259d14SSimon Schubert 	error = SSL_connect(config.ssl);
19292fe556dSDaniel Fojt 	if (error != 1) {
193405f48eeSSimon Schubert 		syslog(LOG_ERR, "remote delivery deferred: SSL handshake failed fatally: %s",
194405f48eeSSimon Schubert 		       ssl_errstr());
195e2c88018SSimon Schubert 		return (1);
196f67bedddSMatthias Schmidt 	}
197f67bedddSMatthias Schmidt 
198f67bedddSMatthias Schmidt 	/* Get peer certificate */
199ca259d14SSimon Schubert 	cert = SSL_get_peer_certificate(config.ssl);
200f67bedddSMatthias Schmidt 	if (cert == NULL) {
201405f48eeSSimon Schubert 		syslog(LOG_WARNING, "remote delivery deferred: Peer did not provide certificate: %s",
202405f48eeSSimon Schubert 		       ssl_errstr());
20392fe556dSDaniel Fojt 		return (1);
20492fe556dSDaniel Fojt 	}
20592fe556dSDaniel Fojt 	if(config.fingerprint != NULL && verify_server_fingerprint(cert)) {
20692fe556dSDaniel Fojt 		X509_free(cert);
20792fe556dSDaniel Fojt 		return (1);
208f67bedddSMatthias Schmidt 	}
209f67bedddSMatthias Schmidt 	X509_free(cert);
210f67bedddSMatthias Schmidt 
211f67bedddSMatthias Schmidt 	return (0);
212f67bedddSMatthias Schmidt }
213f67bedddSMatthias Schmidt 
2147b68d8aeSMatthias Schmidt /*
2157b68d8aeSMatthias Schmidt  * hmac_md5() taken out of RFC 2104.  This RFC was written by H. Krawczyk,
2167b68d8aeSMatthias Schmidt  * M. Bellare and R. Canetti.
217bc7baf1dSSascha Wildner  *
218bc7baf1dSSascha Wildner  * text      pointer to data stream
219bc7baf1dSSascha Wildner  * text_len  length of data stream
220bc7baf1dSSascha Wildner  * key       pointer to authentication key
221bc7baf1dSSascha Wildner  * key_len   length of authentication key
222bc7baf1dSSascha Wildner  * digest    caller digest to be filled int
2237b68d8aeSMatthias Schmidt  */
2247b68d8aeSMatthias Schmidt void
hmac_md5(unsigned char * text,int text_len,unsigned char * key,int key_len,unsigned char * digest)225bc7baf1dSSascha Wildner hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
226c8b07ee5SSascha Wildner     unsigned char* digest)
2277b68d8aeSMatthias Schmidt {
2287b68d8aeSMatthias Schmidt         MD5_CTX context;
2297b68d8aeSMatthias Schmidt         unsigned char k_ipad[65];    /* inner padding -
2307b68d8aeSMatthias Schmidt                                       * key XORd with ipad
2317b68d8aeSMatthias Schmidt                                       */
2327b68d8aeSMatthias Schmidt         unsigned char k_opad[65];    /* outer padding -
2337b68d8aeSMatthias Schmidt                                       * key XORd with opad
2347b68d8aeSMatthias Schmidt                                       */
2357b68d8aeSMatthias Schmidt         unsigned char tk[16];
2367b68d8aeSMatthias Schmidt         int i;
2377b68d8aeSMatthias Schmidt         /* if key is longer than 64 bytes reset it to key=MD5(key) */
2387b68d8aeSMatthias Schmidt         if (key_len > 64) {
2397b68d8aeSMatthias Schmidt 
2407b68d8aeSMatthias Schmidt                 MD5_CTX      tctx;
2417b68d8aeSMatthias Schmidt 
2427b68d8aeSMatthias Schmidt                 MD5_Init(&tctx);
2437b68d8aeSMatthias Schmidt                 MD5_Update(&tctx, key, key_len);
2447b68d8aeSMatthias Schmidt                 MD5_Final(tk, &tctx);
2457b68d8aeSMatthias Schmidt 
2467b68d8aeSMatthias Schmidt                 key = tk;
2477b68d8aeSMatthias Schmidt                 key_len = 16;
2487b68d8aeSMatthias Schmidt         }
2497b68d8aeSMatthias Schmidt 
2507b68d8aeSMatthias Schmidt         /*
2517b68d8aeSMatthias Schmidt          * the HMAC_MD5 transform looks like:
2527b68d8aeSMatthias Schmidt          *
2537b68d8aeSMatthias Schmidt          * MD5(K XOR opad, MD5(K XOR ipad, text))
2547b68d8aeSMatthias Schmidt          *
2557b68d8aeSMatthias Schmidt          * where K is an n byte key
2567b68d8aeSMatthias Schmidt          * ipad is the byte 0x36 repeated 64 times
2577b68d8aeSMatthias Schmidt 	 *
2587b68d8aeSMatthias Schmidt          * opad is the byte 0x5c repeated 64 times
2597b68d8aeSMatthias Schmidt          * and text is the data being protected
2607b68d8aeSMatthias Schmidt          */
2617b68d8aeSMatthias Schmidt 
2627b68d8aeSMatthias Schmidt         /* start out by storing key in pads */
2637b68d8aeSMatthias Schmidt         bzero( k_ipad, sizeof k_ipad);
2647b68d8aeSMatthias Schmidt         bzero( k_opad, sizeof k_opad);
2657b68d8aeSMatthias Schmidt         bcopy( key, k_ipad, key_len);
2667b68d8aeSMatthias Schmidt         bcopy( key, k_opad, key_len);
2677b68d8aeSMatthias Schmidt 
2687b68d8aeSMatthias Schmidt         /* XOR key with ipad and opad values */
2697b68d8aeSMatthias Schmidt         for (i=0; i<64; i++) {
2707b68d8aeSMatthias Schmidt                 k_ipad[i] ^= 0x36;
2717b68d8aeSMatthias Schmidt                 k_opad[i] ^= 0x5c;
2727b68d8aeSMatthias Schmidt         }
2737b68d8aeSMatthias Schmidt         /*
2747b68d8aeSMatthias Schmidt          * perform inner MD5
2757b68d8aeSMatthias Schmidt          */
2767b68d8aeSMatthias Schmidt         MD5_Init(&context);                   /* init context for 1st
2777b68d8aeSMatthias Schmidt                                               * pass */
2787b68d8aeSMatthias Schmidt         MD5_Update(&context, k_ipad, 64);     /* start with inner pad */
2797b68d8aeSMatthias Schmidt         MD5_Update(&context, text, text_len); /* then text of datagram */
2807b68d8aeSMatthias Schmidt         MD5_Final(digest, &context);          /* finish up 1st pass */
2817b68d8aeSMatthias Schmidt         /*
2827b68d8aeSMatthias Schmidt          * perform outer MD5
2837b68d8aeSMatthias Schmidt          */
2847b68d8aeSMatthias Schmidt         MD5_Init(&context);                   /* init context for 2nd
2857b68d8aeSMatthias Schmidt                                               * pass */
2867b68d8aeSMatthias Schmidt         MD5_Update(&context, k_opad, 64);     /* start with outer pad */
2877b68d8aeSMatthias Schmidt         MD5_Update(&context, digest, 16);     /* then results of 1st
2887b68d8aeSMatthias Schmidt                                               * hash */
2897b68d8aeSMatthias Schmidt         MD5_Final(digest, &context);          /* finish up 2nd pass */
2907b68d8aeSMatthias Schmidt }
2917b68d8aeSMatthias Schmidt 
292f67bedddSMatthias Schmidt /*
293f67bedddSMatthias Schmidt  * CRAM-MD5 authentication
294f67bedddSMatthias Schmidt  */
295f67bedddSMatthias Schmidt int
smtp_auth_md5(int fd,char * login,char * password)296405f48eeSSimon Schubert smtp_auth_md5(int fd, char *login, char *password)
297f67bedddSMatthias Schmidt {
298c8b07ee5SSascha Wildner 	unsigned char digest[BUF_SIZE];
299c8b07ee5SSascha Wildner 	char buffer[BUF_SIZE], ascii_digest[33];
3007b68d8aeSMatthias Schmidt 	char *temp;
3017b68d8aeSMatthias Schmidt 	int len, i;
3027b68d8aeSMatthias Schmidt 	static char hextab[] = "0123456789abcdef";
3037b68d8aeSMatthias Schmidt 
3047b68d8aeSMatthias Schmidt 	temp = calloc(BUF_SIZE, 1);
3057b68d8aeSMatthias Schmidt 	memset(buffer, 0, sizeof(buffer));
3067b68d8aeSMatthias Schmidt 	memset(digest, 0, sizeof(digest));
3077b68d8aeSMatthias Schmidt 	memset(ascii_digest, 0, sizeof(ascii_digest));
3087b68d8aeSMatthias Schmidt 
3097b68d8aeSMatthias Schmidt 	/* Send AUTH command according to RFC 2554 */
3107b68d8aeSMatthias Schmidt 	send_remote_command(fd, "AUTH CRAM-MD5");
3117b68d8aeSMatthias Schmidt 	if (read_remote(fd, sizeof(buffer), buffer) != 3) {
31234599d02SMatthias Schmidt 		syslog(LOG_DEBUG, "smarthost authentication:"
313405f48eeSSimon Schubert 		       " AUTH cram-md5 not available: %s", neterr);
3147b68d8aeSMatthias Schmidt 		/* if cram-md5 is not available */
315c8b07ee5SSascha Wildner 		free(temp);
3167b68d8aeSMatthias Schmidt 		return (-1);
317f67bedddSMatthias Schmidt 	}
3187b68d8aeSMatthias Schmidt 
3197b68d8aeSMatthias Schmidt 	/* skip 3 char status + 1 char space */
3207b68d8aeSMatthias Schmidt 	base64_decode(buffer + 4, temp);
321c8b07ee5SSascha Wildner 	hmac_md5((unsigned char *)temp, strlen(temp),
322c8b07ee5SSascha Wildner 		 (unsigned char *)password, strlen(password), digest);
3235d7fe8bbSSimon Schubert 	free(temp);
3247b68d8aeSMatthias Schmidt 
3257b68d8aeSMatthias Schmidt 	ascii_digest[32] = 0;
3267b68d8aeSMatthias Schmidt 	for (i = 0; i < 16; i++) {
3277b68d8aeSMatthias Schmidt 		ascii_digest[2*i] = hextab[digest[i] >> 4];
3287b68d8aeSMatthias Schmidt 		ascii_digest[2*i+1] = hextab[digest[i] & 15];
3297b68d8aeSMatthias Schmidt 	}
3307b68d8aeSMatthias Schmidt 
3317b68d8aeSMatthias Schmidt 	/* prepare answer */
3327b68d8aeSMatthias Schmidt 	snprintf(buffer, BUF_SIZE, "%s %s", login, ascii_digest);
3337b68d8aeSMatthias Schmidt 
3347b68d8aeSMatthias Schmidt 	/* encode answer */
3357b68d8aeSMatthias Schmidt 	len = base64_encode(buffer, strlen(buffer), &temp);
3365d7fe8bbSSimon Schubert 	if (len < 0) {
337405f48eeSSimon Schubert 		syslog(LOG_ERR, "can not encode auth reply: %m");
3387b68d8aeSMatthias Schmidt 		return (-1);
3395d7fe8bbSSimon Schubert 	}
3407b68d8aeSMatthias Schmidt 
3417b68d8aeSMatthias Schmidt 	/* send answer */
3427b68d8aeSMatthias Schmidt 	send_remote_command(fd, "%s", temp);
3435d7fe8bbSSimon Schubert 	free(temp);
3447b68d8aeSMatthias Schmidt 	if (read_remote(fd, 0, NULL) != 2) {
345405f48eeSSimon Schubert 		syslog(LOG_WARNING, "remote delivery deferred:"
346405f48eeSSimon Schubert 				" AUTH cram-md5 failed: %s", neterr);
3477b68d8aeSMatthias Schmidt 		return (-2);
3487b68d8aeSMatthias Schmidt 	}
3497b68d8aeSMatthias Schmidt 
3507b68d8aeSMatthias Schmidt 	return (0);
3517b68d8aeSMatthias Schmidt }
352