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 rfc5652 16from pyasn1_modules import rfc6031 17 18try: 19 import unittest2 as unittest 20except ImportError: 21 import unittest 22 23 24class SymmetricKeyPkgTestCase(unittest.TestCase): 25 key_pkg_pem_text = """\ 26MIG7BgsqhkiG9w0BCRABGaCBqzCBqKBEMCMGCyqGSIb3DQEJEAwBMRQMElZpZ2ls 27IFNlY3VyaXR5IExMQzAdBgsqhkiG9w0BCRAMAzEODAxQcmV0ZW5kIDA0OEEwYDBe 28MFYwGwYLKoZIhvcNAQkQDBsxDAwKZXhhbXBsZUlEMTAVBgsqhkiG9w0BCRAMCjEG 29DARIT1RQMCAGCyqGSIb3DQEJEAwLMREMD2t0YS5leGFtcGxlLmNvbQQEMTIzNA== 30""" 31 32 def setUp(self): 33 self.asn1Spec = rfc5652.ContentInfo() 34 35 def testDerCodec(self): 36 substrate = pem.readBase64fromText(self.key_pkg_pem_text) 37 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 38 assert not rest 39 assert asn1Object.prettyPrint() 40 assert der_encode(asn1Object) == substrate 41 42 assert asn1Object['contentType'] in rfc5652.cmsContentTypesMap 43 asn1Spec = rfc5652.cmsContentTypesMap[asn1Object['contentType']] 44 skp, rest = der_decode(asn1Object['content'], asn1Spec=asn1Spec) 45 assert not rest 46 assert skp.prettyPrint() 47 assert der_encode(skp) == asn1Object['content'] 48 49 for attr in skp['sKeyPkgAttrs']: 50 assert attr['attrType'] in rfc6031.sKeyPkgAttributesMap.keys() 51 52 for osk in skp['sKeys']: 53 for attr in osk['sKeyAttrs']: 54 assert attr['attrType'] in rfc6031.sKeyAttributesMap.keys() 55 56 def testOpenTypes(self): 57 substrate = pem.readBase64fromText(self.key_pkg_pem_text) 58 asn1Object, rest = der_decode(substrate, 59 asn1Spec=self.asn1Spec, 60 decodeOpenTypes=True) 61 assert not rest 62 assert asn1Object.prettyPrint() 63 assert der_encode(asn1Object) == substrate 64 65 assert asn1Object['contentType'] in rfc5652.cmsContentTypesMap 66 assert asn1Object['content'].hasValue() 67 keypkg = asn1Object['content'] 68 assert keypkg['version'] == rfc6031.KeyPkgVersion().subtype(value='v1') 69 70 for attr in keypkg['sKeyPkgAttrs']: 71 assert attr['attrType'] in rfc6031.sKeyPkgAttributesMap.keys() 72 assert attr['attrValues'][0].prettyPrint()[:2] != '0x' 73 # decodeOpenTypes=True did not decode if the value is shown in hex ... 74 if attr['attrType'] == rfc6031.id_pskc_manufacturer: 75 attr['attrValues'][0] == 'Vigil Security LLC' 76 77 for osk in keypkg['sKeys']: 78 for attr in osk['sKeyAttrs']: 79 assert attr['attrType'] in rfc6031.sKeyAttributesMap.keys() 80 assert attr['attrValues'][0].prettyPrint()[:2] != '0x' 81 # decodeOpenTypes=True did not decode if the value is shown in hex ... 82 if attr['attrType'] == rfc6031.id_pskc_issuer: 83 attr['attrValues'][0] == 'kta.example.com' 84 85 86suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 87 88if __name__ == '__main__': 89 unittest.TextTestRunner(verbosity=2).run(suite) 90