write_file(path, len, pem);
}
-static void gen_key(mbedtls_pk_context *key, int ksize, int exp, bool pem)
+static void gen_rsa_key(mbedtls_pk_context *key, int ksize, int exp)
{
mbedtls_pk_init(key);
fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
}
}
+static void gen_ec_key(mbedtls_pk_context *key, const char *curve_name)
+{
+ const mbedtls_ecp_curve_info *curve_info;
+
+ mbedtls_pk_init(key);
+ fprintf(stderr, "Generating EC private key on curve %s\n", curve_name);
+ mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
+ curve_info = mbedtls_ecp_curve_info_from_name(curve_name);
+ if (!curve_info) {
+ fprintf(stderr, "unkown curve name \"%s\" given\n", curve_name);
+ exit(1);
+ }
+ if (mbedtls_ecp_gen_key(curve_info->grp_id, mbedtls_pk_ec(*key), _urandom, NULL)) {
+ fprintf(stderr, "error: key generation failed\n");
+ exit(1);
+ }
+}
+
int rsakey(char **arg)
{
mbedtls_pk_context key;
if (*arg)
ksize = (unsigned int)atoi(*arg);
- gen_key(&key, ksize, exp, pem);
+ gen_rsa_key(&key, ksize, exp);
write_key(&key, path, pem);
mbedtls_pk_free(&key);
time_t from = time(NULL), to;
char fstr[20], tstr[20], sstr[17];
int len;
+ bool rsa = false;
+ bool ec = false;
+ char *ec_curve = NULL;
while (*arg && **arg == '-') {
if (!strcmp(*arg, "-der")) {
pem = false;
} else if (!strcmp(*arg, "-newkey") && arg[1]) {
- if (strncmp(arg[1], "rsa:", 4)) {
+ if (!strncmp(arg[1], "rsa:", 4)) {
+ rsa = true;
+ ksize = (unsigned int)atoi(arg[1] + 4);
+ arg++;
+ } else if (!strncmp(arg[1], "ec:", 3)) {
+ ec = true;
+ ec_curve = arg[1] + 3;
+ arg++;
+ } else {
fprintf(stderr, "error: invalid algorithm");
return 1;
}
- ksize = (unsigned int)atoi(arg[1] + 4);
- arg++;
} else if (!strcmp(*arg, "-days") && arg[1]) {
days = (unsigned int)atoi(arg[1]);
arg++;
arg++;
}
- gen_key(&key, ksize, exp, pem);
+ if (rsa)
+ gen_rsa_key(&key, ksize, exp);
+ else if (ec)
+ gen_ec_key(&key, ec_curve);
+ else
+ exit(1);
if (keypath)
write_key(&key, keypath, pem);