test_rfc3058.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.type import univ
  11. from pyasn1.codec.der.decoder import decode as der_decoder
  12. from pyasn1.codec.der.encoder import encode as der_encoder
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc3058
  15. from pyasn1_modules import rfc5280
  16. from pyasn1_modules import rfc5652
  17. from pyasn1_modules import rfc5751
  18. class EnvelopedDataTestCase(unittest.TestCase):
  19. env_data_pem_text = """\
  20. MIIFgwYJKoZIhvcNAQcDoIIFdDCCBXACAQIxXaJbAgEEMCMEEDiCUYXKXu8SzLos
  21. n2xeYP4YDzIwMTkwOTEyMTIwMDAwWjAPBgsrBgEEAYE8BwEBBgUABCB0G/YBGH3L
  22. 3RhoG0mK33M8IvRYAOsnHB5MfUAOGF6kuDCCBQoGCSqGSIb3DQEHATAZBgsrBgEE
  23. AYE8BwEBAjAKBAhCT0dVU19JVoCCBOBzx7F6GMkP+C0Q4iuDq0rkSZprg8nuXx/4
  24. S3IMP999BrJdUAbPYxdQhAwTOZIuueyv00TJe/Eam9yyLuZXy0PFlTRi7KED8L8c
  25. yHsRoEobWGMLvE3D4hEhTGttElqQxBvMxZZgm6kLnNG7j8Z72L4lU4aARLYTQvkt
  26. lJnnfCaccDSiWzU8eXcXdnZAzcKR7CoDc0/XBpdDRddvQ7KXoarXYHuSybt649YD
  27. cpy0SN9gEPqcFPrBB3nusAx4VOTlpx5Z3ZJv/TEymN8KDobNfykBZURTwupO9WaV
  28. JZ3Hd/d8C1SCJn6DHuM1jwDp26WfzO8xCfea08MJrnQbNKsDHwmt4dFZIO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9x+ynzWO8kIGYNK2uxSBIzPLQ6Daq4c53rWFFNWVjPC8m98zMc
  45. Yp0hbBhRsdk4qj8osSTcTfpT0+Q+hkYQvZl4IfgX1aHeaCDSScF8SaU+cZ7GYFvL
  46. o1cYrtVbeXrFwmWl0xpco1Ux+XZgryT/fgfJ+3ToppgsQmzECqTWmYsSYaF1kLU4
  47. Cqi9UH/VqBLOkwxoH05Zao2xOMNzu2QO3wFnvY2wBsIj1eaxfzVb42o9vom7V20j
  48. T1ufXXctf9ls5J1WJxBxdKmXQWdNloeAcl1AtxTbw7vIUU5uWqu9wwqly11MDVPA
  49. b0tcQW20auWmCNkXd52jQJ7PXR6kr5I=
  50. """
  51. def setUp(self):
  52. self.asn1Spec = rfc5652.ContentInfo()
  53. def testDerCodec(self):
  54. substrate = pem.readBase64fromText(self.env_data_pem_text)
  55. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  56. self.assertFalse(rest)
  57. self.assertTrue(asn1Object.prettyPrint())
  58. self.assertEqual(substrate, der_encoder(asn1Object))
  59. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  60. ed, rest = der_decoder(
  61. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  62. self.assertFalse(rest)
  63. self.assertTrue(ed.prettyPrint())
  64. self.assertEqual(asn1Object['content'], der_encoder(ed))
  65. kwa = ed['recipientInfos'][0]['kekri']['keyEncryptionAlgorithm']
  66. self.assertEqual(rfc3058.id_alg_CMSIDEAwrap, kwa['algorithm'])
  67. self.assertEqual(kwa['parameters'], der_encoder(univ.Null("")))
  68. cea = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  69. self.assertEqual(rfc3058.id_IDEA_CBC, cea['algorithm'])
  70. param, rest = der_decoder(
  71. cea['parameters'], asn1Spec=rfc3058.IDEA_CBCPar())
  72. self.assertFalse(rest)
  73. self.assertTrue(param.prettyPrint())
  74. self.assertEqual(cea['parameters'], der_encoder(param))
  75. iv = univ.OctetString(hexValue='424f4755535f4956')
  76. self.assertEqual(iv, param['iv'])
  77. def testOpenTypes(self):
  78. substrate = pem.readBase64fromText(self.env_data_pem_text)
  79. asn1Object, rest = der_decoder(
  80. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  81. self.assertFalse(rest)
  82. self.assertTrue(asn1Object.prettyPrint())
  83. self.assertEqual(substrate, der_encoder(asn1Object))
  84. kekri = asn1Object['content']['recipientInfos'][0]['kekri']
  85. kwa = kekri['keyEncryptionAlgorithm']
  86. self.assertEqual(rfc3058.id_alg_CMSIDEAwrap, kwa['algorithm'])
  87. self.assertEqual(univ.Null(""), kwa['parameters'])
  88. eci = asn1Object['content']['encryptedContentInfo']
  89. cea = eci['contentEncryptionAlgorithm']
  90. self.assertEqual(rfc3058.id_IDEA_CBC, cea['algorithm'])
  91. iv = univ.OctetString(hexValue='424f4755535f4956')
  92. self.assertEqual(iv, cea['parameters']['iv'])
  93. class SMIMECapabilitiesTestCase(unittest.TestCase):
  94. smime_capabilities_pem_text = "MB4wDQYLKwYBBAGBPAcBAQIwDQYLKwYBBAGBPAcBAQY="
  95. def setUp(self):
  96. self.asn1Spec = rfc5751.SMIMECapabilities()
  97. def testDerCodec(self):
  98. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  99. asn1Object, rest = der_decoder(
  100. substrate, asn1Spec=self.asn1Spec)
  101. self.assertFalse(rest)
  102. self.assertTrue(asn1Object.prettyPrint())
  103. self.assertEqual(substrate, der_encoder(asn1Object))
  104. alg_oid_list = [ ]
  105. for cap in asn1Object:
  106. self.assertFalse(cap['parameters'].hasValue())
  107. alg_oid_list.append(cap['capabilityID'])
  108. self.assertIn(rfc3058.id_IDEA_CBC, alg_oid_list)
  109. self.assertIn(rfc3058.id_alg_CMSIDEAwrap, alg_oid_list)
  110. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  111. if __name__ == '__main__':
  112. result = unittest.TextTestRunner(verbosity=2).run(suite)
  113. sys.exit(not result.wasSuccessful())