{"id":3885,"library":"asn1","title":"Python-ASN1","description":"Python-ASN1 is a straightforward library for encoding and decoding ASN.1 data, compatible with Python 2.7+ and 3.5+. It supports both BER (Basic Encoding Rules) for parsing and DER (Distinguished Encoding Rules) for parsing and generation, including indefinite lengths. The library is written entirely in Python and supports most common ASN.1 types, including the REAL type. The latest version is 3.2.0, with a regular release cadence addressing new Python versions and API enhancements.","status":"active","version":"3.2.0","language":"en","source_language":"en","source_url":"https://github.com/andrivet/python-asn1","tags":["asn.1","encoding","decoding","serialization","cryptography","der","ber","x.509"],"install":[{"cmd":"pip install asn1","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for Python 2.7 and 3.x compatibility in older environments. Not strictly needed for modern Python 3.5+ installations.","package":"future","optional":true},{"reason":"Required for type hints in Python 2.7. Not needed for Python 3.5+ as 'typing' is part of the standard library.","package":"typing","optional":true}],"imports":[{"symbol":"Encoder","correct":"from asn1 import Encoder"},{"symbol":"Decoder","correct":"from asn1 import Decoder"},{"symbol":"Numbers","correct":"from asn1 import Numbers"}],"quickstart":{"code":"import asn1\n\n# 1. Encoding an ObjectIdentifier and a Sequence\nwith asn1.Encoder() as encoder:\n    # Encode an ObjectIdentifier (e.g., \"1.2.3\")\n    encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)\n\n    # Encode a Sequence containing an Integer and a UTF8String\n    with encoder.enter(asn1.Numbers.Sequence) as sequence_encoder:\n        sequence_encoder.write(123, asn1.Numbers.Integer)\n        sequence_encoder.write('hello', asn1.Numbers.UTF8String)\n\n    encoded_bytes = encoder.output()\nprint(f\"Encoded bytes: {encoded_bytes.hex()}\")\n\n# 2. Decoding the ASN.1 data\ndecoded_elements = []\nwith asn1.Decoder(stream=encoded_bytes) as decoder:\n    # Decode the first element (ObjectIdentifier)\n    tag, value = decoder.read()\n    decoded_elements.append(f\"Tag: {tag.nr} (ObjectIdentifier), Class: {tag.cls}, Value: {value}\")\n\n    # Decode the next element, which is the Sequence\n    tag, value = decoder.read()\n    if tag.nr == asn1.Numbers.Sequence:\n        decoded_elements.append(f\"Tag: {tag.nr} (Sequence), Class: {tag.cls}\")\n        # Enter the sequence to decode its elements\n        with decoder.enter() as sequence_decoder:\n            while not sequence_decoder.eof():\n                tag, value = sequence_decoder.read()\n                decoded_elements.append(f\"  -> Tag: {tag.nr}, Class: {tag.cls}, Value: {value}\")\n\nprint(\"\\nDecoded data:\")\nfor item in decoded_elements:\n    print(item)","lang":"python","description":"This quickstart demonstrates encoding and decoding a simple ObjectIdentifier and a Sequence containing an Integer and a UTF8String. It utilizes the modern context manager API introduced in version 3.2.0 for both encoding and decoding, which simplifies resource management and constructed type handling."},"warnings":[{"fix":"Upgrade to version 3.0.1 or later. If staying on 3.0.0, explicitly specify `asn1.Encoder.DER` when initializing the encoder or `start()` method if DER is desired, or be aware that CER will be used by default without a stream, and DER with a stream.","message":"In version 3.0.0, the `Encoder` incorrectly defaulted to CER encoding, diverging from previous versions which used DER. This was a significant behavioral change for users who did not explicitly specify the encoding method.","severity":"breaking","affected_versions":"3.0.0"},{"fix":"Always explicitly specify the ASN.1 `Numbers` type (e.g., `asn1.Numbers.IA5String`) when encoding a Python value if the default mapping is not the desired ASN.1 type.","message":"Due to ASN.1 having more distinct data types than Python, a single Python type might map to multiple ASN.1 types. The library uses the most frequently expected ASN.1 type by default. If a different ASN.1 type (e.g., 'IA5String' vs. 'UTF8String' for a Python string) is intended, it must be explicitly specified during encoding using `encoder.write(value, asn1.Numbers.DesiredType)`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate encoding and decoding logic to use the `with asn1.Encoder(...) as encoder:` and `with asn1.Decoder(...) as decoder:` syntax for improved readability and maintainability. Review the documentation for examples of the new API.","message":"Version 3.2.0 introduced a new, more Pythonic context manager API for `Encoder` and `Decoder` (e.g., `with asn1.Encoder() as encoder:`). While the older `encoder.start()` / `encoder.output()` methods remain backward-compatible, the context manager approach is recommended for cleaner code and proper resource handling, especially for constructed types and streams.","severity":"gotcha","affected_versions":"Prior to 3.2.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}