- if (!(BN_hex2bn(&(dh->p), DH_P))) {
- lprintf(3, "init_ssl() can't assign DH_P: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_CTX_free(ssl_ctx);
- ssl_ctx = NULL;
- return;
- }
- if (!(BN_hex2bn(&(dh->g), DH_G))) {
- lprintf(3, "init_ssl() can't assign DH_G: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_CTX_free(ssl_ctx);
- ssl_ctx = NULL;
- return;
- }
- dh->length = DH_L;
- SSL_CTX_set_tmp_dh(ssl_ctx, dh);
- DH_free(dh);
-
- /* 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);
- 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) {
-
- /* 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) {
-
- /* Set the public key */
- X509_REQ_set_pubkey(req, pk);
- X509_REQ_set_version(req, 0L);
-
- name = X509_REQ_get_subject_name(req);