- if (access(CTDL_KEY_PATH, R_OK) != 0) {
- syslog(LOG_INFO, "Generating RSA key pair.\n");
- rsa = RSA_generate_key(2048, /* modulus size */
- 65537, /* exponent */
- NULL, /* no callback */
- NULL /* no callback */
- );
- if (rsa == NULL) {
- syslog(LOG_WARNING, "Key generation failed: %s", 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(LOG_WARNING, "Cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
- unlink(CTDL_KEY_PATH);
- }
- fclose(fp);
- }
- else {
- syslog(LOG_WARNING, "Cannot write key: %s", CTDL_KEY_PATH);
- exit(1);
- }
- RSA_free(rsa);
- }
- }