test_rfc8209.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 rfc8209
  15. class CertificateTestCase(unittest.TestCase):
  16. cert_pem_text = """\
  17. MIIBiDCCAS+gAwIBAgIEAk3WfDAKBggqhkjOPQQDAjAaMRgwFgYDVQQDDA9ST1VU
  18. RVItMDAwMEZCRjAwHhcNMTcwMTAxMDUwMDAwWhcNMTgwNzAxMDUwMDAwWjAaMRgw
  19. FgYDVQQDDA9ST1VURVItMDAwMEZCRjAwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
  20. AARzkbq7kqDLO+EOWbGev/shTgSpHgy6GxOafTjZD3flWqBbjmlWeOD6FpBLVdnU
  21. 9cDfxYiV7lC8T3XSBaJb02/1o2MwYTALBgNVHQ8EBAMCB4AwHQYDVR0OBBYEFKtN
  22. kQ9VyucaIV7zyv46zEW17sFUMBMGA1UdJQQMMAoGCCsGAQUFBwMeMB4GCCsGAQUF
  23. BwEIAQH/BA8wDaAHMAUCAwD78KECBQAwCgYIKoZIzj0EAwIDRwAwRAIgB7e0al+k
  24. 8cxoNjkDpIPsfIAC0vYInUay7Cp75pKzb7ECIACRBUqh9bAYnSck6LQi/dEc8D2x
  25. OCRdZCk1KI3uDDgp
  26. """
  27. def setUp(self):
  28. self.asn1Spec = rfc5280.Certificate()
  29. def testDerCodec(self):
  30. substrate = pem.readBase64fromText(self.cert_pem_text)
  31. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  32. self.assertFalse(rest)
  33. self.assertTrue(asn1Object.prettyPrint())
  34. self.assertEqual(substrate, der_encoder(asn1Object))
  35. extn_list = []
  36. for extn in asn1Object['tbsCertificate']['extensions']:
  37. extn_list.append(extn['extnID'])
  38. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  39. extnValue, rest = der_decoder(
  40. extn['extnValue'],
  41. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  42. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  43. if extn['extnID'] == rfc5280.id_ce_extKeyUsage:
  44. self.assertIn(rfc8209.id_kp_bgpsec_router, extnValue)
  45. self.assertIn(rfc5280.id_ce_extKeyUsage, extn_list)
  46. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  47. if __name__ == '__main__':
  48. unittest.TextTestRunner(verbosity=2).run(suite)