aboutsummaryrefslogtreecommitdiff
path: root/tests/test_rfc7292.py
blob: 8bebef319c35576683502351d761000b9b4597b9 (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
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
#
# 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

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 rfc7292

try:
    import unittest2 as unittest
except ImportError:
    import unittest


class PKCS12TestCase(unittest.TestCase):
    pfx_pem_text = """\
MIIJ0wIBAzCCCY8GCSqGSIb3DQEHAaCCCYAEggl8MIIJeDCCBggGCSqGSIb3DQEHAaCCBfkE
ggX1MIIF8TCCBe0GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAjuq0/+
0pyutQICB9AEggTYZe/mYBpmkDvKsve4EwIVwo1TNv4ldyx1qHZW2Ih6qQCY+Nv1Mnv9we0z
UTl4p3tQzCPWXnrSA82IgOdotLIez4YwXrgiKhcIkSSL+2yCmAoM+qkjiAIKq+l3UJ6Xhafe
2Kg4Ek/0RkHpe6GwjTtdefkpXpZgccMEopOtKQMLJWsDM7p77x/amn6yIk2tpskKqUY/4n8Y
xEiTWc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shLuUTUUfK
jx4Xif4LoS0/ev4BiOI5a1MlIRZ7T5Cyjg8bvuympzMuinQ/j1RPLIV0VGU2HuDxuuP3O898
GqZ3+F6Al5CUcwmOX9zCs91JdN/ZFZ05SXIpHQuyPSPUX5Vy8F1ZeJ8VG3nkbemfFlVkuKQq
vteL9mlT7z95rVZgGB3nUZL0tOB68eMcffA9zUksOmeTi5M6jnBcNeX2Jh9jS3YYd+IEliZm
mggQG7kPta8f+NqezL77AgMBAAGjVTBTMBUGA1UdJQQOMAwGCisGAQQBgjcKAwQwLwYDVR0R
BCgwJqAkBgorBgEEAYI3FAIDoBYMFGFub255bW91c0B3aW5kb3dzLXgAMAkGA1UdEwQCMAAw
DQYJKoZIhvcNAQEFBQADggEBALh+4qmNPzC6M8BW9/SC2ACQxxPh06GQUGx0D+GLYnp61ErZ
OtKyKdFh+uZWpu5vyYYAHCLXP7VdS/JhJy677ynAPjXiC/LAzrTNvGs74HDotD966Hiyy0Qr
ospFGiplHGRA5vXA2CiKSX+0HrVkN7rhk5PYkc6R+/cdosd+QZ8lkEa9yDWc5l//vWEbzwVy
mJf/PRf8NTkWAK6SPV7Y37j1mhkJjOH9VkRxNrd6kcihRa4u0ImXaXEsec77ER0so31DKCrP
m+rqZPj9NZSIYP3sMGJ4Bmm/n2YRdeaUzTdocfD3TRnKxs65DSgpiSq1gmtsXM7jAPs/Egrg
tbWEypgxFTATBgkqhkiG9w0BCRUxBgQEAQAAADA7MB8wBwYFKw4DAhoEFKVgj/32UdEyuQcB
rqr03dPnboinBBSU7mxdpB5LTCvorCI8Tk5OMiUzjgICB9A=
"""

    def setUp(self):
        self.asn1Spec = rfc7292.PFX()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pfx_pem_text)
        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['version'] == univ.Integer(3)
        oid = asn1Object['macData']['mac']['digestAlgorithm']['algorithm']
        assert oid ==  univ.ObjectIdentifier('1.3.14.3.2.26')
        md_hex = asn1Object['macData']['mac']['digest'].prettyPrint()
        assert md_hex == '0xa5608ffdf651d132b90701aeaaf4ddd3e76e88a7'

        assert asn1Object['authSafe']['contentType'] == rfc5652.id_data
        data, rest = der_decode(asn1Object['authSafe']['content'], 
            asn1Spec=univ.OctetString())
        assert not rest

        authsafe, rest = der_decode(data, asn1Spec=rfc7292.AuthenticatedSafe())
        assert not rest
        assert authsafe.prettyPrint()
        assert der_encode(authsafe) == data

        for ci in authsafe:
            assert ci['contentType'] == rfc5652.id_data
            data, rest = der_decode(ci['content'], asn1Spec=univ.OctetString())
            assert not rest

            sc, rest = der_decode(data, asn1Spec=rfc7292.SafeContents())
            assert not rest
            assert sc.prettyPrint()
            assert der_encode(sc) == data

            for sb in sc:
                if sb['bagId'] in rfc7292.pkcs12BagTypeMap:
                    bv, rest = der_decode(sb['bagValue'],
                       asn1Spec=rfc7292.pkcs12BagTypeMap[sb['bagId']])
                    assert not rest
                    assert bv.prettyPrint()
                    assert der_encode(bv) == sb['bagValue']

                    for attr in sb['bagAttributes']:
                        if attr['attrType'] in rfc5652.cmsAttributesMap:
                            av, rest = der_decode(attr['attrValues'][0],
                                asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
                            assert not rest
                            assert av.prettyPrint()
                            assert der_encode(av) == attr['attrValues'][0]

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

        digest_alg = asn1Object['macData']['mac']['digestAlgorithm']
        assert not digest_alg['parameters'].hasValue()

        authsafe, rest = der_decode(asn1Object['authSafe']['content'],
            asn1Spec=rfc7292.AuthenticatedSafe(),
            decodeOpenTypes=True
        )
        assert not rest
        assert authsafe.prettyPrint()
        assert der_encode(authsafe) == asn1Object['authSafe']['content']

        for ci in authsafe:
            assert ci['contentType'] == rfc5652.id_data
            sc, rest = der_decode(ci['content'],
                asn1Spec=rfc7292.SafeContents(),
                decodeOpenTypes=True
            )
            assert not rest
            assert sc.prettyPrint()
            assert der_encode(sc) == ci['content']
    
            for sb in sc:
                if sb['bagId'] == rfc7292.id_pkcs8ShroudedKeyBag:
                    bv = sb['bagValue']
                    enc_alg = bv['encryptionAlgorithm']['algorithm']
                    assert enc_alg == rfc7292.pbeWithSHAAnd3_KeyTripleDES_CBC
                    enc_alg_param = bv['encryptionAlgorithm']['parameters']
                    assert enc_alg_param['iterations'] == 2000


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

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)