1 # 2 # This file is part of pyasn1-modules software. 3 # 4 # Copyright (c) 2005-2017, Ilya Etingof <etingof (at] gmail.com> 5 # License: http://pyasn1.sf.net/license.html 6 # 7 import sys 8 9 from pyasn1.codec.der import decoder as der_decoder 10 from pyasn1.codec.der import encoder as der_encoder 11 12 from pyasn1_modules import pem 13 from pyasn1_modules import rfc5280 14 15 try: 16 import unittest2 as unittest 17 18 except ImportError: 19 import unittest 20 21 22 class CertificateTestCase(unittest.TestCase): 23 pem_text = """\ 24 MIIC5zCCAlACAQEwDQYJKoZIhvcNAQEFBQAwgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0 25 IFZhbGlkYXRpb24gTmV0d29yazEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAz 26 BgNVBAsTLFZhbGlDZXJ0IENsYXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9y 27 aXR5MSEwHwYDVQQDExhodHRwOi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG 28 9w0BCQEWEWluZm9AdmFsaWNlcnQuY29tMB4XDTk5MDYyNjAwMjIzM1oXDTE5MDYy 29 NjAwMjIzM1owgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0IFZhbGlkYXRpb24gTmV0d29y 30 azEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAzBgNVBAsTLFZhbGlDZXJ0IENs 31 YXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9yaXR5MSEwHwYDVQQDExhodHRw 32 Oi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG9w0BCQEWEWluZm9AdmFsaWNl 33 cnQuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjmFGWHOjVsQaBalfD 34 cnWTq8+epvzzFlLWLU2fNUSoLgRNB0mKOCn1dzfnt6td3zZxFJmP3MKS8edgkpfs 35 2Ejcv8ECIMYkpChMMFp2bbFc893enhBxoYjHW5tBbcqwuI4V7q0zK89HBFx1cQqY 36 JJgpp0lZpd34t0NiYfPT4tBVPwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAFa7AliE 37 Zwgs3x/be0kz9dNnnfS0ChCzycUs4pJqcXgn8nCDQtM+z6lU9PHYkhaM0QTLS6vJ 38 n0WuPIqpsHEzXcjFV9+vqDWzf4mH6eglkrh/hXqu1rweN1gqZ8mRzyqBPu3GOd/A 39 PhmcGcwTTYJBtYze4D1gCCAPRX5ron+jjBXu 40 """ 41 42 def setUp(self): 43 self.asn1Spec = rfc5280.Certificate() 44 45 def testDerCodec(self): 46 47 substrate = pem.readBase64fromText(self.pem_text) 48 49 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 50 51 assert not rest 52 assert asn1Object.prettyPrint() 53 assert der_encoder.encode(asn1Object) == substrate 54 55 56 class CertificateListTestCase(unittest.TestCase): 57 pem_text = """\ 58 MIIBVjCBwAIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJBVTETMBEGA1UE 59 CBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk 60 MRUwEwYDVQQDEwxzbm1wbGFicy5jb20xIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25t 61 cGxhYnMuY29tFw0xMjA0MTExMzQwNTlaFw0xMjA1MTExMzQwNTlaoA4wDDAKBgNV 62 HRQEAwIBATANBgkqhkiG9w0BAQUFAAOBgQC1D/wwnrcY/uFBHGc6SyoYss2kn+nY 63 RTwzXmmldbNTCQ03x5vkWGGIaRJdN8QeCzbEi7gpgxgpxAx6Y5WkxkMQ1UPjNM5n 64 DGVDOtR0dskFrrbHuNpWqWrDaBN0/ryZiWKjr9JRbrpkHgVY29I1gLooQ6IHuKHY 65 vjnIhxTFoCb5vA== 66 """ 67 68 def setUp(self): 69 self.asn1Spec = rfc5280.CertificateList() 70 71 def testDerCodec(self): 72 73 substrate = pem.readBase64fromText(self.pem_text) 74 75 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 76 77 assert not rest 78 assert asn1Object.prettyPrint() 79 assert der_encoder.encode(asn1Object) == substrate 80 81 82 suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 83 84 if __name__ == '__main__': 85 unittest.TextTestRunner(verbosity=2).run(suite) 86