test_rfc8520.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 rfc8520
  15. class MUDCertTestCase(unittest.TestCase):
  16. mud_cert_pem_text = """\
  17. MIIFODCCAyCgAwIBAgICEEAwDQYJKoZIhvcNAQELBQAwZTELMAkGA1UEBhMCQ0gx
  18. DzANBgNVBAgMBlp1cmljaDERMA8GA1UEBwwIV2V0emlrb24xEDAOBgNVBAoMB0lt
  19. UmlnaHQxIDAeBgNVBAMMF0ltUmlnaHQgVGVzdCA4MDIuMUFSIENBMB4XDTE5MDUw
  20. MTE4MDMyMVoXDTE5MDUzMTE4MDMyMVowZzELMAkGA1UEBhMCQ0gxEzARBgNVBAgM
  21. ClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEg
  22. MB4GA1UEAwwXTGlnaHRidWxiMjAwMCwgU04jMjAyMDIwggEiMA0GCSqGSIb3DQEB
  23. AQUAA4IBDwAwggEKAoIBAQCzntv6tCdkZWPUx+CK9A9PCgKF8zGCJwdU4eIjo0oe
  24. A81i7iltOPnU416GJMEc2jGhlZPn2Rjjy8tPbyh1RVBfkgdq4UPWPnZPb+Gkq1c8
  25. X8zLRrMSWKqkSGOPENieDuQpzcrkMfj7dCPcxTcJ5Gluv1jEI7bxoZOZXjNxaFXi
  26. vsaZWFub7b+5zDLWpvmpKDaeCU+gad7rWpRE/Hjh3FX8paW8KE/hMF/au4xX2Qj/
  27. rDwHSxgs3n8FtuFUELotSgL3Acy3aISmJILBx6XrSs3nLruZzamulwWupSryHo3L
  28. U+GsOETiXwxiyrfOZo3aJNnWzlEvrYCQGyqd8Nd/XOENAgMBAAGjge8wgewwCQYD
  29. VR0TBAIwADBABggrBgEFBQcBGQQ0FjJodHRwczovL3d3dy5vZmNvdXJzZWltcmln
  30. aHQuY29tL0x1bWluYWlyZV8xNTAuanNvbjBdBggrBgEFBQcBHgRRME8xCzAJBgNV
  31. BAYTAkNIMSswKQYJKoZIhvcNAQkBFhxhc2NlcnRpYUBvZmNvdXJzZWltcmlnaHQu
  32. Y29tMRMwEQYDVQQDEwpFbGlvdCBMZWFyMB0GA1UdDgQWBBS00spi6cRFdqz95TQI
  33. 9AuPn5/DRjAfBgNVHSMEGDAWgBREKvrASIa7JJ41mQWDkJ06rXTCtTANBgkqhkiG
  34. 9w0BAQsFAAOCAgEAiS4OlazkDpgR4qhrq5Wpx6m3Bmkk5RkXnqey1yyhyfZlAGH7
  35. ewQiybkF3nN6at/TcNWMRfGBLhRrQn1h75KEXKlc18RDorj72/bvkbJLoBmA43Mv
  36. xMF0w4YX8pQwzb4hSt04p79P2RVVYM3ex/vdok0KkouhLTlxzY7vhv1T8WGTVQHJ
  37. k2EyswS2nFa/OtIkwruXqJj+lotdV2yPgFav5j9lkw5VbOztlfSKT7qQInVm+VBI
  38. /qddz/LOYrls1A7KHzWkTvOwmvQBqI4e9xLjc3r8K4pZyMd7EsmepYmLOU+pfINf
  39. /sEjliCluR65mKcKGiUa5J31pzbVpCr6FM/NGEjqpp6F+slyNC8YM/UlaJK1W9ZI
  40. W7JAhmfil5z1CtQILFSnUh4VneTVOaYg6+gXr169fXUDlMM4ECnuqWAE2PLhfhI8
  41. +lY8u18rFiX0bNSiUySgxU3asCC92xNmvJHuL4QwiYaGtTne36NMN7dH/32nMKl+
  42. G3XA8cX8yZIrIkmWLBSji8UwOXwVhYovmbhHjaUMTQommxYv/Cuqi5nJUJfh5YJr
  43. APeEK6fTYpPMiZ6U1++qzZDp78MRAq7UQbluJHh8ujPuK6kQmSLXmvK5yGpnJ+Cw
  44. izaUuU1EEwgOMELjeFL62Ssvq8X+x6hZFCLygI7GNeitlblNhCXhFFurqMs=
  45. """
  46. def setUp(self):
  47. self.asn1Spec = rfc5280.Certificate()
  48. def testDerCodec(self):
  49. substrate = pem.readBase64fromText(self.mud_cert_pem_text)
  50. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  51. self.assertFalse(rest)
  52. self.assertTrue(asn1Object.prettyPrint())
  53. self.assertEqual(substrate, der_encoder(asn1Object))
  54. extn_list = []
  55. for extn in asn1Object['tbsCertificate']['extensions']:
  56. extn_list.append(extn['extnID'])
  57. if extn['extnID'] == rfc8520.id_pe_mudsigner:
  58. mudsigner, rest = der_decoder(
  59. extn['extnValue'], rfc8520.MUDsignerSyntax())
  60. self.assertEqual(extn['extnValue'], der_encoder(mudsigner))
  61. c = rfc5280.X520countryName(value="CH")
  62. self.assertEqual(mudsigner[0][0][0]['value'], der_encoder(c))
  63. e = rfc5280.EmailAddress(value="ascertia@ofcourseimright.com")
  64. self.assertEqual(mudsigner[0][1][0]['value'], der_encoder(e))
  65. cn = rfc5280.X520CommonName()
  66. cn['printableString'] = "Eliot Lear"
  67. self.assertEqual(mudsigner[0][2][0]['value'], der_encoder(cn))
  68. if extn['extnID'] == rfc8520.id_pe_mud_url:
  69. mudurl, rest = der_decoder(
  70. extn['extnValue'], rfc8520.MUDURLSyntax())
  71. self.assertEqual(extn['extnValue'], der_encoder(mudurl))
  72. self.assertEqual(".json", mudurl[-5:])
  73. self.assertIn(rfc8520.id_pe_mudsigner, extn_list)
  74. self.assertIn(rfc8520.id_pe_mud_url, extn_list)
  75. def testExtensionsMap(self):
  76. substrate = pem.readBase64fromText(self.mud_cert_pem_text)
  77. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  78. self.assertFalse(rest)
  79. self.assertTrue(asn1Object.prettyPrint())
  80. self.assertEqual(substrate, der_encoder(asn1Object))
  81. for extn in asn1Object['tbsCertificate']['extensions']:
  82. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  83. extnValue, rest = der_decoder(
  84. extn['extnValue'],
  85. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  86. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  87. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  88. if __name__ == '__main__':
  89. result = unittest.TextTestRunner(verbosity=2).run(suite)
  90. sys.exit(not result.wasSuccessful())