Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String Types

All string types expose .as_str() -> str and support __str__, __eq__, __len__. Types whose character sets are subsets of ASCII raise ValueError on construction if invalid characters are supplied.

Overview table

ClassConstructorValid charsetNotes
Utf8String(value: str)Unicode
PrintableString(value: str)A–Z a–z 0–9 ' ( ) + , - . / : = ?
IA5String(value: str)ASCII (0x00–0x7f)
NumericString(value: str)Digits and space
TeletexString(data: bytes)Arbitrary bytesAlso: TeletexString.from_latin1(str), from_str(str). .to_bytes() returns raw bytes.
VisibleString(value: str)Printable ASCII (0x21–0x7e)
GeneralString(data: bytes)Arbitrary bytesAlso: GeneralString.from_ascii(str) (ASCII only, raises ValueError for > U+007F), from_str(str). .to_bytes() returns raw bytes.
UniversalString(value: str)Unicode (UCS-4 BE)Also: UniversalString.from_bytes(data: bytes).
BmpString(value: str)BMP only (U+0000–U+FFFF)Also: BmpString.from_bytes(data: bytes). Raises ValueError for non-BMP code points.

Utf8String

ASN.1 tag 0x0c.

class Utf8String:
    def __init__(self, value: str) -> None: ...
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: Utf8String) -> bool: ...
    def __len__(self) -> int: ...

PrintableString

ASN.1 tag 0x13. Valid characters: A-Z a-z 0-9 ' ( ) + , - . / : = ?

class PrintableString:
    def __init__(self, value: str) -> None: ...  # Raises ValueError for invalid chars
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: PrintableString) -> bool: ...
    def __len__(self) -> int: ...

IA5String

ASN.1 tag 0x16. ASCII subset (0x00–0x7f).

class IA5String:
    def __init__(self, value: str) -> None: ...  # Raises ValueError for non-ASCII
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: IA5String) -> bool: ...
    def __len__(self) -> int: ...

NumericString

ASN.1 tag 0x12. Restricted to the digits 09 and the space character.

Valid characters: 0 1 2 3 4 5 6 7 8 9 (digits and space)

class NumericString:
    def __init__(self, value: str) -> None: ...  # Raises ValueError for invalid chars
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: NumericString) -> bool: ...
    def __len__(self) -> int: ...

TeletexString

ASN.1 tag 0x14, also known as T61String. Stores arbitrary 8-bit bytes without enforcing a character encoding. as_str() interprets the bytes as Latin-1 (ISO 8859-1).

class TeletexString:
    def __init__(self, data: bytes) -> None: ...
    @staticmethod
    def from_str(value: str) -> TeletexString: ...  # Encodes as Latin-1 (lossy for non-Latin-1 code points)
    def to_bytes(self) -> bytes: ...
    def as_str(self) -> str: ...    # Latin-1 decode
    def __len__(self) -> int: ...   # byte length
    def __eq__(self, other: TeletexString) -> bool: ...

VisibleString

ASN.1 tag 0x1a. Printable ASCII subset: bytes 0x200x7e (space through tilde). Control characters (below 0x20) and DEL (0x7f) are rejected.

class VisibleString:
    def __init__(self, value: str) -> None: ...  # Raises ValueError for control chars or non-ASCII
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: VisibleString) -> bool: ...
    def __len__(self) -> int: ...

GeneralString

ASN.1 tag 0x1b. Stores arbitrary 8-bit bytes without enforcing a character encoding. as_str() interprets the bytes as Latin-1 (ISO 8859-1). Commonly used for Kerberos realm names and principal name components (RFC 4120).

class GeneralString:
    def __init__(self, data: bytes) -> None: ...
    @staticmethod
    def from_ascii(value: str) -> GeneralString: ...  # Raises ValueError for non-ASCII (> U+007F)
    @staticmethod
    def from_str(value: str) -> GeneralString: ...    # Alias for from_ascii
    def to_bytes(self) -> bytes: ...
    def as_str(self) -> str: ...    # Latin-1 decode
    def __len__(self) -> int: ...   # byte length
    def __eq__(self, other: GeneralString) -> bool: ...

UniversalString

ASN.1 tag 0x1c. Encodes Unicode text as UCS-4 big-endian (4 bytes per code point, superset of BMP). Accepts any valid Unicode string.

class UniversalString:
    def __init__(self, value: str) -> None: ...
    @staticmethod
    def from_bytes(data: bytes) -> UniversalString: ...
    # Decodes UCS-4 BE bytes.  Raises ValueError if length is not a multiple of 4
    # or if any four-byte sequence is not a valid Unicode scalar value.
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: UniversalString) -> bool: ...
    def __len__(self) -> int: ...

BmpString

ASN.1 tag 0x1e. Encodes Unicode text as UCS-2 big-endian (2 bytes per code point, Basic Multilingual Plane only, U+0000–U+FFFF). Code points above U+FFFF are rejected.

class BmpString:
    def __init__(self, value: str) -> None: ...
    # Raises ValueError if the string contains code points outside the BMP (> U+FFFF).
    @staticmethod
    def from_bytes(data: bytes) -> BmpString: ...
    # Decodes UCS-2 BE bytes.  Raises ValueError if length is odd or if any
    # two-byte sequence is a surrogate half.
    def as_str(self) -> str: ...
    def __str__(self) -> str: ...
    def __eq__(self, other: BmpString) -> bool: ...
    def __len__(self) -> int: ...