test_rfc6487.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 rfc6487
  14. class CertificateWithManifestTestCase(unittest.TestCase):
  15. rpki_cert_pem_text = """\
  16. MIIGCTCCBPGgAwIBAgICKJgwDQYJKoZIhvcNAQELBQAwRjERMA8GA1UEAxMIQTkwREM1QkUx
  17. MTAvBgNVBAUTKDBDRkNFNzc4NTdGQ0YwMUYzOUQ5OUE2MkI0QUE2MkU2MTU5RTc2RjgwHhcN
  18. MTkwODA2MDQwMzIyWhcNMjAxMDMxMDAwMDAwWjBGMREwDwYDVQQDEwhBOTFEMTY5MTExMC8G
  19. A1UEBRMoREMwNEFGMTk4Qzk3RjI1ODJGMTVBRERFRUU3QzY4MjYxMUNBREE1MTCCASIwDQYJ
  20. KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMksR6bPbZFpxlXID/2dhYFuS11agb6ACDUFJpII
  21. 41uw65tFIPT+Y4laccnYRcWPWMTvHLyj0ggU+bc2zJCTYfmGD/GW/Q3WW0A3niBCdXDfkrp2
  22. DXvSTASJ5+wtVb+AE74C4Mr3UiMOXhJre1rRd5Lq7o6+TEKbVkmUrmTlbsz2Vs2F4//t5sCr
  23. WjAVP9D5jUBGH2MInbleBP1Bwf+kIxD16OKftRb/vGLzk1UhLsbq22GGE0vZ2hnJP3CbyXkN
  24. dLBraErzvyCnqYF7/yA0JL0KWRDwr7a9y37s8O3xOxhA/dL8hLZ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ZXQvbm90aWZpY2F0aW9uLnhtbDArBggrBgEFBQcBBwEB
  39. /wQcMBowGAQCAAEwEgMEAdQI5gMEAdQI/gMEAdRcZjANBgkqhkiG9w0BAQsFAAOCAQEAGvJ+
  40. s7VgIZk8LDSz6uvsyX80KzZgaqMF7sMsqln0eo5KiGGBHjwvZuiDf46xbNseWW2nwAHmjLda
  41. osCbcTGVu0JzFYBdkimgyHiq2l8yEchh5BUXr8x4CQIxwGEZEOlEp5mRa/AfHVEfDeMm7mob
  42. eiCfyTC8q8KH9Tb/rY192kBe+n9MuRyn7TkimV5eYMdwWMyT/VSBCQzzfJ0r+S9o0rBYWH9k
  43. HDFd3u1ztO8WGjH/LOehoO30xsm52kbxZjc4SJWubgBgxTMIWyjPHbKqCF44NwYev/6eFcOC
  44. +KTEQ/hydcURm3YtX7EZLDtksWB2me576J8opeLsbNeNgzfJpg==
  45. """
  46. def setUp(self):
  47. self.asn1Spec = rfc5280.Certificate()
  48. def testDerCodec(self):
  49. access_methods = [
  50. rfc6487.id_ad_rpkiManifest,
  51. rfc6487.id_ad_signedObject,
  52. ]
  53. substrate = pem.readBase64fromText(self.rpki_cert_pem_text)
  54. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  55. self.assertFalse(rest)
  56. self.assertTrue(asn1Object.prettyPrint())
  57. self.assertEqual(substrate, der_encoder(asn1Object))
  58. count = 0
  59. for extn in asn1Object['tbsCertificate']['extensions']:
  60. if extn['extnID'] == rfc5280.id_pe_subjectInfoAccess:
  61. extnValue, rest = der_decoder(
  62. extn['extnValue'], asn1Spec=rfc5280.SubjectInfoAccessSyntax())
  63. for ad in extnValue:
  64. if ad['accessMethod'] in access_methods:
  65. uri = ad['accessLocation']['uniformResourceIdentifier']
  66. self.assertIn('rpki.apnic.net', uri)
  67. count += 1
  68. self.assertEqual(1, count)
  69. class CertificateWithSignedObjectTestCase(unittest.TestCase):
  70. rpki_cert_pem_text = """\
  71. MIIEuDCCA6CgAwIBAgICBhgwD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87. BgEFBQcBCwR+MHwwSwYIKwYBBQUHMAuGP3JzeW5jOi8vY2EucmcubmV0L3Jwa2kv
  88. UkduZXQtT1UvVzRQZGg5NmF4OGJqUzRkOTlRR2lzU01LZ2JRLnJvYTAtBggrBgEF
  89. BQcwDYYhaHR0cHM6Ly9jYS5yZy5uZXQvcnJkcC9ub3RpZnkueG1sMB8GCCsGAQUF
  90. BwEHAQH/BBAwDjAMBAIAATAGAwQAkxwtMA0GCSqGSIb3DQEBCwUAA4IBAQCoYaCd
  91. 17R3o7xul5BWgk8SXItdIDoDb7zxVqs/gnzl9i5gdDd0IWIy4gGW32EjsTUXsi+G
  92. 1gyv7aWYFQNlR7kvBgfHyPPp2rkFIj9/KK1VygG3FFMaO1JBDB8UOU+tRbV6xGcf
  93. IYCk5bH6H9BtkPm2kiczVdjCFIB5krMy+DMf3x1F7/G+5f+ZmUG3b93GfUmzgxw9
  94. IjlQMyt35h3rgOK6EjpOJgUA1jUWNTpPsR/xzA0HaDlbW38ue02SNluztrsXxJSr
  95. 8XwXhHPUzmlqg89Mb5iem3WZ5lkbr6lteO+ZocYtLPyOHhNmXWgop764K4JQaf46
  96. WYtY4rWNeHcfgNTz
  97. """
  98. def setUp(self):
  99. self.asn1Spec = rfc5280.Certificate()
  100. def testDerCodec(self):
  101. access_methods = [
  102. rfc6487.id_ad_rpkiManifest,
  103. rfc6487.id_ad_signedObject,
  104. ]
  105. substrate = pem.readBase64fromText(self.rpki_cert_pem_text)
  106. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  107. self.assertFalse(rest)
  108. self.assertTrue(asn1Object.prettyPrint())
  109. self.assertEqual(substrate, der_encoder(asn1Object))
  110. count = 0
  111. for extn in asn1Object['tbsCertificate']['extensions']:
  112. if extn['extnID'] == rfc5280.id_pe_subjectInfoAccess:
  113. extnValue, rest = der_decoder(
  114. extn['extnValue'], asn1Spec=rfc5280.SubjectInfoAccessSyntax())
  115. for ad in extnValue:
  116. if ad['accessMethod'] in access_methods:
  117. uri = ad['accessLocation']['uniformResourceIdentifier']
  118. self.assertIn('ca.rg.net', uri)
  119. count += 1
  120. self.assertEqual(1, count)
  121. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  122. if __name__ == '__main__':
  123. result = unittest.TextTestRunner(verbosity=2).run(suite)
  124. sys.exit(not result.wasSuccessful())