Skip to content

Commit 5ffbbcc

Browse files
committed
feat(param) add new function to use OSSL_PARAM
1 parent 6812232 commit 5ffbbcc

File tree

3 files changed

+117
-81
lines changed

3 files changed

+117
-81
lines changed

lib/resty/openssl/include/param.lua

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
local ffi = require "ffi"
2+
local C = ffi.C
3+
local ffi_new = ffi.new
4+
local ffi_str = ffi.string
5+
6+
require "resty.openssl.include.ossl_typ"
7+
8+
ffi.cdef [[
9+
typedef struct ossl_param_st {
10+
const char *key; /* the name of the parameter */
11+
unsigned int data_type; /* declare what kind of content is in buffer */
12+
void *data; /* value being passed in or out */
13+
size_t data_size; /* data size */
14+
size_t return_size; /* returned content size */
15+
} OSSL_PARAM;
16+
17+
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf);
18+
OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
19+
size_t bsize);
20+
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
21+
size_t bsize);
22+
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
23+
size_t bsize);
24+
OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
25+
size_t bsize);
26+
OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
27+
size_t bsize);
28+
OSSL_PARAM OSSL_PARAM_construct_end(void);
29+
]]
30+
31+
local _M = {
32+
OSSL_PARAM_INTEGER = 1,
33+
OSSL_PARAM_UNSIGNED_INTEGER = 2,
34+
OSSL_PARAM_REAL = 3,
35+
OSSL_PARAM_UTF8_STRING = 4,
36+
OSSL_PARAM_OCTET_STRING = 5,
37+
OSSL_PARAM_UTF8_PTR = 6,
38+
OSSL_PARAM_OCTET_PTR = 7,
39+
}
40+
41+
function _M.construct(buf_t, length, types_map)
42+
local params = ffi_new("OSSL_PARAM[?]", length + 1)
43+
44+
local i = 0
45+
for key, value in pairs(buf_t) do
46+
local typ = types_map[key]
47+
if not typ then
48+
return nil, "param:construct: unknown key type \"" .. key .. "\""
49+
end
50+
local param
51+
if typ == _M.OSSL_PARAM_UTF8_PTR then -- out
52+
local buf = ffi_new("char*[1]")
53+
buf_t[key] = buf
54+
param = C.OSSL_PARAM_construct_utf8_ptr(key, buf, 0)
55+
elseif typ == _M.OSSL_PARAM_INTEGER then -- out (and in?)
56+
local buf = ffi_new("int[1]")
57+
buf_t[key] = buf
58+
param = C.OSSL_PARAM_construct_int(key, buf)
59+
elseif typ == _M.OSSL_PARAM_UTF8_STRING then -- in
60+
local buf = ffi.cast("char *", buf_t[key])
61+
param = C.OSSL_PARAM_construct_utf8_string(key, buf, #buf_t[key])
62+
else
63+
error("type " .. typ .. " is not yet implemented")
64+
end
65+
params[i] = param
66+
i = i + 1
67+
end
68+
69+
params[length] = C.OSSL_PARAM_construct_end()
70+
71+
return params
72+
end
73+
74+
function _M.parse(buf_t, length, types_map)
75+
for key, buf in pairs(buf_t) do
76+
local typ = types_map[key]
77+
if not typ then
78+
return nil, "param:parse: unknown key type \"" .. key .. "\""
79+
end
80+
if typ == _M.OSSL_PARAM_UTF8_PTR then
81+
buf_t[key] = ffi_str(buf[0])
82+
elseif typ == _M.OSSL_PARAM_INTEGER then
83+
buf_t[key] = tonumber(buf[0])
84+
else
85+
error("type " .. typ .. " is not yet implemented")
86+
end
87+
-- crypto_macro.OPENSSL_free(req[i-1].data)
88+
end
89+
90+
return buf_t
91+
end
92+
93+
return _M
+2-32
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
local ffi = require "ffi"
22

33
require "resty.openssl.include.ossl_typ"
4+
require "resty.openssl.include.param"
45

56
ffi.cdef [[
67
typedef struct ossl_provider_st OSSL_PROVIDER;
78
typedef struct ossl_lib_ctx_st OSSL_LIB_CTX;
89

9-
typedef struct ossl_param_st {
10-
const char *key; /* the name of the parameter */
11-
unsigned int data_type; /* declare what kind of content is in buffer */
12-
void *data; /* value being passed in or out */
13-
size_t data_size; /* data size */
14-
size_t return_size; /* returned content size */
15-
} OSSL_PARAM;
16-
1710
void OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
1811
const char *path);
1912

@@ -29,29 +22,6 @@ ffi.cdef [[
2922
// int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
3023
// ossl_provider_init_fn *init_fn);
3124

32-
const char *OSSL_PROVIDER_name(const OSSL_PROVIDER *prov);
25+
const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov);
3326
int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov);
34-
35-
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf);
36-
OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
37-
size_t bsize);
38-
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
39-
size_t bsize);
40-
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
41-
size_t bsize);
42-
OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
43-
size_t bsize);
44-
OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
45-
size_t bsize);
46-
OSSL_PARAM OSSL_PARAM_construct_end(void);
4727
]]
48-
49-
return {
50-
OSSL_PARAM_INTEGER = 1,
51-
OSSL_PARAM_UNSIGNED_INTEGER = 2,
52-
OSSL_PARAM_REAL = 3,
53-
OSSL_PARAM_UTF8_STRING = 4,
54-
OSSL_PARAM_OCTET_STRING = 5,
55-
OSSL_PARAM_UTF8_PTR = 6,
56-
OSSL_PARAM_OCTET_PTR = 7,
57-
}

lib/resty/openssl/provider.lua

+22-49
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local ffi = require "ffi"
22
local C = ffi.C
3-
local ffi_new = ffi.new
43
local ffi_str = ffi.string
54

6-
local provider_macro = require "resty.openssl.include.provider"
5+
require "resty.openssl.include.provider"
6+
local param_macro = require "resty.openssl.include.param"
77
local OPENSSL_30 = require("resty.openssl.version").OPENSSL_30
88
local format_error = require("resty.openssl.err").format_error
99

@@ -14,7 +14,7 @@ end
1414
local _M = {}
1515
local mt = {__index = _M}
1616

17-
local ossl_lib_ctx_st = ffi.typeof('OSSL_LIB_CTX*')
17+
local ossl_provider_ctx_ct = ffi.typeof('OSSL_PROVIDER*')
1818

1919
function _M.load(name, try)
2020
local ctx
@@ -37,18 +37,15 @@ function _M.load(name, try)
3737
end
3838

3939
function _M.set_default_search_path(path)
40-
if C.OSSL_PROVIDER_set_default_search_path(nil, path) ~= 1 then
41-
return false, format_error("provider.set_default_search_path")
42-
end
43-
return true
40+
C.OSSL_PROVIDER_set_default_search_path(nil, path)
4441
end
4542

4643
function _M.is_available(name)
4744
return C.OSSL_PROVIDER_available(nil, name) == 1
4845
end
4946

5047
function _M.istype(l)
51-
return l and l.ctx and ffi.istype(ossl_lib_ctx_st, l.ctx)
48+
return l and l.ctx and ffi.istype(ossl_provider_ctx_ct, l.ctx)
5249
end
5350

5451
function _M:unload()
@@ -67,16 +64,16 @@ end
6764

6865
local params_well_known = {
6966
-- Well known parameter names that core passes to providers
70-
["openssl-version"] = provider_macro.OSSL_PARAM_UTF8_PTR,
71-
["provider-name"] = provider_macro.OSSL_PARAM_UTF8_PTR,
72-
["module-filename"] = provider_macro.OSSL_PARAM_UTF8_PTR,
67+
["openssl-version"] = param_macro.OSSL_PARAM_UTF8_PTR,
68+
["provider-name"] = param_macro.OSSL_PARAM_UTF8_PTR,
69+
["module-filename"] = param_macro.OSSL_PARAM_UTF8_PTR,
7370

7471
-- Well known parameter names that Providers can define
75-
["name"] = provider_macro.OSSL_PARAM_UTF8_PTR,
76-
["version"] = provider_macro.OSSL_PARAM_UTF8_PTR,
77-
["buildinfo"] = provider_macro.OSSL_PARAM_UTF8_PTR,
78-
["status"] = provider_macro.OSSL_PARAM_INTEGER,
79-
["security-checks"] = provider_macro.OSSL_PARAM_INTEGER,
72+
["name"] = param_macro.OSSL_PARAM_UTF8_PTR,
73+
["version"] = param_macro.OSSL_PARAM_UTF8_PTR,
74+
["buildinfo"] = param_macro.OSSL_PARAM_UTF8_PTR,
75+
["status"] = param_macro.OSSL_PARAM_INTEGER,
76+
["security-checks"] = param_macro.OSSL_PARAM_INTEGER,
8077
}
8178

8279
local function load_gettable_names(ctx)
@@ -116,46 +113,22 @@ function _M:get_params(...)
116113
self.param_types = param_types
117114
end
118115

119-
local req = ffi_new("OSSL_PARAM[?]", key_length + 1)
120-
121116
local buffers = {}
122-
for i, key in ipairs(keys) do
123-
local typ = self.param_types[key]
124-
if not typ then
125-
return nil, "provider:get_params: unknown key \"" .. key .. "\""
126-
end
127-
local param
128-
if typ == provider_macro.OSSL_PARAM_UTF8_PTR then
129-
local buf = ffi_new("char*[1]")
130-
buffers[i] = buf
131-
param = C.OSSL_PARAM_construct_utf8_ptr(key, buf, 0)
132-
elseif typ == provider_macro.OSSL_PARAM_INTEGER then
133-
local buf = ffi_new("int[1]")
134-
buffers[i] = buf
135-
param = C.OSSL_PARAM_construct_int(key, buf)
136-
else
137-
return nil, "provider:get_params: not yet supported type \"" .. typ .. "\" for \"" .. key .. "\""
138-
end
139-
req[i-1] = param
117+
for _, key in ipairs(keys) do
118+
buffers[key] = ngx.null
119+
end
120+
local req, err = param_macro.construct(buffers, key_length, self.param_types)
121+
if not req then
122+
return nil, "provider:get_params: failed to construct params: " .. err
140123
end
141-
142-
req[key_length] = C.OSSL_PARAM_construct_end()
143124

144125
if C.OSSL_PROVIDER_get_params(self.ctx, req) ~= 1 then
145126
return nil, format_error("provider:get_params")
146127
end
147128

148-
for i=0, key_length do
149-
local buf = buffers[i]
150-
local key = keys[i]
151-
local typ = self.param_types[key]
152-
if typ == provider_macro.OSSL_PARAM_UTF8_PTR then
153-
buffers[key] = ffi_str(buf[0])
154-
elseif typ == provider_macro.OSSL_PARAM_INTEGER then
155-
buffers[key] = tonumber(buf[0])
156-
end
157-
buffers[i] = nil
158-
-- crypto_macro.OPENSSL_free(req[i-1].data)
129+
buffers, err = param_macro.parse(buffers, key_length, self.param_types)
130+
if err then
131+
return nil, "provider:get_params: failed to parse params: " .. err
159132
end
160133

161134
if key_length == 1 then

0 commit comments

Comments
 (0)