• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _type.opentype:
3
4Dynamic or open type
5--------------------
6
7ASN.1 allows data structure designer to leave "holes" in field type
8specification of :ref:`Sequence <univ.Sequence>` or
9:ref:`Set <univ.Set>` types.
10
11The idea behind that feature is that there can be times, when the
12exact field type is not known at the design time, or it is anticipated
13that new field types may come up in the future.
14
15This "hole" type is manifested in the data structure by :ref:`Any <univ.Any>`
16type. Technically, the actual type is serialized into an octet stream
17and then put into :ref:`Any <univ.Any>` "container", which is in fact an
18(untagged, by default) specialization of ASN.1
19:ref:`OctetString <univ.OctetString>` type.
20
21.. code-block:: bash
22
23       Algorithm ::= SEQUENCE {
24               algorithm OBJECT IDENTIFIER,
25               parameters ANY DEFINED BY algorithm OPTIONAL
26       }
27
28On the receiving end, to know how to interpret the open type
29serialization, the receiver can rely on the supplied value in
30the other field of the data structure. That other field is
31semantically linked with the open type field. This link
32is expressed in ASN.1 by the *DEFINE BY* clause.
33
34From ASN.1 perspective, it is not an error if the decoder does
35not know a type selector value it receives. In that case pyasn1 decoder
36just leaves serialized blob in the open type field.
37
38.. note::
39
40   By default, ASN.1 ANY type has no tag. That makes it an
41   "invisible" in serialization. However, like any other ASN.1 type,
42   ANY type can be subtyped by :ref:`tagging <type.tag>`.
43
44Besides scalar open type fields, ASN.1 allows using *SET OF*
45or *SEQUENCE OF* containers holding zero or more of *ANY*
46scalars.
47
48.. code-block:: bash
49
50   AttributeTypeAndValues ::= SEQUENCE {
51      type OBJECT IDENTIFIER,
52      values SET OF ANY DEFINED BY type
53   }
54
55.. note::
56
57   A single type selector field is used to guide the decoder
58   of potentially many elements of a *SET OF* or *SEQUENCE OF* container
59   all at once. That implies that all *ANY* elements must be of the same
60   type in any given instance of a data structure.
61
62When expressing ASN.1 type "holes" in pyasn1, the
63:ref:`OpenType <opentype.OpenType>` object should be used to establish
64a semantic link between type selector field and open type field.
65
66.. code-block:: python
67
68   algo_map = {
69       ObjectIdentifier('1.2.840.113549.1.1.1'): rsaEncryption(),
70       ObjectIdentifier('1.2.840.113549.1.1.2'): md2WithRSAEncryption()
71   }
72
73
74   class Algorithm(Sequence):
75       """
76       Algorithm ::= SEQUENCE {
77               algorithm OBJECT IDENTIFIER,
78               parameters ANY DEFINED BY algorithm
79       }
80       """
81       componentType = NamedTypes(
82           NamedType('algorithm', ObjectIdentifier()),
83           NamedType('parameters', Any(),
84                     openType=OpenType('algorithm', algo_map))
85       )
86
87Similarly for `SET OF ANY DEFINED BY` or `SEQUENCE OF ANY DEFINED BY`
88constructs:
89
90.. code-block:: python
91
92   algo_map = {
93       ObjectIdentifier('1.2.840.113549.1.1.1'): rsaEncryption(),
94       ObjectIdentifier('1.2.840.113549.1.1.2'): md2WithRSAEncryption()
95   }
96
97
98   class Algorithm(Sequence):
99       """
100       Algorithm ::= SEQUENCE {
101               algorithm OBJECT IDENTIFIER,
102               parameters SET OF ANY DEFINED BY algorithm
103       }
104       """
105       componentType = NamedTypes(
106           NamedType('algorithm', ObjectIdentifier()),
107           NamedType('parameters', SetOf(componentType=Any()),
108                     openType=OpenType('algorithm', algo_map))
109       )
110
111.. toctree::
112   :maxdepth: 2
113
114   /pyasn1/type/opentype/opentype
115