test_rfc4387.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 rfc4387
  15. class CertificateTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MIIDLzCCArWgAwIBAgIJAKWzVCgbsG5JMAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  18. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  19. dXMgQ0EwHhcNMTkxMTIyMDI1MzAzWhcNMjAxMTIxMDI1MzAzWjBZMQswCQYDVQQG
  20. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoTB0V4
  21. YW1wbGUxGTAXBgNVBAMTEHJlcG8uZXhhbXBsZS5jb20wdjAQBgcqhkjOPQIBBgUr
  22. gQQAIgNiAAS/J1NNkqicN432Uwlw+Gu4pLvYpSr2W8zJvCOy61ncEzKNIs4cxqSc
  23. N0rl6K32tNCQGCsQFaBK4wZKXbHpUEPWrfYAWYebYDOhMlOE/agxH3nZRRnYv4O7
  24. pGrk/YZamGijggFhMIIBXTALBgNVHQ8EBAMCB4AwQgYJYIZIAYb4QgENBDUWM1Ro
  25. aXMgY2VydGlmaWNhdGUgY2Fubm90IGJlIHRydXN0ZWQgZm9yIGFueSBwdXJwb3Nl
  26. LjAdBgNVHQ4EFgQUWDRoN3XtN1n8ZH+bQuSAsr42gQwwHwYDVR0jBBgwFoAU8jXb
  27. NATapVXyvWkDmbBi7OIVCMEwgckGCCsGAQUFBwEBBIG8MIG5MCQGCCsGAQUFBzAB
  28. hhhodHRwOi8vb2NzcC5leGFtcGxlLmNvbS8wMgYIKwYBBQUHMAKGJmh0dHA6Ly9y
  29. ZXBvLmV4YW1wbGUuY29tL2NhaXNzdWVycy5odG1sMC4GCCsGAQUFBzAGhiJodHRw
  30. Oi8vcmVwby5leGFtcGxlLmNvbS9jZXJ0cy5odG1sMC0GCCsGAQUFBzAHhiFodHRw
  31. Oi8vcmVwby5leGFtcGxlLmNvbS9jcmxzLmh0bWwwCgYIKoZIzj0EAwMDaAAwZQIw
  32. C9Y1McQ+hSEZLtzLw1xzk3QSQX6NxalySoIIoNXpcDrGZJcjLRunBg8G9B0hqG69
  33. AjEAxtzj8BkMvhb5d9DTKDVg5pmjl9z7UtRK87/LJM+EW/9+PAzB2IT3T+BPHKb4
  34. kjBJ
  35. """
  36. def setUp(self):
  37. self.asn1Spec = rfc5280.Certificate()
  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. oid_list = [
  45. rfc4387.id_ad_http_certs,
  46. rfc4387.id_ad_http_crls,
  47. ]
  48. count = 0
  49. for extn in asn1Object['tbsCertificate']['extensions']:
  50. if extn['extnID'] == rfc5280.id_pe_authorityInfoAccess:
  51. extnValue, rest = der_decoder(
  52. extn['extnValue'],
  53. asn1Spec=rfc5280.AuthorityInfoAccessSyntax())
  54. self.assertFalse(rest)
  55. self.assertTrue(extnValue.prettyPrint())
  56. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  57. for ad in extnValue:
  58. if ad['accessMethod'] in oid_list:
  59. uri = ad['accessLocation']['uniformResourceIdentifier']
  60. self.assertIn('http://repo.example.com/c', uri)
  61. count += 1
  62. self.assertEqual(len(oid_list), count)
  63. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  64. if __name__ == '__main__':
  65. import sys
  66. result = unittest.TextTestRunner(verbosity=2).run(suite)
  67. sys.exit(not result.wasSuccessful())