test_rfc3281.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 rfc3281
  14. class AttributeCertificateTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIDBTCCAm4CAQEwgY+gUTBKpEgwRjEjMCEGA1UEAwwaQUNNRSBJbnRlcm1lZGlh
  17. dGUgRUNEU0EgQ0ExCzAJBgNVBAYTAkZJMRIwEAYDVQQKDAlBQ01FIEx0ZC4CAx7N
  18. WqE6pDgwNjETMBEGA1UEAwwKQUNNRSBFQ0RTQTELMAkGA1UEBhMCRkkxEjAQBgNV
  19. BAoMCUFDTUUgTHRkLqA9MDukOTA3MRQwEgYDVQQDDAtleGFtcGxlLmNvbTELMAkG
  20. A1UEBhMCRkkxEjAQBgNVBAoMCUFDTUUgTHRkLjANBgkqhkiG9w0BAQsFAAIEC63K
  21. /jAiGA8yMDE2MDEwMTEyMDAwMFoYDzIwMTYwMzAxMTIwMDAwWjCB8jA8BggrBgEF
  22. BQcKATEwMC6GC3VybjpzZXJ2aWNlpBUwEzERMA8GA1UEAwwIdXNlcm5hbWUECHBh
  23. c3N3b3JkMDIGCCsGAQUFBwoCMSYwJIYLdXJuOnNlcnZpY2WkFTATMREwDwYDVQQD
  24. DAh1c2VybmFtZTA1BggrBgEFBQcKAzEpMCegGKQWMBQxEjAQBgNVBAMMCUFDTUUg
  25. THRkLjALDAlBQ01FIEx0ZC4wIAYIKwYBBQUHCgQxFDASMBAMBmdyb3VwMQwGZ3Jv
  26. dXAyMCUGA1UESDEeMA2hC4YJdXJuOnJvbGUxMA2hC4YJdXJuOnJvbGUyMGowHwYD
  27. VR0jBBgwFoAUgJCMhskAsEBzvklAX8yJBOXO500wCQYDVR04BAIFADA8BgNVHTcB
  28. Af8EMjAwMB2gCoYIdXJuOnRlc3SgD4INKi5leGFtcGxlLmNvbTAPoA2GC3Vybjph
  29. bm90aGVyMA0GCSqGSIb3DQEBCwUAA4GBACygfTs6TkPurZQTLufcE3B1H2707OXK
  30. sJlwRpuodR2oJbunSHZ94jcJHs5dfbzFs6vNfVLlBiDBRieX4p+4JcQ2P44bkgyi
  31. UTJu7g1b6C1liB3vO6yH5hOZicOAaKd+c/myuGb9uJ4n6y2oLNxnk/fDzpuZUe2h
  32. Q4eikPk4LQey
  33. """
  34. def setUp(self):
  35. self.asn1Spec = rfc3281.AttributeCertificate()
  36. def testDerCodec(self):
  37. substrate = pem.readBase64fromText(self.pem_text)
  38. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  39. self.assertFalse(rest)
  40. self.assertTrue(asn1Object.prettyPrint())
  41. self.assertEqual(substrate, der_encoder(asn1Object))
  42. self.assertEqual(1, asn1Object['acinfo']['version'])
  43. attributeMap = {
  44. rfc3281.id_at_role: rfc3281.RoleSyntax(),
  45. rfc3281.id_aca_authenticationInfo: rfc3281.SvceAuthInfo(),
  46. rfc3281.id_aca_accessIdentity: rfc3281.SvceAuthInfo(),
  47. rfc3281.id_aca_chargingIdentity: rfc3281.IetfAttrSyntax(),
  48. rfc3281.id_aca_group: rfc3281.IetfAttrSyntax(),
  49. }
  50. count = 0
  51. for attr in asn1Object['acinfo']['attributes']:
  52. self.assertIn(attr['type'], attributeMap)
  53. av, rest = der_decoder(
  54. attr['values'][0], asn1Spec=attributeMap[attr['type']])
  55. self.assertFalse(rest)
  56. self.assertTrue(av.prettyPrint())
  57. self.assertEqual(attr['values'][0], der_encoder(av))
  58. count += 1
  59. self.assertEqual(5, count)
  60. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  61. if __name__ == '__main__':
  62. result = unittest.TextTestRunner(verbosity=2).run(suite)
  63. sys.exit(not result.wasSuccessful())