test_rfc5083.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2018, 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 rfc5083
  15. from pyasn1_modules import rfc5035
  16. class AuthEnvelopedDataTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIICdQIBADGCAiekggIjBgsqhkiG9w0BCRANATCCAhICAQAEE3B0Zi1rbWM6MTM2MTQxMjIx
  19. MTIwDQYLKoZIhvcNAQkQAzAwCwYJYIZIAWUDBAEtMIIBsDCCAawCAQKAFJ7rZ8m5WnTUTS8W
  20. OWaA6AG1y6ScMA0GCSqGSIb3DQEBAQUABIIBgHfnHNqDbyyql2NqX6UQggelWMTjwzJJ1L2e
  21. rbsj1bIAGmpIsUijw+fX8VOS7v1C9ui2Md9NFgCfkmKLo8T/jELqrk7MpMu09G5zDgeXzJfQ
  22. DFc115wbrWAUU3XP7XIb6TNOc3xtq4UxA5V6jNUK2XyWKpjzOtM7gm0VWIJGVVlYu+u32LQc
  23. CjRFb87kvOY/WEnjxQpCW8g+4V747Ud97dYpMub7TLJiRNZkdHnq8xEGKlXjVHSgc10lhphe
  24. 1kFGeCpfJEsqjtN7YsVzf65ri9Z+3FJ1IO4cnMDbzGhyRXkS7a0k58/miJbSj88PvzKNSURw
  25. pu4YHMQQX/mjT2ey1SY4ihPMuxxgTdCa04L0UxaRr7xAucz3n2UWShelm3IIjnWRlYdXypnX
  26. vKvwCLoeh5mJwUl1JNFPCQkQ487cKRyobUyNgXQKT4ZDHCgXciwsX5nTsom87Ixp5vqSDJ+D
  27. hXA0r/Caiu1vnY5X9GLHSkqgXkgqgUuu0LfcsQERD8psfQQogbiuZDqJmYt1Iau/pkuGfmee
  28. qeiM3aeQ4NZf9AFZUVWBGArPNHrvVDA3BgkqhkiG9w0BBwEwGwYJYIZIAWUDBAEuMA4EDMr+
  29. ur76ztut3sr4iIANmvLRbyFUf87+2bPvLQQMoOWSXMGE4BckY8RM
  30. """
  31. def setUp(self):
  32. self.asn1Spec = rfc5083.AuthEnvelopedData()
  33. def testDerCodec(self):
  34. substrate = pem.readBase64fromText(self.pem_text)
  35. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  36. self.assertFalse(rest)
  37. self.assertTrue(asn1Object.prettyPrint())
  38. self.assertEqual(substrate, der_encoder(asn1Object))
  39. class AuthEnvelopedDataOpenTypesTestCase(unittest.TestCase):
  40. pem_text = """\
  41. MIICvQYLKoZIhvcNAQkQARegggKsMIICqAIBADGCAiekggIjBgsqhkiG9w0BCRAN
  42. ATCCAhICAQAEE3B0Zi1rbWM6MTM2MTQxMjIxMTIwDQYLKoZIhvcNAQkQAzAwCwYJ
  43. YIZIAWUDBAEtMIIBsDCCAawCAQKAFJ7rZ8m5WnTUTS8WOWaA6AG1y6ScMA0GCSqG
  44. SIb3DQEBAQUABIIBgHfnHNqDbyyql2NqX6UQggelWMTjwzJJ1L2erbsj1bIAGmpI
  45. sUijw+fX8VOS7v1C9ui2Md9NFgCfkmKLo8T/jELqrk7MpMu09G5zDgeXzJfQDFc1
  46. 15wbrWAUU3XP7XIb6TNOc3xtq4UxA5V6jNUK2XyWKpjzOtM7gm0VWIJGVVlYu+u3
  47. 2LQcCjRFb87kvOY/WEnjxQpCW8g+4V747Ud97dYpMub7TLJiRNZkdHnq8xEGKlXj
  48. VHSgc10lhphe1kFGeCpfJEsqjtN7YsVzf65ri9Z+3FJ1IO4cnMDbzGhyRXkS7a0k
  49. 58/miJbSj88PvzKNSURwpu4YHMQQX/mjT2ey1SY4ihPMuxxgTdCa04L0UxaRr7xA
  50. ucz3n2UWShelm3IIjnWRlYdXypnXvKvwCLoeh5mJwUl1JNFPCQkQ487cKRyobUyN
  51. gXQKT4ZDHCgXciwsX5nTsom87Ixp5vqSDJ+DhXA0r/Caiu1vnY5X9GLHSkqgXkgq
  52. gUuu0LfcsQERD8psfQQogbiuZDqJmYt1Iau/pkuGfmeeqeiM3aeQ4NZf9AFZUVWB
  53. GArPNHrvVDA3BgkqhkiG9w0BBwEwGwYJYIZIAWUDBAEuMA4EDMr+ur76ztut3sr4
  54. iIANmvLRbyFUf87+2bPvLQQMoOWSXMGE4BckY8RMojEwLwYLKoZIhvcNAQkQAgQx
  55. IDAeDBFXYXRzb24sIGNvbWUgaGVyZQYJKoZIhvcNAQcB
  56. """
  57. def setUp(self):
  58. self.asn1Spec = rfc5652.ContentInfo()
  59. def testDerCodec(self):
  60. substrate = pem.readBase64fromText(self.pem_text)
  61. asn1Object, rest = der_decoder(
  62. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  63. self.assertFalse(rest)
  64. self.assertTrue(asn1Object.prettyPrint())
  65. self.assertEqual(substrate, der_encoder(asn1Object))
  66. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap)
  67. self.assertEqual(rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  68. authenv = asn1Object['content']
  69. self.assertEqual(0, authenv['version'])
  70. for attr in authenv['unauthAttrs']:
  71. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  72. if attr['attrType'] == rfc5035.id_aa_contentHint:
  73. self.assertIn(
  74. 'Watson', attr['attrValues'][0]['contentDescription'])
  75. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  76. if __name__ == '__main__':
  77. result = unittest.TextTestRunner(verbosity=2).run(suite)
  78. sys.exit(not result.wasSuccessful())