put previously global error handling and key hashes into ptx (pcp context) to make libpcp threadsafe.

This commit is contained in:
TLINDEN
2014-05-04 17:11:03 +02:00
parent d1c87d1001
commit da9891ff81
58 changed files with 1330 additions and 958 deletions

View File

@@ -34,7 +34,7 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, i
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errde3;
}
}
@@ -43,7 +43,7 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, i
out = stdout;
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not open output file %s\n", outfile);
fatal(ptx, "Could not open output file %s\n", outfile);
goto errde3;
}
}
@@ -73,15 +73,15 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, i
strncpy(passphrase, passwd, strlen(passwd));
}
symkey = pcp_scrypt(passphrase, strlen(passphrase), salt, 90);
symkey = pcp_scrypt(ptx, passphrase, strlen(passphrase), salt, 90);
free(salt);
}
else {
/* asymetric mode */
if(useid) {
HASH_FIND_STR(pcpkey_hash, id, secret);
secret = pcphash_keyexists(ptx, id);
if(secret == NULL) {
fatal("Could not find a secret key with id 0x%s in vault %s!\n",
fatal(ptx, "Could not find a secret key with id 0x%s in vault %s!\n",
id, vault->filename);
goto errde3;
}
@@ -89,7 +89,7 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, i
else {
secret = pcp_find_primary_secret();
if(secret == NULL) {
fatal("Could not find a secret key in vault %s!\n", id, vault->filename);
fatal(ptx, "Could not find a secret key in vault %s!\n", id, vault->filename);
goto errde3;
}
}
@@ -105,21 +105,21 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, i
strncpy(passphrase, passwd, strlen(passwd)+1);
}
secret = pcpkey_decrypt(secret, passphrase);
secret = pcpkey_decrypt(ptx, secret, passphrase);
if(secret == NULL)
goto errde3;
}
}
}
else {
fatal("Could not determine input file type\n");
fatal(ptx, "Could not determine input file type\n");
goto errde3;
}
if(symkey == NULL)
dlen = pcp_decrypt_stream(pin, pout, secret, NULL, verify);
dlen = pcp_decrypt_stream(ptx, pin, pout, secret, NULL, verify);
else
dlen = pcp_decrypt_stream(pin, pout, NULL, symkey, verify);
dlen = pcp_decrypt_stream(ptx, pin, pout, NULL, symkey, verify);
ps_close(pin);
ps_close(pout);
@@ -164,23 +164,22 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
byte *salt = ucmalloc(90); /* FIXME: use random salt, concat it with result afterwards */
char stsalt[] = PBP_COMPAT_SALT;
memcpy(salt, stsalt, 90);
symkey = pcp_scrypt(passphrase, strlen(passphrase), salt, 90);
symkey = pcp_scrypt(ptx, passphrase, strlen(passphrase), salt, 90);
free(salt);
}
else if(id != NULL && recipient == NULL) {
/* lookup by id */
HASH_FIND_STR(pcppubkey_hash, id, tmp);
tmp = pcphash_pubkeyexists(ptx, id);
if(tmp == NULL) {
/* self-encryption: look if its a secret one */
pcp_key_t *s = NULL;
HASH_FIND_STR(pcpkey_hash, id, s);
pcp_key_t *s = pcphash_keyexists(ptx, id);
if(s != NULL) {
tmp = pcpkey_pub_from_secret(s);
HASH_ADD_STR( pubhash, id, tmp);
self = 1;
}
else {
fatal("Could not find a public key with id 0x%s in vault %s!\n",
fatal(ptx, "Could not find a public key with id 0x%s in vault %s!\n",
id, vault->filename);
goto erren3;
}
@@ -197,7 +196,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
/* iterate through global hashlist */
/* copy matches into temporary pubhash */
plist_t *rec;
pcphash_iteratepub(tmp) {
pcphash_iteratepub(ptx, tmp) {
rec = recipient->first;
while (rec != NULL) {
_lc(rec->value);
@@ -211,7 +210,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
}
}
if(HASH_COUNT(pubhash) == 0) {
fatal("no matching key found for specified recipient(s)!\n");
fatal(ptx, "no matching key found for specified recipient(s)!\n");
goto erren3;
}
}
@@ -224,7 +223,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
#else
secret = pcp_find_primary_secret();
if(secret == NULL) {
fatal("Could not find a secret key in vault %s!\n", id, vault->filename);
fatal(ptx, "Could not find a secret key in vault %s!\n", id, vault->filename);
goto erren2;
}
@@ -239,7 +238,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
passphrase = ucmalloc(strlen(passwd)+1);
strncpy(passphrase, passwd, strlen(passwd)+1);
}
secret = pcpkey_decrypt(secret, passphrase);
secret = pcpkey_decrypt(ptx, secret, passphrase);
if(secret == NULL)
goto erren2;
}
@@ -250,7 +249,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto erren2;
}
}
@@ -259,7 +258,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
out = stdout;
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not open output file %s\n", outfile);
fatal(ptx, "Could not open output file %s\n", outfile);
goto erren2;
}
}
@@ -275,9 +274,9 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
}
if(self == 1)
clen = pcp_encrypt_stream_sym(pin, pout, symkey, 0, NULL);
clen = pcp_encrypt_stream_sym(ptx, pin, pout, symkey, 0, NULL);
else
clen = pcp_encrypt_stream(pin, pout, secret, pubhash, signcrypt);
clen = pcp_encrypt_stream(ptx, pin, pout, secret, pubhash, signcrypt);
if(armor == 1) {
ps_finish(pout);
@@ -296,11 +295,10 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for 0x%s successfully\n", (SIZE_T_CAST)clen, id);
else {
fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for:\n", (SIZE_T_CAST)clen);
pcp_pubkey_t *cur, *t;
HASH_ITER(hh, pubhash, cur, t) {
pcp_pubkey_t *cur;
pcphash_iteratepub(ptx, cur) {
fprintf(stderr, "%s <%s>\n", cur->owner, cur->mail);
}
free(t);
free(cur);
}
if(signcrypt)

View File

@@ -36,6 +36,7 @@
#include "keyhash.h"
#include "plist.h"
#include "pcpstream.h"
#include "context.h"
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, int verify);
int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *recipient, int signcrypt, int armor);

View File

@@ -30,14 +30,14 @@ char *pcp_getstdin(const char *prompt) {
fprintf(stderr, "%s: ", prompt);
if (fgets(line, 255, stdin) == NULL) {
fatal("Cannot read from stdin");
fatal(ptx, "Cannot read from stdin");
goto errgst;
}
line[strcspn(line, "\r\n")] = '\0';
if ((out = strdup(line)) == NULL) {
fatal("Cannot allocate memory");
fatal(ptx, "Cannot allocate memory");
goto errgst;
}
@@ -48,11 +48,11 @@ char *pcp_getstdin(const char *prompt) {
}
int pcp_storekey (pcp_key_t *key) {
if(vault->isnew == 1 || HASH_COUNT(pcpkey_hash) == 0) {
if(vault->isnew == 1 || pcphash_count(ptx) == 0) {
key->type = PCP_KEY_TYPE_MAINSECRET;
}
if(pcpvault_addkey(vault, key, key->type) == 0) {
if(pcpvault_addkey(ptx, vault, key, key->type) == 0) {
if(vault->isnew)
fprintf(stderr, "new vault created, ");
fprintf(stderr, "key 0x%s added to %s.\n", key->id, vault->filename);
@@ -87,7 +87,7 @@ void pcp_keygen(char *passwd) {
}
if(strnlen(passphrase, 1024) > 0)
key = pcpkey_encrypt(k, passphrase);
key = pcpkey_encrypt(ptx, k, passphrase);
else {
char *yes = pcp_getstdin("WARNING: secret key will be stored unencrypted. Are you sure [yes|NO]?");
if(strncmp(yes, "yes", 1024) == 0)
@@ -117,22 +117,22 @@ void pcp_keygen(char *passwd) {
void pcp_listkeys() {
pcp_key_t *k;
int nkeys = HASH_COUNT(pcpkey_hash) + HASH_COUNT(pcppubkey_hash);
int nkeys = pcphash_count(ptx) + pcphash_countpub(ptx);
if(nkeys > 0) {
printf("Key ID Type Creation Time Owner\n");
pcphash_iterate(k) {
pcphash_iterate(ptx, k) {
pcpkey_printlineinfo(k);
}
pcp_pubkey_t *p;
pcphash_iteratepub(p) {
pcphash_iteratepub(ptx, p) {
pcppubkey_printlineinfo(p);
}
}
else {
fatal("The key vault file %s doesn't contain any keys so far.\n", vault->filename);
fatal(ptx, "The key vault file %s doesn't contain any keys so far.\n", vault->filename);
}
}
@@ -145,12 +145,12 @@ char *pcp_normalize_id(char *keyid) {
memcpy(id, keyid, 17);
}
else if(len < 16) {
fatal("Specified key id %s is too short!\n", keyid);
fatal(ptx, "Specified key id %s is too short!\n", keyid);
free(id);
return NULL;
}
else if(len > 18) {
fatal("Specified key id %s is too long!\n", keyid);
fatal(ptx, "Specified key id %s is too long!\n", keyid);
free(id);
return NULL;
}
@@ -163,7 +163,7 @@ char *pcp_normalize_id(char *keyid) {
id[16] = 0;
}
else {
fatal("Specified key id %s is too long!\n", keyid);
fatal(ptx, "Specified key id %s is too long!\n", keyid);
free(id);
return NULL;
}
@@ -175,7 +175,7 @@ char *pcp_normalize_id(char *keyid) {
pcp_key_t *pcp_find_primary_secret() {
pcp_key_t *key = NULL;
pcp_key_t *k;
pcphash_iterate(k) {
pcphash_iterate(ptx, k) {
if(k->type == PCP_KEY_TYPE_MAINSECRET) {
key = ucmalloc(sizeof(pcp_key_t));
memcpy(key, k, sizeof(pcp_key_t));
@@ -184,9 +184,9 @@ pcp_key_t *pcp_find_primary_secret() {
}
/* no primary? whoops */
int nkeys = HASH_COUNT(pcpkey_hash);
int nkeys = pcphash_count(ptx);
if(nkeys == 1) {
pcphash_iterate(k) {
pcphash_iterate(ptx, k) {
key = ucmalloc(sizeof(pcp_key_t));
memcpy(key, k, sizeof(pcp_key_t));
return key;
@@ -201,9 +201,9 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
if(useid == 1) {
/* look if we've got that one */
HASH_FIND_STR(pcpkey_hash, keyid, key);
key = pcphash_keyexists(ptx, keyid);
if(key == NULL) {
fatal("Could not find a secret key with id 0x%s in vault %s!\n", keyid, vault->filename);
fatal(ptx, "Could not find a secret key with id 0x%s in vault %s!\n", keyid, vault->filename);
goto errexpse1;
}
}
@@ -211,7 +211,7 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
/* look for our primary key */
key = pcp_find_primary_secret();
if(key == NULL) {
fatal("There's no primary secret key in the vault %s!\n", vault->filename);
fatal(ptx, "There's no primary secret key in the vault %s!\n", vault->filename);
goto errexpse1;
}
}
@@ -222,7 +222,7 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
}
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not create output file %s", outfile);
fatal(ptx, "Could not create output file %s", outfile);
goto errexpse1;
}
}
@@ -237,7 +237,7 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
char *passphrase;
pcp_readpass(&passphrase,
"Enter passphrase to decrypt your secret key", NULL, 1);
key = pcpkey_decrypt(key, passphrase);
key = pcpkey_decrypt(ptx, key, passphrase);
if(key == NULL) {
memset(passphrase, 0, strlen(passphrase));
free(passphrase);
@@ -247,7 +247,7 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
free(passphrase);
}
else {
key = pcpkey_decrypt(key, passwd);
key = pcpkey_decrypt(ptx, key, passwd);
if(key == NULL) {
goto errexpse1;
}
@@ -257,13 +257,13 @@ void pcp_exportsecret(char *keyid, int useid, char *outfile, int armor, char *pa
Buffer *exported_sk;
if(passwd != NULL) {
exported_sk = pcp_export_secret(key, passwd);
exported_sk = pcp_export_secret(ptx, key, passwd);
}
else {
char *passphrase;
pcp_readpass(&passphrase,
"Enter passphrase to encrypt the exported secret key", "Repeat passphrase", 1);
exported_sk = pcp_export_secret(key, passphrase);
exported_sk = pcp_export_secret(ptx, key, passphrase);
memset(passphrase, 0, strlen(passphrase));
free(passphrase);
}
@@ -307,19 +307,19 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
}
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not create output file %s", outfile);
fatal(ptx, "Could not create output file %s", outfile);
goto errpcpexpu1;
}
}
if(keyid != NULL) {
/* keyid specified, check if it exists and if yes, what type it is */
HASH_FIND_STR(pcppubkey_hash, keyid, pk);
pk = pcphash_pubkeyexists(ptx, keyid);
if(pk == NULL) {
/* ok, so, then look for a secret key with that id */
HASH_FIND_STR(pcpkey_hash, keyid, sk);
sk = pcphash_keyexists(ptx, keyid);
if(sk == NULL) {
fatal("Could not find a key with id 0x%s in vault %s!\n",
fatal(ptx, "Could not find a key with id 0x%s in vault %s!\n",
keyid, vault->filename);
goto errpcpexpu1;
}
@@ -337,7 +337,7 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
/* we use our primary key anyway */
sk = pcp_find_primary_secret();
if(sk == NULL) {
fatal("There's no primary secret key in the vault %s!\n", vault->filename);
fatal(ptx, "There's no primary secret key in the vault %s!\n", vault->filename);
goto errpcpexpu1;
}
is_foreign = 0;
@@ -347,13 +347,13 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
if(is_foreign == 0 && sk->secret[0] == 0 && format <= EXP_FORMAT_PBP) {
/* decrypt the secret key */
if(passwd != NULL) {
sk = pcpkey_decrypt(sk, passwd);
sk = pcpkey_decrypt(ptx, sk, passwd);
}
else {
char *passphrase;
pcp_readpass(&passphrase,
"Enter passphrase to decrypt your secret key", NULL, 1);
sk = pcpkey_decrypt(sk, passphrase);
sk = pcpkey_decrypt(ptx, sk, passphrase);
memset(passphrase, 0, strlen(passphrase));
free(passphrase);
}
@@ -381,7 +381,7 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
}
else {
/* FIXME: export foreign keys unsupported yet */
fatal("Exporting foreign public keys in native format unsupported yet");
fatal(ptx, "Exporting foreign public keys in native format unsupported yet");
goto errpcpexpu1;
}
}
@@ -399,7 +399,7 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
}
}
else {
fatal("Exporting foreign public keys in PBP format not possible");
fatal(ptx, "Exporting foreign public keys in PBP format not possible");
goto errpcpexpu1;
}
}
@@ -429,38 +429,38 @@ void pcp_exportpublic(char *keyid, char *passwd, char *outfile, int format, int
void pcpdelete_key(char *keyid) {
pcp_pubkey_t *p = pcphash_pubkeyexists(keyid);
pcp_pubkey_t *p = pcphash_pubkeyexists(ptx, keyid);
if(p != NULL) {
/* delete public */
HASH_DEL(pcppubkey_hash, p);
pcphash_del(ptx, p, p->type);
free(p);
vault->unsafed = 1;
fprintf(stderr, "Public key deleted.\n");
}
else {
pcp_key_t *s = pcphash_keyexists(keyid);
pcp_key_t *s = pcphash_keyexists(ptx, keyid);
if(s != NULL) {
/* delete secret */
HASH_DEL(pcpkey_hash, s);
pcphash_del(ptx, s, s->type);
free(s);
vault->unsafed = 1;
fprintf(stderr, "Secret key deleted.\n");
}
else {
fatal("No key with id 0x%s found!\n", keyid);
fatal(ptx, "No key with id 0x%s found!\n", keyid);
}
}
}
void pcpedit_key(char *keyid) {
pcp_key_t *key = pcphash_keyexists(keyid);
pcp_key_t *key = pcphash_keyexists(ptx, keyid);
if(key != NULL) {
if(key->secret[0] == 0) {
char *passphrase;
pcp_readpass(&passphrase, "Enter passphrase to decrypt the key", NULL, 1);
key = pcpkey_decrypt(key, passphrase);
key = pcpkey_decrypt(ptx, key, passphrase);
ucfree(passphrase, strlen(passphrase));
}
@@ -481,7 +481,7 @@ void pcpedit_key(char *keyid) {
if(key->type != PCP_KEY_TYPE_MAINSECRET) {
pcp_key_t *other = NULL;
uint8_t haveprimary = 0;
pcphash_iterate(other) {
pcphash_iterate(ptx, other) {
if(other->type == PCP_KEY_TYPE_MAINSECRET) {
haveprimary = 1;
break;
@@ -515,7 +515,7 @@ void pcpedit_key(char *keyid) {
"Enter the passphrase again", 1);
if(strnlen(passphrase, 1024) > 0) {
key = pcpkey_encrypt(key, passphrase);
key = pcpkey_encrypt(ptx, key, passphrase);
ucfree(passphrase, strlen(passphrase));
}
@@ -529,7 +529,7 @@ void pcpedit_key(char *keyid) {
}
}
else {
fatal("No key with id 0x%s found!\n", keyid);
fatal(ptx, "No key with id 0x%s found!\n", keyid);
}
}
@@ -538,7 +538,7 @@ char *pcp_find_id_byrec(char *recipient) {
pcp_pubkey_t *p;
char *id = NULL;
_lc(recipient);
pcphash_iteratepub(p) {
pcphash_iteratepub(ptx, p) {
if(strncmp(p->owner, recipient, 255) == 0) {
id = ucmalloc(17);
strncpy(id, p->id, 17);
@@ -569,12 +569,12 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
bufsize = ps_read(pin, buf, PCP_BLOCK_SIZE);
if(bufsize == 0) {
fatal("Input file is empty!\n");
fatal(ptx, "Input file is empty!\n");
goto errimp1;
}
/* first try as rfc pub key */
bundle = pcp_import_binpub(buf, bufsize);
bundle = pcp_import_binpub(ptx, buf, bufsize);
if(bundle != NULL) {
keysig = bundle->s;
pub = bundle->p;
@@ -583,7 +583,7 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
pcp_dumppubkey(pub);
if(keysig == NULL) {
fatals_ifany();
fatals_ifany(ptx);
char *yes = pcp_getstdin("WARNING: signature doesn't verify, import anyway [yes|NO]?");
if(strncmp(yes, "yes", 1024) != 0) {
free(yes);
@@ -592,8 +592,8 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
free(yes);
}
if(pcp_sanitycheck_pub(pub) == 0) {
if(pcpvault_addkey(vault, (void *)pub, PCP_KEY_TYPE_PUBLIC) == 0) {
if(pcp_sanitycheck_pub(ptx, pub) == 0) {
if(pcpvault_addkey(ptx, vault, (void *)pub, PCP_KEY_TYPE_PUBLIC) == 0) {
fprintf(stderr, "key 0x%s added to %s.\n", pub->id, vault->filename);
/* avoid double free */
pub = NULL;
@@ -603,7 +603,7 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
goto errimp2;
if(keysig != NULL) {
if(pcpvault_addkey(vault, keysig, keysig->type) != 0) {
if(pcpvault_addkey(ptx, vault, keysig, keysig->type) != 0) {
/* FIXME: remove pubkey if storing the keysig failed */
goto errimp2;
}
@@ -616,13 +616,13 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
else {
/* it's not public key, so let's try to interpret it as secret key */
if(passwd != NULL) {
sk = pcp_import_secret(buf, bufsize, passwd);
sk = pcp_import_secret(ptx, buf, bufsize, passwd);
}
else {
char *passphrase;
pcp_readpass(&passphrase,
"Enter passphrase to decrypt the secret key file", NULL, 1);
sk = pcp_import_secret(buf, bufsize, passphrase);
sk = pcp_import_secret(ptx, buf, bufsize, passphrase);
ucfree(passphrase, strlen(passphrase));
}
@@ -633,15 +633,15 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
if(debug)
pcp_dumpkey(sk);
pcp_key_t *maybe = pcphash_keyexists(sk->id);
pcp_key_t *maybe = pcphash_keyexists(ptx, sk->id);
if(maybe != NULL) {
fatal("Secretkey sanity check: there already exists a key with the id 0x%s\n", sk->id);
fatal(ptx, "Secretkey sanity check: there already exists a key with the id 0x%s\n", sk->id);
goto errimp2;
}
/* store it */
if(passwd != NULL) {
sk = pcpkey_encrypt(sk, passwd);
sk = pcpkey_encrypt(ptx, sk, passwd);
}
else {
char *passphrase;
@@ -651,7 +651,7 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
if(strnlen(passphrase, 1024) > 0) {
/* encrypt the key */
sk = pcpkey_encrypt(sk, passphrase);
sk = pcpkey_encrypt(ptx, sk, passphrase);
ucfree(passphrase, strlen(passphrase));
}
else {
@@ -668,7 +668,7 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
if(sk != NULL) {
/* store it to the vault if we got it til here */
if(pcp_sanitycheck_key(sk) == 0) {
if(pcp_sanitycheck_key(ptx, sk) == 0) {
if(pcp_storekey(sk) == 0) {
pcpkey_printshortinfo(sk);
success = 0;

View File

@@ -42,6 +42,7 @@
#include "base85.h"
#include "buffer.h"
#include "mgmt.h"
#include "context.h"
#define _WITH_GETLINE

View File

@@ -28,7 +28,7 @@ int pcptext_infile(char *infile) {
int insize;
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errtinf1;
}
@@ -42,7 +42,7 @@ int pcptext_infile(char *infile) {
}
/* maybe a vault? */
vault_t *v = pcpvault_init(infile);
vault_t *v = pcpvault_init(ptx, infile);
if(v != NULL) {
fprintf(stdout, "%s is a vault file\n", infile);
pcptext_vault(v);
@@ -50,14 +50,14 @@ int pcptext_infile(char *infile) {
}
/* try z85ing it */
char *z85 = pcp_readz85file(in);
char *z85 = pcp_readz85file(ptx, in);
if(z85 == NULL) {
fprintf(stdout, "Can't handle %s - unknown file type.\n", infile);
goto errtinf1;
}
size_t clen;
byte *bin = pcp_z85_decode((char *)z85, &clen);
byte *bin = pcp_z85_decode(ptx, (char *)z85, &clen);
free(z85);
if(bin == NULL) {
@@ -71,31 +71,31 @@ int pcptext_infile(char *infile) {
fprintf(stdout, "%s looks Z85 encoded but otherwise unknown and is possibly encrypted.\n", infile);
tdone:
fatals_reset();
fatals_reset(ptx);
return 0;
errtinf1:
fatals_reset();
fatals_reset(ptx);
return 1;
}
void pcptext_key(char *keyid) {
pcp_key_t *s = pcphash_keyexists(keyid);
pcp_key_t *s = pcphash_keyexists(ptx, keyid);
if(s != NULL) {
if(debug)
pcp_dumpkey(s);
pcpkey_print(s, stdout);
}
else {
pcp_pubkey_t *p = pcphash_pubkeyexists(keyid);
pcp_pubkey_t *p = pcphash_pubkeyexists(ptx, keyid);
if(p != NULL) {
if(debug)
pcp_dumppubkey(p);
pcppubkey_print(p, stdout);
}
else {
fatal("No key with id 0x%s found!\n", keyid);
fatal(ptx, "No key with id 0x%s found!\n", keyid);
}
}
}
@@ -113,8 +113,8 @@ void pcptext_vault(vault_t *vault) {
printf("%02X", vault->checksum[31]);
printf("\n");
printf(" Secret keys: %d\n", HASH_COUNT(pcpkey_hash));
printf(" Public keys: %d\n", HASH_COUNT(pcppubkey_hash));
printf(" Secret keys: %d\n", pcphash_count(ptx));
printf(" Public keys: %d\n", pcphash_countpub(ptx) );
}
void pcpkey_printlineinfo(pcp_key_t *key) {
@@ -128,7 +128,7 @@ void pcpkey_printlineinfo(pcp_key_t *key) {
c->tm_hour, c->tm_min, c->tm_sec,
key->owner, key->mail);
if(PCPVERBOSE) {
if(ptx->verbose) {
printf(" ");
byte *hash = pcpkey_getchecksum(key);
int i, y;
@@ -157,7 +157,7 @@ void pcppubkey_printlineinfo(pcp_pubkey_t *key) {
c->tm_hour, c->tm_min, c->tm_sec,
key->owner, key->mail);
if(PCPVERBOSE) {
if(ptx->verbose) {
printf(" ");
byte *hash = pcppubkey_getchecksum(key);
int i, y;
@@ -171,7 +171,7 @@ void pcppubkey_printlineinfo(pcp_pubkey_t *key) {
printf("\n signed: %s, serial: %08x, version: %d, ",
(key->valid == 1) ? "yes" : " no",
key->serial, (int)key->version);
pcp_keysig_t *sig = pcphash_keysigexists(key->id);
pcp_keysig_t *sig = pcphash_keysigexists(ptx, key->id);
if(sig != NULL) {
printf("signature fingerprint:\n ");
byte *checksum = sig->checksum;
@@ -305,7 +305,7 @@ void pcpexport_yaml(char *outfile) {
}
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not create output file %s", outfile);
fatal(ptx, "Could not create output file %s", outfile);
out = NULL;
}
}
@@ -325,7 +325,7 @@ void pcpexport_yaml(char *outfile) {
fprintf(out, "---\n");
fprintf(out, "secret-keys:\n");
pcphash_iterate(s) {
pcphash_iterate(ptx, s) {
fprintf(out, " -\n");
fprintf(out, " id: %s\n", s->id);
fprintf(out, " owner: %s\n", s->owner);
@@ -350,7 +350,7 @@ void pcpexport_yaml(char *outfile) {
}
fprintf(out, "public-keys:\n");
pcphash_iteratepub(p) {
pcphash_iteratepub(ptx, p) {
fprintf(out, " -\n");
fprintf(out, " id: %s\n", p->id);
fprintf(out, " owner: %s\n", p->owner);

View File

@@ -30,6 +30,7 @@
#include "keymgmt.h"
#include "keyhash.h"
#include "base85.h"
#include "context.h"
void pcpkey_print(pcp_key_t *key, FILE *out);
void pcppubkey_print(pcp_pubkey_t *key, FILE *out);

View File

@@ -58,7 +58,6 @@ int main (int argc, char **argv) {
plist_t *recipient = NULL;
FILE *in;
PCP_EXIT = 0;
errno = 0;
debug = 0;
mode = 0;
@@ -71,7 +70,7 @@ int main (int argc, char **argv) {
signcrypt = 0;
exportformat = EXP_FORMAT_NATIVE;
PCPVERBOSE = 0;
ptx = ptx_new();
static struct option longopts[] = {
/* generics */
@@ -138,7 +137,7 @@ int main (int argc, char **argv) {
usevault = 1;
break;
case 'L':
PCPVERBOSE = 1; /* no break by purpose, turn on -l */
ptx->verbose = 1; /* no break by purpose, turn on -l */
case 'l':
mode += PCP_MODE_LISTKEYS;
usevault = 1;
@@ -268,7 +267,7 @@ int main (int argc, char **argv) {
case '0':
version();
case 'v':
PCPVERBOSE = 1;
ptx->verbose = 1;
break;
case 'h':
usage(0);
@@ -280,7 +279,6 @@ int main (int argc, char **argv) {
argc -= optind;
argv += optind;
if(mode == 0) {
/* turn -z|-Z into a mode if there's nothing else specified */
if(armor == 1) {
@@ -390,8 +388,7 @@ int main (int argc, char **argv) {
}
if(usevault == 1) {
pcphash_init();
vault = pcpvault_init(vaultfile);
vault = pcpvault_init(ptx, vaultfile);
if(vault != NULL) {
switch (mode) {
case PCP_MODE_KEYGEN:
@@ -435,7 +432,7 @@ int main (int argc, char **argv) {
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
free(infile);
break;
}
@@ -452,7 +449,7 @@ int main (int argc, char **argv) {
}
}
else {
fatal("You need to specify a key id (--keyid)!\n");
fatal(ptx, "You need to specify a key id (--keyid)!\n");
}
break;
@@ -465,7 +462,7 @@ int main (int argc, char **argv) {
}
}
else {
fatal("You need to specify a key id (--keyid)!\n");
fatal(ptx, "You need to specify a key id (--keyid)!\n");
}
break;
@@ -481,7 +478,7 @@ int main (int argc, char **argv) {
}
else {
/* -i and -r specified */
fatal("You can't specify both -i and -r, use either -i or -r!\n");
fatal(ptx, "You can't specify both -i and -r, use either -i or -r!\n");
}
if(id != NULL)
free(id);
@@ -510,7 +507,7 @@ int main (int argc, char **argv) {
case PCP_MODE_SIGN:
if(detach) {
if(outfile != NULL && sigfile != NULL)
fatal("You can't both specify -O and -f, use -O for std signatures and -f for detached ones\n");
fatal(ptx, "You can't both specify -O and -f, use -O for std signatures and -f for detached ones\n");
else
pcpsign(infile, sigfile, xpass, armor, detach);
}
@@ -540,8 +537,7 @@ int main (int argc, char **argv) {
goto ELSEMODE;
break;
}
pcpvault_close(vault);
pcphash_clean();
pcpvault_close(ptx, vault);
free(vaultfile);
}
}
@@ -565,8 +561,7 @@ int main (int argc, char **argv) {
pcptext_infile(infile);
}
else {
pcphash_init();
vault = pcpvault_init(vaultfile);
vault = pcpvault_init(ptx, vaultfile);
if(! useid && infile == NULL) {
pcptext_vault(vault);
}
@@ -577,21 +572,20 @@ int main (int argc, char **argv) {
free(id);
}
}
pcpvault_close(vault);
pcphash_clean();
pcpvault_close(ptx, vault);
free(vaultfile);
}
break;
default:
/* mode params mixed */
fatal("Sorry, invalid combination of commandline parameters (0x%04X)!\n", mode);
fatal(ptx, "Sorry, invalid combination of commandline parameters (0x%04X)!\n", mode);
break;
}
}
fatals_ifany();
fatals_done();
return PCP_EXIT;
fatals_ifany(ptx);
int e = ptx->pcp_exit;
ptx_clean(ptx);
return e;
}

View File

@@ -39,6 +39,7 @@
#include "z85util.h"
#include "version.h"
#include "vault.h"
#include "context.h"
/* subs */
#include "keymgmt.h"
@@ -80,7 +81,9 @@
#define PCP_HELP_INTRO "This is Pretty Curved Privacy. Licensed under the GPLv3. This is\n" \
"BETA software. Use with care. NOT intended for production use.\n"
/* some globals */
vault_t *vault;
PCPCTX *ptx;
int debug;
void version();

View File

@@ -58,13 +58,13 @@ pcp_readpass(char ** passwd, const char * prompt,
/* If we're reading from a terminal, try to disable echo. */
if ((usingtty = isatty(fileno(readfrom))) != 0) {
if (tcgetattr(fileno(readfrom), &term_old)) {
fatal("Cannot read terminal settings");
fatal(ptx, "Cannot read terminal settings");
goto err1;
}
memcpy(&term, &term_old, sizeof(struct termios));
term.c_lflag = (term.c_lflag & ~ECHO) | ECHONL;
if (tcsetattr(fileno(readfrom), TCSANOW, &term)) {
fatal("Cannot set terminal settings");
fatal(ptx, "Cannot set terminal settings");
goto err1;
}
}
@@ -76,7 +76,7 @@ retry:
/* Read the password. */
if (fgets(passbuf, MAXPASSLEN, readfrom) == NULL) {
fatal("Cannot read password");
fatal(ptx, "Cannot read password");
goto err2;
}
@@ -85,7 +85,7 @@ retry:
if (usingtty)
fprintf(stderr, "%s: ", confirmprompt);
if (fgets(confpassbuf, MAXPASSLEN, readfrom) == NULL) {
fatal("Cannot read password");
fatal(ptx, "Cannot read password");
goto err2;
}
if (strcmp(passbuf, confpassbuf)) {
@@ -108,7 +108,7 @@ retry:
/* Copy the password out. */
if ((*passwd = strdup(passbuf)) == NULL) {
fatal("Cannot allocate memory");
fatal(ptx, "Cannot allocate memory");
goto err1;
}

View File

@@ -36,6 +36,8 @@
#include <unistd.h>
#include "defines.h"
#include "context.h"
#include "pcp.h"
#define MAXPASSLEN 2048

View File

@@ -32,7 +32,7 @@ int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
secret = pcp_find_primary_secret();
if(secret == NULL) {
fatal("Could not find a secret key in vault %s!\n", vault->filename);
fatal(ptx, "Could not find a secret key in vault %s!\n", vault->filename);
goto errs1;
}
@@ -40,7 +40,7 @@ int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errs1;
}
}
@@ -49,7 +49,7 @@ int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
out = stdout;
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not open output file %s\n", outfile);
fatal(ptx, "Could not open output file %s\n", outfile);
goto errs1;
}
}
@@ -66,7 +66,7 @@ int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
strncpy(passphrase, passwd, strlen(passwd)+1);
}
secret = pcpkey_decrypt(secret, passphrase);
secret = pcpkey_decrypt(ptx, secret, passphrase);
if(secret == NULL)
goto errs1;
}
@@ -78,7 +78,7 @@ int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
if(detach == 1)
sigsize = pcp_ed_detachsign_buffered(pin, pout, secret);
else
sigsize = pcp_ed_sign_buffered(pin, pout, secret, z85);
sigsize = pcp_ed_sign_buffered(ptx, pin, pout, secret, z85);
ps_close(pin);
ps_close(pout);
@@ -103,30 +103,30 @@ int pcpverify(char *infile, char *sigfile, char *id, int detach) {
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errv1;
}
}
if(sigfile != NULL) {
if((sigfd = fopen(sigfile, "rb")) == NULL) {
fatal("Could not open signature file %s\n", sigfile);
fatal(ptx, "Could not open signature file %s\n", sigfile);
goto errv1;
}
}
if(id != NULL)
HASH_FIND_STR(pcppubkey_hash, id, pub);
pub = pcphash_pubkeyexists(ptx, id);
Pcpstream *pin = ps_new_file(in);
if(detach) {
Pcpstream *psigfd = ps_new_file(sigfd);
pub = pcp_ed_detachverify_buffered(pin, psigfd, pub);
pub = pcp_ed_detachverify_buffered(ptx, pin, psigfd, pub);
ps_close(psigfd);
}
else
pub = pcp_ed_verify_buffered(pin, pub);
pub = pcp_ed_verify_buffered(ptx, pin, pub);
ps_close(pin);

View File

@@ -32,6 +32,7 @@
#include "uthash.h"
#include "z85.h"
#include "pcpstream.h"
#include "context.h"
int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach);
int pcpverify(char *infile, char *sigfile, char *id, int detach);

View File

@@ -30,7 +30,7 @@ int pcpz85_encode(char *infile, char *outfile) {
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errz1;
}
}
@@ -39,7 +39,7 @@ int pcpz85_encode(char *infile, char *outfile) {
out = stdout;
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not open output file %s\n", outfile);
fatal(ptx, "Could not open output file %s\n", outfile);
goto errz1;
}
}
@@ -59,7 +59,7 @@ int pcpz85_encode(char *infile, char *outfile) {
fclose(in);
if(inputBufSize == 0) {
fatal("Input file is empty!\n");
fatal(ptx, "Input file is empty!\n");
goto errz2;
}
@@ -69,7 +69,7 @@ int pcpz85_encode(char *infile, char *outfile) {
if(encoded != NULL) {
fprintf(out, "%s\n%s\n%s\n", PCP_ZFILE_HEADER, encoded, PCP_ZFILE_FOOTER);
if(ferror(out) != 0) {
fatal("Failed to write z85 output!\n");
fatal(ptx, "Failed to write z85 output!\n");
}
free(encoded);
goto errz2;
@@ -95,7 +95,7 @@ int pcpz85_decode(char *infile, char *outfile) {
in = stdin;
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal("Could not open input file %s\n", infile);
fatal(ptx, "Could not open input file %s\n", infile);
goto errdz1;
}
}
@@ -104,18 +104,18 @@ int pcpz85_decode(char *infile, char *outfile) {
out = stdout;
else {
if((out = fopen(outfile, "wb+")) == NULL) {
fatal("Could not open output file %s\n", outfile);
fatal(ptx, "Could not open output file %s\n", outfile);
goto errdz1;
}
}
char *encoded = pcp_readz85file(in);
char *encoded = pcp_readz85file(ptx, in);
if(encoded == NULL)
goto errdz1;
size_t clen;
byte *decoded = pcp_z85_decode(encoded, &clen);
byte *decoded = pcp_z85_decode(ptx, encoded, &clen);
@@ -125,7 +125,7 @@ int pcpz85_decode(char *infile, char *outfile) {
fwrite(decoded, clen, 1, out);
fclose(out);
if(ferror(out) != 0) {
fatal("Failed to write decoded output!\n");
fatal(ptx, "Failed to write decoded output!\n");
goto errdz3;
}

View File

@@ -35,6 +35,9 @@
#include "z85.h"
#include "zmq_z85.h"
#include "defines.h"
#include "context.h"
extern PCPCTX *ptx;
int pcpz85_encode(char *infile, char *outfile);
int pcpz85_decode(char *infile, char *outfile);