context fixes

This commit is contained in:
TLINDEN
2014-05-06 11:49:31 +02:00
parent db264ff16b
commit 729c137e3c
11 changed files with 48 additions and 44 deletions

View File

@@ -44,8 +44,8 @@ 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);
// PK encryption methods
// sender pubkey is P

View File

@@ -25,7 +25,7 @@
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;
@@ -33,7 +33,7 @@ Crypto::Crypto(PcpContext C, Key &skey, PubKey &pkey) {
pcphash_add(PTX.ptx, P.K, PCP_KEY_TYPE_PUBLIC);
}
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;
@@ -47,6 +47,7 @@ bool Crypto::encrypt(FILE *in, FILE *out, bool sign) {
//HASH_ADD_STR( pubhash, id, P.K);
Pcpstream *pin = ps_new_file(in);
Pcpstream *pout = ps_new_file(out);
ptx_dump(PTX.ptx);
size_t clen = pcp_encrypt_stream(PTX.ptx, pin, pout, S.K, pubhash, sign);
if(clen <= 0)
throw exception(PTX);
@@ -58,6 +59,7 @@ bool Crypto::encrypt(FILE *in, FILE *out, bool sign) {
bool Crypto::decrypt(FILE *in, FILE *out, bool verify) {
Pcpstream *pin = ps_new_file(in);
Pcpstream *pout = ps_new_file(out);
ptx_dump(PTX.ptx);
if(pcp_decrypt_stream(PTX.ptx, pin, pout, S.K, NULL, verify) <= 0)
throw exception(PTX);
ps_close(pin);

View File

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

View File

@@ -43,11 +43,11 @@ namespace pcp {
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();
@@ -79,15 +79,15 @@ namespace pcp {
// 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();

View File

@@ -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,14 +45,14 @@ 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());
PTX = P;
}
Key::Key(PcpContext P, const string& passphrase,
Key::Key(PcpContext &P, const string& passphrase,
const string& owner,
const string& mail) {
stored = false;
@@ -64,19 +64,19 @@ Key::Key(PcpContext P, const string& passphrase,
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;
@@ -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;

View File

@@ -39,8 +39,9 @@ namespace pcp {
// constructors
PcpContext();
// destructors
~PcpContext();
// clean up, wo don't do it in the destructor,
// since it will be called multiple times otherwise
void done();
};
};

View File

@@ -28,7 +28,7 @@ PcpContext::PcpContext() {
ptx = ptx_new();
}
PcpContext::~PcpContext() {
void PcpContext::done() {
ptx_clean(ptx);
}

View File

@@ -48,10 +48,10 @@ namespace pcp {
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();

View File

@@ -24,26 +24,26 @@
using namespace std;
using namespace pcp;
Signature::Signature(PcpContext P, Key &skey) {
Signature::Signature(PcpContext &P, Key &skey) {
S = skey;
PTX = P;
havevault = false;
}
Signature::Signature(PcpContext C,PubKey &pkey) {
Signature::Signature(PcpContext &C,PubKey &pkey) {
P = pkey;
PTX = C;
havevault = false;
}
Signature::Signature(PcpContext C,Key &skey, PubKey &pkey) {
Signature::Signature(PcpContext &C,Key &skey, PubKey &pkey) {
P = pkey;
S = skey;
PTX = C;
havevault = false;
}
Signature::Signature(PcpContext P,Vault &v) {
Signature::Signature(PcpContext &P,Vault &v) {
vault = v;
havevault = true;
PTX = P;

View File

@@ -51,8 +51,8 @@ namespace pcp {
public:
// constructors
Vault();
Vault(PcpContext P);
Vault(PcpContext P, std::string filename);
Vault(PcpContext &P);
Vault(PcpContext &P, std::string filename);
// destructor
~Vault();

View File

@@ -28,12 +28,12 @@ Vault::Vault() {
V = 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());
if (V == NULL)