test_rfc7906.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc2985
  15. from pyasn1_modules import rfc5652
  16. from pyasn1_modules import rfc5280
  17. from pyasn1_modules import rfc7906
  18. class AttributeSetTestCase(unittest.TestCase):
  19. attr_set_pem_text = """\
  20. MYIRmDAQBglghkgBZQIBDQcxA4IBATAQBglghkgBZQIBDQ0xAwoBUzAQBglghkgB
  21. ZQIBDQ4xAwoBAjAQBglghkgBZQIBDQ8xAwoBATARBglghkgBZQIBBUIxBAQCeQYw
  22. EgYJYIZIAWUCAQ0LMQUwAwoBATAVBglghkgBZQIBDQUxCDAGAgReAA//MBUGCyqG
  23. SIb3DQEJEAIuMQYCBF1qowYwGQYJYIZIAWUCAQVHMQwGCisGAQQBgaxgME0wGgYJ
  24. YIZIAWUCAQ0BMQ0wCwYJYIZIAWUDBAEtMBoGCWCGSAFlAgENDDENBgsqhkiG9w0B
  25. CRABGTAaBglghkgBZQIBDRUxDTALBglghkgBZQMEAS0wGwYJYIZIAWUCAQ0GMQ4w
  26. DAIEXQAAAAIEXwAP/zAdBgsqhkiG9w0BCRACKDEOMAwGCisGAQQBgaxgMDAwLQYJ
  27. YIZIAWUCAQVGMSAwHoYcaHR0cDovL3JlcG8uZXhhbXBsZS5jb20vcGtpLzAvBglg
  28. hkgBZQIBDQMxIjAgExFCb2d1cyBTaG9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LcYASKJGtWjOAIFB8sXrsiE7G1tC+IP+iOE
  107. dJCZ+UvJ9Enx7v6dtaU4uy1FzuWCar45BVpKVK2TNWT8E7XATkGBTIXGN76yJ5S0
  108. 9FdWp+hVkIkmyCJJujXzV6OBlDCBkTALBgNVHQ8EBAMCB4AwQgYJYIZIAYb4QgEN
  109. BDUWM1RoaXMgY2VydGlmaWNhdGUgY2Fubm90IGJlIHRydXN0ZWQgZm9yIGFueSBw
  110. dXJwb3NlLjAdBgNVHQ4EFgQUbZtc/QOvtbnVi/FknxpW4LWtTQ8wHwYDVR0jBBgw
  111. FoAU8jXbNATapVXyvWkDmbBi7OIVCMEwCgYIKoZIzj0EAwMDZwAwZAIwBniWpO11
  112. toMsV8fLBpBjA5YGQvd3TAcSw1lNbWpArL+hje1dzQ7pxslnkklv3CTxAjBuVebz
  113. 4mN0Qkew2NK/itwlmi7i+QxPs/MSZ7YFsyTA5Z4h2GbLW+zN3xNCC91vfpc=
  114. """
  115. def setUp(self):
  116. self.asn1Spec = rfc2985.AttributeSet()
  117. def testDerCodec(self):
  118. substrate = pem.readBase64fromText(self.attr_set_pem_text)
  119. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  120. self.assertFalse(rest)
  121. self.assertTrue(asn1Object.prettyPrint())
  122. self.assertEqual(substrate, der_encoder(asn1Object))
  123. for attr in asn1Object:
  124. self.assertIn(attr['type'], rfc5652.cmsAttributesMap)
  125. av, rest = der_decoder(
  126. attr['values'][0],
  127. asn1Spec=rfc5652.cmsAttributesMap[attr['type']])
  128. self.assertFalse(rest)
  129. self.assertTrue(av.prettyPrint())
  130. self.assertEqual(attr['values'][0], der_encoder(av))
  131. if attr['type'] == rfc7906.id_aa_KP_contentDecryptKeyID:
  132. self.assertEqual(univ.OctetString(hexValue='7906'), av)
  133. def testOpenTypes(self):
  134. openTypesMap = rfc5280.certificateAttributesMap.copy()
  135. openTypesMap.update(rfc5652.cmsAttributesMap)
  136. substrate = pem.readBase64fromText(self.attr_set_pem_text)
  137. asn1Object, rest = der_decoder(
  138. substrate, asn1Spec=self.asn1Spec, openTypes=openTypesMap,
  139. decodeOpenTypes=True)
  140. self.assertFalse(rest)
  141. self.assertTrue(asn1Object.prettyPrint())
  142. self.assertEqual(substrate, der_encoder(asn1Object))
  143. for attr in asn1Object:
  144. if attr['type'] == rfc7906.id_aa_KP_contentDecryptKeyID:
  145. self.assertEqual(
  146. univ.OctetString(hexValue='7906'), attr['values'][0])
  147. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  148. if __name__ == '__main__':
  149. result = unittest.TextTestRunner(verbosity=2).run(suite)
  150. sys.exit(not result.wasSuccessful())