added PBP public key import/export compatibility mode (-b --pbpcompat)

This commit is contained in:
git@daemon.de
2014-01-28 16:53:26 +01:00
parent fa9d8ed800
commit 3f1bfef581
12 changed files with 248 additions and 82 deletions

View File

@@ -91,7 +91,7 @@ int pcptext_infile(char *infile) {
pubkey2native(key);
if(pcp_sanitycheck_pub(key) == 0) {
fprintf(stdout, "%s is a public key file:\n", infile);
pcppubkey_print(key, stdout);
pcppubkey_print(key, stdout, 0);
free(key);
goto tdone;
}
@@ -127,7 +127,7 @@ void pcptext_key(char *keyid) {
if(p != NULL) {
if(debug)
pcp_dumppubkey(p);
pcppubkey_print(p, stdout);
pcppubkey_print(p, stdout, 0);
}
else {
fatal("No key with id 0x%s found!\n", keyid);
@@ -214,74 +214,139 @@ void pcpkey_print(pcp_key_t *key, FILE* out) {
free(z85encoded);
}
void pcppubkey_print(pcp_pubkey_t *key, FILE* out) {
size_t zlen;
//printf("version: %08x\n", key->version);
pubkey2be(key);
void *blob = ucmalloc(PCP_RAW_PUBKEYSIZE);
pcp_pubkeyblob(blob, key);
char *z85encoded = pcp_z85_encode((unsigned char*)blob, PCP_RAW_PUBKEYSIZE, &zlen);
pubkey2native(key);
free(blob);
void pcppubkey_print(pcp_pubkey_t *key, FILE* out, int pbpcompat) {
struct tm *c;
time_t t = (time_t)key->ctime;
c = localtime(&t);
fprintf(out, "%s\n", PCP_PUBKEY_HEADER);
if(pbpcompat == 1) {
// sign(mk, master public | cipher public | sign public | created[32] | valid[32] | name... )
// dates='{:<32}{:<32}'.format(self.created.isoformat(), self.valid.isoformat())
// fd.write(nacl.crypto_sign(self.mp+self.sp+self.cp+dates+self.name, self.ms))
// >>> dates='{:<32}{:<32}'.format(c.isoformat(), c.isoformat())
// >>> dates
// '2014-01-28T13:30:32.674394 2014-01-28T13:30:32.674394 '
size_t namelen = strlen(key->owner) + 3 + strlen(key->mail);
size_t rawsize = (crypto_box_PUBLICKEYBYTES * 2) + crypto_sign_PUBLICKEYBYTES +\
64 + namelen;
size_t pos = 0;
unsigned char *raw = ucmalloc(rawsize);
char *dates = ucmalloc(65);
char *name = ucmalloc(strlen(key->owner) + 3 + strlen(key->mail));
fprintf(out, " Generated by: %s Version %d.%d.%d\n",
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
memcpy(raw, key->pub, crypto_box_PUBLICKEYBYTES);
pos += crypto_box_PUBLICKEYBYTES;
fprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
memcpy(&raw[pos], key->edpub, crypto_sign_PUBLICKEYBYTES);
pos += crypto_sign_PUBLICKEYBYTES;
fprintf(out, " Owner: %s\n", key->owner);
fprintf(out, " Mail: %s\n", key->mail);
memcpy(&raw[pos], key->pub, crypto_box_PUBLICKEYBYTES);
pos += crypto_box_PUBLICKEYBYTES;
fprintf(out, " Key-ID: 0x%s\n", key->id);
fprintf(out, " Public-Key: %s\n", pcp_z85_encode(key->pub, 32, &zlen));
struct tm *v;
time_t vt = t + 31536000;
v = localtime(&vt);
//2004-06-14T23:34:30.
fprintf(out, " Creation Time: %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);
sprintf(dates, "%04d-%02d-%02dT%02d:%02d:%02d %04d-%02d-%02dT%02d:%02d:%02d ",
c->tm_year+1900-1, c->tm_mon+1, c->tm_mday, // wtf? why -1?
c->tm_hour, c->tm_min, c->tm_sec,
v->tm_year+1900-1, v->tm_mon+1, v->tm_mday,
v->tm_hour, v->tm_min, v->tm_sec);
unsigned char *hash = pcppubkey_getchecksum(key);
fprintf(out, " Checksum: ");
sprintf(name, "%s|%s", key->owner, key->mail);
int i;
for ( i = 0;i <15 ;++i) fprintf(out, "%02X:",(unsigned int) hash[i]);
fprintf(out, "%02X", hash[15]);
fprintf(out, "\n ");
for ( i = 16;i <31 ;++i) fprintf(out, "%02X:",(unsigned int) hash[i]);
fprintf(out, "%02X", hash[31]);
fprintf(out, "\n");
fprintf(out, " Serial Number: 0x%08X\n", key->serial);
fprintf(out, " Key Version: 0x%08X\n", key->version);
char *r = pcppubkey_get_art(key);
fprintf(out, " Random Art ID: ");
for (i=0; i<strlen(r); ++i) {
if(r[i] == '\n') {
fprintf(out, "\n ");
memcpy(&raw[pos], dates, 64);
pos += 64;
memcpy(&raw[pos], name, namelen);
pcp_key_t *secret = NULL;
secret = pcp_find_primary_secret();
if(secret == NULL) {
fatal("Could not find a secret key in vault %s!\n", vault->filename);
}
else {
fprintf(out, "%c", r[i]);
char *passphrase;
pcp_readpass(&passphrase,
"Enter passphrase to decrypt your secret key for signing the export", NULL, 1);
secret = pcpkey_decrypt(secret, passphrase);
if(secret != NULL) {
size_t siglen = rawsize + crypto_sign_BYTES;
unsigned char *sig = pcp_ed_sign(raw, rawsize, secret);
if(sig != NULL)
fwrite(sig, 1, siglen, out);
}
}
}
fprintf(out, "\n");
else {
size_t zlen;
//printf("version: %08x\n", key->version);
pubkey2be(key);
void *blob = ucmalloc(PCP_RAW_PUBKEYSIZE);
pcp_pubkeyblob(blob, key);
char *z85encoded = pcp_z85_encode((unsigned char*)blob, PCP_RAW_PUBKEYSIZE, &zlen);
pubkey2native(key);
free(blob);
fprintf(out, "%s\n", PCP_PUBKEY_HEADER);
fprintf(out, " Generated by: %s Version %d.%d.%d\n",
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
fprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
fprintf(out, " Owner: %s\n", key->owner);
fprintf(out, " Mail: %s\n", key->mail);
fprintf(out, " Key-ID: 0x%s\n", key->id);
fprintf(out, " Public-Key: %s\n", pcp_z85_encode(key->pub, 32, &zlen));
//2004-06-14T23:34:30.
fprintf(out, " Creation Time: %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);
unsigned char *hash = pcppubkey_getchecksum(key);
fprintf(out, " Checksum: ");
int i;
for ( i = 0;i <15 ;++i) fprintf(out, "%02X:",(unsigned int) hash[i]);
fprintf(out, "%02X", hash[15]);
fprintf(out, "\n ");
for ( i = 16;i <31 ;++i) fprintf(out, "%02X:",(unsigned int) hash[i]);
fprintf(out, "%02X", hash[31]);
fprintf(out, "\n");
fprintf(out, " Serial Number: 0x%08X\n", key->serial);
fprintf(out, " Key Version: 0x%08X\n", key->version);
fprintf(out, "\n%s\n", z85encoded);
fprintf(out, "%s\n", PCP_PUBKEY_FOOTER);
free(hash);
free(r);
free(z85encoded);
char *r = pcppubkey_get_art(key);
fprintf(out, " Random Art ID: ");
for (i=0; i<strlen(r); ++i) {
if(r[i] == '\n') {
fprintf(out, "\n ");
}
else {
fprintf(out, "%c", r[i]);
}
}
fprintf(out, "\n");
fprintf(out, "\n%s\n", z85encoded);
fprintf(out, "%s\n", PCP_PUBKEY_FOOTER);
free(hash);
free(r);
free(z85encoded);
}
}