mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
moved the actual public key export code out into the lib (mgmt.c). cmdline client does only armor the blobs, if neccessary. Also, armored pubkey exports don't contain any comments anymore.
This commit is contained in:
204
src/keyprint.c
204
src/keyprint.c
@@ -85,13 +85,14 @@ int pcptext_infile(char *infile) {
|
||||
}
|
||||
}
|
||||
|
||||
/* FIXME: can't determine keytype by using its size */
|
||||
if(clen == PCP_RAW_PUBKEYSIZE) {
|
||||
/* public key? */
|
||||
pcp_pubkey_t *key = (pcp_pubkey_t *)bin;
|
||||
pubkey2native(key);
|
||||
if(pcp_sanitycheck_pub(key) == 0) {
|
||||
fprintf(stdout, "%s is a public key file:\n", infile);
|
||||
pcppubkey_print(key, stdout, 0);
|
||||
// pcppubkey_print(key, stdout, 0);
|
||||
free(key);
|
||||
goto tdone;
|
||||
}
|
||||
@@ -127,7 +128,7 @@ void pcptext_key(char *keyid) {
|
||||
if(p != NULL) {
|
||||
if(debug)
|
||||
pcp_dumppubkey(p);
|
||||
pcppubkey_print(p, stdout, 0);
|
||||
pcppubkey_print(p, stdout);
|
||||
}
|
||||
else {
|
||||
fatal("No key with id 0x%s found!\n", keyid);
|
||||
@@ -175,6 +176,57 @@ void pcppubkey_printlineinfo(pcp_pubkey_t *key) {
|
||||
key->owner, key->mail);
|
||||
}
|
||||
|
||||
void pcppubkey_print(pcp_pubkey_t *key, FILE* out) {
|
||||
size_t zlen;
|
||||
struct tm *c;
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
|
||||
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);
|
||||
|
||||
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");
|
||||
|
||||
free(hash);
|
||||
free(r);
|
||||
free(c);
|
||||
}
|
||||
|
||||
void pcpkey_print(pcp_key_t *key, FILE* out) {
|
||||
size_t zlen;
|
||||
@@ -214,154 +266,6 @@ void pcpkey_print(pcp_key_t *key, FILE* out) {
|
||||
free(z85encoded);
|
||||
}
|
||||
|
||||
void pcppubkey_print(pcp_pubkey_t *key, FILE* out, int pbpcompat) {
|
||||
struct tm *c;
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
|
||||
if(pbpcompat == 1) {
|
||||
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 {
|
||||
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 pbplen = crypto_sign_PUBLICKEYBYTES+crypto_box_PUBLICKEYBYTES+crypto_sign_PUBLICKEYBYTES+strlen(key->owner)+64;
|
||||
|
||||
/* we need to do the padding here, since pbp verifies the sig including the pad */
|
||||
/*
|
||||
int pad = pbplen % 4;
|
||||
if(pad > 0) {
|
||||
pad = 4 - pad;
|
||||
pbplen += pad;
|
||||
}
|
||||
*/
|
||||
|
||||
unsigned char *blob = ucmalloc(pbplen);
|
||||
|
||||
if(debug) {
|
||||
_dump(" mp", secret->edpub, crypto_sign_PUBLICKEYBYTES);
|
||||
_dump(" cp", key->pub, crypto_sign_PUBLICKEYBYTES);
|
||||
_dump(" sp", key->edpub, crypto_sign_PUBLICKEYBYTES);
|
||||
_dump("name", (unsigned char *)key->owner, strlen(key->owner));
|
||||
}
|
||||
|
||||
/* pkt = keys.sign(keys.mp+keys.sp+keys.cp+dates+keys.name, master=True) */
|
||||
memcpy(blob, secret->edpub, crypto_sign_PUBLICKEYBYTES);
|
||||
memcpy(&blob[crypto_sign_PUBLICKEYBYTES], key->edpub, crypto_sign_PUBLICKEYBYTES);
|
||||
memcpy(&blob[crypto_sign_PUBLICKEYBYTES*2], key->pub, crypto_box_PUBLICKEYBYTES);
|
||||
|
||||
struct tm *v;
|
||||
time_t vt = t + 31536000;
|
||||
v = localtime(&vt);
|
||||
|
||||
char *date = ucmalloc(65);
|
||||
|
||||
sprintf(date, "%04d-%02d-%02dT%02d:%02d:%02d.000000 %04d-%02d-%02dT%02d:%02d:%02d.000000 ",
|
||||
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);
|
||||
|
||||
memcpy(&blob[crypto_sign_PUBLICKEYBYTES+crypto_box_PUBLICKEYBYTES*2], date, 64);
|
||||
|
||||
memcpy(&blob[crypto_sign_PUBLICKEYBYTES+crypto_box_PUBLICKEYBYTES*2+64], key->owner, strlen(key->owner));
|
||||
|
||||
unsigned char *sig = pcp_ed_sign(blob, pbplen, secret);
|
||||
|
||||
if(debug)
|
||||
_dump(" sig", sig, crypto_sign_BYTES+pbplen);
|
||||
|
||||
if(sig != NULL) {
|
||||
size_t siglen = pbplen + crypto_sign_BYTES;
|
||||
size_t blen = ((siglen / 4) * 5) + siglen;
|
||||
char *b85sig = ucmalloc(blen);
|
||||
encode_85(b85sig, sig, siglen);
|
||||
fprintf(out, "%s", b85sig);
|
||||
free(b85sig);
|
||||
free(sig);
|
||||
}
|
||||
free(blob);
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pcp_dumpkey(pcp_key_t *k) {
|
||||
int i;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user