function _M.parse_simple_sequence()

in kong/kong/plugins/jwt/asn_sequence.lua [41:85]


function _M.parse_simple_sequence(input)
  if type(input) ~= "string" then
    error("Argument #1 must be string", 2)
  elseif #input == 0 then
    error("Argument #1 must not be empty", 2)
  end
  if string_byte(input, 1) ~= 0x30 then
    error("Argument #1 is not a sequence")
  end
  local length = string_byte(input, 2)
  if length == nil then
    error("Sequence is incomplete")
  elseif length > 0x7F then
    error("Multi-byte lengths are not supported")
  elseif length ~= #input-2 then
    error("Sequence's asn length does not match expected length")
  end
  local seq = {}
  local counter = 1
  local position = 3
  while true do
    if position == #input+1 then
      break
    elseif position > #input+1 then
      error("Sequence moved out of bounds.")
    elseif counter > 0xFF then
      error("Sequence is too long")
    end
    local chunk = string_sub(input, position)
    if string_byte(chunk, 1) ~= 0x2 then
      error("Sequence did not contain integers")
    end
    local integerLength = string_byte(chunk, 2)
    if integerLength > 0x7F then
      error("Multi-byte lengths are not supported.")
    elseif integerLength > #chunk-2 then
      error("Integer is longer than remaining length")
    end
    local integer = string_sub(chunk, 3, integerLength+2)
    seq[counter] = integer
    position = position + integerLength + 2
    counter = counter + 1
  end
  return seq
end