test_rfc8649.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, Vigil Security, LLC
  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 rfc4055
  13. from pyasn1_modules import rfc5280
  14. from pyasn1_modules import rfc8649
  15. class RootCertificateExtnTestCase(unittest.TestCase):
  16. extn_pem_text = """\
  17. MGEGCisGAQQBg5IbAgEEUzBRMA0GCWCGSAFlAwQCAwUABEBxId+rK+WVDLOda2Yk
  18. FFRbqQAztXhs91j/RxHjYJIv/3gleQg3Qix/yQy2rIg3xysjCvHWw8AuYOGVh/sL
  19. GANG
  20. """
  21. def setUp(self):
  22. self.asn1Spec = rfc5280.Extension()
  23. def testDerCodec(self):
  24. substrate = pem.readBase64fromText(self.extn_pem_text)
  25. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  26. self.assertFalse(rest)
  27. self.assertTrue(asn1Object.prettyPrint())
  28. self.assertEqual(substrate, der_encoder(asn1Object))
  29. self.assertEqual(rfc8649.id_ce_hashOfRootKey, asn1Object['extnID'])
  30. hashed_root_key, rest = der_decoder(
  31. asn1Object['extnValue'], rfc8649.HashedRootKey())
  32. self.assertFalse(rest)
  33. self.assertTrue(hashed_root_key.prettyPrint())
  34. self.assertEqual(asn1Object['extnValue'], der_encoder(hashed_root_key))
  35. self.assertEqual(
  36. rfc4055.id_sha512, hashed_root_key['hashAlg']['algorithm'])
  37. def testExtensionsMap(self):
  38. substrate = pem.readBase64fromText(self.extn_pem_text)
  39. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  40. self.assertFalse(rest)
  41. self.assertEqual(rfc8649.id_ce_hashOfRootKey, asn1Object['extnID'])
  42. self.assertIn(asn1Object['extnID'], rfc5280.certificateExtensionsMap)
  43. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  44. if __name__ == '__main__':
  45. result = unittest.TextTestRunner(verbosity=2).run(suite)
  46. sys.exit(not result.wasSuccessful())