Exemplo n.º 1
0
def gpg_decrypt_all_payloads(message):

    # We don't want to modify the original message
    decrypted_message = copy.deepcopy(message)

    # Check if message is PGP/MIME encrypted
    if not (message.get_param('protocol') is None) and message.get_param(
            'protocol'
    ) == 'application/pgp-encrypted' and message.is_multipart():
        decrypted_message = decrypt_mime(decrypted_message)

    # At this point the message could only be PGP/INLINE encrypted, unencrypted or
    # encrypted with a mechanism not covered by GPG-Mailgate
    elif get_bool_from_cfg('default', 'no_inline_dec', 'no'):
        # Check if message is PGP/INLINE encrypted and has attachments (or unencrypted with attachments)
        if message.is_multipart():

            # Set message's payload to list so payloads can be attached later on
            decrypted_message.set_payload(list())

            # We only need to hand over the original message here. Not needed for other decrypt implementations.
            decrypted_message, success = decrypt_inline_with_attachments(
                message, False, decrypted_message)

            # Add header here to avoid it being appended several times
            if get_bool_from_cfg('default', 'add_header', 'yes') and success:
                decrypted_message[
                    'X-GPG-Mailgate'] = 'Decrypted by GPG Mailgate'

        # Check if message is PGP/INLINE encrypted without attachments (or unencrypted without attachments)
        else:
            decrypted_message = decrypt_inline_without_attachments(
                decrypted_message)

    return decrypted_message
Exemplo n.º 2
0
    def read(self) -> Tuple[str, email.message.Message, bytes]:
        data, entry_type = self.__file.read()

        headers_length = data.index(b'\n\n') + 2

        parser = email.parser.BytesParser()
        message = parser.parsebytes(data[:headers_length])
        content = data[headers_length:]

        if 'Checksum' in message:
            should_checksum = cast(str, message['Checksum']).split(';')[0]
            checksum_type = cast(str,
                                 message.get_param('type', None, 'Checksum'))
            if checksum_type is None:
                raise BadFileFormatError("Checksum type not specified")
            if checksum_type == 'md5':
                have_checksum = hashlib.md5(content).hexdigest()
            else:
                raise BadFileFormatError("Unsupported checksum type '%s'" %
                                         checksum_type)

            if have_checksum != should_checksum:
                logger.error("%r", content)
                raise CorruptedFileError("Checksum mismatch (%s != %s)" %
                                         (have_checksum, should_checksum))

        encoding = cast(str, message.get_param('charset', 'ascii'))

        return content.decode(encoding), message, entry_type
Exemplo n.º 3
0
    def read(self):
        data, entry_type = super().read()

        headers_length = data.index(b'\n\n') + 2

        parser = email.parser.BytesParser()
        message = parser.parsebytes(data[:headers_length])
        content = data[headers_length:]

        if 'Checksum' in message:
            should_checksum = message['Checksum'].split(';')[0]
            checksum_type = message.get_param('type', None, 'Checksum')
            if checksum_type is None:
                raise BadFileFormatError("Checksum type not specified")
            if checksum_type == 'md5':
                have_checksum = hashlib.md5(content).hexdigest()
            else:
                raise BadFileFormatError(
                    "Unsupported checksum type '%s'" % checksum_type)

            if have_checksum != should_checksum:
                logger.error("%r", content)
                raise CorruptedFileError(
                    "Checksum mismatch (%s != %s)"
                    % (have_checksum, should_checksum))

        encoding = message.get_param('charset', 'ascii')

        return content.decode(encoding), message, entry_type
Exemplo n.º 4
0
def gpg_decrypt_all_payloads( message ):

	# We don't want to modify the original message
	decrypted_message = copy.deepcopy(message)

	# Check if message is PGP/MIME encrypted
	if not (message.get_param('protocol') is None) and message.get_param('protocol') == 'application/pgp-encrypted' and message.is_multipart():
		decrypted_message = decrypt_mime(decrypted_message)

	# At this point the message could only be PGP/INLINE encrypted, unencrypted or
	# encrypted with a mechanism not covered by GPG-Mailgate
	elif get_bool_from_cfg('default', 'no_inline_dec', 'no'):
		# Check if message is PGP/INLINE encrypted and has attachments (or unencrypted with attachments)
		if message.is_multipart():

			# Set message's payload to list so payloads can be attached later on
			decrypted_message.set_payload(list())

			# We only need to hand over the original message here. Not needed for other decrypt implementations.
			decrypted_message, success = decrypt_inline_with_attachments(message, False, decrypted_message)

			# Add header here to avoid it being appended several times
			if get_bool_from_cfg('default', 'add_header', 'yes') and success:
				decrypted_message['X-GPG-Mailgate'] = 'Decrypted by GPG Mailgate'

		# Check if message is PGP/INLINE encrypted without attachments (or unencrypted without attachments)
		else:
			decrypted_message = decrypt_inline_without_attachments(decrypted_message)

	return decrypted_message
Exemplo n.º 5
0
    def read(self) -> Tuple[FileInfo, bytes]:
        if not os.path.exists(self.path):
            raise NotFoundError()

        with open(self.path, 'rb') as fp:
            magic = fp.read(len(self.MAGIC))
            if magic != self.MAGIC:
                raise BadFileFormatError("Not an noisicaä file")

            # email.parser's headersonly attribute doesn't seem to work the
            # way I would expect it.
            headers = b''
            while headers[-2:] != b'\n\n':
                b = fp.read(1)
                if not b:
                    break
                headers += b

            parser = email.parser.BytesParser()
            message = parser.parsebytes(headers)

            content = fp.read()

            if 'Checksum' in message:
                should_checksum = cast(str, message['Checksum']).split(';')[0]
                checksum_type = cast(
                    str, message.get_param('type', None, 'Checksum'))
                if checksum_type is None:
                    raise BadFileFormatError("Checksum type not specified")
                if checksum_type == 'md5':
                    have_checksum = hashlib.md5(content).hexdigest()
                else:
                    raise BadFileFormatError("Unsupported checksum type '%s'" %
                                             checksum_type)

                if have_checksum != should_checksum:
                    raise CorruptedFileError("Checksum mismatch (%s != %s)" %
                                             (have_checksum, should_checksum))

            file_info = FileInfo()
            file_info.content_type = message.get_content_type()
            file_info.encoding = cast(str,
                                      message.get_param('charset', 'ascii'))
            if 'Version' in message:
                file_info.version = int(cast(str, message['Version']))
            if 'File-Type' in message:
                file_info.filetype = cast(str, message['File-Type'])

            return file_info, content
Exemplo n.º 6
0
    def read(self):
        if not os.path.exists(self.path):
            raise NotFoundError()

        with open(self.path, 'rb') as fp:
            magic = fp.read(len(self.MAGIC))
            if magic != self.MAGIC:
                raise BadFileFormatError("Not an noisicaä file")

            # email.parser's headersonly attribute doesn't seem to work the
            # way I would expect it.
            headers = b''
            while headers[-2:] != b'\n\n':
                b = fp.read(1)
                if not b:
                    break
                headers += b

            parser = email.parser.BytesParser()
            message = parser.parsebytes(headers)

            content = fp.read()

            if 'Checksum' in message:
                should_checksum = message['Checksum'].split(';')[0]
                checksum_type = message.get_param('type', None, 'Checksum')
                if checksum_type is None:
                    raise BadFileFormatError("Checksum type not specified")
                if checksum_type == 'md5':
                    have_checksum = hashlib.md5(content).hexdigest()
                else:
                    raise BadFileFormatError(
                        "Unsupported checksum type '%s'" % checksum_type)

                if have_checksum != should_checksum:
                    raise CorruptedFileError(
                        "Checksum mismatch (%s != %s)"
                        % (have_checksum, should_checksum))

            file_info = FileInfo()
            file_info.content_type = message.get_content_type()
            file_info.encoding = message.get_param('charset', 'ascii')
            if 'Version' in message:
                file_info.version = int(message['Version'])
            if 'File-Type' in message:
                file_info.filetype = message['File-Type']

            return file_info, content