]>
git.ipfire.org Git - people/ms/dma.git/blob - crypto.c
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
35 * $DragonFly: src/libexec/dma/crypto.c,v 1.4 2008/09/30 17:47:21 swildner Exp $
40 #include <openssl/x509.h>
41 #include <openssl/md5.h>
42 #include <openssl/ssl.h>
43 #include <openssl/err.h>
44 #include <openssl/pem.h>
45 #include <openssl/rand.h>
51 extern struct config
*config
;
54 init_cert_file(struct qitem
*it
, SSL_CTX
*ctx
, const char *path
)
58 /* Load certificate into ctx */
59 error
= SSL_CTX_use_certificate_chain_file(ctx
, path
);
61 syslog(LOG_ERR
, "%s: SSL: Cannot load certificate: %s",
66 /* Add private key to ctx */
67 error
= SSL_CTX_use_PrivateKey_file(ctx
, path
, SSL_FILETYPE_PEM
);
69 syslog(LOG_ERR
, "%s: SSL: Cannot load private key: %s",
75 * Check the consistency of a private key with the corresponding
78 error
= SSL_CTX_check_private_key(ctx
);
80 syslog(LOG_ERR
, "%s: SSL: Cannot check private key: %s",
89 smtp_init_crypto(struct qitem
*it
, int fd
, int feature
)
92 SSL_METHOD
*meth
= NULL
;
96 /* Init SSL library */
99 meth
= TLSv1_client_method();
101 ctx
= SSL_CTX_new(meth
);
103 syslog(LOG_ERR
, "%s: remote delivery deferred:"
104 " SSL init failed: %m", it
->queueid
);
108 /* User supplied a certificate */
109 if (config
->certfile
!= NULL
)
110 init_cert_file(it
, ctx
, config
->certfile
);
113 * If the user wants STARTTLS, we have to send EHLO here
115 if (((feature
& SECURETRANS
) != 0) &&
116 (feature
& STARTTLS
) != 0) {
117 /* TLS init phase, disable SSL_write */
118 config
->features
|= NOSSL
;
120 send_remote_command(fd
, "EHLO %s", hostname());
121 if (read_remote(fd
, 0, NULL
) == 2) {
122 send_remote_command(fd
, "STARTTLS");
123 if (read_remote(fd
, 0, NULL
) != 2) {
124 syslog(LOG_ERR
, "%s: remote delivery failed:"
125 " STARTTLS not available: %s", it
->queueid
,
127 config
->features
&= ~NOSSL
;
131 /* End of TLS init phase, enable SSL_write/read */
132 config
->features
&= ~NOSSL
;
135 config
->ssl
= SSL_new(ctx
);
136 if (config
->ssl
== NULL
) {
137 syslog(LOG_ERR
, "%s: remote delivery deferred:"
138 " SSL struct creation failed:", it
->queueid
);
142 /* Set ssl to work in client mode */
143 SSL_set_connect_state(config
->ssl
);
145 /* Set fd for SSL in/output */
146 error
= SSL_set_fd(config
->ssl
, fd
);
148 error
= SSL_get_error(config
->ssl
, error
);
149 syslog(LOG_ERR
, "%s: remote delivery deferred:"
150 " SSL set fd failed (%d): %m", it
->queueid
, error
);
154 /* Open SSL connection */
155 error
= SSL_connect(config
->ssl
);
157 syslog(LOG_ERR
, "%s: remote delivery failed:"
158 " SSL handshake failed fatally: %m", it
->queueid
);
162 /* Get peer certificate */
163 cert
= SSL_get_peer_certificate(config
->ssl
);
165 syslog(LOG_ERR
, "%s: remote delivery deferred:"
166 " Peer did not provide certificate: %m", it
->queueid
);
174 * hmac_md5() taken out of RFC 2104. This RFC was written by H. Krawczyk,
175 * M. Bellare and R. Canetti.
177 * text pointer to data stream
178 * text_len length of data stream
179 * key pointer to authentication key
180 * key_len length of authentication key
181 * digest caller digest to be filled int
184 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
188 unsigned char k_ipad
[65]; /* inner padding -
191 unsigned char k_opad
[65]; /* outer padding -
194 unsigned char tk
[16];
196 /* if key is longer than 64 bytes reset it to key=MD5(key) */
202 MD5_Update(&tctx
, key
, key_len
);
203 MD5_Final(tk
, &tctx
);
210 * the HMAC_MD5 transform looks like:
212 * MD5(K XOR opad, MD5(K XOR ipad, text))
214 * where K is an n byte key
215 * ipad is the byte 0x36 repeated 64 times
217 * opad is the byte 0x5c repeated 64 times
218 * and text is the data being protected
221 /* start out by storing key in pads */
222 bzero( k_ipad
, sizeof k_ipad
);
223 bzero( k_opad
, sizeof k_opad
);
224 bcopy( key
, k_ipad
, key_len
);
225 bcopy( key
, k_opad
, key_len
);
227 /* XOR key with ipad and opad values */
228 for (i
=0; i
<64; i
++) {
235 MD5_Init(&context
); /* init context for 1st
237 MD5_Update(&context
, k_ipad
, 64); /* start with inner pad */
238 MD5_Update(&context
, text
, text_len
); /* then text of datagram */
239 MD5_Final(digest
, &context
); /* finish up 1st pass */
243 MD5_Init(&context
); /* init context for 2nd
245 MD5_Update(&context
, k_opad
, 64); /* start with outer pad */
246 MD5_Update(&context
, digest
, 16); /* then results of 1st
248 MD5_Final(digest
, &context
); /* finish up 2nd pass */
252 * CRAM-MD5 authentication
255 smtp_auth_md5(struct qitem
*it
, int fd
, char *login
, char *password
)
257 unsigned char buffer
[BUF_SIZE
], digest
[BUF_SIZE
], ascii_digest
[33];
260 static char hextab
[] = "0123456789abcdef";
262 temp
= calloc(BUF_SIZE
, 1);
263 memset(buffer
, 0, sizeof(buffer
));
264 memset(digest
, 0, sizeof(digest
));
265 memset(ascii_digest
, 0, sizeof(ascii_digest
));
267 /* Send AUTH command according to RFC 2554 */
268 send_remote_command(fd
, "AUTH CRAM-MD5");
269 if (read_remote(fd
, sizeof(buffer
), buffer
) != 3) {
270 syslog(LOG_ERR
, "%s: smarthost authentification:"
271 " AUTH cram-md5 not available: %s", it
->queueid
,
273 /* if cram-md5 is not available */
277 /* skip 3 char status + 1 char space */
278 base64_decode(buffer
+ 4, temp
);
279 hmac_md5(temp
, strlen(temp
), password
, strlen(password
), digest
);
281 ascii_digest
[32] = 0;
282 for (i
= 0; i
< 16; i
++) {
283 ascii_digest
[2*i
] = hextab
[digest
[i
] >> 4];
284 ascii_digest
[2*i
+1] = hextab
[digest
[i
] & 15];
288 snprintf(buffer
, BUF_SIZE
, "%s %s", login
, ascii_digest
);
290 /* temp will be allocated inside base64_encode again */
293 len
= base64_encode(buffer
, strlen(buffer
), &temp
);
298 send_remote_command(fd
, "%s", temp
);
299 if (read_remote(fd
, 0, NULL
) != 2) {
300 syslog(LOG_ERR
, "%s: remote delivery deferred:"
301 " AUTH cram-md5 failed: %s", it
->queueid
,
309 #endif /* HAVE_CRYPTO */