support --options also in ipsec pki --self
This commit is contained in:
+62
-48
@@ -496,18 +496,21 @@ static int self(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
key_type_t type = KEY_RSA;
|
key_type_t type = KEY_RSA;
|
||||||
hash_algorithm_t digest = HASH_SHA1;
|
hash_algorithm_t digest = HASH_SHA1;
|
||||||
certificate_t *cert;
|
certificate_t *cert = NULL;
|
||||||
private_key_t *private;
|
private_key_t *private = NULL;
|
||||||
public_key_t *public;
|
public_key_t *public = NULL;
|
||||||
char *file = NULL, *dn = NULL, *hex = NULL;
|
char *file = NULL, *dn = NULL, *hex = NULL, *error = NULL;
|
||||||
identification_t *id;
|
identification_t *id = NULL;
|
||||||
linked_list_t *san;
|
linked_list_t *san;
|
||||||
int lifetime = 1080;
|
int lifetime = 1080;
|
||||||
chunk_t serial, encoding;
|
chunk_t serial = chunk_empty;
|
||||||
|
chunk_t encoding = chunk_empty;
|
||||||
time_t not_before, not_after;
|
time_t not_before, not_after;
|
||||||
x509_flag_t flags = 0;
|
x509_flag_t flags = 0;
|
||||||
|
options_t *options;
|
||||||
|
|
||||||
struct option long_opts[] = {
|
struct option long_opts[] = {
|
||||||
|
{ "options", required_argument, NULL, '+' },
|
||||||
{ "type", required_argument, NULL, 't' },
|
{ "type", required_argument, NULL, 't' },
|
||||||
{ "in", required_argument, NULL, 'i' },
|
{ "in", required_argument, NULL, 'i' },
|
||||||
{ "dn", required_argument, NULL, 'd' },
|
{ "dn", required_argument, NULL, 'd' },
|
||||||
@@ -519,12 +522,20 @@ static int self(int argc, char *argv[])
|
|||||||
{ 0,0,0,0 }
|
{ 0,0,0,0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
options = options_create();
|
||||||
san = linked_list_create();
|
san = linked_list_create();
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
switch (getopt_long(argc, argv, "", long_opts, NULL))
|
switch (getopt_long(argc, argv, "", long_opts, NULL))
|
||||||
{
|
{
|
||||||
|
case '+':
|
||||||
|
if (!options->from(options, optarg, &argc, &argv, optind))
|
||||||
|
{
|
||||||
|
error = "invalid options file";
|
||||||
|
goto usage;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
case 't':
|
case 't':
|
||||||
if (streq(optarg, "rsa"))
|
if (streq(optarg, "rsa"))
|
||||||
{
|
{
|
||||||
@@ -536,16 +547,16 @@ static int self(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "invalid input type";
|
||||||
return usage("invalid input type");
|
goto usage;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
case 'h':
|
case 'h':
|
||||||
digest = get_digest(optarg);
|
digest = get_digest(optarg);
|
||||||
if (digest == HASH_UNKNOWN)
|
if (digest == HASH_UNKNOWN)
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "invalid --digest type";
|
||||||
return usage("invalid --digest type");
|
goto usage;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
case 'i':
|
case 'i':
|
||||||
@@ -561,7 +572,8 @@ static int self(int argc, char *argv[])
|
|||||||
lifetime = atoi(optarg);
|
lifetime = atoi(optarg);
|
||||||
if (!lifetime)
|
if (!lifetime)
|
||||||
{
|
{
|
||||||
return usage("invalid --lifetime value");
|
error = "invalid --lifetime value";
|
||||||
|
goto usage;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
case 's':
|
case 's':
|
||||||
@@ -573,24 +585,22 @@ static int self(int argc, char *argv[])
|
|||||||
case EOF:
|
case EOF:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "invalid --self option";
|
||||||
return usage("invalid --self option");
|
goto usage;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dn)
|
if (!dn)
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "--dn is required";
|
||||||
return usage("--dn is required");
|
goto usage;
|
||||||
}
|
}
|
||||||
id = identification_create_from_string(dn);
|
id = identification_create_from_string(dn);
|
||||||
if (id->get_type(id) != ID_DER_ASN1_DN)
|
if (id->get_type(id) != ID_DER_ASN1_DN)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "supplied --dn is not a distinguished name";
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
goto end;
|
||||||
fprintf(stderr, "supplied --dn is not a distinguished name\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
if (file)
|
if (file)
|
||||||
{
|
{
|
||||||
@@ -604,19 +614,14 @@ static int self(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
if (!private)
|
if (!private)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "parsing private key failed";
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
goto end;
|
||||||
fprintf(stderr, "parsing private key failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
public = private->get_public_key(private);
|
public = private->get_public_key(private);
|
||||||
if (!public)
|
if (!public)
|
||||||
{
|
{
|
||||||
private->destroy(private);
|
error = "extracting public key failed";
|
||||||
id->destroy(id);
|
goto end;
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "extracting public key failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
if (hex)
|
if (hex)
|
||||||
{
|
{
|
||||||
@@ -625,14 +630,11 @@ static int self(int argc, char *argv[])
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||||
|
|
||||||
if (!rng)
|
if (!rng)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "no random number generator found";
|
||||||
private->destroy(private);
|
goto end;
|
||||||
public->destroy(public);
|
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "no random number generator found\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
rng->allocate_bytes(rng, 8, &serial);
|
rng->allocate_bytes(rng, 8, &serial);
|
||||||
rng->destroy(rng);
|
rng->destroy(rng);
|
||||||
@@ -645,32 +647,44 @@ static int self(int argc, char *argv[])
|
|||||||
BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
|
BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
|
||||||
BUILD_DIGEST_ALG, digest, BUILD_X509_FLAG, flags,
|
BUILD_DIGEST_ALG, digest, BUILD_X509_FLAG, flags,
|
||||||
BUILD_SUBJECT_ALTNAMES, san, BUILD_END);
|
BUILD_SUBJECT_ALTNAMES, san, BUILD_END);
|
||||||
private->destroy(private);
|
|
||||||
public->destroy(public);
|
|
||||||
id->destroy(id);
|
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
free(serial.ptr);
|
|
||||||
if (!cert)
|
if (!cert)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "generating certificate failed\n");
|
error = "generating certificate failed";
|
||||||
return 1;
|
goto end;
|
||||||
}
|
}
|
||||||
encoding = cert->get_encoding(cert);
|
encoding = cert->get_encoding(cert);
|
||||||
if (!encoding.ptr)
|
if (!encoding.ptr)
|
||||||
{
|
{
|
||||||
cert->destroy(cert);
|
error = "encoding certificate failed";
|
||||||
fprintf(stderr, "encoding certificate failed\n");
|
goto end;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
cert->destroy(cert);
|
|
||||||
if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
|
if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "writing certificate key failed\n");
|
error = "writing certificate key failed";
|
||||||
free(encoding.ptr);
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
DESTROY_IF(id);
|
||||||
|
DESTROY_IF(cert);
|
||||||
|
DESTROY_IF(public);
|
||||||
|
DESTROY_IF(private);
|
||||||
|
san->destroy_offset(san, offsetof(identification_t, destroy));
|
||||||
|
options->destroy(options);
|
||||||
|
free(encoding.ptr);
|
||||||
|
free(serial.ptr);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", error);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
free(encoding.ptr);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
usage:
|
||||||
|
san->destroy_offset(san, offsetof(identification_t, destroy));
|
||||||
|
options->destroy(options);
|
||||||
|
return usage(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user