mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
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:
@@ -48,11 +48,17 @@ Buf::Buf(string name, size_t blocksize) {
|
||||
}
|
||||
|
||||
Buf::~Buf() {
|
||||
buffer_free(B);
|
||||
if(B != NULL) {
|
||||
buffer_free(B);
|
||||
B = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace pcp {
|
||||
|
||||
class Crypto {
|
||||
private:
|
||||
PcpContext PTX;
|
||||
PcpContext *PTX;
|
||||
bool havevault;
|
||||
|
||||
public:
|
||||
@@ -44,8 +44,11 @@ namespace pcp {
|
||||
Vault vault;
|
||||
|
||||
// constructors
|
||||
Crypto(PcpContext &C, Key &skey, PubKey &pkey);
|
||||
Crypto(PcpContext &C, Vault &v, Key &skey, PubKey &pkey);
|
||||
Crypto(PcpContext *C, Key &skey, PubKey &pkey);
|
||||
Crypto(PcpContext *C, Vault &v, Key &skey, PubKey &pkey);
|
||||
|
||||
// destructor
|
||||
~Crypto();
|
||||
|
||||
// PK encryption methods
|
||||
// sender pubkey is P
|
||||
|
||||
@@ -25,16 +25,19 @@
|
||||
using namespace std;
|
||||
using namespace pcp;
|
||||
|
||||
Crypto::Crypto(PcpContext &C, Key &skey, PubKey &pkey) {
|
||||
Crypto::Crypto(PcpContext *C, Key &skey, PubKey &pkey) {
|
||||
P = pkey;
|
||||
S = skey;
|
||||
PTX = C;
|
||||
havevault = false;
|
||||
pcphash_add(PTX.ptx, P.K, PCP_KEY_TYPE_PUBLIC);
|
||||
pcphash_add(PTX.ptx, S.K, PCP_KEY_TYPE_SECRET);
|
||||
vault = Vault();
|
||||
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;
|
||||
S = skey;
|
||||
PTX = C;
|
||||
@@ -42,13 +45,16 @@ Crypto::Crypto(PcpContext &C, Vault &v, Key &skey, PubKey &pkey) {
|
||||
havevault = true;
|
||||
}
|
||||
|
||||
Crypto::~Crypto() {
|
||||
}
|
||||
|
||||
bool Crypto::encrypt(FILE *in, FILE *out, bool sign) {
|
||||
pcp_pubkey_t *pubhash = NULL;
|
||||
HASH_ADD_STR( pubhash, id, P.K);
|
||||
Pcpstream *pin = ps_new_file(in);
|
||||
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)
|
||||
throw exception(PTX);
|
||||
ps_close(pin);
|
||||
@@ -60,7 +66,7 @@ bool Crypto::decrypt(FILE *in, FILE *out, bool verify) {
|
||||
Pcpstream *pin = ps_new_file(in);
|
||||
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);
|
||||
ps_close(pin);
|
||||
ps_close(pout);
|
||||
|
||||
@@ -37,9 +37,9 @@ namespace pcp {
|
||||
class exception : public std::runtime_error {
|
||||
private:
|
||||
PCPCTX *ptx;
|
||||
std::string getfatals(PcpContext &P) {
|
||||
std::string getfatals(PcpContext *P) {
|
||||
std::string msg;
|
||||
PCPCTX *ptx = P.ptx;
|
||||
PCPCTX *ptx = P->ptx;
|
||||
if(ptx->pcp_errset == 1) {
|
||||
msg = ptx->pcp_err;
|
||||
}
|
||||
@@ -51,8 +51,8 @@ namespace pcp {
|
||||
return msg;
|
||||
}
|
||||
public:
|
||||
exception(PcpContext &P, const std::string & msg) : runtime_error(msg) { ptx = P.ptx; }
|
||||
exception(PcpContext &P) : runtime_error(getfatals(P)) { }
|
||||
exception(PcpContext *P, const std::string & msg) : runtime_error(msg) { ptx = P->ptx; }
|
||||
exception(PcpContext *P) : runtime_error(getfatals(P)) { }
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -40,14 +40,14 @@ namespace pcp {
|
||||
|
||||
public:
|
||||
pcp_pubkey_t *K;
|
||||
PcpContext PTX;
|
||||
PcpContext *PTX;
|
||||
|
||||
// constructors
|
||||
PubKey(PcpContext &P);
|
||||
PubKey(PcpContext *P);
|
||||
PubKey();
|
||||
PubKey(PcpContext &P, pcp_pubkey_t *k);
|
||||
PubKey(PcpContext &P, pcp_pubkey_t *k, bool store);
|
||||
PubKey(PcpContext &P, std::string &z85encoded);
|
||||
PubKey(PcpContext *P, pcp_pubkey_t *k);
|
||||
PubKey(PcpContext *P, pcp_pubkey_t *k, bool store);
|
||||
PubKey(PcpContext *P, std::string &z85encoded);
|
||||
|
||||
// destructors
|
||||
~PubKey();
|
||||
@@ -75,19 +75,19 @@ namespace pcp {
|
||||
public:
|
||||
// make access to the underlying struct easier
|
||||
pcp_key_t *K;
|
||||
PcpContext PTX;
|
||||
PcpContext *PTX;
|
||||
|
||||
// constructors
|
||||
Key();
|
||||
Key(PcpContext &P);
|
||||
Key(PcpContext &P, bool generate);
|
||||
Key(PcpContext &P, const std::string& passphrase);
|
||||
Key(PcpContext &P, const std::string& passphrase,
|
||||
Key(PcpContext *P);
|
||||
Key(PcpContext *P, bool generate);
|
||||
Key(PcpContext *P, const std::string& passphrase);
|
||||
Key(PcpContext *P, const std::string& passphrase,
|
||||
const std::string& owner,
|
||||
const std::string& mail);
|
||||
Key(PcpContext &P, pcp_key_t *k);
|
||||
Key(PcpContext &P, pcp_key_t *k, bool store);
|
||||
Key(PcpContext &P, std::string &z85encoded, std::string& passphrase);
|
||||
Key(PcpContext *P, pcp_key_t *k);
|
||||
Key(PcpContext *P, pcp_key_t *k, bool store);
|
||||
Key(PcpContext *P, std::string &z85encoded, std::string& passphrase);
|
||||
|
||||
// destructor
|
||||
~Key();
|
||||
|
||||
@@ -30,13 +30,13 @@ Key::Key() {
|
||||
K = NULL;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P) {
|
||||
Key::Key(PcpContext *P) {
|
||||
stored = false;
|
||||
K = NULL;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P, bool generate) {
|
||||
Key::Key(PcpContext *P, bool generate) {
|
||||
stored = false;
|
||||
if(generate)
|
||||
K = pcpkey_new();
|
||||
@@ -45,50 +45,49 @@ Key::Key(PcpContext &P, bool generate) {
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P, const string& passphrase) {
|
||||
Key::Key(PcpContext *P, const string& passphrase) {
|
||||
stored = false;
|
||||
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;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P, const string& passphrase,
|
||||
Key::Key(PcpContext *P, const string& passphrase,
|
||||
const string& owner,
|
||||
const string& mail) {
|
||||
stored = false;
|
||||
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->mail, mail.c_str(), mail.length()+1);
|
||||
// free(_K);
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P, pcp_key_t *k) {
|
||||
Key::Key(PcpContext *P, pcp_key_t *k) {
|
||||
stored = false;
|
||||
K = k;
|
||||
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);
|
||||
K = k;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Key::Key(PcpContext &P, string &z85encoded, string &passphrase) {
|
||||
Key::Key(PcpContext *P, string &z85encoded, string &passphrase) {
|
||||
stored = false;
|
||||
PTX = P;
|
||||
|
||||
if(z85encoded.length() == 0)
|
||||
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)
|
||||
throw pcp::exception(PTX);
|
||||
|
||||
if(pcp_sanitycheck_key(PTX.ptx, key) != 0) {
|
||||
if(pcp_sanitycheck_key(PTX->ptx, key) != 0) {
|
||||
free(key);
|
||||
throw pcp::exception(PTX);
|
||||
}
|
||||
@@ -103,7 +102,8 @@ Key::~Key() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ string Key::export_secret(const string &passphrase) {
|
||||
if(passphrase.length() == 0)
|
||||
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)
|
||||
throw pcp::exception(PTX);
|
||||
@@ -152,13 +152,13 @@ bool pcp::operator!(Key& k) {
|
||||
|
||||
|
||||
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)
|
||||
throw exception(PTX);
|
||||
}
|
||||
|
||||
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)
|
||||
throw exception(PTX);
|
||||
}
|
||||
@@ -214,26 +214,26 @@ PubKey::PubKey() {
|
||||
K = NULL;
|
||||
}
|
||||
|
||||
PubKey::PubKey(PcpContext &P) {
|
||||
PubKey::PubKey(PcpContext *P) {
|
||||
stored = false;
|
||||
K = NULL;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
|
||||
PubKey::PubKey(PcpContext &P, pcp_pubkey_t *k) {
|
||||
PubKey::PubKey(PcpContext *P, pcp_pubkey_t *k) {
|
||||
stored = false;
|
||||
K = k;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
PubKey::PubKey(PcpContext &P, pcp_pubkey_t *k, bool store) {
|
||||
PubKey::PubKey(PcpContext *P, pcp_pubkey_t *k, bool store) {
|
||||
stored = store;
|
||||
K = k;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
PubKey::PubKey(PcpContext &P, string &z85encoded) {
|
||||
PubKey::PubKey(PcpContext *P, string &z85encoded) {
|
||||
stored = false;
|
||||
PTX = P;
|
||||
|
||||
@@ -243,20 +243,19 @@ PubKey::PubKey(PcpContext &P, string &z85encoded) {
|
||||
Buf blob("pub", 256);
|
||||
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) {
|
||||
throw pcp::exception(PTX);
|
||||
}
|
||||
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->s);
|
||||
free(KS);
|
||||
throw pcp::exception(PTX);
|
||||
}
|
||||
|
||||
K = pub;
|
||||
}
|
||||
|
||||
@@ -267,7 +266,8 @@ PubKey::~PubKey() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,15 +33,22 @@
|
||||
namespace pcp {
|
||||
|
||||
class PcpContext {
|
||||
private:
|
||||
bool iscopy;
|
||||
|
||||
public:
|
||||
PCPCTX *ptx;
|
||||
|
||||
// constructors
|
||||
PcpContext();
|
||||
|
||||
// clean up, wo don't do it in the destructor,
|
||||
// since it will be called multiple times otherwise
|
||||
void done();
|
||||
// destructor
|
||||
~PcpContext();
|
||||
|
||||
// copy constructor. holds the same pointer
|
||||
// as the original and doesn't free()
|
||||
PcpContext& operator = (const PcpContext *PTX);
|
||||
PcpContext(const PcpContext *PTX);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -26,9 +26,22 @@ using namespace pcp;
|
||||
|
||||
PcpContext::PcpContext() {
|
||||
ptx = ptx_new();
|
||||
iscopy = false;
|
||||
}
|
||||
|
||||
void PcpContext::done() {
|
||||
ptx_clean(ptx);
|
||||
PcpContext::~PcpContext() {
|
||||
if(!iscopy) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -43,15 +43,15 @@ namespace pcp {
|
||||
PubKey P;
|
||||
Key S;
|
||||
Vault vault;
|
||||
PubKey Signedby;
|
||||
PubKey *Signedby;
|
||||
Buf sig;
|
||||
PcpContext PTX;
|
||||
PcpContext *PTX;
|
||||
|
||||
// constructors
|
||||
Signature(PcpContext &P, Key &skey); // sign only
|
||||
Signature(PcpContext &P,PubKey &pkey); // verify only
|
||||
Signature(PcpContext &P,Key &skey, PubKey &pkey); // both/bulk
|
||||
Signature(PcpContext &P,Vault &v);
|
||||
Signature(PcpContext *P, Key &skey); // sign only
|
||||
Signature(PcpContext *P,PubKey &pkey); // verify only
|
||||
Signature(PcpContext *P,Key &skey, PubKey &pkey); // both/bulk
|
||||
Signature(PcpContext *P,Vault &v);
|
||||
|
||||
// destructor
|
||||
~Signature();
|
||||
@@ -65,7 +65,8 @@ namespace pcp {
|
||||
// verify using P or use vault if defined
|
||||
bool verify(std::vector<unsigned char> message);
|
||||
bool verify(unsigned char *signature, size_t mlen);
|
||||
bool verify(Buf _sig);
|
||||
bool verify(Buf &_sig);
|
||||
// FIXME: return Signedby
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -24,33 +24,41 @@
|
||||
using namespace std;
|
||||
using namespace pcp;
|
||||
|
||||
Signature::Signature(PcpContext &P, Key &skey) {
|
||||
Signature::Signature(PcpContext *ptx, Key &skey) {
|
||||
S = skey;
|
||||
PTX = P;
|
||||
PTX = ptx;
|
||||
sig = Buf("sign2");
|
||||
havevault = false;
|
||||
Signedby = NULL;
|
||||
}
|
||||
|
||||
Signature::Signature(PcpContext &C,PubKey &pkey) {
|
||||
Signature::Signature(PcpContext *C,PubKey &pkey) {
|
||||
P = pkey;
|
||||
PTX = C;
|
||||
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;
|
||||
S = skey;
|
||||
PTX = C;
|
||||
havevault = false;
|
||||
Signedby = NULL;
|
||||
}
|
||||
|
||||
Signature::Signature(PcpContext &P,Vault &v) {
|
||||
Signature::Signature(PcpContext *C,Vault &v) {
|
||||
vault = v;
|
||||
havevault = true;
|
||||
PTX = P;
|
||||
PTX = C;
|
||||
S = vault.get_primary();
|
||||
Signedby = NULL;
|
||||
}
|
||||
|
||||
Signature::~Signature() {
|
||||
if(Signedby != NULL)
|
||||
delete Signedby;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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());
|
||||
|
||||
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);
|
||||
|
||||
if(pub != NULL) {
|
||||
Signedby = PubKey(PTX, pub);
|
||||
Signedby = new PubKey(PTX, pub);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -46,13 +46,13 @@ namespace pcp {
|
||||
class Vault {
|
||||
private:
|
||||
vault_t *V;
|
||||
PcpContext PTX;
|
||||
PcpContext *PTX;
|
||||
|
||||
public:
|
||||
// constructors
|
||||
Vault();
|
||||
Vault(PcpContext &P);
|
||||
Vault(PcpContext &P, std::string filename);
|
||||
Vault(PcpContext *P);
|
||||
Vault(PcpContext *P, std::string filename);
|
||||
|
||||
// destructor
|
||||
~Vault();
|
||||
|
||||
@@ -26,29 +26,31 @@ using namespace pcp;
|
||||
|
||||
Vault::Vault() {
|
||||
V = NULL;
|
||||
PTX = NULL;
|
||||
}
|
||||
|
||||
Vault::Vault(PcpContext &P) {
|
||||
Vault::Vault(PcpContext *P) {
|
||||
V = NULL;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Vault::Vault(PcpContext &P, string filename) {
|
||||
Vault::Vault(PcpContext *P, string filename) {
|
||||
PTX = P;
|
||||
V = pcpvault_init(PTX.ptx, (char *)filename.c_str());
|
||||
V = pcpvault_init(PTX->ptx, (char *)filename.c_str());
|
||||
if (V == NULL)
|
||||
throw pcp::exception(PTX);
|
||||
}
|
||||
|
||||
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> kmap;
|
||||
|
||||
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)) );
|
||||
}
|
||||
|
||||
@@ -59,7 +61,7 @@ std::map<std::string, PubKey> Vault::pubkeys() {
|
||||
std::map<std::string, PubKey> kmap;
|
||||
|
||||
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)) );
|
||||
}
|
||||
|
||||
@@ -67,31 +69,31 @@ std::map<std::string, PubKey> Vault::pubkeys() {
|
||||
}
|
||||
|
||||
int Vault::key_count() {
|
||||
return pcphash_count(PTX.ptx);
|
||||
return pcphash_count(PTX->ptx);
|
||||
}
|
||||
|
||||
int Vault::pubkey_count() {
|
||||
return pcphash_countpub(PTX.ptx);
|
||||
return pcphash_countpub(PTX->ptx);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
key.is_stored(true);
|
||||
}
|
||||
|
||||
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);
|
||||
key.is_stored(true);
|
||||
}
|
||||
|
||||
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)
|
||||
return false;
|
||||
else
|
||||
@@ -99,7 +101,7 @@ bool Vault::key_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)
|
||||
return false;
|
||||
else
|
||||
@@ -107,19 +109,19 @@ bool Vault::pubkey_exists(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) {
|
||||
// delete public
|
||||
pcphash_del(PTX.ptx, p, p->type);
|
||||
pcphash_del(PTX->ptx, p, p->type);
|
||||
free(p);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
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) {
|
||||
// delete secret
|
||||
pcphash_del(PTX.ptx, s, s->type);
|
||||
pcphash_del(PTX->ptx, s, s->type);
|
||||
free(s);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
@@ -131,14 +133,14 @@ void Vault::key_delete(std::string &id) {
|
||||
|
||||
Key Vault::get_primary() {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
pcphash_iterate(PTX->ptx, k) {
|
||||
if(k->type == PCP_KEY_TYPE_MAINSECRET) {
|
||||
return Key(PTX, k);
|
||||
}
|
||||
}
|
||||
|
||||
if(Vault::key_count() == 1) {
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
pcphash_iterate(PTX->ptx, k) {
|
||||
return Key(PTX, k);
|
||||
}
|
||||
}
|
||||
@@ -149,7 +151,7 @@ Key Vault::get_primary() {
|
||||
|
||||
Key Vault::get_secret(std::string &id) {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
pcphash_iterate(PTX->ptx, k) {
|
||||
if(memcmp(k->id, id.c_str(), 16) == 0) {
|
||||
return Key(PTX, k);
|
||||
}
|
||||
@@ -160,7 +162,7 @@ Key Vault::get_secret(std::string &id) {
|
||||
|
||||
PubKey Vault::get_public(std::string &id) {
|
||||
pcp_pubkey_t *k = NULL;
|
||||
pcphash_iteratepub(PTX.ptx, k) {
|
||||
pcphash_iteratepub(PTX->ptx, k) {
|
||||
if(memcmp(k->id, id.c_str(), 16) == 0) {
|
||||
return PubKey(PTX, k);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ void pr(string name, unsigned char *data, size_t len) {
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
FILE *_openwr(string file, PcpContext &ptx) {
|
||||
FILE *_openwr(string file, PcpContext *ptx) {
|
||||
FILE *fd;
|
||||
if((fd = fopen(file.c_str(), "wb+")) == NULL) {
|
||||
throw pcp::exception(ptx, "Could not open output file " + file + "\n");
|
||||
@@ -22,7 +22,7 @@ FILE *_openwr(string file, PcpContext &ptx) {
|
||||
return fd;
|
||||
}
|
||||
|
||||
FILE *_openrd(string file, PcpContext &ptx) {
|
||||
FILE *_openrd(string file, PcpContext *ptx) {
|
||||
FILE *fd;
|
||||
if((fd = fopen(file.c_str(), "rb")) == NULL) {
|
||||
throw pcp::exception(ptx, "Could not open input file " + file + "\n");
|
||||
@@ -32,12 +32,13 @@ FILE *_openrd(string file, PcpContext &ptx) {
|
||||
|
||||
void test0() {
|
||||
// test keygen and crypto
|
||||
PcpContext CA; // we need different contexts for sender and recipient!
|
||||
PcpContext CB;
|
||||
PcpContext *CA = new PcpContext(); // we need different contexts for sender and recipient!
|
||||
PcpContext *CB = new PcpContext();
|
||||
|
||||
FILE *CLEAR, *CIPHER, *DECRYPTED;
|
||||
Key A = Key(CA, "a", "alicia", "alicia@local");
|
||||
Key B = Key(CA, "b", "bobby", "bobby@local");
|
||||
|
||||
PubKey PA = A.get_public();
|
||||
PubKey PB = B.get_public();
|
||||
|
||||
@@ -78,11 +79,11 @@ void test0() {
|
||||
|
||||
cout << "0 ok" << endl;
|
||||
|
||||
CA.done();
|
||||
CB.done();
|
||||
delete CA;
|
||||
delete CB;
|
||||
}
|
||||
|
||||
void test1(PcpContext &ptx) {
|
||||
void test1(PcpContext *ptx) {
|
||||
// test the vault
|
||||
Key A = Key(ptx, "a", "alicia", "alicia@local");
|
||||
Key B = Key(ptx, "b", "bobby", "bobby@local");
|
||||
@@ -113,7 +114,8 @@ void test1(PcpContext &ptx) {
|
||||
cout << "1 ok" << endl;
|
||||
}
|
||||
|
||||
void test2(PcpContext &ptx) {
|
||||
void test2(PcpContext *ptx) {
|
||||
cerr << " enter test2()" << endl;
|
||||
// try importing a key from disk
|
||||
ifstream pf("key-bobby-pub");
|
||||
string z;
|
||||
@@ -124,13 +126,14 @@ void test2(PcpContext &ptx) {
|
||||
if(strlen(buf) > 0)
|
||||
z += buf + string("\n");
|
||||
}
|
||||
cerr << " PubKey B(ptx, z);" << endl;
|
||||
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
|
||||
Key A = Key(ptx, "a", "alicia", "alicia@local");
|
||||
A.decrypt("a");
|
||||
@@ -142,7 +145,7 @@ void test3(PcpContext &ptx) {
|
||||
Signature SigB(ptx, PA);
|
||||
|
||||
if(SigA.sign((unsigned char*)message.c_str(), message.length()))
|
||||
if(SigB.verify(SigA.sig) )
|
||||
if(SigB.verify(SigA.sig) )
|
||||
cout << "3 ok" << endl;
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ void test4() {
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
sodium_init();
|
||||
PcpContext ptx;
|
||||
PcpContext *ptx = new PcpContext();
|
||||
|
||||
try {
|
||||
if(argc < 2)
|
||||
@@ -199,7 +202,6 @@ int main(int argc, char **argv) {
|
||||
cerr << "Catched exception: " << E.what() << endl;
|
||||
}
|
||||
|
||||
ptx.done();
|
||||
|
||||
delete ptx;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user