test_rfc5084.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2018, 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 rfc5083
  14. from pyasn1_modules import rfc5084
  15. from pyasn1_modules import rfc5652
  16. class CCMParametersTestCase(unittest.TestCase):
  17. ccm_pem_text = "MBEEDE2HVyIurFKUEX8MEgIBBA=="
  18. def setUp(self):
  19. self.asn1Spec = rfc5084.CCMParameters()
  20. def testDerCodec(self):
  21. substrate = pem.readBase64fromText(self.ccm_pem_text)
  22. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  23. self.assertFalse(rest)
  24. self.assertTrue(asn1Object.prettyPrint())
  25. self.assertEqual(substrate, der_encoder(asn1Object))
  26. class GCMParametersTestCase(unittest.TestCase):
  27. gcm_pem_text = "MBEEDE2HVyIurFKUEX8MEgIBEA=="
  28. def setUp(self):
  29. self.asn1Spec = rfc5084.GCMParameters()
  30. def testDerCodec(self):
  31. substrate = pem.readBase64fromText(self.gcm_pem_text)
  32. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  33. self.assertFalse(rest)
  34. self.assertTrue(asn1Object.prettyPrint())
  35. self.assertEqual(substrate, der_encoder(asn1Object))
  36. class GCMOpenTypesTestCase(unittest.TestCase):
  37. rfc8591_pem_pext = """\
  38. MIIHkAYLKoZIhvcNAQkQARegggd/MIIHewIBADGCAk8wggJLAgEAMDMwJjEUMBIGA1UECgwL
  39. ZXhhbXBsZS5jb20xDjAMBgNVBAMMBUFsaWNlAgkAg/ULtwvVx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MaBCVaOhXHwybvaSP
  68. TfMG/nR/NxF1FI8xgydnzXZs8HtFDL9iytKnvXHx+IIz8Rahp/PK8S80vPQNIeef/JgnIhto
  69. sID/A614LW1tB4cWdveYlD5U8T/XXInAtCY78Q9WJD+ecu87OJmlOdmjrFvitpQAo8+NGWxc
  70. 7Wl7LtgDuYel7oXFCVtI2npbA7R+K5/kzUvDCY6GTgzn1Gfamc1/Op6Ue17qd/emvhbIx+ng
  71. 3swf8TJVnCNDIXucKVA4boXSlCEhCGzfoZZYGVvm1/hrypiBtpUIKWTxLnz4AQJdZ5LGiCQJ
  72. QU1wMyHsg6vWmNaJVhGHE6D/EnKsvJptFIkAx0wWkh35s48p7EbU8QBg//5eNru6yvLRutfd
  73. BX7T4w681pCD+dOiom75C3UdahrfoFkNsZ2hB88+qNsEEPb/xuGu8ZzSPZhakhl2NS0=
  74. """
  75. def setUp(self):
  76. self.asn1Spec = rfc5652.ContentInfo()
  77. def testDerCodec(self):
  78. substrate = pem.readBase64fromText(self.rfc8591_pem_pext)
  79. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  80. self.assertFalse(rest)
  81. self.assertTrue(asn1Object.prettyPrint())
  82. self.assertEqual(substrate, der_encoder(asn1Object))
  83. self.assertEqual(
  84. rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  85. aed, rest = der_decoder(
  86. asn1Object['content'], asn1Spec=rfc5083.AuthEnvelopedData(),
  87. decodeOpenTypes=True)
  88. self.assertFalse(rest)
  89. self.assertTrue(aed.prettyPrint())
  90. self.assertEqual(asn1Object['content'], der_encoder(aed))
  91. self.assertEqual(0, aed['version'])
  92. cea = aed['authEncryptedContentInfo']['contentEncryptionAlgorithm']
  93. self.assertEqual(rfc5084.id_aes128_GCM, cea['algorithm'])
  94. self.assertEqual(16, cea['parameters']['aes-ICVlen'])
  95. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  96. if __name__ == '__main__':
  97. result = unittest.TextTestRunner(verbosity=2).run(suite)
  98. sys.exit(not result.wasSuccessful())