• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1-modules software.
3#
4# Created by Russ Housley
5# Copyright (c) 2019, Vigil Security, LLC
6# License: http://snmplabs.com/pyasn1/license.html
7#
8
9import sys
10
11from pyasn1.codec.der.decoder import decode as der_decode
12from pyasn1.codec.der.encoder import encode as der_encode
13
14from pyasn1.type import univ
15
16from pyasn1_modules import pem
17from pyasn1_modules import rfc3852
18from pyasn1_modules import rfc6402
19
20try:
21    import unittest2 as unittest
22
23except ImportError:
24    import unittest
25
26
27class ContentInfoTestCase(unittest.TestCase):
28    pem_text = """\
29MIIEJQYJKoZIhvcNAQcCoIIEFjCCBBICAQMxCzAJBgUrDgMCGgUAMIIDAgYIKwYBBQUHDAKgggL0
30BIIC8DCCAuwweDB2AgECBgorBgEEAYI3CgoBMWUwYwIBADADAgEBMVkwVwYJKwYBBAGCNxUUMUow
31SAIBBQwZcGl0dWNoYTEuZW1lYS5ocHFjb3JwLm5ldAwMRU1FQVxwaXR1Y2hhDBpDTUNSZXFHZW5l
32cmF0b3IudnNob3N0LmV4ZTCCAmqgggJmAgEBMIICXzCCAcgCAQAwADCBnzANBgkqhkiG9w0BAQEF
33AAOBjQAwgYkCgYEA0jm7SSSm2wyEAzuNKtFZFJKo91SrJq9wQwEhEKHDavZwMQOm1rZ2PF8NWCEb
34PqrhToQ7rtiGLSZa4dF4bzgmBqQ9aoSfEX4jISt31Vy+skHidXjHHpbsjT24NPhrZgANivL7CxD6
35Ft+s7qS1gL4HRm2twQkqSwOLrE/q2QeXl2UCAwEAAaCCAR0wGgYKKwYBBAGCNw0CAzEMFgo2LjIu
36OTIwMC4yMD4GCSqGSIb3DQEJDjExMC8wHQYDVR0OBBYEFMW2skn88gxhONWZQA4sWGBDb68yMA4G
37A1UdDwEB/wQEAwIHgDBXBgkrBgEEAYI3FRQxSjBIAgEFDBlwaXR1Y2hhMS5lbWVhLmhwcWNvcnAu
38bmV0DAxFTUVBXHBpdHVjaGEMGkNNQ1JlcUdlbmVyYXRvci52c2hvc3QuZXhlMGYGCisGAQQBgjcN
39AgIxWDBWAgECHk4ATQBpAGMAcgBvAHMAbwBmAHQAIABTAHQAcgBvAG4AZwAgAEMAcgB5AHAAdABv
40AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIDAQAwDQYJKoZIhvcNAQEFBQADgYEAJZlu
41mxjtCxSOQi27jsVdd3y8NSIlzNv0b3LqmzvAly6L+CstXcnuG2MPQqPH9R7tbJonGUniBQO9sQ7C
42KhYWj2gfhiEkSID82lV5chINVUFKoUlSiEhWr0tPGgvOaqdsKQcrHfzrsBbFkhDqrFSVy7Yivbnh
43qYszKrOjJKiiCPMwADAAMYH5MIH2AgEDgBTFtrJJ/PIMYTjVmUAOLFhgQ2+vMjAJBgUrDgMCGgUA
44oD4wFwYJKoZIhvcNAQkDMQoGCCsGAQUFBwwCMCMGCSqGSIb3DQEJBDEWBBTFTkK/OifaFjwqHiJu
45xM7qXcg/VzANBgkqhkiG9w0BAQEFAASBgKfC6jOi1Wgy4xxDCQVK9+e5tktL8wE/j2cb9JSqq+aU
465UxEgXEw7q7BoYZCAzcxMRriGzakXr8aXHcgkRJ7XcFvLPUjpmGg9SOZ2sGW4zQdWAwImN/i8loc
47xicQmJP+VoMHo/ZpjFY9fYCjNZUArgKsEwK/s+p9yrVVeB1Nf8Mn
48"""
49
50    def setUp(self):
51        self.asn1Spec = rfc3852.ContentInfo()
52
53    def testDerCodec(self):
54        substrate = pem.readBase64fromText(self.pem_text)
55
56        layers = {
57            rfc3852.id_ct_contentInfo: rfc3852.ContentInfo(),
58            rfc3852.id_signedData: rfc3852.SignedData(),
59            rfc6402.id_cct_PKIData: rfc6402.PKIData()
60        }
61
62        getNextLayer = {
63            rfc3852.id_ct_contentInfo: lambda x: x['contentType'],
64            rfc3852.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
65            rfc6402.id_cct_PKIData: lambda x: None
66        }
67
68        getNextSubstrate = {
69            rfc3852.id_ct_contentInfo: lambda x: x['content'],
70            rfc3852.id_signedData: lambda x: x['encapContentInfo']['eContent'],
71            rfc6402.id_cct_PKIData: lambda x: None
72        }
73
74        alg_oids = (
75            univ.ObjectIdentifier('1.3.14.3.2.26'),
76            univ.ObjectIdentifier('1.2.840.113549.1.1.1'),
77            univ.ObjectIdentifier('1.2.840.113549.1.1.5'),
78            univ.ObjectIdentifier('1.2.840.113549.1.1.11'),
79        )
80
81        encoded_null = der_encode(univ.Null(""))
82
83        next_layer = rfc3852.id_ct_contentInfo
84
85        count = 0
86        while next_layer:
87            asn1Object, rest = der_decode(substrate, asn1Spec=layers[next_layer])
88            assert not rest
89            assert asn1Object.prettyPrint()
90            assert der_encode(asn1Object) == substrate
91
92            if next_layer == rfc3852.id_signedData:
93               for d in asn1Object['digestAlgorithms']:
94                   assert d['algorithm'] in alg_oids
95                   assert d['parameters'] == encoded_null
96                   count += 1
97
98               for si in asn1Object['signerInfos']:
99                   assert si['digestAlgorithm']['algorithm'] in alg_oids
100                   assert si['digestAlgorithm']['parameters'] == encoded_null
101                   count += 1
102
103                   assert si['signatureAlgorithm']['algorithm'] in alg_oids
104                   assert si['signatureAlgorithm']['parameters'] == encoded_null
105                   count += 1
106
107            if next_layer == rfc6402.id_cct_PKIData:
108                for req in asn1Object['reqSequence']:
109                    cr = req['tcr']['certificationRequest']
110                    assert cr['signatureAlgorithm']['algorithm'] in alg_oids
111                    assert cr['signatureAlgorithm']['parameters'] == encoded_null
112                    count += 1
113
114                    cri_spki = cr['certificationRequestInfo']['subjectPublicKeyInfo']
115                    assert cri_spki['algorithm']['algorithm'] in alg_oids
116                    assert cri_spki['algorithm']['parameters'] == encoded_null
117                    count += 1
118
119            substrate = getNextSubstrate[next_layer](asn1Object)
120            next_layer = getNextLayer[next_layer](asn1Object)
121
122        assert count == 5
123
124
125suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
126
127if __name__ == '__main__':
128    import sys
129
130    result = unittest.TextTestRunner(verbosity=2).run(suite)
131    sys.exit(not result.wasSuccessful())
132