- CRYPTO_set_locking_callback(ssl_lock);
- CRYPTO_set_id_callback(id_callback);
-
- // Get our certificates in order.
- // 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).
- if (!strcasecmp(ctdlhost, "uds")) {
- sprintf(buf, "%s/keys/citadel.key", ctdlport);
- rv = symlink(buf, CTDL_KEY_PATH);
- if (!rv) {
- syslog(LOG_DEBUG, "%s", strerror(errno));
- }
- sprintf(buf, "%s/keys/citadel.csr", ctdlport);
- rv = symlink(buf, CTDL_CSR_PATH);
- if (!rv) {
- syslog(LOG_DEBUG, "%s", strerror(errno));
- }
- sprintf(buf, "%s/keys/citadel.cer", ctdlport);
- rv = symlink(buf, CTDL_CER_PATH);
- if (!rv) {
- syslog(LOG_DEBUG, "%s", strerror(errno));
- }
- }
-
- // If we still don't have a private key, generate one.
- generate_key(CTDL_KEY_PATH);
-
- // If there is no certificate file on disk, we will be generating a self-signed certificate
- // in the next step. Therefore, if we have neither a CSR nor a certificate, generate
- // the CSR in this step so that the next step may commence.
- if ( (access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0) ) {
- syslog(LOG_INFO, "Generating a certificate signing request.");
-
- // 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) {
- // Create a public key from the private key
- if (pk=EVP_PKEY_new(), pk != NULL) {
- EVP_PKEY_assign_RSA(pk, rsa);
- if (req = X509_REQ_new(), req != NULL) {
- const char *env;
- // Set the public key
- X509_REQ_set_pubkey(req, pk);
- X509_REQ_set_version(req, 0L);
-
- name = X509_REQ_get_subject_name(req);
-
- // Tell it who we are
- env = getenv("O");
- if (env == NULL) {
- env = "Organization name";
- }
-
- X509_NAME_add_entry_by_txt(
- name, "O",
- MBSTRING_ASC,
- (unsigned char*)env,
- -1, -1, 0
- );
-
- env = getenv("OU");
- if (env == NULL) {
- env = "Citadel server";
- }
-
- X509_NAME_add_entry_by_txt(
- name, "OU",
- MBSTRING_ASC,
- (unsigned char*)env,
- -1, -1, 0
- );
-
- env = getenv("CN");
- if (env == NULL)
- env = "*";
-
- X509_NAME_add_entry_by_txt(
- name, "CN",
- MBSTRING_ASC,
- (unsigned char*)env,
- -1, -1, 0
- );
-
- X509_REQ_set_subject_name(req, name);
-
- // Sign the CSR
- if (!X509_REQ_sign(req, pk, EVP_md5())) {
- syslog(LOG_WARNING, "X509_REQ_sign(): error");
- }
- else {
- // Write it to disk.
- fp = fopen(CTDL_CSR_PATH, "w");
- if (fp != NULL) {
- chmod(CTDL_CSR_PATH, 0600);
- PEM_write_X509_REQ(fp, req);
- fclose(fp);
- }
- else {
- syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH);
- ShutDownWebcit();
- exit(0);
- }
- }
-
- X509_REQ_free(req);
- }
- }
- RSA_free(rsa);
- }
- else {
- syslog(LOG_WARNING, "Unable to read private key.");
- }
- }
-
- // Generate a self-signed certificate if we don't have one.
- if (access(CTDL_CER_PATH, R_OK) != 0) {
- syslog(LOG_INFO, "Generating a self-signed certificate.\n");