Module osyspyrt.asn1univtype

Provide classes relateed to some ASN.1 types.

Global variables

var null_value

ASN.1 NULL value

Functions

def asn1str(value, indent_level=0)

This function creates a string representation of an ASN.1 value for printing. It is similar to the Python built-in str function except for printing byte strings which are formatted in a hex-dump like format.

Classes

class Asn1BitString (value=None, bit_count=None, nocopy=False)

This represents an ASN.1 BIT STRING value.

The BIT STRING value is represented as a bytes object together with a number of bits, which may be at most 7 bits fewer than the number of bits held in the bytes object. Unused bits will be set to zero.

This class presents itself as a sequence of bytes, by implementing many of the functions applicable to sequences. Supported functionality includes:

  • len
  • use of []
  • iteration
  • append(x)
  • clear()

The bits attribute presents a view of the bit string as a sequence of bits.

Attributes

bit_count : int
The number of bits in the BIT STRING. Read-only. bit_count >= 0 and (len(data) * 8) - 7 < bit_count <= len(data) * 8.

Parameters

value : bytes or bytearray or None
The value of the BIT STRING. If a bytearray is provided, a copy will be made. None may be passed when the bit string is empty.
bit_count : int or None
The number of bits in the BIT STRING. bit_count >= 0 and (len(data) * 8) - 7 < bit_count <= len(data) * 8. If None, bit_count is set to 8 * len(value).
nocopy : bool
If True, don't make a copy of value if it is a bytearray; you're transferring ownership of the bytearray to this object. Default is False.

Create an Asn1BitString with the given value and number of bits.

Subclasses

Class variables

var BitView

Returns a readable-writable bitwise view of an Asn1BitString. Supports:

  • len (returning number of bits)
  • [] (using bit indices 0..(1-bit_count)
  • append
  • clear
  • iteration

Instance variables

var bit_count
var bits

Return a readable, writable bitwise view of this Asn1BitString, supporting viewing it as a sequence of bits, with support for:

  • len
  • []
  • iteration
  • append
  • clear

Returns

Asn1BitString.BitView
 
var value

Return a memoryview on the data. This is useful for passing to functions that require a bytes-like object.

The memoryview may be readonly (if you require a writeable view, pass writeable=True). If the memoryview is readonly, should this value be modified, the changes will not be reflected in the memoryview.

If the memoryview not readonly, changes to it will be reflected in this object. While it is active, operations on the bit string that would involve resizing will not be allowed. Use memoryview.release() or del the memoryview to allow resizing operations again.

Methods

def append(self, x)

Append the given byte to the value, increasing the bit count by 8 bits.

def append_bit(self, x)

Append the given bit to the value, increasing the bit count by 1 bit.

Parameters

x : bool or int, with any non-zero int value being a 1 bit.

def clear(self)

Clears the bit string and sets the bit count to 0.

def copy_value(self)
def decode(self, encoding='utf-8', errors='strict')

Equivalent to value().decode(), an invocation of bytearray.decode

def get_bit(self, idx)

Returns the bit at the given bit index (0 for the first bit). See also bits for a bitwise view of the bit string. Returns


bool
 

Raises

IndexError if the index is out of range.

def set(self, value=None, bit_count=None, nocopy=False)

Assign value to Asn1BitString. The parameters and their interpretation are the same as for init.

def set_bit(self, idx, value=True, append=False, expand=False)

Set the bit at the given bit index. See also bits for a bitwise view of the bit string. If append is True, it is legal to set the next bit at the end of the bit string. If expand is True, it is legal to set any bit beyond the end of the current bit string. The requisite number of zero bits will be added.

Parameters

bool or int, with any non-zero int value being a 1-bit.

Raises

IndexError if the index is out of range.

def trimmed_bit_count(self)

Return the length of this BIT STRING, in bits, if all trailing zero bits were trimmed off.

class Asn1CharString

This class contains validation methods for various string types.

Static methods

def validate_bmp_str(charstr, charset=None)

Validate BMPString character string.

Parameters

charstr : str
BMPString value to validate.
charset : str
Constrained character set to validate against. (None = Full character set; no constraint)

Returns

result : bool
True if string is valid, false otherwise.
def validate_gentime_str(charstr)

Validate GeneralizedTime character string.

Parameters

charstr : str
GeneralizedTime string value to validate.

Returns

result : bool
True if string is valid, false otherwise.
def validate_ia5_str(charstr, charset=None)

Validate IA5String character string.

Parameters

charstr : str
IA5String value to validate.
charset : str
Constrained character set to validate against. (None = Full character set; no constraint)

Returns

result : bool
True if string is valid, false otherwise.
def validate_numeric_str(charstr, charset=None)

Validate NumericString character string.

Parameters

charstr : str
NumericString value to validate.
charset : str
Constrained character set to validate against. (None = Full character set; no constraint)

Returns

result : bool
True if string is valid, false otherwise.
def validate_printable_str(charstr, charset=None)

Validate PrintableString character string.

Parameters

charstr : str
PrintableString value to validate.
charset : str
Constrained character set to validate against. (None = Full character set; no constraint)

Returns

result : bool
True if string is valid, false otherwise.
def validate_utctime_str(charstr)

Validate UTCTime character string.

Parameters

charstr : str
UTC String value to validate.

Returns

result : bool
True if string is valid, false otherwise.
def validate_visible_str(charstr, charset=None)

Validate VisibleString character string.

Parameters

charstr : str
VisibleString value to validate.
charset : str
Constrained character set to validate against. (None = Full character set; no constraint)

Returns

result : bool
True if string is valid, false otherwise.
class Asn1Choice

Methods

def which(self)
class Asn1Element (name, required)

This class provides information on an ASN.1 constructed type element.

The constructor sets the name and other properties of the element.

Parameters

name : str
ASN.1 name of the element.
required : bool
Indicates if element is required in a SEQUENCE or SET.
class Asn1Null
class Asn1ObjectIdentifier

Provides class methods useful for working with OBJECT IDENTIFIER

Static methods

def from_xml(value, relative=False)

Parse a string matching an ASN.1 XMLObjectIdentifierValue into a sequence of integers.

Parameters

relative : bool
If True, the input is a relative OID and components must not use number form.
def to_xml(value)

Convert a sequence of integers into an ASN.1 XMLObjectIdentifierValue/XMLRelativeOIDValue. This string will only use XMLNumberForm for the components, allowing this to be used for relative and non-relative OID alike.

Returns

str
 

The XMLObjectIdentifierValue representation.

class Asn1OctetString

This class has utility methods for working with OCTET STRING values.

Static methods

def tohexstring(value, indent_level=None)

This method will build a string containing two digits 0 through F for each octet in the passed value.

Parameters

value : bytearray or bytes
Array of bytes comprising the octet string.
indent_level : integer
Indentation level.

Returns

hexstring : string representation of the OCTET STRING.

class NamedBitsBase (value=None, bit_count=None, nocopy=False)

Base class for classes generated in support of BIT STRING with named bits.

This class cannot be used alone. Subclasses must set named_bits to a dict mapping bit values (0-based indices) to bit names.

Classes derived from this base gain the following abilities:

  • To create instances from a list of named bits.
  • To set the value from a list of named bits.
  • To set and get individual bits by name.

See Asn1BitString.init. The only difference here is that value can be a list or tuple of named bits (each being a string)

Ancestors

Static methods

def get_max_named()

Return the largest bit value (0..) that has a name.

Methods

def get_bit(self, idx)

Extends the behavior of Asn1BitString.get_bit by allowing a bit name to be used for idx. When a name is given, IndexError will only be raised if the name is not a valid name according to named_bits. If the name corresponds to a bit beyond the current length, it is considered not set.

Returns

bool
 

Raises

IndexError if the index is out of range.

def has_unnamed_bits(self)

Returns true if this bit string has some unnamed bits set.

def set(self, value=None, bit_count=None, nocopy=False)

See Asn1BitString.init. The only difference here is that value can be a list or tuple of named bits (each being a string)

def set_bit(self, idx, value=True, append=False, expand=False)

Extends the behavior of Asn1BitString.set_bit by allowing idx to be the name of a bit. In that case, expand is ignored and treated as if True had been passed.

Inherited members

class NamedInt (val, name)

Represents an INTEGER value with a name. Instances of NamedInt can generally interoperate with ints, but str() returns the name, and values can be compared for (in)equality to either an int or string.

Parameters

val : int
 
name : str