test_rfc4334.py 3.2 KB

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