- CRYPTO_set_locking_callback(ssl_lock);
- CRYPTO_set_id_callback(id_callback);
-
- /*
- * Get our certificates in order. (FIXME: 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).
- */
- if (!strcasecmp(ctdlhost, "uds")) {
- sprintf(buf, "%s/keys/citadel.key", ctdlport);
- rv = symlink(buf, CTDL_KEY_PATH);
- if (!rv) syslog(1, "%s\n", strerror(errno));
- sprintf(buf, "%s/keys/citadel.csr", ctdlport);
- rv = symlink(buf, CTDL_CSR_PATH);
- if (!rv) syslog(1, "%s\n", strerror(errno));
- sprintf(buf, "%s/keys/citadel.cer", ctdlport);
- rv = symlink(buf, CTDL_CER_PATH);
- if (!rv) syslog(1, "%s\n", strerror(errno));
- }
-
- /*
- * If we still don't have a private key, generate one.
- */
- if (access(CTDL_KEY_PATH, R_OK) != 0) {
- syslog(5, "Generating RSA key pair.\n");
- rsa = RSA_generate_key(1024, /* modulus size */
- 65537, /* exponent */
- NULL, /* no callback */
- NULL /* no callback */
- );
- if (rsa == NULL) {
- syslog(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) {
- syslog(3, "Cannot write key: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- unlink(CTDL_KEY_PATH);
- }
- fclose(fp);
- }
- else {
- syslog(3, "Cannot write key: %s\n", CTDL_KEY_PATH);
- ShutDownWebcit();
- exit(0);
- }
- RSA_free(rsa);
- }
- }
-
- /*
- * 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(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) {
-
- /** 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 */
-
- /*
- * We used to add these fields to the subject, but
- * now we don't. Someone doing this for real isn't
- * going to use the webcit-generated CSR anyway.
- *
- X509_NAME_add_entry_by_txt(name, "C",
- MBSTRING_ASC, "US", -1, -1, 0);
- *
- X509_NAME_add_entry_by_txt(name, "ST",
- MBSTRING_ASC, "New York", -1, -1, 0);
- *
- X509_NAME_add_entry_by_txt(name, "L",
- MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
- */
-
- 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(3, "X509_REQ_sign(): error\n");
- }
- 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(3, "Cannot write key: %s\n", CTDL_CSR_PATH);
- ShutDownWebcit();
- exit(0);
- }
- }
-
- X509_REQ_free(req);
- }
- }