test_rfc5913.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 rfc5913
  15. from pyasn1_modules import rfc5755
  16. from pyasn1_modules import rfc3114
  17. class ClearanceTestCase(unittest.TestCase):
  18. cert_pem_text = """\
  19. MIIDhzCCAw6gAwIBAgIJAKWzVCgbsG5GMAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  20. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  21. dXMgQ0EwHhcNMTkxMTAyMTg0MjE4WhcNMjAxMTAxMTg0MjE4WjBmMQswCQYDVQQG
  22. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoTB0V4
  23. YW1wbGUxDDAKBgNVBAsTA1BDQTEYMBYGA1UEAxMPcGNhLmV4YW1wbGUuY29tMHYw
  24. EAYHKoZIzj0CAQYFK4EEACIDYgAEPf5vbgAqbE5dn6wbiCx4sCCcn1BKSrHmCfiW
  25. C9QLSGVNGHifQwPt9odGXjRiQ7QwpZ2wRD6Z91v+fk85XXLE3kJQCQdPIHFUY5EM
  26. pvS7T6u6xrmwnlVpUURPTOxfc55Oo4IBrTCCAakwHQYDVR0OBBYEFCbqJQ8LMiAo
  27. pNdaCo3/Ldy9f1RlMG8GA1UdIwRoMGaAFPI12zQE2qVV8r1pA5mwYuziFQjBoUOk
  28. QTA/MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExEDAOBgNVBAcMB0hlcm5kb24x
  29. ETAPBgNVBAoMCEJvZ3VzIENBggkA6JHWBpFPzvIwDwYDVR0TAQH/BAUwAwEB/zAL
  30. BgNVHQ8EBAMCAYYwQgYJYIZIAYb4QgENBDUWM1RoaXMgY2VydGlmaWNhdGUgY2Fu
  31. bm90IGJlIHRydXN0ZWQgZm9yIGFueSBwdXJwb3NlLjAVBgNVHSAEDjAMMAoGCCsG
  32. AQUFBw0CMAoGA1UdNgQDAgECMIGRBggrBgEFBQcBFQSBhDCBgTBZBgsqhkiG9w0B
  33. CRAHAwMCBeAxRjBEgAsqhkiG9w0BCRAHBIE1MDMMF0xBVyBERVBBUlRNRU5UIFVT
  34. RSBPTkxZDBhIVU1BTiBSRVNPVVJDRVMgVVNFIE9OTFkwEQYLKoZIhvcNAQkQBwID
  35. AgTwMBEGCyqGSIb3DQEJEAcBAwIF4DAKBggqhkjOPQQDAwNnADBkAjAZSD+BVqzc
  36. 1l0fDoH3LwixjxvtddBHbJsM5yBek4U9b2yWL2KEmwV02fTgof3AjDECMCTsksmx
  37. 5f3i5DSYfe9Q1heJlEJLd1hgZmfvUYNnCU3WrdmYzyoNdNTbg7ZFMoxsXw==
  38. """
  39. def setUp(self):
  40. self.asn1Spec = rfc5280.Certificate()
  41. def testDerCodec(self):
  42. substrate = pem.readBase64fromText(self.cert_pem_text)
  43. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  44. self.assertFalse(rest)
  45. self.assertTrue(asn1Object.prettyPrint())
  46. self.assertEqual(substrate, der_encoder(asn1Object))
  47. cat_value_found = False
  48. for extn in asn1Object['tbsCertificate']['extensions']:
  49. if extn['extnID'] == rfc5913.id_pe_clearanceConstraints:
  50. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  51. ev, rest = der_decoder(
  52. extn['extnValue'],
  53. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  54. self.assertFalse(rest)
  55. self.assertTrue(ev.prettyPrint())
  56. self.assertEqual(extn['extnValue'], der_encoder(ev))
  57. for c in ev:
  58. if c['policyId'] == rfc3114.id_tsp_TEST_Whirlpool:
  59. for sc in c['securityCategories']:
  60. self.assertIn(sc['type'], rfc5755.securityCategoryMap)
  61. scv, rest = der_decoder(
  62. sc['value'],
  63. asn1Spec=rfc5755.securityCategoryMap[sc['type']])
  64. for cat in scv:
  65. self.assertIn('USE ONLY', cat)
  66. cat_value_found = True
  67. self.assertTrue(cat_value_found)
  68. def testOpenTypes(self):
  69. substrate = pem.readBase64fromText(self.cert_pem_text)
  70. asn1Object, rest = der_decoder(
  71. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  72. self.assertFalse(rest)
  73. self.assertTrue(asn1Object.prettyPrint())
  74. self.assertEqual(substrate, der_encoder(asn1Object))
  75. cat_value_found = False
  76. for extn in asn1Object['tbsCertificate']['extensions']:
  77. if extn['extnID'] == rfc5913.id_pe_clearanceConstraints:
  78. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  79. ev, rest = der_decoder(
  80. extn['extnValue'],
  81. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  82. decodeOpenTypes=True)
  83. self.assertFalse(rest)
  84. self.assertTrue(ev.prettyPrint())
  85. self.assertEqual(extn['extnValue'], der_encoder(ev))
  86. for c in ev:
  87. if c['policyId'] == rfc3114.id_tsp_TEST_Whirlpool:
  88. for sc in c['securityCategories']:
  89. self.assertIn(sc['type'], rfc5755.securityCategoryMap)
  90. for cat in sc['value']:
  91. self.assertIn('USE ONLY', cat)
  92. cat_value_found = True
  93. self.assertTrue(cat_value_found)
  94. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  95. if __name__ == '__main__':
  96. unittest.TextTestRunner(verbosity=2).run(suite)