Skip to content

Commit

Permalink
- opmsg: more d_ prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
stealth committed May 8, 2018
1 parent aeba26a commit 94255c6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
44 changes: 22 additions & 22 deletions src/keystore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,22 @@ int keystore::load(const string &hex, uint32_t how)
// tells us to just load a single persona of this id
if (hex.size() > 16) {
// was already loaded? ->load() may be called multiple times
if (personas.count(hex) > 0)
if (d_personas.count(hex) > 0)
return 0;

unique_ptr<persona> p(new (nothrow) persona(cfgbase, hex));
unique_ptr<persona> p(new (nothrow) persona(d_cfgbase, hex));
if (!p.get())
return build_error("keystore::load:: OOM", -1);
if (p->load("", how) < 0)
return build_error("keystore::load::" + string(p->why()), -1);
personas[hex] = p.release();
d_personas[hex] = p.release();
return 0;
}

persona *p = nullptr;
string dhex = "";

DIR *d = opendir(cfgbase.c_str());
DIR *d = opendir(d_cfgbase.c_str());
if (!d)
return build_error("load::opendir:", -1);

Expand All @@ -207,10 +207,10 @@ int keystore::load(const string &hex, uint32_t how)
if (hex.size() == 16 && dhex.find(hex) != 0)
continue;

if (personas.count(dhex) > 0)
if (d_personas.count(dhex) > 0)
continue;

p = new (nothrow) persona(cfgbase, dhex);
p = new (nothrow) persona(d_cfgbase, dhex);
if (!p)
break;

Expand All @@ -219,7 +219,7 @@ int keystore::load(const string &hex, uint32_t how)
delete p;
continue;
}
personas[dhex] = p;
d_personas[dhex] = p;

// short id was given, no more loads after success
if (hex.size() == 16)
Expand Down Expand Up @@ -303,8 +303,8 @@ int gen_ec(string &pub, string &priv, int nid, string &err)

int keystore::gen_ec(string &pub, string &priv, int nid)
{
err = "keystore::";
return opmsg::gen_ec(pub, priv, nid, err);
d_err = "keystore::";
return opmsg::gen_ec(pub, priv, nid, d_err);
}


Expand Down Expand Up @@ -378,14 +378,14 @@ persona *keystore::find_persona(const std::string &hex)

// try to find 64bit shortcuts
if (hex.size() == 16) {
for (auto i : personas) {
for (auto i : d_personas) {
if (i.first.find(hex) == 0)
return i.second;
}
}

auto i = personas.find(hex);
if (i == personas.end())
auto i = d_personas.find(hex);
if (i == d_personas.end())
return build_error("find_persona: No such persona.", nullptr);
return i->second;
}
Expand All @@ -401,11 +401,11 @@ persona *keystore::add_persona(const string &name, const string &c_pub_pem, cons
// create hash (hex view) of public part and use as a reference
string hex = "";
string pub_pem = c_pub_pem;
if (normalize_and_hexhash(md, pub_pem, hex) < 0)
if (normalize_and_hexhash(d_md, pub_pem, hex) < 0)
return build_error("add_persona: Invalid pubkey blob. Missing BEGIN/END markers?", nullptr);

string tmpdir;
if (mkdir_helper(cfgbase, tmpdir) < 0)
if (mkdir_helper(d_cfgbase, tmpdir) < 0)
return build_error("add_persona::mkdir:", nullptr);

if (name.size() > 0) {
Expand Down Expand Up @@ -474,7 +474,7 @@ persona *keystore::add_persona(const string &name, const string &c_pub_pem, cons
close(fd);
}

string hexdir = cfgbase + "/" + hex;
string hexdir = d_cfgbase + "/" + hex;
if (rename(tmpdir.c_str(), hexdir.c_str()) < 0) {
int saved_errno = errno;
unlink(string(tmpdir + "/" + type1 + ".priv.pem").c_str());
Expand All @@ -485,7 +485,7 @@ persona *keystore::add_persona(const string &name, const string &c_pub_pem, cons
return build_error("add_persona::rename: Error creating persona " + hex, nullptr);
}

unique_ptr<persona> p(new (nothrow) persona(cfgbase, hex, name));
unique_ptr<persona> p(new (nothrow) persona(d_cfgbase, hex, name));
if (!p.get())
return build_error("add_persona::OOM", nullptr);

Expand All @@ -503,20 +503,20 @@ persona *keystore::add_persona(const string &name, const string &c_pub_pem, cons
}

// do not free evp_ structs and persona
personas[hex] = p.get();
d_personas[hex] = p.get();
return p.release();
}


map<string, persona *>::iterator keystore::first_pers()
{
return personas.begin();
return d_personas.begin();
}


map<string, persona *>::iterator keystore::end_pers()
{
return personas.end();
return d_personas.end();
}


Expand Down Expand Up @@ -908,7 +908,7 @@ DHbox *persona::new_dh_params(const string &pem)
if (!d_dh_params)
return build_error("new_dh_params::OOM", nullptr);

d_dh_params->pub_pem = pem;
d_dh_params->d_pub_pem = pem;

return d_dh_params;
}
Expand Down Expand Up @@ -967,7 +967,7 @@ DHbox *persona::new_dh_params()
if (!d_dh_params)
return build_error("new_dh_params::OOM", nullptr);

d_dh_params->pub_pem = string(buf, r);
d_dh_params->d_pub_pem = string(buf, r);

// do not call DH_free(dh)

Expand Down Expand Up @@ -1134,7 +1134,7 @@ int persona::gen_dh_key(const EVP_MD *md, string &pub, string &priv, string &hex
if (!d_dh_params)
return build_error("gen_dh_key: Invalid persona. No DH params for " + d_id, -1);

unique_ptr<DH, DH_del> dh(DHparams_dup(d_dh_params->pub), DH_free);
unique_ptr<DH, DH_del> dh(DHparams_dup(d_dh_params->d_pub), DH_free);
if (!dh.get() || DH_generate_key(dh.get()) != 1 || DH_check(dh.get(), &ecode) != 1)
return build_error("gen_dh_key::DH_generate_key: Error generating DH key for " + d_id, -1);

Expand Down
62 changes: 31 additions & 31 deletions src/keystore.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,30 @@ class DHbox {

public:

DH *pub, *priv;
DH *d_pub{nullptr}, *d_priv{nullptr};

std::string pub_pem, priv_pem, hex;
std::string d_pub_pem{""}, d_priv_pem{""}, d_hex{""};

DHbox(DH *dh1, DH *dh2) : pub(dh1), priv(dh2), pub_pem(""), priv_pem(""), hex("")
DHbox(DH *dh1, DH *dh2) : d_pub(dh1), d_priv(dh2)
{
}

virtual ~DHbox()
{
if (pub)
DH_free(pub);
if (priv)
DH_free(priv);
if (d_pub)
DH_free(d_pub);
if (d_priv)
DH_free(d_priv);
}

bool can_decrypt()
{
return priv != nullptr;
return d_priv != nullptr;
}

bool can_encrypt()
{
return pub != nullptr;
return d_pub != nullptr;
}
};

Expand All @@ -156,18 +156,18 @@ typedef enum : uint32_t {

class persona {

std::string d_id, d_name, d_link_src, d_ptype;
std::string d_id{""}, d_name{""}, d_link_src{""}, d_ptype{""};

// The (EC)DH 'session' keys this persona holds
std::map<std::string, std::vector<PKEYbox *>> d_keys;

// List of hashes of all imported keys so far
std::map<std::string, unsigned int> d_imported;

PKEYbox *d_pkey;
DHbox *d_dh_params;
PKEYbox *d_pkey{nullptr};
DHbox *d_dh_params{nullptr};

std::string d_cfgbase, d_err;
std::string d_cfgbase{""}, d_err{""};

template<class T>
T build_error(const std::string &msg, T r)
Expand All @@ -193,7 +193,7 @@ class persona {
public:

persona(const std::string &dir, const std::string &hash, const std::string &n = "")
: d_id(hash), d_name(n), d_link_src(""), d_pkey(nullptr), d_dh_params(nullptr), d_cfgbase(dir), d_err("")
: d_id(hash), d_name(n), d_cfgbase(dir)
{
if (!is_hex_hash(d_id))
d_id = "dead";
Expand Down Expand Up @@ -248,7 +248,7 @@ class persona {
bool can_kex_gen()
{
if (d_ptype == marker::rsa)
return d_dh_params != nullptr && d_dh_params->pub != nullptr;
return d_dh_params != nullptr && d_dh_params->d_pub != nullptr;
return true;
}

Expand Down Expand Up @@ -329,27 +329,27 @@ class persona {

class keystore {

std::string cfgbase;
std::map<std::string, persona *> personas;
std::string d_cfgbase{""};
std::map<std::string, persona *> d_personas;

const EVP_MD *md;
const EVP_MD *d_md{nullptr};

std::string err;
std::string d_err{""};

template<class T>
T build_error(const std::string &msg, T r)
{
int e = 0;
err = "keystore::";
err += msg;
d_err = "keystore::";
d_err += msg;
if ((e = ERR_get_error())) {
ERR_load_crypto_strings();
err += ":";
err += ERR_error_string(e, nullptr);
d_err += ":";
d_err += ERR_error_string(e, nullptr);
ERR_clear_error();
} else if (errno) {
err += ":";
err += strerror(errno);
d_err += ":";
d_err += strerror(errno);
}
errno = 0;
return r;
Expand All @@ -358,21 +358,21 @@ class keystore {
public:

keystore(const std::string& hash, const std::string &base = ".opmsg")
: cfgbase(base), md(nullptr)
: d_cfgbase(base)
{
md = algo2md(hash);
d_md = algo2md(hash);
}


~keystore()
{
for (auto i : personas)
for (auto i : d_personas)
delete i.second;
}

const EVP_MD *md_type()
{
return md;
return d_md;
}

int load(const std::string &id = "", uint32_t how = LFLAGS_ALL);
Expand All @@ -387,7 +387,7 @@ class keystore {

int size()
{
return personas.size();
return d_personas.size();
}

std::map<std::string, persona *>::iterator first_pers();
Expand All @@ -399,7 +399,7 @@ class keystore {

const char *why()
{
return err.c_str();
return d_err.c_str();
}
};

Expand Down

0 comments on commit 94255c6

Please sign in to comment.