test_rfc2459.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc2459
  13. class CertificateTestCase(unittest.TestCase):
  14. pem_text = """\
  15. MIIC5zCCAlACAQEwDQYJKoZIhvcNAQEFBQAwgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0
  16. IFZhbGlkYXRpb24gTmV0d29yazEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAz
  17. BgNVBAsTLFZhbGlDZXJ0IENsYXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9y
  18. aXR5MSEwHwYDVQQDExhodHRwOi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG
  19. 9w0BCQEWEWluZm9AdmFsaWNlcnQuY29tMB4XDTk5MDYyNjAwMjIzM1oXDTE5MDYy
  20. NjAwMjIzM1owgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0IFZhbGlkYXRpb24gTmV0d29y
  21. azEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAzBgNVBAsTLFZhbGlDZXJ0IENs
  22. YXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9yaXR5MSEwHwYDVQQDExhodHRw
  23. Oi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG9w0BCQEWEWluZm9AdmFsaWNl
  24. cnQuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjmFGWHOjVsQaBalfD
  25. cnWTq8+epvzzFlLWLU2fNUSoLgRNB0mKOCn1dzfnt6td3zZxFJmP3MKS8edgkpfs
  26. 2Ejcv8ECIMYkpChMMFp2bbFc893enhBxoYjHW5tBbcqwuI4V7q0zK89HBFx1cQqY
  27. JJgpp0lZpd34t0NiYfPT4tBVPwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAFa7AliE
  28. Zwgs3x/be0kz9dNnnfS0ChCzycUs4pJqcXgn8nCDQtM+z6lU9PHYkhaM0QTLS6vJ
  29. n0WuPIqpsHEzXcjFV9+vqDWzf4mH6eglkrh/hXqu1rweN1gqZ8mRzyqBPu3GOd/A
  30. PhmcGcwTTYJBtYze4D1gCCAPRX5ron+jjBXu
  31. """
  32. def setUp(self):
  33. self.asn1Spec = rfc2459.Certificate()
  34. def testDerCodec(self):
  35. substrate = pem.readBase64fromText(self.pem_text)
  36. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  37. self.assertFalse(rest)
  38. self.assertTrue(asn1Object.prettyPrint())
  39. self.assertEqual(der_encoder(asn1Object), substrate)
  40. def testDerCodecDecodeOpenTypes(self):
  41. substrate = pem.readBase64fromText(self.pem_text)
  42. asn1Object, rest = der_decoder(
  43. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  44. self.assertFalse(rest)
  45. self.assertTrue(asn1Object.prettyPrint())
  46. self.assertEqual(der_encoder(asn1Object), substrate)
  47. class CertificateListTestCase(unittest.TestCase):
  48. pem_text = """\
  49. MIIBVjCBwAIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJBVTETMBEGA1UE
  50. CBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk
  51. MRUwEwYDVQQDEwxzbm1wbGFicy5jb20xIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25t
  52. cGxhYnMuY29tFw0xMjA0MTExMzQwNTlaFw0xMjA1MTExMzQwNTlaoA4wDDAKBgNV
  53. HRQEAwIBATANBgkqhkiG9w0BAQUFAAOBgQC1D/wwnrcY/uFBHGc6SyoYss2kn+nY
  54. RTwzXmmldbNTCQ03x5vkWGGIaRJdN8QeCzbEi7gpgxgpxAx6Y5WkxkMQ1UPjNM5n
  55. DGVDOtR0dskFrrbHuNpWqWrDaBN0/ryZiWKjr9JRbrpkHgVY29I1gLooQ6IHuKHY
  56. vjnIhxTFoCb5vA==
  57. """
  58. def setUp(self):
  59. self.asn1Spec = rfc2459.CertificateList()
  60. def testDerCodec(self):
  61. substrate = pem.readBase64fromText(self.pem_text)
  62. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  63. self.assertFalse(rest)
  64. self.assertTrue(asn1Object.prettyPrint())
  65. self.assertEqual(der_encoder(asn1Object), substrate)
  66. def testDerCodecDecodeOpenTypes(self):
  67. substrate = pem.readBase64fromText(self.pem_text)
  68. asn1Object, rest = der_decoder(
  69. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  70. self.assertFalse(rest)
  71. self.assertTrue(asn1Object.prettyPrint())
  72. self.assertEqual(der_encoder(asn1Object), substrate)
  73. class DSAPrivateKeyTestCase(unittest.TestCase):
  74. pem_text = """\
  75. MIIBugIBAAKBgQCN91+Cma8UPw09gjwP9WOJCdpv3mv3/qFqzgiODGZx0Q002iTl
  76. 1dq36m5TsWYFEcMCEyC3tFuoQ0mGq5zUUOmJvHCIPufs0g8Av0fhY77uFqneHHUi
  77. VQMCPCHX9vTCWskmDE21LJppU27bR4H2q+ysE30d6u3+84qrItsn4bjpcQIVAPR5
  78. QrmooOXDn7fHJzshmxImGC4VAoGAXxKyEnlvzq93d4V6KLWX3H5Jk2JP771Ss1bT
  79. 6D/mSbLlvjjo7qsj6diul1axu6Wny31oPertzA2FeGEzkqvjSNmSxyYYMDB3kEcx
  80. ahntt37I1FgSlgdZHuhdtl1h1DBKXqCCneOZuNj+kW5ib14u5HDfFIbec2HJbvVs
  81. lJ/k83kCgYB4TD8vgHetXHxqsiZDoy5wOnQ3mmFAfl8ZdQsIfov6kEgArwPYUOVB
  82. JsX84f+MFjIOKXUV8dHZ8VRrGCLAbXcxKqLNWKlKHUnEsvt63pkaTy/RKHyQS+pn
  83. wontdTt9EtbF+CqIWnm2wpn3O+SbdtawzPOL1CcGB0jYABwbeQ81RwIUFKdyRYaa
  84. INow2I3/ks+0MxDabTY=
  85. """
  86. def setUp(self):
  87. self.asn1Spec = rfc2459.DSAPrivateKey()
  88. def testDerCodec(self):
  89. substrate = pem.readBase64fromText(self.pem_text)
  90. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  91. self.assertFalse(rest)
  92. self.assertTrue(asn1Object.prettyPrint())
  93. self.assertEqual(substrate, der_encoder(asn1Object))
  94. def testDerCodecDecodeOpenTypes(self):
  95. substrate = pem.readBase64fromText(self.pem_text)
  96. asn1Object, rest = der_decoder(
  97. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  98. self.assertFalse(rest)
  99. self.assertTrue(asn1Object.prettyPrint())
  100. self.assertEqual(substrate, der_encoder(asn1Object))
  101. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  102. if __name__ == '__main__':
  103. result = unittest.TextTestRunner(verbosity=2).run(suite)
  104. sys.exit(not result.wasSuccessful())