Crypto.Util.asn1 module

This module provides minimal support for encoding and decoding ASN.1 DER objects.

class Crypto.Util.asn1.DerInteger(value=0)
decode(derEle, noLeftOvers=0)

Decode a complete INTEGER DER element, and re-initializes this object with it.

@param derEle A complete INTEGER DER element. It must start with a DER

INTEGER tag.

@param noLeftOvers Indicate whether it is acceptable to complete the

parsing of the DER element and find that not all bytes in derEle have been used.

@return Index of the first unused byte in the given DER element.

Raises a ValueError exception if the DER element is not a valid non-negative INTEGER. Raises an IndexError exception if the DER element is too short.

encode()

Return a complete INTEGER DER element, fully encoded as a TLV.

class Crypto.Util.asn1.DerNull
class Crypto.Util.asn1.DerObject(ASN1Type=None, payload=b'')

Base class for defining a single DER object.

Instantiate this class ONLY when you have to decode a DER element.

decode(derEle, noLeftOvers=0)

Decode a complete DER element, and re-initializes this object with it.

@param derEle A complete DER element. It must start with a DER T

tag.

@param noLeftOvers Indicate whether it is acceptable to complete the

parsing of the DER element and find that not all bytes in derEle have been used.

@return Index of the first unused byte in the given DER element.

Raises a ValueError exception in case of parsing errors. Raises an IndexError exception if the DER element is too short.

encode()

Return a complete DER element, fully encoded as a TLV.

class Crypto.Util.asn1.DerObjectId
decode(derEle, noLeftOvers=0)

Decode a complete DER element, and re-initializes this object with it.

@param derEle A complete DER element. It must start with a DER T

tag.

@param noLeftOvers Indicate whether it is acceptable to complete the

parsing of the DER element and find that not all bytes in derEle have been used.

@return Index of the first unused byte in the given DER element.

Raises a ValueError exception in case of parsing errors. Raises an IndexError exception if the DER element is too short.

class Crypto.Util.asn1.DerOctetString(value=b'')
decode(derEle, noLeftOvers=0)

Decode a complete DER element, and re-initializes this object with it.

@param derEle A complete DER element. It must start with a DER T

tag.

@param noLeftOvers Indicate whether it is acceptable to complete the

parsing of the DER element and find that not all bytes in derEle have been used.

@return Index of the first unused byte in the given DER element.

Raises a ValueError exception in case of parsing errors. Raises an IndexError exception if the DER element is too short.

class Crypto.Util.asn1.DerSequence(startSeq=None)

Class to model a SEQUENCE DER element.

This object behave like a dynamic Python sequence. Sub-elements that are INTEGERs, look like Python integers. Any other sub-element is a binary string encoded as the complete DER sub-element (TLV).

decode(derEle, noLeftOvers=0)

Decode a complete SEQUENCE DER element, and re-initializes this object with it.

@param derEle A complete SEQUENCE DER element. It must start with a DER

SEQUENCE tag.

@param noLeftOvers Indicate whether it is acceptable to complete the

parsing of the DER element and find that not all bytes in derEle have been used.

@return Index of the first unused byte in the given DER element.

DER INTEGERs are decoded into Python integers. Any other DER element is not decoded. Its validity is not checked.

Raises a ValueError exception if the DER element is not a valid DER SEQUENCE. Raises an IndexError exception if the DER element is too short.

encode()

Return the DER encoding for the ASN.1 SEQUENCE, containing the non-negative integers and longs added to this object.

Limitation: Raises a ValueError exception if it some elements in the sequence are neither Python integers nor complete DER INTEGERs.

hasInts()

Return the number of items in this sequence that are numbers.

hasOnlyInts()

Return True if all items in this sequence are numbers.