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 rfc7633 17 18try: 19 import unittest2 as unittest 20 21except ImportError: 22 import unittest 23 24 25class TLSFeaturesExtnTestCase(unittest.TestCase): 26 pem_text = """\ 27MIIEbTCCBBOgAwIBAgIRAO5f2N8q74GBATjTMXQCjlgwCgYIKoZIzj0EAwIwgZYx 28CzAJBgNVBAYTAkdCMRswGQYDVQQIExJHcmVhdGVyIE1hbmNoZXN0ZXIxEDAOBgNV 29BAcTB1NhbGZvcmQxGjAYBgNVBAoTEUNPTU9ETyBDQSBMaW1pdGVkMTwwOgYDVQQD 30EzNDT01PRE8gRUNDIE9yZ2FuaXphdGlvbiBWYWxpZGF0aW9uIFNlY3VyZSBTZXJ2 31ZXIgQ0EwHhcNMTYwMTE1MDAwMDAwWhcNMTgwMTE0MjM1OTU5WjCBwjELMAkGA1UE 32BhMCUlUxDzANBgNVBBETBjExNzY0NzEUMBIGA1UECBMLTW9zY293IENpdHkxDzAN 33BgNVBAcTBk1vc2NvdzE4MDYGA1UECRMvQWthZGVtaWthIEthcGljeSBzdHJlZXQs 34IGhvdXNlIDQsIGFwYXJ0bWVudCAxNjYxGDAWBgNVBAoTD0FuZHJleSBDaHVyYW5v 35djETMBEGA1UECxMKSW5zdGFudFNTTDESMBAGA1UEAxMJYWRtc2VsLmVjMHYwEAYH 36KoZIzj0CAQYFK4EEACIDYgAEwrPPzgBO1vDNmV0UVvYSBnys9B7LVkGLiIBbKYf2 37nNFRuJKo1gzNurI8pv4CbvqjkCX4Je/aSeYFHSCR9y82+zTwYQuJFt5LIL5f+Syp 38xZ7aLH56bOiQ+QhCtIvWP4YWo4IB9TCCAfEwHwYDVR0jBBgwFoAUdr4iSO4/PvZG 39A9mHGNBlfiKcC+EwHQYDVR0OBBYEFHTFQqV+H5a7+RVL+70Z6zqCbqq9MA4GA1Ud 40DwEB/wQEAwIFgDAMBgNVHRMBAf8EAjAAMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggr 41BgEFBQcDAjBQBgNVHSAESTBHMDsGDCsGAQQBsjEBAgEDBDArMCkGCCsGAQUFBwIB 42Fh1odHRwczovL3NlY3VyZS5jb21vZG8uY29tL0NQUzAIBgZngQwBAgIwWgYDVR0f 43BFMwUTBPoE2gS4ZJaHR0cDovL2NybC5jb21vZG9jYS5jb20vQ09NT0RPRUNDT3Jn 44YW5pemF0aW9uVmFsaWRhdGlvblNlY3VyZVNlcnZlckNBLmNybDCBiwYIKwYBBQUH 45AQEEfzB9MFUGCCsGAQUFBzAChklodHRwOi8vY3J0LmNvbW9kb2NhLmNvbS9DT01P 46RE9FQ0NPcmdhbml6YXRpb25WYWxpZGF0aW9uU2VjdXJlU2VydmVyQ0EuY3J0MCQG 47CCsGAQUFBzABhhhodHRwOi8vb2NzcC5jb21vZG9jYS5jb20wEQYIKwYBBQUHARgE 48BTADAgEFMCMGA1UdEQQcMBqCCWFkbXNlbC5lY4INd3d3LmFkbXNlbC5lYzAKBggq 49hkjOPQQDAgNIADBFAiAi6TXl76FTKPP1AhqtEjU5BjAj9Ju7CSKChHZSmzxeXQIh 50AOQSxhs011emVxyBIXT0ZGbmBY8LFRh6eGIOCAJbkM5T 51""" 52 53 def setUp(self): 54 self.asn1Spec = rfc5280.Certificate() 55 56 def testDerCodec(self): 57 substrate = pem.readBase64fromText(self.pem_text) 58 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 59 assert not rest 60 assert asn1Object.prettyPrint() 61 assert der_encode(asn1Object) == substrate 62 63 extn_list = [ ] 64 for extn in asn1Object['tbsCertificate']['extensions']: 65 extn_list.append(extn['extnID']) 66 if extn['extnID'] == rfc7633.id_pe_tlsfeature: 67 s = extn['extnValue'] 68 features, rest = der_decode(s, 69 rfc5280.certificateExtensionsMap[extn['extnID']]) 70 assert not rest 71 assert features.prettyPrint() 72 assert s == der_encode(features) 73 74 assert len(features) == 1 75 assert features[0] == 5 76 77 assert rfc7633.id_pe_tlsfeature in extn_list 78 79 80suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 81 82if __name__ == '__main__': 83 import sys 84 85 result = unittest.TextTestRunner(verbosity=2).run(suite) 86 sys.exit(not result.wasSuccessful()) 87