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