test_rfc5940.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, Vigil Security, LLC
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc2560
  13. from pyasn1_modules import rfc5940
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc5280
  16. class CRLandOCSPResponseTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIIHWQYJKoZIhvcNAQcCoIIHSjCCB0YCAQExDTALBglghkgBZQMEAgEwUwYJKoZI
  19. hvcNAQcBoEYERENvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpXYXRzb24sIGNv
  20. bWUgaGVyZSAtIEkgd2FudCB0byBzZWUgeW91Lg0KoIIBaDCCAWQwggEKoAMCAQIC
  21. CQClWUKCJkwnGTAKBggqhkjOPQQDAjAkMRQwEgYDVQQKDAtleGFtcGxlLm9yZzEM
  22. MAoGA1UEAwwDQm9iMB4XDTE3MTIyMDIzMDc0OVoXDTE4MTIyMDIzMDc0OVowJDEU
  23. MBIGA1UECgwLZXhhbXBsZS5vcmcxDDAKBgNVBAMMA0JvYjBZMBMGByqGSM49AgEG
  24. CCqGSM49AwEHA0IABIZP//xT8ah2ymmxfidIegeccVKuGxN+OTuvGq69EnQ8fUFD
  25. ov2KNw8Cup0DtzAfHaZOMFWUu2+Vy3H6SLbQo4OjJTAjMCEGA1UdEQEB/wQXMBWG
  26. E3NpcDpib2JAZXhhbXBsZS5vcmcwCgYIKoZIzj0EAwIDSAAwRQIhALIkjJJAKCI4
  27. nsklf2TM/RBvuguWwRkHMD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iEAujFVH+NvuTLYa8RW3pvWSUwZfjOW5H5171JI+/50BjcCIQDhwige
  44. wl+ts6TIvhU+CFoOipQBNKyKXKh7ngJkUtpZ86CCAVIwggFOMIIBSjCB8aADAgEC
  45. AgEBMAoGCCqGSM49BAMCMB4xHDAJBgNVBAYTAlJVMA8GA1UEAx4IAFQAZQBzAHQw
  46. HhcNMTkwMjAxMDUwMDAwWhcNMjIwMjAxMDUwMDAwWjAeMRwwCQYDVQQGEwJSVTAP
  47. BgNVBAMeCABUAGUAcwB0MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEM0jxEYgg
  48. RxC/r87uV/h6iZ8BAdHT/6fxRuzG0PRMIlFBy38skFUXJJulKV9JW16YJqOkVsqv
  49. xwMM61z7p1vQ/qMgMB4wDwYDVR0TBAgwBgEB/wIBAzALBgNVHQ8EBAMCAAYwCgYI
  50. KoZIzj0EAwIDSAAwRQIhAIdpCt5g89ofSADXmBD3KXQGnTghwbAMeWrKXqTGww+x
  51. AiAl8NQgfUk4xMymZ3VtCLJ2MdczDps4Zh2KPOqAR5fZAjGCAQcwggEDAgEBMDEw
  52. JDEUMBIGA1UECgwLZXhhbXBsZS5vcmcxDDAKBgNVBAMMA0JvYgIJAKVZQoImTCcZ
  53. MAsGCWCGSAFlAwQCAaBpMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZI
  54. hvcNAQkFMQ8XDTE5MDEyNDIzNTI1NlowLwYJKoZIhvcNAQkEMSIEIO93j8lA1ebc
  55. JXb0elmbMSYZWp8aInra81+iLAUNjRlaMAoGCCqGSM49BAMCBEcwRQIhAPeI7URq
  56. tw//LB/6TAN0/Qh3/WHukXwxRbOJpnYVx0b6AiB3lK3FfwBhx4S5YSPMblS7goJl
  57. ttTMEpl2prH8bbwo1g==
  58. """
  59. def setUp(self):
  60. self.asn1Spec = rfc5652.ContentInfo()
  61. def testDerCodec(self):
  62. substrate = pem.readBase64fromText(self.pem_text)
  63. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  64. self.assertFalse(rest)
  65. self.assertTrue(asn1Object.prettyPrint())
  66. self.assertEqual(substrate, der_encoder(asn1Object))
  67. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  68. sd, rest = der_decoder(
  69. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  70. self.assertTrue(sd.prettyPrint())
  71. self.assertEqual(
  72. rfc5652.id_data, sd['encapContentInfo']['eContentType'])
  73. self.assertTrue(sd['encapContentInfo']['eContent'])
  74. v2 = rfc5280.Version(value='v2')
  75. self.assertEqual(v2, sd['crls'][0]['crl']['tbsCertList']['version'])
  76. ocspr_oid = rfc5940.id_ri_ocsp_response
  77. self.assertEqual(ocspr_oid, sd['crls'][1]['other']['otherRevInfoFormat'])
  78. ocspr, rest = der_decoder(
  79. sd['crls'][1]['other']['otherRevInfo'],
  80. asn1Spec=rfc5940.OCSPResponse())
  81. self.assertTrue(ocspr.prettyPrint())
  82. success = rfc2560.OCSPResponseStatus(value='successful')
  83. self.assertEqual(success, ocspr['responseStatus'])
  84. def testOpenTypes(self):
  85. substrate = pem.readBase64fromText(self.pem_text)
  86. asn1Object, rest = der_decoder(
  87. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  88. self.assertFalse(rest)
  89. self.assertTrue(asn1Object.prettyPrint())
  90. self.assertEqual(substrate, der_encoder(asn1Object))
  91. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  92. sd_eci = asn1Object['content']['encapContentInfo']
  93. self.assertEqual(rfc5652.id_data, sd_eci['eContentType'])
  94. self.assertTrue(sd_eci['eContent'].hasValue())
  95. for ri in asn1Object['content']['crls']:
  96. if ri.getName() == 'crl':
  97. v2 = rfc5280.Version(value='v2')
  98. self.assertEqual(v2, ri['crl']['tbsCertList']['version'])
  99. if ri.getName() == 'other':
  100. ori = ri['other']
  101. ocspr_oid = rfc5940.id_ri_ocsp_response
  102. self.assertEqual(ocspr_oid, ori['otherRevInfoFormat'])
  103. ocspr_status = ori['otherRevInfo']['responseStatus']
  104. success = rfc2560.OCSPResponseStatus(value='successful')
  105. self.assertEqual(success, ocspr_status)
  106. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  107. if __name__ == '__main__':
  108. result = unittest.TextTestRunner(verbosity=2).run(suite)
  109. sys.exit(not result.wasSuccessful())