Skip to content

Commit 231c54e

Browse files
committed
Fix #287: Use DER by default
1 parent 2bc9454 commit 231c54e

File tree

10 files changed

+589
-59
lines changed

10 files changed

+589
-59
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 3.0.0
2+
current_version = 3.0.1
33
commit = True
44
tag = True
55

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
3.0.1 (2025-05-02)
5+
------------------
6+
7+
* Fix #288 - Use DER encoding by default as in previous versions
8+
49
3.0.0 (2025-03-03)
510
------------------
611

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
:target: https://github.com/andrivet/python-asn1
77
:alt: GitHub Actions
88

9-
.. |docs| image:: https://readthedocs.org/projects/python-asn1/badge/?style=flat
10-
:target: https://readthedocs.org/projects/python-asn1
9+
.. |docs| image:: https://app.readthedocs.org/projects/python-asn1/badge/?style=flat
10+
:target: https://python-asn1.readthedocs.io/en/latest/
1111
:alt: Documentation Status
1212

1313
.. |version| image:: https://img.shields.io/pypi/v/asn1.svg?style=flat

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
year = '2007-2022'
2828
author = 'Sebastien Andrivet'
2929
copyright = '{0}, {1}'.format(year, author)
30-
version = release = '3.0.0'
30+
version = release = '3.0.1'
3131

3232
pygments_style = 'trac'
3333
templates_path = ['.']

docs/usage.rst

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,52 @@ If you want to precisely specify the ASN.1 type, you have to use the `Encoder.en
141141
142142
This also allows to encode data progressively, without having to keep everything in memory.
143143

144+
DER and CER
145+
-----------
146+
147+
The encoder uses DER (Distinguished Encoding Rules) encoding by default. If you want to use CER (Canonical Encoding Rules) encoding,
148+
you can do so by calling the `Encoder.start()` method with a stream as argument:
149+
150+
.. code-block:: python
151+
152+
stream = open('output.cer', 'wb')
153+
encoder = asn1.Encoder()
154+
encoder.start(stream)
155+
156+
You can explicitly specify the CER encoding without using a stream:
157+
158+
.. code-block:: python
159+
160+
encoder = asn1.Encoder()
161+
encoder.start(Encoder.CER)
162+
163+
You can explicitly specify the DER encoding when using a stream:
164+
165+
.. code-block:: python
166+
167+
stream = open('output.cer', 'wb')
168+
encoder = asn1.Encoder()
169+
encoder.start(stream, Encoder.DER)
170+
171+
DER has the advantage to be predicatable: there is one and only one way to encode a message using DER. DER is
172+
commonly used in security-related applications such as X.509 digital certificates. DER uses definite lengths for all
173+
encoded messages. This means that the length of the encoded message is known in advance. This is useful for encoding
174+
messages that are fixed in size or that do not change in size over time. The disadvantage of DER is that the encoded
175+
binary data are kept in memory until the encoding is finished. This can be a problem for very large messages.
176+
177+
CER is similar to DER, but it uses indefinite lengths. This means that the length of the encoded message is not
178+
known in advance. This is useful for encoding messages that may be very large or that may change in size over time.
179+
The advantage of CER is that the encoded binary data are not kept in memory until the encoding is finished. This
180+
means that the encoder can start writing the encoded message to a file or a stream as soon as it is available.
181+
182+
IMPORTANT: There was a mistake in version 3.0.0 of Python-ASN1 where the encoder used CER encoding by default contrary to the
183+
previous versions that were using DER. This was fixed in version 3.0.1: CER is the default encoding when using a stream.
184+
Otherwise, DER is the default encoding.
185+
144186
Decoding
145187
--------
146188

147-
If you want to decode ASN.1 from DER or BER encoded bytes, use code such as:
189+
If you want to decode ASN.1 from BER (DER, CER, ...) encoded bytes, use code such as:
148190

149191
.. code-block:: python
150192
@@ -201,8 +243,7 @@ Constants
201243

202244
A few constants are defined in the `asn1` module. The
203245
constants immediately below correspond to ASN.1 tag numbers.
204-
They can be used as
205-
the ``nr`` parameter of the
246+
They can be used as the ``nr`` parameter of the
206247
`Encoder.write()` method, and are returned as the
207248
first part of a ``(nr, typ, cls)`` tuple as returned by
208249
`Decoder.peek()` and

examples/examples.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ def example2():
3030
print('Example 2')
3131
with open('example2.der', 'wb') as f:
3232
encoder = asn1.Encoder()
33-
encoder.start(f)
33+
encoder.start(f, asn1.Encoding.DER) # CER is the default when using a stream
3434
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
35+
encoder.output() # Do nt forget to call output() to flush the buffer
36+
3537

3638
def example3():
37-
"""Encoding of a complex data."""
39+
"""Encoding of complex data."""
3840
print('Example 3')
3941
with open('example3.der', 'wb') as f:
4042
encoder = asn1.Encoder()
41-
encoder.start(f)
43+
encoder.start(f, asn1.Encoding.DER) # CER is the default when using a stream
4244
encoder.write(['test1', 'test2', [
4345
1,
4446
0.125,
4547
b'\x01\x02\x03'
4648
]])
49+
encoder.output() # Do nt forget to call output() to flush the buffer
4750
print()
4851

4952

@@ -180,6 +183,24 @@ def example8():
180183
print()
181184

182185

186+
def example9():
187+
"""Using CER encoding with a stream (file)."""
188+
with open('exmple9.cer', 'wb') as f:
189+
encoder = asn1.Encoder()
190+
encoder.start(f)
191+
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
192+
# No need to call encoder.output() with CER encoding and a stream
193+
194+
195+
def example10():
196+
"""Using DER encoding with a stream (file)."""
197+
with open('exmple10.der', 'wb') as f:
198+
encoder = asn1.Encoder()
199+
encoder.start(f, asn1.Encoding.DER)
200+
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
201+
encoder.output() # Do nt forget to call output() to flush the buffer
202+
203+
183204
example1()
184205
example2()
185206
example3()
@@ -188,3 +209,5 @@ def example8():
188209
example6()
189210
example7()
190211
example8()
212+
example9()
213+
example10()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read(*names, **kwargs):
2929

3030
setup(
3131
name='asn1',
32-
version='3.0.0',
32+
version='3.0.1',
3333
license='BSD',
3434
description='Python-ASN1 is a simple ASN.1 encoder and decoder for Python 2.7+ and 3.5+.',
3535
long_description='%s\n%s' % (

0 commit comments

Comments
 (0)