{"id":281,"library":"pyasn1","title":"pyasn1","description":"pyasn1 is a pure-Python implementation of ASN.1 types and BER/DER/CER codecs (X.208). It lets developers define ASN.1 schemas as Python classes, then encode/decode wire-format bytes for network protocols and file formats such as X.509 certificates, PKCS structures, SNMP, and LDAP. Current version is 0.6.3 (released 2026), which adds a nesting depth limit to prevent stack overflow (CVE-2026-30922) and fixes an OverflowError from oversized BER length fields. The project is maintained by Christian Heimes and Simon Pichugin under the github.com/pyasn1 organisation following ownership transfer at v0.5.0; releases are irregular but active, with multiple releases per year addressing security CVEs and Python version support.","status":"active","version":"0.6.3","language":"python","source_language":"en","source_url":"https://github.com/pyasn1/pyasn1","tags":["asn1","ber","der","cer","codec","x509","cryptography","network-protocols","serialization","pure-python"],"install":[{"cmd":"pip install pyasn1","lang":"bash","label":"Core library"},{"cmd":"pip install pyasn1 pyasn1-modules","lang":"bash","label":"With pre-compiled RFC modules (X.509, PKCS, etc.)"}],"dependencies":[{"reason":"Pre-compiled ASN.1 schemas for standard protocols (RFC 2459/5280 X.509, PKCS#1/8/12, SNMP MIBs, etc.). Not bundled since v0.3.x.","package":"pyasn1-modules","optional":true}],"imports":[{"note":"All primitive ASN.1 scalar types live in pyasn1.type.univ","symbol":"Integer, OctetString, ObjectIdentifier","correct":"from pyasn1.type.univ import Integer, OctetString, ObjectIdentifier"},{"note":"Structured/constructed types also in pyasn1.type.univ","symbol":"Sequence, SequenceOf, Set, SetOf, Choice","correct":"from pyasn1.type.univ import Sequence, SequenceOf, Set, SetOf, Choice"},{"note":"Required when declaring SEQUENCE field names; wrong module is a common ImportError","symbol":"NamedType, OptionalNamedType, DefaultedNamedType, NamedTypes","correct":"from pyasn1.type.namedtype import NamedType, OptionalNamedType, DefaultedNamedType, NamedTypes"},{"note":"Used with .subtype(implicitTag=...) or .subtype(explicitTag=...) for context tagging","symbol":"Tag, tagClassContext, tagFormatSimple","correct":"from pyasn1.type.tag import Tag, tagClassContext, tagFormatSimple"},{"note":"Use the DER encoder for certificates and strict protocols; BER allows non-canonical forms that some validators reject","wrong":"from pyasn1.codec.ber.encoder import encode","symbol":"encode (DER)","correct":"from pyasn1.codec.der.encoder import encode"},{"note":"Always unpack as (value, remainder) — decode() returns a 2-tuple, not just the value","symbol":"decode (DER)","correct":"from pyasn1.codec.der.decoder import decode"},{"note":"BER is the permissive superset; use for parsing untrusted or legacy inputs where indefinite length is possible","symbol":"encode/decode (BER)","correct":"from pyasn1.codec.ber.encoder import encode\nfrom pyasn1.codec.ber.decoder import decode"},{"note":"Converts pyasn1 objects to/from plain Python dicts/lists/ints; useful for JSON interop","symbol":"encode/decode (native Python dicts)","correct":"from pyasn1.codec.native.encoder import encode\nfrom pyasn1.codec.native.decoder import decode"},{"note":"Base exception for all pyasn1 errors; catch this for encoder/decoder failures","symbol":"PyAsn1Error","correct":"from pyasn1.error import PyAsn1Error"}],"quickstart":{"code":"from pyasn1.type.univ import Sequence, Integer\nfrom pyasn1.type.namedtype import NamedType, OptionalNamedType, DefaultedNamedType, NamedTypes\nfrom pyasn1.type.tag import Tag, tagClassContext, tagFormatSimple\nfrom pyasn1.codec.der.encoder import encode\nfrom pyasn1.codec.der.decoder import decode\nfrom pyasn1.error import PyAsn1Error\n\n# 1. Define ASN.1 schema as a Python class\nclass Record(Sequence):\n    componentType = NamedTypes(\n        NamedType('id', Integer()),\n        OptionalNamedType(\n            'room',\n            Integer().subtype(implicitTag=Tag(tagClassContext, tagFormatSimple, 0))\n        ),\n        DefaultedNamedType(\n            'house',\n            Integer(0).subtype(implicitTag=Tag(tagClassContext, tagFormatSimple, 1))\n        ),\n    )\n\n# 2. Populate the schema object\nrecord = Record()\nrecord['id'] = 123\nrecord['room'] = 321\n\n# 3. DER-encode to bytes\nsubstrate = encode(record)\nprint('DER bytes:', substrate.hex())  # e.g. 30070201 7b800201 41\n\n# 4. Decode back — ALWAYS unpack the 2-tuple (value, remainder)\ntry:\n    received, remainder = decode(substrate, asn1Spec=Record())\nexcept PyAsn1Error as exc:\n    raise SystemExit(f'Decode failed: {exc}')\n\nassert remainder == b'', 'Unexpected trailing bytes'\nassert received['id'] == record['id']\nprint('Round-trip OK:', received.prettyPrint())\n","lang":"python","description":"Define an ASN.1 SEQUENCE schema, populate it, DER-encode it, then decode it back and verify round-trip equality."},"warnings":[{"fix":"Always unpack: `value, remainder = decode(substrate, asn1Spec=MyType())`; check `remainder == b''` to detect trailing garbage.","message":"decode() always returns a 2-tuple (asn1Object, remainingSubstrate). Ignoring the second element or trying to use the result directly as the decoded value is the single most common runtime bug.","severity":"breaking","affected_versions":"all"},{"fix":"Upgrade to Python 3.8+ and pin pyasn1>=0.6.0. Remove any Python 2 compatibility shims (six, future) from your codebase.","message":"Python 2 and Python < 3.8 support dropped in v0.6.0. The PyPI package now requires Python >=3.8.","severity":"breaking","affected_versions":"<0.6.0"},{"fix":"Call `.clear()` on a SequenceOf/SetOf instance to explicitly make it a value object (empty list), or append a component first. Do not assume an empty SequenceOf is a value object.","message":"SequenceOf/SetOf instances are no longer auto-initialised as value objects on instantiation (changed in 0.4.x). Code that tested `if mySeqOf:` or iterated an empty SequenceOf immediately after construction will silently get wrong results or raise errors.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"If passing substrateFun to the top-level `decoder.decode()`, both old and new signatures work as of 0.5.1+. For streaming decoders directly, migrate to the v0.5 streaming callback signature.","message":"The substrateFun callback signature changed in v0.5.0 from non-streaming (v0.4 style) to streaming. The v0.5.1 patch restores transparent compatibility for decoder.decode(), but custom substrateFun callbacks passed to low-level streaming decoders must use the new streaming signature.","severity":"breaking","affected_versions":"0.5.0"},{"fix":"Always pass `asn1Spec=MySchemaType()` to decode(). This is mandatory for any schema that uses IMPLICIT or EXPLICIT tagging.","message":"Without asn1Spec=, the decoder falls back to generic BER decoding and returns untyped Any objects. IMPLICIT tags cannot be decoded without a spec, so fields will silently be missing or mis-typed.","severity":"gotcha","affected_versions":"all"},{"fix":"Install pyasn1-modules separately: `pip install pyasn1-modules`. Import e.g. `from pyasn1_modules import rfc5280`.","message":"Pre-built ASN.1 modules (X.509 RFC 5280, PKCS, SNMP MIBs, etc.) were moved to the separate pyasn1-modules package long ago and are no longer included in pyasn1 itself.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"Explicitly wrap values: `record['id'] = Integer(123)` rather than relying on implicit coercion. Use `.hasValue()` / `.isValue` to check whether a component has been populated before encoding.","message":"pyasn1 types mimic Python built-ins (Integer ≈ int, OctetString ≈ bytes) but comparisons, hashing, and arithmetic can differ subtly. Passing a raw Python int where an Integer() instance is expected often works, but not always — especially inside Sequence field assignment with constraints.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T12:48:38.514Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure `pyasn1` is installed for the correct Python interpreter: `pip install pyasn1` or `python -m pip install pyasn1`.","cause":"The `pyasn1` library is not installed in the Python environment being used, or there's a Python version conflict (e.g., installed for Python 2 but running Python 3).","error":"ModuleNotFoundError: No module named 'pyasn1'"},{"fix":"Reinstall or upgrade `pyasn1`: `pip install --upgrade pyasn1` or `python -m pip install --upgrade pyasn1`. If using an older Python version or specific distribution packages, ensure the correct `pyasn1` package (e.g., `python3-pyasn1` on Debian/Ubuntu) is installed.","cause":"This specific submodule of `pyasn1` (or another submodule like `pyasn1.type.univ`) cannot be found, often due to an incomplete or corrupted `pyasn1` installation, or an environment path issue.","error":"ModuleNotFoundError: No module named 'pyasn1.codec.der.decoder'"},{"fix":"Verify the input BER/DER/CER encoded data is well-formed and matches the ASN.1 specification (`asn1Spec`) used for decoding. Debug the decoding process to identify the exact malformed segment or schema mismatch.","cause":"This typically occurs during ASN.1 decoding when `pyasn1` fails to parse a part of the input data, resulting in `None` being returned for an expected ASN.1 object, and subsequent code attempts to access attributes (like `tagSet`) on this `None` object. This usually indicates malformed input data or a mismatch with the provided ASN.1 schema.","error":"AttributeError: 'NoneType' object has no attribute 'tagSet'"},{"fix":"Ensure the `asn1Spec` passed to the decoder accurately reflects the structure and tags of the ASN.1 data being decoded. The error message will often show the unexpected `TagSet`, which can help in correcting the schema.","cause":"This error arises when `pyasn1` attempts to decode an ASN.1 object, but the tag set identified in the input byte stream does not correspond to any type defined in the `asn1Spec` provided to the decoder.","error":"pyasn1.error.PyAsn1Error: Type TagSet(...) not found in asn1Spec"},{"fix":"Upgrade both `pyasn1` and `pyasn1-modules` to their latest compatible versions (`pip install --upgrade pyasn1 pyasn1-modules`). If the issue persists with the latest versions, check the `pyasn1` changelog for breaking API changes related to the specific class being instantiated and adjust your code accordingly.","cause":"This error often indicates an API incompatibility, particularly between different versions of `pyasn1` or when `pyasn1-modules` is used with an incompatible `pyasn1` version. Older `pyasn1` versions (e.g., pre-0.3.1) might have had constructors with different signatures than newer ones, especially for types like `univ.SequenceOf` or `SetOf` when defining components.","error":"TypeError: __init__() takes 1 positional argument but 2 were given"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18.6M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"20.5M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"19M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"21M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"20.6M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"23.1M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"21M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"24M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"12.5M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"14.9M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"13M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"15M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.9,"disk_size":"12.1M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.9,"disk_size":"14.5M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"13M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"15M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18.2M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"20.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"19M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"21M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}