test_rfc8702.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2020, Vigil Security, LLC
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1.type import univ
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc2985
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc5280
  16. from pyasn1_modules import rfc6211
  17. from pyasn1_modules import rfc8702
  18. class AlgorithmIdentifierTestCase(unittest.TestCase):
  19. pem_text = """\
  20. MEowCwYJYIZIAWUDBAILMAsGCWCGSAFlAwQCDDAKBggrBgEFBQcGHjAKBggrBgEF
  21. BQcGHzAKBggrBgEFBQcGIDAKBggrBgEFBQcGIQ==
  22. """
  23. def setUp(self):
  24. self.asn1Spec = rfc2985.SMIMECapabilities()
  25. def testDerCodec(self):
  26. substrate = pem.readBase64fromText(self.pem_text)
  27. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  28. self.assertFalse(rest)
  29. self.assertTrue(asn1Object.prettyPrint())
  30. self.assertEqual(substrate, der_encoder(asn1Object))
  31. oid_list = (
  32. rfc8702.id_shake128,
  33. rfc8702.id_shake256,
  34. rfc8702.id_RSASSA_PSS_SHAKE128,
  35. rfc8702.id_RSASSA_PSS_SHAKE256,
  36. rfc8702.id_ecdsa_with_shake128,
  37. rfc8702.id_ecdsa_with_shake256,
  38. )
  39. for algid in asn1Object:
  40. self.assertIn(algid['algorithm'], oid_list)
  41. class AuthenticatedDataTestCase(unittest.TestCase):
  42. auth_message_pem_text = """\
  43. MIIDqgYLKoZIhvcNAQkQAQKgggOZMIIDlQIBADGCAk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AQkEMRIEENiFx45okcgTCVIBhhgF+ogwLwYLKoZIhvcN
  60. AQkQAgQxIDAeDBFXYXRzb24sIGNvbWUgaGVyZQYJKoZIhvcNAQcBMDMGCSqGSIb3
  61. DQEJNDEmMCQwCwYJYIZIAWUDBAILohUGCWCGSAFlAwQCEzAIBAZweWFzbjEEIBxm
  62. 7hx+iivDlWYp8iUmYYbc2xkpBAcTACkWH+KBRZuF
  63. """
  64. def setUp(self):
  65. self.asn1Spec = rfc5652.ContentInfo()
  66. def testDerCodec(self):
  67. substrate = pem.readBase64fromText(self.auth_message_pem_text)
  68. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  69. self.assertFalse(rest)
  70. self.assertTrue(asn1Object.prettyPrint())
  71. self.assertEqual(substrate, der_encoder(asn1Object))
  72. self.assertEqual(rfc5652.id_ct_authData, asn1Object['contentType'])
  73. ad, rest = der_decoder(
  74. asn1Object['content'], asn1Spec=rfc5652.AuthenticatedData())
  75. self.assertFalse(rest)
  76. self.assertTrue(ad.prettyPrint())
  77. self.assertEqual(asn1Object['content'], der_encoder(ad))
  78. self.assertEqual(
  79. rfc8702.id_shake128, ad['digestAlgorithm']['algorithm'])
  80. ad_mac = ad['macAlgorithm']
  81. self.assertEqual(
  82. rfc8702.id_KMACWithSHAKE128, ad_mac['algorithm'])
  83. kmac128_p, rest = der_decoder(
  84. ad_mac['parameters'],
  85. asn1Spec=rfc5280.algorithmIdentifierMap[ad_mac['algorithm']])
  86. self.assertFalse(rest)
  87. self.assertTrue(kmac128_p.prettyPrint())
  88. self.assertEqual(ad_mac['parameters'], der_encoder(kmac128_p))
  89. self.assertEqual(
  90. univ.OctetString("pyasn1"), kmac128_p['customizationString'])
  91. found_kmac128_params = False
  92. for attr in ad['authAttrs']:
  93. if attr['attrType'] == rfc6211.id_aa_cmsAlgorithmProtect:
  94. av, rest = der_decoder(
  95. attr['attrValues'][0],
  96. asn1Spec=rfc6211.CMSAlgorithmProtection())
  97. self.assertFalse(rest)
  98. self.assertTrue(av.prettyPrint())
  99. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  100. self.assertEqual(
  101. rfc8702.id_shake128, av['digestAlgorithm']['algorithm'])
  102. self.assertEqual(
  103. rfc8702.id_KMACWithSHAKE128, av['macAlgorithm']['algorithm'])
  104. found_kmac128_params = True
  105. self.assertTrue(found_kmac128_params)
  106. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  107. if __name__ == '__main__':
  108. import sys
  109. result = unittest.TextTestRunner(verbosity=2).run(suite)
  110. sys.exit(not result.wasSuccessful())