- /*
- * 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) ) {
- 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);
-
- /** Tell it who we are */
-
- /* \todo whats this?
- 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);
- */
-
- X509_NAME_add_entry_by_txt(
- name, "O",
- MBSTRING_ASC,
- (unsigned char*)"Organization name",
- -1, -1, 0);
-
- X509_NAME_add_entry_by_txt(
- name, "OU",
- MBSTRING_ASC,
- (unsigned char*)"Citadel server1",
- -1, -1, 0);
-
- X509_NAME_add_entry_by_txt(
- name, "CN",
- MBSTRING_ASC,
- (unsigned char*)"*", -1, -1, 0);
-
- X509_REQ_set_subject_name(req, name);
-
- /** Sign the CSR */
- if (!X509_REQ_sign(req, pk, EVP_md5())) {
- lprintf(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 {
- lprintf(3, "Cannot write key: %s\n", CTDL_CSR_PATH);
- ShutDownWebcit();
- exit(0);
- }
- }
-
- X509_REQ_free(req);
- }
- }