added yaml and perl pubkey export formats (c and python will follow)

This commit is contained in:
git@daemon.de
2014-02-13 17:05:22 +01:00
parent 1c46de629b
commit 7408ebd90c
4 changed files with 116 additions and 4 deletions

View File

@@ -190,8 +190,13 @@ typedef struct _pcp_ks_bundle_t pcp_ks_bundle_t;
/* pubkey export formats */ /* pubkey export formats */
#define EXP_FORMAT_NATIVE 0x01 #define EXP_FORMAT_NATIVE 1
#define EXP_FORMAT_PBP 0x03 #define EXP_FORMAT_PBP 2
#define EXP_FORMAT_YAML 3
#define EXP_FORMAT_C 4
#define EXP_FORMAT_PY 5
#define EXP_FORMAT_PERL 6
/* export self signed public key from master secret */ /* export self signed public key from master secret */
Buffer *pcp_export_rfc_pub (pcp_key_t *sk); Buffer *pcp_export_rfc_pub (pcp_key_t *sk);
@@ -202,6 +207,12 @@ Buffer *pcp_export_rfc_pub (pcp_key_t *sk);
/* export public key in pbp format */ /* export public key in pbp format */
Buffer *pcp_export_pbp_pub(pcp_key_t *sk); Buffer *pcp_export_pbp_pub(pcp_key_t *sk);
/* export public key in yaml format */
Buffer *pcp_export_yaml_pub(pcp_key_t *sk);
/* export public key in perl format */
Buffer *pcp_export_perl_pub(pcp_key_t *sk);
/* export secret key */ /* export secret key */
Buffer *pcp_export_secret(pcp_key_t *sk, char *passphrase); Buffer *pcp_export_secret(pcp_key_t *sk, char *passphrase);

View File

@@ -373,6 +373,83 @@ pcp_ks_bundle_t *pcp_import_pub_pbp(Buffer *blob) {
return NULL; return NULL;
} }
Buffer *pcp_export_yaml_pub(pcp_key_t *sk) {
Buffer *b = buffer_new_str("yamlbuf");
struct tm *c;
time_t t = time(0);
c = localtime(&t);
buffer_add_str(b, "#\n# YAML export of public key\n");
buffer_add_str(b, "# Generated on: %04d-%02d-%02dT%02d:%02d:%02d\n",
c->tm_year+1900, c->tm_mon+1, c->tm_mday,
c->tm_hour, c->tm_min, c->tm_sec);
buffer_add_str(b, "---\n");
buffer_add_str(b, "id: %s\n", sk->id);
buffer_add_str(b, "owner: %s\n", sk->owner);
buffer_add_str(b, "mail: %s\n", sk->mail);
buffer_add_str(b, "ctime: %ld\n", (long int)sk->ctime);
buffer_add_str(b, "version: %08x\n", sk->version);
buffer_add_str(b, "serial: %08x\n", sk->serial);
buffer_add_str(b, "type: public\n");
buffer_add_str(b, "cryptpub: "); buffer_add_hex(b, sk->pub, 32); buffer_add_str(b, "\n");
buffer_add_str(b, "sigpub: "); buffer_add_hex(b, sk->edpub, 32); buffer_add_str(b, "\n");
buffer_add_str(b, "masterpub: "); buffer_add_hex(b, sk->masterpub, 32); buffer_add_str(b, "\n");
return b;
}
Buffer *pcp_export_perl_pub(pcp_key_t *sk) {
Buffer *b = buffer_new_str("perlbuf");
struct tm *c;
time_t t = time(0);
c = localtime(&t);
size_t i;
buffer_add_str(b, "#\n# Perl export of public key\n");
buffer_add_str(b, "# Generated on: %04d-%02d-%02dT%02d:%02d:%02d\n",
c->tm_year+1900, c->tm_mon+1, c->tm_mday,
c->tm_hour, c->tm_min, c->tm_sec);
buffer_add_str(b, "# \nmy %%key = (\n");
buffer_add_str(b, " id => \"%s\",\n", sk->id);
buffer_add_str(b, " owner => \"%s\",\n", sk->owner);
buffer_add_str(b, " mail => \"%s\",\n", sk->mail);
buffer_add_str(b, " ctime => %ld,\n", (long int)sk->ctime);
buffer_add_str(b, " version => x%08x,\n", sk->version);
buffer_add_str(b, " serial => x%08x,\n", sk->serial);
buffer_add_str(b, " type => \"public\",\n");
buffer_add_str(b, " cryptpub => [");
for (i=0; i<31; ++i) {
buffer_add_str(b, "x%02x,", sk->pub[i]);
if(i % 8 == 7 && i > 0)
buffer_add_str(b, "\n ");
}
buffer_add_str(b, "x%02x],\n", sk->pub[31]);
buffer_add_str(b, " sigpub => [");
for (i=0; i<31; ++i) {
buffer_add_str(b, "x%02x,", sk->edpub[i]);
if(i % 8 == 7 && i > 0)
buffer_add_str(b, "\n ");
}
buffer_add_str(b, "x%02x],\n", sk->edpub[31]);
buffer_add_str(b, " masterpub => [");
for (i=0; i<31; ++i) {
buffer_add_str(b, "x%02x,", sk->masterpub[i]);
if(i % 8 == 7 && i > 0)
buffer_add_str(b, "\n ");
}
buffer_add_str(b, "x%02x]\n", sk->masterpub[31]);
buffer_add_str(b, ");\n");
return b;
}
Buffer *pcp_export_pbp_pub(pcp_key_t *sk) { Buffer *pcp_export_pbp_pub(pcp_key_t *sk) {
struct tm *v, *c; struct tm *v, *c;
unsigned char *signature = NULL; unsigned char *signature = NULL;

View File

@@ -344,7 +344,7 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
} }
if(is_foreign == 0 && sk->secret[0] == 0) { if(is_foreign == 0 && sk->secret[0] == 0 && format <= EXP_FORMAT_PBP) {
/* decrypt the secret key */ /* decrypt the secret key */
if(passwd != NULL) { if(passwd != NULL) {
sk = pcpkey_decrypt(sk, passwd); sk = pcpkey_decrypt(sk, passwd);
@@ -403,6 +403,18 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
goto errpcpexpu1; goto errpcpexpu1;
} }
} }
else if(format == EXP_FORMAT_YAML) {
exported_pk = pcp_export_yaml_pub(sk);
if(exported_pk != NULL) {
fprintf(out, "%s", buffer_get_str(exported_pk));
}
}
else if(format == EXP_FORMAT_PERL) {
exported_pk = pcp_export_perl_pub(sk);
if(exported_pk != NULL) {
fprintf(out, "%s", buffer_get_str(exported_pk));
}
}
errpcpexpu1: errpcpexpu1:
buffer_free(exported_pk); buffer_free(exported_pk);

View File

@@ -114,7 +114,7 @@ int main (int argc, char **argv) {
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
while ((opt = getopt_long(argc, argv, "klV:vdehsO:i:I:pSPRtEx:DzZr:gcymf:b1", while ((opt = getopt_long(argc, argv, "klV:vdehsO:i:I:pSPRtEx:DzZr:gcymf:b1F:",
longopts, NULL)) != -1) { longopts, NULL)) != -1) {
switch (opt) { switch (opt) {
@@ -187,6 +187,18 @@ int main (int argc, char **argv) {
else if(strncmp(optarg, "pcp", 3) == 0) { else if(strncmp(optarg, "pcp", 3) == 0) {
exportformat = EXP_FORMAT_NATIVE; exportformat = EXP_FORMAT_NATIVE;
} }
else if(strncmp(optarg, "yaml", 3) == 0) {
exportformat = EXP_FORMAT_YAML;
}
else if(strncmp(optarg, "c", 3) == 0) {
exportformat = EXP_FORMAT_C;
}
else if(strncmp(optarg, "py", 3) == 0) {
exportformat = EXP_FORMAT_PY;
}
else if(strncmp(optarg, "perl", 3) == 0) {
exportformat = EXP_FORMAT_PERL;
}
else { else {
warn("Unknown export format specified, using native\n"); warn("Unknown export format specified, using native\n");
exportformat = EXP_FORMAT_NATIVE; exportformat = EXP_FORMAT_NATIVE;