test_rfc4334.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 rfc4334
  15. class CertificateTestCase(unittest.TestCase):
  16. cert_pem_text = """\
  17. MIICqzCCAjCgAwIBAgIJAKWzVCgbsG4/MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  18. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  19. dXMgQ0EwHhcNMTkwNzE5MTk0MjQ3WhcNMjAwNzE4MTk0MjQ3WjBjMQswCQYDVQQG
  20. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xGzAZBgNVBAoTElZp
  21. Z2lsIFNlY3VyaXR5IExMQzEYMBYGA1UEAxMPZWFwLmV4YW1wbGUuY29tMHYwEAYH
  22. KoZIzj0CAQYFK4EEACIDYgAEMMbnIp2BUbuyMgH9HhNHrh7VBy7ql2lBjGRSsefR
  23. Wa7+vCWs4uviW6On4eem5YoP9/UdO7DaIL+/J9/3DJHERI17oFxn+YWiE4JwXofy
  24. QwfSu3cncVNMqpiDjEkUGGvBo4HTMIHQMAsGA1UdDwQEAwIHgDBCBglghkgBhvhC
  25. AQ0ENRYzVGhpcyBjZXJ0aWZpY2F0ZSBjYW5ub3QgYmUgdHJ1c3RlZCBmb3IgYW55
  26. IHB1cnBvc2UuMB0GA1UdDgQWBBSDjPGr7M742rsE4oQGwBvGvllZ+zAfBgNVHSME
  27. GDAWgBTyNds0BNqlVfK9aQOZsGLs4hUIwTAeBggrBgEFBQcBDQQSMBAEB0V4YW1w
  28. bGUEBUJvZ3VzMB0GA1UdJQQWMBQGCCsGAQUFBwMOBggrBgEFBQcDDTAKBggqhkjO
  29. PQQDAwNpADBmAjEAmCPZnnlUQOKlcOIIOgFrRCkOqO0ESs+dobYwAc2rFCBtQyP7
  30. C3N00xkX8WZZpiAZAjEAi1Z5+nGbJg5eJTc8fwudutN/HNwJEIS6mHds9kfcy26x
  31. DAlVlhox680Jxy5J8Pkx
  32. """
  33. def setUp(self):
  34. self.asn1Spec = rfc5280.Certificate()
  35. def testDerCodec(self):
  36. substrate = pem.readBase64fromText(self.cert_pem_text)
  37. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  38. self.assertFalse(rest)
  39. self.assertTrue(asn1Object.prettyPrint())
  40. self.assertEqual(substrate, der_encoder(asn1Object))
  41. def testOpenTypes(self):
  42. substrate = pem.readBase64fromText(self.cert_pem_text)
  43. asn1Object, rest = der_decoder(
  44. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  45. self.assertFalse(rest)
  46. self.assertTrue(asn1Object.prettyPrint())
  47. self.assertEqual(substrate, der_encoder(asn1Object))
  48. extn_list = []
  49. for extn in asn1Object['tbsCertificate']['extensions']:
  50. extn_list.append(extn['extnID'])
  51. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  52. extnValue, rest = der_decoder(
  53. extn['extnValue'],
  54. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  55. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  56. if extn['extnID'] == rfc4334.id_pe_wlanSSID:
  57. self.assertIn(b'Example', extnValue)
  58. if extn['extnID'] == rfc5280.id_ce_extKeyUsage:
  59. self.assertIn(rfc4334.id_kp_eapOverLAN, extnValue)
  60. self.assertIn(rfc4334.id_kp_eapOverPPP, extnValue)
  61. self.assertIn(rfc4334.id_pe_wlanSSID, extn_list)
  62. self.assertIn(rfc5280.id_ce_extKeyUsage, extn_list)
  63. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  64. if __name__ == '__main__':
  65. unittest.TextTestRunner(verbosity=2).run(suite)