def MinimalExport(keydata): '''Returns the minimised version of a key For now, you must provide one key only.''' tmpkeyring = TempKeyring() ret = tmpkeyring.import_data(keydata) log.debug("Returned %s after importing %r", ret, keydata) assert ret tmpkeyring.context.set_option('export-options', 'export-minimal') keys_dict = tmpkeyring.get_keys() # We assume the keydata to contain one key only keys = list(keys_dict.items()) log.debug("Keys after importing: %s (%s)", keys, keys) fingerprint, key = keys[0] stripped_key = tmpkeyring.export_data(fingerprint) return stripped_key
def UIDExport(uid, keydata): """Export only the UID of a key. Unfortunately, GnuPG does not provide smth like --export-uid-only in order to obtain a UID and its signatures.""" tmp = TempKeyring() # Hm, apparently this needs to be set, otherwise gnupg will issue # a stray "gpg: checking the trustdb" which confuses the gnupg library tmp.context.set_option('always-trust') tmp.import_data(keydata) for fpr, key in tmp.get_keys(uid).items(): for u in key.uidslist: key_uid = u.uid if key_uid != uid: log.info('Deleting UID %s from key %s', key_uid, fpr) tmp.del_uid(fingerprint=fpr, pattern=key_uid) only_uid = tmp.export_data(uid) return only_uid
def openpgpkey_from_data(keydata): "Creates an OpenPGP object from given data" keyring = TempKeyring() if not keyring.import_data(keydata): raise ValueError("Could not import %r - stdout: %r, stderr: %r", keydata, keyring.context.stdout, keyring.context.stderr) # As we have imported only one key, we should also # only have one key at our hands now. keys = keyring.get_keys() if len(keys) != 1: log.debug('Operation on keydata "%s" failed', keydata) raise ValueError("Expected exactly one key, but got %d: %r" % (len(keys), keys)) else: # The first (key, value) pair in the keys dict # next(iter(keys.items()))[0] might be semantically # more correct than list(d.items()) as we don't care # much about having a list created, but I think it's # more legible. fpr_key = list(keys.items())[0] # is composed of the fpr as key and an OpenPGP key as value key = fpr_key[1] return Key.from_monkeysign(key)
def UIDExport(uid, keydata): """Export only the UID of a key. Unfortunately, GnuPG does not provide smth like --export-uid-only in order to obtain a UID and its signatures.""" log = logging.getLogger(__name__ + ".UIDExport") tmp = TempKeyring() # Hm, apparently this needs to be set, otherwise gnupg will issue # a stray "gpg: checking the trustdb" which confuses the gnupg library tmp.context.set_option('always-trust') tmp.import_data(keydata) fpr = fingerprint_from_keydata(keydata) log.debug("Looking for %r", fpr) for fpr, key in tmp.get_keys(fpr).items(): for u in key.uidslist: key_uid = decode_gpg_uid(u.uid) if key_uid != uid: log.info('Deleting UID %s from key %s', key_uid, fpr) # As pattern we need to provide the unescaped one, otherwise monkeysign # will hang trying to find the correct uid to delete tmp.del_uid(fingerprint=fpr, pattern=u.uid) only_uid = tmp.export_data(fpr) return only_uid
def openpgpkey_from_data(keydata): "Creates an OpenPGP object from given data" keyring = TempKeyring() if not keyring.import_data(keydata): raise ValueError("Could not import %r - stdout: %r, stderr: %r", keydata, keyring.context.stdout, keyring.context.stderr) # As we have imported only one key, we should also # only have one key at our hands now. keys = keyring.get_keys() if len(keys) != 1: log.debug('Operation on keydata "%s" failed', keydata) raise ValueError("Expected exactly one key, but got %d: %r" % ( len(keys), keys)) else: # The first (key, value) pair in the keys dict # next(iter(keys.items()))[0] might be semantically # more correct than list(d.items()) as we don't care # much about having a list created, but I think it's # more legible. fpr_key = list(keys.items())[0] # is composed of the fpr as key and an OpenPGP key as value key = fpr_key[1] return Key.from_monkeysign(key)