test_rfc4985.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 rfc4985
  15. class XMPPCertificateTestCase(unittest.TestCase):
  16. xmpp_server_cert_pem_text = """\
  17. MIIC6DCCAm+gAwIBAgIJAKWzVCgbsG5DMAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  18. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  19. dXMgQ0EwHhcNMTkxMDI0MjMxNjA0WhcNMjAxMDIzMjMxNjA0WjBNMQswCQYDVQQG
  20. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xHzAdBgNVBAoTFkV4
  21. YW1wbGUgUHJvZHVjdHMsIEluYy4wdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQZzQlk
  22. 03nJRPF6+w1NxFELmQ5vJTjTRz3eu03CRtahK4Wnwd4GwbDe8NVHAEG2qTzBXFDu
  23. p6RZugsBdf9GcEZHG42rThYYOzIYzVFnI7tQgA+nTWSWZN6eoU/EXcknhgijggEn
  24. MIIBIzAdBgNVHQ4EFgQUkQpUMYcbUesEn5buI03POFnktJgwHwYDVR0jBBgwFoAU
  25. 8jXbNATapVXyvWkDmbBi7OIVCMEwCwYDVR0PBAQDAgeAMIGPBgNVHREEgYcwgYSg
  26. KQYIKwYBBQUHCAegHRYbX3htcHAtY2xpZW50LmltLmV4YW1wbGUuY29toCkGCCsG
  27. AQUFBwgHoB0WG194bXBwLXNlcnZlci5pbS5leGFtcGxlLmNvbaAcBggrBgEFBQcI
  28. BaAQDA5pbS5leGFtcGxlLmNvbYIOaW0uZXhhbXBsZS5jb20wQgYJYIZIAYb4QgEN
  29. BDUWM1RoaXMgY2VydGlmaWNhdGUgY2Fubm90IGJlIHRydXN0ZWQgZm9yIGFueSBw
  30. dXJwb3NlLjAKBggqhkjOPQQDAwNnADBkAjAEo4mhDGC6/R39HyNgzLseNAp36qBH
  31. yQJ/AWsBojN0av8akeVv9IuM45yqLKdiCzcCMDCjh1lFnCvurahwp5D1j9pAZMsg
  32. nOzhcMpnHs2U/eN0lHl/JNgnbftl6Dvnt59xdA==
  33. """
  34. def setUp(self):
  35. self.asn1Spec = rfc5280.Certificate()
  36. def testDerCodec(self):
  37. substrate = pem.readBase64fromText(self.xmpp_server_cert_pem_text)
  38. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  39. self.assertFalse(rest)
  40. self.assertTrue(asn1Object.prettyPrint())
  41. self.assertEqual(substrate, der_encoder(asn1Object))
  42. count = 0
  43. for extn in asn1Object['tbsCertificate']['extensions']:
  44. if extn['extnID'] == rfc5280.id_ce_subjectAltName:
  45. extnValue, rest = der_decoder(
  46. extn['extnValue'], asn1Spec=rfc5280.SubjectAltName())
  47. self.assertFalse(rest)
  48. self.assertTrue(extnValue.prettyPrint())
  49. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  50. for gn in extnValue:
  51. if gn['otherName'].hasValue():
  52. gn_on = gn['otherName']
  53. if gn_on['type-id'] == rfc4985.id_on_dnsSRV:
  54. self.assertIn(gn_on['type-id'], rfc5280.anotherNameMap)
  55. spec = rfc5280.anotherNameMap[gn['otherName']['type-id']]
  56. on, rest = der_decoder(gn_on['value'], asn1Spec=spec)
  57. self.assertFalse(rest)
  58. self.assertTrue(on.prettyPrint())
  59. self.assertEqual(gn_on['value'], der_encoder(on))
  60. self.assertIn('im.example.com', on)
  61. count += 1
  62. self.assertEqual(2, count)
  63. def testOpenTypes(self):
  64. substrate = pem.readBase64fromText(self.xmpp_server_cert_pem_text)
  65. asn1Object, rest = der_decoder(
  66. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  67. self.assertFalse(rest)
  68. self.assertTrue(asn1Object.prettyPrint())
  69. self.assertEqual(substrate, der_encoder(asn1Object))
  70. count = 0
  71. for extn in asn1Object['tbsCertificate']['extensions']:
  72. if extn['extnID'] == rfc5280.id_ce_subjectAltName:
  73. extnValue, rest = der_decoder(
  74. extn['extnValue'], asn1Spec=rfc5280.SubjectAltName(),
  75. decodeOpenTypes=True)
  76. self.assertFalse(rest)
  77. self.assertTrue(extnValue.prettyPrint())
  78. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  79. for gn in extnValue:
  80. if gn['otherName'].hasValue():
  81. if gn['otherName']['type-id'] == rfc4985.id_on_dnsSRV:
  82. self.assertIn('im.example.com', gn['otherName']['value'])
  83. count += 1
  84. self.assertEqual(2, count)
  85. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  86. if __name__ == '__main__':
  87. result = unittest.TextTestRunner(verbosity=2).run(suite)
  88. sys.exit(not result.wasSuccessful())