test_rfc6010.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der.decoder import decode as der_decoder
  11. from pyasn1.codec.der.encoder import encode as der_encoder
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc5280
  14. from pyasn1_modules import rfc6010
  15. class UnconstrainedCCCExtensionTestCase(unittest.TestCase):
  16. unconstrained_pem_text = "MB0GCCsGAQUFBwESBBEwDzANBgsqhkiG9w0BCRABAA=="
  17. def setUp(self):
  18. self.asn1Spec = rfc5280.Extension()
  19. def testDerCodec(self):
  20. substrate = pem.readBase64fromText(self.unconstrained_pem_text)
  21. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  22. self.assertFalse(rest)
  23. self.assertTrue(asn1Object.prettyPrint())
  24. self.assertEqual(substrate, der_encoder(asn1Object))
  25. self.assertEqual(
  26. rfc6010.id_pe_cmsContentConstraints, asn1Object['extnID'])
  27. evalue, rest = der_decoder(
  28. asn1Object['extnValue'],
  29. asn1Spec=rfc6010.CMSContentConstraints())
  30. self.assertFalse(rest)
  31. self.assertTrue(evalue.prettyPrint())
  32. self.assertEqual(asn1Object['extnValue'], der_encoder(evalue))
  33. self.assertEqual(
  34. rfc6010.id_ct_anyContentType, evalue[0]['contentType'])
  35. class ConstrainedCCCExtensionTestCase(unittest.TestCase):
  36. constrained_pem_text = """\
  37. MIG7BggrBgEFBQcBEgSBrjCBqzA0BgsqhkiG9w0BCRABEDAlMCMGCyqGSIb3DQEJ
  38. EAwBMRQMElZpZ2lsIFNlY3VyaXR5IExMQzAwBgpghkgBZQIBAk4CMCIwIAYLKoZI
  39. hvcNAQkQDAsxEQwPa3RhLmV4YW1wbGUuY29tMDEGCyqGSIb3DQEJEAEZMCIwIAYL
  40. KoZIhvcNAQkQDAsxEQwPa3RhLmV4YW1wbGUuY29tMA4GCSqGSIb3DQEHAQoBAQ==
  41. """
  42. def setUp(self):
  43. self.asn1Spec = rfc5280.Extension()
  44. def testDerCodec(self):
  45. substrate = pem.readBase64fromText(self.constrained_pem_text)
  46. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  47. self.assertFalse(rest)
  48. self.assertTrue(asn1Object.prettyPrint())
  49. self.assertEqual(substrate, der_encoder(asn1Object))
  50. self.assertEqual(
  51. rfc6010.id_pe_cmsContentConstraints, asn1Object['extnID'])
  52. evalue, rest = der_decoder(
  53. asn1Object['extnValue'],
  54. asn1Spec=rfc6010.CMSContentConstraints())
  55. self.assertFalse(rest)
  56. self.assertTrue(evalue.prettyPrint())
  57. self.assertEqual(asn1Object['extnValue'], der_encoder(evalue))
  58. constraint_count = 0
  59. attribute_count = 0
  60. cannot_count = 0
  61. for ccc in evalue:
  62. constraint_count += 1
  63. if ccc['canSource'] == 1:
  64. cannot_count += 1
  65. if ccc['attrConstraints'].hasValue():
  66. for attr in ccc['attrConstraints']:
  67. attribute_count += 1
  68. self.assertEqual(4, constraint_count)
  69. self.assertEqual(3, attribute_count)
  70. self.assertEqual(1, cannot_count)
  71. def testExtensionsMap(self):
  72. substrate = pem.readBase64fromText(self.constrained_pem_text)
  73. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  74. self.assertIn(asn1Object['extnID'], rfc5280.certificateExtensionsMap)
  75. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  76. if __name__ == '__main__':
  77. unittest.TextTestRunner(verbosity=2).run(suite)