Example #1
0
    def _decodeLen(self, idx, der):
        """Given a (part of a) DER element, and an index to the first byte of
                a DER length tag (L), return a tuple with the payload size,
                and the index of the first byte of the such payload (V).

                Raises a ValueError exception if the DER length is invalid.
                Raises an IndexError exception if the DER element is too short.
                """
        length = bord(der[idx])
        if length <= 127:
            return (length, idx + 1)
        payloadLength = bytes_to_long(der[idx + 1 : idx + 1 + (length & 0x7F)])
        if payloadLength <= 127:
            raise ValueError("Not a DER length tag.")
        return (payloadLength, idx + 1 + (length & 0x7F))
Example #2
0
    def _decodeLen(self, idx, der):
        """Given a (part of a) DER element, and an index to the first byte of
                a DER length tag (L), return a tuple with the payload size,
                and the index of the first byte of the such payload (V).

                Raises a ValueError exception if the DER length is invalid.
                Raises an IndexError exception if the DER element is too short.
                """
        length = bord(der[idx])
        if length <= 127:
            return (length, idx + 1)
        payloadLength = bytes_to_long(der[idx + 1:idx + 1 + (length & 0x7F)])
        if payloadLength <= 127:
            raise ValueError("Not a DER length tag.")
        return (payloadLength, idx + 1 + (length & 0x7F))
Example #3
0
    def decode(self, 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.
                """
        tlvLength = DerObject.decode(self, derEle, noLeftOvers)
        if self.typeTag != self.typeTags["INTEGER"]:
            raise ValueError("Not a DER INTEGER.")
        if bord(self.payload[0]) > 127:
            raise ValueError("Negative INTEGER.")
        self.value = bytes_to_long(self.payload)
        return tlvLength
Example #4
0
    def decode(self, 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.
                """
        tlvLength = DerObject.decode(self, derEle, noLeftOvers)
        if self.typeTag != self.typeTags['INTEGER']:
            raise ValueError("Not a DER INTEGER.")
        if bord(self.payload[0]) > 127:
            raise ValueError("Negative INTEGER.")
        self.value = bytes_to_long(self.payload)
        return tlvLength