test_rfc6032.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.compat.octets import str2octs
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc6032
  16. class EncryptedKeyPkgTestCase(unittest.TestCase):
  17. encrypted_key_pkg_pem_text = """\
  18. MIIBBwYKYIZIAWUCAQJOAqCB+DCB9QIBAjCBzgYKYIZIAWUCAQJOAjAdBglghkgB
  19. ZQMEASoEEN6HFteHMZ3DyeO35xIwWQOAgaCKTs0D0HguNzMhsLgiwG/Kw8OwX+GF
  20. 9/cZ1YVNesUTW/VsbXJcbTmFmWyfqZsM4DLBegIbrUEHQZnQRq6/NO4ricQdHApD
  21. B/ip6RRqeN1yxMJLv1YN0zUOOIDBS2iMEjTLXZLWw3w22GN2JK7G+Lr4OH1NhMgU
  22. ILJyh/RePmPseMwxvcJs7liEfkiSNMtDfEcpjtzA9bDe95GjhQRsiSByoR8wHQYJ
  23. YIZIAWUCAQVCMRAEDnB0Zi1rZGMtODEyMzc0
  24. """
  25. def setUp(self):
  26. self.asn1Spec = rfc5652.ContentInfo()
  27. def testDerCodec(self):
  28. substrate = pem.readBase64fromText(self.encrypted_key_pkg_pem_text)
  29. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  30. self.assertFalse(rest)
  31. self.assertTrue(asn1Object.prettyPrint())
  32. self.assertEqual(substrate, der_encoder(asn1Object))
  33. self.assertEqual(
  34. rfc6032.id_ct_KP_encryptedKeyPkg, asn1Object['contentType'])
  35. content, rest = der_decoder(
  36. asn1Object['content'], rfc6032.EncryptedKeyPackage())
  37. self.assertFalse(rest)
  38. self.assertTrue(content.prettyPrint())
  39. self.assertEqual(asn1Object['content'], der_encoder(content))
  40. self.assertEqual('encrypted', content.getName())
  41. eci = content['encrypted']['encryptedContentInfo']
  42. self.assertEqual(
  43. rfc6032.id_ct_KP_encryptedKeyPkg, eci['contentType'])
  44. attrType = content['encrypted']['unprotectedAttrs'][0]['attrType']
  45. self.assertEqual(rfc6032.id_aa_KP_contentDecryptKeyID, attrType)
  46. attrVal0 = content['encrypted']['unprotectedAttrs'][0]['attrValues'][0]
  47. keyid, rest = der_decoder(attrVal0, rfc6032.ContentDecryptKeyID())
  48. self.assertFalse(rest)
  49. self.assertTrue(keyid.prettyPrint())
  50. self.assertEqual(attrVal0, der_encoder(keyid))
  51. self.assertEqual(str2octs('ptf-kdc-812374'), keyid)
  52. def testOpenTypes(self):
  53. substrate = pem.readBase64fromText(self.encrypted_key_pkg_pem_text)
  54. asn1Object, rest = der_decoder(substrate,
  55. asn1Spec=self.asn1Spec,
  56. decodeOpenTypes=True)
  57. self.assertFalse(rest)
  58. self.assertTrue(asn1Object.prettyPrint())
  59. self.assertEqual(substrate, der_encoder(asn1Object))
  60. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap)
  61. eci = asn1Object['content']['encrypted']['encryptedContentInfo']
  62. self.assertIn(eci['contentType'], rfc5652.cmsContentTypesMap)
  63. for attr in asn1Object['content']['encrypted']['unprotectedAttrs']:
  64. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  65. self.assertNotEqual('0x', attr['attrValues'][0].prettyPrint()[:2])
  66. if attr['attrType'] == rfc6032.id_aa_KP_contentDecryptKeyID:
  67. self.assertEqual(str2octs(
  68. 'ptf-kdc-812374'), attr['attrValues'][0])
  69. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  70. if __name__ == '__main__':
  71. unittest.TextTestRunner(verbosity=2).run(suite)