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.decoder import decode as der_decode 12from pyasn1.codec.der.encoder import encode as der_encode 13 14from pyasn1_modules import pem 15from pyasn1_modules import rfc5280 16from pyasn1_modules import rfc7773 17 18try: 19 import unittest2 as unittest 20 21except ImportError: 22 import unittest 23 24 25class AuthenticationContextExtnTestCase(unittest.TestCase): 26 pem_text = """\ 27MIIMUjCCCzqgAwIBAgIQevDaX+wRYAlpUgjTYjCCRjANBgkqhkiG9w0BAQsFADCBuDELMAkGA1UE 28BhMCU0UxNTAzBgNVBAoTLERldiBURVNUIENBIG9yZyBBQiAoTk9UIEEgUkVBTCBPUkdBTklaQVRJ 29T04pMSAwHgYDVQQLExdDZW50cmFsIFNpZ25pbmcgU2VydmljZTEVMBMGA1UEBRMMQTEyMzQ1Ni03 30ODkwMTkwNwYDVQQDEzBDZW50cmFsIFNpZ25pbmcgQ0EwMDEgLSBFSUQgMi4wIERldiBURVNUIFNl 31cnZpY2UwHhcNMTkxMDA5MDc0ODI2WhcNMjAxMDA5MDc0ODI2WjBgMRUwEwYDVQQFEwwxODg4MDMw 32OTkzNjgxCzAJBgNVBAYTAlNFMQ0wCwYDVQQqEwRBZ2RhMRcwFQYDVQQDEw5BZ2RhIEFuZGVyc3Nv 33bjESMBAGA1UEBBMJQW5kZXJzc29uMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjl1H 347vveI/EUaF9z6EiL/AmTHDbpLAKoWh9JJjpRlb8lU0TseYOzZp6ySiAO8St2a/HxxhrNuAAELUwZ 353oICkmxM/NeYgI7EEaLVPUwBAWfGZrRWb/+h8C6SrivWc73M/LI1A0B9tcEpUuh0CHTSVIBZsH+L 36IDyKW6n3T8YeI7+0CX391I/j3iyEBNFcfDaHaFChzkPxgPg6Xh1D1JWs+mUj1rOoTLxsyus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0QPwAYcLfcD/Vy1A2deHtiqcwDQYJKoZIhvcNAQELBQADggEBAETlZOIL 78NknxlMiYHCxoYypyzYuza2l3M4+YWakT0vFPgXpCk+l0dNst7h9nWvKKHCboSj+YP5dUCSsuUXhb 797xTei/F2nj7q1oCPuVJGThZqhWgF/JkqOy34hHEM5VniJiQu2W9TjzRMSOSFzRlQsHcOuXzdTkhr 80CQpD1TWxYL9sCy4YoCdE4edfgBGBMujxoijl3/xJ5uI1FjhlSPVP88p8Wsi8i7GdMYuxqjZMwrt2 81PHIPgop3BNN9/BzW0cmdyNvFgcD9qR8Rv5aFBYuQbyg6fST8JdAOrbMrCST6v2U41OOXH5MC/kL6 82tAGXsYdcuQpglUngmo/FV4Z9qjIDkYQ= 83""" 84 85 def setUp(self): 86 self.asn1Spec = rfc5280.Certificate() 87 88 def testDerCodec(self): 89 substrate = pem.readBase64fromText(self.pem_text) 90 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 91 assert not rest 92 assert asn1Object.prettyPrint() 93 assert der_encode(asn1Object) == substrate 94 95 extn_list = [ ] 96 for extn in asn1Object['tbsCertificate']['extensions']: 97 extn_list.append(extn['extnID']) 98 99 if extn['extnID'] == rfc7773.id_ce_authContext: 100 s = extn['extnValue'] 101 acs, rest = der_decode(s, 102 asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']]) 103 assert not rest 104 assert acs.prettyPrint() 105 assert s == der_encode(acs) 106 107 assert u'id.elegnamnden.se' in acs[0]['contextType'] 108 assert u'AuthContextInfo IdentityProvider' in acs[0]['contextInfo'] 109 110 assert rfc7773.id_ce_authContext in extn_list 111 112 113suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 114 115if __name__ == '__main__': 116 import sys 117 118 result = unittest.TextTestRunner(verbosity=2).run(suite) 119 sys.exit(not result.wasSuccessful()) 120