aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/openssl/err.lua
blob: a047a7c01cddfadbe52871561e76a5f4862cb695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local ffi = require "ffi"
local C = ffi.C
local ffi_str = ffi.string
local ffi_sizeof = ffi.sizeof

local ctypes = require "resty.openssl.auxiliary.ctypes"
require "resty.openssl.include.err"

local constchar_ptrptr = ffi.typeof("const char*[1]")

local buf = ffi.new('char[256]')

local function format_error(ctx, code, all_errors)
  local errors = {}
  if code then
    table.insert(errors, string.format("code: %d", code or 0))
  end
  -- get the OpenSSL errors
  while C.ERR_peek_error() ~= 0 do
    local line = ctypes.ptr_of_int()
    local path = constchar_ptrptr()
    local code
    if all_errors then
      code = C.ERR_get_error_line(path, line)
    else
      code = C.ERR_peek_last_error_line(path, line)
    end

    local abs_path = ffi_str(path[0])
    -- ../crypto/asn1/a_d2i_fp.c => crypto/asn1/a_d2i_fp.c
    local start = abs_path:find("/")
    if start then
      abs_path = abs_path:sub(start+1)
    end

    C.ERR_error_string_n(code, buf, ffi_sizeof(buf))
    table.insert(errors, string.format("%s:%d:%s",
      abs_path, line[0], ffi_str(buf))
    )

    if not all_errors then
      break
    end
  end

  C.ERR_clear_error()

  if #errors > 0 then
    return string.format("%s%s%s", (ctx or ""), (ctx and ": " or ""), table.concat(errors, " "))
  else
    return string.format("%s failed", ctx)
  end
end

local function format_all_error(ctx, code)
  return format_error(ctx, code, true)
end

return {
  format_error = format_error,
  format_all_error = format_all_error,
}