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# 7import sys 8 9from pyasn1.codec.der.decoder import decode as der_decode 10from pyasn1.codec.der.encoder import encode as der_encode 11 12from pyasn1_modules import pem 13from pyasn1_modules import rfc5280 14from pyasn1_modules import rfc6487 15 16try: 17 import unittest2 as unittest 18except ImportError: 19 import unittest 20 21 22class CertificateWithManifestTestCase(unittest.TestCase): 23 rpki_cert_pem_text = """\ 24MIIGCTCCBPGgAwIBAgICKJgwDQYJKoZIhvcNAQELBQAwRjERMA8GA1UEAxMIQTkwREM1QkUx 25MTAvBgNVBAUTKDBDRkNFNzc4NTdGQ0YwMUYzOUQ5OUE2MkI0QUE2MkU2MTU5RTc2RjgwHhcN 26MTkwODA2MDQwMzIyWhcNMjAxMDMxMDAwMDAwWjBGMREwDwYDVQQDEwhBOTFEMTY5MTExMC8G 27A1UEBRMoREMwNEFGMTk4Qzk3RjI1ODJGMTVBRERFRUU3QzY4MjYxMUNBREE1MTCCASIwDQYJ 28KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMksR6bPbZFpxlXID/2dhYFuS11agb6ACDUFJpII 2941uw65tFIPT+Y4laccnYRcWPWMTvHLyj0ggU+bc2zJCTYfmGD/GW/Q3WW0A3niBCdXDfkrp2 30DXvSTASJ5+wtVb+AE74C4Mr3UiMOXhJre1rRd5Lq7o6+TEKbVkmUrmTlbsz2Vs2F4//t5sCr 31WjAVP9D5jUBGH2MInbleBP1Bwf+kIxD16OKftRb/vGLzk1UhLsbq22GGE0vZ2hnJP3CbyXkN 32dLBraErzvyCnqYF7/yA0JL0KWRDwr7a9y37s8O3xOxhA/dL8hLZXllzJmoxvxHmq8D+5CjHv 332/EmH8ODGm2aAzcCAwEAAaOCAv8wggL7MB0GA1UdDgQWBBTcBK8ZjJfyWC8Vrd7ufGgmEcra 34UTAfBgNVHSMEGDAWgBQM/Od4V/zwHznZmmK0qmLmFZ52+DA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NseWW2nwAHmjLda 49osCbcTGVu0JzFYBdkimgyHiq2l8yEchh5BUXr8x4CQIxwGEZEOlEp5mRa/AfHVEfDeMm7mob 50eiCfyTC8q8KH9Tb/rY192kBe+n9MuRyn7TkimV5eYMdwWMyT/VSBCQzzfJ0r+S9o0rBYWH9k 51HDFd3u1ztO8WGjH/LOehoO30xsm52kbxZjc4SJWubgBgxTMIWyjPHbKqCF44NwYev/6eFcOC 52+KTEQ/hydcURm3YtX7EZLDtksWB2me576J8opeLsbNeNgzfJpg== 53""" 54 55 def setUp(self): 56 self.asn1Spec = rfc5280.Certificate() 57 58 def testDerCodec(self): 59 access_methods = [ 60 rfc6487.id_ad_rpkiManifest, 61 rfc6487.id_ad_signedObject, 62 ] 63 64 substrate = pem.readBase64fromText(self.rpki_cert_pem_text) 65 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 66 assert not rest 67 assert asn1Object.prettyPrint() 68 assert der_encode(asn1Object) == substrate 69 70 count = 0 71 for extn in asn1Object['tbsCertificate']['extensions']: 72 if extn['extnID'] == rfc5280.id_pe_subjectInfoAccess: 73 extnValue, rest = der_decode(extn['extnValue'], 74 asn1Spec=rfc5280.SubjectInfoAccessSyntax()) 75 for ad in extnValue: 76 if ad['accessMethod'] in access_methods: 77 uri = ad['accessLocation']['uniformResourceIdentifier'] 78 assert 'rpki.apnic.net' in uri 79 count += 1 80 81 assert count == 1 82 83class CertificateWithSignedObjectTestCase(unittest.TestCase): 84 rpki_cert_pem_text = """\ 85MIIEuDCCA6CgAwIBAgICBhgwDQYJKoZIhvcNAQELBQAwMzExMC8GA1UEAxMoNmQ2 86ZmJmYTk3NTNkYjhkODQ2NDMzZGI1MzUxZDlhOWVjMDdjOTZiZDAeFw0xOTA4MjAw 87MDQ5MjlaFw0yMDA3MDEwMDAwMDBaMDMxMTAvBgNVBAMTKDVCODNERDg3REU5QUM3 88QzZFMzRCODc3REY1MDFBMkIxMjMwQTgxQjQwggEiMA0GCSqGSIb3DQEBA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 10517R3o7xul5BWgk8SXItdIDoDb7zxVqs/gnzl9i5gdDd0IWIy4gGW32EjsTUXsi+G 1061gyv7aWYFQNlR7kvBgfHyPPp2rkFIj9/KK1VygG3FFMaO1JBDB8UOU+tRbV6xGcf 107IYCk5bH6H9BtkPm2kiczVdjCFIB5krMy+DMf3x1F7/G+5f+ZmUG3b93GfUmzgxw9 108IjlQMyt35h3rgOK6EjpOJgUA1jUWNTpPsR/xzA0HaDlbW38ue02SNluztrsXxJSr 1098XwXhHPUzmlqg89Mb5iem3WZ5lkbr6lteO+ZocYtLPyOHhNmXWgop764K4JQaf46 110WYtY4rWNeHcfgNTz 111""" 112 113 def setUp(self): 114 self.asn1Spec = rfc5280.Certificate() 115 116 def testDerCodec(self): 117 access_methods = [ 118 rfc6487.id_ad_rpkiManifest, 119 rfc6487.id_ad_signedObject, 120 ] 121 122 substrate = pem.readBase64fromText(self.rpki_cert_pem_text) 123 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 124 assert not rest 125 assert asn1Object.prettyPrint() 126 assert der_encode(asn1Object) == substrate 127 128 count = 0 129 for extn in asn1Object['tbsCertificate']['extensions']: 130 if extn['extnID'] == rfc5280.id_pe_subjectInfoAccess: 131 extnValue, rest = der_decode(extn['extnValue'], 132 asn1Spec=rfc5280.SubjectInfoAccessSyntax()) 133 for ad in extnValue: 134 if ad['accessMethod'] in access_methods: 135 uri = ad['accessLocation']['uniformResourceIdentifier'] 136 assert 'ca.rg.net' in uri 137 count += 1 138 139 assert count == 1 140 141 142suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 143 144if __name__ == '__main__': 145 import sys 146 147 result = unittest.TextTestRunner(verbosity=2).run(suite) 148 sys.exit(not result.wasSuccessful()) 149