test_rfc8226.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 import decoder as der_decoder
  11. from pyasn1.codec.der import encoder as der_encoder
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc5280
  14. from pyasn1_modules import rfc8226
  15. class JWTClaimConstraintsTestCase(unittest.TestCase):
  16. jwtcc_pem_text = ("MD2gBzAFFgNmb2+hMjAwMBkWA2ZvbzASDARmb28xDARmb28yDARmb2"
  17. "8zMBMWA2JhcjAMDARiYXIxDARiYXIy")
  18. def setUp(self):
  19. self.asn1Spec = rfc8226.JWTClaimConstraints()
  20. def testDerCodec(self):
  21. substrate = pem.readBase64fromText(self.jwtcc_pem_text)
  22. asn1Object, rest = der_decoder.decode(
  23. substrate, asn1Spec=self.asn1Spec)
  24. self.assertFalse(rest)
  25. self.assertTrue(asn1Object.prettyPrint())
  26. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  27. class TNAuthorizationListTestCase(unittest.TestCase):
  28. tnal_pem_text = ("MCugBxYFYm9ndXOhEjAQFgo1NzE1NTUxMjEyAgIDFKIMFgo3MDM1NTU"
  29. "xMjEy")
  30. def setUp(self):
  31. self.asn1Spec = rfc8226.TNAuthorizationList()
  32. def testDerCodec(self):
  33. substrate = pem.readBase64fromText(self.tnal_pem_text)
  34. asn1Object, rest = der_decoder.decode(
  35. substrate, asn1Spec=self.asn1Spec)
  36. self.assertFalse(rest)
  37. self.assertTrue(asn1Object.prettyPrint())
  38. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  39. class CertificateOpenTypesTestCase(unittest.TestCase):
  40. cert_pem_text = """\
  41. MIICkTCCAhegAwIBAgIJAKWzVCgbsG4+MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  42. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  43. dXMgQ0EwHhcNMTkwNzE4MTUwNzQ5WhcNMjAwNzE3MTUwNzQ5WjBxMQswCQYDVQQG
  44. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xKDAmBgNVBAoTH0Zh
  45. a2UgVGVsZXBob25lIFNlcnZpY2UgUHJvdmlkZXIxGTAXBgNVBAMTEGZha2UuZXhh
  46. bXBsZS5jb20wdjAQBgcqhkjOPQIBBgUrgQQAIgNiAARLyLhnsvrS9WBY29tmN2LI
  47. CF/wuX4ohhUy3sxO0ynCplHHojpDg+tghGzusf0aLtMDu1II915O8YK5XVL+KZJD
  48. C82jybxWIKjjzX2qc5/O06joUttdEDzkTaD0kgbcXl6jgawwgakwCwYDVR0PBAQD
  49. AgeAMEIGCWCGSAGG+EIBDQQ1FjNUaGlzIGNlcnRpZmljYXRlIGNhbm5vdCBiZSB0
  50. cnVzdGVkIGZvciBhbnkgcHVycG9zZS4wHQYDVR0OBBYEFHOI3GpDt9dWsTAZxhcj
  51. 96uyL2aIMB8GA1UdIwQYMBaAFPI12zQE2qVV8r1pA5mwYuziFQjBMBYGCCsGAQUF
  52. BwEaBAowCKAGFgRmYWtlMAoGCCqGSM49BAMDA2gAMGUCMQCy+qFhT7X1i18jcyIa
  53. Jkgz/tumrPsaBA2RihkooTEr4GbqC650Z4Cwt7+x2xZq37sCMFSM6fRueLyV5StG
  54. yEFWA6G95b/HbtPMTjLpPKtrOjhofc4LyVCDYhFhKzpvHh1qeA==
  55. """
  56. def setUp(self):
  57. self.asn1Spec = rfc5280.Certificate()
  58. def testDerCodec(self):
  59. substrate = pem.readBase64fromText(self.cert_pem_text)
  60. asn1Object, rest = der_decoder.decode(
  61. substrate, asn1Spec=self.asn1Spec)
  62. self.assertFalse(rest)
  63. self.assertTrue(asn1Object.prettyPrint())
  64. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  65. extn_list = []
  66. for extn in asn1Object['tbsCertificate']['extensions']:
  67. extn_list.append(extn['extnID'])
  68. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  69. extnValue, rest = der_decoder.decode(
  70. extn['extnValue'],
  71. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  72. self.assertEqual(
  73. extn['extnValue'], der_encoder.encode(extnValue))
  74. if extn['extnID'] == rfc8226.id_pe_TNAuthList:
  75. self.assertEqual('fake', extnValue[0]['spc'])
  76. self.assertIn(rfc8226.id_pe_TNAuthList, extn_list)
  77. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  78. if __name__ == '__main__':
  79. result = unittest.TextTestRunner(verbosity=2).run(suite)
  80. sys.exit(not result.wasSuccessful())