test_rfc6187.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, Vigil Security, LLC
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc5280
  13. from pyasn1_modules import rfc6187
  14. class SSHClientCertificateTestCase(unittest.TestCase):
  15. cert_pem_text = """\
  16. MIICkDCCAhegAwIBAgIJAKWzVCgbsG5BMAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  17. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  18. dXMgQ0EwHhcNMTkxMDI0MTgyNjA3WhcNMjAxMDIzMTgyNjA3WjB0MQswCQYDVQQG
  19. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoTB0V4
  20. YW1wbGUxEDAOBgNVBAMTB0NoYXJsaWUxIjAgBgkqhkiG9w0BCQEWE2NoYXJsaWVA
  21. ZXhhbXBsZS5jb20wdjAQBgcqhkjOPQIBBgUrgQQAIgNiAARfr1XPl5S0A/BwTOm4
  22. /rO7mGVt2Tmfr3yvYnfN/ggMvyS3RiIXSsdzcAwzeqc907Jp7Dggab0PpaOKDOxD
  23. WoK0g6B8+kC/VMsU23mfShlb9et8qcR3A8gdU6g8uvSMahWjgakwgaYwCwYDVR0P
  24. BAQDAgeAMB0GA1UdDgQWBBQfwm5u0GoxiDcjhDt33UJYlvMPFTAfBgNVHSMEGDAW
  25. gBTyNds0BNqlVfK9aQOZsGLs4hUIwTATBgNVHSUEDDAKBggrBgEFBQcDFTBCBglg
  26. hkgBhvhCAQ0ENRYzVGhpcyBjZXJ0aWZpY2F0ZSBjYW5ub3QgYmUgdHJ1c3RlZCBm
  27. b3IgYW55IHB1cnBvc2UuMAoGCCqGSM49BAMDA2cAMGQCMGEme38A3k8q4RGSEs2D
  28. ThQQOQz3TBJrIW8zr92S8e8BNPkRcQDR+C72TEhL/qoPCQIwGpGaC4ERiUypETkC
  29. voNP0ODFhhlpFo6lwVHd8Gu+6hShC2PKdAfs4QFDS9ZKgQeZ
  30. """
  31. def setUp(self):
  32. self.asn1Spec = rfc5280.Certificate()
  33. def testDerCodec(self):
  34. ssh_eku_oids = [
  35. rfc6187.id_kp_secureShellClient,
  36. rfc6187.id_kp_secureShellServer,
  37. ]
  38. substrate = pem.readBase64fromText(self.cert_pem_text)
  39. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  40. self.assertFalse(rest)
  41. self.assertTrue(asn1Object.prettyPrint())
  42. self.assertEqual(substrate, der_encoder(asn1Object))
  43. count = 0
  44. for extn in asn1Object['tbsCertificate']['extensions']:
  45. if extn['extnID'] == rfc5280.id_ce_extKeyUsage:
  46. extnValue, rest = der_decoder(
  47. extn['extnValue'], asn1Spec=rfc5280.ExtKeyUsageSyntax())
  48. for oid in extnValue:
  49. if oid in ssh_eku_oids:
  50. count += 1
  51. self.assertEqual(1, count)
  52. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  53. if __name__ == '__main__':
  54. result = unittest.TextTestRunner(verbosity=2).run(suite)
  55. sys.exit(not result.wasSuccessful())