--options reads command line options from file
This commit is contained in:
+81
-81
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include <library.h>
|
#include <library.h>
|
||||||
#include <utils/linked_list.h>
|
#include <utils/linked_list.h>
|
||||||
|
#include <utils/optionsfrom.h>
|
||||||
#include <credentials/keys/private_key.h>
|
#include <credentials/keys/private_key.h>
|
||||||
#include <credentials/certificates/certificate.h>
|
#include <credentials/certificates/certificate.h>
|
||||||
#include <credentials/certificates/x509.h>
|
#include <credentials/certificates/x509.h>
|
||||||
@@ -61,7 +62,7 @@ static int usage(char *error)
|
|||||||
fprintf(out, " pki --self [--in file] [--type rsa|ecdsa]\n");
|
fprintf(out, " pki --self [--in file] [--type rsa|ecdsa]\n");
|
||||||
fprintf(out, " --dn distinguished-name [--san subjectAltName]+\n");
|
fprintf(out, " --dn distinguished-name [--san subjectAltName]+\n");
|
||||||
fprintf(out, " [--lifetime days] [--serial hex] [--ca]\n");
|
fprintf(out, " [--lifetime days] [--serial hex] [--ca]\n");
|
||||||
fprintf(out, " [--digest md5|sha1|sha224|sha256|sha384|sha512\n");
|
fprintf(out, " [--digest md5|sha1|sha224|sha256|sha384|sha512]\n");
|
||||||
fprintf(out, " create a self signed certificate\n");
|
fprintf(out, " create a self signed certificate\n");
|
||||||
fprintf(out, " --in private key input file, default: stdin\n");
|
fprintf(out, " --in private key input file, default: stdin\n");
|
||||||
fprintf(out, " --type type of input key, default: rsa\n");
|
fprintf(out, " --type type of input key, default: rsa\n");
|
||||||
@@ -75,7 +76,8 @@ static int usage(char *error)
|
|||||||
fprintf(out, " --cacert file --cakey file\n");
|
fprintf(out, " --cacert file --cakey file\n");
|
||||||
fprintf(out, " --dn subject-dn [--san subjectAltName]+\n");
|
fprintf(out, " --dn subject-dn [--san subjectAltName]+\n");
|
||||||
fprintf(out, " [--lifetime days] [--serial hex] [--ca]\n");
|
fprintf(out, " [--lifetime days] [--serial hex] [--ca]\n");
|
||||||
fprintf(out, " [--digest md5|sha1|sha224|sha256|sha384|sha512\n");
|
fprintf(out, " [--digest md5|sha1|sha224|sha256|sha384|sha512]\n");
|
||||||
|
fprintf(out, " [--options file]\n");
|
||||||
fprintf(out, " issue a certificate using a CA certificate and key\n");
|
fprintf(out, " issue a certificate using a CA certificate and key\n");
|
||||||
fprintf(out, " --in public key/request file to issue, default: stdin\n");
|
fprintf(out, " --in public key/request file to issue, default: stdin\n");
|
||||||
fprintf(out, " --type type of input, default: pub\n");
|
fprintf(out, " --type type of input, default: pub\n");
|
||||||
@@ -87,6 +89,7 @@ static int usage(char *error)
|
|||||||
fprintf(out, " --serial serial number in hex, default: random\n");
|
fprintf(out, " --serial serial number in hex, default: random\n");
|
||||||
fprintf(out, " --ca include CA basicConstraint, default: no\n");
|
fprintf(out, " --ca include CA basicConstraint, default: no\n");
|
||||||
fprintf(out, " --digest digest for signature creation, default: sha1\n");
|
fprintf(out, " --digest digest for signature creation, default: sha1\n");
|
||||||
|
fprintf(out, " --options read command line options from file\n");
|
||||||
fprintf(out, " pki --verify [--in file] [--ca file]\n");
|
fprintf(out, " pki --verify [--in file] [--ca file]\n");
|
||||||
fprintf(out, " verify a certificate using the CA certificate\n");
|
fprintf(out, " verify a certificate using the CA certificate\n");
|
||||||
fprintf(out, " --in x509 certifcate to verify, default: stdin\n");
|
fprintf(out, " --in x509 certifcate to verify, default: stdin\n");
|
||||||
@@ -676,19 +679,23 @@ static int self(int argc, char *argv[])
|
|||||||
static int issue(int argc, char *argv[])
|
static int issue(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
hash_algorithm_t digest = HASH_SHA1;
|
hash_algorithm_t digest = HASH_SHA1;
|
||||||
certificate_t *cert, *ca;
|
certificate_t *cert = NULL, *ca =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, *cacert = NULL, *cakey = NULL;
|
char *file = NULL, *dn = NULL, *hex = NULL, *cacert = NULL, *cakey = NULL;
|
||||||
identification_t *id;
|
char *error = NULL;
|
||||||
|
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;
|
||||||
x509_t *x509;
|
x509_t *x509;
|
||||||
|
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' },
|
||||||
{ "cacert", required_argument, NULL, 'c' },
|
{ "cacert", required_argument, NULL, 'c' },
|
||||||
@@ -702,25 +709,33 @@ static int issue(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, "pub"))
|
if (!streq(optarg, "pub"))
|
||||||
{
|
{
|
||||||
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':
|
||||||
@@ -742,8 +757,8 @@ static int issue(int argc, char *argv[])
|
|||||||
lifetime = atoi(optarg);
|
lifetime = atoi(optarg);
|
||||||
if (!lifetime)
|
if (!lifetime)
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "invalid --lifetime value";
|
||||||
return usage("invalid --lifetime value");
|
goto usage;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
case 's':
|
case 's':
|
||||||
@@ -755,86 +770,67 @@ static int issue(int argc, char *argv[])
|
|||||||
case EOF:
|
case EOF:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "invalid --issue option";
|
||||||
return usage("invalid --issue 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;
|
||||||
}
|
}
|
||||||
if (!cacert)
|
if (!cacert)
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "--cacert is required";
|
||||||
return usage("--cacert is required");
|
goto usage;
|
||||||
}
|
}
|
||||||
if (!cakey)
|
if (!cakey)
|
||||||
{
|
{
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
error = "--cakey is required";
|
||||||
return usage("--cakey 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;
|
|
||||||
}
|
}
|
||||||
ca = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
|
ca = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
|
||||||
BUILD_FROM_FILE, cacert, BUILD_END);
|
BUILD_FROM_FILE, cacert, BUILD_END);
|
||||||
if (!ca)
|
if (!ca)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "parsing CA certificate failed";
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
goto end;
|
||||||
fprintf(stderr, "parsing CA certificate failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
x509 = (x509_t*)ca;
|
x509 = (x509_t*)ca;
|
||||||
if (!(x509->get_flags(x509) & X509_CA))
|
if (!(x509->get_flags(x509) & X509_CA))
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "CA certificate misses CA basicConstraint";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "CA certificate misses CA basicConstraint\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public = ca->get_public_key(ca);
|
public = ca->get_public_key(ca);
|
||||||
if (!public)
|
if (!public)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "extracting CA certificate public key failed";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "extracting CA certificate public key failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
|
private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
|
||||||
public->get_type(public),
|
public->get_type(public),
|
||||||
BUILD_FROM_FILE, cakey, BUILD_END);
|
BUILD_FROM_FILE, cakey, BUILD_END);
|
||||||
if (!private)
|
if (!private)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "parsing CA private key failed";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
public->destroy(public);
|
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "parsing CA private key failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
if (!private->belongs_to(private, public))
|
if (!private->belongs_to(private, public))
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "CA private key does not match CA certificate";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
public->destroy(public);
|
|
||||||
private->destroy(private);
|
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "CA private key does not match CA certificate\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
public->destroy(public);
|
|
||||||
if (file)
|
if (file)
|
||||||
{
|
{
|
||||||
public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
|
public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
|
||||||
@@ -847,12 +843,8 @@ static int issue(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
if (!public)
|
if (!public)
|
||||||
{
|
{
|
||||||
id->destroy(id);
|
error = "parsing public key failed";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
private->destroy(private);
|
|
||||||
san->destroy_offset(san, offsetof(identification_t, destroy));
|
|
||||||
fprintf(stderr, "parsing public key failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hex)
|
if (hex)
|
||||||
@@ -862,15 +854,11 @@ static int issue(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";
|
||||||
ca->destroy(ca);
|
goto end;
|
||||||
private->destroy(private);
|
|
||||||
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);
|
||||||
@@ -884,33 +872,45 @@ static int issue(int argc, char *argv[])
|
|||||||
BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
|
BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
|
||||||
BUILD_SUBJECT_ALTNAMES, san, BUILD_X509_FLAG, flags,
|
BUILD_SUBJECT_ALTNAMES, san, BUILD_X509_FLAG, flags,
|
||||||
BUILD_END);
|
BUILD_END);
|
||||||
private->destroy(private);
|
|
||||||
public->destroy(public);
|
|
||||||
ca->destroy(ca);
|
|
||||||
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(ca);
|
||||||
|
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