2014-12-05 09:41:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Tobias Brunner
|
2018-05-23 14:04:50 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2014-12-05 09:41:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "pki.h"
|
|
|
|
|
|
|
|
#include <credentials/certificates/x509.h>
|
|
|
|
#include <credentials/containers/pkcs12.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show info about PKCS#12 container
|
|
|
|
*/
|
|
|
|
static int show(pkcs12_t *pkcs12)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
certificate_t *cert;
|
|
|
|
private_key_t *key;
|
2014-12-09 10:40:12 +00:00
|
|
|
int index = 1;
|
2014-12-05 09:41:07 +00:00
|
|
|
|
2014-12-09 10:40:12 +00:00
|
|
|
printf("Certificates:\n");
|
2014-12-05 09:41:07 +00:00
|
|
|
enumerator = pkcs12->create_cert_enumerator(pkcs12);
|
|
|
|
while (enumerator->enumerate(enumerator, &cert))
|
|
|
|
{
|
|
|
|
x509_t *x509 = (x509_t*)cert;
|
|
|
|
|
|
|
|
if (x509->get_flags(x509) & X509_CA)
|
|
|
|
{
|
2014-12-09 10:40:12 +00:00
|
|
|
printf("[%2d] \"%Y\" (CA)\n", index++, cert->get_subject(cert));
|
2014-12-05 09:41:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-09 10:40:12 +00:00
|
|
|
printf("[%2d] \"%Y\"\n", index++, cert->get_subject(cert));
|
2014-12-05 09:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2014-12-09 10:40:12 +00:00
|
|
|
|
|
|
|
printf("Private keys:\n");
|
2014-12-05 09:41:07 +00:00
|
|
|
enumerator = pkcs12->create_key_enumerator(pkcs12);
|
|
|
|
while (enumerator->enumerate(enumerator, &key))
|
|
|
|
{
|
2014-12-09 10:40:12 +00:00
|
|
|
printf("[%2d] %N %d bits\n", index++, key_type_names,
|
|
|
|
key->get_type(key), key->get_keysize(key));
|
2014-12-05 09:41:07 +00:00
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:54:03 +00:00
|
|
|
static int export(pkcs12_t *pkcs12, int index, char *outform)
|
|
|
|
{
|
|
|
|
cred_encoding_type_t form;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
certificate_t *cert;
|
|
|
|
private_key_t *key;
|
|
|
|
chunk_t encoding;
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
enumerator = pkcs12->create_cert_enumerator(pkcs12);
|
|
|
|
while (enumerator->enumerate(enumerator, &cert))
|
|
|
|
{
|
|
|
|
if (i++ == index)
|
|
|
|
{
|
|
|
|
form = CERT_ASN1_DER;
|
|
|
|
if (outform && !get_form(outform, &form, CRED_CERTIFICATE))
|
|
|
|
{
|
2014-12-19 15:30:10 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2014-12-09 11:54:03 +00:00
|
|
|
return command_usage("invalid output format");
|
|
|
|
}
|
|
|
|
if (cert->get_encoding(cert, form, &encoding))
|
|
|
|
{
|
|
|
|
set_file_mode(stdout, form);
|
|
|
|
if (fwrite(encoding.ptr, encoding.len, 1, stdout) == 1)
|
|
|
|
{
|
|
|
|
free(encoding.ptr);
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(encoding.ptr);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "certificate export failed\n");
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
|
|
|
|
enumerator = pkcs12->create_key_enumerator(pkcs12);
|
|
|
|
while (enumerator->enumerate(enumerator, &key))
|
|
|
|
{
|
|
|
|
if (i++ == index)
|
|
|
|
{
|
|
|
|
form = PRIVKEY_ASN1_DER;
|
|
|
|
if (outform && !get_form(outform, &form, CRED_PRIVATE_KEY))
|
|
|
|
{
|
2014-12-19 15:30:10 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2014-12-09 11:54:03 +00:00
|
|
|
return command_usage("invalid output format");
|
|
|
|
}
|
|
|
|
if (key->get_encoding(key, form, &encoding))
|
|
|
|
{
|
|
|
|
set_file_mode(stdout, form);
|
|
|
|
if (fwrite(encoding.ptr, encoding.len, 1, stdout) == 1)
|
|
|
|
{
|
|
|
|
free(encoding.ptr);
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(encoding.ptr);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "private key export failed\n");
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
|
|
|
|
fprintf(stderr, "invalid index %d\n", index);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-05 09:41:07 +00:00
|
|
|
/**
|
|
|
|
* Handle PKCs#12 containers
|
|
|
|
*/
|
|
|
|
static int pkcs12()
|
|
|
|
{
|
2014-12-09 11:54:03 +00:00
|
|
|
char *arg, *file = NULL, *outform = NULL;
|
2014-12-05 09:41:07 +00:00
|
|
|
pkcs12_t *p12 = NULL;
|
2014-12-09 11:54:03 +00:00
|
|
|
int res = 1, index = 0;
|
2014-12-05 09:41:07 +00:00
|
|
|
enum {
|
|
|
|
OP_NONE,
|
2014-12-09 10:40:12 +00:00
|
|
|
OP_LIST,
|
2014-12-09 11:54:03 +00:00
|
|
|
OP_EXPORT,
|
2014-12-05 09:41:07 +00:00
|
|
|
} op = OP_NONE;
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
switch (command_getopt(&arg))
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
return command_usage(NULL);
|
|
|
|
case 'i':
|
|
|
|
file = arg;
|
|
|
|
continue;
|
2014-12-09 10:40:12 +00:00
|
|
|
case 'l':
|
2014-12-05 09:41:07 +00:00
|
|
|
if (op != OP_NONE)
|
|
|
|
{
|
|
|
|
goto invalid;
|
|
|
|
}
|
2014-12-09 10:40:12 +00:00
|
|
|
op = OP_LIST;
|
2014-12-05 09:41:07 +00:00
|
|
|
continue;
|
2014-12-09 11:54:03 +00:00
|
|
|
case 'e':
|
|
|
|
if (op != OP_NONE)
|
|
|
|
{
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
op = OP_EXPORT;
|
|
|
|
index = atoi(arg);
|
|
|
|
continue;
|
|
|
|
case 'f':
|
|
|
|
outform = arg;
|
|
|
|
continue;
|
2014-12-05 09:41:07 +00:00
|
|
|
case EOF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
invalid:
|
|
|
|
return command_usage("invalid --pkcs12 option");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
p12 = lib->creds->create(lib->creds, CRED_CONTAINER, CONTAINER_PKCS12,
|
|
|
|
BUILD_FROM_FILE, file, BUILD_END);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunk_t chunk;
|
|
|
|
|
|
|
|
set_file_mode(stdin, CERT_ASN1_DER);
|
|
|
|
if (!chunk_from_fd(0, &chunk))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "reading input failed: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p12 = lib->creds->create(lib->creds, CRED_CONTAINER, CONTAINER_PKCS12,
|
|
|
|
BUILD_BLOB, chunk, BUILD_END);
|
|
|
|
free(chunk.ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p12)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "reading input failed!\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:54:03 +00:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OP_LIST:
|
|
|
|
res = show(p12);
|
|
|
|
break;
|
|
|
|
case OP_EXPORT:
|
|
|
|
res = export(p12, index, outform);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p12->container.destroy(&p12->container);
|
|
|
|
return command_usage(NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-05 09:41:07 +00:00
|
|
|
end:
|
|
|
|
if (p12)
|
|
|
|
{
|
|
|
|
p12->container.destroy(&p12->container);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the command.
|
|
|
|
*/
|
|
|
|
static void __attribute__ ((constructor))reg()
|
|
|
|
{
|
|
|
|
command_register((command_t) {
|
|
|
|
pkcs12, 'u', "pkcs12", "PKCS#12 functions",
|
2014-12-09 11:54:03 +00:00
|
|
|
{"--export index|--list [--in file]",
|
2014-12-19 15:30:47 +00:00
|
|
|
"[--outform der|pem]"},
|
2014-12-05 09:41:07 +00:00
|
|
|
{
|
|
|
|
{"help", 'h', 0, "show usage information"},
|
|
|
|
{"in", 'i', 1, "input file, default: stdin"},
|
2014-12-09 10:40:12 +00:00
|
|
|
{"list", 'l', 0, "list certificates and keys"},
|
2014-12-09 11:54:03 +00:00
|
|
|
{"export", 'e', 1, "export the credential with the given index"},
|
2014-12-19 15:30:47 +00:00
|
|
|
{"outform", 'f', 1, "encoding of exported credentials, default: der"},
|
2014-12-05 09:41:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|