mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
put previously global error handling and key hashes into ptx (pcp context) to make libpcp threadsafe.
This commit is contained in:
@@ -28,24 +28,28 @@ Vault::Vault() {
|
||||
V = NULL;
|
||||
}
|
||||
|
||||
Vault::Vault(string filename) {
|
||||
pcphash_init();
|
||||
V = pcpvault_init((char *)filename.c_str());
|
||||
Vault::Vault(PcpContext P) {
|
||||
V = NULL;
|
||||
PTX = P;
|
||||
}
|
||||
|
||||
Vault::Vault(PcpContext P, string filename) {
|
||||
PTX = P;
|
||||
V = pcpvault_init(PTX.ptx, (char *)filename.c_str());
|
||||
if (V == NULL)
|
||||
throw pcp::exception();
|
||||
throw pcp::exception(PTX);
|
||||
}
|
||||
|
||||
Vault::~Vault() {
|
||||
pcpvault_close(V);
|
||||
pcphash_clean();
|
||||
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(k) {
|
||||
kmap.insert ( pair<string,Key>(string(k->id), Key(k, true)) );
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
kmap.insert ( pair<string,Key>(string(k->id), Key(PTX, k, true)) );
|
||||
}
|
||||
|
||||
return kmap;
|
||||
@@ -55,39 +59,39 @@ std::map<std::string, PubKey> Vault::pubkeys() {
|
||||
std::map<std::string, PubKey> kmap;
|
||||
|
||||
pcp_pubkey_t *k = NULL;
|
||||
pcphash_iteratepub(k) {
|
||||
kmap.insert ( pair<string,PubKey>(string(k->id), PubKey(k, true)) );
|
||||
pcphash_iteratepub(PTX.ptx, k) {
|
||||
kmap.insert ( pair<string,PubKey>(string(k->id), PubKey(PTX, k, true)) );
|
||||
}
|
||||
|
||||
return kmap;
|
||||
}
|
||||
|
||||
int Vault::key_count() {
|
||||
return pcphash_count();
|
||||
return pcphash_count(PTX.ptx);
|
||||
}
|
||||
|
||||
int Vault::pubkey_count() {
|
||||
return pcphash_countpub();
|
||||
return pcphash_countpub(PTX.ptx);
|
||||
}
|
||||
|
||||
void Vault::key_add(Key &key) {
|
||||
if(V->isnew == 1 || HASH_COUNT(pcpkey_hash) == 0) {
|
||||
if(V->isnew == 1 || pcphash_count(PTX.ptx) == 0) {
|
||||
key.K->type = PCP_KEY_TYPE_MAINSECRET;
|
||||
}
|
||||
|
||||
if(pcpvault_addkey(V, (void *)key.K, key.K->type) != 0)
|
||||
throw pcp::exception();
|
||||
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(V, (void *)key.K, key.K->type) != 0)
|
||||
throw pcp::exception();
|
||||
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((char *)id.c_str());
|
||||
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str());
|
||||
if(s == NULL)
|
||||
return false;
|
||||
else
|
||||
@@ -95,7 +99,7 @@ bool Vault::key_exists(string &id) {
|
||||
}
|
||||
|
||||
bool Vault::pubkey_exists(string &id) {
|
||||
pcp_pubkey_t *p = pcphash_pubkeyexists((char *)id.c_str());
|
||||
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str());
|
||||
if(p == NULL)
|
||||
return false;
|
||||
else
|
||||
@@ -103,63 +107,63 @@ bool Vault::pubkey_exists(string &id) {
|
||||
}
|
||||
|
||||
void Vault::key_delete(std::string &id) {
|
||||
pcp_pubkey_t *p = pcphash_pubkeyexists((char *)id.c_str());
|
||||
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str());
|
||||
|
||||
if(p != NULL) {
|
||||
// delete public
|
||||
HASH_DEL(pcppubkey_hash, p);
|
||||
pcphash_del(PTX.ptx, p, p->type);
|
||||
free(p);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
else {
|
||||
pcp_key_t *s = pcphash_keyexists((char *)id.c_str());
|
||||
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str());
|
||||
if(s != NULL) {
|
||||
// delete secret
|
||||
HASH_DEL(pcpkey_hash, s);
|
||||
pcphash_del(PTX.ptx, s, s->type);
|
||||
free(s);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
else {
|
||||
throw exception("Key not found!\n");
|
||||
throw exception(PTX, "Key not found!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Key Vault::get_primary() {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(k) {
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
if(k->type == PCP_KEY_TYPE_MAINSECRET) {
|
||||
return Key(k);
|
||||
return Key(PTX, k);
|
||||
}
|
||||
}
|
||||
|
||||
if(Vault::key_count() == 1) {
|
||||
pcphash_iterate(k) {
|
||||
return Key(k);
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
return Key(PTX, k);
|
||||
}
|
||||
}
|
||||
|
||||
// too bad
|
||||
throw exception("No primary key found in vault.");
|
||||
throw exception(PTX, "No primary key found in vault.");
|
||||
}
|
||||
|
||||
Key Vault::get_secret(std::string &id) {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(k) {
|
||||
pcphash_iterate(PTX.ptx, k) {
|
||||
if(memcmp(k->id, id.c_str(), 16) == 0) {
|
||||
return Key(k);
|
||||
return Key(PTX, k);
|
||||
}
|
||||
}
|
||||
throw exception("Secret key doesn't exist in vault.");
|
||||
throw exception(PTX, "Secret key doesn't exist in vault.");
|
||||
}
|
||||
|
||||
|
||||
PubKey Vault::get_public(std::string &id) {
|
||||
pcp_pubkey_t *k = NULL;
|
||||
pcphash_iteratepub(k) {
|
||||
pcphash_iteratepub(PTX.ptx, k) {
|
||||
if(memcmp(k->id, id.c_str(), 16) == 0) {
|
||||
return PubKey(k);
|
||||
return PubKey(PTX, k);
|
||||
}
|
||||
}
|
||||
throw exception("Public key doesn't exist in vault.");
|
||||
throw exception(PTX, "Public key doesn't exist in vault.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user