예제 #1
0
    def _maybe_decrypt_msg(self, data):
        """
        Tries to decrypt a gpg message if data looks like one.

        :param data: the text to be decrypted.
        :type data: str

        :return: a Deferred that will be fired with an str of data, possibly
                 decrypted.
        :rtype: Deferred
        """
        leap_assert_type(data, str)
        log.msg('maybe decrypting doc')

        # parse the original message
        encoding = get_email_charset(data)
        msg = self._parser.parsestr(data)

        fromHeader = msg.get('from', None)
        senderAddress = None

        if (fromHeader is not None and
            (msg.get_content_type() == MULTIPART_ENCRYPTED or
             msg.get_content_type() == MULTIPART_SIGNED)):
                senderAddress = parseaddr(fromHeader)[1]

        def add_leap_header(ret):
            decrmsg, signkey = ret
            if (senderAddress is None or signkey is None or
                    isinstance(signkey, keymanager_errors.KeyNotFound)):
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_COULD_NOT_VERIFY)
            elif isinstance(signkey, keymanager_errors.InvalidSignature):
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_INVALID)
            else:
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_VALID,
                    pubkey=signkey.fingerprint)
            return decrmsg.as_string()

        if msg.get_content_type() == MULTIPART_ENCRYPTED:
            d = self._decrypt_multipart_encrypted_msg(
                msg, encoding, senderAddress)
        elif msg.get_content_type() == MULTIPART_SIGNED:
            d = self._verify_signature_not_encrypted_msg(msg, senderAddress)
        else:
            d = self._maybe_decrypt_inline_encrypted_msg(
                msg, encoding, senderAddress)
        d.addCallback(add_leap_header)
        return d
예제 #2
0
파일: fetch.py 프로젝트: pmaia/leap_mail
    def _maybe_decrypt_msg(self, data):
        """
        Tries to decrypt a gpg message if data looks like one.

        :param data: the text to be decrypted.
        :type data: str
        :return: data, possibly descrypted.
        :rtype: str
        """
        leap_assert_type(data, str)
        log.msg('maybe decrypting doc')

        # parse the original message
        encoding = get_email_charset(data)
        msg = self._parser.parsestr(data)

        # try to obtain sender public key
        senderPubkey = None
        fromHeader = msg.get('from', None)
        if (fromHeader is not None
            and (msg.get_content_type() == MULTIPART_ENCRYPTED
                 or msg.get_content_type() == MULTIPART_SIGNED)):
            _, senderAddress = parseaddr(fromHeader)
            try:
                senderPubkey = self._keymanager.get_key_from_cache(
                    senderAddress, OpenPGPKey)
            except keymanager_errors.KeyNotFound:
                pass

        valid_sig = False  # we will add a header saying if sig is valid
        decrypt_multi = self._decrypt_multipart_encrypted_msg
        decrypt_inline = self._maybe_decrypt_inline_encrypted_msg

        if msg.get_content_type() == MULTIPART_ENCRYPTED:
            decrmsg, valid_sig = decrypt_multi(
                msg, encoding, senderPubkey)
        else:
            decrmsg, valid_sig = decrypt_inline(
                msg, encoding, senderPubkey)

        # add x-leap-signature header
        if senderPubkey is None:
            decrmsg.add_header(
                self.LEAP_SIGNATURE_HEADER,
                self.LEAP_SIGNATURE_COULD_NOT_VERIFY)
        else:
            decrmsg.add_header(
                self.LEAP_SIGNATURE_HEADER,
                self.LEAP_SIGNATURE_VALID if valid_sig else
                self.LEAP_SIGNATURE_INVALID,
                pubkey=senderPubkey.key_id)

        return decrmsg.as_string()
예제 #3
0
    def _get_charset(self, stuff):
        """
        Gets (guesses?) the charset of a payload.

        :param stuff: the stuff to guess about.
        :type stuff: basestring
        :returns: charset
        """
        # XXX shouldn't we make the scope
        # of the decorator somewhat more persistent?
        # ah! yes! and put memory bounds.
        return get_email_charset(stuff)
예제 #4
0
    def _maybe_decrypt_msg(self, data):
        """
        Tries to decrypt a gpg message if data looks like one.

        :param data: the text to be decrypted.
        :type data: str

        :return: a Deferred that will be fired with an str of data, possibly
                 decrypted.
        :rtype: Deferred
        """
        leap_assert_type(data, str)
        log.msg('maybe decrypting doc')

        # parse the original message
        encoding = get_email_charset(data)
        msg = self._parser.parsestr(data)

        fromHeader = msg.get('from', None)
        senderAddress = None
        if (fromHeader is not None
            and (msg.get_content_type() == MULTIPART_ENCRYPTED
                 or msg.get_content_type() == MULTIPART_SIGNED)):
                senderAddress = parseaddr(fromHeader)[1]

        def add_leap_header(ret):
            decrmsg, signkey = ret
            if (senderAddress is None or signkey is None or
                    isinstance(signkey, keymanager_errors.KeyNotFound)):
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_COULD_NOT_VERIFY)
            elif isinstance(signkey, keymanager_errors.InvalidSignature):
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_INVALID)
            else:
                decrmsg.add_header(
                    self.LEAP_SIGNATURE_HEADER,
                    self.LEAP_SIGNATURE_VALID,
                    pubkey=signkey.key_id)
            return decrmsg.as_string()

        if msg.get_content_type() == MULTIPART_ENCRYPTED:
            d = self._decrypt_multipart_encrypted_msg(
                msg, encoding, senderAddress)
        else:
            d = self._maybe_decrypt_inline_encrypted_msg(
                msg, encoding, senderAddress)
        d.addCallback(add_leap_header)
        return d
예제 #5
0
    def _get_charset(self, stuff):
        # TODO put in a common class with LeapMessage
        """
        Gets (guesses?) the charset of a payload.

        :param stuff: the stuff to guess about.
        :type stuff: str or unicode
        :return: charset
        :rtype: unicode
        """
        # XXX existential doubt 2. shouldn't we make the scope
        # of the decorator somewhat more persistent?
        # ah! yes! and put memory bounds.
        return get_email_charset(stuff)