using namespace std;
-namespace fides {
+namespace Fides {
+ static regexp authexp("^a[+0-] ");
+ static regexp trustexp("^t[+0-] ");
+
/// Saves a certificate to a file.
//
/// @param cert Certificate to save.
/// @param filename File to save the certificate to.
- void fides::certificate_save(const certificate *cert, const std::string &filename) const {
+ void Manager::certificate_save(const Certificate *cert, const std::string &filename) const {
ofstream file(filename.c_str());
file << cert->to_string() << '\n';
}
//
/// @param filename File to save the certificate to.
/// @return The certificate.
- certificate *fides::certificate_load(const std::string &filename) {
+ Certificate *Manager::certificate_load(const std::string &filename) {
ifstream file(filename.c_str());
string data;
getline(file, data);
//
/// @param data String containing the certificate in textual form.
/// @return The certificate.
- certificate *fides::certificate_from_string(const std::string &data) {
+ Certificate *Manager::certificate_from_string(const std::string &data) {
size_t b, e;
e = data.find(' ', 0);
if(e == string::npos)
- throw exception("Invalid certificate");
+ throw exception("Invalid Certificate");
string fingerprint = hexdecode(data.substr(0, e));
- const publickey *signer = find_key(fingerprint);
+ const PublicKey *signer = find_key(fingerprint);
if(!signer)
throw exception("Unknown public key");
b = e + 1;
e = data.find('.', b);
if(e == string::npos)
- throw exception("Invalid certificate");
+ throw exception("Invalid Certificate");
struct timeval timestamp;
timestamp.tv_sec = atol(data.c_str() + b);
b = e + 1;
timestamp.tv_usec = atol(data.c_str() + b);
e = data.find(' ', b);
if(e == string::npos)
- throw exception("Invalid certificate");
+ throw exception("Invalid Certificate");
b = e + 1;
e = data.find(' ', b);
if(e == string::npos)
- throw exception("Invalid certificate");
+ throw exception("Invalid Certificate");
string signature = b64decode(data.substr(b, e - b));
b = e + 1;
string statement = data.substr(b);
- return new certificate(signer, timestamp, statement, signature);
+ return new Certificate(signer, timestamp, statement, signature);
}
- /// \class fides
+ /// \class Manager
///
/// \brief Interaction with a Fides database.
///
- /// A fides object manages a database of public keys and certificates.
+ /// A Manager object manages a database of public keys and certificates.
/// New certificates can be created, certificates can be imported and exported,
/// and queries can be done on the database.
/// are used, in the given order:
/// - \$FIDES_HOME
/// - \$HOME/.fides
- /// - \$WPD/.fides
- fides::fides(const std::string &dir): homedir(dir) {
+ /// - \$PWD/.fides
+ Manager::Manager(const std::string &dir): homedir(dir) {
debug cerr << "Fides initialising\n";
// Set homedir to provided directory, or $FIDES_HOME, or $HOME/.fides, or as a last resort $PWD/.fides
try {
mykey.load_private(homedir + "priv");
firstrun = false;
- } catch(fides::exception &e) {
+ } catch(exception &e) {
cerr << "Fides generating keypair\n";
mykey.generate();
mykey.save_private(homedir + "priv");
for(size_t i = 0; i < files.size(); ++i) {
debug cerr << "Loading key " << files[i] << '\n';
- publickey *key = new publickey();
+ PublicKey *key = new PublicKey();
key->load(keydir + files[i]);
keys[hexdecode(files[i])] = key;
}
files = dirlist(certdir);
for(size_t i = 0; i < files.size(); ++i) {
- debug cerr << "Loading certificate " << files[i] << '\n';
- certificate *cert = certificate_load(certdir + files[i]);
+ debug cerr << "Loading Certificate " << files[i] << '\n';
+ Certificate *cert = certificate_load(certdir + files[i]);
if(false && !cert->validate()) {
- cerr << "Bad certificate in database: " << cert->to_string() << '\n';
+ cerr << "Bad Certificate in database: " << cert->to_string() << '\n';
continue;
}
certs[hexdecode(files[i])] = cert;
}
- // TODO: save and load this value
+ /// \TODO save and load this value
latest.tv_sec = 0;
latest.tv_usec = 0;
update_trust();
}
- fides::~fides() {
+ Manager::~Manager() {
debug cerr << "Fides exitting\n";
- for(map<string, certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i)
+ for(map<string, Certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i)
delete i->second;
- for(map<string, publickey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
+ for(map<string, PublicKey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
if(i->second != &mykey)
delete i->second;
}
/// Checks the validaty of all certificates.
//
/// @return True if all known certificates are valid, false otherwise.
- bool fides::fsck() const {
+ bool Manager::fsck() const {
int errors = 0;
- for(map<string, certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i) {
+ for(map<string, Certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i) {
if(!i->second->validate()) {
- cerr << "Validation of certificate failed: " << i->second->to_string() << '\n';
+ cerr << "Validation of Certificate failed: " << i->second->to_string() << '\n';
errors++;
}
}
- cerr << errors << " errors in " << certs.size() << " certificates\n";
+ cerr << errors << " errors in " << certs.size() << " Certificates\n";
return !errors;
}
/// Returns the base directory used by Fides.
//
/// @return The home directory.
- string fides::get_homedir() const {
+ string Manager::get_homedir() const {
return homedir;
}
/// Tests whether this is the first time Fides has run and has generated new keys.
//
/// @return True if this is the first time, false otherwise.
- bool fides::is_firstrun() const {
+ bool Manager::is_firstrun() const {
return firstrun;
}
//
/// @param fingerprint String containing a fingerprint.
/// @return Pointer to the public key corresponding to the fingerprint, or NULL if it was not found.
- publickey *fides::find_key(const std::string &fingerprint) const {
- map<string, publickey *>::const_iterator i;
+ PublicKey *Manager::find_key(const std::string &fingerprint) const {
+ map<string, PublicKey *>::const_iterator i;
i = keys.find(fingerprint);
if(i != keys.end())
return i->second;
/// @param signer Public key to match certificates to.
/// @param regex Regular expression to match the statement of each certificate to.
/// @return A vector of certificates that match the criteria.
- vector<const certificate *> fides::find_certificates(const publickey *signer, const std::string ®ex) const {
- vector<const certificate *> found;
- map<string, certificate *>::const_iterator i;
+ vector<const Certificate *> Manager::find_certificates(const PublicKey *signer, const std::string ®ex) const {
+ vector<const Certificate *> found;
+ map<string, Certificate *>::const_iterator i;
regexp regexp(regex);
for(i = certs.begin(); i != certs.end(); ++i) {
if(!i->second) {
- cerr << "No certificate for " << hexencode(i->first) << '\n';
+ cerr << "No Certificate for " << hexencode(i->first) << '\n';
continue;
}
if(i->second->signer == signer)
//
/// @param regex Regular expression to match the statement of each certificate to.
/// @return A vector of certificates that match the criteria.
- vector<const certificate *> fides::find_certificates(const std::string ®ex) const {
- vector<const certificate *> found;
- map<string, certificate *>::const_iterator i;
+ vector<const Certificate *> Manager::find_certificates(const std::string ®ex) const {
+ vector<const Certificate *> found;
+ map<string, Certificate *>::const_iterator i;
regexp regexp(regex);
for(i = certs.begin(); i != certs.end(); ++i)
if(regexp.match(i->second->statement))
//
/// @param signer Public key to match certificates to.
/// @return A vector of certificates that match the criteria.
- vector<const certificate *> fides::find_certificates(const publickey *signer) const {
- vector<const certificate *> found;
- map<string, certificate *>::const_iterator i;
+ vector<const Certificate *> Manager::find_certificates(const PublicKey *signer) const {
+ vector<const Certificate *> found;
+ map<string, Certificate *>::const_iterator i;
for(i = certs.begin(); i != certs.end(); ++i)
if(i->second->signer == signer)
found.push_back(i->second);
/// Import public keys and certificates from a stream.
//
/// @param in Stream to read from.
- void fides::import_all(std::istream &in) {
+ void Manager::import_all(std::istream &in) {
string line, pem;
bool is_pem = false;
if(is_pem || !line.compare(0, 11, "-----BEGIN ")) {
pem += line + '\n';
if(!line.compare(0, 9, "-----END ")) {
- publickey *key = new publickey();
+ PublicKey *key = new PublicKey();
key->from_string(pem);
debug cerr << "Imported key " << hexencode(key->fingerprint()) << '\n';
merge(key);
continue;
}
- certificate *cert = certificate_from_string(line);
- debug cerr << "Importing certificate " << hexencode(cert->fingerprint()) << '\n';
+ Certificate *cert = certificate_from_string(line);
+ debug cerr << "Importing Certificate " << hexencode(cert->fingerprint()) << '\n';
merge(cert);
}
}
/// Export all public keys and certificates to a stream.
//
/// @param out Stream to write to.
- void fides::export_all(std::ostream &out) const {
- for(map<string, publickey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
+ void Manager::export_all(std::ostream &out) const {
+ for(map<string, PublicKey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
out << i->second->to_string();
- for(map<string, certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i)
+ for(map<string, Certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i)
out << i->second->to_string() << '\n';
}
//
/// This creates a certificate that says that we trust the given public key.
/// If a key is trusted, then authorisation certificates from that key are taken into account
- /// when calling functions such as fides::is_allowed().
+ /// when calling functions such as Manager::is_allowed().
///
/// @param key Public key to trust.
- void fides::trust(const publickey *key) {
+ void Manager::trust(const PublicKey *key) {
string full = "t+ " + hexencode(key->fingerprint());
sign(full);
}
//
/// This creates a certificate that says that we distrust the given public key.
/// If a key is distrusted, then authorisation certificates from that key are not taken into account
- /// when calling functions such as fides::is_allowed().
+ /// when calling functions such as Manager::is_allowed().
///
/// @param key Public key to trust.
- void fides::distrust(const publickey *key) {
+ void Manager::distrust(const PublicKey *key) {
string full = "t- " + hexencode(key->fingerprint());
sign(full);
}
/// This key and certificates created by it are then treated as if we have never trusted nor distrusted this key.
///
/// @param key Public key to trust.
- void fides::dctrust(const publickey *key) {
+ void Manager::dctrust(const PublicKey *key) {
string full = "t0 " + hexencode(key->fingerprint());
sign(full);
}
/// Recalculate the trust value of all known public keys.
- void fides::update_trust() {
+ void Manager::update_trust() {
// clear trust on all keys
- for(map<string, publickey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
+ for(map<string, PublicKey *>::const_iterator i = keys.begin(); i != keys.end(); ++i)
i->second->trust = 0;
// Start by checking all trust certificates from ourself.
// Already checked keys are never updated anymore (TODO: is that smart?)
// Certificates of keys with a zero or negative trust score are not processed.
- set<publickey *> checked;
- set<publickey *> tocheck;
- set<publickey *> newkeys;
- set<publickey *>::iterator i;
+ set<PublicKey *> checked;
+ set<PublicKey *> tocheck;
+ set<PublicKey *> newkeys;
+ set<PublicKey *>::iterator i;
mykey.trust = 3;
tocheck.insert(&mykey);
// find all non-zero trust certificates of this key
- vector<const certificate *> matches = find_certificates(*i, "^t[+-] ");
+ vector<const Certificate *> matches = find_certificates(*i, "^t[+-] ");
// update trust value of those keys
for(size_t j = 0; j < matches.size(); j++) {
- publickey *other = find_key(hexdecode(matches[j]->statement.substr(3)));
+ PublicKey *other = find_key(hexdecode(matches[j]->statement.substr(3)));
if(!other) {
- cerr << "Trust certificate for unknown key: " << matches[j]->to_string() << '\n';
+ cerr << "Trust Certificate for unknown key: " << matches[j]->to_string() << '\n';
continue;
}
// except for keys we already checked
if(checked.find(other) != checked.end()) {
- debug cerr << "Skipping trust certificate for already checked key: " << matches[j]->to_string() << '\n';
+ debug cerr << "Skipping trust Certificate for already checked key: " << matches[j]->to_string() << '\n';
continue;
}
/// Merges a public key into the database.
//
/// @param key The public key to merge.
- void fides::merge(publickey *key) {
+ void Manager::merge(PublicKey *key) {
if(keys.find(key->fingerprint()) != keys.end()) {
debug cerr << "Key already known\n";
return;
/// the older certificate will be removed.
///
/// @param cert The certificate to merge.
- void fides::merge(certificate *cert) {
+ void Manager::merge(Certificate *cert) {
// TODO: check if cert is already in database
// TODO: check if cert obsoletes other certs
// If the certificate does not validate, drop it.
if(!cert->validate()) {
// TODO: this should not happen, be wary of DoS attacks
- cerr << "Trying to merge invalid certificate: " << cert->to_string() << '\n';
+ cerr << "Trying to merge invalid Certificate: " << cert->to_string() << '\n';
return;
}
- // TODO: move these regexps to the class?
- regexp authexp("^a[+0-] ");
- regexp trustexp("^t[+0-] ");
- vector<const certificate *> others;
+ vector<const Certificate *> others;
// Is this an authorisation cert?
if(authexp.match(cert->statement)) {
others = find_certificates(cert->signer, string("^a[+0-] ") + cert->statement.substr(3) + '$');
if(others.size()) {
if(timercmp(&others[0]->timestamp, &cert->timestamp, >)) {
- debug cerr << "Certificate is overruled by a newer certificate\n";
+ debug cerr << "Certificate is overruled by a newer Certificate\n";
return;
}
if(timercmp(&others[0]->timestamp, &cert->timestamp, ==)) {
debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- debug cerr << "Certificate overrules an older certificate!\n";
+ debug cerr << "Certificate overrules an older Certificate!\n";
// save new cert first
certificate_save(cert, certdir + hexencode(cert->fingerprint()));
certs[cert->fingerprint()] = cert;
others = find_certificates(cert->signer, string("^t[+0-] ") + cert->statement.substr(3) + '$');
if(others.size()) {
if(timercmp(&others[0]->timestamp, &cert->timestamp, >)) {
- debug cerr << "Certificate is overruled by a newer certificate\n";
+ debug cerr << "Certificate is overruled by a newer Certificate\n";
return;
}
if(timercmp(&others[0]->timestamp, &cert->timestamp, ==)) {
debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- debug cerr << "Certificate overrules an older certificate!\n";
+ debug cerr << "Certificate overrules an older Certificate!\n";
// delete old one
rename((certdir + hexencode(others[0]->fingerprint())).c_str(), (obsoletedir + hexencode(others[0]->fingerprint())).c_str());
certs.erase(others[0]->fingerprint());
others = find_certificates(cert->signer, string("^") + cert->statement + '$');
if(others.size()) {
if(timercmp(&others[0]->timestamp, &cert->timestamp, >)) {
- debug cerr << "Certificate is overruled by a newer certificate\n";
+ debug cerr << "Certificate is overruled by a newer Certificate\n";
return;
}
if(timercmp(&others[0]->timestamp, &cert->timestamp, ==)) {
debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- debug cerr << "Certificate overrules an older certificate!\n";
+ debug cerr << "Certificate overrules an older Certificate!\n";
// delete old one
rename((certdir + hexencode(others[0]->fingerprint())).c_str(), (obsoletedir + hexencode(others[0]->fingerprint())).c_str());
certs.erase(others[0]->fingerprint());
/// gave a positive authorisation, 0 if there is a tie,
/// or negative if the majority gave a negative authorisation.
/// @param all Same as trusted but for all public keys.
- void fides::auth_stats(const std::string &statement, int &self, int &trusted, int &all) const {
+ void Manager::auth_stats(const std::string &statement, int &self, int &trusted, int &all) const {
self = trusted = all = 0;
- vector<const certificate *> matches = find_certificates(string("^a[+0-] ") + statement + '$');
+ vector<const Certificate *> matches = find_certificates(string("^a[+0-] ") + statement + '$');
for(size_t i = 0; i < matches.size(); ++i) {
char code = matches[i]->statement[1];
int diff = 0;
//
/// @param key The public key to test.
/// @return True if the key is explicitly trusted, false otherwise.
- bool fides::is_trusted(const publickey *key) const {
+ bool Manager::is_trusted(const PublicKey *key) const {
return key->trust > 0;
}
//
/// @param key The public key to test.
/// @return True if the key is explicitly distrusted, false otherwise.
- bool fides::is_distrusted(const publickey *key) const {
+ bool Manager::is_distrusted(const PublicKey *key) const {
return key->trust < 0;
}
/// @param statement The statement to test.
/// @param key The public key to test.
/// @return True if the statement is allowed for the given key, false otherwise.
- bool fides::is_allowed(const std::string &statement, const publickey *key) const {
+ bool Manager::is_allowed(const std::string &statement, const PublicKey *key) const {
int self, trusted, all;
if(key)
/// @param statement The statement to test.
/// @param key The public key to test.
/// @return True if the statement is denied for the given key, false otherwise.
- bool fides::is_denied(const std::string &statement, const publickey *key) const {
+ bool Manager::is_denied(const std::string &statement, const PublicKey *key) const {
int self, trusted, all;
if(key)
/// Creates a certificate for the given statement.
//
/// @param statement The statement to create a certificate for.
- void fides::sign(const std::string &statement) {
+ void Manager::sign(const std::string &statement) {
// Try to set "latest" to now, but ensure monoticity
struct timeval now;
gettimeofday(&now, 0);
}
// Create a new certificate and merge it with our database
- merge(new certificate(&mykey, latest, statement));
+ merge(new Certificate(&mykey, latest, statement));
}
- void fides::allow(const std::string &statement, const publickey *key) {
+ void Manager::allow(const std::string &statement, const PublicKey *key) {
string full = "a+ ";
if(key)
full += hexencode(key->fingerprint()) + ' ';
sign(full);
}
- void fides::dontcare(const std::string &statement, const publickey *key) {
+ void Manager::dontcare(const std::string &statement, const PublicKey *key) {
string full = "a0 ";
if(key)
full += hexencode(key->fingerprint()) + ' ';
sign(full);
}
- void fides::deny(const std::string &statement, const publickey *key) {
+ void Manager::deny(const std::string &statement, const PublicKey *key) {
string full = "a- ";
if(key)
full += hexencode(key->fingerprint()) + ' ';
#include "privatekey.h"
#include "utility.h"
-namespace fides {
- class fides {
+namespace Fides {
+ class exception: public std::runtime_error {
+ public:
+ exception(const std::string reason): runtime_error(reason) {}
+ };
+
+ class Manager {
std::string homedir;
std::string certdir;
std::string obsoletedir;
bool firstrun;
struct timeval latest;
- public:
- // Utility functions
-
- class exception: public std::runtime_error {
- public:
- exception(const std::string reason): runtime_error(reason) {}
- };
- // Fides class itself
-
private:
- privatekey mykey;
- std::map<std::string, publickey *> keys;
- std::map<std::string, certificate *> certs;
+ PrivateKey mykey;
+ std::map<std::string, PublicKey *> keys;
+ std::map<std::string, Certificate *> certs;
- void merge(certificate *cert);
- void merge(publickey *key);
+ void merge(Certificate *cert);
+ void merge(PublicKey *key);
public:
- fides(const std::string &homedir = "");
- ~fides();
+ Manager(const std::string &homedir = "");
+ ~Manager();
bool is_firstrun() const;
bool fsck() const;
void sign(const std::string &statement);
- void allow(const std::string &statement, const publickey *key = 0);
- void dontcare(const std::string &statement, const publickey *key = 0);
- void deny(const std::string &statement, const publickey *key = 0);
- bool is_allowed(const std::string &statement, const publickey *key = 0) const;
- bool is_denied(const std::string &statement, const publickey *key = 0) const;
+ void allow(const std::string &statement, const PublicKey *key = 0);
+ void dontcare(const std::string &statement, const PublicKey *key = 0);
+ void deny(const std::string &statement, const PublicKey *key = 0);
+ bool is_allowed(const std::string &statement, const PublicKey *key = 0) const;
+ bool is_denied(const std::string &statement, const PublicKey *key = 0) const;
void auth_stats(const std::string &statement, int &self, int &trusted, int &all) const;
- void trust(const publickey *key);
- void dctrust(const publickey *key);
- void distrust(const publickey *key);
- bool is_trusted(const publickey *key) const;
- bool is_distrusted(const publickey *key) const;
- publickey *find_key(const std::string &fingerprint) const;
+ void trust(const PublicKey *key);
+ void dctrust(const PublicKey *key);
+ void distrust(const PublicKey *key);
+ bool is_trusted(const PublicKey *key) const;
+ bool is_distrusted(const PublicKey *key) const;
+ PublicKey *find_key(const std::string &fingerprint) const;
void update_trust();
- std::vector<const certificate *> find_certificates(const publickey *key, const std::string &statement) const;
- std::vector<const certificate *> find_certificates(const std::string &statement) const;
- std::vector<const certificate *> find_certificates(const publickey *key) const;
+ std::vector<const Certificate *> find_certificates(const PublicKey *key, const std::string &statement) const;
+ std::vector<const Certificate *> find_certificates(const std::string &statement) const;
+ std::vector<const Certificate *> find_certificates(const PublicKey *key) const;
- const certificate *import_certificate(const std::string &certificate);
- std::string export_certificate(const certificate *) const;
+ const Certificate *import_certificate(const std::string &Certificate);
+ std::string export_certificate(const Certificate *) const;
- const publickey *import_key(const std::string &key);
- std::string export_key(const publickey *key) const;
+ const PublicKey *import_key(const std::string &key);
+ std::string export_key(const PublicKey *key) const;
void import_all(std::istream &in);
void export_all(std::ostream &out) const;
- certificate *certificate_from_string(const std::string &certificate);
- certificate *certificate_load(const std::string &filename);
- void certificate_save(const certificate *cert, const std::string &filename) const;
+ Certificate *certificate_from_string(const std::string &Certificate);
+ Certificate *certificate_load(const std::string &filename);
+ void certificate_save(const Certificate *cert, const std::string &filename) const;
};
}