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.type import univ 15 16from pyasn1_modules import pem 17from pyasn1_modules import rfc5652 18from pyasn1_modules import rfc5751 19 20try: 21 import unittest2 as unittest 22except ImportError: 23 import unittest 24 25 26class SignedMessageTestCase(unittest.TestCase): 27 pem_text = """\ 28MIIGigYJKoZIhvcNAQcCoIIGezCCBncCAQExCTAHBgUrDgMCGjArBgkqhkiG9w0B 29BwGgHgQcVGhpcyBpcyBzb21lIHNhbXBsZSBjb250ZW50LqCCAuAwggLcMIICm6AD 30AgECAgIAyDAJBgcqhkjOOAQDMBIxEDAOBgNVBAMTB0NhcmxEU1MwHhcNOTkwODE3 31MDExMDQ5WhcNMzkxMjMxMjM1OTU5WjATMREwDwYDVQQDEwhBbGljZURTUzCCAbYw 32ggErBgcqhkjOOAQBMIIBHgKBgQCBjc3tg+oKnjk+wkgoo+RHk90O16gO7FPFq4QI 33T/+U4XNIfgzW80RI0f6fr6ShiS/h2TDINt4/m7+3TNxfaYrkddA3DJEIlZvep175 34/PSfL91DqItU8T+wBwhHTV2Iw8O1s+NVCHXVOXYQxHi9/52whJc38uRRG7XkCZZc 35835b2wIVAOJHphpFZrgTxtqPuDchK2KL95PNAoGAJjjQFIkyqjn7Pm3ZS1lqTHYj 36OQQCNVzyyxowwx5QXd2bWeLNqgU9WMB7oja4bgevfYpCJaf0dc9KCF5LPpD4beqc 37ySGKO3YU6c4uXaMHzSOFuC8wAXxtSYkRiTZEvfjIlUpTVrXi+XPsGmE2HxF/wr3t 380VD/mHTC0YFKYDm6NjkDgYQAAoGAXOO5WnUUlgupet3jP6nsrF7cvbcTETSmFoko 39ESPZNIZndXUTEj1DW2/lUb/6ifKiGz4kfT0HjVtjyLtFpaBK44XWzgaAP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wNWREExEjAQBgNV 56BAMTCURhaXN5IFJTQQIEClVEMzCB/AYLKoZIhvcNAQkQAgMxgewwgekwgeYEBzU3 57MzgyOTkYDzE5OTkwMzExMTA0NDMzWqGByTCBxqRhMF8xCzAJBgNVBAYTAlVTMRYw 58FAYDVQQKEw1VUyBHb3Zlcm5tZW50MREwDwYDVQQLEwhWREEgU2l0ZTEMMAoGA1UE 59CxMDVkRBMRcwFQYDVQQDEw5CdWdzIEJ1bm55IERTQaRhMF8xCzAJBgNVBAYTAlVT 60MRYwFAYDVQQKEw1VUyBHb3Zlcm5tZW50MREwDwYDVQQLEwhWREEgU2l0ZTEMMAoG 61A1UECxMDVkRBMRcwFQYDVQQDEw5FbG1lciBGdWRkIERTQTAJBgcqhkjOOAQDBC8w 62LQIVALwzN2XE93BcF0kTqkyFyrtSkUhZAhRjlqIUi89X3rBIX2xk3YQESV8cyg== 63""" 64 65 def setUp(self): 66 self.asn1Spec = rfc5652.ContentInfo() 67 68 def testDerCodec(self): 69 smimeCapMap = { 70 univ.ObjectIdentifier('1.2.3.4.5.6.77'): univ.OctetString(), 71 } 72 smimeCapMap.update(rfc5751.smimeCapabilityMap) 73 74 substrate = pem.readBase64fromText(self.pem_text) 75 asn1Object, rest = der_decode (substrate, 76 asn1Spec=self.asn1Spec, 77 decodeOpenTypes=True) 78 assert not rest 79 assert asn1Object.prettyPrint() 80 assert der_encode(asn1Object) == substrate 81 82 assert asn1Object['contentType'] == rfc5652.id_signedData 83 assert asn1Object['content']['version'] == 1 84 85 for si in asn1Object['content']['signerInfos']: 86 assert si['version'] == 1 87 for attr in si['signedAttrs']: 88 89 if attr['attrType'] == rfc5751.smimeCapabilities: 90 for scap in attr['attrValues'][0]: 91 if scap['capabilityID'] in smimeCapMap.keys(): 92 scap_p, rest = der_decode(scap['parameters'], 93 asn1Spec=smimeCapMap[scap['capabilityID']]) 94 assert not rest 95 assert der_encode(scap_p) == scap['parameters'] 96 assert 'parameters' in scap_p.prettyPrint() 97 98 if attr['attrType'] == rfc5751.id_aa_encrypKeyPref: 99 ekp_issuer_serial = attr['attrValues'][0]['issuerAndSerialNumber'] 100 assert ekp_issuer_serial['serialNumber'] == 173360179 101 102 103suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 104 105if __name__ == '__main__': 106 import sys 107 108 result = unittest.TextTestRunner(verbosity=2).run(suite) 109 sys.exit(not result.wasSuccessful()) 110