• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1-modules software.
3#
4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7import sys
8
9from pyasn1.codec.der import decoder as der_decoder
10from pyasn1.codec.der import encoder as der_encoder
11
12from pyasn1.type import char
13from pyasn1.type import namedtype
14from pyasn1.type import univ
15
16from pyasn1_modules import pem
17from pyasn1_modules import rfc5280
18from pyasn1_modules import rfc5652
19from pyasn1_modules import rfc6402
20
21try:
22    import unittest2 as unittest
23
24except ImportError:
25    import unittest
26
27
28class ContentInfoTestCase(unittest.TestCase):
29    pem_text = """\
30MIIEJQYJKoZIhvcNAQcCoIIEFjCCBBICAQMxCzAJBgUrDgMCGgUAMIIDAgYIKwYBBQUHDAKgggL0
31BIIC8DCCAuwweDB2AgECBgorBgEEAYI3CgoBMWUwYwIBADADAgEBMVkwVwYJKwYBBAGCNxUUMUow
32SAIBBQwZcGl0dWNoYTEuZW1lYS5ocHFjb3JwLm5ldAwMRU1FQVxwaXR1Y2hhDBpDTUNSZXFHZW5l
33cmF0b3IudnNob3N0LmV4ZTCCAmqgggJmAgEBMIICXzCCAcgCAQAwADCBnzANBgkqhkiG9w0BAQEF
34AAOBjQAwgYkCgYEA0jm7SSSm2wyEAzuNKtFZFJKo91SrJq9wQwEhEKHDavZwMQOm1rZ2PF8NWCEb
35PqrhToQ7rtiGLSZa4dF4bzgmBqQ9aoSfEX4jISt31Vy+skHidXjHHpbsjT24NPhrZgANivL7CxD6
36Ft+s7qS1gL4HRm2twQkqSwOLrE/q2QeXl2UCAwEAAaCCAR0wGgYKKwYBBAGCNw0CAzEMFgo2LjIu
37OTIwMC4yMD4GCSqGSIb3DQEJDjExMC8wHQYDVR0OBBYEFMW2skn88gxhONWZQA4sWGBDb68yMA4G
38A1UdDwEB/wQEAwIHgDBXBgkrBgEEAYI3FRQxSjBIAgEFDBlwaXR1Y2hhMS5lbWVhLmhwcWNvcnAu
39bmV0DAxFTUVBXHBpdHVjaGEMGkNNQ1JlcUdlbmVyYXRvci52c2hvc3QuZXhlMGYGCisGAQQBgjcN
40AgIxWDBWAgECHk4ATQBpAGMAcgBvAHMAbwBmAHQAIABTAHQAcgBvAG4AZwAgAEMAcgB5AHAAdABv
41AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIDAQAwDQYJKoZIhvcNAQEFBQADgYEAJZlu
42mxjtCxSOQi27jsVdd3y8NSIlzNv0b3LqmzvAly6L+CstXcnuG2MPQqPH9R7tbJonGUniBQO9sQ7C
43KhYWj2gfhiEkSID82lV5chINVUFKoUlSiEhWr0tPGgvOaqdsKQcrHfzrsBbFkhDqrFSVy7Yivbnh
44qYszKrOjJKiiCPMwADAAMYH5MIH2AgEDgBTFtrJJ/PIMYTjVmUAOLFhgQ2+vMjAJBgUrDgMCGgUA
45oD4wFwYJKoZIhvcNAQkDMQoGCCsGAQUFBwwCMCMGCSqGSIb3DQEJBDEWBBTFTkK/OifaFjwqHiJu
46xM7qXcg/VzANBgkqhkiG9w0BAQEFAASBgKfC6jOi1Wgy4xxDCQVK9+e5tktL8wE/j2cb9JSqq+aU
475UxEgXEw7q7BoYZCAzcxMRriGzakXr8aXHcgkRJ7XcFvLPUjpmGg9SOZ2sGW4zQdWAwImN/i8loc
48xicQmJP+VoMHo/ZpjFY9fYCjNZUArgKsEwK/s+p9yrVVeB1Nf8Mn
49"""
50
51    def setUp(self):
52        self.asn1Spec = rfc5652.ContentInfo()
53
54    def testDerCodec(self):
55
56        substrate = pem.readBase64fromText(self.pem_text)
57
58        layers = {
59            rfc5652.id_ct_contentInfo: rfc5652.ContentInfo(),
60            rfc5652.id_signedData: rfc5652.SignedData(),
61            rfc6402.id_cct_PKIData: rfc6402.PKIData()
62        }
63
64        getNextLayer = {
65            rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
66            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
67            rfc6402.id_cct_PKIData: lambda x: None
68        }
69
70        getNextSubstrate = {
71            rfc5652.id_ct_contentInfo: lambda x: x['content'],
72            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
73            rfc6402.id_cct_PKIData: lambda x: None
74        }
75
76        next_layer = rfc5652.id_ct_contentInfo
77
78        while next_layer:
79
80            asn1Object, rest = der_decoder.decode(
81                substrate, asn1Spec=layers[next_layer]
82            )
83
84            assert not rest
85            assert asn1Object.prettyPrint()
86            assert der_encoder.encode(asn1Object) == substrate
87
88            substrate = getNextSubstrate[next_layer](asn1Object)
89            next_layer = getNextLayer[next_layer](asn1Object)
90
91    def testOpenTypes(self):
92        class ClientInformation(univ.Sequence):
93            pass
94
95        ClientInformation.componentType = namedtype.NamedTypes(
96            namedtype.NamedType('clientId', univ.Integer()),
97            namedtype.NamedType('MachineName', char.UTF8String()),
98            namedtype.NamedType('UserName', char.UTF8String()),
99            namedtype.NamedType('ProcessName', char.UTF8String())
100        )
101
102        class EnrollmentCSP(univ.Sequence):
103            pass
104
105        EnrollmentCSP.componentType = namedtype.NamedTypes(
106            namedtype.NamedType('KeySpec', univ.Integer()),
107            namedtype.NamedType('Name', char.BMPString()),
108            namedtype.NamedType('Signature', univ.BitString())
109        )
110
111        openTypeMap = {
112            # attributes
113            univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.3'): char.IA5String(),
114            univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.2'): EnrollmentCSP(),
115            univ.ObjectIdentifier('1.3.6.1.4.1.311.21.20'): ClientInformation(),
116            # algorithm identifier parameters
117            univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
118            univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
119            univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
120        }
121
122        openTypeMap.update(rfc5652.cmsAttributesMap)
123        openTypeMap.update(rfc6402.cmcControlAttributesMap)
124
125        substrate = pem.readBase64fromText(self.pem_text)
126        asn1Object, rest = der_decoder.decode(substrate,
127            asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)
128        assert not rest
129        assert asn1Object.prettyPrint()
130        assert der_encoder.encode(asn1Object) == substrate
131
132        eci = asn1Object['content']['encapContentInfo']
133        assert eci['eContentType'] in rfc5652.cmsContentTypesMap.keys()
134        assert eci['eContentType'] == rfc6402.id_cct_PKIData
135        pkid, rest = der_decoder.decode(eci['eContent'],
136            asn1Spec=rfc5652.cmsContentTypesMap[eci['eContentType']],
137            openTypes=openTypeMap,
138            decodeOpenTypes=True)
139        assert not rest
140        assert pkid.prettyPrint()
141        assert der_encoder.encode(pkid) == eci['eContent']
142
143        for req in pkid['reqSequence']:
144            cr = req['tcr']['certificationRequest']
145
146            sig_alg = cr['signatureAlgorithm']
147            assert sig_alg['algorithm'] in openTypeMap.keys()
148            assert sig_alg['parameters'] == univ.Null("")
149
150            cri = cr['certificationRequestInfo']
151            spki_alg = cri['subjectPublicKeyInfo']['algorithm']
152            assert spki_alg['algorithm'] in openTypeMap.keys()
153            assert spki_alg['parameters'] == univ.Null("")
154
155            attrs = cr['certificationRequestInfo']['attributes']
156            for attr in attrs:
157                assert attr['attrType'] in openTypeMap.keys()
158                if attr['attrType'] == univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.3'):
159                    assert attr['attrValues'][0] == "6.2.9200.2"
160                else:
161                    assert attr['attrValues'][0].hasValue()
162
163
164suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
165
166if __name__ == '__main__':
167    import sys
168
169    result = unittest.TextTestRunner(verbosity=2).run(suite)
170    sys.exit(not result.wasSuccessful())
171