c++ binding now supports vaults and encryption, added test program

This commit is contained in:
TLINDEN
2013-12-01 16:16:53 +01:00
parent dc5f74e9be
commit aa140ed1c8
9 changed files with 465 additions and 109 deletions

View File

@@ -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;
}