2013-11-28 19:36:50 +01:00
|
|
|
/*
|
|
|
|
|
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>.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-12-02 22:53:03 +01:00
|
|
|
#include "vault++.h"
|
|
|
|
|
#include "key++.h"
|
2013-11-28 19:36:50 +01:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace pcp;
|
|
|
|
|
|
|
|
|
|
Key::Key() {
|
|
|
|
|
stored = false;
|
|
|
|
|
K = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Key::Key(bool generate) {
|
|
|
|
|
stored = false;
|
2014-02-25 18:05:32 +01:00
|
|
|
if(generate)
|
|
|
|
|
K = pcpkey_new();
|
|
|
|
|
else
|
|
|
|
|
K = NULL;
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Key::Key(const string& passphrase) {
|
|
|
|
|
stored = false;
|
|
|
|
|
K = pcpkey_new();
|
2013-11-29 20:02:27 +01:00
|
|
|
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Key::Key(const string& passphrase,
|
|
|
|
|
const string& owner,
|
|
|
|
|
const string& mail) {
|
|
|
|
|
stored = false;
|
2013-11-29 20:02:27 +01:00
|
|
|
pcp_key_t *_K = pcpkey_new();
|
|
|
|
|
K = pcpkey_encrypt(_K, (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
memcpy(K->owner, owner.c_str(), owner.length()+1);
|
|
|
|
|
memcpy(K->mail, mail.c_str(), mail.length()+1);
|
2013-12-01 16:16:53 +01:00
|
|
|
// free(_K);
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-29 20:02:27 +01:00
|
|
|
Key::Key(pcp_key_t *k) {
|
2013-11-28 19:36:50 +01:00
|
|
|
stored = false;
|
|
|
|
|
K = k;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
Key::Key(pcp_key_t *k, bool store) {
|
|
|
|
|
stored = new bool(store);
|
|
|
|
|
K = k;
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
Key::Key(string &z85encoded, string &passphrase) {
|
2013-12-01 16:16:53 +01:00
|
|
|
stored = false;
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
if(z85encoded.length() == 0)
|
2013-11-29 20:02:27 +01:00
|
|
|
throw pcp::exception("Error: zero length input");
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
pcp_key_t *key = pcp_import_secret((unsigned char *)z85encoded.c_str(), z85encoded.length(), (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
if(key == NULL)
|
|
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
|
|
|
|
|
if(pcp_sanitycheck_key(key) != 0) {
|
|
|
|
|
free(key);
|
2013-11-29 20:02:27 +01:00
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
K = key;
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
Key::~Key() {
|
|
|
|
|
if (! stored) {
|
|
|
|
|
free(K);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 12:30:26 +01:00
|
|
|
Key& Key::operator = (const Key &k) {
|
2013-12-01 16:16:53 +01:00
|
|
|
K = k.K;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
string Key::export_secret(const string &passphrase) {
|
|
|
|
|
Buffer *exported_sk;
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
if(passphrase.length() == 0)
|
|
|
|
|
throw pcp::exception("Error: empty passphrase");
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
exported_sk = pcp_export_secret(K, (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
if(exported_sk == NULL)
|
|
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
size_t zlen;
|
|
|
|
|
char *z85 = pcp_z85_encode(buffer_get(exported_sk), buffer_size(exported_sk), &zlen);
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
string out = string(EXP_SK_HEADER) + "\r\n" + string(z85) + "\r\n" + string(EXP_SK_FOOTER) + "\r\n";
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
return out;
|
|
|
|
|
}
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
string Key::export_public() {
|
|
|
|
|
Buffer *exported_pk;
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
exported_pk = pcp_export_rfc_pub(K);
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
if(exported_pk == NULL)
|
|
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
size_t zlen;
|
|
|
|
|
char *z85 = pcp_z85_encode(buffer_get(exported_pk), buffer_size(exported_pk), &zlen);
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
string out = string(EXP_PK_HEADER) + "\r\n" + string(z85) + "\r\n" + string(EXP_PK_FOOTER) + "\r\n";
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
return out;
|
2013-12-01 16:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2013-12-02 22:53:03 +01:00
|
|
|
bool pcp::operator!(Key& k) {
|
|
|
|
|
if(k.K == NULL)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
|
2013-11-28 19:36:50 +01:00
|
|
|
void Key::encrypt(const string& passphrase) {
|
2013-11-29 20:02:27 +01:00
|
|
|
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
if(PCP_ERRSET == 1)
|
|
|
|
|
throw exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Key::decrypt(const string& passphrase) {
|
2013-11-29 20:02:27 +01:00
|
|
|
K = pcpkey_decrypt(K, (char *)passphrase.c_str());
|
2013-11-28 19:36:50 +01:00
|
|
|
if(PCP_ERRSET == 1)
|
|
|
|
|
throw exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PubKey Key::get_public() {
|
|
|
|
|
return PubKey(pcpkey_pub_from_secret(K));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Key::get_id() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string id = K->id;
|
2013-11-28 19:36:50 +01:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Key::get_owner() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string o = K->owner;
|
2013-11-28 19:36:50 +01:00
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Key::get_mail() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string m = K->mail;
|
2013-11-28 19:36:50 +01:00
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Key::set_owner(const string& owner) {
|
|
|
|
|
memcpy(K->owner, owner.c_str(), owner.length()+1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Key::set_mail(const string& mail) {
|
|
|
|
|
memcpy(K->mail, mail.c_str(), mail.length()+1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Key::is_stored(bool s) {
|
|
|
|
|
stored = s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Key::is_stored() {
|
|
|
|
|
return stored;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Key::is_encrypted() {
|
|
|
|
|
if(K->secret[0] == '\0')
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-02 22:53:03 +01:00
|
|
|
// class Key ends here.
|
2013-11-28 19:36:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PubKey::PubKey() {
|
|
|
|
|
stored = false;
|
|
|
|
|
K = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-29 20:02:27 +01:00
|
|
|
PubKey::PubKey(pcp_pubkey_t *k) {
|
2013-11-28 19:36:50 +01:00
|
|
|
stored = false;
|
|
|
|
|
K = k;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
PubKey::PubKey(pcp_pubkey_t *k, bool store) {
|
|
|
|
|
stored = store;
|
|
|
|
|
K = k;
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
PubKey::PubKey(string &z85encoded) {
|
|
|
|
|
stored = false;
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
if(z85encoded.length() == 0)
|
2013-11-29 20:02:27 +01:00
|
|
|
throw pcp::exception("Error: zero length input");
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
Buf blob("pub", 256);
|
|
|
|
|
blob.add(z85encoded.c_str(), z85encoded.length());
|
2013-12-02 22:53:03 +01:00
|
|
|
|
2014-02-24 16:59:04 +01:00
|
|
|
pcp_ks_bundle_t *KS = pcp_import_pub(buffer_get(blob.get_buffer()), buffer_size(blob.get_buffer()));
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-14 16:40:09 +01:00
|
|
|
if(KS == NULL) {
|
|
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
2014-02-14 16:40:09 +01:00
|
|
|
pcp_pubkey_t *pub = KS->p;
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2014-02-14 16:40:09 +01:00
|
|
|
if(pcp_sanitycheck_pub(pub) != 0) {
|
|
|
|
|
free(KS->p);
|
|
|
|
|
free(KS->s);
|
|
|
|
|
free(KS);
|
2013-11-29 20:02:27 +01:00
|
|
|
throw pcp::exception();
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-14 16:40:09 +01:00
|
|
|
K = pub;
|
2013-12-01 16:16:53 +01:00
|
|
|
}
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2013-12-01 16:16:53 +01:00
|
|
|
PubKey::~PubKey() {
|
|
|
|
|
if (! stored) {
|
|
|
|
|
free(K);
|
|
|
|
|
}
|
2013-11-28 19:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-19 12:30:26 +01:00
|
|
|
PubKey& PubKey::operator = (const PubKey &k) {
|
2013-12-01 16:16:53 +01:00
|
|
|
K = k.K;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2013-11-28 19:36:50 +01:00
|
|
|
|
2013-12-02 22:53:03 +01:00
|
|
|
bool pcp::operator!(PubKey& k) {
|
|
|
|
|
if(k.K == NULL) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 19:36:50 +01:00
|
|
|
string PubKey::get_id() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string id = K->id;
|
2013-11-28 19:36:50 +01:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string PubKey::get_owner() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string o = K->owner;
|
2013-11-28 19:36:50 +01:00
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string PubKey::get_mail() {
|
2013-11-29 20:02:27 +01:00
|
|
|
string m = K->mail;
|
2013-11-28 19:36:50 +01:00
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PubKey::is_stored(bool s) {
|
|
|
|
|
stored = s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PubKey::is_stored() {
|
|
|
|
|
return stored;
|
|
|
|
|
}
|
|
|
|
|
|