test_rfc3565.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 import decoder as der_decoder
  11. from pyasn1.codec.der import encoder as der_encoder
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc3565
  15. class AESKeyWrapTestCase(unittest.TestCase):
  16. kw_alg_id_pem_text = "MAsGCWCGSAFlAwQBLQ=="
  17. def setUp(self):
  18. self.asn1Spec = rfc3565.AlgorithmIdentifier()
  19. def testDerCodec(self):
  20. substrate = pem.readBase64fromText(self.kw_alg_id_pem_text)
  21. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  22. self.assertFalse(rest)
  23. self.assertTrue(asn1Object.prettyPrint())
  24. self.assertEqual(rfc3565.id_aes256_wrap, asn1Object[0])
  25. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  26. class AESCBCTestCase(unittest.TestCase):
  27. aes_alg_id_pem_text = "MB0GCWCGSAFlAwQBKgQQEImWuoUOPwM5mTu1h4oONw=="
  28. def setUp(self):
  29. self.asn1Spec = rfc3565.AlgorithmIdentifier()
  30. def testDerCodec(self):
  31. substrate = pem.readBase64fromText(self.aes_alg_id_pem_text)
  32. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  33. self.assertFalse(rest)
  34. self.assertTrue(asn1Object.prettyPrint())
  35. self.assertEqual(rfc3565.id_aes256_CBC, asn1Object[0])
  36. self.assertTrue(asn1Object[1].isValue)
  37. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  38. def testOpenTypes(self):
  39. substrate = pem.readBase64fromText(self.aes_alg_id_pem_text)
  40. asn1Object, rest = der_decoder.decode(substrate,
  41. asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  42. self.assertFalse(rest)
  43. self.assertTrue(asn1Object.prettyPrint())
  44. self.assertEqual(rfc3565.id_aes256_CBC, asn1Object[0])
  45. aes_iv = univ.OctetString(hexValue='108996ba850e3f0339993bb5878a0e37')
  46. self.assertEqual(aes_iv, asn1Object[1])
  47. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  48. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  49. if __name__ == '__main__':
  50. unittest.TextTestRunner(verbosity=2).run(suite)