- mkdir(ctdl_key_dir, 0700); // If the keys directory does not exist, create it
- generate_key(file_crpt_file_key); // If a private key does not exist, create it
-
- // 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(file_crpt_file_cer, R_OK) != 0) && (access(file_crpt_file_csr, R_OK) != 0) ) {
- syslog(LOG_INFO, "crypto: generating a generic 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(file_crpt_file_key, "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);
-
- // Tell it who we are
- X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned const char *)"ZZ", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, (unsigned const char *)"The World", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, (unsigned const char *)"My Location", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned const char *)"Generic certificate", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned const char *)"Citadel server", -1, -1, 0);
- X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned const char *)"*", -1, -1, 0);
- X509_REQ_set_subject_name(req, name);
-
- // Sign the CSR
- if (!X509_REQ_sign(req, pk, EVP_md5())) {
- syslog(LOG_ERR, "crypto: X509_REQ_sign(): error");
- }
- else {
- // Write it to disk
- fp = fopen(file_crpt_file_csr, "w");
- if (fp != NULL) {
- chmod(file_crpt_file_csr, 0600);
- PEM_write_X509_REQ(fp, req);
- fclose(fp);
- }
- }
-
- X509_REQ_free(req);
- }
- }
- RSA_free(rsa);
- }
- else {
- syslog(LOG_ERR, "crypto: unable to read private key.");
- }
- }
-
- // Generate a self-signed certificate if we don't have one.
- if (access(file_crpt_file_cer, R_OK) != 0) {
- syslog(LOG_INFO, "crypto: generating a generic self-signed certificate.");
-
- // Same deal as before: always read the key from disk because
- // it may or may not have just been generated.
- fp = fopen(file_crpt_file_key, "r");
- if (fp) {
- rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
- fclose(fp);
- }
-
- // This also holds true for the CSR.
- req = NULL;
- cer = NULL;
- pk = NULL;
- if (rsa) {
- if (pk=EVP_PKEY_new(), pk != NULL) {
- EVP_PKEY_assign_RSA(pk, rsa);
- }
-
- 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, X509_REQ_get_subject_name(req));
- X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
- X509_gmtime_adj(X509_get_notBefore(cer),0);
- X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
- req_pkey = X509_REQ_get_pubkey(req);
- X509_set_pubkey(cer, req_pkey);
- EVP_PKEY_free(req_pkey);
-
- // Sign the cert
- if (!X509_sign(cer, pk, EVP_md5())) {
- syslog(LOG_ERR, "crypto: X509_sign() error");
- }
- else {
- // Write it to disk.
- fp = fopen(file_crpt_file_cer, "w");
- if (fp != NULL) {
- chmod(file_crpt_file_cer, 0600);
- PEM_write_X509(fp, cer);
- fclose(fp);
- }
- }
- X509_free(cer);
- }
- }
-
- RSA_free(rsa);
- }
- }
-
- // Now try to bind to the key and certificate.
- bind_to_key_and_certificate();