test_rfc5480.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class ECCertTestCase(unittest.TestCase):
  16. digicert_ec_cert_pem_text = """\
  17. MIIDrDCCApSgAwIBAgIQCssoukZe5TkIdnRw883GEjANBgkqhkiG9w0BAQwFADBh
  18. MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
  19. d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
  20. QTAeFw0xMzAzMDgxMjAwMDBaFw0yMzAzMDgxMjAwMDBaMEwxCzAJBgNVBAYTAlVT
  21. MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxJjAkBgNVBAMTHURpZ2lDZXJ0IEVDQyBT
  22. ZWN1cmUgU2VydmVyIENBMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE4ghC6nfYJN6g
  23. LGSkE85AnCNyqQIKDjc/ITa4jVMU9tWRlUvzlgKNcR7E2Munn17voOZ/WpIRllNv
  24. 68DLP679Wz9HJOeaBy6Wvqgvu1cYr3GkvXg6HuhbPGtkESvMNCuMo4IBITCCAR0w
  25. EgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwNAYIKwYBBQUHAQEE
  26. KDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5jb20wQgYDVR0f
  27. BDswOTA3oDWgM4YxaHR0cDovL2NybDMuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0R2xv
  28. YmFsUm9vdENBLmNybDA9BgNVHSAENjA0MDIGBFUdIAAwKjAoBggrBgEFBQcCARYc
  29. aHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzAdBgNVHQ4EFgQUo53mH/naOU/A
  30. buiRy5Wl2jHiCp8wHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUwDQYJ
  31. KoZIhvcNAQEMBQADggEBAMeKoENL7HTJxavVHzA1Nm6YVntIrAVjrnuaVyRXzG/6
  32. 3qttnMe2uuzO58pzZNvfBDcKAEmzP58mrZGMIOgfiA4q+2Y3yDDo0sIkp0VILeoB
  33. UEoxlBPfjV/aKrtJPGHzecicZpIalir0ezZYoyxBEHQa0+1IttK7igZFcTMQMHp6
  34. mCHdJLnsnLWSB62DxsRq+HfmNb4TDydkskO/g+l3VtsIh5RHFPVfKK+jaEyDj2D3
  35. loB5hWp2Jp2VDCADjT7ueihlZGak2YPqmXTNbk19HOuNssWvFhtOyPNV6og4ETQd
  36. Ea8/B6hPatJ0ES8q/HO3X8IVQwVs1n3aAr0im0/T+Xc=
  37. """
  38. def setUp(self):
  39. self.asn1Spec = rfc5280.Certificate()
  40. def testDerCodec(self):
  41. substrate = pem.readBase64fromText(self.digicert_ec_cert_pem_text)
  42. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  43. self.assertFalse(rest)
  44. self.assertTrue(asn1Object.prettyPrint())
  45. self.assertEqual(substrate, der_encoder(asn1Object))
  46. algid = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  47. self.assertEqual(rfc5480.id_ecPublicKey, algid['algorithm'])
  48. param, rest = der_decoder(algid['parameters'], asn1Spec=rfc5480.ECParameters())
  49. self.assertTrue(param.prettyPrint())
  50. self.assertEqual(rfc5480.secp384r1, param['namedCurve'])
  51. def testOpenTypes(self):
  52. substrate = pem.readBase64fromText(self.digicert_ec_cert_pem_text)
  53. asn1Object, rest = der_decoder(substrate,
  54. asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  55. self.assertFalse(rest)
  56. self.assertTrue(asn1Object.prettyPrint())
  57. self.assertEqual(substrate, der_encoder(asn1Object))
  58. spki_alg = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  59. self.assertEqual(rfc5480.id_ecPublicKey, spki_alg['algorithm'])
  60. self.assertEqual(rfc5480.secp384r1, spki_alg['parameters']['namedCurve'])
  61. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  62. if __name__ == '__main__':
  63. result = unittest.TextTestRunner(verbosity=2).run(suite)
  64. sys.exit(not result.wasSuccessful())