+/*
+ Generate a public/private ECDSA keypair, and ask for a file to store
+ them in.
+*/
+static bool ecdsa_keygen() {
+ ecdsa_t key;
+ FILE *f;
+ char *filename;
+
+ fprintf(stderr, "Generating ECDSA keypair:\n");
+
+ if(!ecdsa_generate(&key)) {
+ fprintf(stderr, "Error during key generation!\n");
+ return false;
+ } else
+ fprintf(stderr, "Done.\n");
+
+ xasprintf(&filename, "%s/ecdsa_key.priv", confbase);
+ f = ask_and_open(filename, "private ECDSA key", "a");
+
+ if(!f)
+ return false;
+
+#ifdef HAVE_FCHMOD
+ /* Make it unreadable for others. */
+ fchmod(fileno(f), 0600);
+#endif
+
+ if(ftell(f))
+ fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
+
+ ecdsa_write_pem_private_key(&key, f);
+
+ fclose(f);
+ free(filename);
+
+ if(name)
+ xasprintf(&filename, "%s/hosts/%s", confbase, name);
+ else
+ xasprintf(&filename, "%s/ecdsa_key.pub", confbase);
+
+ f = ask_and_open(filename, "public ECDSA key", "a");
+
+ if(!f)
+ return false;
+
+ if(ftell(f))
+ fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
+
+ char *pubkey = ecdsa_get_base64_public_key(&key);
+ fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
+ free(pubkey);
+
+ fclose(f);
+ free(filename);
+
+ return true;
+}
+