test_rfc7914.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 rfc5958
  15. from pyasn1_modules import rfc7914
  16. from pyasn1_modules import rfc8018
  17. # From RFC 7914, Section 13
  18. class MultiprimeRSAPrivateKeyTestCase(unittest.TestCase):
  19. pem_text = """\
  20. MIHiME0GCSqGSIb3DQEFDTBAMB8GCSsGAQQB2kcECzASBAVNb3VzZQIDEAAAAgEI
  21. AgEBMB0GCWCGSAFlAwQBKgQQyYmguHMsOwzGMPoyObk/JgSBkJb47EWd5iAqJlyy
  22. +ni5ftd6gZgOPaLQClL7mEZc2KQay0VhjZm/7MbBUNbqOAXNM6OGebXxVp6sHUAL
  23. iBGY/Dls7B1TsWeGObE0sS1MXEpuREuloZjcsNVcNXWPlLdZtkSH6uwWzR0PyG/Z
  24. +ZXfNodZtd/voKlvLOw5B3opGIFaLkbtLZQwMiGtl42AS89lZg==
  25. """
  26. def setUp(self):
  27. self.asn1Spec = rfc5958.EncryptedPrivateKeyInfo()
  28. def testDerCodec(self):
  29. substrate = pem.readBase64fromText(self.pem_text)
  30. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  31. self.assertFalse(rest)
  32. self.assertTrue(asn1Object.prettyPrint())
  33. self.assertEqual(substrate, der_encoder(asn1Object))
  34. ea = asn1Object['encryptionAlgorithm']
  35. self.assertEqual(rfc8018.id_PBES2, ea['algorithm'])
  36. self.assertIn(ea['algorithm'], rfc5280.algorithmIdentifierMap)
  37. params, rest = der_decoder(
  38. ea['parameters'],
  39. asn1Spec=rfc5280.algorithmIdentifierMap[ea['algorithm']])
  40. self.assertFalse(rest)
  41. self.assertTrue(params.prettyPrint())
  42. self.assertEqual(ea['parameters'], der_encoder(params))
  43. kdf = params['keyDerivationFunc']
  44. self.assertEqual(rfc7914.id_scrypt, kdf['algorithm'])
  45. self.assertIn(kdf['algorithm'], rfc5280.algorithmIdentifierMap)
  46. kdfp, rest = der_decoder(
  47. kdf['parameters'],
  48. asn1Spec=rfc5280.algorithmIdentifierMap[kdf['algorithm']])
  49. self.assertFalse(rest)
  50. self.assertTrue(kdfp.prettyPrint())
  51. self.assertTrue(kdf['parameters'], der_encoder(kdfp))
  52. self.assertEqual(1048576, kdfp['costParameter'])
  53. def testOpenTypes(self):
  54. substrate = pem.readBase64fromText(self.pem_text)
  55. asn1Object, rest = der_decoder(
  56. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  57. self.assertFalse(rest)
  58. self.assertTrue(asn1Object.prettyPrint())
  59. self.assertEqual(substrate, der_encoder(asn1Object))
  60. ea = asn1Object['encryptionAlgorithm']
  61. self.assertEqual(rfc8018.id_PBES2, ea['algorithm'])
  62. params = asn1Object['encryptionAlgorithm']['parameters']
  63. self.assertEqual(
  64. rfc7914.id_scrypt, params['keyDerivationFunc']['algorithm'])
  65. kdfp = params['keyDerivationFunc']['parameters']
  66. self.assertEqual(1048576, kdfp['costParameter'])
  67. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  68. if __name__ == '__main__':
  69. result = unittest.TextTestRunner(verbosity=2).run(suite)
  70. sys.exit(not result.wasSuccessful())