test_rfc8018.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 rfc8018
  15. class PWRITestCase(unittest.TestCase):
  16. rfc3211_ex1_pem_text = """\
  17. o1MCAQCgGgYJKoZIhvcNAQUMMA0ECBI0Vnh4VjQSAgEFMCAGCyqGSIb3DQEJEAMJMBEGBSsO
  18. AwIHBAjv5ZjvIbM9bQQQuBslZe43PKbe3KJqF4sMEA==
  19. """
  20. def setUp(self):
  21. self.asn1Spec = rfc5652.RecipientInfo()
  22. def testDerCodec(self):
  23. substrate = pem.readBase64fromText(self.rfc3211_ex1_pem_text)
  24. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  25. self.assertFalse(rest)
  26. self.assertTrue(asn1Object.prettyPrint())
  27. self.assertEqual(substrate, der_encoder(asn1Object))
  28. alg_oid = asn1Object['pwri']['keyDerivationAlgorithm']['algorithm']
  29. self.assertEqual(rfc8018.id_PBKDF2, alg_oid)
  30. def testOpenTypes(self):
  31. substrate = pem.readBase64fromText(self.rfc3211_ex1_pem_text)
  32. asn1Object, rest = der_decoder(
  33. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  34. self.assertFalse(rest)
  35. self.assertTrue(asn1Object.prettyPrint())
  36. self.assertEqual(substrate, der_encoder(asn1Object))
  37. icount = (asn1Object['pwri']['keyDerivationAlgorithm']
  38. ['parameters']['iterationCount'])
  39. self.assertEqual(5, icount)
  40. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  41. if __name__ == '__main__':
  42. unittest.TextTestRunner(verbosity=2).run(suite)