test_rfc5755.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 rfc5755
  15. from pyasn1_modules import rfc3114
  16. class AttributeCertificateTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIIDBTCCAm4CAQEwgY+gUTBKpEgwRjEjMCEGA1UEAwwaQUNNRSBJbnRlcm1lZGlh
  19. dGUgRUNEU0EgQ0ExCzAJBgNVBAYTAkZJMRIwEAYDVQQKDAlBQ01FIEx0ZC4CAx7N
  20. WqE6pDgwNjETMBEGA1UEAwwKQUNNRSBFQ0RTQTELMAkGA1UEBhMCRkkxEjAQBgNV
  21. BAoMCUFDTUUgTHRkLqA9MDukOTA3MRQwEgYDVQQDDAtleGFtcGxlLmNvbTELMAkG
  22. A1UEBhMCRkkxEjAQBgNVBAoMCUFDTUUgTHRkLjANBgkqhkiG9w0BAQsFAAIEC63K
  23. /jAiGA8yMDE2MDEwMTEyMDAwMFoYDzIwMTYwMzAxMTIwMDAwWjCB8jA8BggrBgEF
  24. BQcKATEwMC6GC3VybjpzZXJ2aWNlpBUwEzERMA8GA1UEAwwIdXNlcm5hbWUECHBh
  25. c3N3b3JkMDIGCCsGAQUFBwoCMSYwJIYLdXJuOnNlcnZpY2WkFTATMREwDwYDVQQD
  26. DAh1c2VybmFtZTA1BggrBgEFBQcKAzEpMCegGKQWMBQxEjAQBgNVBAMMCUFDTUUg
  27. THRkLjALDAlBQ01FIEx0ZC4wIAYIKwYBBQUHCgQxFDASMBAMBmdyb3VwMQwGZ3Jv
  28. dXAyMCUGA1UESDEeMA2hC4YJdXJuOnJvbGUxMA2hC4YJdXJuOnJvbGUyMGowHwYD
  29. VR0jBBgwFoAUgJCMhskAsEBzvklAX8yJBOXO500wCQYDVR04BAIFADA8BgNVHTcB
  30. Af8EMjAwMB2gCoYIdXJuOnRlc3SgD4INKi5leGFtcGxlLmNvbTAPoA2GC3Vybjph
  31. bm90aGVyMA0GCSqGSIb3DQEBCwUAA4GBACygfTs6TkPurZQTLufcE3B1H2707OXK
  32. sJlwRpuodR2oJbunSHZ94jcJHs5dfbzFs6vNfVLlBiDBRieX4p+4JcQ2P44bkgyi
  33. UTJu7g1b6C1liB3vO6yH5hOZicOAaKd+c/myuGb9uJ4n6y2oLNxnk/fDzpuZUe2h
  34. Q4eikPk4LQey
  35. """
  36. def setUp(self):
  37. self.asn1Spec = rfc5755.AttributeCertificate()
  38. def testDerCodec(self):
  39. substrate = pem.readBase64fromText(self.pem_text)
  40. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  41. self.assertFalse(rest)
  42. self.assertTrue(asn1Object.prettyPrint())
  43. self.assertEqual(substrate, der_encoder(asn1Object))
  44. self.assertEqual(1, asn1Object['acinfo']['version'])
  45. count = 0
  46. for attr in asn1Object['acinfo']['attributes']:
  47. self.assertIn(attr['type'], rfc5280.certificateAttributesMap)
  48. av, rest = der_decoder(
  49. attr['values'][0],
  50. asn1Spec=rfc5280.certificateAttributesMap[attr['type']])
  51. self.assertFalse(rest)
  52. self.assertTrue(av.prettyPrint())
  53. self.assertEqual(attr['values'][0], der_encoder(av))
  54. count += 1
  55. self.assertEqual(5, count)
  56. def testOpenTypes(self):
  57. substrate = pem.readBase64fromText(self.pem_text)
  58. asn1Object, rest = der_decoder(substrate,
  59. asn1Spec=self.asn1Spec,
  60. decodeOpenTypes=True)
  61. self.assertFalse(rest)
  62. self.assertTrue(asn1Object.prettyPrint())
  63. self.assertEqual(substrate, der_encoder(asn1Object))
  64. self.assertEqual(1, asn1Object['acinfo']['version'])
  65. count = 0
  66. for attr in asn1Object['acinfo']['attributes']:
  67. self.assertIn(attr['type'], rfc5280.certificateAttributesMap)
  68. count += 1
  69. if attr['type'] == rfc5755.id_aca_authenticationInfo:
  70. self.assertEqual(
  71. b'password', attr['values'][0]['authInfo'])
  72. self.assertEqual(5, count)
  73. class CertificateWithClearanceTestCase(unittest.TestCase):
  74. cert_pem_text = """\
  75. MIID1DCCA1qgAwIBAgIUUc1IQGJpeYQ0XwOS2ZmVEb3aeZ0wCgYIKoZIzj0EAwMw
  76. ZjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlZBMRAwDgYDVQQHEwdIZXJuZG9uMRAw
  77. DgYDVQQKEwdFeGFtcGxlMQwwCgYDVQQLEwNQQ0ExGDAWBgNVBAMTD3BjYS5leGFt
  78. cGxlLmNvbTAeFw0xOTExMDUyMjIwNDZaFw0yMDExMDQyMjIwNDZaMIGSMQswCQYD
  79. VQQGEwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoT
  80. B0V4YW1wbGUxIjAgBgNVBAsTGUh1bWFuIFJlc291cmNlIERlcGFydG1lbnQxDTAL
  81. BgNVBAMTBEZyZWQxHzAdBgkqhkiG9w0BCQEWEGZyZWRAZXhhbXBsZS5jb20wdjAQ
  82. BgcqhkjOPQIBBgUrgQQAIgNiAAQObFslQ2EBP0xlDJ3sRnsNaqm/woQgKpBispSx
  83. XxK5bWUVpfnWsZnjLWhtDuPcu1BcBlM2g7gwL/aw8nUSIK3D8Ja9rTUQQXc3zxnk
  84. cl8+8znNXHMGByRjPUH87C+TOrqjggGaMIIBljAdBgNVHQ4EFgQU5m711OqFDNGR
  85. SWMOSzTXjpTLIFUwbwYDVR0jBGgwZoAUJuolDwsyICik11oKjf8t3L1/VGWhQ6RB
  86. MD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjER
  87. MA8GA1UECgwIQm9ndXMgQ0GCCQCls1QoG7BuRjAPBgNVHRMBAf8EBTADAQH/MAsG
  88. A1UdDwQEAwIBhjBCBglghkgBhvhCAQ0ENRYzVGhpcyBjZXJ0aWZpY2F0ZSBjYW5u
  89. b3QgYmUgdHJ1c3RlZCBmb3IgYW55IHB1cnBvc2UuMBUGA1UdIAQOMAwwCgYIKwYB
  90. BQUHDQIwCgYDVR02BAMCAQIwfwYDVR0JBHgwdjBJBgNVBDcxQjBABgsqhkiG9w0B
  91. CRAHAwMCBeAxLTArgAsqhkiG9w0BCRAHBIEcMBoMGEhVTUFOIFJFU09VUkNFUyBV
  92. U0UgT05MWTApBglghkgBZQIBBUQxHAwaSHVtYW4gUmVzb3VyY2VzIERlcGFydG1l
  93. bnQwCgYIKoZIzj0EAwMDaAAwZQIwVh/RypULFgPpAN0I7OvuMomRWnm/Hea3Hk8P
  94. tTRz2Zai8iYat7oeAmGVgMhSXy2jAjEAuJW4l/CFatBy4W/lZ7gS3weBdBa5WEDI
  95. FFMC7GjGtCeLtXYqWfBnRdK26dOaHLB2
  96. """
  97. def setUp(self):
  98. self.asn1Spec = rfc5280.Certificate()
  99. def testDerCodec(self):
  100. substrate = pem.readBase64fromText(self.cert_pem_text)
  101. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  102. self.assertFalse(rest)
  103. self.assertTrue(asn1Object.prettyPrint())
  104. self.assertEqual(substrate, der_encoder(asn1Object))
  105. clearance_found = False
  106. for extn in asn1Object['tbsCertificate']['extensions']:
  107. if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes:
  108. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  109. ev, rest = der_decoder(
  110. extn['extnValue'],
  111. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  112. self.assertFalse(rest)
  113. self.assertTrue(ev.prettyPrint())
  114. self.assertEqual(extn['extnValue'], der_encoder(ev))
  115. for attr in ev:
  116. if attr['type'] == rfc5755.id_at_clearance:
  117. self.assertIn(attr['type'], rfc5280.certificateAttributesMap)
  118. av, rest = der_decoder(
  119. attr['values'][0],
  120. asn1Spec=rfc5280.certificateAttributesMap[attr['type']])
  121. self.assertEqual(rfc3114.id_tsp_TEST_Whirlpool, av['policyId'])
  122. for cat in av['securityCategories']:
  123. self.assertEqual(
  124. rfc3114.id_tsp_TEST_Whirlpool_Categories, cat['type'])
  125. self.assertIn(
  126. cat['type'], rfc5755.securityCategoryMap)
  127. catv, rest = der_decoder(
  128. cat['value'],
  129. asn1Spec=rfc5755.securityCategoryMap[cat['type']])
  130. self.assertIn('USE ONLY', catv[0])
  131. clearance_found = True
  132. self.assertTrue(clearance_found)
  133. def testOpenTypes(self):
  134. substrate = pem.readBase64fromText(self.cert_pem_text)
  135. asn1Object, rest = der_decoder(
  136. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  137. self.assertFalse(rest)
  138. self.assertTrue(asn1Object.prettyPrint())
  139. self.assertEqual(substrate, der_encoder(asn1Object))
  140. clearance_found = False
  141. for extn in asn1Object['tbsCertificate']['extensions']:
  142. if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes:
  143. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  144. ev, rest = der_decoder(
  145. extn['extnValue'],
  146. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  147. decodeOpenTypes=True)
  148. self.assertFalse(rest)
  149. self.assertTrue(ev.prettyPrint())
  150. self.assertEqual(extn['extnValue'], der_encoder(ev))
  151. for attr in ev:
  152. if attr['type'] == rfc5755.id_at_clearance:
  153. spid = rfc3114.id_tsp_TEST_Whirlpool
  154. catid = rfc3114.id_tsp_TEST_Whirlpool_Categories
  155. self.assertEqual(spid, attr['values'][0]['policyId'])
  156. for cat in attr['values'][0]['securityCategories']:
  157. self.assertEqual(catid, cat['type'])
  158. self.assertIn( u'USE ONLY', cat['value'][0])
  159. clearance_found = True
  160. self.assertTrue(clearance_found)
  161. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  162. if __name__ == '__main__':
  163. result = unittest.TextTestRunner(verbosity=2).run(suite)
  164. sys.exit(not result.wasSuccessful())