test_rfc7585.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 rfc7585
  15. class NAIRealmCertTestCase(unittest.TestCase):
  16. cert_pem_text = """\
  17. MIIEZzCCA0+gAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBkjELMAkGA1UEBhMCRlIx
  18. DzANBgNVBAgMBlJhZGl1czESMBAGA1UEBwwJU29tZXdoZXJlMRQwEgYDVQQKDAtF
  19. eGFtcGxlIEluYzEgMB4GCSqGSIb3DQEJARYRYWRtaW5AZXhhbXBsZS5vcmcxJjAk
  20. BgNVBAMMHUV4YW1wbGUgQ2VydGlmaWNhdGUgQXV0aG9yaXR5MB4XDTE5MTExMTE4
  21. MDQyMVoXDTIwMDExMDE4MDQyMVowezELMAkGA1UEBhMCRlIxDzANBgNVBAgMBlJh
  22. ZGl1czEUMBIGA1UECgwLRXhhbXBsZSBJbmMxIzAhBgNVBAMMGkV4YW1wbGUgU2Vy
  23. dmVyIENlcnRpZmljYXRlMSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBleGFtcGxlLm9y
  24. ZzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAM9HqbuyWpsTMKo739Dm
  25. DwmQo2HUkNdQYbvsB+e7ILsw8fWa2qnsF1CoRr/1bcZqXUR1To/QbHse7xSMZH9t
  26. F7rdlDMc7QtgdwVfn8TiL3hCg5LSE8iaBzfJUjrts/V5WOByP1DwJVM7W3Va/5dN
  27. oOiceVeC7ThghMlwIx/wN5cy78a8fPYV2FvPR6e+U2HG35zaIv2PizYcliF/QmZG
  28. gnw4Q9dYC1Lw/ogVBZBALlv+/MuGheb/xIuL8lu1PFZ0YbW65WLD9Cx4wvytAke7
  29. tKlhL/Kd4OBSeOY3OYmpxbc1gEUmFoLTlZesY2NP9Jyl5mGsIHtPdvVkh/tSBy8o
  30. VLUCAwEAAaOB3TCB2jAJBgNVHRMEAjAAMAsGA1UdDwQEAwIF4DATBgNVHSUEDDAK
  31. BggrBgEFBQcDATA2BgNVHR8ELzAtMCugKaAnhiVodHRwOi8vd3d3LmV4YW1wbGUu
  32. Y29tL2V4YW1wbGVfY2EuY3JsMDcGCCsGAQUFBwEBBCswKTAnBggrBgEFBQcwAYYb
  33. aHR0cDovL3d3dy5leGFtcGxlLm9yZy9vY3NwMDoGA1UdEQQzMDGCEnJhZGl1cy5l
  34. eGFtcGxlLm9yZ6AbBggrBgEFBQcICKAPDA0qLmV4YW1wbGUuY29tMA0GCSqGSIb3
  35. DQEBCwUAA4IBAQBOhtH2Jpi0b0MZ8FBKTqDl44rIHL1rHG2mW/YYmRI4jZo8kFhA
  36. yWm/T8ZpdaotJgRqbQbeXvTXIg4/JNFheyLG4yLOzS1esdMAYDD5EN9/dXE++jND
  37. /wrfPU+QtTgzAjkgFDKuqO7gr1/vSizxLYTWLKBPRHhiQo7GGlEC6/CPb38x4mfQ
  38. 5Y9DsKCp6BEZu+LByCho/HMDzcIPCdtXRX7Fs8rtX4/zRpVIdm6D+vebuo6CwRKp
  39. mIljfssCvZjb9YIxSVDmA/6Lapqsfsfo922kb+MTXvPrq2ynPx8LrPDrxKc8maYc
  40. Jiw8B0yjkokwojxyRGftMT8uxNjWQVsMDbxl
  41. """
  42. def setUp(self):
  43. self.asn1Spec = rfc5280.Certificate()
  44. def testDerCodec(self):
  45. substrate = pem.readBase64fromText(self.cert_pem_text)
  46. asn1Object, rest = der_decoder(
  47. substrate, asn1Spec=self.asn1Spec)
  48. self.assertFalse(rest)
  49. self.assertTrue(asn1Object.prettyPrint())
  50. self.assertEqual(substrate, der_encoder(asn1Object))
  51. nai_realm_oid = rfc7585.id_on_naiRealm
  52. nai_realm_found = False
  53. for extn in asn1Object['tbsCertificate']['extensions']:
  54. if extn['extnID'] == rfc5280.id_ce_subjectAltName:
  55. extnValue, rest = der_decoder(
  56. extn['extnValue'], asn1Spec=rfc5280.SubjectAltName())
  57. self.assertFalse(rest)
  58. self.assertTrue(extnValue.prettyPrint())
  59. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  60. for gn in extnValue:
  61. if gn['otherName'].hasValue():
  62. self.assertEqual(
  63. nai_realm_oid, gn['otherName']['type-id'])
  64. onValue, rest = der_decoder(
  65. gn['otherName']['value'], asn1Spec=rfc7585.NAIRealm())
  66. self.assertFalse(rest)
  67. self.assertTrue(onValue.prettyPrint())
  68. self.assertEqual(
  69. gn['otherName']['value'], der_encoder(onValue))
  70. self.assertIn('example', onValue)
  71. nai_realm_found = True
  72. self.assertTrue(nai_realm_found)
  73. def testOpenTypes(self):
  74. substrate = pem.readBase64fromText(self.cert_pem_text)
  75. asn1Object, rest = der_decoder(
  76. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  77. self.assertFalse(rest)
  78. self.assertTrue(asn1Object.prettyPrint())
  79. self.assertEqual(substrate, der_encoder(asn1Object))
  80. nai_realm_oid = rfc7585.id_on_naiRealm
  81. nai_realm_found = False
  82. for extn in asn1Object['tbsCertificate']['extensions']:
  83. if extn['extnID'] == rfc5280.id_ce_subjectAltName:
  84. extnValue, rest = der_decoder(
  85. extn['extnValue'], asn1Spec=rfc5280.SubjectAltName(),
  86. decodeOpenTypes=True)
  87. self.assertFalse(rest)
  88. self.assertTrue(extnValue.prettyPrint())
  89. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  90. for gn in extnValue:
  91. if gn['otherName'].hasValue():
  92. self.assertEqual(
  93. nai_realm_oid, gn['otherName']['type-id'])
  94. self.assertIn('example', gn['otherName']['value'])
  95. nai_realm_found = True
  96. self.assertTrue(nai_realm_found)
  97. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  98. if __name__ == '__main__':
  99. result = unittest.TextTestRunner(verbosity=2).run(suite)
  100. sys.exit(not result.wasSuccessful())