Supported types

x690.types has a class for every universal type of X.690. Each class decodes its tag, and each instance encodes back to bytes. They differ in the Python value they hold.

Types with a Python value

These types convert between bytes and a Python value in both directions:

Class

Tag

Python value

Boolean

0x01

bool

Integer

0x02

int

BitString

0x03

Bits

OctetString

0x04

bytes

Null

0x05

None

ObjectIdentifier

0x06

str, such as "1.3.6.1"

ObjectDescriptor

0x07

str (ASCII)

Real

0x09

float

Enumerated

0x0A

int

Utf8String

0x0C

str

RelativeOid

0x0D

str, such as "8571.3.2"

Sequence

0x10

list of x690 values

Set

0x11

list of x690 values, in their encoded order

NumericString

0x12

str (ASCII)

PrintableString

0x13

str (ASCII)

T61String

0x14

str

IA5String

0x16

str (ASCII)

UtcTime

0x17

timezone-aware datetime

GeneralizedTime

0x18

datetime

GraphicString

0x19

str

VisibleString

0x1A

str (ASCII)

UniversalString

0x1C

str

BmpString

0x1E

str

Object identifiers

ObjectIdentifier also works as a value in code: it compares, sorts, hashes, concatenates and slices like the dotted string it represents.

>>> from x690.types import ObjectIdentifier
>>> oid = ObjectIdentifier("1.3.6.1.2.1.1.1.0")
>>> oid.nodes
(1, 3, 6, 1, 2, 1, 1, 1, 0)
>>> oid.childof(ObjectIdentifier("1.3.6.1.2.1"))
True
>>> oid[:6] + ObjectIdentifier("2")
ObjectIdentifier('1.3.6.1.2.1.2')

Bit strings

BitString holds a Bits value. It keeps the octets and the number of unused bits, so a decoded value encodes back unchanged. Bit 0 is the most significant bit of the first octet, as in ASN.1 named bit lists.

Read flags such as the X.509 KeyUsage by position. DER drops trailing zero bits, so use is_set(), which reads bits past the end as 0:

>>> from x690 import decode
>>> usage = decode(b"\x03\x02\x05\xa0")[0].value
>>> str(usage)
'101'
>>> usage.is_set(0), usage.is_set(1)
(True, False)
>>> usage.is_set(8)
False

Pass bytes for keys and signatures, and write flags with from_bin():

>>> from x690.types import BitString, Bits
>>> bytes(BitString(b"\x01\x02"))
b'\x03\x03\x00\x01\x02'
>>> bytes(Bits.from_bin("1011"))
b'\xb0'

T61String

T61String decodes and encodes strictly according to ITU-T T.61 (1988) through t61codec. Invalid T.61 raises UnicodeDecodeError, and characters T.61 cannot represent raise UnicodeEncodeError.

Note

Data written by OpenSSL is often Latin-1 rather than T.61. Decode such values from their raw bytes with bytes.decode("latin-1").

Time values

UtcTime always holds a timezone-aware datetime. It decodes a Z or an offset such as +0130. The two-digit year follows the X.509 rule: 50 to 99 mean 1950 to 1999, and 00 to 49 mean 2000 to 2049.

>>> from x690 import decode
>>> value, _ = decode(b"\x17\x0d251026120000Z")
>>> value.value
datetime.datetime(2025, 10, 26, 12, 0, tzinfo=datetime.timezone.utc)
>>> decode(b"\x17\x0b4910261200Z")[0].value.year
2049
>>> decode(b"\x17\x0b5010261200Z")[0].value.year
1950

Encoding converts to UTC and writes Z. A naive datetime, or a year outside 1950 to 2049, raises ValueError.

GeneralizedTime has a four-digit year and optional minutes, seconds and fractions. Without a zone it holds a naive datetime, which stands for local time. With Z or an offset it holds an aware one. Encoding keeps that difference. Microseconds are written only when they are not zero.

>>> from datetime import datetime
>>> from x690.types import GeneralizedTime
>>> moment = datetime(2025, 10, 26, 12)
>>> bytes(GeneralizedTime(moment))
b'\x18\x0e20251026120000'
>>> data = b"\x18\x1120251026120000.5Z"
>>> decode(data)[0].value.microsecond
500000

Types with raw bytes

Six universal types hold the bytes of their value, without the type and length header. They decode and encode without loss, and the bytes are left for the application to interpret:

>>> text, _ = decode(b"\x15\x03abc")
>>> text
VideotexString(b'abc')
>>> text.value.decode("ascii")
'abc'

These types are:

  • EOC, the end-of-contents marker. It is part of the structure of an encoding, not a value.

  • External, EmbeddedPdv and CharacterString, constructed types that are rarely used.

  • VideotexString and GeneralString, which switch character sets inside the value, as T.100 and ISO 2022 define.

To give one of them a Python value, subclass it, set its TAG again, and override decode_raw() and encode_raw() as shown in Add custom types.