test_rfc6031.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 rfc6031
  15. class SymmetricKeyPkgTestCase(unittest.TestCase):
  16. key_pkg_pem_text = """\
  17. MIG7BgsqhkiG9w0BCRABGaCBqzCBqKBEMCMGCyqGSIb3DQEJEAwBMRQMElZpZ2ls
  18. IFNlY3VyaXR5IExMQzAdBgsqhkiG9w0BCRAMAzEODAxQcmV0ZW5kIDA0OEEwYDBe
  19. MFYwGwYLKoZIhvcNAQkQDBsxDAwKZXhhbXBsZUlEMTAVBgsqhkiG9w0BCRAMCjEG
  20. DARIT1RQMCAGCyqGSIb3DQEJEAwLMREMD2t0YS5leGFtcGxlLmNvbQQEMTIzNA==
  21. """
  22. def setUp(self):
  23. self.asn1Spec = rfc5652.ContentInfo()
  24. def testDerCodec(self):
  25. substrate = pem.readBase64fromText(self.key_pkg_pem_text)
  26. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  27. self.assertFalse(rest)
  28. self.assertTrue(asn1Object.prettyPrint())
  29. self.assertEqual(substrate, der_encoder(asn1Object))
  30. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap)
  31. asn1Spec = rfc5652.cmsContentTypesMap[asn1Object['contentType']]
  32. skp, rest = der_decoder(asn1Object['content'], asn1Spec=asn1Spec)
  33. self.assertFalse(rest)
  34. self.assertTrue(skp.prettyPrint())
  35. self.assertEqual(asn1Object['content'], der_encoder(skp))
  36. for attr in skp['sKeyPkgAttrs']:
  37. self.assertIn(attr['attrType'], rfc6031.sKeyPkgAttributesMap)
  38. for osk in skp['sKeys']:
  39. for attr in osk['sKeyAttrs']:
  40. self.assertIn(attr['attrType'], rfc6031.sKeyAttributesMap)
  41. def testOpenTypes(self):
  42. substrate = pem.readBase64fromText(self.key_pkg_pem_text)
  43. asn1Object, rest = der_decoder(
  44. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  45. self.assertFalse(rest)
  46. self.assertTrue(asn1Object.prettyPrint())
  47. self.assertEqual(substrate, der_encoder(asn1Object))
  48. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap)
  49. self.assertTrue(asn1Object['content'].hasValue())
  50. keypkg = asn1Object['content']
  51. self.assertEqual(
  52. rfc6031.KeyPkgVersion().subtype(value='v1'), keypkg['version'])
  53. for attr in keypkg['sKeyPkgAttrs']:
  54. self.assertIn(attr['attrType'], rfc6031.sKeyPkgAttributesMap)
  55. self.assertNotEqual('0x', attr['attrValues'][0].prettyPrint()[:2])
  56. # decodeOpenTypes=True did not decode if the value is shown in hex ...
  57. if attr['attrType'] == rfc6031.id_pskc_manufacturer:
  58. attr['attrValues'][0] == 'Vigil Security LLC'
  59. for osk in keypkg['sKeys']:
  60. for attr in osk['sKeyAttrs']:
  61. self.assertIn(attr['attrType'], rfc6031.sKeyAttributesMap)
  62. self.assertNotEqual(
  63. '0x', attr['attrValues'][0].prettyPrint()[:2])
  64. # decodeOpenTypes=True did not decode if the value is shown in hex ...
  65. if attr['attrType'] == rfc6031.id_pskc_issuer:
  66. attr['attrValues'][0] == 'kta.example.com'
  67. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  68. if __name__ == '__main__':
  69. unittest.TextTestRunner(verbosity=2).run(suite)