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

@@ -30,55 +30,67 @@ Key::Key() {
K = NULL;
}
Key::Key(bool generate) {
Key::Key(PcpContext P) {
stored = false;
K = NULL;
PTX = P;
}
Key::Key(PcpContext P, bool generate) {
stored = false;
if(generate)
K = pcpkey_new();
else
K = NULL;
PTX = P;
}
Key::Key(const string& passphrase) {
Key::Key(PcpContext P, const string& passphrase) {
stored = false;
K = pcpkey_new();
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
K = pcpkey_encrypt(PTX.ptx, K, (char *)passphrase.c_str());
PTX = P;
}
Key::Key(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(_K, (char *)passphrase.c_str());
K = pcpkey_encrypt(PTX.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(pcp_key_t *k) {
Key::Key(PcpContext P, pcp_key_t *k) {
stored = false;
K = k;
PTX = P;
}
Key::Key(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(string &z85encoded, string &passphrase) {
Key::Key(PcpContext P, string &z85encoded, string &passphrase) {
stored = false;
PTX = P;
if(z85encoded.length() == 0)
throw pcp::exception("Error: zero length input");
throw pcp::exception(PTX, "Error: zero length input");
pcp_key_t *key = pcp_import_secret((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();
throw pcp::exception(PTX);
if(pcp_sanitycheck_key(key) != 0) {
if(pcp_sanitycheck_key(PTX.ptx, key) != 0) {
free(key);
throw pcp::exception();
throw pcp::exception(PTX);
}
K = key;
@@ -99,12 +111,12 @@ string Key::export_secret(const string &passphrase) {
Buffer *exported_sk;
if(passphrase.length() == 0)
throw pcp::exception("Error: empty passphrase");
throw pcp::exception(PTX, "Error: empty passphrase");
exported_sk = pcp_export_secret(K, (char *)passphrase.c_str());
exported_sk = pcp_export_secret(PTX.ptx, K, (char *)passphrase.c_str());
if(exported_sk == NULL)
throw pcp::exception();
throw pcp::exception(PTX);
size_t zlen;
char *z85 = pcp_z85_encode(buffer_get(exported_sk), buffer_size(exported_sk), &zlen);
@@ -120,7 +132,7 @@ string Key::export_public() {
exported_pk = pcp_export_rfc_pub(K);
if(exported_pk == NULL)
throw pcp::exception();
throw pcp::exception(PTX);
size_t zlen;
char *z85 = pcp_z85_encode(buffer_get(exported_pk), buffer_size(exported_pk), &zlen);
@@ -140,19 +152,19 @@ bool pcp::operator!(Key& k) {
void Key::encrypt(const string& passphrase) {
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
if(PCP_ERRSET == 1)
throw exception();
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(K, (char *)passphrase.c_str());
if(PCP_ERRSET == 1)
throw exception();
K = pcpkey_decrypt(PTX.ptx, K, (char *)passphrase.c_str());
if(K == NULL)
throw exception(PTX);
}
PubKey Key::get_public() {
return PubKey(pcpkey_pub_from_secret(K));
return PubKey(PTX, pcpkey_pub_from_secret(K));
}
string Key::get_id() {
@@ -197,44 +209,52 @@ bool Key::is_encrypted() {
PubKey::PubKey() {
stored = false;
K = NULL;
}
PubKey::PubKey(pcp_pubkey_t *k) {
PubKey::PubKey(PcpContext P) {
stored = false;
K = k;
K = NULL;
PTX = P;
}
PubKey::PubKey(pcp_pubkey_t *k, bool store) {
PubKey::PubKey(PcpContext P, pcp_pubkey_t *k) {
stored = false;
K = k;
PTX = P;
}
PubKey::PubKey(PcpContext P, pcp_pubkey_t *k, bool store) {
stored = store;
K = k;
PTX = P;
}
PubKey::PubKey(string &z85encoded) {
PubKey::PubKey(PcpContext P, string &z85encoded) {
stored = false;
PTX = P;
if(z85encoded.length() == 0)
throw pcp::exception("Error: zero length input");
throw pcp::exception(PTX, "Error: zero length input");
Buf blob("pub", 256);
blob.add(z85encoded.c_str(), z85encoded.length());
pcp_ks_bundle_t *KS = pcp_import_pub(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();
throw pcp::exception(PTX);
}
pcp_pubkey_t *pub = KS->p;
if(pcp_sanitycheck_pub(pub) != 0) {
if(pcp_sanitycheck_pub(PTX.ptx, pub) != 0) {
free(KS->p);
free(KS->s);
free(KS);
throw pcp::exception();
throw pcp::exception(PTX);
}
K = pub;