aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyasn1-modules/py3/tests/test_rfc5752.py
blob: 76776323d3b44f13954d903cd3fcb25c114ed62a (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# This file is part of pyasn1-modules software.
#
# Copyright (c) 2019, Vigil Security, LLC
# License: http://snmplabs.com/pyasn1/license.html
#
import sys
import unittest

from pyasn1.codec.der.decoder import decode as der_decoder
from pyasn1.codec.der.encoder import encode as der_encoder

from pyasn1_modules import pem
from pyasn1_modules import rfc4055
from pyasn1_modules import rfc5652
from pyasn1_modules import rfc5752


class MultipleSignaturesTestCase(unittest.TestCase):
    pem_text = """\
MIIKawYJKoZIhvcNAQcCoIIKXDCCClgCAQExGjALBglghkgBZQMEAgEwCwYJYIZI
AWUDBAICMFEGCSqGSIb3DQEHAaBEBEJDb250ZW50LVR5cGU6IHRleHQvcGxhaW4N
Cg0KV2F0c29uLCBjb21lIGhlcmUgLSBJIHdhbnQgdG8gc2VlIHlvdS6gggYmMIIC
eDCCAf6gAwIBAgIJAKWzVCgbsG47MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYTAlVT
MQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA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/xyIX6g7
QL9hfgKz9i/lPoE35xmRwL/9/H0viqg3HvnDWAIxAIADENLOLox7NiiMK+Ya70I0
jdEOIlE+zO/fF9I+syiz898JzTosN/V8wvaDoALtnQ==
"""

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

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

        layers = { }
        layers.update(rfc5652.cmsContentTypesMap)

        getNextLayer = {
            rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
            rfc5652.id_data: lambda x: None
        }

        getNextSubstrate = {
            rfc5652.id_ct_contentInfo: lambda x: x['content'],
            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
            rfc5652.id_data: lambda x: None
        }

        next_layer = rfc5652.id_ct_contentInfo
        while not next_layer == rfc5652.id_data:
            asn1Object, rest = der_decoder(
                substrate, asn1Spec=layers[next_layer])

            self.assertFalse(rest)
            self.assertTrue(asn1Object.prettyPrint())
            self.assertEqual(substrate, der_encoder(asn1Object))

            if next_layer == rfc5652.id_signedData:
                signerInfos = asn1Object['signerInfos']

            substrate = getNextSubstrate[next_layer](asn1Object)
            next_layer = getNextLayer[next_layer](asn1Object)

        found_mult_sig1 = False
        for attr in signerInfos[0]['signedAttrs']:
            if attr['attrType'] in rfc5652.cmsAttributesMap:
                av, rest = der_decoder(
                    attr['attrValues'][0],
                    asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])

                self.assertFalse(rest)
                self.assertTrue(av.prettyPrint())
                self.assertEqual(attr['attrValues'][0], der_encoder(av))

                if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
                    self.assertEqual(
                        av['bodyHashAlg']['algorithm'], rfc4055.id_sha384)

                    self.assertEqual(
                        'dfaf6c0a',
                        av['signAttrsHash']['hash'].prettyPrint()[2:10])

                    found_mult_sig1 = True

        found_mult_sig2 = False
        for attr in signerInfos[1]['signedAttrs']:
            if attr['attrType'] in rfc5652.cmsAttributesMap:
                av, rest = der_decoder(
                    attr['attrValues'][0],
                    asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])

                self.assertFalse(rest)
                self.assertTrue(av.prettyPrint())
                self.assertEqual(attr['attrValues'][0], der_encoder(av))

                if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
                    self.assertEqual(
                        av['bodyHashAlg']['algorithm'], rfc4055.id_sha256)

                    self.assertEqual(
                        '7329527d',
                        av['signAttrsHash']['hash'].prettyPrint()[2:10])

                    found_mult_sig2 = True

        self.assertTrue(found_mult_sig1)
        self.assertTrue(found_mult_sig2)

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found_mult_sig1 = False
        for attr in asn1Object['content']['signerInfos'][0]['signedAttrs']:
            if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
                av = attr['attrValues'][0]

                self.assertEqual(
                    av['bodyHashAlg']['algorithm'], rfc4055.id_sha384)

                self.assertEqual(
                    'dfaf6c0a',
                    av['signAttrsHash']['hash'].prettyPrint()[2:10])

                found_mult_sig1 = True

        found_mult_sig2 = False
        for attr in asn1Object['content']['signerInfos'][1]['signedAttrs']:
            if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
                av = attr['attrValues'][0]

                self.assertEqual(
                    av['bodyHashAlg']['algorithm'], rfc4055.id_sha256)

                self.assertEqual(
                    '7329527d',
                    av['signAttrsHash']['hash'].prettyPrint()[2:10])

                found_mult_sig2 = True

        self.assertTrue(found_mult_sig1)
        self.assertTrue(found_mult_sig2)


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

if __name__ == '__main__':
    import sys

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