aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyasn1-modules/py3/tests/test_rfc6032.py
blob: 287bad89ae18d8928220ee1b7b59b531e2888e0f (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
#
# 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.compat.octets import str2octs

from pyasn1_modules import pem
from pyasn1_modules import rfc5652
from pyasn1_modules import rfc6032


class EncryptedKeyPkgTestCase(unittest.TestCase):
    encrypted_key_pkg_pem_text = """\
MIIBBwYKYIZIAWUCAQJOAqCB+DCB9QIBAjCBzgYKYIZIAWUCAQJOAjAdBglghkgB
ZQMEASoEEN6HFteHMZ3DyeO35xIwWQOAgaCKTs0D0HguNzMhsLgiwG/Kw8OwX+GF
9/cZ1YVNesUTW/VsbXJcbTmFmWyfqZsM4DLBegIbrUEHQZnQRq6/NO4ricQdHApD
B/ip6RRqeN1yxMJLv1YN0zUOOIDBS2iMEjTLXZLWw3w22GN2JK7G+Lr4OH1NhMgU
ILJyh/RePmPseMwxvcJs7liEfkiSNMtDfEcpjtzA9bDe95GjhQRsiSByoR8wHQYJ
YIZIAWUCAQVCMRAEDnB0Zi1rZGMtODEyMzc0
"""

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

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.encrypted_key_pkg_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(
            rfc6032.id_ct_KP_encryptedKeyPkg, asn1Object['contentType'])

        content, rest = der_decoder(
            asn1Object['content'], rfc6032.EncryptedKeyPackage())

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

        eci = content['encrypted']['encryptedContentInfo']

        self.assertEqual(
            rfc6032.id_ct_KP_encryptedKeyPkg, eci['contentType'])

        attrType = content['encrypted']['unprotectedAttrs'][0]['attrType']

        self.assertEqual(rfc6032.id_aa_KP_contentDecryptKeyID, attrType)

        attrVal0 = content['encrypted']['unprotectedAttrs'][0]['attrValues'][0]
        keyid, rest = der_decoder(attrVal0, rfc6032.ContentDecryptKeyID())

        self.assertFalse(rest)
        self.assertTrue(keyid.prettyPrint())
        self.assertEqual(attrVal0, der_encoder(keyid))
        self.assertEqual(str2octs('ptf-kdc-812374'), keyid)

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.encrypted_key_pkg_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.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap)

        eci = asn1Object['content']['encrypted']['encryptedContentInfo']

        self.assertIn(eci['contentType'], rfc5652.cmsContentTypesMap)

        for attr in asn1Object['content']['encrypted']['unprotectedAttrs']:
            self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
            self.assertNotEqual('0x', attr['attrValues'][0].prettyPrint()[:2])

            if attr['attrType'] == rfc6032.id_aa_KP_contentDecryptKeyID:
                self.assertEqual(str2octs(
                    'ptf-kdc-812374'), attr['attrValues'][0])


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

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