aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/openssl/param.lua
blob: 2c8dcea65338b42ae7c4eae3767ed8003b9ae19e (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
local ffi = require "ffi"
local C = ffi.C
local ffi_new = ffi.new
local ffi_str = ffi.string
local ffi_cast = ffi.cast

require "resty.openssl.include.param"
local format_error = require("resty.openssl.err").format_error
local bn_lib = require("resty.openssl.bn")
local null = require("resty.openssl.auxiliary.ctypes").null

local OSSL_PARAM_INTEGER = 1
local OSSL_PARAM_UNSIGNED_INTEGER = 2
local OSSL_PARAM_REAL = 3
local OSSL_PARAM_UTF8_STRING = 4
local OSSL_PARAM_OCTET_STRING = 5
local OSSL_PARAM_UTF8_PTR = 6
local OSSL_PARAM_OCTET_PTR = 7

local alter_type_key = {}
local buf_param_key = {}

local function construct(buf_t, length, types_map, types_size)
  if not length then
    length = 0
    for k, v in pairs(buf_t) do length = length + 1 end
  end

  local params = ffi_new("OSSL_PARAM[?]", length + 1)

  local i = 0
  local buf_param
  for key, value in pairs(buf_t) do
    local typ = types_map[key]
    if not typ then
      return nil, "param:construct: unknown key \"" .. key .. "\""
    end
    local param, buf, size
    if value == null then -- out
      value = nil
      size = types_size and types_size[key] or 100
      if typ == OSSL_PARAM_UTF8_STRING or typ == OSSL_PARAM_OCTET_STRING then
        buf = ffi_new("char[?]", size)
      end
    else
      local numeric = type(value) == "number"
      if (numeric and typ >= OSSL_PARAM_UTF8_STRING) or
        (not numeric and typ <= OSSL_PARAM_UNSIGNED_INTEGER) then
        local alter_typ = types_map[alter_type_key] and types_map[alter_type_key][key]
        if alter_typ and ((numeric and alter_typ <= OSSL_PARAM_UNSIGNED_INTEGER) or
                        (not numeric and alter_typ >= OSSL_PARAM_UTF8_STRING)) then
          typ = alter_typ
        else
          return nil, "param:construct: key \"" .. key .. "\" can't be a " .. type(value)
        end
      end
    end

    if typ == "bn" then -- out only
      buf = ffi_new("char[?]", size)
      param = C.OSSL_PARAM_construct_BN(key, buf, size)
      buf_param = buf_param or {}
      buf_param[key] = param
    elseif typ == OSSL_PARAM_INTEGER then
      buf = value and ffi_new("int[1]", value) or ffi_new("int[1]")
      param = C.OSSL_PARAM_construct_int(key, buf)
    elseif typ == OSSL_PARAM_UNSIGNED_INTEGER then
      buf = value and ffi_new("unsigned int[1]", value) or
                      ffi_new("unsigned int[1]")
      param = C.OSSL_PARAM_construct_uint(key, buf)
    elseif typ == OSSL_PARAM_UTF8_STRING then
      buf = value and ffi_cast("char *", value) or buf
      param = C.OSSL_PARAM_construct_utf8_string(key, buf, value and #value or size)
    elseif typ == OSSL_PARAM_OCTET_STRING then
      buf = value and ffi_cast("char *", value) or buf
      param = C.OSSL_PARAM_construct_octet_string(key, ffi_cast("void*", buf),
                                                  value and #value or size)
    elseif typ == OSSL_PARAM_UTF8_PTR then
      buf = ffi_new("char*[1]")
      param = C.OSSL_PARAM_construct_utf8_ptr(key, buf, 0)
    elseif typ == OSSL_PARAM_OCTET_PTR then
      buf = ffi_new("char*[1]")
      param = C.OSSL_PARAM_construct_octet_ptr(key, ffi_cast("void**", buf), 0)
    else
      error("type " .. typ .. " is not yet implemented")
    end
    if not value then -- out
      buf_t[key] = buf
    end
    params[i] = param
    i = i + 1
  end

  buf_t[buf_param_key] = buf_param
  params[length] = C.OSSL_PARAM_construct_end()

  return params
end

local function parse(buf_t, length, types_map, types_size)
  for key, buf in pairs(buf_t) do
    local typ = types_map[key]
    local sz = types_size and types_size[key]

    if key == buf_param_key then -- luacheck: ignore
      -- ignore
    elseif buf == nil or buf[0] == nil then
      buf_t[key] = nil
    elseif typ == "bn" then
      local bn_t = ffi_new("BIGNUM*[1]")
      local param = buf_t[buf_param_key][key]
      if C.OSSL_PARAM_get_BN(param, bn_t) ~= 1 then
        return nil, format_error("param:parse: OSSL_PARAM_get_BN")
      end
      buf_t[key] = bn_lib.dup(bn_t[0])
    elseif typ == OSSL_PARAM_INTEGER or
        typ == OSSL_PARAM_UNSIGNED_INTEGER then
      buf_t[key] = tonumber(buf[0])
    elseif typ == OSSL_PARAM_UTF8_STRING or
        typ == OSSL_PARAM_OCTET_STRING then
      buf_t[key] = sz and ffi_str(buf, sz) or ffi_str(buf)
    elseif typ == OSSL_PARAM_UTF8_PTR or
          typ == OSSL_PARAM_OCTET_PTR then
      buf_t[key] = sz and ffi_str(buf[0], sz) or ffi_str(buf[0])
    elseif not typ then
        return nil, "param:parse: unknown key type \"" .. key .. "\""
    else
      error("type " .. typ .. " is not yet implemented")
    end
  end
  -- for GC
  buf_t[buf_param_key] = nil

  return buf_t
end

local param_type_readable = {
  [OSSL_PARAM_UNSIGNED_INTEGER] = "unsigned integer",
  [OSSL_PARAM_INTEGER] = "integer",
  [OSSL_PARAM_REAL] = "real number",
  [OSSL_PARAM_UTF8_PTR] = "pointer to a UTF8 encoded string",
  [OSSL_PARAM_UTF8_STRING] = "UTF8 encoded string",
  [OSSL_PARAM_OCTET_PTR] = "pointer to an octet string",
  [OSSL_PARAM_OCTET_STRING] = "octet string",
}

local function readable_data_type(p)
  local typ = p.data_type
  local literal = param_type_readable[typ]
  if not literal then
    literal = string.format("unknown type [%d]", typ)
  end

  local sz = tonumber(p.data_size)
  if sz == 0 then
    literal = literal .. " (arbitrary size)"
  else
    literal = literal .. string.format(" (max %d bytes large)", sz)
  end
  return literal
end

local function parse_params_schema(params, schema, schema_readable)
  if params == nil then
    return nil, format_error("parse_params_schema")
  end

  local i = 0
  while true do
    local p = params[i]
    if p.key == nil then
      break
    end
    local key = ffi_str(p.key)
    if schema then
      -- TODO: don't support same key with different types for now
      -- prefer string type over integer types
      local typ = tonumber(p.data_type)
      if schema[key] then
        schema[alter_type_key] = schema[alter_type_key] or {}
        schema[alter_type_key][key] = typ
      else
        schema[key] = typ
      end
    end
    -- if schema_return_size then -- only non-ptr string types are needed actually
    --   schema_return_size[key] = tonumber(p.return_size)
    -- end
    if schema_readable then
      table.insert(schema_readable, { key, readable_data_type(p) })
    end
    i = i + 1
  end
  return schema
end

local param_maps_set, param_maps_get = {}, {}

local function get_params_func(typ, field)
  local typ_lower = typ:sub(5):lower()
  if typ_lower:sub(-4) == "_ctx" then
    typ_lower = typ_lower:sub(0, -5)
  end
  -- field name for indexing schema, usually the (const) one created by
  -- EVP_TYP_fetch or EVP_get_typebynam,e
  field = field or "algo"

  local cf_settable = C[typ .. "_settable_params"]
  local settable = function(self, raw)
    local k = self[field]
    if raw and param_maps_set[k] then
      return param_maps_set[k]
    end

    local param = cf_settable(self.ctx)
    -- no params, this is fine, shouldn't be regarded as an error
    if param == nil then
      param_maps_set[k] = {}
      return {}
    end
    local schema, schema_reabale = {}, raw and nil or {}
    parse_params_schema(param, schema, schema_reabale)
    param_maps_set[k] = schema

    return raw and schema or schema_reabale
  end

  local cf_set = C[typ .. "_set_params"]
  local set = function(self, params)
    if not param_maps_set[self[field]] then
      local ok, err = self:settable_params()
      if not ok then
        return false, typ_lower .. ":set_params: " .. err
      end
    end

    local oparams, err = construct(params, nil, param_maps_set[self[field]])
    if err then
      return false, typ_lower .. ":set_params: " .. err
    end

    if cf_set(self.ctx, oparams) ~= 1 then
      return false, format_error(typ_lower .. ":set_params: " .. typ .. "_set_params")
    end

    return true
  end

  local cf_gettable = C[typ .. "_gettable_params"]
  local gettable = function(self, raw)
    local k = self[field]
    if raw and param_maps_set[k] then
      return param_maps_set[k]
    end

    local param = cf_gettable(self.ctx)
    -- no params, this is fine, shouldn't be regarded as an error
    if param == nil then
      param_maps_get[k] = {}
      return {}
    end
    local schema, schema_reabale = {}, raw and nil or {}
    parse_params_schema(param, schema, schema_reabale)
    param_maps_set[k] = schema

    return raw and schema or schema_reabale
  end

  local cf_get = C[typ .. "_get_params"]
  local get_buffer, get_size_map = {}, {}
  local get = function(self, key, want_size, want_type)
    if not param_maps_get[self[field]] then
      local ok, err = self:gettable_params()
      if not ok then
        return false, typ_lower .. ":set_params: " .. err
      end
    end
    local schema = param_maps_set[self[field]]
    if schema == nil or not schema[key] then -- nil or null
      return nil, typ_lower .. ":get_param: unknown key \"" .. key .. "\""
    end

    table.clear(get_buffer)
    table.clear(get_size_map)
    get_buffer[key] = null
    get_size_map[key] = want_size
    schema = want_type and { [key] = want_type } or schema

    local req, err = construct(get_buffer, 1, schema, get_size_map)
    if not req then
      return nil, typ_lower .. ":get_param: failed to construct params: " .. err
    end

    if cf_get(self.ctx, req) ~= 1 then
      return nil, format_error(typ_lower .. ":get_param:get")
    end

    get_buffer, err = parse(get_buffer, 1, schema, get_size_map)
    if err then
      return nil, typ_lower .. ":get_param: failed to parse params: " .. err
    end

    return get_buffer[key]
  end

  return settable, set, gettable, get
end

return {
  OSSL_PARAM_INTEGER = OSSL_PARAM_INTEGER,
  OSSL_PARAM_UNSIGNED_INTEGER = OSSL_PARAM_INTEGER,
  OSSL_PARAM_REAL = OSSL_PARAM_REAL,
  OSSL_PARAM_UTF8_STRING = OSSL_PARAM_UTF8_STRING,
  OSSL_PARAM_OCTET_STRING = OSSL_PARAM_OCTET_STRING,
  OSSL_PARAM_UTF8_PTR = OSSL_PARAM_UTF8_PTR,
  OSSL_PARAM_OCTET_PTR = OSSL_PARAM_OCTET_PTR,

  construct = construct,
  parse = parse,
  parse_params_schema = parse_params_schema,
  get_params_func = get_params_func,
}