+ /*
+ * Generate a key pair if we don't have one.
+ */
+ if (access(CTDL_KEY_PATH, R_OK) != 0) {
+ lprintf(3, "Generating RSA key pair.\n");
+ rsa = RSA_generate_key(1024, /* modulus size */
+ 65537, /* exponent */
+ NULL, /* no callback */
+ NULL); /* no callback */
+ if (rsa == NULL) {
+ lprintf(2, "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(2, "Cannot write key: %s\n",
+ ERR_reason_error_string(ERR_get_error()));
+ unlink(CTDL_KEY_PATH);
+ }
+ fclose(fp);
+ }
+ RSA_free(rsa);
+ }
+ }