def test_sign_private_cert_with_ca_sha_1(self): crt_path = self.__get_cert_path('edi04_with_ca.pem') pass_phrase = "newegg@123" rst = SMIMEHelper.sign_to_mime_detached(self.clearText, crt_path, pass_phrase) assert_not_equal(None, rst) assert_equal('sha1', rst.get_param('micalg'))
def test_sign_private_cert_without_ca_md5(self): crt_path = self.__get_cert_path('P1_private.pem') pass_phrase = "123456" alg = 'md5' rst = SMIMEHelper.sign_to_mime_detached(self.clearText, crt_path, pass_phrase, alg) assert_not_equal(None, rst) assert_equal('md5', rst.get_param('micalg'))
def test_sign_private_cert_with_china_without_ca_sha_1(self): clearText = '测试文件' crt_path = self.__get_cert_path('P1_private.pem') pass_phrase = "123456" rst = SMIMEHelper.sign_to_mime_detached(clearText, crt_path, pass_phrase) assert_not_equal(None, rst) assert_equal('sha1', rst.get_param('micalg'))
def test_sign_private_cert_with_iso_8859_01_without_ca_sha_1(self): clearText = 'ISA¦00¦ ¦00¦ ¦16¦081940553PA10 ¦ZZ¦5626958823BVF ¦170522¦1940¦U¦00401¦001038283¦0¦P¦`~' crt_path = self.__get_cert_path('P1_private.pem') pass_phrase = "123456" rst = SMIMEHelper.sign_to_mime_detached(clearText, crt_path, pass_phrase) assert_not_equal(None, rst) assert_equal('sha1', rst.get_param('micalg'))
def test_get_signature_from_mime_without_Override(self): crt_path = self.__get_cert_path('edi04_with_ca.pem') pass_phrase = "newegg@123" rst = SMIMEHelper.sign_to_mime_detached(self.clearText, crt_path, pass_phrase) signature = SMIMEHelper.get_signature_from_mime(rst, False, 'application/pkcs7-signature') assert_not_equal(None, signature) assert_equal(True, "Content-Disposition" in signature) assert_equal(True, "Content-Type" in signature)
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 _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
def test_sign_private_cert_without_ca_md5_pass_phrase_is_error(self): crt_path = self.__get_cert_path('P1_private.pem') pass_phrase = "123" alg = 'md5' SMIMEHelper.sign_to_mime_detached(self.clearText, crt_path, pass_phrase, alg)