test_rfc8479.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, 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 rfc4055
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc5958
  16. from pyasn1_modules import rfc8479
  17. class ValidationParmTestCase(unittest.TestCase):
  18. pem_text = """\
  19. MIIE/gIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpPwXwfhDsWA3q
  20. jN2BWg1xfDjvZDVNfgTV/b95g304Aty3z13xPXAhHZ3ROW3pgPxTj9fiq7ZMy4Ua
  21. gMpPK81v3pHX1uokC2KcGXbgbAq2Q8ClxSXgEJllRwDENufjEdV10gArt8NlIP0N
  22. lota1kQUuI1DMsqc5DTIa35Nq4j1GW+KmLtP0kCrGq9fMGwjDbPEpSp9DTquEMHJ
  23. o7kyJIjB+93ikLvBUTgbxr+jcnTLXuhA8rC8r+KXre4NPPNPRyefRcALLt/URvfA
  24. rTvFOQfi3vIjNhBZL5FdC+FVAr5QnF3r2+cuDPbnczr4/rr81kzFGWrwyAgF5FWu
  25. pFtB5IYDAgMBAAECggEAHZ88vGNsNdmRkfhWupGW4cKCuo+Y7re8Q/H2Jd/4Nin2
  26. FKvUPuloaztiSGDbVm+vejama/Nu5FEIumNJRYMeoVJcx2DDuUxO1ZB1aIEwfMct
  27. /DWd0/JDzuCXB0Cu5GTWLhlz0zMGHXihIdQ0DtGKt++3Ncg5gy1D+cIqqJB515/z
  28. jYdZmb0Wqmz7H3DisuxvnhiCAOuNrjcDau80hpMA9TQlb+X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gorBgEEAZIIEggBMS8wLQYJYIZIAWUDBAIC
  45. BCCK9DKMh7687DHjA7j1U37/y2qR2UcITZmjaYI7NvAUYg==
  46. """
  47. def setUp(self):
  48. self.asn1Spec = rfc5958.OneAsymmetricKey()
  49. def testDerCodec(self):
  50. substrate = pem.readBase64fromText(self.pem_text)
  51. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  52. self.assertFalse(rest)
  53. self.assertTrue(asn1Object.prettyPrint())
  54. self.assertEqual(substrate, der_encoder(asn1Object))
  55. for attr in asn1Object['attributes']:
  56. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  57. if attr['attrType'] == rfc8479.id_attr_validation_parameters:
  58. av, rest = der_decoder(
  59. attr['attrValues'][0],
  60. asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
  61. self.assertFalse(rest)
  62. self.assertTrue(av.prettyPrint())
  63. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  64. self.assertEqual(rfc4055.id_sha384, av['hashAlg'])
  65. seed = univ.OctetString(hexValue='8af4328c87bebcec31e303b8f55'
  66. '37effcb6a91d947084d99a36982'
  67. '3b36f01462')
  68. self.assertEqual(seed, av['seed'])
  69. def testOpenTypes(self):
  70. substrate = pem.readBase64fromText(self.pem_text)
  71. asn1Object, rest = der_decoder(
  72. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  73. self.assertFalse(rest)
  74. self.assertTrue(asn1Object.prettyPrint())
  75. self.assertEqual(substrate, der_encoder(asn1Object))
  76. for attr in asn1Object['attributes']:
  77. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  78. if attr['attrType'] == rfc8479.id_attr_validation_parameters:
  79. av = attr['attrValues'][0]
  80. self.assertEqual(av['hashAlg'], rfc4055.id_sha384)
  81. seed = univ.OctetString(hexValue='8af4328c87bebcec31e303b8f553'
  82. '7effcb6a91d947084d99a369823b'
  83. '36f01462')
  84. self.assertEqual(seed, av['seed'])
  85. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  86. if __name__ == '__main__':
  87. result = unittest.TextTestRunner(verbosity=2).run(suite)
  88. sys.exit(not result.wasSuccessful())