mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
c++ binding now supports vaults and encryption, added test program
This commit is contained in:
@@ -22,5 +22,5 @@ AM_CXXFLAGS = -I../../include -Wall -g
|
||||
|
||||
lib_LTLIBRARIES = libpcp1++.la
|
||||
|
||||
libpcp1___la_SOURCES = pcp++.h key.cpp
|
||||
libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp
|
||||
include_HEADERS = pcp++.h
|
||||
|
||||
@@ -96,7 +96,7 @@ am__uninstall_files_from_dir = { \
|
||||
am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)"
|
||||
LTLIBRARIES = $(lib_LTLIBRARIES)
|
||||
libpcp1___la_LIBADD =
|
||||
am_libpcp1___la_OBJECTS = key.lo
|
||||
am_libpcp1___la_OBJECTS = key.lo vault.lo
|
||||
libpcp1___la_OBJECTS = $(am_libpcp1___la_OBJECTS)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/pcp
|
||||
depcomp = $(SHELL) $(top_srcdir)/config/depcomp
|
||||
@@ -272,7 +272,7 @@ top_srcdir = @top_srcdir@
|
||||
#
|
||||
AM_CXXFLAGS = -I../../include -Wall -g
|
||||
lib_LTLIBRARIES = libpcp1++.la
|
||||
libpcp1___la_SOURCES = pcp++.h key.cpp
|
||||
libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp
|
||||
include_HEADERS = pcp++.h
|
||||
all: all-am
|
||||
|
||||
@@ -352,6 +352,7 @@ distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/key.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vault.Plo@am__quote@
|
||||
|
||||
.cpp.o:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
|
||||
@@ -48,11 +48,7 @@ Key::Key(const string& passphrase,
|
||||
K = pcpkey_encrypt(_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);
|
||||
}
|
||||
|
||||
Key::Key(const Key &k) {
|
||||
K = k.K;
|
||||
// free(_K);
|
||||
}
|
||||
|
||||
Key::Key(pcp_key_t *k) {
|
||||
@@ -60,26 +56,19 @@ Key::Key(pcp_key_t *k) {
|
||||
K = k;
|
||||
}
|
||||
|
||||
Key::~Key() {
|
||||
if (! stored) {
|
||||
free(K);
|
||||
}
|
||||
Key::Key(pcp_key_t *k, bool store) {
|
||||
stored = new bool(store);
|
||||
K = k;
|
||||
}
|
||||
|
||||
Key::Key& Key::operator = (const Key &k) {
|
||||
K = k.K;
|
||||
return *this;
|
||||
}
|
||||
Key::Key(string &z85encoded) {
|
||||
stored = false;
|
||||
|
||||
istream& operator>>(istream& input, Key& k) {
|
||||
string z85;
|
||||
input >> z85;
|
||||
|
||||
if(z85.length() == 0)
|
||||
if(z85encoded.length() == 0)
|
||||
throw pcp::exception("Error: zero length input");
|
||||
|
||||
size_t clen;
|
||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85.c_str(), &clen);
|
||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85encoded.c_str(), &clen);
|
||||
|
||||
if(z85decoded == NULL)
|
||||
throw pcp::exception("Error: could not decode input - it's probably not Z85.\n");
|
||||
@@ -102,16 +91,25 @@ istream& operator>>(istream& input, Key& k) {
|
||||
throw pcp::exception();
|
||||
}
|
||||
|
||||
k = Key(key);
|
||||
free(key);
|
||||
K = key;
|
||||
cout << 7 << " false" << endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
Key::~Key() {
|
||||
if (! stored) {
|
||||
free(K);
|
||||
}
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& output, Key& k) {
|
||||
Key::Key& Key::operator = (const Key &k) {
|
||||
K = k.K;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string Key::to_text() {
|
||||
size_t zlen;
|
||||
pcp_key_t *key = k.get_key();
|
||||
pcp_key_t *key = K;
|
||||
|
||||
key2be(key);
|
||||
void *blob = ucmalloc(PCP_RAW_KEYSIZE);
|
||||
@@ -129,41 +127,55 @@ ostream& operator<<(ostream& output, Key& k) {
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
|
||||
string z85;
|
||||
char *out = (char *)ucmalloc(2048);
|
||||
|
||||
sprintf(out, "%s\n", PCP_KEY_HEADER);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Generated by: %s Version %d.%d.%d\n",
|
||||
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Key-ID: 0x%s\n", key->id);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
//2004-06-14T23:34:30.
|
||||
sprintf(out, " Creation Time: %04d-%02d-%02dT%02d:%02d:%02d\n",
|
||||
c->tm_year+1900, c->tm_mon+1, c->tm_mday,
|
||||
c->tm_hour, c->tm_min, c->tm_sec);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Serial Number: 0x%08X\n", key->serial);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Key Version: 0x%08X\n", key->version);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "\n%s\n", z85encoded);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "%s\n", PCP_KEY_FOOTER);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
free(z85encoded);
|
||||
|
||||
return z85;
|
||||
}
|
||||
|
||||
istream& pcp::operator>>(istream& input, Key& k) {
|
||||
string z85;
|
||||
input >> z85;
|
||||
Key t = new Key(z85); // use the import constructor, FIXME: use a method
|
||||
k.K = t.K;
|
||||
return input;
|
||||
}
|
||||
|
||||
ostream& pcp::operator<<(ostream& output, Key& k) {
|
||||
output << k.to_text();
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -198,10 +210,6 @@ string Key::get_mail() {
|
||||
return m;
|
||||
}
|
||||
|
||||
pcp_key_t *Key::get_key() {
|
||||
return K;
|
||||
}
|
||||
|
||||
void Key::set_owner(const string& owner) {
|
||||
memcpy(K->owner, owner.c_str(), owner.length()+1);
|
||||
}
|
||||
@@ -225,8 +233,77 @@ bool Key::is_encrypted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
string Key::encrypt(PubKey &recipient, string message) {
|
||||
unsigned char *m = (unsigned char *)ucmalloc(message.size() + 1);
|
||||
memcpy(m, message.c_str(), message.size());
|
||||
return Key::encrypt(recipient, m, message.size() + 1);
|
||||
}
|
||||
|
||||
string Key::encrypt(PubKey &recipient, vector<unsigned char> message) {
|
||||
unsigned char *m = (unsigned char *)ucmalloc(message.size());
|
||||
for(size_t i=0; i<message.size(); ++i)
|
||||
m[i] = message[i];
|
||||
return Key::encrypt(recipient, m, message.size());
|
||||
}
|
||||
|
||||
string Key::encrypt(PubKey &recipient, unsigned char *message, size_t mlen) {
|
||||
size_t clen, zlen, rlen;
|
||||
unsigned char *cipher;
|
||||
|
||||
cipher = pcp_box_encrypt(K, recipient.K, message, mlen, &clen);
|
||||
|
||||
if(cipher == NULL)
|
||||
throw exception();
|
||||
|
||||
rlen = clen + crypto_hash_BYTES;
|
||||
unsigned char *combined = (unsigned char *)ucmalloc(rlen);
|
||||
unsigned char *hash = (unsigned char *)ucmalloc(crypto_hash_BYTES);
|
||||
|
||||
crypto_hash(hash, (unsigned char*)K->id, 16);
|
||||
memcpy(combined, hash, crypto_hash_BYTES);
|
||||
memcpy(&combined[crypto_hash_BYTES], cipher, clen);
|
||||
|
||||
// combined consists of:
|
||||
// keyid|nonce|cipher
|
||||
char *encoded = pcp_z85_encode(combined, rlen, &zlen);
|
||||
|
||||
if(encoded == NULL)
|
||||
throw exception();
|
||||
|
||||
return string((char *)encoded);
|
||||
}
|
||||
|
||||
ResultSet Key::decrypt(PubKey &sender, std::string cipher) {
|
||||
|
||||
size_t clen;
|
||||
unsigned char *combined = pcp_z85_decode((char *)cipher.c_str(), &clen);
|
||||
|
||||
if(combined == NULL)
|
||||
throw exception();
|
||||
|
||||
unsigned char *encrypted = (unsigned char*)ucmalloc(clen - crypto_hash_BYTES);
|
||||
memcpy(encrypted, &combined[crypto_hash_BYTES], clen - crypto_hash_BYTES);
|
||||
|
||||
size_t dlen;
|
||||
unsigned char *decrypted = (unsigned char*)pcp_box_decrypt(K, sender.K,
|
||||
encrypted,
|
||||
clen - crypto_hash_BYTES, &dlen);
|
||||
|
||||
if(decrypted == NULL) {
|
||||
free(combined);
|
||||
throw exception();
|
||||
}
|
||||
|
||||
ResultSet r;
|
||||
r.Uchar = decrypted;
|
||||
r.String = string((char *)decrypted);
|
||||
r.Size = dlen;
|
||||
|
||||
for(size_t i=0; i<dlen; ++i)
|
||||
r.Vector.push_back(decrypted[i]);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -240,35 +317,25 @@ PubKey::PubKey() {
|
||||
K = NULL;
|
||||
}
|
||||
|
||||
PubKey::PubKey(const PubKey &k) {
|
||||
K = k.K;
|
||||
}
|
||||
|
||||
PubKey::PubKey(pcp_pubkey_t *k) {
|
||||
stored = false;
|
||||
K = k;
|
||||
}
|
||||
|
||||
PubKey::~PubKey() {
|
||||
if (! stored) {
|
||||
free(K);
|
||||
}
|
||||
PubKey::PubKey(pcp_pubkey_t *k, bool store) {
|
||||
stored = store;
|
||||
K = k;
|
||||
}
|
||||
|
||||
PubKey::PubKey& PubKey::operator = (const PubKey &k) {
|
||||
K = k.K;
|
||||
return *this;
|
||||
}
|
||||
PubKey::PubKey(string &z85encoded) {
|
||||
stored = false;
|
||||
|
||||
istream& operator>>(istream& input, PubKey& k) {
|
||||
string z85;
|
||||
input >> z85;
|
||||
|
||||
if(z85.length() == 0)
|
||||
if(z85encoded.length() == 0)
|
||||
throw pcp::exception("Error: zero length input");
|
||||
|
||||
size_t clen;
|
||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85.c_str(), &clen);
|
||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85encoded.c_str(), &clen);
|
||||
|
||||
if(z85decoded == NULL)
|
||||
throw pcp::exception("Error: could not decode input - it's probably not Z85.\n");
|
||||
@@ -291,16 +358,24 @@ istream& operator>>(istream& input, PubKey& k) {
|
||||
throw pcp::exception();
|
||||
}
|
||||
|
||||
k = PubKey(key);
|
||||
free(key);
|
||||
|
||||
return input;
|
||||
*this = PubKey(key);
|
||||
free(key);
|
||||
}
|
||||
|
||||
PubKey::~PubKey() {
|
||||
if (! stored) {
|
||||
free(K);
|
||||
}
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& output, PubKey& k) {
|
||||
PubKey::PubKey& PubKey::operator = (const PubKey &k) {
|
||||
K = k.K;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string PubKey::to_text() {
|
||||
size_t zlen;
|
||||
pcp_pubkey_t *key = k.get_key();
|
||||
pcp_pubkey_t *key = K;
|
||||
|
||||
pubkey2be(key);
|
||||
void *blob = ucmalloc(PCP_RAW_PUBKEYSIZE);
|
||||
@@ -319,74 +394,88 @@ ostream& operator<<(ostream& output, PubKey& k) {
|
||||
c = localtime(&t);
|
||||
|
||||
char *out = (char *)ucmalloc(2048);
|
||||
string z85;
|
||||
|
||||
sprintf(out, "%s\n", PCP_PUBKEY_HEADER);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Generated by: %s Version %d.%d.%d\n",
|
||||
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " PubKey-ID: 0x%s\n", key->id);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
//2004-06-14T23:34:30.
|
||||
sprintf(out, " Creation Time: %04d-%02d-%02dT%02d:%02d:%02d\n",
|
||||
c->tm_year+1900, c->tm_mon+1, c->tm_mday,
|
||||
c->tm_hour, c->tm_min, c->tm_sec);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
unsigned char *hash = pcppubkey_getchecksum(key);
|
||||
output << " Checksum: ";
|
||||
z85 += " Checksum: ";
|
||||
|
||||
int i;
|
||||
for ( i = 0;i <15 ;++i) {
|
||||
sprintf(out, "%02X:",(unsigned int) hash[i]);
|
||||
output << out;
|
||||
z85 += out;
|
||||
}
|
||||
sprintf(out, "%02X", hash[15]);
|
||||
output << out;
|
||||
output << "\n ";
|
||||
z85 += out;
|
||||
z85 += "\n ";
|
||||
|
||||
for ( i = 16;i <31 ;++i) {
|
||||
sprintf(out, "%02X:",(unsigned int) hash[i]);
|
||||
output << out;
|
||||
z85 += out;
|
||||
}
|
||||
sprintf(out, "%02X", hash[31]);
|
||||
output << out;
|
||||
output << "\n";
|
||||
z85 += out;
|
||||
z85 += "\n";
|
||||
|
||||
sprintf(out, " Serial Number: 0x%08X\n", key->serial);
|
||||
output << out;
|
||||
z85 += out;
|
||||
sprintf(out, " Key Version: 0x%08X\n", key->version);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
char *r = pcppubkey_get_art(key);
|
||||
output << " Random Art ID: ";
|
||||
z85 += " Random Art ID: ";
|
||||
int rlen = strlen(r);
|
||||
|
||||
for (i=0; i<rlen; ++i) {
|
||||
if(r[i] == '\n') {
|
||||
output << "\n ";
|
||||
z85 += "\n ";
|
||||
}
|
||||
else {
|
||||
sprintf(out, "%c", r[i]);
|
||||
output << out;
|
||||
z85 += out;
|
||||
}
|
||||
}
|
||||
output << "\n";
|
||||
z85 += "\n";
|
||||
|
||||
sprintf(out, "\n%s\n", z85encoded);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "%s\n", PCP_PUBKEY_FOOTER);
|
||||
output << out;
|
||||
z85 += out;
|
||||
|
||||
free(z85encoded);
|
||||
|
||||
return z85;
|
||||
}
|
||||
|
||||
istream& pcp::operator>>(istream& input, PubKey& k) {
|
||||
string z85;
|
||||
input >> z85;
|
||||
k = PubKey(z85);
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
ostream& pcp::operator<<(ostream& output, PubKey& k) {
|
||||
output << k.to_text();
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -405,10 +494,6 @@ string PubKey::get_mail() {
|
||||
return m;
|
||||
}
|
||||
|
||||
pcp_pubkey_t *PubKey::get_key() {
|
||||
return K;
|
||||
}
|
||||
|
||||
void PubKey::is_stored(bool s) {
|
||||
stored = s;
|
||||
}
|
||||
|
||||
@@ -24,10 +24,9 @@
|
||||
#define _HAVE_PCPPP_H
|
||||
|
||||
#include <pcp.h>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
@@ -54,18 +53,29 @@ namespace pcp {
|
||||
|
||||
|
||||
|
||||
class ResultSet {
|
||||
public:
|
||||
std::string String;
|
||||
std::vector<unsigned char> Vector;
|
||||
unsigned char *Uchar;
|
||||
size_t Size;
|
||||
|
||||
~ResultSet() { free(Uchar); }
|
||||
};
|
||||
|
||||
|
||||
class PubKey {
|
||||
private:
|
||||
pcp_pubkey_t *K;
|
||||
bool stored;
|
||||
|
||||
public:
|
||||
pcp_pubkey_t *K;
|
||||
|
||||
// constructors
|
||||
PubKey();
|
||||
PubKey(const PubKey &k);
|
||||
PubKey(pcp_pubkey_t *k);
|
||||
PubKey(pcp_pubkey_t *k, bool store);
|
||||
PubKey(std::string &z85encoded);
|
||||
|
||||
// destructors
|
||||
~PubKey();
|
||||
@@ -73,13 +83,14 @@ namespace pcp {
|
||||
// operators
|
||||
PubKey& operator = (const PubKey &k);
|
||||
|
||||
|
||||
std::string get_id();
|
||||
std::string get_owner();
|
||||
std::string get_mail();
|
||||
pcp_pubkey_t *get_key();
|
||||
|
||||
void is_stored(bool s);
|
||||
bool is_stored();
|
||||
|
||||
std::string to_text();
|
||||
};
|
||||
|
||||
std::istream& operator>>(std::istream& input, PubKey& k);
|
||||
@@ -89,10 +100,12 @@ namespace pcp {
|
||||
|
||||
class Key {
|
||||
private:
|
||||
pcp_key_t *K;
|
||||
bool stored;
|
||||
|
||||
public:
|
||||
// make access to the underlying struct easier
|
||||
pcp_key_t *K;
|
||||
|
||||
// constructors
|
||||
Key();
|
||||
Key(bool generate);
|
||||
@@ -100,16 +113,16 @@ namespace pcp {
|
||||
Key(const std::string& passphrase,
|
||||
const std::string& owner,
|
||||
const std::string& mail);
|
||||
Key(const Key &k);
|
||||
Key(pcp_key_t *k);
|
||||
Key(pcp_key_t *k, bool store);
|
||||
Key(std::string &z85encoded);
|
||||
|
||||
// destructors
|
||||
// destructor
|
||||
~Key();
|
||||
|
||||
// operators
|
||||
Key& operator = (const Key &k);
|
||||
|
||||
|
||||
// methods
|
||||
void encrypt(const std::string& passphrase);
|
||||
void decrypt(const std::string& passphrase);
|
||||
@@ -117,19 +130,64 @@ namespace pcp {
|
||||
std::string get_id();
|
||||
std::string get_owner();
|
||||
std::string get_mail();
|
||||
pcp_key_t *get_key();
|
||||
|
||||
void set_owner(const std::string& owner);
|
||||
void set_mail(const std::string& mail);
|
||||
void is_stored(bool s);
|
||||
bool is_stored();
|
||||
bool is_encrypted();
|
||||
bool is_primary();
|
||||
|
||||
std::string to_text();
|
||||
|
||||
std::string encrypt(PubKey &recipient, std::vector<unsigned char> message);
|
||||
std::string encrypt(PubKey &recipient, std::string message);
|
||||
std::string encrypt(PubKey &recipient, unsigned char *message, size_t mlen);
|
||||
|
||||
ResultSet decrypt(PubKey &sender, std::string cipher);
|
||||
};
|
||||
|
||||
// << and >> operators
|
||||
std::istream& operator>>(std::istream& input, Key& k);
|
||||
std::ostream& operator<<(std::ostream& output, Key& k);
|
||||
|
||||
|
||||
typedef std::map<std::string, Key> KeyMap;
|
||||
typedef std::map<std::string, PubKey> PubKeyMap;
|
||||
|
||||
typedef std::map<std::string,Key>::iterator KeyIterator;
|
||||
typedef std::map<std::string,PubKey>::iterator PubKeyIterator;
|
||||
|
||||
// the vault
|
||||
class Vault {
|
||||
private:
|
||||
vault_t *V;
|
||||
|
||||
public:
|
||||
// constructors
|
||||
Vault();
|
||||
Vault(std::string filename);
|
||||
|
||||
// destructor
|
||||
~Vault();
|
||||
|
||||
// methods
|
||||
KeyMap keys();
|
||||
PubKeyMap pubkeys();
|
||||
|
||||
bool key_exists(std::string &id);
|
||||
bool pubkey_exists(std::string &id);
|
||||
|
||||
int key_count();
|
||||
int pubkey_count();
|
||||
|
||||
void key_add(Key &key);
|
||||
void pubkey_add(PubKey &key);
|
||||
|
||||
void key_delete(std::string &id);
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
110
bindings/cpp/vault.cpp
Normal file
110
bindings/cpp/vault.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
This file is part of Pretty Curved Privacy (pcp1).
|
||||
|
||||
Copyright (C) 2013 T.Linden.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||
*/
|
||||
|
||||
#include "pcp++.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pcp;
|
||||
|
||||
Vault::Vault() {
|
||||
V = NULL;
|
||||
}
|
||||
|
||||
Vault::Vault(string filename) {
|
||||
pcphash_init();
|
||||
V = pcpvault_init((char *)filename.c_str());
|
||||
if (V == NULL)
|
||||
throw pcp::exception();
|
||||
}
|
||||
|
||||
Vault::~Vault() {
|
||||
pcpvault_close(V);
|
||||
pcphash_clean();
|
||||
}
|
||||
|
||||
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)) );
|
||||
}
|
||||
|
||||
return kmap;
|
||||
}
|
||||
|
||||
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)) );
|
||||
}
|
||||
|
||||
return kmap;
|
||||
}
|
||||
|
||||
int Vault::key_count() {
|
||||
return pcphash_count();
|
||||
}
|
||||
|
||||
int Vault::pubkey_count() {
|
||||
return pcphash_countpub();
|
||||
}
|
||||
|
||||
void Vault::key_add(Key &key) {
|
||||
if(V->isnew == 1 || HASH_COUNT(pcpkey_hash) == 0) {
|
||||
key.K->type = PCP_KEY_TYPE_MAINSECRET;
|
||||
}
|
||||
|
||||
if(pcpvault_addkey(V, (void *)key.K, key.K->type) != 0)
|
||||
throw pcp::exception();
|
||||
key.is_stored(true);
|
||||
}
|
||||
|
||||
void Vault::pubkey_add(PubKey &key) {
|
||||
if(pcpvault_addkey(V, (void *)key.K, key.K->type) != 0)
|
||||
throw pcp::exception();
|
||||
key.is_stored(true);
|
||||
}
|
||||
|
||||
void Vault::key_delete(std::string &id) {
|
||||
pcp_pubkey_t *p = pcphash_pubkeyexists((char *)id.c_str());
|
||||
|
||||
if(p != NULL) {
|
||||
// delete public
|
||||
HASH_DEL(pcppubkey_hash, p);
|
||||
free(p);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
else {
|
||||
pcp_key_t *s = pcphash_keyexists((char *)id.c_str());
|
||||
if(s != NULL) {
|
||||
// delete secret
|
||||
HASH_DEL(pcpkey_hash, s);
|
||||
free(s);
|
||||
V->unsafed = 1;
|
||||
}
|
||||
else {
|
||||
throw exception("Key not found!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user