test_rfc5958.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc5958
  16. from pyasn1_modules import rfc8410
  17. class PrivateKeyTestCase(unittest.TestCase):
  18. priv_key_pem_text = """\
  19. MHICAQEwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC
  20. oB8wHQYKKoZIhvcNAQkJFDEPDA1DdXJkbGUgQ2hhaXJzgSEAGb9ECWmEzf6FQbrB
  21. Z9w7lshQhqowtrbLDFw4rXAxZuE=
  22. """
  23. def setUp(self):
  24. self.asn1Spec = rfc5958.PrivateKeyInfo()
  25. def testDerCodec(self):
  26. substrate = pem.readBase64fromText(self.priv_key_pem_text)
  27. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  28. self.assertFalse(rest)
  29. self.assertTrue(asn1Object.prettyPrint())
  30. self.assertEqual(
  31. rfc8410.id_Ed25519, asn1Object['privateKeyAlgorithm']['algorithm'])
  32. self.assertTrue(asn1Object['privateKey'].isValue)
  33. self.assertEqual(
  34. "0x0420d4ee", asn1Object['privateKey'].prettyPrint()[0:10])
  35. self.assertTrue(asn1Object['publicKey'].isValue)
  36. self.assertEqual(
  37. "1164575857", asn1Object['publicKey'].prettyPrint()[0:10])
  38. self.assertEqual(substrate, der_encoder(asn1Object))
  39. class PrivateKeyOpenTypesTestCase(unittest.TestCase):
  40. asymmetric_key_pkg_pem_text = """\
  41. MIGEBgpghkgBZQIBAk4FoHYwdDByAgEBMAUGAytlcAQiBCDU7nLb+RNYStW22PH3
  42. afitOv58KMvx1Pvgl6iPRHVYQqAfMB0GCiqGSIb3DQEJCRQxDwwNQ3VyZGxlIENo
  43. YWlyc4EhABm/RAlphM3+hUG6wWfcO5bIUIaqMLa2ywxcOK1wMWbh
  44. """
  45. def setUp(self):
  46. self.asn1Spec = rfc5652.ContentInfo()
  47. def testOpenTypes(self):
  48. substrate = pem.readBase64fromText(self.asymmetric_key_pkg_pem_text)
  49. asn1Object, rest = der_decoder(
  50. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  51. self.assertFalse(rest)
  52. self.assertTrue(asn1Object.prettyPrint())
  53. self.assertEqual(substrate, der_encoder(asn1Object))
  54. self.assertIn(
  55. rfc5958.id_ct_KP_aKeyPackage, rfc5652.cmsContentTypesMap)
  56. oneKey = asn1Object['content'][0]
  57. self.assertEqual(
  58. rfc8410.id_Ed25519, oneKey['privateKeyAlgorithm']['algorithm'])
  59. pkcs_9_at_friendlyName = univ.ObjectIdentifier('1.2.840.113549.1.9.9.20')
  60. self.assertEqual(
  61. pkcs_9_at_friendlyName, oneKey['attributes'][0]['attrType'])
  62. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  63. if __name__ == '__main__':
  64. result = unittest.TextTestRunner(verbosity=2).run(suite)
  65. sys.exit(not result.wasSuccessful())