test_rfc7030.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 rfc5652
  15. from pyasn1_modules import rfc7030
  16. class CSRAttrsTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MEEGCSqGSIb3DQEJBzASBgcqhkjOPQIBMQcGBSuBBAAiMBYGCSqGSIb3DQEJDjEJ
  19. BgcrBgEBAQEWBggqhkjOPQQDAw==
  20. """
  21. the_oids = (
  22. univ.ObjectIdentifier('1.2.840.113549.1.9.7'),
  23. univ.ObjectIdentifier('1.2.840.10045.4.3.3')
  24. )
  25. the_attrTypes = (
  26. univ.ObjectIdentifier('1.2.840.10045.2.1'),
  27. univ.ObjectIdentifier('1.2.840.113549.1.9.14'),
  28. )
  29. the_attrVals = (
  30. '1.3.132.0.34',
  31. '1.3.6.1.1.1.1.22',
  32. )
  33. def setUp(self):
  34. self.asn1Spec = rfc7030.CsrAttrs()
  35. def testDerCodec(self):
  36. substrate = pem.readBase64fromText(self.pem_text)
  37. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  38. self.assertFalse(rest)
  39. self.assertTrue(asn1Object.prettyPrint())
  40. self.assertEqual(substrate, der_encoder(asn1Object))
  41. for attr_or_oid in asn1Object:
  42. if attr_or_oid.getName() == 'oid':
  43. self.assertIn(attr_or_oid['oid'], self.the_oids)
  44. if attr_or_oid.getName() == 'attribute':
  45. self.assertIn(
  46. attr_or_oid['attribute']['attrType'], self.the_attrTypes)
  47. def testOpenTypes(self):
  48. openTypesMap = rfc5652.cmsAttributesMap.copy()
  49. for at in self.the_attrTypes:
  50. openTypesMap.update({at: univ.ObjectIdentifier()})
  51. substrate = pem.readBase64fromText(self.pem_text)
  52. asn1Object, rest = der_decoder(
  53. substrate, asn1Spec=self.asn1Spec, openTypes=openTypesMap,
  54. decodeOpenTypes=True)
  55. self.assertFalse(rest)
  56. self.assertTrue(asn1Object.prettyPrint())
  57. self.assertEqual(substrate, der_encoder(asn1Object))
  58. for attr_or_oid in asn1Object:
  59. if attr_or_oid.getName() == 'attribute':
  60. valString = attr_or_oid['attribute']['attrValues'][0].prettyPrint()
  61. if attr_or_oid['attribute']['attrType'] == self.the_attrTypes[0]:
  62. self.assertEqual(self.the_attrVals[0], valString)
  63. if attr_or_oid['attribute']['attrType'] == self.the_attrTypes[1]:
  64. self.assertEqual(self.the_attrVals[1], valString)
  65. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  66. if __name__ == '__main__':
  67. unittest.TextTestRunner(verbosity=2).run(suite)