Abstract Syntax Notation One

Python implementation

Abstract Syntax Notation One

Value mapping:

ASN.1 type

Python type

Boolean

bool

Integer

int

BitString

List[bool]

OctetString

bytes

Null

None

ObjectIdentifier

List[int]

String

str

External

External

Real

float

Enumerated

int

EmbeddedPDV

EmbeddedPDV

Choice

Tuple[str,Value]

Set

Dict[str,Value]

SetOf

Iterable[Value]

Sequence

Dict[str,Value]

SequenceOf

List[Value]

ABSTRACT-SYNTAX.&Type

Entity

For Choice, Set and Sequence, str represents field name.

hat.asn1.Data

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Union[bytes, bytearray, memoryview]

hat.asn1.Value

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]

hat.asn1.Boolean

alias of builtins.bool

hat.asn1.Integer

alias of builtins.int

hat.asn1.BitString

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of List[bool]

hat.asn1.OctetString

alias of builtins.bytes

hat.asn1.ObjectIdentifier

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of List[Union[int, Tuple[str, int]]]

hat.asn1.String

alias of builtins.str

class hat.asn1.External(data, direct_ref, indirect_ref)

Bases: tuple

Create new instance of External(data, direct_ref, indirect_ref)

data

Union[Entity,Data,List[bool]]

direct_ref

Optional[ObjectIdentifier]

indirect_ref

Optional[int]

hat.asn1.Real

alias of builtins.float

hat.asn1.Enumerated

alias of builtins.int

class hat.asn1.EmbeddedPDV(abstract, transfer, data)

Bases: tuple

Create new instance of EmbeddedPDV(abstract, transfer, data)

abstract

Optional[Union[int,ObjectIdentifier]]

data

Data

transfer

Optional[ObjectIdentifier]

hat.asn1.Choice

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Tuple[str, Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]]

hat.asn1.Set

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Dict[str, Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]]

hat.asn1.SetOf

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Iterable[Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]]

hat.asn1.Sequence

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Dict[str, Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]]

hat.asn1.SequenceOf

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of List[Union[Boolean, Integer, BitString, OctetString, Null, ObjectIdentifier, String, External, Real, Enumerated, EmbeddedPDV, Choice, Set, SetOf, Sequence, SequenceOf, Entity]]

class hat.asn1.Entity

Bases: abc.ABC

Encoding independent ASN.1 Entity

hat.asn1.is_oid_eq(x, y)

Check if two ASN.1 object identifiers are equal

Parameters
  • x (ObjectIdentifier) – object identifier

  • y (ObjectIdentifier) – object identifier

Returns

bool

class hat.asn1.Encoding(value)

Bases: enum.Enum

An enumeration.

  • BER

BER
class hat.asn1.Encoder(encoding, repository)

Bases: object

ASN1 Encoder

Parameters
  • encoding (Encoding) – encoding

  • repository (Repository) – repository

property syntax_name

encoder syntax name

Type

ObjectIdentifier

encode(module, name, value)

Encode value to data

Parameters
  • module (str) – module name

  • name (str) – type name

  • value (Value) – value

Returns

Data

decode(module, name, data)

Decode value from data

Parameters
  • module (str) – module name

  • name (str) – type name

  • data (Data) – data

Returns

value and remaining data

Return type

Tuple[Value,Data]

encode_value(module, name, value)

Encode value to entity

Parameters
  • module (str) – module name

  • name (str) – type name

  • value (Value) – value

Returns

Entity

decode_value(module, name, entity)

Decode value from entity

Parameters
  • module (str) – module name

  • name (str) – type name

  • entity (Entity) – entity

Returns

Value

encode_entity(entity)

Encode entity to data

Parameters
  • module (str) – module name

  • name (str) – type name

  • entity (Entity) – entity

Returns

Data

decode_entity(data)

Decode entity from data

Parameters
  • module (str) – module name

  • name (str) – type name

  • data (Data) – data

Returns

entity and remaining data

Return type

Tuple[Entity,Data]

class hat.asn1.Repository(*args)

Bases: object

ASN.1 type definition repository.

Repository can be initialized with multiple arguments, which can be instances of pathlib.PurePath, str or Repository.

If an argument is of type pathlib.PurePath, and path points to file with a suffix ‘.asn’, ASN.1 type definitions are decoded from the file. Otherwise, it is assumed that path points to a directory, which is recursively searched for ASN.1 definitions. All decoded types are added to the repository. Previously added type definitions with the same references are replaced.

If an argument is of type str, it represents ASN.1 type definitions. All decoded types are added to the repository. Previously added type definitions with the same references are replaced.

If an argument is of type Repository, its data definitions are added to the new repository. Previously added type definitions with the same references are replaced.

Parameters

args (Union[pathlib.PurePath,str,Repository]) – init arguments

static from_json(data)

Create repository from JSON data representation

Parameters

data (Union[pathlib.PurePath,json.Data]) – repository data

Returns

Repository

to_json()

Represent repository as JSON data

Returns

json.Data

generate_html_doc()

Generate HTML documentation

Returns

str