test_rfc5697.py 5.1 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.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc5280
  15. from pyasn1_modules import rfc5697
  16. class OtherCertTestCase(unittest.TestCase):
  17. cert_pem_text = """\
  18. MIIGUTCCBfegAwIBAgIUY8xt3l0B9nIPWSpjs0hDJUJZmCswCwYJYIZIAWUDBAMC
  19. MD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjER
  20. MA8GA1UEChMIQm9ndXMgQ0EwHhcNMTkxMjExMTczMzQ0WhcNMjAxMjEwMTczMzQ0
  21. WjBNMQswCQYDVQQGEwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24x
  22. EDAOBgNVBAoTB0V4YW1wbGUxDTALBgNVBAMTBEdhaWwwggNHMIICOgYHKoZIzjgE
  23. ATCCAi0CggEBAMj5CIXkPmfEDm3rrTqf/sIPh5XNWTT+U/+W74HbEXfi0NdafvNc
  24. WowncDznn4BZuotmuahJKBLFL0WCE28SAcJlhoOZ+gy6CMBV3LbupTEhPcWdc+qC
  25. wj1kL6WQwBfuzMlfKqXbGcO+CAP59iirw/LGcgmjLk/BpNAQ5oPtmD88DKAm4Ysz
  26. l3+n0F8ZhLhw33NEcEVNcVr+Q+ZZP/4ezAizvOK46QA5KnlXBQoC+MgTqxk+zhjw
  27. JRE5UnQDv8FbUF3GrehLDN0q+Pt76+jl+ikOnMzeXi+tz8d49LCogxh7oq6N2Ptt
  28. o9ksMkExNRJhW6JeVQ4PggOR4CI8BwYt7T0CIQD5VsG4AQIeMIDGmu8ek+FEKp8l
  29. utd6GBzrQwfDkgiGpQKCAQEAo2c3ze980XHSjTnsFAcDXb71KrQV5FadnRAzWxWO
  30. MrDDCVUq6JqaRKWAMRmk72Tl3V1c6IC3Y3mjorYH0HEi3EbYq5KxGXRaoK8NJAFh
  31. YKhHk5VAVyCvM1J9NNdlDyl0uYrxLLSwt+S7yrEL4qCijAzQ270h0cnBiYG06e5l
  32. XVola9Wec4KqFfqnDQGiDIYZSWvGqMGKbrMzkJMmYN/8ls54l3ATvSEt5ijeDJzk
  33. MkyMaTV77g/R9n43JqvyOdkizZCRKovvL+m+wRdilFcIMDXwSG1Pw9kmCa/NenjF
  34. 5swCfyF3P2TsO3QsppM7KWfLglj9j7sPM4MTiOfc+wPKqwOCAQUAAoIBACcxpFMg
  35. T2EEPRojEYDwIY4t9u6eP2scBrkrc3JJ6osTXHfkeluR9OvME620Hm01+EivnETI
  36. W5o+hCAdoic2h93kjx137QLAAL9ECoYgzm32SB796Nn630XVnd44gP1G3KbPZ8eD
  37. uC1GsSuxkmDR9PH0Tbx6XdnbTKW4ycHpKrrDLLeryZsghQfv4O63oaXgaJHwdQD3
  38. BwTZcUexZGstI7hFEdZrc7HWF3kmZdHjxuXYL/DP2T7akHyLc6ktepastZ6cGTZr
  39. GUJ52sgM50Swb2CtrJuGDvtnEcZjtEb+rJgFIWHDs3lelLT72GWX+Xs7jeJaSjx5
  40. +NK1qahR8hguww6jggHQMIIBzDAdBgNVHQ4EFgQU34Ol7JNqPoDCG/WE8toUQUiS
  41. tUQwegYDVR0jBHMwcYAUzUhlAYOypgdbBv4jgQzEc+TRtTihQ6RBMD8xCzAJBgNV
  42. BAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjERMA8GA1UEChMI
  43. Qm9ndXMgQ0GCFCVehe2QOuzvkY+pMECid/MyYVKJMA8GA1UdEwEB/wQFMAMBAf8w
  44. CwYDVR0PBAQDAgGGMEIGCWCGSAGG+EIBDQQ1FjNUaGlzIGNlcnRpZmljYXRlIGNh
  45. bm5vdCBiZSB0cnVzdGVkIGZvciBhbnkgcHVycG9zZS4wUwYDVR0RBEwwSqA2Bggr
  46. BgEFBQcIA6AqMCgMGzgyNjIwOC00MTcwMjgtNTQ4MTk1LTIxNTIzMwYJKwYBBAGB
  47. rGAwgRBnYWlsQGV4YW1wbGUuY29tMHgGCCsGAQUFBwETBGwwajBoBBT9+d0Ci+/R
  48. j5toRA+A7p+ECmGaWDBQMEOkQTA/MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkEx
  49. EDAOBgNVBAcMB0hlcm5kb24xETAPBgNVBAoMCEJvZ3VzIENBAgkApbNUKBuwbkcw
  50. CwYJYIZIAWUDBAMCA0cAMEQCIAyAog0z/KyROhb8Fl3Hyjcia/POnMq4yhPZFwlI
  51. hn1cAiAIfnI1FVrosL/94ZKfGW+xydYaelsPL+WBgqGvKuTMEg==
  52. """
  53. def setUp(self):
  54. self.asn1Spec = rfc5280.Certificate()
  55. def testDerCodec(self):
  56. substrate = pem.readBase64fromText(self.cert_pem_text)
  57. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  58. self.assertFalse(rest)
  59. self.assertTrue(asn1Object.prettyPrint())
  60. self.assertEqual(substrate, der_encoder(asn1Object))
  61. other_cert_found = False
  62. for extn in asn1Object['tbsCertificate']['extensions']:
  63. if extn['extnID'] == rfc5697.id_pe_otherCerts:
  64. extnValue, rest = der_decoder(
  65. extn['extnValue'],
  66. asn1Spec=rfc5697.OtherCertificates())
  67. self.assertFalse(rest)
  68. self.assertTrue(extnValue.prettyPrint())
  69. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  70. self.assertEqual(
  71. 11939979568329289287,
  72. extnValue[0]['issuerSerial']['serialNumber'])
  73. other_cert_found = True
  74. self.assertTrue(other_cert_found)
  75. def testOpenTypes(self):
  76. substrate = pem.readBase64fromText(self.cert_pem_text)
  77. asn1Object, rest = der_decoder(
  78. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  79. self.assertFalse(rest)
  80. self.assertTrue(asn1Object.prettyPrint())
  81. self.assertEqual(substrate, der_encoder(asn1Object))
  82. other_cert_found = False
  83. for extn in asn1Object['tbsCertificate']['extensions']:
  84. if extn['extnID'] == rfc5697.id_pe_otherCerts:
  85. self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
  86. extnValue, rest = der_decoder(
  87. extn['extnValue'],
  88. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  89. decodeOpenTypes=True)
  90. self.assertFalse(rest)
  91. self.assertTrue(extnValue.prettyPrint())
  92. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  93. self.assertEqual(
  94. 11939979568329289287,
  95. extnValue[0]['issuerSerial']['serialNumber'])
  96. other_cert_found = True
  97. self.assertTrue(other_cert_found)
  98. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  99. if __name__ == '__main__':
  100. result = unittest.TextTestRunner(verbosity=2).run(suite)
  101. sys.exit(not result.wasSuccessful())