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 rfc6120 17 18try: 19 import unittest2 as unittest 20except ImportError: 21 import unittest 22 23 24class XMPPCertificateTestCase(unittest.TestCase): 25 xmpp_server_cert_pem_text = """\ 26MIIC6DCCAm+gAwIBAgIJAKWzVCgbsG5DMAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT 27AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n 28dXMgQ0EwHhcNMTkxMDI0MjMxNjA0WhcNMjAxMDIzMjMxNjA0WjBNMQswCQYDVQQG 29EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xHzAdBgNVBAoTFkV4 30YW1wbGUgUHJvZHVjdHMsIEluYy4wdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQZzQlk 3103nJRPF6+w1NxFELmQ5vJTjTRz3eu03CRtahK4Wnwd4GwbDe8NVHAEG2qTzBXFDu 32p6RZugsBdf9GcEZHG42rThYYOzIYzVFnI7tQgA+nTWSWZN6eoU/EXcknhgijggEn 33MIIBIzAdBgNVHQ4EFgQUkQpUMYcbUesEn5buI03POFnktJgwHwYDVR0jBBgwFoAU 348jXbNATapVXyvWkDmbBi7OIVCMEwCwYDVR0PBAQDAgeAMIGPBgNVHREEgYcwgYSg 35KQYIKwYBBQUHCAegHRYbX3htcHAtY2xpZW50LmltLmV4YW1wbGUuY29toCkGCCsG 36AQUFBwgHoB0WG194bXBwLXNlcnZlci5pbS5leGFtcGxlLmNvbaAcBggrBgEFBQcI 37BaAQDA5pbS5leGFtcGxlLmNvbYIOaW0uZXhhbXBsZS5jb20wQgYJYIZIAYb4QgEN 38BDUWM1RoaXMgY2VydGlmaWNhdGUgY2Fubm90IGJlIHRydXN0ZWQgZm9yIGFueSBw 39dXJwb3NlLjAKBggqhkjOPQQDAwNnADBkAjAEo4mhDGC6/R39HyNgzLseNAp36qBH 40yQJ/AWsBojN0av8akeVv9IuM45yqLKdiCzcCMDCjh1lFnCvurahwp5D1j9pAZMsg 41nOzhcMpnHs2U/eN0lHl/JNgnbftl6Dvnt59xdA== 42""" 43 44 def setUp(self): 45 self.asn1Spec = rfc5280.Certificate() 46 47 def testDerCodec(self): 48 substrate = pem.readBase64fromText(self.xmpp_server_cert_pem_text) 49 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 50 assert not rest 51 assert asn1Object.prettyPrint() 52 assert der_encode(asn1Object) == substrate 53 54 count = 0 55 for extn in asn1Object['tbsCertificate']['extensions']: 56 if extn['extnID'] == rfc5280.id_ce_subjectAltName: 57 extnValue, rest = der_decode(extn['extnValue'], 58 asn1Spec=rfc5280.SubjectAltName()) 59 assert not rest 60 assert extnValue.prettyPrint() 61 assert der_encode(extnValue) == extn['extnValue'] 62 for gn in extnValue: 63 if gn['otherName'].hasValue(): 64 gn_on = gn['otherName'] 65 if gn_on['type-id'] == rfc6120.id_on_xmppAddr: 66 assert gn_on['type-id'] in rfc5280.anotherNameMap.keys() 67 spec = rfc5280.anotherNameMap[gn['otherName']['type-id']] 68 on, rest = der_decode(gn_on['value'], asn1Spec=spec) 69 assert not rest 70 assert on.prettyPrint() 71 assert der_encode(on) == gn_on['value'] 72 assert on == u'im.example.com' 73 count += 1 74 75 assert count == 1 76 77 def testOpenTypes(self): 78 substrate = pem.readBase64fromText(self.xmpp_server_cert_pem_text) 79 asn1Object, rest = der_decode(substrate, 80 asn1Spec=self.asn1Spec, 81 decodeOpenTypes=True) 82 assert not rest 83 assert asn1Object.prettyPrint() 84 assert der_encode(asn1Object) == substrate 85 86 count = 0 87 for extn in asn1Object['tbsCertificate']['extensions']: 88 if extn['extnID'] == rfc5280.id_ce_subjectAltName: 89 extnValue, rest = der_decode(extn['extnValue'], 90 asn1Spec=rfc5280.SubjectAltName(), decodeOpenTypes=True) 91 assert not rest 92 assert extnValue.prettyPrint() 93 assert der_encode(extnValue) == extn['extnValue'] 94 for gn in extnValue: 95 if gn['otherName'].hasValue(): 96 if gn['otherName']['type-id'] == rfc6120.id_on_xmppAddr: 97 assert gn['otherName']['value'] == u'im.example.com' 98 count += 1 99 100 assert count == 1 101 102 103suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 104 105if __name__ == '__main__': 106 import sys 107 108 result = unittest.TextTestRunner(verbosity=2).run(suite) 109 sys.exit(not result.wasSuccessful()) 110