test_rfc5990.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 rfc5990
  15. class RSAKEMTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MEcGCyqGSIb3DQEJEAMOMDgwKQYHKIGMcQICBDAeMBkGCiuBBRCGSAksAQIwCwYJ
  18. YIZIAWUDBAIBAgEQMAsGCWCGSAFlAwQBBQ==
  19. """
  20. def setUp(self):
  21. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  22. def testDerCodec(self):
  23. substrate = pem.readBase64fromText(self.pem_text)
  24. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  25. self.assertFalse(rest)
  26. self.assertTrue(asn1Object.prettyPrint())
  27. self.assertEqual(substrate, der_encoder(asn1Object))
  28. self.assertEqual(rfc5990.id_rsa_kem, asn1Object['algorithm'])
  29. rsa_kem_p, rest = der_decoder(
  30. asn1Object['parameters'],
  31. asn1Spec=rfc5280.algorithmIdentifierMap[rfc5990.id_rsa_kem])
  32. self.assertFalse(rest)
  33. self.assertTrue(rsa_kem_p.prettyPrint())
  34. self.assertEqual(asn1Object['parameters'], der_encoder(rsa_kem_p))
  35. self.assertEqual(rfc5990.id_kem_rsa, rsa_kem_p['kem']['algorithm'])
  36. kem_rsa_p, rest = der_decoder(
  37. rsa_kem_p['kem']['parameters'],
  38. asn1Spec=rfc5280.algorithmIdentifierMap[rfc5990.id_kem_rsa])
  39. self.assertFalse(rest)
  40. self.assertTrue(kem_rsa_p.prettyPrint())
  41. self.assertEqual(
  42. rsa_kem_p['kem']['parameters'], der_encoder(kem_rsa_p))
  43. self.assertEqual(16, kem_rsa_p['keyLength'])
  44. self.assertEqual(
  45. rfc5990.id_kdf_kdf3, kem_rsa_p['keyDerivationFunction']['algorithm'])
  46. kdf_p, rest = der_decoder(
  47. kem_rsa_p['keyDerivationFunction']['parameters'],
  48. asn1Spec=rfc5280.algorithmIdentifierMap[rfc5990.id_kdf_kdf3])
  49. self.assertFalse(rest)
  50. self.assertTrue(kdf_p.prettyPrint())
  51. self.assertEqual(
  52. kem_rsa_p['keyDerivationFunction']['parameters'],
  53. der_encoder(kdf_p))
  54. def testOpenTypes(self):
  55. substrate = pem.readBase64fromText(self.pem_text)
  56. asn1Object, rest = der_decoder(
  57. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  58. self.assertFalse(rest)
  59. self.assertTrue(asn1Object.prettyPrint())
  60. self.assertEqual(substrate, der_encoder(asn1Object))
  61. self.assertEqual(rfc5990.id_rsa_kem, asn1Object['algorithm'])
  62. self.assertEqual(
  63. rfc5990.id_kem_rsa, asn1Object['parameters']['kem']['algorithm'])
  64. self.assertEqual(
  65. 16, asn1Object['parameters']['kem']['parameters']['keyLength'])
  66. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  67. if __name__ == '__main__':
  68. result = unittest.TextTestRunner(verbosity=2).run(suite)
  69. sys.exit(not result.wasSuccessful())