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])
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
def _signature_mdn_mime(self, mdn_report): signed_mdn_report = MIMEMultipart( 'signed', boundary=SMIMEHelper.get_random_boundary(), protocol="application/pkcs7-signature") signed_mdn_report.attach(mdn_report) 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.inbound_agreement.mdn_signature_algorithm try: mime_message = SMIMEHelper.sign_to_mime_detached( SMIMEHelper.format_with_cr_lf( SMIMEHelper.mime_to_string(mdn_report)), cert_local_file_path, cert_pass_phrase, cert_signature_algorithm) signature = SMIMEHelper.get_signature_from_mime(mime_message) del signature['MIME-Version'] signed_mdn_report.set_param('micalg', cert_signature_algorithm) signed_mdn_report.attach(signature) self.context.trace( "mdn signature finished; thumbprint: {thumbprint}, algorithm: {algorithm}", thumbprint=cert_thumbprint, algorithm=cert_signature_algorithm) except: logger.exception('sign mdn failed') raise AS2MdnException( "mdn signature failed; thumbprint: {thumbprint}, algorithm: {algorithm}, due to: {message}", thumbprint=cert_thumbprint, algorithm=cert_signature_algorithm, message=sys.exc_info()[1]) return signed_mdn_report