test_rfc5917.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 rfc5917
  15. class ClearanceSponsorTestCase(unittest.TestCase):
  16. cert_pem_text = """\
  17. MIID1DCCA1qgAwIBAgIUUc1IQGJpeYQ0XwOS2ZmVEb3aeZ0wCgYIKoZIzj0EAwMw
  18. ZjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlZBMRAwDgYDVQQHEwdIZXJuZG9uMRAw
  19. DgYDVQQKEwdFeGFtcGxlMQwwCgYDVQQLEwNQQ0ExGDAWBgNVBAMTD3BjYS5leGFt
  20. cGxlLmNvbTAeFw0xOTExMDUyMjIwNDZaFw0yMDExMDQyMjIwNDZaMIGSMQswCQYD
  21. VQQGEwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoT
  22. B0V4YW1wbGUxIjAgBgNVBAsTGUh1bWFuIFJlc291cmNlIERlcGFydG1lbnQxDTAL
  23. BgNVBAMTBEZyZWQxHzAdBgkqhkiG9w0BCQEWEGZyZWRAZXhhbXBsZS5jb20wdjAQ
  24. BgcqhkjOPQIBBgUrgQQAIgNiAAQObFslQ2EBP0xlDJ3sRnsNaqm/woQgKpBispSx
  25. XxK5bWUVpfnWsZnjLWhtDuPcu1BcBlM2g7gwL/aw8nUSIK3D8Ja9rTUQQXc3zxnk
  26. cl8+8znNXHMGByRjPUH87C+TOrqjggGaMIIBljAdBgNVHQ4EFgQU5m711OqFDNGR
  27. SWMOSzTXjpTLIFUwbwYDVR0jBGgwZoAUJuolDwsyICik11oKjf8t3L1/VGWhQ6RB
  28. MD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjER
  29. MA8GA1UECgwIQm9ndXMgQ0GCCQCls1QoG7BuRjAPBgNVHRMBAf8EBTADAQH/MAsG
  30. A1UdDwQEAwIBhjBCBglghkgBhvhCAQ0ENRYzVGhpcyBjZXJ0aWZpY2F0ZSBjYW5u
  31. b3QgYmUgdHJ1c3RlZCBmb3IgYW55IHB1cnBvc2UuMBUGA1UdIAQOMAwwCgYIKwYB
  32. BQUHDQIwCgYDVR02BAMCAQIwfwYDVR0JBHgwdjBJBgNVBDcxQjBABgsqhkiG9w0B
  33. CRAHAwMCBeAxLTArgAsqhkiG9w0BCRAHBIEcMBoMGEhVTUFOIFJFU09VUkNFUyBV
  34. U0UgT05MWTApBglghkgBZQIBBUQxHAwaSHVtYW4gUmVzb3VyY2VzIERlcGFydG1l
  35. bnQwCgYIKoZIzj0EAwMDaAAwZQIwVh/RypULFgPpAN0I7OvuMomRWnm/Hea3Hk8P
  36. tTRz2Zai8iYat7oeAmGVgMhSXy2jAjEAuJW4l/CFatBy4W/lZ7gS3weBdBa5WEDI
  37. FFMC7GjGtCeLtXYqWfBnRdK26dOaHLB2
  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. cs = rfc5917.DirectoryString()
  48. cs['utf8String'] = u'Human Resources Department'
  49. encoded_cs = der_encoder(cs)
  50. clearance_sponsor_found = False
  51. for extn in asn1Object['tbsCertificate']['extensions']:
  52. if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes:
  53. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  54. ev, rest = der_decoder(
  55. extn['extnValue'],
  56. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  57. self.assertFalse(rest)
  58. self.assertTrue(ev.prettyPrint())
  59. self.assertEqual(extn['extnValue'], der_encoder(ev))
  60. for attr in ev:
  61. if attr['type'] == rfc5917.id_clearanceSponsor:
  62. self.assertEqual(encoded_cs, attr['values'][0])
  63. clearance_sponsor_found = True
  64. self.assertTrue(clearance_sponsor_found)
  65. def testOpenTypes(self):
  66. substrate = pem.readBase64fromText(self.cert_pem_text)
  67. asn1Object, rest = der_decoder(
  68. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  69. self.assertFalse(rest)
  70. self.assertTrue(asn1Object.prettyPrint())
  71. self.assertEqual(substrate, der_encoder(asn1Object))
  72. clearance_sponsor_found = False
  73. for extn in asn1Object['tbsCertificate']['extensions']:
  74. if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes:
  75. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  76. ev, rest = der_decoder(
  77. extn['extnValue'],
  78. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  79. decodeOpenTypes=True)
  80. self.assertFalse(rest)
  81. self.assertTrue(ev.prettyPrint())
  82. self.assertEqual(extn['extnValue'], der_encoder(ev))
  83. for attr in ev:
  84. if attr['type'] == rfc5917.id_clearanceSponsor:
  85. hrd = u'Human Resources Department'
  86. self.assertEqual(hrd, attr['values'][0]['utf8String'])
  87. clearance_sponsor_found = True
  88. self.assertTrue(clearance_sponsor_found)
  89. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  90. if __name__ == '__main__':
  91. unittest.TextTestRunner(verbosity=2).run(suite)