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 rfc5208 14 15try: 16 import unittest2 as unittest 17 18except ImportError: 19 import unittest 20 21 22class PrivateKeyInfoTestCase(unittest.TestCase): 23 pem_text = """\ 24MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAx8CO8E0MNgEKXXDf 25I1xqBmQ+Gp3Srkqp45OApIu4lZ97n5VJ5HljU9wXcPIfx29Le3w8hCPEkugpLsdV 26GWx+EQIDAQABAkEAiv3f+DGEh6ddsPszKQXK+LuTwy2CRajKYgJnBxf5zpG50XK4 27899An+x/pGYVmVED1f0JCbk3BUbv7HViLq0qgQIhAOYlQJaQ8KJBijDpjF62lcVr 28QrqFPM4+ZrHsw0dVY2CZAiEA3jE5ngkVPfjFWEr7wS50EJhGiYlQeY4l+hADGIhd 29XDkCIQDIHt5xzmif/nOGop5/gS7ssp8ch1zfTh2IW4NWlOZMCQIgLZmYo5BlpaRK 30jAZHiKwJ8eXuhAeEVo4PyTREDmLeFjECIQCfyUPDclPo2O8ycPpozwoGwvKFrNZJ 31VWRpRKqYnOAIXQ== 32""" 33 34 def setUp(self): 35 self.asn1Spec = rfc5208.PrivateKeyInfo() 36 37 def testDerCodec(self): 38 39 substrate = pem.readBase64fromText(self.pem_text) 40 41 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 42 43 assert not rest 44 assert asn1Object.prettyPrint() 45 assert der_encoder.encode(asn1Object) == substrate 46 47 48class EncryptedPrivateKeyInfoInfoTestCase(unittest.TestCase): 49 pem_text = """\ 50MIIBgTAbBgkqhkiG9w0BBQMwDgQIdtFgDWnipT8CAggABIIBYN0hkm2xqkTCt8dJ 51iZS8+HNiyHxy8g+rmWSXv/i+bTHFUReZA2GINtTRUkWpXqWcSHxNslgf7QdfgbVJ 52xQiUM+lLhwOFh85iAHR3xmPU1wfN9NvY9DiLSpM0DMhF3OvAMZD75zIhA0GSKu7w 53dUu7ey7H4fv7bez6RhEyLdKw9/Lf2KNStNOs4ow9CAtCoxeoMSniTt6CNhbvCkve 549vNHKiGavX1tS/YTog4wiiGzh2YxuW1RiQpTdhWiKyECgD8qQVg2tY5t3QRcXrzi 55OkStpkiAPAbiwS/gyHpsqiLo0al63SCxRefugbn1ucZyc5Ya59e3xNFQXCNhYl+Z 56Hl3hIl3cssdWZkJ455Z/bBE29ks1HtsL+bTfFi+kw/4yuMzoaB8C7rXScpGNI/8E 57pvTU2+wtuoOFcttJregtR94ZHu5wgdYqRydmFNG8PnvZT1mRMmQgUe/vp88FMmsZ 58dLsZjNQ= 59""" 60 61 def setUp(self): 62 self.asn1Spec = rfc5208.EncryptedPrivateKeyInfo() 63 64 def testDerCodec(self): 65 substrate = pem.readBase64fromText(self.pem_text) 66 67 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 68 69 assert not rest 70 assert asn1Object.prettyPrint() 71 assert der_encoder.encode(asn1Object) == substrate 72 73 74suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 75 76if __name__ == '__main__': 77 import sys 78 79 result = unittest.TextTestRunner(verbosity=2).run(suite) 80 sys.exit(not result.wasSuccessful()) 81