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 import decoder as der_decoder 12from pyasn1.codec.der import encoder as der_encoder 13 14from pyasn1.type import univ 15 16from pyasn1_modules import pem 17from pyasn1_modules import rfc3565 18 19try: 20 import unittest2 as unittest 21except ImportError: 22 import unittest 23 24 25class AESKeyWrapTestCase(unittest.TestCase): 26 kw_alg_id_pem_text = "MAsGCWCGSAFlAwQBLQ==" 27 28 def setUp(self): 29 self.asn1Spec = rfc3565.AlgorithmIdentifier() 30 31 def testDerCodec(self): 32 substrate = pem.readBase64fromText(self.kw_alg_id_pem_text) 33 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 34 assert not rest 35 assert asn1Object.prettyPrint() 36 assert asn1Object[0] == rfc3565.id_aes256_wrap 37 assert der_encoder.encode(asn1Object) == substrate 38 39 40class AESCBCTestCase(unittest.TestCase): 41 aes_alg_id_pem_text = "MB0GCWCGSAFlAwQBKgQQEImWuoUOPwM5mTu1h4oONw==" 42 43 def setUp(self): 44 self.asn1Spec = rfc3565.AlgorithmIdentifier() 45 46 def testDerCodec(self): 47 substrate = pem.readBase64fromText(self.aes_alg_id_pem_text) 48 asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec) 49 assert not rest 50 assert asn1Object.prettyPrint() 51 assert asn1Object[0] == rfc3565.id_aes256_CBC 52 assert asn1Object[1].isValue 53 assert der_encoder.encode(asn1Object) == substrate 54 55 def testOpenTypes(self): 56 substrate = pem.readBase64fromText(self.aes_alg_id_pem_text) 57 asn1Object, rest = der_decoder.decode(substrate, 58 asn1Spec=self.asn1Spec, 59 decodeOpenTypes=True) 60 assert not rest 61 assert asn1Object.prettyPrint() 62 assert asn1Object[0] == rfc3565.id_aes256_CBC 63 aes_iv = univ.OctetString(hexValue='108996ba850e3f0339993bb5878a0e37') 64 assert asn1Object[1] == aes_iv 65 assert der_encoder.encode(asn1Object) == substrate 66 67 68suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 69 70if __name__ == '__main__': 71 unittest.TextTestRunner(verbosity=2).run(suite) 72