Example #1
0
    def _get_key(self, offset):
        self.lock_db()
        try:
            with open(self.filename, 'rb') as fh:
                fh.seek(offset)
                packets = []
                packet_iter = parse_binary_packet_stream(fh)
                for packet in packet_iter:
                    if packet.type == constants.COMPRESSED_DATA_PACKET_TYPE:
                        continue
                    elif packets and packet.type in (
                            constants.PUBLIC_KEY_PACKET_TYPE,
                            constants.SECRET_KEY_PACKET_TYPE,
                    ):
                        break
                    else:
                        packets.append(packet)
        finally:
            self.unlock_db()

        key = None
        if packets and packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
            key = TransferablePublicKey.from_packets(packets)
        elif packets and packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
            key = TransferableSecretKey.from_packets(packets)
        elif packets:
            raise ValueError(packets[0])
        else:
            raise KeyError()

        return key
Example #2
0
    def _get_key(self, offset):
        self.lock_db()
        try:
            with open(self.filename, 'rb') as fh:
                fh.seek(offset)
                packets = []
                packet_iter = parse_binary_packet_stream(fh)
                for packet in packet_iter:
                    if packet.type == constants.COMPRESSED_DATA_PACKET_TYPE:
                        continue
                    elif packets and packet.type in (
                            constants.PUBLIC_KEY_PACKET_TYPE,
                            constants.SECRET_KEY_PACKET_TYPE,
                            ):
                        break
                    else:
                        packets.append(packet)
        finally:
            self.unlock_db()

        key = None
        if packets and packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
            key = TransferablePublicKey.from_packets(packets)
        elif packets and packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
            key = TransferableSecretKey.from_packets(packets)
        elif packets:
            raise ValueError(packets[0])
        else:
            raise KeyError()

        return key
Example #3
0
def read_key(data, armored=False):
    if armor.is_armor(data):
        # Assume the user made a mistake
        if isinstance(data, bytes):
            data = data.decode('us-ascii')
        armored = True
    if armored:
        fn = parse_ascii_packet_data
    else:
        fn = parse_binary_packet_data

    packets = list(fn(data))

    if packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
        return TransferablePublicKey.from_packets(packets)
    elif packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
        return TransferableSecretKey.from_packets(packets)
    else:
        raise ValueError('Unexpected packet')
Example #4
0
def read_key(data, armored=False):
    if armor.is_armor(data):
        # Assume the user made a mistake
        if isinstance(data, bytes):
            data = data.decode('us-ascii')
        armored = True
    if armored:
        fn = parse_ascii_packet_data
    else:
        fn = parse_binary_packet_data

    packets = list(fn(data))

    if packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
        return TransferablePublicKey.from_packets(packets)
    elif packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
        return TransferableSecretKey.from_packets(packets)
    else:
        raise ValueError('Unexpected packet')
Example #5
0
    def get_transferrable_key(self, fingerprint):
        if len(fingerprint) != 40:
            # Actually a key ID - find the fingerprint first.
            fingerprint = ([k
                            for k in self.keys() if k.endswith(fingerprint)] +
                           [None])[0]

        if fingerprint is None:
            return None

        self.lock_db()
        try:
            with gdbm.open(self.filename, 'r') as db:
                packet_data = db[fingerprint]
            packets = list(parse_binary_packet_data(packet_data))
            if packets:
                if packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
                    return TransferablePublicKey.from_packets(packets)
                elif packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
                    return TransferableSecretKey.from_packets(packets)
        finally:
            self.unlock_db()
Example #6
0
    def get_transferrable_key(self, fingerprint):
        if len(fingerprint) != 40:
            # Actually a key ID - find the fingerprint first.
            fingerprint = ([
                k for k in self.keys()
                if k.endswith(fingerprint)
                ] + [None]
                )[0]

        if fingerprint is None:
            return None

        self.lock_db()
        try:
            with gdbm.open(self.filename, 'r') as db:
                packet_data = db[fingerprint]
            packets = list(parse_binary_packet_data(packet_data))
            if packets:
                if packets[0].type == constants.PUBLIC_KEY_PACKET_TYPE:
                    return TransferablePublicKey.from_packets(packets)
                elif packets[0].type == constants.SECRET_KEY_PACKET_TYPE:
                    return TransferableSecretKey.from_packets(packets)
        finally:
            self.unlock_db()