{"id":5115,"library":"asn1tools","title":"asn1tools","description":"asn1tools is a Python package for ASN.1 parsing, encoding, and decoding. It provides functionality to compile ASN.1 specifications and then encode and decode data using various encoding rules such as Basic Encoding Rules (BER), Distinguished Encoding Rules (DER), Packed Encoding Rules (PER), Unaligned Packed Encoding Rules (UPER), and XML Encoding Rules (XER), among others. The library is currently at version 0.167.0 and is actively maintained with frequent releases addressing bug fixes and performance improvements.","status":"active","version":"0.167.0","language":"en","source_language":"en","source_url":"https://github.com/eerimoq/asn1tools","tags":["ASN.1","encoding","decoding","parsing","telecommunications","networking","security"],"install":[{"cmd":"pip install asn1tools","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for ASN.1 specification parsing.","package":"pyparsing","optional":false},{"reason":"Used for efficient bit-level operations during encoding/decoding.","package":"bitstruct","optional":false},{"reason":"Used by the command-line interface (CLI) for interactive features.","package":"prompt_toolkit","optional":true},{"reason":"Used for caching compiled ASN.1 specifications.","package":"diskcache","optional":true}],"imports":[{"symbol":"asn1tools","correct":"import asn1tools"}],"quickstart":{"code":"import asn1tools\nimport os\nimport tempfile\n\n# Define an example ASN.1 specification\nasn1_spec = \"\"\"\nFoo DEFINITIONS ::= BEGIN\n  Question ::= SEQUENCE {\n    id        INTEGER,\n    question  IA5String\n  }\n  Answer ::= SEQUENCE {\n    id      INTEGER,\n    answer  BOOLEAN\n  }\nEND\n\"\"\"\n\n# Create a temporary .asn file for the specification\nwith tempfile.NamedTemporaryFile(mode='w', suffix='.asn', delete=False) as f:\n    f.write(asn1_spec)\n    asn_filepath = f.name\n\ntry:\n    # Compile the ASN.1 specification using the default BER codec\n    # You can specify other codecs like 'per', 'uper', 'xer', etc.\n    foo = asn1tools.compile_files(asn_filepath, 'ber')\n\n    # Encode a Question message\n    question_data = {'id': 1, 'question': 'Is 1+1=3?'}\n    encoded_question = foo.encode('Question', question_data)\n\n    print(f\"Encoded (BER): {encoded_question.hex()}\")\n\n    # Decode the Question message\n    decoded_question = foo.decode('Question', encoded_question)\n\n    print(f\"Decoded: {decoded_question}\")\n\n    # Example with another message type (Answer)\n    answer_data = {'id': 1, 'answer': False}\n    encoded_answer = foo.encode('Answer', answer_data)\n    print(f\"Encoded Answer (BER): {encoded_answer.hex()}\")\n    decoded_answer = foo.decode('Answer', encoded_answer)\n    print(f\"Decoded Answer: {decoded_answer}\")\n\nfinally:\n    # Clean up the temporary file\n    os.remove(asn_filepath)\n","lang":"python","description":"This quickstart demonstrates how to compile an ASN.1 specification from a file, then encode Python dictionaries into ASN.1 binary format using the BER codec, and decode the binary data back into Python dictionaries."},"warnings":[{"fix":"Review the 'Known limitations' in the official documentation and adjust ASN.1 specifications to avoid unsupported features. Parameterized types (e.g., `SetupRelease { ElementTypeParam }`) must be manually expanded in the ASN.1 specification before compilation.","message":"asn1tools only supports a subset of the full ASN.1 specification syntax. Known unsupported features include the CLASS keyword (X.681), Parametrization (X.683), EMBEDDED PDV, ANY/ANY DEFINED BY types, WITH COMPONENT/WITH COMPONENTS constraints (except for OER REAL), and the DURATION type. Recursive types are also not supported.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `check_constraints=True` (the default for `decode`) during development and in production where data integrity is critical. Only disable it if performance is absolutely paramount and the data source is fully trusted to adhere to all constraints.","message":"When encoding or decoding, setting `check_constraints=False` will skip validation against ASN.1 type constraints. While this can minimize runtime overhead, it allows the processing of values that do not fulfill the constraints, potentially leading to `DecodeError` exceptions or data corruption later on if the data is malformed.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If upgrading from a version older than 0.156.0, review code that processes BER-encoded SET or SEQUENCE OF types to ensure it handles potential reordering of members, if that was previously an implicit expectation. The change generally improves robustness but could alter behavior for non-compliant streams.","message":"Version 0.156.0 introduced support for decoding BER SET and SEQUENCE OF members in any order. Prior to this version, implementations might have relied on a specific order, and decoding data with out-of-order members could have failed or yielded incorrect results.","severity":"breaking","affected_versions":"<0.156.0"},{"fix":"Ensure that ASN.1 specifications intended for C code generation adhere strictly to these limitations. For UPER, use `compact_extensions_uper` for extendable CHOICE and SEQUENCE without `...`. Avoid named numbers in ENUMERATED for C code generation.","message":"The C code generator for OER and UPER has specific limitations, including support only for BOOLEAN, INTEGER, NULL, OCTET STRING, BIT STRING, ENUMERATED, SEQUENCE, SEQUENCE OF, and CHOICE types. All types must have a known maximum size (e.g., INTEGER (0..7)), INTEGERs must be 64 bits or less, and REAL types must be IEEE 754 binary32 or binary64. Extension additions (...) are only supported in the OER generator.","severity":"gotcha","affected_versions":"All versions with C code generation"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}