C++ API changes+fixes:

- context is now a pointer to make sure there's only one all the time
- fixed a couple of double free's
- some minor bug fixes
This commit is contained in:
git@daemon.de
2014-08-01 14:46:38 +02:00
parent 1e4e65b811
commit e6a5c51d8a
13 changed files with 168 additions and 114 deletions

View File

@@ -48,11 +48,17 @@ Buf::Buf(string name, size_t blocksize) {
} }
Buf::~Buf() { Buf::~Buf() {
if(B != NULL) {
buffer_free(B); buffer_free(B);
B = NULL;
}
} }
Buf& Buf::operator = (const Buf &b) { Buf& Buf::operator = (const Buf &b) {
B = b.B; char *name = (char *)ucmalloc(20);
sprintf(name, "copy of %s", b.B->name);
B = buffer_new(32, name);
buffer_add_buf(B, b.B);
return *this; return *this;
} }

View File

@@ -35,7 +35,7 @@ namespace pcp {
class Crypto { class Crypto {
private: private:
PcpContext PTX; PcpContext *PTX;
bool havevault; bool havevault;
public: public:
@@ -44,8 +44,11 @@ namespace pcp {
Vault vault; Vault vault;
// constructors // constructors
Crypto(PcpContext &C, Key &skey, PubKey &pkey); Crypto(PcpContext *C, Key &skey, PubKey &pkey);
Crypto(PcpContext &C, Vault &v, Key &skey, PubKey &pkey); Crypto(PcpContext *C, Vault &v, Key &skey, PubKey &pkey);
// destructor
~Crypto();
// PK encryption methods // PK encryption methods
// sender pubkey is P // sender pubkey is P

View File

@@ -25,16 +25,19 @@
using namespace std; using namespace std;
using namespace pcp; using namespace pcp;
Crypto::Crypto(PcpContext &C, Key &skey, PubKey &pkey) { Crypto::Crypto(PcpContext *C, Key &skey, PubKey &pkey) {
P = pkey; P = pkey;
S = skey; S = skey;
PTX = C; PTX = C;
havevault = false; havevault = false;
pcphash_add(PTX.ptx, P.K, PCP_KEY_TYPE_PUBLIC); vault = Vault();
pcphash_add(PTX.ptx, S.K, PCP_KEY_TYPE_SECRET); P.is_stored(true);
S.is_stored(true);
pcphash_add(PTX->ptx, P.K, PCP_KEY_TYPE_PUBLIC);
pcphash_add(PTX->ptx, S.K, PCP_KEY_TYPE_SECRET);
} }
Crypto::Crypto(PcpContext &C, Vault &v, Key &skey, PubKey &pkey) { Crypto::Crypto(PcpContext *C, Vault &v, Key &skey, PubKey &pkey) {
P = pkey; P = pkey;
S = skey; S = skey;
PTX = C; PTX = C;
@@ -42,13 +45,16 @@ Crypto::Crypto(PcpContext &C, Vault &v, Key &skey, PubKey &pkey) {
havevault = true; havevault = true;
} }
Crypto::~Crypto() {
}
bool Crypto::encrypt(FILE *in, FILE *out, bool sign) { bool Crypto::encrypt(FILE *in, FILE *out, bool sign) {
pcp_pubkey_t *pubhash = NULL; pcp_pubkey_t *pubhash = NULL;
HASH_ADD_STR( pubhash, id, P.K); HASH_ADD_STR( pubhash, id, P.K);
Pcpstream *pin = ps_new_file(in); Pcpstream *pin = ps_new_file(in);
Pcpstream *pout = ps_new_file(out); Pcpstream *pout = ps_new_file(out);
size_t clen = pcp_encrypt_stream(PTX.ptx, pin, pout, S.K, pubhash, sign); size_t clen = pcp_encrypt_stream(PTX->ptx, pin, pout, S.K, pubhash, sign);
if(clen <= 0) if(clen <= 0)
throw exception(PTX); throw exception(PTX);
ps_close(pin); ps_close(pin);
@@ -60,7 +66,7 @@ bool Crypto::decrypt(FILE *in, FILE *out, bool verify) {
Pcpstream *pin = ps_new_file(in); Pcpstream *pin = ps_new_file(in);
Pcpstream *pout = ps_new_file(out); Pcpstream *pout = ps_new_file(out);
if(pcp_decrypt_stream(PTX.ptx, pin, pout, S.K, NULL, verify) <= 0) if(pcp_decrypt_stream(PTX->ptx, pin, pout, S.K, NULL, verify) <= 0)
throw exception(PTX); throw exception(PTX);
ps_close(pin); ps_close(pin);
ps_close(pout); ps_close(pout);

View File

@@ -37,9 +37,9 @@ namespace pcp {
class exception : public std::runtime_error { class exception : public std::runtime_error {
private: private:
PCPCTX *ptx; PCPCTX *ptx;
std::string getfatals(PcpContext &P) { std::string getfatals(PcpContext *P) {
std::string msg; std::string msg;
PCPCTX *ptx = P.ptx; PCPCTX *ptx = P->ptx;
if(ptx->pcp_errset == 1) { if(ptx->pcp_errset == 1) {
msg = ptx->pcp_err; msg = ptx->pcp_err;
} }
@@ -51,8 +51,8 @@ namespace pcp {
return msg; return msg;
} }
public: public:
exception(PcpContext &P, const std::string & msg) : runtime_error(msg) { ptx = P.ptx; } exception(PcpContext *P, const std::string & msg) : runtime_error(msg) { ptx = P->ptx; }
exception(PcpContext &P) : runtime_error(getfatals(P)) { } exception(PcpContext *P) : runtime_error(getfatals(P)) { }
}; };

View File

@@ -40,14 +40,14 @@ namespace pcp {
public: public:
pcp_pubkey_t *K; pcp_pubkey_t *K;
PcpContext PTX; PcpContext *PTX;
// constructors // constructors
PubKey(PcpContext &P); PubKey(PcpContext *P);
PubKey(); PubKey();
PubKey(PcpContext &P, pcp_pubkey_t *k); PubKey(PcpContext *P, pcp_pubkey_t *k);
PubKey(PcpContext &P, pcp_pubkey_t *k, bool store); PubKey(PcpContext *P, pcp_pubkey_t *k, bool store);
PubKey(PcpContext &P, std::string &z85encoded); PubKey(PcpContext *P, std::string &z85encoded);
// destructors // destructors
~PubKey(); ~PubKey();
@@ -75,19 +75,19 @@ namespace pcp {
public: public:
// make access to the underlying struct easier // make access to the underlying struct easier
pcp_key_t *K; pcp_key_t *K;
PcpContext PTX; PcpContext *PTX;
// constructors // constructors
Key(); Key();
Key(PcpContext &P); Key(PcpContext *P);
Key(PcpContext &P, bool generate); Key(PcpContext *P, bool generate);
Key(PcpContext &P, const std::string& passphrase); Key(PcpContext *P, const std::string& passphrase);
Key(PcpContext &P, const std::string& passphrase, Key(PcpContext *P, const std::string& passphrase,
const std::string& owner, const std::string& owner,
const std::string& mail); const std::string& mail);
Key(PcpContext &P, pcp_key_t *k); Key(PcpContext *P, pcp_key_t *k);
Key(PcpContext &P, pcp_key_t *k, bool store); Key(PcpContext *P, pcp_key_t *k, bool store);
Key(PcpContext &P, std::string &z85encoded, std::string& passphrase); Key(PcpContext *P, std::string &z85encoded, std::string& passphrase);
// destructor // destructor
~Key(); ~Key();

View File

@@ -30,13 +30,13 @@ Key::Key() {
K = NULL; K = NULL;
} }
Key::Key(PcpContext &P) { Key::Key(PcpContext *P) {
stored = false; stored = false;
K = NULL; K = NULL;
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, bool generate) { Key::Key(PcpContext *P, bool generate) {
stored = false; stored = false;
if(generate) if(generate)
K = pcpkey_new(); K = pcpkey_new();
@@ -45,50 +45,49 @@ Key::Key(PcpContext &P, bool generate) {
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, const string& passphrase) { Key::Key(PcpContext *P, const string& passphrase) {
stored = false; stored = false;
K = pcpkey_new(); K = pcpkey_new();
K = pcpkey_encrypt(PTX.ptx, K, (char *)passphrase.c_str()); K = pcpkey_encrypt(P->ptx, K, (char *)passphrase.c_str());
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, const string& passphrase, Key::Key(PcpContext *P, const string& passphrase,
const string& owner, const string& owner,
const string& mail) { const string& mail) {
stored = false; stored = false;
pcp_key_t *_K = pcpkey_new(); pcp_key_t *_K = pcpkey_new();
K = pcpkey_encrypt(PTX.ptx, _K, (char *)passphrase.c_str()); K = pcpkey_encrypt(P->ptx, _K, (char *)passphrase.c_str());
memcpy(K->owner, owner.c_str(), owner.length()+1); memcpy(K->owner, owner.c_str(), owner.length()+1);
memcpy(K->mail, mail.c_str(), mail.length()+1); memcpy(K->mail, mail.c_str(), mail.length()+1);
// free(_K);
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, pcp_key_t *k) { Key::Key(PcpContext *P, pcp_key_t *k) {
stored = false; stored = false;
K = k; K = k;
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, pcp_key_t *k, bool store) { Key::Key(PcpContext *P, pcp_key_t *k, bool store) {
stored = new bool(store); stored = new bool(store);
K = k; K = k;
PTX = P; PTX = P;
} }
Key::Key(PcpContext &P, string &z85encoded, string &passphrase) { Key::Key(PcpContext *P, string &z85encoded, string &passphrase) {
stored = false; stored = false;
PTX = P; PTX = P;
if(z85encoded.length() == 0) if(z85encoded.length() == 0)
throw pcp::exception(PTX, "Error: zero length input"); throw pcp::exception(PTX, "Error: zero length input");
pcp_key_t *key = pcp_import_secret(PTX.ptx, (unsigned char *)z85encoded.c_str(), z85encoded.length(), (char *)passphrase.c_str()); pcp_key_t *key = pcp_import_secret(PTX->ptx, (unsigned char *)z85encoded.c_str(), z85encoded.length(), (char *)passphrase.c_str());
if(key == NULL) if(key == NULL)
throw pcp::exception(PTX); throw pcp::exception(PTX);
if(pcp_sanitycheck_key(PTX.ptx, key) != 0) { if(pcp_sanitycheck_key(PTX->ptx, key) != 0) {
free(key); free(key);
throw pcp::exception(PTX); throw pcp::exception(PTX);
} }
@@ -103,7 +102,8 @@ Key::~Key() {
} }
Key& Key::operator = (const Key &k) { Key& Key::operator = (const Key &k) {
K = k.K; K = (pcp_key_t *)ucmalloc(sizeof(pcp_key_t));
memcpy(K, k.K, sizeof(pcp_key_t));
return *this; return *this;
} }
@@ -113,7 +113,7 @@ string Key::export_secret(const string &passphrase) {
if(passphrase.length() == 0) if(passphrase.length() == 0)
throw pcp::exception(PTX, "Error: empty passphrase"); throw pcp::exception(PTX, "Error: empty passphrase");
exported_sk = pcp_export_secret(PTX.ptx, K, (char *)passphrase.c_str()); exported_sk = pcp_export_secret(PTX->ptx, K, (char *)passphrase.c_str());
if(exported_sk == NULL) if(exported_sk == NULL)
throw pcp::exception(PTX); throw pcp::exception(PTX);
@@ -152,13 +152,13 @@ bool pcp::operator!(Key& k) {
void Key::encrypt(const string& passphrase) { void Key::encrypt(const string& passphrase) {
K = pcpkey_encrypt(PTX.ptx, K, (char *)passphrase.c_str()); K = pcpkey_encrypt(PTX->ptx, K, (char *)passphrase.c_str());
if(K == NULL) if(K == NULL)
throw exception(PTX); throw exception(PTX);
} }
void Key::decrypt(const string& passphrase) { void Key::decrypt(const string& passphrase) {
K = pcpkey_decrypt(PTX.ptx, K, (char *)passphrase.c_str()); K = pcpkey_decrypt(PTX->ptx, K, (char *)passphrase.c_str());
if(K == NULL) if(K == NULL)
throw exception(PTX); throw exception(PTX);
} }
@@ -214,26 +214,26 @@ PubKey::PubKey() {
K = NULL; K = NULL;
} }
PubKey::PubKey(PcpContext &P) { PubKey::PubKey(PcpContext *P) {
stored = false; stored = false;
K = NULL; K = NULL;
PTX = P; PTX = P;
} }
PubKey::PubKey(PcpContext &P, pcp_pubkey_t *k) { PubKey::PubKey(PcpContext *P, pcp_pubkey_t *k) {
stored = false; stored = false;
K = k; K = k;
PTX = P; PTX = P;
} }
PubKey::PubKey(PcpContext &P, pcp_pubkey_t *k, bool store) { PubKey::PubKey(PcpContext *P, pcp_pubkey_t *k, bool store) {
stored = store; stored = store;
K = k; K = k;
PTX = P; PTX = P;
} }
PubKey::PubKey(PcpContext &P, string &z85encoded) { PubKey::PubKey(PcpContext *P, string &z85encoded) {
stored = false; stored = false;
PTX = P; PTX = P;
@@ -243,20 +243,19 @@ PubKey::PubKey(PcpContext &P, string &z85encoded) {
Buf blob("pub", 256); Buf blob("pub", 256);
blob.add(z85encoded.c_str(), z85encoded.length()); blob.add(z85encoded.c_str(), z85encoded.length());
pcp_ks_bundle_t *KS = pcp_import_pub(PTX.ptx, buffer_get(blob.get_buffer()), buffer_size(blob.get_buffer())); pcp_ks_bundle_t *KS = pcp_import_pub(PTX->ptx, buffer_get(blob.get_buffer()), buffer_size(blob.get_buffer()));
if(KS == NULL) { if(KS == NULL) {
throw pcp::exception(PTX); throw pcp::exception(PTX);
} }
pcp_pubkey_t *pub = KS->p; pcp_pubkey_t *pub = KS->p;
if(pcp_sanitycheck_pub(PTX.ptx, pub) != 0) { if(pcp_sanitycheck_pub(PTX->ptx, pub) != 0) {
free(KS->p); free(KS->p);
free(KS->s); free(KS->s);
free(KS); free(KS);
throw pcp::exception(PTX); throw pcp::exception(PTX);
} }
K = pub; K = pub;
} }
@@ -267,7 +266,8 @@ PubKey::~PubKey() {
} }
PubKey& PubKey::operator = (const PubKey &k) { PubKey& PubKey::operator = (const PubKey &k) {
K = k.K; K = (pcp_pubkey_t *)ucmalloc(sizeof(pcp_pubkey_t));
memcpy(K, k.K, sizeof(pcp_pubkey_t));
return *this; return *this;
} }

View File

@@ -33,15 +33,22 @@
namespace pcp { namespace pcp {
class PcpContext { class PcpContext {
private:
bool iscopy;
public: public:
PCPCTX *ptx; PCPCTX *ptx;
// constructors // constructors
PcpContext(); PcpContext();
// clean up, wo don't do it in the destructor, // destructor
// since it will be called multiple times otherwise ~PcpContext();
void done();
// copy constructor. holds the same pointer
// as the original and doesn't free()
PcpContext& operator = (const PcpContext *PTX);
PcpContext(const PcpContext *PTX);
}; };
}; };

View File

@@ -26,9 +26,22 @@ using namespace pcp;
PcpContext::PcpContext() { PcpContext::PcpContext() {
ptx = ptx_new(); ptx = ptx_new();
iscopy = false;
} }
void PcpContext::done() { PcpContext::~PcpContext() {
if(!iscopy) {
ptx_clean(ptx); ptx_clean(ptx);
} }
}
PcpContext& PcpContext::operator = (const PcpContext *PTX) {
ptx = PTX->ptx;
iscopy = 1;
return *this;
}
PcpContext::PcpContext(const PcpContext *PTX) {
ptx = PTX->ptx;
iscopy = 1;
}

View File

@@ -43,15 +43,15 @@ namespace pcp {
PubKey P; PubKey P;
Key S; Key S;
Vault vault; Vault vault;
PubKey Signedby; PubKey *Signedby;
Buf sig; Buf sig;
PcpContext PTX; PcpContext *PTX;
// constructors // constructors
Signature(PcpContext &P, Key &skey); // sign only Signature(PcpContext *P, Key &skey); // sign only
Signature(PcpContext &P,PubKey &pkey); // verify only Signature(PcpContext *P,PubKey &pkey); // verify only
Signature(PcpContext &P,Key &skey, PubKey &pkey); // both/bulk Signature(PcpContext *P,Key &skey, PubKey &pkey); // both/bulk
Signature(PcpContext &P,Vault &v); Signature(PcpContext *P,Vault &v);
// destructor // destructor
~Signature(); ~Signature();
@@ -65,7 +65,8 @@ namespace pcp {
// verify using P or use vault if defined // verify using P or use vault if defined
bool verify(std::vector<unsigned char> message); bool verify(std::vector<unsigned char> message);
bool verify(unsigned char *signature, size_t mlen); bool verify(unsigned char *signature, size_t mlen);
bool verify(Buf _sig); bool verify(Buf &_sig);
// FIXME: return Signedby
}; };
}; };

View File

@@ -24,33 +24,41 @@
using namespace std; using namespace std;
using namespace pcp; using namespace pcp;
Signature::Signature(PcpContext &P, Key &skey) { Signature::Signature(PcpContext *ptx, Key &skey) {
S = skey; S = skey;
PTX = P; PTX = ptx;
sig = Buf("sign2");
havevault = false; havevault = false;
Signedby = NULL;
} }
Signature::Signature(PcpContext &C,PubKey &pkey) { Signature::Signature(PcpContext *C,PubKey &pkey) {
P = pkey; P = pkey;
PTX = C; PTX = C;
havevault = false; havevault = false;
sig = Buf("sign1");
Signedby = NULL;
} }
Signature::Signature(PcpContext &C,Key &skey, PubKey &pkey) { Signature::Signature(PcpContext *C,Key &skey, PubKey &pkey) {
P = pkey; P = pkey;
S = skey; S = skey;
PTX = C; PTX = C;
havevault = false; havevault = false;
Signedby = NULL;
} }
Signature::Signature(PcpContext &P,Vault &v) { Signature::Signature(PcpContext *C,Vault &v) {
vault = v; vault = v;
havevault = true; havevault = true;
PTX = P; PTX = C;
S = vault.get_primary(); S = vault.get_primary();
Signedby = NULL;
} }
Signature::~Signature() { Signature::~Signature() {
if(Signedby != NULL)
delete Signedby;
} }
bool Signature::sign(std::vector<unsigned char> message) { bool Signature::sign(std::vector<unsigned char> message) {
@@ -102,7 +110,7 @@ bool Signature::sign(unsigned char *message, size_t mlen) {
bool Signature::sign(Pcpstream *message) { bool Signature::sign(Pcpstream *message) {
Pcpstream *out = ps_new_outbuffer(); Pcpstream *out = ps_new_outbuffer();
size_t sigsize = pcp_ed_sign_buffered(PTX.ptx, message, out, S.K, 0); size_t sigsize = pcp_ed_sign_buffered(PTX->ptx, message, out, S.K, 0);
if(sigsize > 0) { if(sigsize > 0) {
Buffer *o = ps_buffer(out); Buffer *o = ps_buffer(out);
@@ -142,15 +150,21 @@ bool Signature::verify(unsigned char *signature, size_t mlen) {
} }
bool Signature::verify(Buf _sig) { bool Signature::verify(Buf &_sig) {
Pcpstream *p = ps_new_inbuffer(_sig.get_buffer()); Pcpstream *p = ps_new_inbuffer(_sig.get_buffer());
pcp_pubkey_t *pub = pcp_ed_verify_buffered(PTX.ptx, p, P.K); /*
we need to exclude current public key from free'ing
because it's used as a hash in ed.c:276.
*/
P.is_stored(true);
pcp_pubkey_t *pub = pcp_ed_verify_buffered(PTX->ptx, p, P.K);
ps_close(p); ps_close(p);
if(pub != NULL) { if(pub != NULL) {
Signedby = PubKey(PTX, pub); Signedby = new PubKey(PTX, pub);
return true; return true;
} }
else { else {

View File

@@ -46,13 +46,13 @@ namespace pcp {
class Vault { class Vault {
private: private:
vault_t *V; vault_t *V;
PcpContext PTX; PcpContext *PTX;
public: public:
// constructors // constructors
Vault(); Vault();
Vault(PcpContext &P); Vault(PcpContext *P);
Vault(PcpContext &P, std::string filename); Vault(PcpContext *P, std::string filename);
// destructor // destructor
~Vault(); ~Vault();

View File

@@ -26,29 +26,31 @@ using namespace pcp;
Vault::Vault() { Vault::Vault() {
V = NULL; V = NULL;
PTX = NULL;
} }
Vault::Vault(PcpContext &P) { Vault::Vault(PcpContext *P) {
V = NULL; V = NULL;
PTX = P; PTX = P;
} }
Vault::Vault(PcpContext &P, string filename) { Vault::Vault(PcpContext *P, string filename) {
PTX = P; PTX = P;
V = pcpvault_init(PTX.ptx, (char *)filename.c_str()); V = pcpvault_init(PTX->ptx, (char *)filename.c_str());
if (V == NULL) if (V == NULL)
throw pcp::exception(PTX); throw pcp::exception(PTX);
} }
Vault::~Vault() { Vault::~Vault() {
pcpvault_close(PTX.ptx, V); if(V && PTX)
pcpvault_close(PTX->ptx, V);
} }
std::map<std::string, Key> Vault::keys() { std::map<std::string, Key> Vault::keys() {
std::map<std::string, Key> kmap; std::map<std::string, Key> kmap;
pcp_key_t *k = NULL; pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) { pcphash_iterate(PTX->ptx, k) {
kmap.insert ( pair<string,Key>(string(k->id), Key(PTX, k, true)) ); kmap.insert ( pair<string,Key>(string(k->id), Key(PTX, k, true)) );
} }
@@ -59,7 +61,7 @@ std::map<std::string, PubKey> Vault::pubkeys() {
std::map<std::string, PubKey> kmap; std::map<std::string, PubKey> kmap;
pcp_pubkey_t *k = NULL; pcp_pubkey_t *k = NULL;
pcphash_iteratepub(PTX.ptx, k) { pcphash_iteratepub(PTX->ptx, k) {
kmap.insert ( pair<string,PubKey>(string(k->id), PubKey(PTX, k, true)) ); kmap.insert ( pair<string,PubKey>(string(k->id), PubKey(PTX, k, true)) );
} }
@@ -67,31 +69,31 @@ std::map<std::string, PubKey> Vault::pubkeys() {
} }
int Vault::key_count() { int Vault::key_count() {
return pcphash_count(PTX.ptx); return pcphash_count(PTX->ptx);
} }
int Vault::pubkey_count() { int Vault::pubkey_count() {
return pcphash_countpub(PTX.ptx); return pcphash_countpub(PTX->ptx);
} }
void Vault::key_add(Key &key) { void Vault::key_add(Key &key) {
if(V->isnew == 1 || pcphash_count(PTX.ptx) == 0) { if(V->isnew == 1 || pcphash_count(PTX->ptx) == 0) {
key.K->type = PCP_KEY_TYPE_MAINSECRET; key.K->type = PCP_KEY_TYPE_MAINSECRET;
} }
if(pcpvault_addkey(PTX.ptx, V, (void *)key.K, key.K->type) != 0) if(pcpvault_addkey(PTX->ptx, V, (void *)key.K, key.K->type) != 0)
throw pcp::exception(PTX); throw pcp::exception(PTX);
key.is_stored(true); key.is_stored(true);
} }
void Vault::pubkey_add(PubKey &key) { void Vault::pubkey_add(PubKey &key) {
if(pcpvault_addkey(PTX.ptx, V, (void *)key.K, key.K->type) != 0) if(pcpvault_addkey(PTX->ptx, V, (void *)key.K, key.K->type) != 0)
throw pcp::exception(PTX); throw pcp::exception(PTX);
key.is_stored(true); key.is_stored(true);
} }
bool Vault::key_exists(string &id) { bool Vault::key_exists(string &id) {
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str()); pcp_key_t *s = pcphash_keyexists(PTX->ptx, (char *)id.c_str());
if(s == NULL) if(s == NULL)
return false; return false;
else else
@@ -99,7 +101,7 @@ bool Vault::key_exists(string &id) {
} }
bool Vault::pubkey_exists(string &id) { bool Vault::pubkey_exists(string &id) {
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str()); pcp_pubkey_t *p = pcphash_pubkeyexists(PTX->ptx, (char *)id.c_str());
if(p == NULL) if(p == NULL)
return false; return false;
else else
@@ -107,19 +109,19 @@ bool Vault::pubkey_exists(string &id) {
} }
void Vault::key_delete(std::string &id) { void Vault::key_delete(std::string &id) {
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str()); pcp_pubkey_t *p = pcphash_pubkeyexists(PTX->ptx, (char *)id.c_str());
if(p != NULL) { if(p != NULL) {
// delete public // delete public
pcphash_del(PTX.ptx, p, p->type); pcphash_del(PTX->ptx, p, p->type);
free(p); free(p);
V->unsafed = 1; V->unsafed = 1;
} }
else { else {
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str()); pcp_key_t *s = pcphash_keyexists(PTX->ptx, (char *)id.c_str());
if(s != NULL) { if(s != NULL) {
// delete secret // delete secret
pcphash_del(PTX.ptx, s, s->type); pcphash_del(PTX->ptx, s, s->type);
free(s); free(s);
V->unsafed = 1; V->unsafed = 1;
} }
@@ -131,14 +133,14 @@ void Vault::key_delete(std::string &id) {
Key Vault::get_primary() { Key Vault::get_primary() {
pcp_key_t *k = NULL; pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) { pcphash_iterate(PTX->ptx, k) {
if(k->type == PCP_KEY_TYPE_MAINSECRET) { if(k->type == PCP_KEY_TYPE_MAINSECRET) {
return Key(PTX, k); return Key(PTX, k);
} }
} }
if(Vault::key_count() == 1) { if(Vault::key_count() == 1) {
pcphash_iterate(PTX.ptx, k) { pcphash_iterate(PTX->ptx, k) {
return Key(PTX, k); return Key(PTX, k);
} }
} }
@@ -149,7 +151,7 @@ Key Vault::get_primary() {
Key Vault::get_secret(std::string &id) { Key Vault::get_secret(std::string &id) {
pcp_key_t *k = NULL; pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) { pcphash_iterate(PTX->ptx, k) {
if(memcmp(k->id, id.c_str(), 16) == 0) { if(memcmp(k->id, id.c_str(), 16) == 0) {
return Key(PTX, k); return Key(PTX, k);
} }
@@ -160,7 +162,7 @@ Key Vault::get_secret(std::string &id) {
PubKey Vault::get_public(std::string &id) { PubKey Vault::get_public(std::string &id) {
pcp_pubkey_t *k = NULL; pcp_pubkey_t *k = NULL;
pcphash_iteratepub(PTX.ptx, k) { pcphash_iteratepub(PTX->ptx, k) {
if(memcmp(k->id, id.c_str(), 16) == 0) { if(memcmp(k->id, id.c_str(), 16) == 0) {
return PubKey(PTX, k); return PubKey(PTX, k);
} }

View File

@@ -14,7 +14,7 @@ void pr(string name, unsigned char *data, size_t len) {
cout << endl; cout << endl;
} }
FILE *_openwr(string file, PcpContext &ptx) { FILE *_openwr(string file, PcpContext *ptx) {
FILE *fd; FILE *fd;
if((fd = fopen(file.c_str(), "wb+")) == NULL) { if((fd = fopen(file.c_str(), "wb+")) == NULL) {
throw pcp::exception(ptx, "Could not open output file " + file + "\n"); throw pcp::exception(ptx, "Could not open output file " + file + "\n");
@@ -22,7 +22,7 @@ FILE *_openwr(string file, PcpContext &ptx) {
return fd; return fd;
} }
FILE *_openrd(string file, PcpContext &ptx) { FILE *_openrd(string file, PcpContext *ptx) {
FILE *fd; FILE *fd;
if((fd = fopen(file.c_str(), "rb")) == NULL) { if((fd = fopen(file.c_str(), "rb")) == NULL) {
throw pcp::exception(ptx, "Could not open input file " + file + "\n"); throw pcp::exception(ptx, "Could not open input file " + file + "\n");
@@ -32,12 +32,13 @@ FILE *_openrd(string file, PcpContext &ptx) {
void test0() { void test0() {
// test keygen and crypto // test keygen and crypto
PcpContext CA; // we need different contexts for sender and recipient! PcpContext *CA = new PcpContext(); // we need different contexts for sender and recipient!
PcpContext CB; PcpContext *CB = new PcpContext();
FILE *CLEAR, *CIPHER, *DECRYPTED; FILE *CLEAR, *CIPHER, *DECRYPTED;
Key A = Key(CA, "a", "alicia", "alicia@local"); Key A = Key(CA, "a", "alicia", "alicia@local");
Key B = Key(CA, "b", "bobby", "bobby@local"); Key B = Key(CA, "b", "bobby", "bobby@local");
PubKey PA = A.get_public(); PubKey PA = A.get_public();
PubKey PB = B.get_public(); PubKey PB = B.get_public();
@@ -78,11 +79,11 @@ void test0() {
cout << "0 ok" << endl; cout << "0 ok" << endl;
CA.done(); delete CA;
CB.done(); delete CB;
} }
void test1(PcpContext &ptx) { void test1(PcpContext *ptx) {
// test the vault // test the vault
Key A = Key(ptx, "a", "alicia", "alicia@local"); Key A = Key(ptx, "a", "alicia", "alicia@local");
Key B = Key(ptx, "b", "bobby", "bobby@local"); Key B = Key(ptx, "b", "bobby", "bobby@local");
@@ -113,7 +114,8 @@ void test1(PcpContext &ptx) {
cout << "1 ok" << endl; cout << "1 ok" << endl;
} }
void test2(PcpContext &ptx) { void test2(PcpContext *ptx) {
cerr << " enter test2()" << endl;
// try importing a key from disk // try importing a key from disk
ifstream pf("key-bobby-pub"); ifstream pf("key-bobby-pub");
string z; string z;
@@ -124,13 +126,14 @@ void test2(PcpContext &ptx) {
if(strlen(buf) > 0) if(strlen(buf) > 0)
z += buf + string("\n"); z += buf + string("\n");
} }
cerr << " PubKey B(ptx, z);" << endl;
PubKey B(ptx, z); PubKey B(ptx, z);
//cout << B.to_text();
cout << "2 ok" << endl; cout << "2 ok " << &ptx->ptx << endl;
} }
void test3(PcpContext &ptx) { void test3(PcpContext *ptx) {
// signature test // signature test
Key A = Key(ptx, "a", "alicia", "alicia@local"); Key A = Key(ptx, "a", "alicia", "alicia@local");
A.decrypt("a"); A.decrypt("a");
@@ -164,7 +167,7 @@ void test4() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
sodium_init(); sodium_init();
PcpContext ptx; PcpContext *ptx = new PcpContext();
try { try {
if(argc < 2) if(argc < 2)
@@ -199,7 +202,6 @@ int main(int argc, char **argv) {
cerr << "Catched exception: " << E.what() << endl; cerr << "Catched exception: " << E.what() << endl;
} }
ptx.done(); delete ptx;
return 0; return 0;
} }