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.2 2008/03/04 11:36:08 matthias Exp $
40 #include <openssl/x509.h>
41 #include <openssl/ssl.h>
42 #include <openssl/err.h>
43 #include <openssl/pem.h>
44 #include <openssl/rand.h>
50 extern struct config
*config
;
53 init_cert_file(struct qitem
*it
, SSL_CTX
*ctx
, const char *path
)
57 /* Load certificate into ctx */
58 error
= SSL_CTX_use_certificate_chain_file(ctx
, path
);
60 syslog(LOG_ERR
, "%s: SSL: Cannot load certificate: %s",
65 /* Add private key to ctx */
66 error
= SSL_CTX_use_PrivateKey_file(ctx
, path
, SSL_FILETYPE_PEM
);
68 syslog(LOG_ERR
, "%s: SSL: Cannot load private key: %s",
74 * Check the consistency of a private key with the corresponding
77 error
= SSL_CTX_check_private_key(ctx
);
79 syslog(LOG_ERR
, "%s: SSL: Cannot check private key: %s",
88 smtp_init_crypto(struct qitem
*it
, int fd
, int feature
)
91 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
) == 2) {
122 send_remote_command(fd
, "STARTTLS");
123 if (read_remote(fd
) != 2) {
124 syslog(LOG_ERR
, "%s: remote delivery failed:"
125 " STARTTLS not available: %m", it
->queueid
);
126 config
->features
&= ~NOSSL
;
130 /* End of TLS init phase, enable SSL_write/read */
131 config
->features
&= ~NOSSL
;
134 config
->ssl
= SSL_new(ctx
);
135 if (config
->ssl
== NULL
) {
136 syslog(LOG_ERR
, "%s: remote delivery deferred:"
137 " SSL struct creation failed:", it
->queueid
);
141 /* Set ssl to work in client mode */
142 SSL_set_connect_state(config
->ssl
);
144 /* Set fd for SSL in/output */
145 error
= SSL_set_fd(config
->ssl
, fd
);
147 error
= SSL_get_error(config
->ssl
, error
);
148 syslog(LOG_ERR
, "%s: remote delivery deferred:"
149 " SSL set fd failed (%d): %m", it
->queueid
, error
);
153 /* Open SSL connection */
154 error
= SSL_connect(config
->ssl
);
156 syslog(LOG_ERR
, "%s: remote delivery failed:"
157 " SSL handshake fataly failed: %m", it
->queueid
);
161 /* Get peer certificate */
162 cert
= SSL_get_peer_certificate(config
->ssl
);
164 syslog(LOG_ERR
, "%s: remote delivery deferred:"
165 " Peer did not provied certificate: %m", it
->queueid
);
174 * CRAM-MD5 authentication
176 * XXX TODO implement me, I don't have a mail server with CRAM-MD5 available
179 smtp_auth_md5(int fd
, char *login
, char *password
)
184 #endif /* HAVE_CRYPTO */