Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
def sign_keydata_and_encrypt(keydata, error_cb=None, homedir=None):
    """Signs OpenPGP keydata with your regular GnuPG secret keys
    and encrypts the result under the given key
    
    error_cb can be a function that is called with any exception
    occuring during signing of the key.
    """
    tmpkeyring = TempKeyring()
    tmpkeyring.import_data(keydata)
    tmpkeyring.context.set_option('always-trust')
    for (uid, signed_key) in sign_keydata(keydata,
        error_cb=error_cb, homedir=homedir):
            if not uid.revoked:
                encrypted_key = tmpkeyring.encrypt_data(data=signed_key,
                    recipient=uid.uid)
                yield (UID.from_monkeysign(uid), encrypted_key)
Exemple #4
0
def sign_keydata_and_encrypt(keydata, error_cb=None, homedir=None):
    """Signs OpenPGP keydata with your regular GnuPG secret keys
    and encrypts the result under the given key
    
    error_cb can be a function that is called with any exception
    occuring during signing of the key.
    """
    tmpkeyring = TempKeyring()
    tmpkeyring.import_data(keydata)
    tmpkeyring.context.set_option('always-trust')
    for (uid, signed_key) in sign_keydata(keydata,
                                          error_cb=error_cb,
                                          homedir=homedir):
        if not uid.revoked:
            encrypted_key = tmpkeyring.encrypt_data(data=signed_key,
                                                    recipient=uid.uid)
            yield (UID.from_monkeysign(uid), encrypted_key)
Exemple #5
0
def sign_keydata_and_encrypt(keydata, error_cb=None, homedir=None):
    """Signs OpenPGP keydata with your regular GnuPG secret keys
    and encrypts the result under the given key
    
    error_cb can be a function that is called with any exception
    occuring during signing of the key.
    """
    tmpkeyring = TempKeyring()
    tmpkeyring.import_data(keydata)
    fingerprint = fingerprint_from_keydata(keydata)
    tmpkeyring.context.set_option('always-trust')
    for (uid, signed_key) in sign_keydata(keydata,
                                          error_cb=error_cb,
                                          homedir=homedir):
        if not uid.revoked:
            encrypted_key = tmpkeyring.encrypt_data(
                data=signed_key,
                # We use the fingerprint rather than the email address,
                # because we cannot get a reliable representation of the
                # UID, i.e. when it contains non-UTF-8 bytes.
                recipient=fingerprint)
            yield (UID.from_monkeysign(uid), encrypted_key)
Exemple #6
0
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)
Exemple #7
0
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)
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
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