7 /* public key certificate and private key loader
9 /* #define TLS_INTERNAL
12 /* int tls_set_ca_certificate_info(ctx, CAfile, CApath)
14 /* const char *CAfile;
15 /* const char *CApath;
17 /* int tls_set_my_certificate_key_info(ctx, cert_file, key_file,
18 /* dcert_file, dkey_file,
19 /* eccert_file, eckey_file)
21 /* const char *cert_file;
22 /* const char *key_file;
23 /* const char *dcert_file;
24 /* const char *dkey_file;
25 /* const char *eccert_file;
26 /* const char *eckey_file;
28 /* OpenSSL supports two options to specify CA certificates:
29 /* either one file CAfile that contains all CA certificates,
30 /* or a directory CApath with separate files for each
31 /* individual CA, with symbolic links named after the hash
32 /* values of the certificates. The second option is not
33 /* convenient with a chrooted process.
35 /* tls_set_ca_certificate_info() loads the CA certificate
36 /* information for the specified TLS server or client context.
37 /* The result is -1 on failure, 0 on success.
39 /* tls_set_my_certificate_key_info() loads the public key
40 /* certificates and private keys for the specified TLS server
41 /* or client context. Up to 3 pairs of key pairs (RSA, DSA and
42 /* ECDSA) may be specified; each certificate and key pair must
43 /* match. The result is -1 on failure, 0 on success.
47 /* This software is free. You can do with it whatever you want.
48 /* The original author kindly requests that you acknowledge
49 /* the use of his software.
51 /* Originally written by:
54 /* Allgemeine Elektrotechnik
55 /* Universitaetsplatz 3-4
56 /* D-03044 Cottbus, Germany
60 /* IBM T.J. Watson Research
62 /* Yorktown Heights, NY 10598, USA
71 /* Utility library. */
80 /* tls_set_ca_certificate_info - load certificate authority certificates */
82 int tls_set_ca_certificate_info(SSL_CTX
*ctx
, const char *CAfile
,
89 if (CAfile
|| CApath
) {
90 if (!SSL_CTX_load_verify_locations(ctx
, CAfile
, CApath
)) {
91 msg_info("cannot load Certificate Authority data: "
92 "disabling TLS support");
96 if (!SSL_CTX_set_default_verify_paths(ctx
)) {
97 msg_info("cannot set certificate verification paths: "
98 "disabling TLS support");
106 /* set_cert_stuff - specify certificate and key information */
108 static int set_cert_stuff(SSL_CTX
*ctx
, const char *cert_type
,
109 const char *cert_file
,
110 const char *key_file
)
114 * We need both the private key (in key_file) and the public key
115 * certificate (in cert_file). Both may specify the same file.
117 * Code adapted from OpenSSL apps/s_cb.c.
120 if (SSL_CTX_use_certificate_chain_file(ctx
, cert_file
) <= 0) {
121 msg_warn("cannot get %s certificate from file %s: "
122 "disabling TLS support", cert_type
, cert_file
);
126 if (SSL_CTX_use_PrivateKey_file(ctx
, key_file
, SSL_FILETYPE_PEM
) <= 0) {
127 msg_warn("cannot get %s private key from file %s: "
128 "disabling TLS support", cert_type
, key_file
);
136 if (!SSL_CTX_check_private_key(ctx
)) {
137 msg_warn("%s private key in %s does not match public key in %s: "
138 "disabling TLS support", cert_type
, key_file
, cert_file
);
144 /* tls_set_my_certificate_key_info - load client or server certificates/keys */
146 int tls_set_my_certificate_key_info(SSL_CTX
*ctx
,
147 const char *cert_file
,
148 const char *key_file
,
149 const char *dcert_file
,
150 const char *dkey_file
,
151 const char *eccert_file
,
152 const char *eckey_file
)
156 * Lack of certificates is fine so long as we are prepared to use
159 if (*cert_file
&& !set_cert_stuff(ctx
, "RSA", cert_file
, key_file
))
160 return (-1); /* logged */
161 if (*dcert_file
&& !set_cert_stuff(ctx
, "DSA", dcert_file
, dkey_file
))
162 return (-1); /* logged */
163 #if OPENSSL_VERSION_NUMBER >= 0x00909000 && !defined(OPENSSL_NO_ECDH)
164 if (*eccert_file
&& !set_cert_stuff(ctx
, "ECDSA", eccert_file
, eckey_file
))
165 return (-1); /* logged */
168 msg_warn("ECDSA not supported. Ignoring ECDSA certificate file \"%s\"",