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 9import sys 10 11from pyasn1.codec.der import decoder as der_decoder 12from pyasn1.codec.der import encoder as der_encoder 13 14from pyasn1_modules import pem 15from pyasn1_modules import rfc5280 16from pyasn1_modules import rfc8226 17 18try: 19 import unittest2 as unittest 20except ImportError: 21 import unittest 22 23 24class JWTClaimConstraintsTestCase(unittest.TestCase): 25 jwtcc_pem_text = "MD2gBzAFFgNmb2+hMjAwMBkWA2ZvbzASDARmb28xDARmb28yDARmb28zMBMWA2JhcjAMDARiYXIxDARiYXIy" 26 27 def setUp(self): 28 self.asn1Spec = rfc8226.JWTClaimConstraints() 29 30 def testDerCodec(self): 31 substrate = pem.readBase64fromText(self.jwtcc_pem_text) 32 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 33 assert not rest 34 assert asn1Object.prettyPrint() 35 assert der_encoder.encode(asn1Object) == substrate 36 37 38class TNAuthorizationListTestCase(unittest.TestCase): 39 tnal_pem_text = "MCugBxYFYm9ndXOhEjAQFgo1NzE1NTUxMjEyAgIDFKIMFgo3MDM1NTUxMjEy" 40 41 def setUp(self): 42 self.asn1Spec = rfc8226.TNAuthorizationList() 43 44 def testDerCodec(self): 45 substrate = pem.readBase64fromText(self.tnal_pem_text) 46 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 47 assert not rest 48 assert asn1Object.prettyPrint() 49 assert der_encoder.encode(asn1Object) == substrate 50 51 52class CertificateOpenTypesTestCase(unittest.TestCase): 53 cert_pem_text = """\ 54MIICkTCCAhegAwIBAgIJAKWzVCgbsG4+MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT 55AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n 56dXMgQ0EwHhcNMTkwNzE4MTUwNzQ5WhcNMjAwNzE3MTUwNzQ5WjBxMQswCQYDVQQG 57EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xKDAmBgNVBAoTH0Zh 58a2UgVGVsZXBob25lIFNlcnZpY2UgUHJvdmlkZXIxGTAXBgNVBAMTEGZha2UuZXhh 59bXBsZS5jb20wdjAQBgcqhkjOPQIBBgUrgQQAIgNiAARLyLhnsvrS9WBY29tmN2LI 60CF/wuX4ohhUy3sxO0ynCplHHojpDg+tghGzusf0aLtMDu1II915O8YK5XVL+KZJD 61C82jybxWIKjjzX2qc5/O06joUttdEDzkTaD0kgbcXl6jgawwgakwCwYDVR0PBAQD 62AgeAMEIGCWCGSAGG+EIBDQQ1FjNUaGlzIGNlcnRpZmljYXRlIGNhbm5vdCBiZSB0 63cnVzdGVkIGZvciBhbnkgcHVycG9zZS4wHQYDVR0OBBYEFHOI3GpDt9dWsTAZxhcj 6496uyL2aIMB8GA1UdIwQYMBaAFPI12zQE2qVV8r1pA5mwYuziFQjBMBYGCCsGAQUF 65BwEaBAowCKAGFgRmYWtlMAoGCCqGSM49BAMDA2gAMGUCMQCy+qFhT7X1i18jcyIa 66Jkgz/tumrPsaBA2RihkooTEr4GbqC650Z4Cwt7+x2xZq37sCMFSM6fRueLyV5StG 67yEFWA6G95b/HbtPMTjLpPKtrOjhofc4LyVCDYhFhKzpvHh1qeA== 68""" 69 70 def setUp(self): 71 self.asn1Spec = rfc5280.Certificate() 72 73 def testDerCodec(self): 74 substrate = pem.readBase64fromText(self.cert_pem_text) 75 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 76 assert not rest 77 assert asn1Object.prettyPrint() 78 assert der_encoder.encode(asn1Object) == substrate 79 80 for extn in asn1Object['tbsCertificate']['extensions']: 81 if extn['extnID'] in rfc5280.certificateExtensionsMap.keys(): 82 extnValue, rest = der_decoder.decode(extn['extnValue'], 83 asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']]) 84 assert der_encoder.encode(extnValue) == extn['extnValue'] 85 86 if extn['extnID'] == rfc8226.id_pe_TNAuthList: 87 assert extnValue[0]['spc'] == 'fake' 88 89 90suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 91 92if __name__ == '__main__': 93 import sys 94 95 result = unittest.TextTestRunner(verbosity=2).run(suite) 96 sys.exit(not result.wasSuccessful()) 97