Package Cryptodome :: Package Util :: Module asn1 :: Class DerSequence

Class DerSequence

object --+    
         |    
 DerObject --+
             |
            DerSequence

Class to model a DER SEQUENCE.

This object behaves like a dynamic Python sequence.

Sub-elements that are INTEGERs behave like Python integers.

Any other sub-element is a binary string encoded as a complete DER sub-element (TLV).

An example of encoding is:

>>> from Cryptodome.Util.asn1 import DerSequence, DerInteger
>>> from binascii import hexlify, unhexlify
>>> obj_der = unhexlify('070102')
>>> seq_der = DerSequence([4])
>>> seq_der.append(9)
>>> seq_der.append(obj_der.encode())
>>> print hexlify(seq_der.encode())

which will show 3009020104020109070102, the DER encoding of the sequence containing 4, 9, and the object with payload 02.

For decoding:

>>> s = unhexlify(b'3009020104020109070102')
>>> try:
>>>   seq_der = DerSequence()
>>>   seq_der.decode(s)
>>>   print len(seq_der)
>>>   print seq_der[0]
>>>   print seq_der[:]
>>> except ValueError:
>>>   print "Not a valid DER SEQUENCE"

the output will be:

3
4
[4, 9, b'']
Instance Methods
 
__init__(self, startSeq=None, implicit=None)
Initialize the DER object as a SEQUENCE.
 
__delitem__(self, n)
 
__getitem__(self, n)
 
__setitem__(self, key, value)
 
__setslice__(self, i, j, sequence)
 
__delslice__(self, i, j)
 
__getslice__(self, i, j)
 
__len__(self)
 
__iadd__(self, item)
 
append(self, item)
 
hasInts(self, only_non_negative=True)
Return the number of items in this sequence that are integers.
 
hasOnlyInts(self, only_non_negative=True)
Return True if all items in this sequence are integers or non-negative integers.
 
encode(self)
Return this DER SEQUENCE, fully encoded as a binary string.
 
decode(self, derEle, nr_elements=None, only_ints_expected=False)
Decode a complete DER SEQUENCE, and re-initializes this object with it.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, startSeq=None, implicit=None)
(Constructor)

 
Initialize the DER object as a SEQUENCE.
Parameters:
  • startSeq (Python sequence) - A sequence whose element are either integers or other DER objects.
  • implicit (integer) - The IMPLICIT tag to use for the encoded object. It overrides the universal tag for SEQUENCE (16).
Overrides: object.__init__

hasInts(self, only_non_negative=True)

 
Return the number of items in this sequence that are integers.
Parameters:
  • only_non_negative (boolean) - If True, negative integers are not counted in.

hasOnlyInts(self, only_non_negative=True)

 

Return True if all items in this sequence are integers or non-negative integers.

This function returns False is the sequence is empty, or at least one member is not an integer.

Parameters:
  • only_non_negative (boolean) - If True, the presence of negative integers causes the method to return False.

encode(self)

 
Return this DER SEQUENCE, fully encoded as a binary string.
Raises:
  • ValueError - If some elements in the sequence are neither integers nor byte strings.
Overrides: DerObject.encode

decode(self, derEle, nr_elements=None, only_ints_expected=False)

 

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

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

Parameters:
  • derEle (byte string) - A complete SEQUENCE DER element.
  • nr_elements (None, integer or list of integers) - The number of members the SEQUENCE can have
  • only_ints_expected (boolean) - Whether the SEQUENCE is expected to contain only integers.
Raises:
  • ValueError - In case of parsing errors.
Overrides: DerObject.decode