1# 2# This file is part of pyasn1-modules software. 3# 4# Created by Russ Housley 5# Copyright (c) 2018, 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 rfc5083 16from pyasn1_modules import rfc5084 17from pyasn1_modules import rfc5652 18 19try: 20 import unittest2 as unittest 21except ImportError: 22 import unittest 23 24 25class CCMParametersTestCase(unittest.TestCase): 26 ccm_pem_text = "MBEEDE2HVyIurFKUEX8MEgIBBA==" 27 28 def setUp(self): 29 self.asn1Spec = rfc5084.CCMParameters() 30 31 def testDerCodec(self): 32 substrate = pem.readBase64fromText(self.ccm_pem_text) 33 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 34 assert not rest 35 assert asn1Object.prettyPrint() 36 assert der_encode(asn1Object) == substrate 37 38 39class GCMParametersTestCase(unittest.TestCase): 40 gcm_pem_text = "MBEEDE2HVyIurFKUEX8MEgIBEA==" 41 42 def setUp(self): 43 self.asn1Spec = rfc5084.GCMParameters() 44 45 def testDerCodec(self): 46 substrate = pem.readBase64fromText(self.gcm_pem_text) 47 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 48 assert not rest 49 assert asn1Object.prettyPrint() 50 assert der_encode(asn1Object) == substrate 51 52 53class GCMOpenTypesTestCase(unittest.TestCase): 54 rfc8591_pem_pext = """\ 55MIIHkAYLKoZIhvcNAQkQARegggd/MIIHewIBADGCAk8wggJLAgEAMDMwJjEUMBIGA1UECgwL 56ZXhhbXBsZS5jb20xDjAMBgNVBAMMBUFsaWNlAgkAg/ULtwvVxA4wDQYJKoZIhvcNAQEBBQAE 57ggIAdZphtN3x8a8kZoAFY15HYRD6JyPBueRUhLbTPoOH3pZ9xeDK+zVXGlahl1y1UOe+McEx 582oD7cxAkhFuruNZMrCYEBCTZMwVhyEOZlBXdZEs8rZUHL3FFE5PJnygsSI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17qd/emvhbIx+ng 883swf8TJVnCNDIXucKVA4boXSlCEhCGzfoZZYGVvm1/hrypiBtpUIKWTxLnz4AQJdZ5LGiCQJ 89QU1wMyHsg6vWmNaJVhGHE6D/EnKsvJptFIkAx0wWkh35s48p7EbU8QBg//5eNru6yvLRutfd 90BX7T4w681pCD+dOiom75C3UdahrfoFkNsZ2hB88+qNsEEPb/xuGu8ZzSPZhakhl2NS0= 91""" 92 93 def setUp(self): 94 self.asn1Spec = rfc5652.ContentInfo() 95 96 def testDerCodec(self): 97 substrate = pem.readBase64fromText(self.rfc8591_pem_pext) 98 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 99 assert not rest 100 assert asn1Object.prettyPrint() 101 assert der_encode(asn1Object) == substrate 102 103 assert asn1Object['contentType'] == rfc5083.id_ct_authEnvelopedData 104 aed, rest = der_decode(asn1Object['content'], 105 asn1Spec=rfc5083.AuthEnvelopedData(), 106 decodeOpenTypes=True) 107 assert not rest 108 assert aed.prettyPrint() 109 assert der_encode(aed) == asn1Object['content'] 110 111 assert aed['version'] == 0 112 cea = aed['authEncryptedContentInfo']['contentEncryptionAlgorithm'] 113 assert cea['algorithm'] == rfc5084.id_aes128_GCM 114 assert cea['parameters']['aes-ICVlen'] == 16 115 116 117suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 118 119if __name__ == '__main__': 120 import sys 121 122 result = unittest.TextTestRunner(verbosity=2).run(suite) 123 sys.exit(not result.wasSuccessful()) 124