test_rfc5639.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 rfc5480
  15. from pyasn1_modules import rfc5639
  16. class ECCertTestCase(unittest.TestCase):
  17. brainpool_ec_cert_pem_text = """\
  18. MIIB0jCCAXmgAwIBAgITPUXQAyl3ZE5iAHYGZYSp1FkqzTAKBggqhkjOPQQDAjA/
  19. MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExEDAOBgNVBAcMB0hlcm5kb24xETAP
  20. BgNVBAoMCEJvZ3VzIENBMB4XDTE5MTIwOTIxNDM0NFoXDTIxMTIwODIxNDM0NFow
  21. PzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQHDAdIZXJuZG9uMREw
  22. DwYDVQQKDAhCb2d1cyBDQTBaMBQGByqGSM49AgEGCSskAwMCCAEBBwNCAASBvvOk
  23. WNZlGAf5O3V94qgC3IUUR/6uxFxT6To0ULFmrVVndXiVP6DE5h5QHGXPwKfO+4Yt
  24. n0OVnGHp68dPS37Go1MwUTAdBgNVHQ4EFgQUiRFFVcdn6Fp9+sEP1GVRtwl9XgIw
  25. HwYDVR0jBBgwFoAUiRFFVcdn6Fp9+sEP1GVRtwl9XgIwDwYDVR0TAQH/BAUwAwEB
  26. /zAKBggqhkjOPQQDAgNHADBEAiB3d+P64Dh5YzwyM++uOL6zHUeLbNpW2sF1eJsm
  27. l3M5uQIgGxpbAXOt/o1xtyhEGLNUBE7ObgQpm7tHMMQGUHo4wV8=
  28. """
  29. def setUp(self):
  30. self.asn1Spec = rfc5280.Certificate()
  31. def testDerCodec(self):
  32. substrate = pem.readBase64fromText(self.brainpool_ec_cert_pem_text)
  33. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  34. self.assertFalse(rest)
  35. self.assertTrue(asn1Object.prettyPrint())
  36. self.assertEqual(substrate, der_encoder(asn1Object))
  37. spki = asn1Object['tbsCertificate']['subjectPublicKeyInfo']
  38. algid = spki['algorithm']
  39. self.assertEqual(rfc5480.id_ecPublicKey, algid['algorithm'])
  40. param, rest = der_decoder(
  41. algid['parameters'], asn1Spec=rfc5480.ECParameters())
  42. self.assertFalse(rest)
  43. self.assertTrue(param.prettyPrint())
  44. self.assertEqual(algid['parameters'], der_encoder(param))
  45. self.assertEqual(rfc5639.brainpoolP256r1, param['namedCurve'])
  46. def testOpenTypes(self):
  47. substrate = pem.readBase64fromText(self.brainpool_ec_cert_pem_text)
  48. asn1Object, rest = der_decoder(
  49. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  50. self.assertFalse(rest)
  51. self.assertTrue(asn1Object.prettyPrint())
  52. self.assertEqual(substrate, der_encoder(asn1Object))
  53. spki = asn1Object['tbsCertificate']['subjectPublicKeyInfo']
  54. algid = spki['algorithm']
  55. self.assertEqual(rfc5480.id_ecPublicKey, algid['algorithm'])
  56. self.assertEqual(
  57. rfc5639.brainpoolP256r1, algid['parameters']['namedCurve'])
  58. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  59. if __name__ == '__main__':
  60. result = unittest.TextTestRunner(verbosity=2).run(suite)
  61. sys.exit(not result.wasSuccessful())