Esempio n. 1
0
    def toVal(self):
        if len(self._payload) > 0 and self._payload[0] >= 0x80:
            raise DerDecodingException(
                "DerInteger: Negative integers are not currently supported")

        result = 0
        for i in range(len(self._payload)):
            result *= 256
            result += self._payload[i]
        return result
Esempio n. 2
0
 def getChildren(self):
     """
     If this object is a DerSequence, get the children of this node. Otherwise,
     raise an exception. (DerSequence overrides to implement this method.)
     :return: The children of this node
     :rtype: array of DerNode
     :raises: DerDecodingException if this object is not a DerSequence.
     """
     raise DerDecodingException(
         "getChildren: This DerNode is not DerSequence")
Esempio n. 3
0
    def parse(inputBuf, startIdx=0):
        """
        Parse the data from the input buffer recursively and return the root as a subclass of DerNode.
        :param inputBuf: The input buffer to read from.
        :type inputBuf: bytearray or Blob
        :param startIdx: (optional) An offset into the buffer.
        :type startIdx: int
        """
        if isinstance(inputBuf, Blob):
            inputBuf = inputBuf.buf()

        if len(inputBuf) <= startIdx:
            raise DerDecodingException(
                "DerNode.parse: The input length is too small")
        nodeType = inputBuf[startIdx]  # don't increment, we're just peeking

        outputType = None

        if nodeType == Der.Boolean:
            outputType = DerBoolean
        elif nodeType == Der.Integer:
            outputType = DerInteger
        elif nodeType == Der.BitString:
            outputType = DerBitString
        elif nodeType == Der.OctetString:
            outputType = DerOctetString
        elif nodeType == Der.Null:
            outputType = DerNull
        elif nodeType == Der.ObjectIdentifier:
            outputType = DerOid
        elif nodeType == Der.Sequence:
            outputType = DerSequence
        elif nodeType == Der.PrintableString:
            outputType = DerPrintableString
        elif nodeType == Der.GeneralizedTime:
            outputType = DerGeneralizedTime
        else:
            raise DerDecodingException(
                "Unimplemented DER type {}".format(nodeType))

        newNode = outputType()
        newNode.decode(inputBuf, startIdx)
        return newNode
Esempio n. 4
0
    def getSequence(children, index):
        """
        Check that index is in bounds for the children list, and return
        children[index].

        :param children: The list of DerNode, usually returned by another
          call to getChildren.
        :type children: array of DerNode
        :param int index: The index of the children.
        :return: children[index] which is a DerSequence
        :rtype: DerSequence
        :raises: DerDecodingException if index is out of bounds or if
          children[index] is not a DerSequence.
        """
        if index < 0 or index >= len(children):
            raise DerDecodingException(
                "getSequence: Child index is out of bounds")

        if not isinstance(children[index], DerSequence):
            raise DerDecodingException(
                "getSequence: Child DerNode is not a DerSequence")

        return children[index]
Esempio n. 5
0
    def _decodeHeader(self, inputBuf, startIdx=0):
        """
            Extracts the header from an input buffer.
            :param inputBuf: The input buffer to read from.
            :type inputBuf: bytearray or Blob
            :param startIdx: (optional) An offset into the buffer.
            :type startIdx: int
        """

        if isinstance(inputBuf, Blob):
            inputBuf = inputBuf.buf()

        idx = startIdx

        nodeType = inputBuf[idx]
        idx += 1

        self._nodeType = nodeType

        sizeLen = inputBuf[idx]
        idx += 1

        self._header = bytearray([nodeType, sizeLen])

        size = sizeLen
        isLongFormat = sizeLen & (1 << 7)
        if isLongFormat:
            lenCount = sizeLen & ((1 << 7) - 1)
            size = 0
            while lenCount > 0:
                if len(inputBuf) <= idx:
                    raise DerDecodingException(
                        "DerNode.decodeHeader: The input length is too small")
                b = inputBuf[idx]
                idx += 1
                self._header.append(b)
                size = 256 * size + int(b)
                lenCount -= 1

        return size