test_rfc3770.py 3.7 KB

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