1# 2# This file is part of pyasn1-modules software. 3# 4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> 5# License: http://snmplabs.com/pyasn1/license.html 6# 7import sys 8 9from pyasn1.codec.der import decoder as der_decoder 10from pyasn1.codec.der import encoder as der_encoder 11 12from pyasn1_modules import pem 13from pyasn1_modules import rfc2459 14 15try: 16 import unittest2 as unittest 17 18except ImportError: 19 import unittest 20 21 22class CertificateTestCase(unittest.TestCase): 23 pem_text = """\ 24MIIC5zCCAlACAQEwDQYJKoZIhvcNAQEFBQAwgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0 25IFZhbGlkYXRpb24gTmV0d29yazEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAz 26BgNVBAsTLFZhbGlDZXJ0IENsYXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9y 27aXR5MSEwHwYDVQQDExhodHRwOi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG 289w0BCQEWEWluZm9AdmFsaWNlcnQuY29tMB4XDTk5MDYyNjAwMjIzM1oXDTE5MDYy 29NjAwMjIzM1owgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0IFZhbGlkYXRpb24gTmV0d29y 30azEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAzBgNVBAsTLFZhbGlDZXJ0IENs 31YXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9yaXR5MSEwHwYDVQQDExhodHRw 32Oi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG9w0BCQEWEWluZm9AdmFsaWNl 33cnQuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjmFGWHOjVsQaBalfD 34cnWTq8+epvzzFlLWLU2fNUSoLgRNB0mKOCn1dzfnt6td3zZxFJmP3MKS8edgkpfs 352Ejcv8ECIMYkpChMMFp2bbFc893enhBxoYjHW5tBbcqwuI4V7q0zK89HBFx1cQqY 36JJgpp0lZpd34t0NiYfPT4tBVPwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAFa7AliE 37Zwgs3x/be0kz9dNnnfS0ChCzycUs4pJqcXgn8nCDQtM+z6lU9PHYkhaM0QTLS6vJ 38n0WuPIqpsHEzXcjFV9+vqDWzf4mH6eglkrh/hXqu1rweN1gqZ8mRzyqBPu3GOd/A 39PhmcGcwTTYJBtYze4D1gCCAPRX5ron+jjBXu 40""" 41 42 def setUp(self): 43 self.asn1Spec = rfc2459.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 def testDerCodecDecodeOpenTypes(self): 56 57 substrate = pem.readBase64fromText(self.pem_text) 58 59 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True) 60 61 assert not rest 62 assert asn1Object.prettyPrint() 63 assert der_encoder.encode(asn1Object) == substrate 64 65 66class CertificateListTestCase(unittest.TestCase): 67 pem_text = """\ 68MIIBVjCBwAIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJBVTETMBEGA1UE 69CBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk 70MRUwEwYDVQQDEwxzbm1wbGFicy5jb20xIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25t 71cGxhYnMuY29tFw0xMjA0MTExMzQwNTlaFw0xMjA1MTExMzQwNTlaoA4wDDAKBgNV 72HRQEAwIBATANBgkqhkiG9w0BAQUFAAOBgQC1D/wwnrcY/uFBHGc6SyoYss2kn+nY 73RTwzXmmldbNTCQ03x5vkWGGIaRJdN8QeCzbEi7gpgxgpxAx6Y5WkxkMQ1UPjNM5n 74DGVDOtR0dskFrrbHuNpWqWrDaBN0/ryZiWKjr9JRbrpkHgVY29I1gLooQ6IHuKHY 75vjnIhxTFoCb5vA== 76""" 77 78 def setUp(self): 79 self.asn1Spec = rfc2459.CertificateList() 80 81 def testDerCodec(self): 82 83 substrate = pem.readBase64fromText(self.pem_text) 84 85 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 86 87 assert not rest 88 assert asn1Object.prettyPrint() 89 assert der_encoder.encode(asn1Object) == substrate 90 91 def testDerCodecDecodeOpenTypes(self): 92 93 substrate = pem.readBase64fromText(self.pem_text) 94 95 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True) 96 97 assert not rest 98 assert asn1Object.prettyPrint() 99 assert der_encoder.encode(asn1Object) == substrate 100 101 102class DSAPrivateKeyTestCase(unittest.TestCase): 103 pem_text = """\ 104MIIBugIBAAKBgQCN91+Cma8UPw09gjwP9WOJCdpv3mv3/qFqzgiODGZx0Q002iTl 1051dq36m5TsWYFEcMCEyC3tFuoQ0mGq5zUUOmJvHCIPufs0g8Av0fhY77uFqneHHUi 106VQMCPCHX9vTCWskmDE21LJppU27bR4H2q+ysE30d6u3+84qrItsn4bjpcQIVAPR5 107QrmooOXDn7fHJzshmxImGC4VAoGAXxKyEnlvzq93d4V6KLWX3H5Jk2JP771Ss1bT 1086D/mSbLlvjjo7qsj6diul1axu6Wny31oPertzA2FeGEzkqvjSNmSxyYYMDB3kEcx 109ahntt37I1FgSlgdZHuhdtl1h1DBKXqCCneOZuNj+kW5ib14u5HDfFIbec2HJbvVs 110lJ/k83kCgYB4TD8vgHetXHxqsiZDoy5wOnQ3mmFAfl8ZdQsIfov6kEgArwPYUOVB 111JsX84f+MFjIOKXUV8dHZ8VRrGCLAbXcxKqLNWKlKHUnEsvt63pkaTy/RKHyQS+pn 112wontdTt9EtbF+CqIWnm2wpn3O+SbdtawzPOL1CcGB0jYABwbeQ81RwIUFKdyRYaa 113INow2I3/ks+0MxDabTY= 114""" 115 116 def setUp(self): 117 self.asn1Spec = rfc2459.DSAPrivateKey() 118 119 def testDerCodec(self): 120 121 substrate = pem.readBase64fromText(self.pem_text) 122 123 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 124 125 assert not rest 126 assert asn1Object.prettyPrint() 127 assert der_encoder.encode(asn1Object) == substrate 128 129 def testDerCodecDecodeOpenTypes(self): 130 131 substrate = pem.readBase64fromText(self.pem_text) 132 133 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True) 134 135 assert not rest 136 assert asn1Object.prettyPrint() 137 assert der_encoder.encode(asn1Object) == substrate 138 139 140suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 141 142if __name__ == '__main__': 143 import sys 144 145 result = unittest.TextTestRunner(verbosity=2).run(suite) 146 sys.exit(not result.wasSuccessful()) 147