test_rfc6210.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 rfc5280
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc6210
  16. class AuthenticatedDataTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIICRQYLKoZIhvcNAQkQAQKgggI0MIICMAIBADGBwDCBvQIBADAmMBIxEDAOBgNVBAMMB0
  19. NhcmxSU0ECEEY0a8eAAFa8EdNuLs1dcdAwDQYJKoZIhvcNAQEBBQAEgYCH70EpEikY7deb
  20. 859YJRAWfFondQv1D4NFltw6C1ceheWnlAU0C2WEXr3LUBXZp1/PSte29FnJxu5bXCTn1g
  21. elMm6zNlZNWNd0KadVBcaxi1n8L52tVM5sWFGJPO5cStOyAka2ucuZM6iAnCSkn1Ju7fgU
  22. 5j2g3bZ/IM8nHTcygjAKBggrBgEFBQgBAqFPBgsqhkiG9w0BCRADDQRAAQIDBAUGBwgJCg
  23. sMDQ4PEBESEwQVFhcYGRobHB0eHyAhIiMEJSYnKCkqKywtLi8wMTIzBDU2Nzg5Ojs8PT4/
  24. QDArBgkqhkiG9w0BBwGgHgQcVGhpcyBpcyBzb21lIHNhbXBsZSBjb250ZW50LqKBxzAYBg
  25. kqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0wOTEyMTAyMzI1MDBa
  26. MB8GCSqGSIb3DQEJBDESBBCWaa5hG1eeg+oQK2tJ3cD5MGwGCSqGSIb3DQEJNDFfMF0wTw
  27. YLKoZIhvcNAQkQAw0EQAECAwQFBgcICQoLDA0ODxAREhMEFRYXGBkaGxwdHh8gISIjBCUm
  28. JygpKissLS4vMDEyMwQ1Njc4OTo7PD0+P0CiCgYIKwYBBQUIAQIEFLjUxQ9PJFzFnWraxb
  29. EIbVbg2xql
  30. """
  31. def setUp(self):
  32. self.asn1Spec = rfc5652.ContentInfo()
  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. self.assertEqual(rfc5652.id_ct_authData, asn1Object['contentType'])
  40. ad, rest = der_decoder(
  41. asn1Object['content'], asn1Spec=rfc5652.AuthenticatedData())
  42. self.assertFalse(rest)
  43. self.assertTrue(ad.prettyPrint())
  44. self.assertEqual(asn1Object['content'], der_encoder(ad))
  45. self.assertEqual(0, ad['version'])
  46. self.assertEqual(
  47. rfc6210.id_alg_MD5_XOR_EXPERIMENT, ad['digestAlgorithm']['algorithm'])
  48. mac_alg_p, rest = der_decoder(
  49. ad['digestAlgorithm']['parameters'],
  50. asn1Spec=rfc5280.algorithmIdentifierMap[ad['digestAlgorithm']['algorithm']])
  51. self.assertFalse(rest)
  52. self.assertTrue(mac_alg_p.prettyPrint())
  53. self.assertEqual(
  54. ad['digestAlgorithm']['parameters'], der_encoder(mac_alg_p))
  55. self.assertEqual("0x01020304", mac_alg_p.prettyPrint()[:10])
  56. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  57. if __name__ == '__main__':
  58. result = unittest.TextTestRunner(verbosity=2).run(suite)
  59. sys.exit(not result.wasSuccessful())