test_rfc8419.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 rfc5280
  14. from pyasn1_modules import rfc8419
  15. class Ed25519TestCase(unittest.TestCase):
  16. alg_id_1_pem_text = "MAUGAytlcA=="
  17. def setUp(self):
  18. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  19. def testDerCodec(self):
  20. substrate = pem.readBase64fromText(self.alg_id_1_pem_text)
  21. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  22. self.assertFalse(rest)
  23. self.assertTrue(asn1Object.prettyPrint())
  24. self.assertEqual(rfc8419.id_Ed25519, asn1Object['algorithm'])
  25. self.assertFalse(asn1Object['parameters'].isValue)
  26. self.assertEqual(substrate, der_encoder(asn1Object))
  27. class Ed448TestCase(unittest.TestCase):
  28. alg_id_2_pem_text = "MAUGAytlcQ=="
  29. def setUp(self):
  30. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  31. def testDerCodec(self):
  32. substrate = pem.readBase64fromText(self.alg_id_2_pem_text)
  33. asn1Object, rest = der_decoder(
  34. substrate, asn1Spec=self.asn1Spec)
  35. self.assertFalse(rest)
  36. self.assertTrue(asn1Object.prettyPrint())
  37. self.assertEqual(rfc8419.id_Ed448, asn1Object['algorithm'])
  38. self.assertFalse(asn1Object['parameters'].isValue)
  39. self.assertEqual(substrate, der_encoder(asn1Object))
  40. class SHA512TestCase(unittest.TestCase):
  41. alg_id_3_pem_text = "MAsGCWCGSAFlAwQCAw=="
  42. def setUp(self):
  43. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  44. def testDerCodec(self):
  45. substrate = pem.readBase64fromText(self.alg_id_3_pem_text)
  46. asn1Object, rest = der_decoder(
  47. substrate, asn1Spec=self.asn1Spec)
  48. self.assertFalse(rest)
  49. self.assertTrue(asn1Object.prettyPrint())
  50. self.assertEqual(rfc8419.id_sha512, asn1Object['algorithm'])
  51. self.assertFalse(asn1Object['parameters'].isValue)
  52. self.assertEqual(substrate, der_encoder(asn1Object))
  53. class SHAKE256TestCase(unittest.TestCase):
  54. alg_id_4_pem_text = "MAsGCWCGSAFlAwQCDA=="
  55. def setUp(self):
  56. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  57. def testDerCodec(self):
  58. substrate = pem.readBase64fromText(self.alg_id_4_pem_text)
  59. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  60. self.assertFalse(rest)
  61. self.assertTrue(asn1Object.prettyPrint())
  62. self.assertEqual(rfc8419.id_shake256, asn1Object['algorithm'])
  63. self.assertFalse(asn1Object['parameters'].isValue)
  64. self.assertEqual(substrate, der_encoder(asn1Object))
  65. class SHAKE256LENTestCase(unittest.TestCase):
  66. alg_id_5_pem_text = "MA8GCWCGSAFlAwQCEgICAgA="
  67. def setUp(self):
  68. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  69. def testDerCodec(self):
  70. substrate = pem.readBase64fromText(self.alg_id_5_pem_text)
  71. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  72. self.assertFalse(rest)
  73. self.assertTrue(asn1Object.prettyPrint())
  74. self.assertEqual(rfc8419.id_shake256_len, asn1Object['algorithm'])
  75. self.assertTrue(asn1Object['parameters'].isValue)
  76. self.assertEqual(substrate, der_encoder(asn1Object))
  77. param, rest = der_decoder(
  78. asn1Object['parameters'],
  79. asn1Spec=rfc5280.algorithmIdentifierMap[asn1Object['algorithm']])
  80. self.assertFalse(rest)
  81. self.assertTrue(param.prettyPrint())
  82. self.assertEqual(asn1Object['parameters'], der_encoder(param))
  83. self.assertEqual(512, param)
  84. def testOpenTypes(self):
  85. substrate = pem.readBase64fromText(self.alg_id_5_pem_text)
  86. asn1Object, rest = der_decoder(
  87. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  88. self.assertFalse(rest)
  89. self.assertTrue(asn1Object.prettyPrint())
  90. self.assertEqual(rfc8419.id_shake256_len, asn1Object['algorithm'])
  91. self.assertEqual(512, asn1Object['parameters'])
  92. self.assertEqual(substrate, der_encoder(asn1Object))
  93. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  94. if __name__ == '__main__':
  95. result = unittest.TextTestRunner(verbosity=2).run(suite)
  96. sys.exit(not result.wasSuccessful())