1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7from pyasn1 import debug
8from pyasn1 import error
9from pyasn1.type import base
10from pyasn1.type import char
11from pyasn1.type import tag
12from pyasn1.type import univ
13from pyasn1.type import useful
14
15__all__ = ['decode']
16
17LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER)
18
19
20class AbstractScalarDecoder(object):
21    def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
22        return asn1Spec.clone(pyObject)
23
24
25class BitStringDecoder(AbstractScalarDecoder):
26    def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
27        return asn1Spec.clone(univ.BitString.fromBinaryString(pyObject))
28
29
30class SequenceOrSetDecoder(object):
31    def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
32        asn1Value = asn1Spec.clone()
33
34        componentsTypes = asn1Spec.componentType
35
36        for field in asn1Value:
37            if field in pyObject:
38                asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
39
40        return asn1Value
41
42
43class SequenceOfOrSetOfDecoder(object):
44    def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
45        asn1Value = asn1Spec.clone()
46
47        for pyValue in pyObject:
48            asn1Value.append(decodeFun(pyValue, asn1Spec.componentType), **options)
49
50        return asn1Value
51
52
53class ChoiceDecoder(object):
54    def __call__(self, pyObject, asn1Spec, decodeFun=None, **options):
55        asn1Value = asn1Spec.clone()
56
57        componentsTypes = asn1Spec.componentType
58
59        for field in pyObject:
60            if field in componentsTypes:
61                asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options)
62                break
63
64        return asn1Value
65
66
67tagMap = {
68    univ.Integer.tagSet: AbstractScalarDecoder(),
69    univ.Boolean.tagSet: AbstractScalarDecoder(),
70    univ.BitString.tagSet: BitStringDecoder(),
71    univ.OctetString.tagSet: AbstractScalarDecoder(),
72    univ.Null.tagSet: AbstractScalarDecoder(),
73    univ.ObjectIdentifier.tagSet: AbstractScalarDecoder(),
74    univ.Enumerated.tagSet: AbstractScalarDecoder(),
75    univ.Real.tagSet: AbstractScalarDecoder(),
76    univ.Sequence.tagSet: SequenceOrSetDecoder(),  # conflicts with SequenceOf
77    univ.Set.tagSet: SequenceOrSetDecoder(),  # conflicts with SetOf
78    univ.Choice.tagSet: ChoiceDecoder(),  # conflicts with Any
79    # character string types
80    char.UTF8String.tagSet: AbstractScalarDecoder(),
81    char.NumericString.tagSet: AbstractScalarDecoder(),
82    char.PrintableString.tagSet: AbstractScalarDecoder(),
83    char.TeletexString.tagSet: AbstractScalarDecoder(),
84    char.VideotexString.tagSet: AbstractScalarDecoder(),
85    char.IA5String.tagSet: AbstractScalarDecoder(),
86    char.GraphicString.tagSet: AbstractScalarDecoder(),
87    char.VisibleString.tagSet: AbstractScalarDecoder(),
88    char.GeneralString.tagSet: AbstractScalarDecoder(),
89    char.UniversalString.tagSet: AbstractScalarDecoder(),
90    char.BMPString.tagSet: AbstractScalarDecoder(),
91    # useful types
92    useful.ObjectDescriptor.tagSet: AbstractScalarDecoder(),
93    useful.GeneralizedTime.tagSet: AbstractScalarDecoder(),
94    useful.UTCTime.tagSet: AbstractScalarDecoder()
95}
96
97# Put in ambiguous & non-ambiguous types for faster codec lookup
98typeMap = {
99    univ.Integer.typeId: AbstractScalarDecoder(),
100    univ.Boolean.typeId: AbstractScalarDecoder(),
101    univ.BitString.typeId: BitStringDecoder(),
102    univ.OctetString.typeId: AbstractScalarDecoder(),
103    univ.Null.typeId: AbstractScalarDecoder(),
104    univ.ObjectIdentifier.typeId: AbstractScalarDecoder(),
105    univ.Enumerated.typeId: AbstractScalarDecoder(),
106    univ.Real.typeId: AbstractScalarDecoder(),
107    # ambiguous base types
108    univ.Set.typeId: SequenceOrSetDecoder(),
109    univ.SetOf.typeId: SequenceOfOrSetOfDecoder(),
110    univ.Sequence.typeId: SequenceOrSetDecoder(),
111    univ.SequenceOf.typeId: SequenceOfOrSetOfDecoder(),
112    univ.Choice.typeId: ChoiceDecoder(),
113    univ.Any.typeId: AbstractScalarDecoder(),
114    # character string types
115    char.UTF8String.typeId: AbstractScalarDecoder(),
116    char.NumericString.typeId: AbstractScalarDecoder(),
117    char.PrintableString.typeId: AbstractScalarDecoder(),
118    char.TeletexString.typeId: AbstractScalarDecoder(),
119    char.VideotexString.typeId: AbstractScalarDecoder(),
120    char.IA5String.typeId: AbstractScalarDecoder(),
121    char.GraphicString.typeId: AbstractScalarDecoder(),
122    char.VisibleString.typeId: AbstractScalarDecoder(),
123    char.GeneralString.typeId: AbstractScalarDecoder(),
124    char.UniversalString.typeId: AbstractScalarDecoder(),
125    char.BMPString.typeId: AbstractScalarDecoder(),
126    # useful types
127    useful.ObjectDescriptor.typeId: AbstractScalarDecoder(),
128    useful.GeneralizedTime.typeId: AbstractScalarDecoder(),
129    useful.UTCTime.typeId: AbstractScalarDecoder()
130}
131
132
133class Decoder(object):
134
135    # noinspection PyDefaultArgument
136    def __init__(self, tagMap, typeMap):
137        self.__tagMap = tagMap
138        self.__typeMap = typeMap
139
140    def __call__(self, pyObject, asn1Spec, **options):
141
142        if LOG:
143            debug.scope.push(type(pyObject).__name__)
144            LOG('decoder called at scope %s, working with type %s' % (debug.scope, type(pyObject).__name__))
145
146        if asn1Spec is None or not isinstance(asn1Spec, base.Asn1Item):
147            raise error.PyAsn1Error('asn1Spec is not valid (should be an instance of an ASN.1 Item, not %s)' % asn1Spec.__class__.__name__)
148
149        try:
150            valueDecoder = self.__typeMap[asn1Spec.typeId]
151
152        except KeyError:
153            # use base type for codec lookup to recover untagged types
154            baseTagSet = tag.TagSet(asn1Spec.tagSet.baseTag, asn1Spec.tagSet.baseTag)
155
156            try:
157                valueDecoder = self.__tagMap[baseTagSet]
158            except KeyError:
159                raise error.PyAsn1Error('Unknown ASN.1 tag %s' % asn1Spec.tagSet)
160
161        if LOG:
162            LOG('calling decoder %s on Python type %s <%s>' % (type(valueDecoder).__name__, type(pyObject).__name__, repr(pyObject)))
163
164        value = valueDecoder(pyObject, asn1Spec, self, **options)
165
166        if LOG:
167            LOG('decoder %s produced ASN.1 type %s <%s>' % (type(valueDecoder).__name__, type(value).__name__, repr(value)))
168            debug.scope.pop()
169
170        return value
171
172
173#: Turns Python objects of built-in types into ASN.1 objects.
174#:
175#: Takes Python objects of built-in types and turns them into a tree of
176#: ASN.1 objects (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
177#: may be a scalar or an arbitrary nested structure.
178#:
179#: Parameters
180#: ----------
181#: pyObject: :py:class:`object`
182#:     A scalar or nested Python objects
183#:
184#: Keyword Args
185#: ------------
186#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
187#:     A pyasn1 type object to act as a template guiding the decoder. It is required
188#:     for successful interpretation of Python objects mapping into their ASN.1
189#:     representations.
190#:
191#: Returns
192#: -------
193#: : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
194#:     A scalar or constructed pyasn1 object
195#:
196#: Raises
197#: ------
198#: ~pyasn1.error.PyAsn1Error
199#:     On decoding errors
200#:
201#: Examples
202#: --------
203#: Decode native Python object into ASN.1 objects with ASN.1 schema
204#:
205#: .. code-block:: pycon
206#:
207#:    >>> seq = SequenceOf(componentType=Integer())
208#:    >>> s, _ = decode([1, 2, 3], asn1Spec=seq)
209#:    >>> str(s)
210#:    SequenceOf:
211#:     1 2 3
212#:
213decode = Decoder(tagMap, typeMap)
214