aboutsummaryrefslogtreecommitdiff
path: root/tests/test_rfc4108.py
blob: 272558c0f0fd5c67c7b8ac6941e7424f43cc241e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# This file is part of pyasn1-modules software.
#
# Copyright (c) 2019, Vigil Security, LLC
# License: http://snmplabs.com/pyasn1/license.html
#
import sys

from pyasn1.codec.der.decoder import decode as der_decode
from pyasn1.codec.der.encoder import encode as der_encode

from pyasn1.type import univ

from pyasn1_modules import pem
from pyasn1_modules import rfc5652
from pyasn1_modules import rfc4108

try:
    import unittest2 as unittest
except ImportError:
    import unittest


class CMSFirmwareWrapperTestCase(unittest.TestCase):
    pem_text = """\
MIIEvAYJKoZIhvcNAQcCoIIErTCCBKkCAQExDTALBglghkgBZQMEAgEwggIVBgsq
hkiG9w0BCRABEKCCAgQEggIA3ntqPr5kDpx+//pgWGfHCH/Ht4pbenGwXv80txyE
Y0I2mT9BUGz8ILkbhD7Xz89pBS5KhEJpthxH8WREJtvS+wL4BqYLt23wjWoZy5Gt
5dPzWgaNlV/aQ5AdfAY9ljmnNYnK8D8r8ur7bQM4cKUdxry+QA0nqXHMAOSpx4Um
8impCc0BICXaFfL3zBrNxyPubbFO9ofbYOAWaNmmIAhzthXf12vDrLostIqmYrP4
LMRCjTr4LeYaVrAWfKtbUbByN6IuBef3Qt5cJaChr74udz3JvbYFsUvCpl64kpRq
g2CT6R+xE4trO/pViJlI15dvJVz04BBYQ2jQsutJwChi97/DDcjIv03VBmrwRE0k
RJNFP9vpDM8CxJIqcobC5Kuv8b0GqGfGl6ouuQKEVMfBcrupgjk3oc3KL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kETd3YyLK4yHwnoIGo1GiW/
dsnhVtIdkPtfJIvcYteYJg==
"""

    def setUp(self):
        self.asn1Spec = rfc5652.ContentInfo()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)

        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)

        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['contentType'] == rfc5652.id_signedData
        inner, rest = der_decode(asn1Object['content'], asn1Spec=rfc5652.SignedData())

        assert inner['encapContentInfo']['eContentType'] == rfc4108.id_ct_firmwarePackage
        assert inner['encapContentInfo']['eContent']

        attribute_list = [ ]
        for attr in inner['signerInfos'][0]['signedAttrs']:
            attribute_list.append(attr['attrType'])
            if attr['attrType'] == rfc4108.id_aa_targetHardwareIDs:
                av, rest = der_decode(attr['attrValues'][0],
                    asn1Spec=rfc4108.TargetHardwareIdentifiers())
                assert len(av) == 2
                for oid in av:
                    assert '1.3.6.1.4.1.221121.1.1.' in oid.prettyPrint()

        assert rfc5652.id_contentType in attribute_list
        assert rfc5652.id_messageDigest in attribute_list
        assert rfc4108.id_aa_targetHardwareIDs in attribute_list
        assert rfc4108.id_aa_fwPkgMessageDigest in attribute_list

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decode(substrate,
            asn1Spec=self.asn1Spec,
            decodeOpenTypes=True)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['contentType'] == rfc5652.id_signedData
        sd_eci = asn1Object['content']['encapContentInfo']
        assert sd_eci['eContentType'] == rfc4108.id_ct_firmwarePackage
        assert sd_eci['eContent'].hasValue()

        for attr in asn1Object['content']['signerInfos'][0]['signedAttrs']:
            assert attr['attrType'] in rfc5652.cmsAttributesMap.keys()
            if attr['attrType'] == rfc4108.id_aa_targetHardwareIDs:
               for oid in attr['attrValues'][0]:
                   assert '1.3.6.1.4.1.221121.1.1.' in oid.prettyPrint()


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    import sys

    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())