test_rfc3779.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 import decoder as der_decoder
  10. from pyasn1.codec.der import encoder as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc5280
  13. from pyasn1_modules import rfc3779
  14. class CertificateExtnTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIECjCCAvKgAwIBAgICAMkwDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAxMLcmlw
  17. ZS1uY2MtdGEwIBcNMTcxMTI4MTQzOTU1WhgPMjExNzExMjgxNDM5NTVaMBYxFDAS
  18. BgNVBAMTC3JpcGUtbmNjLXRhMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
  19. AQEA0URYSGqUz2myBsOzeW1jQ6NsxNvlLMyhWknvnl8NiBCs/T/S2XuNKQNZ+wBZ
  20. xIgPPV2pFBFeQAvoH/WK83HwA26V2siwm/MY2nKZ+Olw+wlpzlZ1p3Ipj2eNcKrm
  21. it8BwBC8xImzuCGaV0jkRB0GZ0hoH6Ml03umLprRsn6v0xOP0+l6Qc1ZHMFVFb38
  22. 5IQ7FQQTcVIxrdeMsoyJq9eMkE6DoclHhF/NlSllXubASQ9KUWqJ0+Ot3QCXr4LX
  23. ECMfkpkVR2TZT+v5v658bHVs6ZxRD1b6Uk1uQKAyHUbn/tXvP8lrjAibGzVsXDT2
  24. L0x4Edx+QdixPgOji3gBMyL2VwIDAQABo4IBXjCCAVowHQYDVR0OBBYEFOhVKx/W
  25. 0aT35ATG2OVoDR68Fj/DMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEG
  26. MIGxBggrBgEFBQcBCwSBpDCBoTA8BggrBgEFBQcwCoYwcnN5bmM6Ly9ycGtpLnJp
  27. cGUubmV0L3JlcG9zaXRvcnkvcmlwZS1uY2MtdGEubWZ0MDIGCCsGAQUFBzANhiZo
  28. dHRwczovL3JyZHAucmlwZS5uZXQvbm90aWZpY2F0aW9uLnhtbDAtBggrBgEFBQcw
  29. BYYhcnN5bmM6Ly9ycGtpLnJpcGUubmV0L3JlcG9zaXRvcnkvMBgGA1UdIAEB/wQO
  30. MAwwCgYIKwYBBQUHDgIwJwYIKwYBBQUHAQcBAf8EGDAWMAkEAgABMAMDAQAwCQQC
  31. AAIwAwMBADAhBggrBgEFBQcBCAEB/wQSMBCgDjAMMAoCAQACBQD/////MA0GCSqG
  32. SIb3DQEBCwUAA4IBAQAVgJjrZ3wFppC8Yk8D2xgzwSeWVT2vtYq96CQQsjaKb8nb
  33. eVz3DwcS3a7RIsevrNVGo43k3AGymg1ki+AWJjvHvJ+tSzCbn5+X6Z7AfYTf2g37
  34. xINVDHru0PTQUargSMBAz/MBNpFG8KThtT7WbJrK4+f/lvx0m8QOlYm2a17iXS3A
  35. GQJ6RHcq9ADscqGdumxmMMDjwED26bGaYdmru1hNIpwF//jVM/eRjBFoPHKFlx0k
  36. Ld/yoCQNmx1kW+xANx4uyWxi/DYgSV7Oynq+C60OucW+d8tIhkblh8+YfrmukJds
  37. V+vo2L72yerdbsP9xjqvhZrLKfsLZjYK4SdYYthi
  38. """
  39. def setUp(self):
  40. self.asn1Spec = rfc5280.Certificate()
  41. def testDerCodec(self):
  42. substrate = pem.readBase64fromText(self.pem_text)
  43. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  44. self.assertFalse(rest)
  45. self.assertTrue(asn1Object.prettyPrint())
  46. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  47. extn_list = []
  48. for extn in asn1Object['tbsCertificate']['extensions']:
  49. extn_list.append(extn['extnID'])
  50. if extn['extnID'] == rfc3779.id_pe_ipAddrBlocks:
  51. s = extn['extnValue']
  52. addr_blocks, rest = der_decoder.decode(s, rfc3779.IPAddrBlocks())
  53. self.assertFalse(rest)
  54. self.assertTrue(addr_blocks.prettyPrint())
  55. self.assertEqual(s, der_encoder.encode(addr_blocks))
  56. if extn['extnID'] == rfc3779.id_pe_autonomousSysIds:
  57. s = extn['extnValue']
  58. as_ids, rest = der_decoder.decode(s, rfc3779.ASIdentifiers())
  59. self.assertFalse(rest)
  60. self.assertTrue(as_ids.prettyPrint())
  61. self.assertEqual(s, der_encoder.encode(as_ids))
  62. self.assertIn(rfc3779.id_pe_ipAddrBlocks, extn_list)
  63. self.assertIn(rfc3779.id_pe_autonomousSysIds, extn_list)
  64. def testExtensionsMap(self):
  65. substrate = pem.readBase64fromText(self.pem_text)
  66. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  67. self.assertFalse(rest)
  68. self.assertTrue(asn1Object.prettyPrint())
  69. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  70. for extn in asn1Object['tbsCertificate']['extensions']:
  71. if (extn['extnID'] == rfc3779.id_pe_ipAddrBlocks or
  72. extn['extnID'] == rfc3779.id_pe_autonomousSysIds):
  73. extnValue, rest = der_decoder.decode(
  74. extn['extnValue'],
  75. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  76. self.assertEqual(extn['extnValue'], der_encoder.encode(extnValue))
  77. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  78. if __name__ == '__main__':
  79. result = unittest.TextTestRunner(verbosity=2).run(suite)
  80. sys.exit(not result.wasSuccessful())