2012-04-08 10:52:01 -04:00
|
|
|
#pragma once
|
|
|
|
|
2012-03-26 17:17:04 -04:00
|
|
|
// This class implements an abstract pgp handler to be used in RetroShare.
|
|
|
|
//
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2012-04-01 08:52:15 -04:00
|
|
|
#include <map>
|
2012-04-09 13:03:47 -04:00
|
|
|
#include <set>
|
2012-03-26 17:17:04 -04:00
|
|
|
#include <util/rsthreads.h>
|
2012-06-06 16:31:19 -04:00
|
|
|
#include <util/rsid.h>
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <openpgpsdk/types.h>
|
|
|
|
#include <openpgpsdk/keyring.h>
|
2012-03-27 16:48:21 -04:00
|
|
|
#include <openpgpsdk/keyring_local.h>
|
2012-03-26 17:17:04 -04:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:31:19 -04:00
|
|
|
static const int KEY_ID_SIZE = 8 ;
|
|
|
|
static const int KEY_FINGERPRINT_SIZE = 20 ;
|
2012-03-26 17:17:04 -04:00
|
|
|
|
2012-06-06 16:31:19 -04:00
|
|
|
typedef std::string (*PassphraseCallback)(void *data, const char *uid_hint, const char *passphrase_info, int prev_was_bad) ;
|
2012-04-04 15:27:07 -04:00
|
|
|
|
2012-06-06 16:31:19 -04:00
|
|
|
typedef t_RsGenericIdType<KEY_ID_SIZE> PGPIdType;
|
|
|
|
typedef t_RsGenericIdType<KEY_FINGERPRINT_SIZE> PGPFingerprintType ;
|
2012-04-09 13:03:47 -04:00
|
|
|
|
|
|
|
class PGPCertificateInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PGPCertificateInfo() {}
|
|
|
|
|
|
|
|
std::string _name;
|
|
|
|
std::string _email;
|
|
|
|
std::string _comment;
|
|
|
|
|
|
|
|
std::set<std::string> signers;
|
|
|
|
|
|
|
|
uint32_t _trustLvl;
|
|
|
|
uint32_t _validLvl;
|
|
|
|
uint32_t _flags ;
|
|
|
|
|
|
|
|
PGPFingerprintType _fpr; /* fingerprint */
|
|
|
|
PGPIdType _key_id ;
|
|
|
|
|
|
|
|
uint32_t _key_index ; // index to array of keys in the public keyring
|
|
|
|
|
2012-06-14 16:13:31 -04:00
|
|
|
static const uint32_t PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION = 0x0001 ;
|
|
|
|
static const uint32_t PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE = 0x0002 ;
|
|
|
|
static const uint32_t PGP_CERTIFICATE_FLAG_HAS_SIGNED_ME = 0x0004 ;
|
|
|
|
static const uint32_t PGP_CERTIFICATE_FLAG_UNSUPPORTED_ALGORITHM = 0x0008 ; // set when the key is not RSA, so that RS avoids to use it.
|
2012-06-29 15:38:19 -04:00
|
|
|
|
|
|
|
static const uint8_t PGP_CERTIFICATE_TRUST_UNDEFINED = 0x00 ;
|
|
|
|
static const uint8_t PGP_CERTIFICATE_TRUST_NEVER = 0x02 ;
|
|
|
|
static const uint8_t PGP_CERTIFICATE_TRUST_MARGINALLY = 0x03 ;
|
|
|
|
static const uint8_t PGP_CERTIFICATE_TRUST_FULLY = 0x04 ;
|
|
|
|
static const uint8_t PGP_CERTIFICATE_TRUST_ULTIMATE = 0x05 ;
|
2012-04-04 15:27:07 -04:00
|
|
|
};
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
class PGPHandler
|
|
|
|
{
|
|
|
|
public:
|
2012-06-20 17:59:04 -04:00
|
|
|
PGPHandler( const std::string& path_to_public_keyring,
|
|
|
|
const std::string& path_to_secret_keyring,
|
|
|
|
const std::string& path_to_trust_database,
|
|
|
|
const std::string& pgp_lock_file) ;
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
virtual ~PGPHandler() ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ids list of gpg certificate ids (note, not the actual certificates)
|
|
|
|
*/
|
2012-04-09 13:03:47 -04:00
|
|
|
bool getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) const ;
|
2012-07-08 17:06:41 -04:00
|
|
|
bool haveSecretKey(const PGPIdType& id) const ;
|
2012-03-26 17:17:04 -04:00
|
|
|
|
2012-07-12 15:20:31 -04:00
|
|
|
bool importGPGKeyPair(const std::string& filename,PGPIdType& imported_id,std::string& import_error) ;
|
2012-07-10 17:40:53 -04:00
|
|
|
bool exportGPGKeyPair(const std::string& filename,const PGPIdType& exported_id) const ;
|
|
|
|
|
2012-03-26 17:17:04 -04:00
|
|
|
bool availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids);
|
|
|
|
bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, PGPIdType& pgpId, std::string& errString) ;
|
|
|
|
|
|
|
|
bool LoadCertificateFromString(const std::string& pem, PGPIdType& gpg_id, std::string& error_string);
|
|
|
|
std::string SaveCertificateToString(const PGPIdType& id,bool include_signatures) ;
|
|
|
|
|
2012-04-01 08:52:15 -04:00
|
|
|
bool SignDataBin(const PGPIdType& id,const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) ;
|
2012-04-04 15:27:07 -04:00
|
|
|
bool VerifySignBin(const void *data, uint32_t data_len, unsigned char *sign, unsigned int sign_len, const PGPFingerprintType& withfingerprint) ;
|
2012-07-05 18:25:46 -04:00
|
|
|
bool privateSignCertificate(const PGPIdType& own_id,const PGPIdType& id_of_key_to_sign) ;
|
2012-04-04 15:27:07 -04:00
|
|
|
|
2012-04-08 10:52:01 -04:00
|
|
|
bool encryptTextToFile(const PGPIdType& key_id,const std::string& text,const std::string& outfile) ;
|
|
|
|
bool decryptTextFromFile(const PGPIdType& key_id,std::string& text,const std::string& inputfile) ;
|
|
|
|
|
2012-04-04 15:27:07 -04:00
|
|
|
bool getKeyFingerprint(const PGPIdType& id,PGPFingerprintType& fp) const ;
|
2012-04-09 13:03:47 -04:00
|
|
|
void setAcceptConnexion(const PGPIdType&,bool) ;
|
2012-07-13 08:20:44 -04:00
|
|
|
void updateOwnSignatureFlag(const PGPIdType& ownId) ;
|
2012-04-09 13:03:47 -04:00
|
|
|
|
2012-07-05 18:25:46 -04:00
|
|
|
//bool isKeySupported(const PGPIdType& id) const ;
|
2012-06-14 16:13:31 -04:00
|
|
|
|
2012-06-29 15:38:19 -04:00
|
|
|
bool privateTrustCertificate(const PGPIdType& id,int valid_level) ;
|
2012-06-20 17:59:04 -04:00
|
|
|
|
2012-05-01 14:45:24 -04:00
|
|
|
// Write keyring
|
|
|
|
|
2012-07-05 18:25:46 -04:00
|
|
|
//bool writeSecretKeyring() ;
|
|
|
|
//bool writePublicKeyring() ;
|
2012-04-09 13:03:47 -04:00
|
|
|
|
|
|
|
const PGPCertificateInfo *getCertificateInfo(const PGPIdType& id) const ;
|
|
|
|
|
|
|
|
bool isGPGId(const std::string &id);
|
|
|
|
bool isGPGSigned(const std::string &id);
|
|
|
|
bool isGPGAccepted(const std::string &id);
|
2012-03-26 17:17:04 -04:00
|
|
|
|
2012-05-01 04:53:32 -04:00
|
|
|
static void setPassphraseCallback(PassphraseCallback cb) ;
|
|
|
|
static PassphraseCallback passphraseCallback() { return _passphrase_callback ; }
|
2012-06-12 16:31:13 -04:00
|
|
|
|
|
|
|
// Debug stuff.
|
|
|
|
virtual bool printKeys() const ;
|
|
|
|
|
2012-06-29 15:38:19 -04:00
|
|
|
// Syncs the keyrings and trust database between memory and disk. The algorithm is:
|
|
|
|
// 1 - lock the keyrings
|
|
|
|
// 2 - compare file modification dates with last writing date
|
|
|
|
// - if file is modified, load it, and merge with memory
|
|
|
|
// 3 - look into memory modification flags
|
|
|
|
// - if flag says keyring has changed, write to disk
|
|
|
|
//
|
|
|
|
bool syncDatabase() ;
|
|
|
|
|
2012-03-26 17:17:04 -04:00
|
|
|
private:
|
2012-04-09 13:03:47 -04:00
|
|
|
void initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t i) ;
|
2012-07-02 16:46:14 -04:00
|
|
|
|
|
|
|
// Returns true if the signatures have been updated
|
|
|
|
//
|
|
|
|
bool validateAndUpdateSignatures(PGPCertificateInfo& cert,const ops_keydata_t *keydata) ;
|
2012-04-09 13:03:47 -04:00
|
|
|
|
2012-04-01 08:52:15 -04:00
|
|
|
const ops_keydata_t *getPublicKey(const PGPIdType&) const ;
|
|
|
|
const ops_keydata_t *getSecretKey(const PGPIdType&) const ;
|
2012-03-29 17:51:37 -04:00
|
|
|
|
2012-06-20 17:59:04 -04:00
|
|
|
void locked_readPrivateTrustDatabase() ;
|
2012-06-29 15:38:19 -04:00
|
|
|
bool locked_writePrivateTrustDatabase() ;
|
|
|
|
|
|
|
|
bool locked_syncPublicKeyring() ;
|
|
|
|
bool locked_syncTrustDatabase() ;
|
|
|
|
|
|
|
|
void mergeKeyringFromDisk(ops_keyring_t *keyring, std::map<std::string,PGPCertificateInfo>& kmap, const std::string& keyring_file) ;
|
|
|
|
bool addOrMergeKey(ops_keyring_t *keyring,std::map<std::string,PGPCertificateInfo>& kmap,const ops_keydata_t *keydata) ;
|
2012-06-20 17:59:04 -04:00
|
|
|
|
2012-05-01 14:45:24 -04:00
|
|
|
// Members.
|
|
|
|
//
|
2012-07-05 18:25:46 -04:00
|
|
|
mutable RsMutex pgphandlerMtx ;
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
ops_keyring_t *_pubring ;
|
|
|
|
ops_keyring_t *_secring ;
|
|
|
|
|
2012-04-09 13:03:47 -04:00
|
|
|
std::map<std::string,PGPCertificateInfo> _public_keyring_map ; // used for fast access to keys. Gives the index in the keyring.
|
|
|
|
std::map<std::string,PGPCertificateInfo> _secret_keyring_map ;
|
2012-04-01 08:52:15 -04:00
|
|
|
|
2012-03-26 17:17:04 -04:00
|
|
|
const std::string _pubring_path ;
|
|
|
|
const std::string _secring_path ;
|
2012-06-20 17:59:04 -04:00
|
|
|
const std::string _trustdb_path ;
|
2012-06-12 16:31:13 -04:00
|
|
|
const std::string _pgp_lock_filename ;
|
|
|
|
|
|
|
|
bool _pubring_changed ;
|
2012-06-20 17:59:04 -04:00
|
|
|
bool _trustdb_changed ;
|
2012-04-01 08:52:15 -04:00
|
|
|
|
2012-06-29 15:38:19 -04:00
|
|
|
time_t _pubring_last_update_time ;
|
|
|
|
time_t _secring_last_update_time ;
|
|
|
|
time_t _trustdb_last_update_time ;
|
|
|
|
|
2012-05-01 14:45:24 -04:00
|
|
|
// Helper functions.
|
|
|
|
//
|
|
|
|
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ;
|
|
|
|
static ops_keyring_t *allocateOPSKeyring() ;
|
|
|
|
static void addNewKeyToOPSKeyring(ops_keyring_t*, const ops_keydata_t&) ;
|
2012-05-01 04:53:32 -04:00
|
|
|
static PassphraseCallback _passphrase_callback ;
|
2012-06-15 17:58:46 -04:00
|
|
|
static bool mergeKeySignatures(ops_keydata_t *dst,const ops_keydata_t *src) ; // returns true if signature lists are different
|
2012-03-26 17:17:04 -04:00
|
|
|
};
|
2012-04-01 08:52:15 -04:00
|
|
|
|