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.type import univ 13 14from pyasn1_modules import pem 15from pyasn1_modules import rfc4055 16from pyasn1_modules import rfc5652 17from pyasn1_modules import rfc5958 18from pyasn1_modules import rfc8479 19 20try: 21 import unittest2 as unittest 22except ImportError: 23 import unittest 24 25 26class ValidationParmTestCase(unittest.TestCase): 27 pem_text = """\ 28MIIE/gIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpPwXwfhDsWA3q 29jN2BWg1xfDjvZDVNfgTV/b95g304Aty3z13xPXAhHZ3ROW3pgPxTj9fiq7ZMy4Ua 30gMpPK81v3pHX1uokC2KcGXbgbAq2Q8ClxSXgEJllRwDENufjEdV10gArt8NlIP0N 31lota1kQUuI1DMsqc5DTIa35Nq4j1GW+KmLtP0kCrGq9fMGwjDbPEpSp9DTquEMHJ 32o7kyJIjB+93ikLvBUTgbxr+jcnTLXuhA8rC8r+KXre4NPPNPRyefRcALLt/URvfA 33rTvFOQfi3vIjNhBZL5FdC+FVAr5QnF3r2+cuDPbnczr4/rr81kzFGWrwyAgF5FWu 34pFtB5IYDAgMBAAECggEAHZ88vGNsNdmRkfhWupGW4cKCuo+Y7re8Q/H2Jd/4Nin2 35FKvUPuloaztiSGDbVm+vejama/Nu5FEIumNJRYMeoVJcx2DDuUxO1ZB1aIEwfMct 36/DWd0/JDzuCXB0Cu5GTWLhlz0zMGHXihIdQ0DtGKt++3Ncg5gy1D+cIqqJB515/z 37jYdZmb0Wqmz7H3DisuxvnhiCAOuNrjcDau80hpMA9TQlb+XKNGHIBgKpJe6lnB0P 38MsS/AjDiDoEpP9GG9mv9+96rAga4Nos6avYlwWwbC6d+hHIWvWEWsmrDfcJlm2gN 39tjvG8omj00t5dAt7qGhfOoNDGr5tvJVo/g96O/0I8QKBgQDdzytVRulo9aKVdAYW 40/Nj04thtnRaqsTyFH+7ibEVwNIUuld/Bp6NnuGrY+K1siX8+zA9f8mKxuXXV9KK4 41O89Ypw9js2BxM7VYO9Gmp6e1RY3Rrd8w7pG7/KqoPWXkuixTay9eybrJMWu3TT36 42q7NheNmBHqcFmSQQuUwEmvp3MQKBgQDDVaisMJkc/sIyQh3XrlfzmMLK+GlPDucD 43w5e50fHl8Q5PmTcP20zVLhTevffCqeItSyeAno94Xdzc9vZ/rt69410kJEHyBO9L 44CmhtYz94wvSdRhbqf4VzAl2WU184sIYiIZDGsnGScgIYvo6v6mITjRhc8AMdYoPR 45rL6xp6frcwKBgFi1+avCj6mFzD+fxqu89nyCmXLFiAI+nmjTy7PM/7yPlNB76qDG 46Dil2bW1Xj+y/1R9ld6S1CVnxRbqLe+TZLuVS82m5nRHJT3b5fbD8jquGJOE+e+xT 47DgA0XoCpBa6D8yRt0uVDIyxCUsVd5DL0JusN7VehzcUEaZMyuL+CyDeRAoGBAImB 48qH6mq3Kc6Komnwlw4ttJ436sxr1vuTKOIyYdZBNB0Zg5PGi+MWU0zl5LDroLi3vl 49FwbVGBxcvxkSBU63FHhKMQw7Ne0gii+iQQcYQdtKKpb4ezNS1+exd55WTIcExTgL 50tvYZMhgsh8tRgfLWpXor7kWmdBrgeflFiOxZIL1/AoGAeBP7sdE+gzsh8jqFnVRj 517nOg+YllJAlWsf7cTH4pLIy2Eo9D+cNjhL9LK6RaAd7PSZ1adm8HfaROA2cfCm84 52RI4c7Ue0G+N6LZiFvC0Bfi5SaPVAExXOty8UqjOCoZavSaXBPuNcTXZuzswcgbxI 53G5/kaJNHoEcdlVsPsYWKRNKgPzA9BgorBgEEAZIIEggBMS8wLQYJYIZIAWUDBAIC 54BCCK9DKMh7687DHjA7j1U37/y2qR2UcITZmjaYI7NvAUYg== 55""" 56 57 def setUp(self): 58 self.asn1Spec = rfc5958.OneAsymmetricKey() 59 60 def testDerCodec(self): 61 substrate = pem.readBase64fromText(self.pem_text) 62 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 63 assert not rest 64 assert asn1Object.prettyPrint() 65 assert der_encode(asn1Object) == substrate 66 67 for attr in asn1Object['attributes']: 68 assert attr['attrType'] in rfc5652.cmsAttributesMap.keys() 69 if attr['attrType'] == rfc8479.id_attr_validation_parameters: 70 av, rest = der_decode(attr['attrValues'][0], 71 asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']]) 72 assert not rest 73 assert av.prettyPrint() 74 assert der_encode(av) == attr['attrValues'][0] 75 76 assert av['hashAlg'] == rfc4055.id_sha384 77 seed = univ.OctetString(hexValue='8af4328c87bebcec31e303b8f5537effcb6a91d947084d99a369823b36f01462') 78 assert av['seed'] == seed 79 80 def testOpenTypes(self): 81 substrate = pem.readBase64fromText(self.pem_text) 82 asn1Object, rest = der_decode(substrate, 83 asn1Spec=self.asn1Spec, 84 decodeOpenTypes=True) 85 assert not rest 86 assert asn1Object.prettyPrint() 87 assert der_encode(asn1Object) == substrate 88 89 for attr in asn1Object['attributes']: 90 assert attr['attrType'] in rfc5652.cmsAttributesMap.keys() 91 if attr['attrType'] == rfc8479.id_attr_validation_parameters: 92 av = attr['attrValues'][0] 93 assert av['hashAlg'] == rfc4055.id_sha384 94 seed = univ.OctetString(hexValue='8af4328c87bebcec31e303b8f5537effcb6a91d947084d99a369823b36f01462') 95 assert av['seed'] == seed 96 97 98suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 99 100if __name__ == '__main__': 101 import sys 102 103 result = unittest.TextTestRunner(verbosity=2).run(suite) 104 sys.exit(not result.wasSuccessful()) 105