aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyasn1-modules/py2/tests/test_rfc7191.py
blob: 40afbd42ea1239ef9b7d81d21d169848fdaf5879 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#
# This file is part of pyasn1-modules software.
#
# Created by Russ Housley
# 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 rfc5652
from pyasn1_modules import rfc7191


class ReceiptRequestTestCase(unittest.TestCase):
    message1_pem_text = """\
MIIGfAYJKoZIhvcNAQcCoIIGbTCCBmkCAQMxDTALBglghkgBZQMEAgIwgb4GCyqGSIb3DQEJ
EAEZoIGuB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OOj25JFqa80xnXGtQqjm/7NSII5SbdJk+DT7KCkSbkElkbgQ=
"""

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

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.message1_pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        sd, rest = der_decoder(
            asn1Object['content'], asn1Spec=rfc5652.SignedData())

        for sa in sd['signerInfos'][0]['signedAttrs']:
            sat = sa['attrType']
            sav0 = sa['attrValues'][0]

            if sat == rfc7191.id_aa_KP_keyPkgIdAndReceiptReq:
                sav, rest = der_decoder(
                    sav0, asn1Spec=rfc7191.KeyPkgIdentifierAndReceiptReq())

                self.assertFalse(rest)
                self.assertTrue(sav.prettyPrint())
                self.assertEqual(sav0, der_encoder(sav))

                package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
                package_id = pem.readBase64fromText(package_id_pem_text)

                self.assertEqual(package_id, sav['pkgID'])

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

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        v3 = rfc5652.CMSVersion().subtype(value='v3')

        self.assertEqual(v3, asn1Object['content']['version'])

        for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
            if sa['attrType'] == rfc7191.id_aa_KP_keyPkgIdAndReceiptReq:
                package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
                package_id = pem.readBase64fromText(package_id_pem_text)
                self.assertEqual(package_id, sa['attrValues'][0]['pkgID'])


class ReceiptTestCase(unittest.TestCase):
    message2_pem_text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OAuZKe/f40MKSF8q4w2ij+0euSaKFiAIxAL3g
xp6sMitCmLQgOH6/RBIC/2syJ97y0KVp9da0PDAvwxLugCHTKZPjjpSLPHHc9TGCARwwggEY
AgEDgBTEuloOPnrjPIGw9AKqaLsW4JYONTALBglghkgBZQMEAgKgejAZBgkqhkiG9w0BCQMx
DAYKYIZIAWUCAQJOAzAcBgkqhkiG9w0BCQUxDxcNMTkwNjEzMTYxNjA4WjA/BgkqhkiG9w0B
CQQxMgQwQSWYpq4jwhMkmS0as0JL3gjYxKLgDfzP2ndTNsAY0m9p8Igp8ZcK4+5n9fXJ43vU
MAoGCCqGSM49BAMDBGgwZgIxAMfq2EJ5pSl9tGOEVJEgZitc266ljrOg5GDjkd2d089qw1A3
bUcOYuCdivgxVuhlAgIxAPR9JavxziwCbVyBUWOAiKKYfglTgG3AwNmrKDj0NtXUQ9qDmGAc
6L+EAY2P5OVB8Q==
"""

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

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.message2_pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        sd, rest = der_decoder(
            asn1Object['content'], asn1Spec=rfc5652.SignedData())

        self.assertFalse(rest)
        self.assertTrue(sd.prettyPrint())
        self.assertEqual(asn1Object['content'], der_encoder(sd))

        oid = sd['encapContentInfo']['eContentType']

        self.assertEqual(rfc7191.id_ct_KP_keyPackageReceipt, oid)

        receipt, rest = der_decoder(
            sd['encapContentInfo']['eContent'],
            asn1Spec=rfc7191.KeyPackageReceipt())

        self.assertFalse(rest)
        self.assertTrue(receipt.prettyPrint())
        self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(receipt))

        package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
        package_id = pem.readBase64fromText(package_id_pem_text)

        self.assertEqual(package_id, receipt['receiptOf']['pkgID'])

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

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        v3 = rfc5652.CMSVersion().subtype(value='v3')

        self.assertEqual(v3, asn1Object['content']['version'])

        for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
            self.assertIn( sa['attrType'], rfc5652.cmsAttributesMap)
            if sa['attrType'] == rfc5652.id_messageDigest:
                self.assertIn(
                    '0x412598a6ae2', sa['attrValues'][0].prettyPrint())

        ct_oid = asn1Object['content']['encapContentInfo']['eContentType']

        self.assertIn(ct_oid, rfc5652.cmsContentTypesMap)
        self.assertEqual(ct_oid, rfc7191.id_ct_KP_keyPackageReceipt)

        # Since receipt is inside an OCTET STRING, decodeOpenTypes=True cannot
        # automatically decode it
        sd_eci = asn1Object['content']['encapContentInfo']
        receipt, rest = der_decoder(
            sd_eci['eContent'],
            asn1Spec=rfc5652.cmsContentTypesMap[sd_eci['eContentType']])
        package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
        package_id = pem.readBase64fromText(package_id_pem_text)

        self.assertEqual(package_id, receipt['receiptOf']['pkgID'])


class ErrorTestCase(unittest.TestCase):
    message3_pem_text = """\
MIIEbwYJKoZIhvcNAQcCoIIEYDCCBFwCAQMxDTALBglghkgBZQMEAgIwga0GCmCGSAFlAgEC
TgaggZ4EgZswgZigFgQUJ7icVjsWIlGdF4cceb+siG3f+D0wewYJYIZIAWUCARAABG4wbDEL
MAkGA1UEBhMCVVMxCzAJBgNVBAgTAlZBMRAwDgYDVQQHEwdIZXJuZG9uMRAwDgYDVQQKEwdF
eGFtcGxlMQwwCgYDVQQDEwNCb2IxHjAcBgkqhki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gXFTUc3ZInjt+MWYkYmXYERk4FgErEZNILlWgVl7Z9pImgLObIpdrGqGPt06/VkwwCgYI
KoZIzj0EAwMEZzBlAjEAsjJ3iWRUteMKBVsjaYeN6TG9NITRTOpRVkSVq55DcnhwS9g9lu8D
iNF8uKtW/lk0AjA7z2q40N0lamXkSU7ECasiWOYV1X4cWGiQwMZDKknBPDqXqB6Es6p4J+qe
0V6+BtY=
"""

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

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.message3_pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        sd, rest = der_decoder(
            asn1Object['content'], asn1Spec=rfc5652.SignedData())

        self.assertFalse(rest)
        self.assertTrue(sd.prettyPrint())
        self.assertEqual(asn1Object['content'], der_encoder(sd))

        oid = sd['encapContentInfo']['eContentType']

        self.assertEqual(rfc7191.id_ct_KP_keyPackageError, oid)

        kpe, rest = der_decoder(
            sd['encapContentInfo']['eContent'],
            asn1Spec=rfc7191.KeyPackageError())

        self.assertFalse(rest)
        self.assertTrue(kpe.prettyPrint())
        self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(kpe))

        package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
        package_id = pem.readBase64fromText(package_id_pem_text)

        self.assertEqual(package_id, kpe['errorOf']['pkgID'])
        self.assertEqual(
            rfc7191.EnumeratedErrorCode(value=10), kpe['errorCode'])

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

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])

        v3 = rfc5652.CMSVersion().subtype(value='v3')

        self.assertEqual(v3, asn1Object['content']['version'])

        for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
            self.assertIn(sa['attrType'], rfc5652.cmsAttributesMap)
            if sa['attrType'] == rfc5652.id_messageDigest:
                self.assertIn(
                    '0xa05c54d4737', sa['attrValues'][0].prettyPrint())

        ct_oid = asn1Object['content']['encapContentInfo']['eContentType']

        self.assertIn(ct_oid, rfc5652.cmsContentTypesMap)
        self.assertEqual(rfc7191.id_ct_KP_keyPackageError, ct_oid)

        # Since receipt is inside an OCTET STRING, decodeOpenTypes=True cannot
        # automatically decode it
        sd_eci = asn1Object['content']['encapContentInfo']
        kpe, rest = der_decoder(
            sd_eci['eContent'],
            asn1Spec=rfc5652.cmsContentTypesMap[sd_eci['eContentType']])
        package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
        package_id = pem.readBase64fromText(package_id_pem_text)

        self.assertEqual(package_id, kpe['errorOf']['pkgID'])
        self.assertEqual(rfc7191.EnumeratedErrorCode(value=10), kpe['errorCode'])


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

if __name__ == '__main__':
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())