Esempio n. 1
0
    def test_extract_payload_with_multipart(self):
        signed_mime_message = MIMEMultipart('signed', protocol="application/pkcs7-signature")

        payload = SMIMEHelper.extract_payload(signed_mime_message)

        assert_not_equal(None, payload)
        assert_not_equal("", payload)
        assert_equal(True, payload.startswith("--"))
Esempio n. 2
0
    def test_extract_payload(self):
        text_plain = "test message"

        mime_message = email.Message.Message()
        mime_message.set_payload(text_plain)

        payload = SMIMEHelper.extract_payload(mime_message)

        assert_not_equal(None, payload)
        assert_not_equal("", payload)
        assert_equal(text_plain, payload)
Esempio n. 3
0
    def _signature(self):
        if not self.is_signed:
            self.context.trace("signature ignored")
            return

        cert_thumbprint = self.context.agreement.outbound_agreement.message_signature_certificate.thumbprint
        cert_local_file_path = self.context.agreement.outbound_agreement.message_signature_certificate.local_file_path
        cert_pass_phrase = self.context.agreement.outbound_agreement.message_signature_certificate.pass_phrase
        cert_signature_algorithm = self.context.agreement.outbound_agreement.message_signature_algorithm

        try:
            f_mime_string = SMIMEHelper.format_with_cr_lf(
                SMIMEHelper.mime_to_string(self.mime_message))

            self.mic_content = f_mime_string
            self.mic_algorithm = self.context.agreement.outbound_agreement.message_signature_algorithm

            detached_signed_message = SMIMEHelper.sign_to_mime_detached(
                f_mime_string,
                cert_local_file_path,
                cert_pass_phrase,
                cert_signature_algorithm)

            signature = SMIMEHelper.get_signature_from_mime(detached_signed_message)
            if signature is None:
                raise Exception("signature is none from detached signed mime message")

            signed_mime_message = MIMEMultipart('signed', boundary=SMIMEHelper.get_random_boundary(),
                                                protocol="application/pkcs7-signature")
            del signed_mime_message['MIME-Version']

            signed_mime_message.set_param('micalg', self.mic_algorithm)
            signed_mime_message.attach(self.mime_message)
            signed_mime_message.attach(signature)

            self.body = SMIMEHelper.format_with_cr_lf(SMIMEHelper.extract_payload(signed_mime_message))
            self.mime_message = signed_mime_message

            self.context.trace("signature finished; thumbprint: {thumbprint}, algorithm: {algorithm}",
                               thumbprint=cert_thumbprint,
                               algorithm=cert_signature_algorithm)
        except:
            logger.exception("signature failed; message-id: {id}".format(id=self.message_id))
            raise AS2SignatureException(
                "signature failed; thumbprint: {thumbprint}, algorithm: {algorithm}, due to: {message}",
                thumbprint=cert_thumbprint,
                algorithm=cert_signature_algorithm,
                message=sys.exc_info()[1])
Esempio n. 4
0
    def encode(self):
        try:
            self._init_mode(self.context.message.message_mdn.mdn_mode)

            self._init_message_id(self.context.message.message_mdn)

            self._init_disposition(self.context.message.message_mdn)

            self._init_mic(self.context.message.message_is_mic,
                           self.context.message.message_mdn)

            mdn_report = MIMEMultipart(
                'report',
                boundary=SMIMEHelper.get_random_boundary(),
                report_type="disposition-notification")

            mdn_confirmation_text = self._build_confirmation_mime()
            mdn_feedback_content = self._build_feedback_content_mime()

            mdn_report.attach(mdn_confirmation_text)
            mdn_report.attach(mdn_feedback_content)
            del mdn_report['MIME-Version']

            if self.context.agreement.inbound_agreement.is_mdn_signed:
                mdn_message = self._signature_mdn_mime(mdn_report)
            else:
                mdn_message = mdn_report
                self.context.trace("mdn signature ignore")

            mdn_headers = self._build_mdn_headers(mdn_message)

            mdn_body = SMIMEHelper.format_with_cr_lf(
                SMIMEHelper.extract_payload(mdn_message))

            return mdn_headers, mdn_body
        except AS2MdnException as ex:
            self.context.trace_error(ex.msg)
            raise
        except:
            self.context.trace_error(sys.exc_info()[1])
            raise