-#include "webserver.h"
-/** \todo dirify */
-/** where to find the keys */
-#define CTDL_CRYPTO_DIR "./keys"
-#define CTDL_KEY_PATH CTDL_CRYPTO_DIR "/citadel.key" /**< the key */
-#define CTDL_CSR_PATH CTDL_CRYPTO_DIR "/citadel.csr" /**< the csr file */
-#define CTDL_CER_PATH CTDL_CRYPTO_DIR "/citadel.cer" /**< the cer file */
-#define SIGN_DAYS 365 /**< how long our certificate should live */
-
-SSL_CTX *ssl_ctx; /**< SSL context */
-pthread_mutex_t **SSLCritters; /**< Things needing locking */
-
-pthread_key_t ThreadSSL; /**< Per-thread SSL context */
-
-/**
- * \brief what?????
- * \return thread id???
- */
-static unsigned long id_callback(void)
-{
- return (unsigned long) pthread_self();
-}
-
-/**
- * \brief initialize ssl engine
- * load certs and initialize openssl internals
- */
-void init_ssl(void)
-{
- SSL_METHOD *ssl_method;
- RSA *rsa=NULL;
- X509_REQ *req = NULL;
- X509 *cer = NULL;
- EVP_PKEY *pk = NULL;
- EVP_PKEY *req_pkey = NULL;
- X509_NAME *name = NULL;
- FILE *fp;
- char buf[SIZ];
-
- if (!access("/var/run/egd-pool", F_OK))
- RAND_egd("/var/run/egd-pool");
-
- if (!RAND_status()) {
- lprintf(3,
- "PRNG not adequately seeded, won't do SSL/TLS\n");
- return;
- }
- SSLCritters =
- malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
- if (!SSLCritters) {
- lprintf(1, "citserver: can't allocate memory!!\n");
- /* Nothing's been initialized, just die */
- exit(WC_EXIT_SSL);
- } else {
- int a;
-
- for (a = 0; a < CRYPTO_num_locks(); a++) {
- SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
- if (!SSLCritters[a]) {
- lprintf(1,
- "citserver: can't allocate memory!!\n");
- /** Nothing's been initialized, just die */
- exit(WC_EXIT_SSL);
- }
- pthread_mutex_init(SSLCritters[a], NULL);
- }
- }
-
- /**
- * Initialize SSL transport layer
- */
- SSL_library_init();
- SSL_load_error_strings();
- ssl_method = SSLv23_server_method();
- if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
- lprintf(3, "SSL_CTX_new failed: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- return;
- }
-
- CRYPTO_set_locking_callback(ssl_lock);
- CRYPTO_set_id_callback(id_callback);
-
- /**
- * Get our certificates in order. \todo dirify. this is a setup job.
- * First, create the key/cert directory if it's not there already...
- */
- mkdir(CTDL_CRYPTO_DIR, 0700);
-
- /**
- * Before attempting to generate keys/certificates, first try
- * link to them from the Citadel server if it's on the same host.
- * We ignore any error return because it either meant that there
- * was nothing in Citadel to link from (in which case we just
- * generate new files) or the target files already exist (which
- * is not fatal either). \todo dirify
- */
- if (!strcasecmp(ctdlhost, "uds")) {
- sprintf(buf, "%s/keys/citadel.key", ctdlport);
- symlink(buf, CTDL_KEY_PATH);
- sprintf(buf, "%s/keys/citadel.csr", ctdlport);
- symlink(buf, CTDL_CSR_PATH);
- sprintf(buf, "%s/keys/citadel.cer", ctdlport);
- symlink(buf, CTDL_CER_PATH);
- }
-
- /**
- * If we still don't have a private key, generate one.
- */
- if (access(CTDL_KEY_PATH, R_OK) != 0) {
- lprintf(5, "Generating RSA key pair.\n");
- rsa = RSA_generate_key(1024, /**< modulus size */
- 65537, /**< exponent */
- NULL, /**< no callback */
- NULL); /**< no callback */
- if (rsa == NULL) {
- lprintf(3, "Key generation failed: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- }
- if (rsa != NULL) {
- fp = fopen(CTDL_KEY_PATH, "w");
- if (fp != NULL) {
- chmod(CTDL_KEY_PATH, 0600);
- if (PEM_write_RSAPrivateKey(fp, /**< the file */
- rsa, /**< the key */
- NULL, /**< no enc */
- NULL, /**< no passphr */
- 0, /**< no passphr */
- NULL, /**< no callbk */
- NULL /**< no callbk */
- ) != 1) {
- lprintf(3, "Cannot write key: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- unlink(CTDL_KEY_PATH);
- }
- fclose(fp);
- }
- RSA_free(rsa);
- }
- }
-
- /**
- * Generate a CSR if we don't have one.
- */
- if (access(CTDL_CSR_PATH, R_OK) != 0) {
- lprintf(5, "Generating a certificate signing request.\n");
-
- /**
- * Read our key from the file. No, we don't just keep this
- * in memory from the above key-generation function, because
- * 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");
- if (fp) {
- rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
- fclose(fp);
- }
-
- if (rsa) {