def test_decrypt_private_key_without_ca_des_ede3_cbc(self): encrypted_text = '''MIME-Version: 1.0 Content-Disposition: attachment; filename="smime.p7m" Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m" Content-Transfer-Encoding: base64 MIICAgYJKoZIhvcNAQcDoIIB8zCCAe8CAQAxggGrMIIBpwIBADCBjjCBgDELMAkG A1UEBhMCQ04xCzAJBgNVBAgMAlNDMQswCQYDVQQHDAJDRDEPMA0GA1UECgwGTmV3 ZWdnMQwwCgYDVQQLDANFREkxGDAWBgNVBAMMD0FTMiBDZXJ0aWZpY2F0ZTEeMBwG CSqGSIb3DQEJARYPdHIyOUBuZXdlZ2cuY29tAgkAtl4VWyGqGb4wDQYJKoZIhvcN AQEBBQAEggEAzzzLpEFOzQDULtCBBcoDLobNGXSPUfSI45uatdlnqrwh8EsG2qan Wbti6jMRIm6jvVX7u/lrI/gNK/zpWiMvg3VIgCN1vgPRE808Tx//EPXbyFH+ODGT n/n77he+krbKY7kFwv86udat2OjzMaGjeQpQ2ILW3YYsf+Ce0zCa/AA0IYQeiZVN G1ksb3kLstdYsU7hOi6OhgZlAYPZSe3zwnEjqe/uToOl8XedEkvmQvNbNbwEzJhB 44h9O7WTABD8DBHydgPahrOWqmcAOdPRjHHyRSnsss9HY9dCFjLHjiWexSszYLAr 5FgGcwITA+YSAMM3fujgFhJFAp1Z/xtQuDA7BgkqhkiG9w0BBwEwFAYIKoZIhvcN AwcECIa9BZlgtfVYgBh82ue/PhnoQ6/wrwekWNPOkVJN8kX2xPw= ''' key_file_path = u'' + self.__get_cert_path('P1_private.pem') pass_phrase = u"123456" rst = SMIMEHelper.decrypt_from_text(encrypted_text, key_file_path, pass_phrase) assert_equal(self.clearText, rst)
def test_decrypt_private_key_with_ca_aes_128_cbc(self): encrypted_text = '''MIME-Version: 1.0 Content-Disposition: attachment; filename="smime.p7m" Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m" Content-Transfer-Encoding: base64 MIICQgYJKoZIhvcNAQcDoIICMzCCAi8CAQAxggHsMIIB6AIBADCBzzCBujELMAkG A1UEBhMCVVMxFjAUBgNVBAoTDUVudHJ1c3QsIEluYy4xKDAmBgNVBAsTH1NlZSB3 d3cuZW50cnVzdC5uZXQvbGVnYWwtdGVybXMxOTA3BgNVBAsTMChjKSAyMDEyIEVu dHJ1c3QsIEluYy4gLSBmb3IgYXV0aG9yaXplZCB1c2Ugb25seTEuMCwGA1UEAxMl RW50cnVzdCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eSAtIEwxSwIQHnF4IP05B10A AAAAUNw1czANBgkqhkiG9w0BAQEFAASCAQCeEwTDvzE8KQgO+Pt/vcGfj/ZMKu20 dJz6AufIIBvHmWIQFGAL/PQ3Fk/Awxcyvz9/LvOXvNEAHnxifRXb2LqnoW+Uxoys gY7eccsx/cQleZ+/GIeY3INPF74BX7ywzhxjy018wvfY8PkuCtWSe8vYKZTmksY4 qdrFxaWIhLHHZDNbRaZ/vrk/BpxZvm5BQdd1B1+g6E8+PVNX8Z/lJXcHmpkdKmEe WZZtqYUQoZpp70eMPMifl0Beyp2bgP6DwGvPmk2Wv7pNE1+wu3K7C5XoAQ6NfeZp bhu51OP2Rb28NFwFqSt984vbThCzaRDZyL4eLi5oUtw5H8L7IUQuh3Z2MDoGCSqG SIb3DQEHATALBglghkgBZQMEAQGAIIIt/NjfM683MbhOQTMf3JuRgSYHx+je3OjP GnNVcfZP ''' key_file_path = self.__get_cert_path('edi04_with_ca.pem') pass_phrase = "newegg@123" rst = SMIMEHelper.decrypt_from_text(encrypted_text, key_file_path, pass_phrase) assert_equal(self.clearText, rst)
def _decrypt(self): if not self.is_encrypted: self.context.trace("decrypt ignored") return content_type = self.mime_message.get_content_type().lower() s_mime_type = self.mime_message.get_param('smime-type') if content_type != 'application/pkcs7-mime': raise AS2DecryptException('decrypt failed; content-type:{type} invalid', type=content_type) if s_mime_type != 'enveloped-data': raise AS2DecryptException('decrypt failed; s/mime-type:{type} invalid', type=s_mime_type) cert_thumbprint = self.context.agreement.inbound_agreement.message_decrypt_certificate.thumbprint cert_local_file_path = self.context.agreement.inbound_agreement.message_decrypt_certificate.local_file_path cert_pass_phrase = self.context.agreement.inbound_agreement.message_decrypt_certificate.pass_phrase try: content_transfer_encoding = self.mime_message.get('Content-Transfer-Encoding', '').lower() if 'base64' != content_transfer_encoding: del self.mime_message['Content-Transfer-Encoding'] email.encoders.encode_base64(self.mime_message) self.context.trace('content transfer encoding to base64') f_mime_string = SMIMEHelper.format_with_cr_lf( SMIMEHelper.mime_to_string(self.mime_message, 78) ) decrypted_content = SMIMEHelper.decrypt_from_text( f_mime_string, cert_local_file_path, cert_pass_phrase ) self.mime_message = email.message_from_string(decrypted_content) self.context.trace("decrypt finished; thumbprint: {thumbprint}", thumbprint=cert_thumbprint) except: logger.exception("decrypt failed; message-id: {id}".format(id=self.message_id)) raise AS2DecryptException("decrypt failed; thumbprint: {thumbprint}, due to: {message}", thumbprint=cert_thumbprint, message=sys.exc_info()[1])