diff --git a/include/pcp/key.h b/include/pcp/key.h index 8088e6b..7b9a04d 100644 --- a/include/pcp/key.h +++ b/include/pcp/key.h @@ -105,12 +105,11 @@ struct _pcp_pubkey_t { }; // the PBP public key format +// keys.mp+keys.cp+keys.sp+keys.name struct _pbp_pubkey_t { - byte sigpub[crypto_box_PUBLICKEYBYTES]; - byte edpub[crypto_sign_PUBLICKEYBYTES]; + byte sigpub[crypto_sign_PUBLICKEYBYTES]; byte pub[crypto_box_PUBLICKEYBYTES]; - char iso_ctime[32]; - char iso_expire[32]; + byte edpub[crypto_sign_PUBLICKEYBYTES]; char name[1024]; }; diff --git a/src/keymgmt.c b/src/keymgmt.c index aaa81a1..fd0db48 100644 --- a/src/keymgmt.c +++ b/src/keymgmt.c @@ -376,7 +376,6 @@ int pcp_importsecret (vault_t *vault, FILE *in) { int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { pcp_pubkey_t *pub = NULL; if(pbpcompat == 1) { - char *date = NULL; char *parts = NULL; int pnum; pbp_pubkey_t *b = ucmalloc(sizeof(pbp_pubkey_t)); @@ -387,11 +386,29 @@ int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { size_t klen; buflen = fread(buf, 1, 2048, in); // base85 encoded - klen = (buflen / 5) * 4; - if(decode_85(bin, (char *)buf, klen) != 0) + // remove trailing newline, if any + size_t i, nlen; + nlen = buflen; + for(i=buflen; i>0; --i) { + if(buf[i] == '\n' || buf[i] == '\r') { + buf[i] = '\0'; + nlen -= 1; + } + } + klen = (nlen / 5) * 4; + + + + if(decode_85((char *)bin, (char *)buf, klen) != 0) goto errimp1; + /* + FILE *o = fopen("out", "wb+"); + fwrite(bin, 1, klen, o); + */ + + if(klen < sizeof(pbp_pubkey_t) - 1024 - crypto_sign_BYTES) { fatal("PBP key seems to be too small, maybe it's not a PBP key (got %ld, expected %ld)\n", klen, sizeof(pbp_pubkey_t) - 1024); @@ -401,16 +418,6 @@ int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { // FIXME: or use first part as sig and verify memcpy(b, &bin[crypto_sign_BYTES], klen - crypto_sign_BYTES); - // parse the date - date = ucmalloc(19); - memcpy(date, b->iso_ctime, 18); - date[19] = '\0'; - struct tm c; - if(strptime(date, "%Y-%m-%dT%H:%M:%S", &c) == NULL) { - fatal("Failed to parse creation time in PBP public key file (<%s>)\n", date); - goto errimp2; - } - // parse the name parts = strtok (b->name, "<>"); pnum = 0; @@ -424,8 +431,14 @@ int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { } free(parts); + if(strlen(b->name) == 0) { + char *owner = pcp_getstdin("Enter the name of the key owner"); + memcpy(b->name, owner, strlen(owner) + 1); + free(owner); + } + // fill in the fields - pub->ctime = (long)mktime(&c); + pub->ctime = (long)time(0); // pbp exports no ctime pub->type = PCP_KEY_TYPE_PUBLIC; pub->version = PCP_KEY_VERSION; pub->serial = arc4random(); @@ -434,16 +447,13 @@ int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { memcpy(pub->edpub, b->edpub, crypto_sign_PUBLICKEYBYTES); free(b); - free(date); free(buf); free(bin); goto kimp; - errimp2: - free(bin); errimp1: - free(date); + free(bin); free(pub); free(b); free(buf); diff --git a/src/keyprint.c b/src/keyprint.c index 5c7a2b3..c77fcd3 100644 --- a/src/keyprint.c +++ b/src/keyprint.c @@ -220,34 +220,13 @@ void pcppubkey_print(pcp_pubkey_t *key, FILE* out, int pbpcompat) { c = localtime(&t); 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) + 2 + strlen(key->mail); pbp_pubkey_t *b = ucmalloc(sizeof(pbp_pubkey_t)); memcpy(b->pub, key->pub, crypto_box_PUBLICKEYBYTES); memcpy(b->edpub, key->edpub, crypto_sign_PUBLICKEYBYTES); - memcpy(b->sigpub, key->pub, crypto_box_PUBLICKEYBYTES); + memcpy(b->sigpub, key->edpub, crypto_sign_PUBLICKEYBYTES); sprintf(b->name, "%s<%s>", key->owner, key->mail); - struct tm *v; - time_t vt = t + 31536000; - v = localtime(&vt); - - char *date = ucmalloc(65); - - sprintf(date, "%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); - - memcpy(b->iso_ctime, date, 32); - memcpy(b->iso_expire, &date[32], 32); - size_t pbplen = sizeof(pbp_pubkey_t) - (1024 - namelen); pcp_key_t *secret = NULL; @@ -266,7 +245,7 @@ void pcppubkey_print(pcp_pubkey_t *key, FILE* out, int pbpcompat) { unsigned char *sig = pcp_ed_sign((unsigned char*)b, pbplen, secret); if(sig != NULL) { size_t siglen = pbplen + crypto_sign_BYTES; - size_t blen = ((siglen / 4) * 5) + siglen + 1; + size_t blen = ((siglen / 4) * 5) + siglen; char *b85sig = ucmalloc(blen); encode_85(b85sig, sig, siglen); fprintf(out, "%s", b85sig);