test_rfc4476.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 rfc5280
  15. from pyasn1_modules import rfc5755
  16. from pyasn1_modules import rfc4476
  17. class AttributeCertificatePolicyTestCase(unittest.TestCase):
  18. pem_text = """\
  19. MIID7zCCA1gCAQEwgY+gUTBKpEgwRjEjMCEGA1UEAwwaQUNNRSBJbnRlcm1lZGlh
  20. dGUgRUNEU0EgQ0ExCzAJBgNVBAYTAkZJMRIwEAYDVQQKDAlBQ01FIEx0ZC4CAx7N
  21. WqE6pDgwNjETMBEGA1UEAwwKQUNNRSBFQ0RTQTELMAkGA1UEBhMCRkkxEjAQBgNV
  22. BAoMCUFDTUUgTHRkLqBWMFSkUjBQMQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkEx
  23. EDAOBgNVBAcMB0hlcm5kb24xIjAgBgNVBAoMGUJvZ3VzIEF0dHJpYnV0ZSBBdXRo
  24. b3RpdHkwDQYJKoZIhvcNAQELBQACBAu1MO4wIhgPMjAxOTEyMTUxMjAwMDBaGA8y
  25. MDE5MTIzMTEyMDAwMFowgfIwPAYIKwYBBQUHCgExMDAuhgt1cm46c2VydmljZaQV
  26. MBMxETAPBgNVBAMMCHVzZXJuYW1lBAhwYXNzd29yZDAyBggrBgEFBQcKAjEmMCSG
  27. C3VybjpzZXJ2aWNlpBUwEzERMA8GA1UEAwwIdXNlcm5hbWUwNQYIKwYBBQUHCgMx
  28. KTAnoBikFjAUMRIwEAYDVQQDDAlBQ01FIEx0ZC4wCwwJQUNNRSBMdGQuMCAGCCsG
  29. AQUFBwoEMRQwEjAQDAZncm91cDEMBmdyb3VwMjAlBgNVBEgxHjANoQuGCXVybjpy
  30. b2xlMTANoQuGCXVybjpyb2xlMjCCATkwHwYDVR0jBBgwFoAUgJCMhskAsEBzvklA
  31. X8yJBOXO500wCQYDVR04BAIFADA8BgNVHTcENTAzoAqGCHVybjp0ZXN0oBaCFEFD
  32. TUUtTHRkLmV4YW1wbGUuY29toA2GC3Vybjphbm90aGVyMIHMBggrBgEFBQcBDwSB
  33. vzCBvDCBuQYKKwYBBAGBrGAwCjCBqjBFBggrBgEFBQcCBBY5aHR0cHM6Ly93d3cu
  34. ZXhhbXBsZS5jb20vYXR0cmlidXRlLWNlcnRpZmljYXRlLXBvbGljeS5odG1sMGEG
  35. CCsGAQUFBwIFMFUwIwwZQm9ndXMgQXR0cmlidXRlIEF1dGhvcml0eTAGAgEKAgEU
  36. Gi5URVNUIGF0dHJpYnV0ZSBjZXJ0aWZpY2F0ZSBwb2xpY3kgZGlzcGxheSB0ZXh0
  37. MA0GCSqGSIb3DQEBCwUAA4GBACygfTs6TkPurZQTLufcE3B1H2707OXKsJlwRpuo
  38. dR2oJbunSHZ94jcJHs5dfbzFs6vNfVLlBiDBRieX4p+4JcQ2P44bkgyiUTJu7g1b
  39. 6C1liB3vO6yH5hOZicOAaKd+c/myuGb9uFRoaXNfc2lnbmF0dXJlX2lzX2ludmFs
  40. aWQh
  41. """
  42. def setUp(self):
  43. self.asn1Spec = rfc5755.AttributeCertificate()
  44. def testDerCodec(self):
  45. substrate = pem.readBase64fromText(self.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(1, asn1Object['acinfo']['version'])
  51. found_ac_policy_qualifier1 = False
  52. found_ac_policy_qualifier2 = False
  53. for extn in asn1Object['acinfo']['extensions']:
  54. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  55. if extn['extnID'] == rfc4476.id_pe_acPolicies:
  56. ev, rest = der_decoder(
  57. extn['extnValue'],
  58. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  59. self.assertFalse(rest)
  60. self.assertTrue(ev.prettyPrint())
  61. self.assertEqual(extn['extnValue'], der_encoder(ev))
  62. oid = univ.ObjectIdentifier((1, 3, 6, 1, 4, 1, 22112, 48, 10,))
  63. self.assertEqual(oid, ev[0]['policyIdentifier'])
  64. for pq in ev[0]['policyQualifiers']:
  65. self.assertIn(
  66. pq['policyQualifierId'], rfc5280.policyQualifierInfoMap)
  67. pqv, rest = der_decoder(
  68. pq['qualifier'],
  69. asn1Spec=rfc5280.policyQualifierInfoMap[
  70. pq['policyQualifierId']])
  71. self.assertFalse(rest)
  72. self.assertTrue(pqv.prettyPrint())
  73. self.assertEqual(pq['qualifier'], der_encoder(pqv))
  74. if pq['policyQualifierId'] == rfc4476.id_qt_acps:
  75. self.assertIn('example.com', pqv)
  76. found_ac_policy_qualifier1 = True
  77. if pq['policyQualifierId'] == rfc4476.id_qt_acunotice:
  78. self.assertIn(20, pqv[0]['noticeNumbers'])
  79. found_ac_policy_qualifier2 = True
  80. assert found_ac_policy_qualifier1
  81. assert found_ac_policy_qualifier2
  82. def testOpenTypes(self):
  83. substrate = pem.readBase64fromText(self.pem_text)
  84. asn1Object, rest = der_decoder(
  85. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  86. self.assertFalse(rest)
  87. self.assertTrue(asn1Object.prettyPrint())
  88. self.assertEqual(substrate, der_encoder(asn1Object))
  89. self.assertEqual(1, asn1Object['acinfo']['version'])
  90. found_ac_policy_qualifier1 = False
  91. found_ac_policy_qualifier2 = False
  92. for extn in asn1Object['acinfo']['extensions']:
  93. if extn['extnID'] == rfc4476.id_pe_acPolicies:
  94. ev, rest = der_decoder(
  95. extn['extnValue'],
  96. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  97. decodeOpenTypes=True)
  98. self.assertFalse(rest)
  99. self.assertTrue(ev.prettyPrint())
  100. self.assertEqual(extn['extnValue'], der_encoder(ev))
  101. oid = univ.ObjectIdentifier((1, 3, 6, 1, 4, 1, 22112, 48, 10,))
  102. self.assertEqual(oid, ev[0]['policyIdentifier'])
  103. for pq in ev[0]['policyQualifiers']:
  104. if pq['policyQualifierId'] == rfc4476.id_qt_acps:
  105. self.assertIn('example.com', pq['qualifier'])
  106. found_ac_policy_qualifier1 = True
  107. if pq['policyQualifierId'] == rfc4476.id_qt_acunotice:
  108. self.assertIn(20, pq['qualifier'][0]['noticeNumbers'])
  109. found_ac_policy_qualifier2 = True
  110. assert found_ac_policy_qualifier1
  111. assert found_ac_policy_qualifier2
  112. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  113. if __name__ == '__main__':
  114. result = unittest.TextTestRunner(verbosity=2).run(suite)
  115. sys.exit(not result.wasSuccessful())