mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
c++ binding now supports vaults and encryption, added test program
This commit is contained in:
3
TODO
3
TODO
@@ -1,3 +1,6 @@
|
||||
aix works now so far, but -R doesnt work - now it does but decryption fails for some unknown reason.
|
||||
|
||||
libpcp/z85.c:148 free(z85) leads to coredump on aix sometimes
|
||||
|
||||
add more abstract en+de-cryption() functions to libpcp/crypto, which
|
||||
work with already en+de-coded in+out-put.
|
||||
@@ -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);
|
||||
*this = PubKey(key);
|
||||
free(key);
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,8 @@
|
||||
# You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||
#
|
||||
|
||||
AM_CFLAGS = -I../include/pcp -Wall -g
|
||||
check_PROGRAMS = col invalidkeys pwhashes gencheader statictest
|
||||
AM_CFLAGS = -I../include/pcp -I../src -Wall -g
|
||||
check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest
|
||||
|
||||
gencheader_LDADD = ../libpcp/.libs/libpcp1.a
|
||||
gencheader_SOURCES = gencheader.c
|
||||
@@ -40,8 +40,9 @@ invalidkeys_SOURCES = invalidkeys.c
|
||||
pwhashes_LDADD = ../libpcp/.libs/libpcp1.a
|
||||
pwhashes_SOURCES = pwhashes.c
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir)/src
|
||||
|
||||
AM_CXXFLAGS = -I../include -I../bindings/cpp -Wall -g
|
||||
cpptest_LDADD = ../bindings/cpp/.libs/libpcp1++.a ../libpcp/.libs/libpcp1.a
|
||||
cpptest_SOURCES = cpptest.cpp
|
||||
|
||||
#
|
||||
# Note: some of these unit tests run only on freebsd.
|
||||
|
||||
@@ -71,7 +71,7 @@ POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
check_PROGRAMS = col$(EXEEXT) invalidkeys$(EXEEXT) pwhashes$(EXEEXT) \
|
||||
gencheader$(EXEEXT) statictest$(EXEEXT)
|
||||
gencheader$(EXEEXT) statictest$(EXEEXT) cpptest$(EXEEXT)
|
||||
subdir = tests
|
||||
DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in \
|
||||
$(top_srcdir)/config/depcomp
|
||||
@@ -90,6 +90,10 @@ CONFIG_CLEAN_VPATH_FILES =
|
||||
am_col_OBJECTS = collisions.$(OBJEXT) compat_getopt.$(OBJEXT)
|
||||
col_OBJECTS = $(am_col_OBJECTS)
|
||||
col_DEPENDENCIES = ../libpcp/.libs/libpcp1.a
|
||||
am_cpptest_OBJECTS = cpptest.$(OBJEXT)
|
||||
cpptest_OBJECTS = $(am_cpptest_OBJECTS)
|
||||
cpptest_DEPENDENCIES = ../bindings/cpp/.libs/libpcp1++.a \
|
||||
../libpcp/.libs/libpcp1.a
|
||||
am_gencheader_OBJECTS = gencheader.$(OBJEXT)
|
||||
gencheader_OBJECTS = $(am_gencheader_OBJECTS)
|
||||
gencheader_DEPENDENCIES = ../libpcp/.libs/libpcp1.a
|
||||
@@ -116,9 +120,19 @@ CCLD = $(CC)
|
||||
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(col_SOURCES) $(gencheader_SOURCES) $(invalidkeys_SOURCES) \
|
||||
$(pwhashes_SOURCES) $(statictest_SOURCES)
|
||||
DIST_SOURCES = $(col_SOURCES) $(gencheader_SOURCES) \
|
||||
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
LTCXXCOMPILE = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
CXXLD = $(CXX)
|
||||
CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(col_SOURCES) $(cpptest_SOURCES) $(gencheader_SOURCES) \
|
||||
$(invalidkeys_SOURCES) $(pwhashes_SOURCES) \
|
||||
$(statictest_SOURCES)
|
||||
DIST_SOURCES = $(col_SOURCES) $(cpptest_SOURCES) $(gencheader_SOURCES) \
|
||||
$(invalidkeys_SOURCES) $(pwhashes_SOURCES) \
|
||||
$(statictest_SOURCES)
|
||||
am__can_run_installinfo = \
|
||||
@@ -247,7 +261,7 @@ target_alias = @target_alias@
|
||||
top_build_prefix = @top_build_prefix@
|
||||
top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AM_CFLAGS = -I../include/pcp -Wall -g
|
||||
AM_CFLAGS = -I../include/pcp -I../src -Wall -g
|
||||
gencheader_LDADD = ../libpcp/.libs/libpcp1.a
|
||||
gencheader_SOURCES = gencheader.c
|
||||
statictest_LDADD = ../libpcp/.libs/libpcp1.a
|
||||
@@ -260,11 +274,13 @@ invalidkeys_LDADD = ../libpcp/.libs/libpcp1.a \
|
||||
invalidkeys_SOURCES = invalidkeys.c
|
||||
pwhashes_LDADD = ../libpcp/.libs/libpcp1.a
|
||||
pwhashes_SOURCES = pwhashes.c
|
||||
AM_CPPFLAGS = -I$(top_builddir)/src
|
||||
AM_CXXFLAGS = -I../include -I../bindings/cpp -Wall -g
|
||||
cpptest_LDADD = ../bindings/cpp/.libs/libpcp1++.a ../libpcp/.libs/libpcp1.a
|
||||
cpptest_SOURCES = cpptest.cpp
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .c .lo .o .obj
|
||||
.SUFFIXES: .c .cpp .lo .o .obj
|
||||
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
|
||||
@for dep in $?; do \
|
||||
case '$(am__configure_deps)' in \
|
||||
@@ -307,6 +323,9 @@ clean-checkPROGRAMS:
|
||||
col$(EXEEXT): $(col_OBJECTS) $(col_DEPENDENCIES) $(EXTRA_col_DEPENDENCIES)
|
||||
@rm -f col$(EXEEXT)
|
||||
$(LINK) $(col_OBJECTS) $(col_LDADD) $(LIBS)
|
||||
cpptest$(EXEEXT): $(cpptest_OBJECTS) $(cpptest_DEPENDENCIES) $(EXTRA_cpptest_DEPENDENCIES)
|
||||
@rm -f cpptest$(EXEEXT)
|
||||
$(CXXLINK) $(cpptest_OBJECTS) $(cpptest_LDADD) $(LIBS)
|
||||
gencheader$(EXEEXT): $(gencheader_OBJECTS) $(gencheader_DEPENDENCIES) $(EXTRA_gencheader_DEPENDENCIES)
|
||||
@rm -f gencheader$(EXEEXT)
|
||||
$(LINK) $(gencheader_OBJECTS) $(gencheader_LDADD) $(LIBS)
|
||||
@@ -328,6 +347,7 @@ distclean-compile:
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/collisions.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/compat_getopt.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpptest.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gencheader.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/invalidkeys.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pwhashes.Po@am__quote@
|
||||
@@ -368,6 +388,27 @@ compat_getopt.obj: ../src/compat_getopt.c
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o compat_getopt.obj `if test -f '../src/compat_getopt.c'; then $(CYGPATH_W) '../src/compat_getopt.c'; else $(CYGPATH_W) '$(srcdir)/../src/compat_getopt.c'; fi`
|
||||
|
||||
.cpp.o:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
.cpp.obj:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
|
||||
.cpp.lo:
|
||||
@am__fastdepCXX_TRUE@ $(LTCXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $<
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
|
||||
|
||||
57
tests/cpptest.cpp
Normal file
57
tests/cpptest.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <pcp++.h>
|
||||
#include <string>
|
||||
|
||||
using namespace pcp;
|
||||
using namespace std;
|
||||
|
||||
void pr(string name, unsigned char *data, size_t len) {
|
||||
int i;
|
||||
cout << name << ": ";
|
||||
for ( i = 0;i < len;++i)
|
||||
printf("%02x", (unsigned int) data[i]);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
try {
|
||||
Key A = Key("a", "alicia", "alicia@local");
|
||||
Key B = Key("b", "bobby", "bobby@local");
|
||||
PubKey PA = A.get_public();
|
||||
PubKey PB = B.get_public();
|
||||
|
||||
A.decrypt("a");
|
||||
B.decrypt("b");
|
||||
|
||||
pr("A secret", A.K->secret, 32);
|
||||
pr("A public", A.K->pub, 32);
|
||||
pr("B secret", B.K->secret, 32);
|
||||
pr("B public", B.K->pub, 32);
|
||||
|
||||
|
||||
string cipher = A.encrypt(PB, "Hallo");
|
||||
ResultSet res = B.decrypt(PA, cipher);
|
||||
|
||||
cout << " Input: Hallo" << endl;
|
||||
cout << "Cipher: " << cipher << endl;
|
||||
cout << " Clear: " << res.String << endl;
|
||||
|
||||
Vault vault = Vault("vcpp");
|
||||
vault.key_add(A);
|
||||
vault.pubkey_add(PB);
|
||||
|
||||
KeyMap m = vault.keys();
|
||||
for(KeyIterator it=m.begin(); it != m.end(); ++it) {
|
||||
cout << "id: " << it->first << endl;
|
||||
}
|
||||
|
||||
PubKeyMap p = vault.pubkeys();
|
||||
for(PubKeyIterator it=p.begin(); it != p.end(); ++it) {
|
||||
cout << "id: " << it->first << endl;
|
||||
}
|
||||
}
|
||||
catch (pcp::exception &E) {
|
||||
cerr << "Catched exception: " << E.what() << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user