• 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_modules import pem
15from pyasn1_modules import rfc5652
16from pyasn1_modules import rfc7030
17
18from pyasn1.type import univ
19
20try:
21    import unittest2 as unittest
22except ImportError:
23    import unittest
24
25
26class CSRAttrsTestCase(unittest.TestCase):
27    pem_text = """\
28MEEGCSqGSIb3DQEJBzASBgcqhkjOPQIBMQcGBSuBBAAiMBYGCSqGSIb3DQEJDjEJ
29BgcrBgEBAQEWBggqhkjOPQQDAw==
30"""
31
32    the_oids = (univ.ObjectIdentifier('1.2.840.113549.1.9.7'),
33                univ.ObjectIdentifier('1.2.840.10045.4.3.3'),
34    )
35
36    the_attrTypes = (univ.ObjectIdentifier('1.2.840.10045.2.1'),
37                     univ.ObjectIdentifier('1.2.840.113549.1.9.14'),
38    )
39
40    the_attrVals = ('1.3.132.0.34',
41                    '1.3.6.1.1.1.1.22',
42    )
43
44
45    def setUp(self):
46        self.asn1Spec = rfc7030.CsrAttrs()
47
48    def testDerCodec(self):
49        substrate = pem.readBase64fromText(self.pem_text)
50        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
51        assert not rest
52        assert asn1Object.prettyPrint()
53        assert der_encode(asn1Object) == substrate
54
55        for attr_or_oid in asn1Object:
56            if attr_or_oid.getName() == 'oid':
57                assert attr_or_oid['oid'] in self.the_oids
58
59            if attr_or_oid.getName() == 'attribute':
60                assert attr_or_oid['attribute']['attrType'] in self.the_attrTypes
61
62    def testOpenTypes(self):
63        openTypesMap = { }
64        openTypesMap.update(rfc5652.cmsAttributesMap)
65        for at in self.the_attrTypes:
66            openTypesMap.update({ at: univ.ObjectIdentifier(), })
67
68        substrate = pem.readBase64fromText(self.pem_text)
69        asn1Object, rest = der_decode(substrate,
70            asn1Spec=self.asn1Spec,
71            openTypes=openTypesMap,
72            decodeOpenTypes=True)
73        assert not rest
74        assert asn1Object.prettyPrint()
75        assert der_encode(asn1Object) == substrate
76
77        for attr_or_oid in asn1Object:
78            if attr_or_oid.getName() == 'attribute':
79                valString = attr_or_oid['attribute']['attrValues'][0].prettyPrint()
80
81                if attr_or_oid['attribute']['attrType'] == self.the_attrTypes[0]:
82                    assert valString == self.the_attrVals[0]
83
84                if attr_or_oid['attribute']['attrType'] == self.the_attrTypes[1]:
85                    assert valString == self.the_attrVals[1]
86
87
88suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
89
90if __name__ == '__main__':
91    unittest.TextTestRunner(verbosity=2).run(suite)
92