def save_mdn(message, mdnContent): ''' Process the received MDN and check status of sent message ''' try: mdnMessage = email.message_from_string(mdnContent) mdnHeaders = '' for key in mdnMessage.keys(): mdnHeaders = mdnHeaders + '%s: %s\n'%(key, mdnMessage[key]) messageId = mdnMessage.get('message-id') if message.partner.mdn_sign and mdnMessage.get_content_type() != 'multipart/signed': models.Log.objects.create(message=message, status='W', text=_(u'Expected signed MDN but unsigned MDN returned')) mdnsigned = False if mdnMessage.get_content_type() == 'multipart/signed': models.Log.objects.create(message=message, status='S', text=_(u'Verifying the signed MDN with partner key %s'%message.partner.signature_key)) mdnsigned = True cert = str(message.partner.signature_key.certificate.path) ca_cert = cert if message.partner.signature_key.ca_cert: ca_cert = str(message.partner.signature_key.ca_cert.path) verify_cert = message.partner.signature_key.verify_cert main_boundary = '--' + mdnMessage.get_boundary() ### Extract the mssage and signature for part in mdnMessage.get_payload(): if part.get_content_type().lower() == "application/pkcs7-signature": sig = part else: mdnMessage = part ### check if signature is base64 encoded and if not encode try: raw_sig = sig.get_payload().encode('ascii').strip() except Exception,e: raw_sig = sig.get_payload().encode('base64').strip() ### Verify the signature using raw contents try: as2utils.verify_payload(mdnContent,None,cert,ca_cert,verify_cert) except Exception, e: ### Verify the signature using extracted signature and message try: as2utils.verify_payload(as2utils.extractpayload_fromstring1(mdnContent,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: ### Verify the signature using extracted signature and message without extra trailing new line in message try: as2utils.verify_payload(as2utils.extractpayload_fromstring2(mdnContent,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: raise as2utils.as2exception(_(u'MDN Signature Verification Error, exception message is %s' %e))
raw_sig = part.get_payload().encode('ascii').strip() except Exception,e: raw_sig = part.get_payload().encode('base64').strip() else: payload = part ### Verify message using complete raw payload received from partner try: as2utils.verify_payload(as2utils.canonicalize2(payload),raw_sig,cert,ca_cert,verify_cert) except Exception, e: ### Verify message using extracted signature and stripped message try: as2utils.verify_payload(as2utils.extractpayload_fromstring1(raw_payload,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: ### Verify message using extracted signature and message without extra trailing new line try: as2utils.verify_payload(as2utils.extractpayload_fromstring2(raw_payload,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: raise as2utils.as2invalidsignature('Signature Verification Failed, exception message is %s'%str(e)) micContent = as2utils.canonicalize2(payload) #micContent = as2utils.extractpayload_fromstring2(raw_payload,main_boundary) if payload.get_content_type() == 'application/pkcs7-mime' and payload.get_param('smime-type') == 'compressed-data': models.Log.objects.create(message=message, status='S', text=_(u'Decompressing the payload')) message.compressed = True ### Decode the data if its base64 try: payload.get_payload().encode('ascii') cdata = base64.b64decode(payload.get_payload()) except Exception,e: cdata = payload.get_payload() pyas2init.logger.debug('Decompressing the payload:\n%s'%cdata) try:
as2utils.verify_payload(as2utils.canonicalize2(payload), raw_sig, cert, ca_cert, verify_cert) except Exception, e: ### Verify message using extracted signature and stripped message try: as2utils.verify_payload( as2utils.extractpayload_fromstring1(raw_payload, main_boundary), raw_sig, cert, ca_cert, verify_cert, ) except Exception, e: ### Verify message using extracted signature and message without extra trailing new line try: as2utils.verify_payload( as2utils.extractpayload_fromstring2(raw_payload, main_boundary), raw_sig, cert, ca_cert, verify_cert, ) except Exception, e: raise as2utils.as2invalidsignature( "Signature Verification Failed, exception message is %s" % str(e) ) micContent = as2utils.canonicalize2(payload) # micContent = as2utils.extractpayload_fromstring2(raw_payload,main_boundary) if ( payload.get_content_type() == "application/pkcs7-mime" and payload.get_param("smime-type") == "compressed-data" ):