#include "server.h"
#include "serv_crypto.h"
#include "sysdep_decls.h"
-#include "serv_extensions.h"
#include "citadel.h"
#include "config.h"
+#include "ctdl_module.h"
+/* TODO: should we use the standard module init stuff to start this? */
+/* TODO: should we register an event handler to call destruct_ssl? */
+
#ifdef HAVE_OPENSSL
SSL_CTX *ssl_ctx; /* SSL context */
pthread_mutex_t **SSLCritters; /* Things needing locking */
return (unsigned long) pthread_self();
}
- /*
- * Set up the cert things on the server side. We do need both the
- * private key (in key_file) and the cert (in cert_file).
- * Both files may be identical.
- *
- * This function is taken from OpenSSL apps/s_cb.c
- */
-
-static int ctdl_install_certificate(SSL_CTX * ctx,
- const char *cert_file, const char *key_file)
+void destruct_ssl(void)
{
- if (cert_file != NULL) {
- if (SSL_CTX_use_certificate_file(ctx, cert_file,
- SSL_FILETYPE_PEM) <= 0) {
- lprintf(CTDL_CRIT, "unable to get certificate from '%s'",
- cert_file);
- return (0);
- }
- if (key_file == NULL)
- key_file = cert_file;
- if (SSL_CTX_use_PrivateKey_file(ctx, key_file,
- SSL_FILETYPE_PEM) <= 0) {
- lprintf(CTDL_CRIT, "unable to get private key from '%s'",
- key_file);
- return (0);
- }
- /* Now we know that a key and cert have been set against
- * the SSL context */
- if (!SSL_CTX_check_private_key(ctx)) {
- lprintf(CTDL_CRIT,
- "Private key does not match the certificate public key");
- return (0);
- }
- }
- return (1);
+ int a;
+ CtdlUnregisterProtoHook(cmd_stls, "STLS");
+ CtdlUnregisterProtoHook(cmd_gtls, "GTLS");
+ for (a = 0; a < CRYPTO_num_locks(); a++)
+ free(SSLCritters[a]);
+ free (SSLCritters);
}
-
void init_ssl(void)
{
SSL_METHOD *ssl_method;
X509_NAME *name = NULL;
FILE *fp;
- if (!access("/var/run/egd-pool", F_OK))
- RAND_egd("/var/run/egd-pool");
+ if (!access(EGD_POOL, F_OK))
+ RAND_egd(EGD_POOL);
if (!RAND_status()) {
lprintf(CTDL_CRIT,
/* Get our certificates in order.
* First, create the key/cert directory if it's not there already...
*/
- mkdir(CTDL_CRYPTO_DIR, 0700);
+ mkdir(ctdl_key_dir, 0700);
/*
* Generate a key pair if we don't have one.
*/
- if (access(CTDL_KEY_PATH, R_OK) != 0) {
+ if (access(file_crpt_file_key, R_OK) != 0) {
lprintf(CTDL_INFO, "Generating RSA key pair.\n");
rsa = RSA_generate_key(1024, /* modulus size */
65537, /* exponent */
ERR_reason_error_string(ERR_get_error()));
}
if (rsa != NULL) {
- fp = fopen(CTDL_KEY_PATH, "w");
+ fp = fopen(file_crpt_file_key, "w");
if (fp != NULL) {
- chmod(CTDL_KEY_PATH, 0600);
+ chmod(file_crpt_file_key, 0600);
if (PEM_write_RSAPrivateKey(fp, /* the file */
rsa, /* the key */
NULL, /* no enc */
) != 1) {
lprintf(CTDL_CRIT, "Cannot write key: %s\n",
ERR_reason_error_string(ERR_get_error()));
- unlink(CTDL_KEY_PATH);
+ unlink(file_crpt_file_key);
}
fclose(fp);
}
/*
* Generate a CSR if we don't have one.
*/
- if (access(CTDL_CSR_PATH, R_OK) != 0) {
+ if (access(file_crpt_file_csr, R_OK) != 0) {
lprintf(CTDL_INFO, "Generating a certificate signing request.\n");
/*
* there is the possibility that the key was already on disk
* and we didn't just generate it now.
*/
- fp = fopen(CTDL_KEY_PATH, "r");
+ fp = fopen(file_crpt_file_key, "r");
if (fp) {
rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
X509_NAME_add_entry_by_txt(name, "OU",
MBSTRING_ASC, "Citadel server", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "CN",
+ /* X509_NAME_add_entry_by_txt(name, "CN",
MBSTRING_ASC, config.c_fqdn, -1, -1, 0);
+ */
+
+ X509_NAME_add_entry_by_txt(name, "CN",
+ MBSTRING_ASC, "*", -1, -1, 0);
X509_REQ_set_subject_name(req, name);
}
else {
/* Write it to disk. */
- fp = fopen(CTDL_CSR_PATH, "w");
+ fp = fopen(file_crpt_file_csr, "w");
if (fp != NULL) {
- chmod(CTDL_CSR_PATH, 0600);
+ chmod(file_crpt_file_csr, 0600);
PEM_write_X509_REQ(fp, req);
fclose(fp);
}
/*
* Generate a self-signed certificate if we don't have one.
*/
- if (access(CTDL_CER_PATH, R_OK) != 0) {
+ if (access(file_crpt_file_cer, R_OK) != 0) {
lprintf(CTDL_INFO, "Generating a self-signed certificate.\n");
/* Same deal as before: always read the key from disk because
* it may or may not have just been generated.
*/
- fp = fopen(CTDL_KEY_PATH, "r");
+ fp = fopen(file_crpt_file_key, "r");
if (fp) {
rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
EVP_PKEY_assign_RSA(pk, rsa);
}
- fp = fopen(CTDL_CSR_PATH, "r");
+ fp = fopen(file_crpt_file_csr, "r");
if (fp) {
req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
fclose(fp);
if (req) {
if (cer = X509_new(), cer != NULL) {
+ ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
X509_set_issuer_name(cer, req->req_info->subject);
X509_set_subject_name(cer, req->req_info->subject);
X509_gmtime_adj(X509_get_notBefore(cer),0);
}
else {
/* Write it to disk. */
- fp = fopen(CTDL_CER_PATH, "w");
+ fp = fopen(file_crpt_file_cer, "w");
if (fp != NULL) {
- chmod(CTDL_CER_PATH, 0600);
+ chmod(file_crpt_file_cer, 0600);
PEM_write_X509(fp, cer);
fclose(fp);
}
/*
* Now try to bind to the key and certificate.
*/
- if (ctdl_install_certificate(ssl_ctx,
- CTDL_CER_PATH,
- CTDL_KEY_PATH) != 1)
- {
+ SSL_CTX_use_certificate_chain_file(ssl_ctx, file_crpt_file_cer);
+ SSL_CTX_use_PrivateKey_file(ssl_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
+ if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
lprintf(CTDL_CRIT, "Cannot install certificate: %s\n",
ERR_reason_error_string(ERR_get_error()));
- }
+ }
/* Finally let the server know we're here */
CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
int retval, bits, alg_bits;
if (!ssl_ctx) {
- cprintf("%s", nosup_response);
+ lprintf(CTDL_CRIT, "SSL failed: no ssl_ctx exists?\n");
+ if (nosup_response != NULL) cprintf("%s", nosup_response);
return;
}
if (!(CC->ssl = SSL_new(ssl_ctx))) {
lprintf(CTDL_CRIT, "SSL_new failed: %s\n",
ERR_reason_error_string(ERR_get_error()));
- cprintf("%s", error_response);
+ if (error_response != NULL) cprintf("%s", error_response);
return;
}
if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
ERR_reason_error_string(ERR_get_error()));
SSL_free(CC->ssl);
CC->ssl = NULL;
- cprintf("%s", error_response);
+ if (error_response != NULL) cprintf("%s", error_response);
return;
}
- cprintf("%s", ok_response);
+ if (ok_response != NULL) cprintf("%s", ok_response);
retval = SSL_accept(CC->ssl);
if (retval < 1) {
/*
* revert to unencrypted communications.
*/
long errval;
+ char error_string[128];
errval = SSL_get_error(CC->ssl, retval);
- lprintf(CTDL_CRIT, "SSL_accept failed: %s\n",
- ERR_reason_error_string(ERR_get_error()));
+ lprintf(CTDL_CRIT, "SSL_accept failed: retval=%d, errval=%ld, err=%s\n",
+ retval,
+ errval,
+ ERR_error_string(errval, error_string)
+ );
SSL_free(CC->ssl);
CC->ssl = NULL;
return;
}
BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
- bits =
- SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
- &alg_bits);
+ bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
lprintf(CTDL_INFO, "SSL/TLS using %s on %s (%d of %d bits)\n",
SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
char nosup_response[SIZ];
char error_response[SIZ];
+ unbuffer_output();
+
sprintf(ok_response,
"%d Begin TLS negotiation now\n",
CIT_OK);