#include "fides.h"
+#ifndef FIDES_DEBUG
+#define FIDES_DEBUG false
+#endif
+
+#define debug if(FIDES_DEBUG)
+
using namespace std;
// Global state
if(e == string::npos)
throw exception("Invalid certificate");
string fingerprint = hexdecode(data.substr(0, e));
- publickey *signer = find_key(fingerprint);
+ const publickey *signer = find_key(fingerprint);
if(!signer)
throw exception("Unknown public key");
b = e + 1;
// Fides main functions
fides::fides(const string &dir): homedir(dir) {
- cerr << "Fides initialising\n";
+ debug cerr << "Fides initialising\n";
// Set homedir to provided directory, or $FIDES_HOME, or $HOME/.fides, or as a last resort $PWD/.fides
if(homedir.empty())
}
vector<string> files = dirlist(keydir);
for(size_t i = 0; i < files.size(); ++i) {
- cerr << "Loading key " << files[i] << '\n';
+ debug cerr << "Loading key " << files[i] << '\n';
publickey *key = new publickey();
key->load(keydir + files[i]);
files = dirlist(certdir);
for(size_t i = 0; i < files.size(); ++i) {
- cerr << "Loading certificate " << files[i] << '\n';
+ debug cerr << "Loading certificate " << files[i] << '\n';
certificate *cert = certificate_load(certdir + files[i]);
if(false && !cert->validate()) {
- cerr << "Bad certificate!\n";
+ cerr << "Bad certificate in database: " << cert->to_string() << '\n';
continue;
}
certs[hexdecode(files[i])] = cert;
}
fides::~fides() {
- cerr << "Fides exitting\n";
- for(map<string, certificate *>::iterator i = certs.begin(); i != certs.end(); ++i)
+ debug cerr << "Fides exitting\n";
+ for(map<string, certificate *>::const_iterator i = certs.begin(); i != certs.end(); ++i)
delete i->second;
- for(map<string, publickey *>::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;
}
return 0;
}
-vector<fides::certificate *> fides::find_certificates(const publickey *signer, const string ®ex) const {
- vector<certificate *> found;
+vector<const fides::certificate *> fides::find_certificates(const publickey *signer, const string ®ex) const {
+ vector<const certificate *> found;
map<string, certificate *>::const_iterator i;
regexp regexp(regex);
for(i = certs.begin(); i != certs.end(); ++i) {
return found;
}
-vector<fides::certificate *> fides::find_certificates(const string ®ex) const {
- vector<certificate *> found;
+vector<const fides::certificate *> fides::find_certificates(const string ®ex) const {
+ vector<const certificate *> found;
map<string, certificate *>::const_iterator i;
regexp regexp(regex);
for(i = certs.begin(); i != certs.end(); ++i)
return found;
}
-vector<fides::certificate *> fides::find_certificates(const publickey *signer) const {
- vector<certificate *> found;
+vector<const fides::certificate *> fides::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)
if(!line.compare(0, 9, "-----END ")) {
fides::publickey *key = new publickey();
key->from_string(pem);
- cerr << "Imported key " << hexencode(key->fingerprint()) << '\n';
+ debug cerr << "Imported key " << hexencode(key->fingerprint()) << '\n';
merge(key);
is_pem = false;
} else {
}
fides::certificate *cert = certificate_from_string(line);
- cerr << "Importing certificate " << hexencode(cert->fingerprint()) << '\n';
+ debug cerr << "Importing certificate " << hexencode(cert->fingerprint()) << '\n';
merge(cert);
}
}
void fides::update_trust() {
// clear trust on all keys
- for(map<string, publickey *>::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.
// loop over all keys whose certificates need to be checked
for(i = tocheck.begin(); i != tocheck.end(); ++i) {
- cerr << "Trust for key " << hexencode((*i)->fingerprint()) << " set to " << (*i)->trust << '\n';
+ debug cerr << "Trust for key " << hexencode((*i)->fingerprint()) << " set to " << (*i)->trust << '\n';
// except if this key is not trusted
// find all non-zero trust certificates of this key
- vector<certificate *> matches = find_certificates(*i, "^t[+-] ");
+ vector<const certificate *> matches = find_certificates(*i, "^t[+-] ");
// update trust value of those keys
// except for keys we already checked
if(checked.find(other) != checked.end()) {
- 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;
}
void fides::merge(publickey *key) {
if(keys.find(key->fingerprint()) != keys.end()) {
- cerr << "Key already known\n";
+ debug cerr << "Key already known\n";
return;
}
// If we already know this certificate, drop it.
if(certs.find(cert->fingerprint()) != certs.end()) {
- cerr << "Certificate already known\n";
+ debug cerr << "Certificate already known\n";
return;
}
// If the certificate does not validate, drop it.
if(!cert->validate()) {
// TODO: this should not happen, be wary of DoS attacks
- cerr << "Certificate invalid\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<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, >)) {
- 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, ==)) {
// TODO: this should not happen, be wary of DoS attacks
- cerr << "Certificate has same timestamp as another timestamp!\n";
+ debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- 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, >)) {
- 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, ==)) {
// TODO: this should not happen, be wary of DoS attacks
- cerr << "Certificate has same timestamp as another timestamp!\n";
+ debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- 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, >)) {
- 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, ==)) {
// TODO: this should not happen, be wary of DoS attacks
- cerr << "Certificate has same timestamp as another timestamp!\n";
+ debug cerr << "Certificate has same timestamp as another timestamp!\n";
return;
}
- 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());
return;
}
- cerr << "Certificate is new\n";
+ debug cerr << "Certificate is new\n";
certs[cert->fingerprint()] = cert;
certificate_save(cert, certdir + hexencode(cert->fingerprint()));
}
void fides::auth_stats(const string &statement, int &self, int &trusted, int &all) const {
self = trusted = all = 0;
- vector<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;