test_rfc5636.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 rfc5652
  14. from pyasn1_modules import rfc5636
  15. class TraceableAnonymousCertificateTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MIIGOgYJKoZIhvcNAQcCoIIGKzCCBicCAQMxDTALBglghkgBZQMEAgEwRQYKKoMajJpECgEB
  18. AaA3BDUwMwQgTgtiLdByNcZGP/PPE1I2lvxDA/6bajEE4VAWF13N9E4YDzIwMTkxMjMxMTIw
  19. MDAwWqCCBB0wggQZMIIDAaADAgECAhQLxXbZnuC+8r+RhlN0rgUga/of6TANBgkqhkiG9w0B
  20. AQsFADA/MQswCQYDVQQGEwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xETAP
  21. BgNVBAoTCEJvZ3VzIENBMB4XDTE5MTIxNTE4MTA0OFoXDTIwMTIxNDE4MTA0OFowTjELMAkG
  22. A1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQHDAdIZXJuZG9uMRAwDgYDVQQKDAdFeGFt
  23. cGxlMQ4wDAYDVQQDDAVBbGljZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALt2
  24. dWnBBb4MnwcHij1I2h+oNy7zGhG7Wd4GhtonVjn5XhyLhZLTjGAbPHqFBOb9fwElS4TfpTtG
  25. d7K9INUIgM0a6wZI3j3qCqDphQBW6sPVksip9Elan1hR8Upd4iutaWKKNxCpNO5gQiMM0Nay
  26. PTIp1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hvcNAQkFMQ8XDTE5MTIxNjE1NTEyMlowLwYJKoZIhvcNAQkEMSIEIJumtIa6
  41. 3jeKcCTvxY+Pf3O8U6jko6J0atleMxdZWNAHMA0GCSqGSIb3DQEBAQUABIIBAJHxEz3qLxDz
  42. UaMxBt1wW/2tMx5AGKlxhBIE2Am/iIpdpkk0nMNt+R6GduAz9yE+lS7V+lZafZq7WKUPpAIR
  43. YYD1apaxWAigHYQCLQg08MSlhzkCjzKiVXtsfAYHYLWutvqPY8WRX7x85If333/v7kVBPZvS
  44. su/MkZ4V9USpocRq/BFYo7VbitBYFHqra+vzhRiYD1pS6EfhFwZoAv/Ud59FUACU8ixw2IuO
  45. Efe1LUIWVmbJ3HKtk8JTrWTg9iLVp+keqOWJfSEEUZXnyNIMt/SCONtZT+6SJQqwQV0C8AcR
  46. 9sxMfZum5/eKypTZ9liGP4jz6nxtD3hEyfEXf7BOfds=
  47. """
  48. def testDerCodec(self):
  49. substrate = pem.readBase64fromText(self.pem_text)
  50. layers = { }
  51. layers.update(rfc5652.cmsContentTypesMap)
  52. getNextLayer = {
  53. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  54. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  55. rfc5636.id_kisa_tac_token: lambda x: None
  56. }
  57. getNextSubstrate = {
  58. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  59. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  60. rfc5636.id_kisa_tac_token: lambda x: None
  61. }
  62. next_layer = rfc5652.id_ct_contentInfo
  63. while next_layer:
  64. asn1Object, rest = der_decoder(
  65. substrate, asn1Spec=layers[next_layer])
  66. self.assertFalse(rest)
  67. self.assertTrue(asn1Object.prettyPrint())
  68. self.assertEqual(substrate, der_encoder(asn1Object))
  69. substrate = getNextSubstrate[next_layer](asn1Object)
  70. next_layer = getNextLayer[next_layer](asn1Object)
  71. self.assertEqual('2019', asn1Object['timeout'][:4])
  72. self.assertEqual('5dcdf44e', asn1Object['userKey'].prettyPrint()[-8:])
  73. def testOpenTypes(self):
  74. asn1Spec=rfc5652.ContentInfo()
  75. substrate = pem.readBase64fromText(self.pem_text)
  76. asn1Object, rest = der_decoder(
  77. substrate, asn1Spec=asn1Spec, decodeOpenTypes=True)
  78. self.assertFalse(rest)
  79. self.assertTrue(asn1Object.prettyPrint())
  80. self.assertEqual(substrate, der_encoder(asn1Object))
  81. substrate = asn1Object['content']['encapContentInfo']['eContent']
  82. oid = asn1Object['content']['encapContentInfo']['eContentType']
  83. self.assertIn(oid, rfc5652.cmsContentTypesMap)
  84. tac_token, rest = der_decoder(
  85. substrate,
  86. asn1Spec=rfc5652.cmsContentTypesMap[oid],
  87. decodeOpenTypes=True)
  88. self.assertFalse(rest)
  89. self.assertTrue(tac_token.prettyPrint())
  90. self.assertEqual(substrate, der_encoder(tac_token))
  91. self.assertEqual('2019', tac_token['timeout'][:4])
  92. self.assertEqual('5dcdf44e', tac_token['userKey'].prettyPrint()[-8:])
  93. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  94. if __name__ == '__main__':
  95. result = unittest.TextTestRunner(verbosity=2).run(suite)
  96. sys.exit(not result.wasSuccessful())