public static function expectFromDER(string $data, int &$offset,
        int $expected = null): self
    {
        $idx = $offset;
        $length = self::fromDER($data, $idx);
        // if certain length was expected
        if (isset($expected)) {
            if ($length->isIndefinite()) {
                throw new DecodeException('Expected length %d, got indefinite.',
                    $expected);
            }
            if ($expected != $length->intLength()) {
                throw new DecodeException(
                    sprintf("Expected length %d, got %d.", $expected,
                        $length->intLength()));
            }
        }
        // check that enough data is available
        if (!$length->isIndefinite() &&
            strlen($data) < $idx + $length->intLength()) {
            throw new DecodeException(
                sprintf("Length %d overflows data, %d bytes left.",
                    $length->intLength(), strlen($data) - $idx));
        }
        $offset = $idx;
        return $length;
    }