mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
changed z85 header and comment syntax and parser
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "helpers++.h"
|
||||
#include "buffer++.h"
|
||||
|
||||
namespace pcp {
|
||||
|
||||
@@ -57,8 +58,6 @@ namespace pcp {
|
||||
|
||||
void is_stored(bool s);
|
||||
bool is_stored();
|
||||
|
||||
std::string to_text();
|
||||
};
|
||||
|
||||
bool operator!(PubKey& k);
|
||||
@@ -83,7 +82,7 @@ namespace pcp {
|
||||
const std::string& mail);
|
||||
Key(pcp_key_t *k);
|
||||
Key(pcp_key_t *k, bool store);
|
||||
Key(std::string &z85encoded);
|
||||
Key(std::string &z85encoded, std::string& passphrase);
|
||||
|
||||
// destructor
|
||||
~Key();
|
||||
@@ -106,12 +105,14 @@ namespace pcp {
|
||||
bool is_encrypted();
|
||||
bool is_primary();
|
||||
|
||||
std::string to_text();
|
||||
std::string export_secret(const std::string& passphrase);
|
||||
std::string export_public();
|
||||
|
||||
};
|
||||
|
||||
// << and >> operators
|
||||
bool operator!(Key& k);
|
||||
std::ostream& operator<<(std::ostream& output, Key& k);
|
||||
//std::ostream& operator<<(std::ostream& output, Key& k);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -62,39 +62,23 @@ Key::Key(pcp_key_t *k, bool store) {
|
||||
K = k;
|
||||
}
|
||||
|
||||
Key::Key(string &z85encoded) {
|
||||
Key::Key(string &z85encoded, string &passphrase) {
|
||||
stored = false;
|
||||
|
||||
if(z85encoded.length() == 0)
|
||||
throw pcp::exception("Error: zero length input");
|
||||
|
||||
size_t clen;
|
||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85encoded.c_str(), &clen);
|
||||
pcp_key_t *key = pcp_import_secret((unsigned char *)z85encoded.c_str(), z85encoded.length(), (char *)passphrase.c_str());
|
||||
|
||||
if(z85decoded == NULL)
|
||||
throw pcp::exception("Error: could not decode input - it's probably not Z85.\n");
|
||||
|
||||
if(clen != PCP_RAW_KEYSIZE) {
|
||||
free(z85decoded);
|
||||
char m[256];
|
||||
sprintf(m, "Error: decoded input didn't result to a proper sized key (got %ld bytes)!\n", clen);
|
||||
throw pcp::exception(string(m));
|
||||
}
|
||||
|
||||
// all good now, import the blob
|
||||
pcp_key_t *key = (pcp_key_t *)ucmalloc(sizeof(pcp_key_t));
|
||||
memcpy(key, z85decoded, PCP_RAW_KEYSIZE);
|
||||
key2native(key);
|
||||
if(key == NULL)
|
||||
throw pcp::exception();
|
||||
|
||||
if(pcp_sanitycheck_key(key) != 0) {
|
||||
free(key);
|
||||
free(z85decoded);
|
||||
throw pcp::exception();
|
||||
}
|
||||
|
||||
K = key;
|
||||
cout << 7 << " false" << endl;
|
||||
|
||||
}
|
||||
|
||||
Key::~Key() {
|
||||
@@ -108,70 +92,42 @@ Key& Key::operator = (const Key &k) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
string Key::to_text() {
|
||||
size_t zlen;
|
||||
pcp_key_t *key = K;
|
||||
string Key::export_secret(const string &passphrase) {
|
||||
Buffer *exported_sk;
|
||||
|
||||
key2be(key);
|
||||
void *blob = ucmalloc(PCP_RAW_KEYSIZE);
|
||||
pcp_seckeyblob(blob, key);
|
||||
char *z85encoded = pcp_z85_encode((unsigned char*)blob, PCP_RAW_KEYSIZE, &zlen);
|
||||
if(passphrase.length() == 0)
|
||||
throw pcp::exception("Error: empty passphrase");
|
||||
|
||||
if(PCP_ERRSET == 1)
|
||||
exported_sk = pcp_export_secret(K, (char *)passphrase.c_str());
|
||||
|
||||
if(exported_sk == NULL)
|
||||
throw pcp::exception();
|
||||
|
||||
key2native(key);
|
||||
size_t zlen;
|
||||
char *z85 = pcp_z85_encode(buffer_get(exported_sk), buffer_size(exported_sk), &zlen);
|
||||
|
||||
free(blob);
|
||||
string out = string(EXP_SK_HEADER) + "\r\n" + string(z85) + "\r\n" + string(EXP_SK_FOOTER) + "\r\n";
|
||||
|
||||
struct tm *c;
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
|
||||
string z85;
|
||||
char *out = (char *)ucmalloc(2048);
|
||||
|
||||
sprintf(out, "%s\n", PCP_KEY_HEADER);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Generated by: %s Version %d.%d.%d\n",
|
||||
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Key-ID: 0x%s\n", key->id);
|
||||
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);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Serial Number: 0x%08X\n", key->serial);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Key Version: 0x%08X\n", key->version);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "\n%s\n", z85encoded);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "%s\n", PCP_KEY_FOOTER);
|
||||
z85 += out;
|
||||
|
||||
free(z85encoded);
|
||||
|
||||
return z85;
|
||||
return out;
|
||||
}
|
||||
|
||||
ostream& pcp::operator<<(ostream& output, Key& k) {
|
||||
output << k.to_text();
|
||||
return output;
|
||||
string Key::export_public() {
|
||||
Buffer *exported_pk;
|
||||
|
||||
exported_pk = pcp_export_rfc_pub(K);
|
||||
|
||||
if(exported_pk == NULL)
|
||||
throw pcp::exception();
|
||||
|
||||
size_t zlen;
|
||||
char *z85 = pcp_z85_encode(buffer_get(exported_pk), buffer_size(exported_pk), &zlen);
|
||||
|
||||
string out = string(EXP_PK_HEADER) + "\r\n" + string(z85) + "\r\n" + string(EXP_PK_FOOTER) + "\r\n";
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
bool pcp::operator!(Key& k) {
|
||||
if(k.K == NULL)
|
||||
return true;
|
||||
@@ -179,6 +135,7 @@ bool pcp::operator!(Key& k) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Key::encrypt(const string& passphrase) {
|
||||
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
|
||||
if(PCP_ERRSET == 1)
|
||||
@@ -254,21 +211,18 @@ PubKey::PubKey(pcp_pubkey_t *k, bool store) {
|
||||
K = k;
|
||||
}
|
||||
|
||||
|
||||
// FIXME: use Buffer class for stuff like this
|
||||
PubKey::PubKey(string &z85encoded) {
|
||||
stored = false;
|
||||
|
||||
if(z85encoded.length() == 0)
|
||||
throw pcp::exception("Error: zero length input");
|
||||
|
||||
Buffer *blob = buffer_new(256, "pub");
|
||||
buffer_add(blob, z85encoded.c_str(), z85encoded.length());
|
||||
Buf blob("pub", 256);
|
||||
blob.add(z85encoded.c_str(), z85encoded.length());
|
||||
|
||||
pcp_ks_bundle_t *KS = pcp_import_pub(buffer_get(blob), buffer_size(blob));
|
||||
pcp_ks_bundle_t *KS = pcp_import_pub(buffer_get(blob.get_buffer()), buffer_size(blob.get_buffer()));
|
||||
|
||||
if(KS == NULL) {
|
||||
buffer_free(blob);
|
||||
throw pcp::exception();
|
||||
}
|
||||
pcp_pubkey_t *pub = KS->p;
|
||||
@@ -277,7 +231,6 @@ PubKey::PubKey(string &z85encoded) {
|
||||
free(KS->p);
|
||||
free(KS->s);
|
||||
free(KS);
|
||||
buffer_free(blob);
|
||||
throw pcp::exception();
|
||||
}
|
||||
|
||||
@@ -295,104 +248,6 @@ PubKey& PubKey::operator = (const PubKey &k) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
string PubKey::to_text() {
|
||||
size_t zlen;
|
||||
pcp_pubkey_t *key = K;
|
||||
|
||||
pubkey2be(key);
|
||||
void *blob = ucmalloc(PCP_RAW_PUBKEYSIZE);
|
||||
pcp_pubkeyblob(blob, key);
|
||||
char *z85encoded = pcp_z85_encode((unsigned char*)blob, PCP_RAW_PUBKEYSIZE, &zlen);
|
||||
|
||||
if(PCP_ERRSET == 1)
|
||||
throw pcp::exception();
|
||||
|
||||
pubkey2native(key);
|
||||
|
||||
free(blob);
|
||||
|
||||
struct tm *c;
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
|
||||
char *out = (char *)ucmalloc(2048);
|
||||
string z85;
|
||||
|
||||
sprintf(out, "%s\n", PCP_PUBKEY_HEADER);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Generated by: %s Version %d.%d.%d\n",
|
||||
PCP_ME, PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " Cipher: %s\n", PCP_KEY_PRIMITIVE);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, " PubKey-ID: 0x%s\n", key->id);
|
||||
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);
|
||||
z85 += out;
|
||||
|
||||
unsigned char *hash = pcppubkey_getchecksum(key);
|
||||
z85 += " Checksum: ";
|
||||
|
||||
int i;
|
||||
for ( i = 0;i <15 ;++i) {
|
||||
sprintf(out, "%02X:",(unsigned int) hash[i]);
|
||||
z85 += out;
|
||||
}
|
||||
sprintf(out, "%02X", hash[15]);
|
||||
z85 += out;
|
||||
z85 += "\n ";
|
||||
|
||||
for ( i = 16;i <31 ;++i) {
|
||||
sprintf(out, "%02X:",(unsigned int) hash[i]);
|
||||
z85 += out;
|
||||
}
|
||||
sprintf(out, "%02X", hash[31]);
|
||||
z85 += out;
|
||||
z85 += "\n";
|
||||
|
||||
sprintf(out, " Serial Number: 0x%08X\n", key->serial);
|
||||
z85 += out;
|
||||
sprintf(out, " Key Version: 0x%08X\n", key->version);
|
||||
z85 += out;
|
||||
|
||||
char *r = pcppubkey_get_art(key);
|
||||
z85 += " Random Art ID: ";
|
||||
int rlen = strlen(r);
|
||||
|
||||
for (i=0; i<rlen; ++i) {
|
||||
if(r[i] == '\n') {
|
||||
z85 += "\n ";
|
||||
}
|
||||
else {
|
||||
sprintf(out, "%c", r[i]);
|
||||
z85 += out;
|
||||
}
|
||||
}
|
||||
z85 += "\n";
|
||||
|
||||
sprintf(out, "\n%s\n", z85encoded);
|
||||
z85 += out;
|
||||
|
||||
sprintf(out, "%s\n", PCP_PUBKEY_FOOTER);
|
||||
z85 += out;
|
||||
|
||||
free(z85encoded);
|
||||
|
||||
return z85;
|
||||
}
|
||||
|
||||
ostream& pcp::operator<<(ostream& output, PubKey& k) {
|
||||
output << k.to_text();
|
||||
return output;
|
||||
}
|
||||
|
||||
bool pcp::operator!(PubKey& k) {
|
||||
if(k.K == NULL) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user