Added contract-based config file. (#4)

Added contract-based config file and command line flow. Added contract creation and rekey support.
This commit is contained in:
Ravin Perera
2019-09-27 15:55:49 +05:30
committed by GitHub
parent 0382475de1
commit 36fae44008
8 changed files with 252 additions and 114 deletions

View File

@@ -1,9 +1,7 @@
#include <cstdio>
#include <iostream>
#include <sodium.h>
#include <fstream>
#include "base64.h"
#include "lib/rapidjson/document.h"
#include "conf.h"
#include "crypto.h"
@@ -12,88 +10,32 @@ using namespace rapidjson;
namespace crypto
{
static const char CFG_FILE[] = "keys.cfg";
//Struct used for storing in JSON
struct KeyPairB64
{
const char *publicKey;
const char *privateKey;
};
//Struct used for crypto operations
struct KeyPairCrypto
{
unsigned char *publicKey;
unsigned char *privateKey;
};
static KeyPairB64 b64KeyPair;
static KeyPairCrypto cryptoKeyPair;
unsigned long long get_sig_len()
{
return crypto_sign_BYTES;
}
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig)
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig, const unsigned char *seckey)
{
crypto_sign_detached(sig, NULL, msg, msg_len, cryptoKeyPair.privateKey);
crypto_sign_detached(sig, NULL, msg, msg_len, seckey);
}
bool verify(const unsigned char *msg, unsigned long long msg_len, const unsigned char *sig)
bool verify(const unsigned char *msg, unsigned long long msg_len, const unsigned char *sig, const unsigned char *pubkey)
{
int result = crypto_sign_verify_detached(sig, msg, msg_len, cryptoKeyPair.publicKey);
int result = crypto_sign_verify_detached(sig, msg, msg_len, pubkey);
return result == 0;
}
void load_keys_b64()
{
Document d;
conf::load(CFG_FILE, d);
if (!d.IsNull())
{
b64KeyPair.publicKey = d["public"].GetString();
b64KeyPair.privateKey = d["private"].GetString();
}
else
{
b64KeyPair.publicKey = NULL;
b64KeyPair.privateKey = NULL;
}
}
void save_keys_b64()
{
Document d;
d.SetObject();
Document::AllocatorType &allocator = d.GetAllocator();
d.AddMember("public", StringRef(b64KeyPair.publicKey), allocator);
d.AddMember("private", StringRef(b64KeyPair.privateKey), allocator);
conf::save(CFG_FILE, d);
}
void cryptopair_to_b64()
{
string b64PubKey = base64_encode(cryptoKeyPair.publicKey, crypto_sign_PUBLICKEYBYTES);
string b64PrivKey = base64_encode(cryptoKeyPair.privateKey, crypto_sign_SECRETKEYBYTES);
char *b64PubKeyChar = (char *)malloc(b64PubKey.size() + 1);
char *b64PrivKeyChar = (char *)malloc(b64PrivKey.size() + 1);
strcpy(b64PubKeyChar, &b64PubKey[0]);
strcpy(b64PrivKeyChar, &b64PrivKey[0]);
b64KeyPair.publicKey = b64PubKeyChar;
b64KeyPair.privateKey = b64PrivKeyChar;
conf::cfg.pubkeyb64 = base64_encode(conf::cfg.pubkey, crypto_sign_PUBLICKEYBYTES);
conf::cfg.seckeyb64 = base64_encode(conf::cfg.seckey, crypto_sign_SECRETKEYBYTES);
}
void b64pair_to_crypto()
{
vector<unsigned char> pubDecoded = base64_decode(b64KeyPair.publicKey);
vector<unsigned char> privDecoded = base64_decode(b64KeyPair.privateKey);
vector<unsigned char> pubDecoded = base64_decode(conf::cfg.pubkeyb64);
vector<unsigned char> privDecoded = base64_decode(conf::cfg.seckeyb64);
unsigned char *pubDecodedBytes = (unsigned char *)malloc(pubDecoded.size());
unsigned char *privDecodedBytes = (unsigned char *)malloc(privDecoded.size());
@@ -107,18 +49,27 @@ void b64pair_to_crypto()
privDecodedBytes[i] = privDecoded[i];
}
cryptoKeyPair.publicKey = pubDecodedBytes;
cryptoKeyPair.privateKey = privDecodedBytes;
if (conf::cfg.pubkey != NULL)
free(conf::cfg.pubkey);
if (conf::cfg.seckey != NULL)
free(conf::cfg.seckey);
conf::cfg.pubkey = pubDecodedBytes;
conf::cfg.seckey = privDecodedBytes;
}
void generate_crypto_keys()
{
unsigned char *pubKey = (unsigned char *)malloc(crypto_sign_PUBLICKEYBYTES);
unsigned char *privKey = (unsigned char *)malloc(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(pubKey, privKey);
if (conf::cfg.pubkey != NULL)
free(conf::cfg.pubkey);
cryptoKeyPair.publicKey = pubKey;
cryptoKeyPair.privateKey = privKey;
if (conf::cfg.seckey != NULL)
free(conf::cfg.seckey);
conf::cfg.pubkey = (unsigned char *)malloc(crypto_sign_PUBLICKEYBYTES);
conf::cfg.seckey = (unsigned char *)malloc(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(conf::cfg.pubkey, conf::cfg.seckey);
}
int init()
@@ -129,20 +80,17 @@ int init()
return 0;
}
load_keys_b64();
//If any keys are missing generate a new pair and save to file.
if (!b64KeyPair.publicKey || !b64KeyPair.privateKey)
if (conf::cfg.pubkeyb64.empty() || conf::cfg.seckeyb64.empty())
{
cout << "Keys not found. Generating.\n";
cout << "Generating new keys.\n";
generate_crypto_keys();
cryptopair_to_b64();
save_keys_b64();
conf::save_config();
}
else
{
b64pair_to_crypto();
cout << "Keys loaded from file.\n";
}
return 1;