test_rfc5755.py 8.5 KB

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