File: examples.rst

package info (click to toggle)
python-asn1 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: python: 1,638; makefile: 4
file content (147 lines) | stat: -rw-r--r-- 4,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Examples
========

Dump
----

This command line utility reads a X509.v3 certificate (in DER or PEM encoding),
decodes it and outputs a textual representation. It more or less mimics
`openssl asn1parse <https://www.openssl.org/docs/man1.1.1/man1/openssl-asn1parse.html>`_:

.. code-block:: shell

  $ python examples/dump.py examples/test.crt

  [U] SEQUENCE
    [U] SEQUENCE
      [C] 0x0
        [U] INTEGER: 2
      [U] INTEGER: 5424
      [U] SEQUENCE
        [U] OBJECT: 1.2.840.113549.1.1.5
        [U] NULL: None
      [U] SEQUENCE
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.6
            [U] PRINTABLESTRING: --
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.8
            [U] PRINTABLESTRING: SomeState
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.7
            [U] PRINTABLESTRING: SomeCity
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.10
            [U] PRINTABLESTRING: SomeOrganization
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.11
            [U] PRINTABLESTRING: SomeOrganizationalUnit
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.3
            [U] PRINTABLESTRING: localhost.localdomain
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 1.2.840.113549.1.9.1
            [U] IA5STRING: root@localhost.localdomain
      [U] SEQUENCE
        [U] UTCTIME: 080205092331Z
        [U] UTCTIME: 090204092331Z
      [U] SEQUENCE
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.6
            [U] PRINTABLESTRING: --
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.8
            [U] PRINTABLESTRING: SomeState
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.7
            [U] PRINTABLESTRING: SomeCity
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.10
            [U] PRINTABLESTRING: SomeOrganization
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.11
            [U] PRINTABLESTRING: SomeOrganizationalUnit
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 2.5.4.3
            [U] PRINTABLESTRING: localhost.localdomain
        [U] SET
          [U] SEQUENCE
            [U] OBJECT: 1.2.840.113549.1.9.1
            [U] IA5STRING: root@localhost.localdomain
      [U] SEQUENCE
        [U] SEQUENCE
          [U] OBJECT: 1.2.840.113549.1.1.1
          [U] NULL: None
        [U] BIT STRING: 0xb'0030818902818100D51...10610203010001'
      [C] BIT STRING
        [U] SEQUENCE
          [U] SEQUENCE
            [U] OBJECT: 2.5.29.14
            [U] OCTET STRING: 0xb'04140A4BFA8754177E30B4217156510FD291C3300236'
          [U] SEQUENCE
            [U] OBJECT: 2.5.29.35
            [U] OCTET STRING: 0xb'3081DE80140A4...D61696E82021530'
          [U] SEQUENCE
            [U] OBJECT: 2.5.29.19
            [U] OCTET STRING: 0xb'30030101FF'
    [U] SEQUENCE
      [U] OBJECT: 1.2.840.113549.1.1.5
      [U] NULL: None
    [U] BIT STRING: 0xb'004E124658A357C59AABFA3...8A6245C7FC58B45A'

The main function is ``pretty_print``:

.. code-block:: python
   :linenos:

   def pretty_print(input_stream, output_stream, indent=0):
        """Pretty print ASN.1 data."""
        while not input_stream.eof():
            tag = input_stream.peek()
            if tag.typ == asn1.Types.Primitive:
                tag, value = input_stream.read()
                output_stream.write(' ' * indent)
                output_stream.write('[{}] {}: {}\n'.format(class_id_to_string(tag.cls),
                    tag_id_to_string(tag.nr), value_to_string(tag.nr, value)))
            elif tag.typ == asn1.Types.Constructed:
                output_stream.write(' ' * indent)
                output_stream.write('[{}] {}\n'.format(class_id_to_string(tag.cls),
                    tag_id_to_string(tag.nr)))
                input_stream.enter()
                pretty_print(input_stream, output_stream, indent + 2)
                input_stream.leave()

This code:

* **line 3**: Loops until its reaches the end of the input stream (with `Decoder.eof()`).

* **line 4**: Looks (with `Decoder.peek()`) at the current tag.

* **line 5**: If the tag is primitive (``TypePrimitive``) ...

* **line 6**: ... the code reads (with `Decoder.read()`) the current tag.

* **line 8**: Then it displays its number and value (after some mapping to be more user-fiendly).

* **line 10**: If the tag is constructed (``TypeConstructed``) ...

* **line 12**: ... the code displays its class and number.

* **line 14**: Then it enters inside the tag and ...

* **line 15**: ... calls itself recursively to decode the ASN.1 tags inside.

* **line 16**: Leaves the current constructed tag (with `Decoder.leave()`) to continue the decoding of its siblings.