test_rfc4010.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 rfc4010
  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. MIIFewYJKoZIhvcNAQcDoIIFbDCCBWgCAQIxUqJQAgEEMCMEEKBBI2KxDUPS5TCo
  21. RCEDJo4YDzIwMTkwOTEyMTIwMDAwWjAMBgoqgxqMmkQHAQEBBBipFE2DxCLAx2Og
  22. E53Jt21V8kAoscU7K3wwggUNBgkqhkiG9w0BBwEwHAYIKoMajJpEAQQEEEJPR1VT
  23. SVZfQk9HVVNJViGAggTgc8exehjJD/gtEOIrg6tK5Emaa4PJ7l8f+EtyDD/ffQay
  24. XVAGz2MXUIQMEzmSLrnsr9NEyXvxGpvcsi7mV8tDxZU0YuyhA/C/HMh7EaBKG1hj
  25. C7xNw+IRIUxrbRJakMQbzMWWYJupC5zRu4/Ge9i+JVOGgES2E0L5LZSZ53wmnHA0
  26. ols1PHl3F3Z2QM3CkewqA3NP1waXQ0XXb0Oyl6Gq12B7ksm7euPWA3KctEjfYBD6
  27. nBT6wQd57rAMeFTk5aceWd2Sb/0xMpjfCg6GzX8pAWVEU8LqTvVmlSWdx3f3fAtU
  28. giZ+gx7jNY8A6duln8zvMQn3mtPDCa50GzSrAx8JreHRWSDr3Dp8EfJz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y0Og2quHOd61hRTVlYzwvJvfMzHGKdIWwYUbHZ
  45. OKo/KLEk3E36U9PkPoZGEL2ZeCH4F9Wh3mgg0knBfEmlPnGexmBby6NXGK7VW3l6
  46. xcJlpdMaXKNVMfl2YK8k/34Hyft06KaYLEJsxAqk1pmLEmGhdZC1OAqovVB/1agS
  47. zpMMaB9OWWqNsTjDc7tkDt8BZ72NsAbCI9XmsX81W+NqPb6Ju1dtI09bn113LX/Z
  48. bOSdVicQcXSpl0FnTZaHgHJdQLcU28O7yFFOblqrvcMKpctdTA1TwG9LXEFttGrl
  49. pgjZF3edo0Cez10epK+S
  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(asn1Object['content'], rfc5652.EnvelopedData())
  61. self.assertFalse(rest)
  62. self.assertTrue(ed.prettyPrint())
  63. self.assertEqual(asn1Object['content'], der_encoder(ed))
  64. kwa = ed['recipientInfos'][0]['kekri']['keyEncryptionAlgorithm']
  65. self.assertEqual(rfc4010.id_npki_app_cmsSeed_wrap, kwa['algorithm'])
  66. cea = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  67. self.assertEqual(rfc4010.id_seedCBC, cea['algorithm'])
  68. param, rest = der_decoder(
  69. cea['parameters'], asn1Spec=rfc4010.SeedCBCParameter())
  70. self.assertFalse(rest)
  71. self.assertTrue(param.prettyPrint())
  72. self.assertEqual(cea['parameters'], der_encoder(param))
  73. iv = univ.OctetString(hexValue='424f47555349565f424f475553495621')
  74. self.assertEqual(iv, param)
  75. def testOpenTypes(self):
  76. substrate = pem.readBase64fromText(self.env_data_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. self.assertTrue(asn1Object['contentType'] in rfc5652.cmsContentTypesMap.keys())
  83. kekri = asn1Object['content']['recipientInfos'][0]['kekri']
  84. kwa = kekri['keyEncryptionAlgorithm']
  85. self.assertEqual(rfc4010.id_npki_app_cmsSeed_wrap, kwa['algorithm'])
  86. eci = asn1Object['content']['encryptedContentInfo']
  87. cea = eci['contentEncryptionAlgorithm']
  88. self.assertEqual(rfc4010.id_seedCBC, cea['algorithm'])
  89. iv = univ.OctetString(hexValue='424f47555349565f424f475553495621')
  90. self.assertEqual(iv, cea['parameters'])
  91. class SMIMECapabilitiesTestCase(unittest.TestCase):
  92. smime_capabilities_pem_text = "MB4wDAYIKoMajJpEAQQFADAOBgoqgxqMmkQHAQEBBQA="
  93. def setUp(self):
  94. self.asn1Spec = rfc5751.SMIMECapabilities()
  95. def testDerCodec(self):
  96. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  97. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  98. self.assertFalse(rest)
  99. self.assertTrue(asn1Object.prettyPrint())
  100. self.assertEqual(substrate, der_encoder(asn1Object))
  101. alg_oid_list = [ ]
  102. for cap in asn1Object:
  103. self.assertTrue(cap['parameters'].hasValue())
  104. self.assertEqual(cap['parameters'], der_encoder(rfc4010.SeedSMimeCapability("")))
  105. alg_oid_list.append(cap['capabilityID'])
  106. self.assertIn(rfc4010.id_seedCBC, alg_oid_list)
  107. self.assertIn(rfc4010.id_npki_app_cmsSeed_wrap, alg_oid_list)
  108. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  109. if __name__ == '__main__':
  110. result = unittest.TextTestRunner(verbosity=2).run(suite)
  111. sys.exit(not result.wasSuccessful())