예제 #1
0
def test_uid_export_single():
    # This key contains only one UID
    data = read_fixture_file("pubkey-1.asc")
    try:
        uid1_data = UIDExport(data, 1)
    except KeyboardInterrupt as e:
        log.exception("Meh.")
        raise RuntimeError()

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(1, len(uids))

    # The first exported UID
    c = TempContext()
    logging.info("uid1: %r", uid1_data)
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1 = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1.uid, uids[0].uid)
예제 #2
0
def test_export_alpha_uids():
    """When UIDs get deleted, their index shrinks, of course
    We didn't, however, take that into account so a key with
    three UIDs would break.
    """
    data = read_fixture_file("alpha.asc")

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    logging.info("UIDs: %r", uids)
    assert_equals(3, len(uids))
    
    for i, uid in enumerate(uids, start=1):
        exported_uid = UIDExport(data, i)
        tmp = TempContext()
        tmp.op_import(exported_uid)
        result = tmp.op_import_result()
        logging.debug("UID %d %r import result: %r", i, uid, result)
        uid_key = tmp.get_key(result.imports[0].fpr)
        assert_equals(1, len(uid_key.uids))
        key_uid = uid_key.uids[0]
        # FIXME: Enable __eq__
        # assert_equal(uids[i-1], key_uid)
        assert_equal(uids[i-1].name, key_uid.name)
        assert_equal(uids[i-1].email, key_uid.email)
예제 #3
0
def export_public_key(keydata):
    "Returns the public portion of the key even if you provide a private key"
    # This might be a secret key, too, so we import and export to
    # get hold of the public portion.
    ctx = TempContext()
    ctx.op_import(keydata)
    result = ctx.op_import_result()
    fpr = result.imports[0].fpr
    sink = gpg.Data()
    ctx.op_export(fpr, 0, sink)
    sink.seek(0, 0)
    public_key = sink.read()
    assert len(public_key) > 0
    return public_key
예제 #4
0
def test_uid_export_single():
    # This key contains only one UID
    data = read_fixture_file("pubkey-1.asc")
    try:
        uid1_data = UIDExport(data, 1)
    except KeyboardInterrupt as e:
        log.exception("Meh.")
        raise RuntimeError()

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(1, len(uids))

    # The first exported UID
    c = TempContext()
    logging.info("uid1: %r", uid1_data)
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1 = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1.uid, uids[0].uid)
예제 #5
0
    def test_sign_and_encrypt(self):
        # This might be a secret key, too, so we import and export to
        # get hold of the public portion.
        keydata = open(self.key_sender_key, "rb").read()
        # We get the public portion of the key
        sender = TempContext()
        sender.op_import(keydata)
        result = sender.op_import_result()
        fpr = result.imports[0].fpr
        sink = gpg.Data()
        sender.op_export(fpr, 0, sink)
        sink.seek(0, 0)
        # This is the key that we will sign
        public_sender_key = sink.read()

        keys = get_usable_keys(homedir=self.key_sender_homedir)
        assert_equals(1, len(keys))
        key = keys[0]
        uids = key.uidslist
        # Now finally call the function under test
        uid_encrypted = list(
            sign_keydata_and_encrypt(public_sender_key,
                                     error_cb=None,
                                     homedir=self.key_receiver_homedir))
        assert_equals(len(uids), len(uid_encrypted))

        # We need to explicitly request signatures
        uids_before = uids
        assert_equals(len(uids_before), len(sender.get_key(fpr).uids))

        sigs_before = [
            s for l in get_signatures_for_uids_on_key(sender, key).values()
            for s in l
        ]
        # FIXME: Refactor this a little bit.
        # We have duplication of code with the other test below.
        for uid, uid_enc in zip(uids_before, uid_encrypted):
            uid_enc_str = uid_enc[0].uid
            # The test doesn't work so well, because comments
            # are not rendered :-/
            # assert_equals(uid, uid_enc[0])
            assert_in(uid.name, uid_enc_str)
            assert_in(uid.email, uid_enc_str)
            ciphertext = uid_enc[1]
            log.debug("Decrypting %r", ciphertext)
            plaintext, result, vrfy = sender.decrypt(ciphertext)
            log.debug("Decrypt Result: %r", result)
            sender.op_import(plaintext)
            import_result = sender.op_import_result()
            log.debug("Import Result: %r", import_result)
            assert_equals(1, import_result.new_signatures)
            updated_key = sender.get_key(fpr)
            log.debug("updated key: %r", updated_key)
            log.debug("updated key sigs: %r",
                      [(uid, uid.signatures) for uid in updated_key.uids])

        sigs_after = [
            s for l in get_signatures_for_uids_on_key(sender, key).values()
            for s in l
        ]
        assert_greater(len(sigs_after), len(sigs_before))
예제 #6
0
def test_export_alpha_uids():
    """When UIDs get deleted, their index shrinks, of course
    We didn't, however, take that into account so a key with
    three UIDs would break.
    """
    data = read_fixture_file("alpha.asc")

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    logging.info("UIDs: %r", uids)
    assert_equals(3, len(uids))

    for i, uid in enumerate(uids, start=1):
        exported_uid = UIDExport(data, i)
        tmp = TempContext()
        tmp.op_import(exported_uid)
        result = tmp.op_import_result()
        logging.debug("UID %d %r import result: %r", i, uid, result)
        uid_key = tmp.get_key(result.imports[0].fpr)
        assert_equals(1, len(uid_key.uids))
        key_uid = uid_key.uids[0]
        # FIXME: Enable __eq__
        # assert_equal(uids[i-1], key_uid)
        assert_equal(uids[i - 1].name, key_uid.name)
        assert_equal(uids[i - 1].email, key_uid.email)
예제 #7
0
def test_export_uids():
    # This key contains two UIDs
    # We ought to have tests with revoked and invalid UIDs
    data = read_fixture_file("pubkey-2-uids.asc")

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(2, len(uids))

    exported_uids = list(export_uids(data))
    assert_equals(2, len(exported_uids))

    exported_uid1 = exported_uids[0]
    uid1, uid1_data = exported_uid1
    exported_uid2 = exported_uids[1]
    uid2, uid2_data = exported_uid2
    assert_equals(uids[0].uid, uid1)
    assert_equals(uids[1].uid, uid2)

    # The first exported UID
    c = TempContext()
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1_key = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1_key.uid, uids[0].uid)

    # The second exported UID
    c = TempContext()
    c.op_import(uid2_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids2_key = c.get_key(fpr).uids
    assert_equals(1, len(uids2_key))
    uid2_key = uids2_key[0]
    # FIXME: The objects don't implement __eq__ it seems :-/
    # assert_equals(uid2, uids[1])
    assert_equals(uid2_key.uid, uids[1].uid)
예제 #8
0
def test_uid_export_double():
    # This key contains two UIDs
    data = read_fixture_file("pubkey-2-uids.asc")
    try:
        uid1_data = UIDExport(data, 1)
        logging.info("uid1: %r", uid1_data)
        uid2_data = UIDExport(data, 2)
    except KeyboardInterrupt as e:
        log.exception("Meh.")
        raise RuntimeError()

    assert_not_equals(uid1_data, uid2_data)

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(2, len(uids))

    # The first exported UID
    c = TempContext()
    logging.info("uid1: %r", uid1_data)
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1 = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1.uid, uids[0].uid)

    # The second exported UID
    c = TempContext()
    c.op_import(uid2_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids2_key = c.get_key(fpr).uids
    assert_equals(1, len(uids2_key))
    uid2 = uids2_key[0]
    # FIXME: The objects don't implement __eq__ it seems :-/
    # assert_equals(uid2, uids[1])
    assert_equals(uid2.uid, uids[1].uid)
예제 #9
0
    def test_sign_and_encrypt(self):
        secret_keydata = open(self.key_sender_key, "r").read()
        # We get the public portion of the key
        sender = TempContext()
        sender.op_import(secret_keydata)
        result = sender.op_import_result()
        fpr = result.imports[0].fpr
        sink = gpg.Data()
        sender.op_export(fpr, 0, sink)
        sink.seek(0, 0)
        # This is the key that we will sign
        public_sender_key = sink.read()

        keys = get_usable_secret_keys(homedir=self.key_sender_homedir)
        assert_equals(1, len(keys))
        key = keys[0]
        uids = key.uidslist
        # Now finally call the function under test
        uid_encrypted = list(
            sign_keydata_and_encrypt(public_sender_key,
                                     error_cb=None,
                                     homedir=self.key_receiver_homedir))
        assert_equals(len(uids), len(uid_encrypted))

        # We need to explicitly request signatures
        sender.set_keylist_mode(gpg.constants.KEYLIST_MODE_SIGS)
        uids_before = sender.get_key(fpr).uids
        sigs = [uid.signatures for uid in uids_before]
        sigs_before = [sig for signatures in sigs for sig in signatures]
        for uid, uid_enc in zip(uids_before, uid_encrypted):
            # The test doesn't work so well, because comments
            # are not rendered :-/
            # assert_equals(uid, uid_enc[0])
            assert_in(uid.name, uid_enc[0].uid)
            assert_in(uid.email, uid_enc[0].uid)
            ciphertext = uid_enc[1]
            log.debug("Decrypting %r", ciphertext)
            plaintext, result, vrfy = sender.decrypt(ciphertext)
            log.debug("Decrypt Result: %r", result)
            sender.op_import(plaintext)
            import_result = sender.op_import_result()
            log.debug("Import Result: %r", import_result)
            assert_equals(1, import_result.new_signatures)
            updated_key = sender.get_key(fpr)
            log.debug("updated key: %r", updated_key)
            log.debug("updated key sigs: %r",
                      [(uid, uid.signatures) for uid in updated_key.uids])

        uids_after = sender.get_key(fpr).uids
        sigs = [uid.signatures for uid in uids_after]
        sigs_after = [sig for signatures in sigs for sig in signatures]
        assert_greater(len(sigs_after), len(sigs_before))
예제 #10
0
def test_uid_export_double():
    # This key contains two UIDs
    data = read_fixture_file("pubkey-2-uids.asc")
    try:
        uid1_data = UIDExport(data, 1)
        logging.info("uid1: %r", uid1_data)
        uid2_data = UIDExport(data, 2)
    except KeyboardInterrupt as e:
        log.exception("Meh.")
        raise RuntimeError()

    assert_not_equals(uid1_data, uid2_data)

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(2, len(uids))

    # The first exported UID
    c = TempContext()
    logging.info("uid1: %r", uid1_data)
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1 = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1.uid, uids[0].uid)

    # The second exported UID
    c = TempContext()
    c.op_import(uid2_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids2_key = c.get_key(fpr).uids
    assert_equals(1, len(uids2_key))
    uid2 = uids2_key[0]
    # FIXME: The objects don't implement __eq__ it seems :-/
    # assert_equals(uid2, uids[1])
    assert_equals(uid2.uid, uids[1].uid)
예제 #11
0
    def test_sign_and_encrypt(self):
        secret_keydata = open(self.key_sender_key, "r").read()
        # We get the public portion of the key
        sender = TempContext()
        sender.op_import(secret_keydata)
        result = sender.op_import_result()
        fpr = result.imports[0].fpr
        sink = gpg.Data()
        sender.op_export(fpr, 0, sink)
        sink.seek(0, 0)
        # This is the key that we will sign
        public_sender_key = sink.read()

        keys = get_usable_secret_keys(homedir=self.key_sender_homedir)
        assert_equals(1, len(keys))
        key = keys[0]
        uids = key.uidslist
        # Now finally call the function under test
        uid_encrypted = list(sign_keydata_and_encrypt(public_sender_key,
            error_cb=None, homedir=self.key_receiver_homedir))
        assert_equals(len(uids), len(uid_encrypted))

        # We need to explicitly request signatures
        sender.set_keylist_mode(gpg.constants.KEYLIST_MODE_SIGS)
        uids_before = sender.get_key(fpr).uids
        sigs = [uid.signatures for uid in uids_before]
        sigs_before = [sig for signatures in sigs for sig in signatures]
        for uid, uid_enc in zip(uids_before, uid_encrypted):
            # The test doesn't work so well, because comments
            # are not rendered :-/
            # assert_equals(uid, uid_enc[0])
            assert_in(uid.name, uid_enc[0].uid)
            assert_in(uid.email, uid_enc[0].uid)
            ciphertext = uid_enc[1]
            log.debug("Decrypting %r", ciphertext)
            plaintext, result, vrfy = sender.decrypt(ciphertext)
            log.debug("Decrypt Result: %r", result)
            sender.op_import(plaintext)
            import_result = sender.op_import_result()
            log.debug("Import Result: %r", import_result)
            assert_equals(1, import_result.new_signatures)
            updated_key = sender.get_key(fpr)
            log.debug("updated key: %r", updated_key)
            log.debug("updated key sigs: %r", [(uid, uid.signatures) for uid in updated_key.uids])

        uids_after = sender.get_key(fpr).uids
        sigs = [uid.signatures for uid in uids_after]
        sigs_after = [sig for signatures in sigs for sig in signatures]
        assert_greater(len(sigs_after), len(sigs_before))
예제 #12
0
def test_export_uids():
    # This key contains two UIDs
    # We ought to have tests with revoked and invalid UIDs
    data = read_fixture_file("pubkey-2-uids.asc")

    # The original key
    c = TempContext()
    c.op_import(data)
    result = c.op_import_result()
    logging.info("Result: %r", result)
    fpr = result.imports[0].fpr
    uids = c.get_key(fpr).uids
    assert_equals(2, len(uids))

    exported_uids = list(export_uids(data))
    assert_equals(2, len(exported_uids))

    exported_uid1 = exported_uids[0]
    uid1, uid1_data = exported_uid1
    exported_uid2 = exported_uids[1]
    uid2, uid2_data = exported_uid2
    assert_equals(uids[0].uid, uid1)
    assert_equals(uids[1].uid, uid2)


    # The first exported UID
    c = TempContext()
    c.op_import(uid1_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids1_key = c.get_key(fpr).uids
    assert_equals(1, len(uids1_key))
    uid1_key = uids1_key[0]
    # assert_equals(uid1, uids[0])
    assert_equals(uid1_key.uid, uids[0].uid)

    # The second exported UID
    c = TempContext()
    c.op_import(uid2_data)
    result = c.op_import_result()
    imports = result.imports
    assert_equals(1, len(imports))
    uids2_key = c.get_key(fpr).uids
    assert_equals(1, len(uids2_key))
    uid2_key = uids2_key[0]
    # FIXME: The objects don't implement __eq__ it seems :-/
    # assert_equals(uid2, uids[1])
    assert_equals(uid2_key.uid, uids[1].uid)