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 rfc5652 17from pyasn1_modules import rfc6210 18 19try: 20 import unittest2 as unittest 21except ImportError: 22 import unittest 23 24 25class AuthenticatedDataTestCase(unittest.TestCase): 26 pem_text = """\ 27MIICRQYLKoZIhvcNAQkQAQKgggI0MIICMAIBADGBwDCBvQIBADAmMBIxEDAOBgNVBAMMB0 28NhcmxSU0ECEEY0a8eAAFa8EdNuLs1dcdAwDQYJKoZIhvcNAQEBBQAEgYCH70EpEikY7deb 29859YJRAWfFondQv1D4NFltw6C1ceheWnlAU0C2WEXr3LUBXZp1/PSte29FnJxu5bXCTn1g 30elMm6zNlZNWNd0KadVBcaxi1n8L52tVM5sWFGJPO5cStOyAka2ucuZM6iAnCSkn1Ju7fgU 315j2g3bZ/IM8nHTcygjAKBggrBgEFBQgBAqFPBgsqhkiG9w0BCRADDQRAAQIDBAUGBwgJCg 32sMDQ4PEBESEwQVFhcYGRobHB0eHyAhIiMEJSYnKCkqKywtLi8wMTIzBDU2Nzg5Ojs8PT4/ 33QDArBgkqhkiG9w0BBwGgHgQcVGhpcyBpcyBzb21lIHNhbXBsZSBjb250ZW50LqKBxzAYBg 34kqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0wOTEyMTAyMzI1MDBa 35MB8GCSqGSIb3DQEJBDESBBCWaa5hG1eeg+oQK2tJ3cD5MGwGCSqGSIb3DQEJNDFfMF0wTw 36YLKoZIhvcNAQkQAw0EQAECAwQFBgcICQoLDA0ODxAREhMEFRYXGBkaGxwdHh8gISIjBCUm 37JygpKissLS4vMDEyMwQ1Njc4OTo7PD0+P0CiCgYIKwYBBQUIAQIEFLjUxQ9PJFzFnWraxb 38EIbVbg2xql 39""" 40 41 def setUp(self): 42 self.asn1Spec = rfc5652.ContentInfo() 43 44 def testDerCodec(self): 45 substrate = pem.readBase64fromText(self.pem_text) 46 asn1Object, rest = der_decode (substrate, asn1Spec=self.asn1Spec) 47 assert not rest 48 assert asn1Object.prettyPrint() 49 assert der_encode(asn1Object) == substrate 50 51 assert asn1Object['contentType'] == rfc5652.id_ct_authData 52 ad, rest = der_decode (asn1Object['content'], asn1Spec=rfc5652.AuthenticatedData()) 53 assert not rest 54 assert ad.prettyPrint() 55 assert der_encode(ad) == asn1Object['content'] 56 57 assert ad['version'] == 0 58 assert ad['digestAlgorithm']['algorithm'] == rfc6210.id_alg_MD5_XOR_EXPERIMENT 59 60 mac_alg_p, rest = der_decode (ad['digestAlgorithm']['parameters'], 61 asn1Spec=rfc5280.algorithmIdentifierMap[ad['digestAlgorithm']['algorithm']]) 62 assert not rest 63 assert mac_alg_p.prettyPrint() 64 assert der_encode(mac_alg_p) == ad['digestAlgorithm']['parameters'] 65 66 assert mac_alg_p.prettyPrint()[:10] == "0x01020304" 67 68 69suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 70 71if __name__ == '__main__': 72 import sys 73 74 result = unittest.TextTestRunner(verbosity=2).run(suite) 75 sys.exit(not result.wasSuccessful()) 76