@@ -20,17 +20,23 @@ local OSSL_PARAM_OCTET_PTR = 7
20
20
21
21
local alter_type_key = {}
22
22
local buf_param_key = {}
23
+ local buf_anchor_key = {}
23
24
24
25
local function construct (buf_t , length , types_map , types_size )
25
26
if not length then
26
27
length = nkeys (buf_t )
27
28
end
28
29
30
+
29
31
local params = ffi_new (" OSSL_PARAM[?]" , length + 1 )
30
32
31
33
local i = 0
32
- local buf_param
34
+ local buf_param , buf_anchored
33
35
for key , value in pairs (buf_t ) do
36
+ if key == buf_anchor_key then
37
+ goto continue
38
+ end
39
+
34
40
local typ = types_map [key ]
35
41
if not typ then
36
42
return nil , " param:construct: unknown key \" " .. key .. " \" "
@@ -69,28 +75,38 @@ local function construct(buf_t, length, types_map, types_size)
69
75
ffi_new (" unsigned int[1]" )
70
76
param = C .OSSL_PARAM_construct_uint (key , buf )
71
77
elseif typ == OSSL_PARAM_UTF8_STRING then
72
- buf = value and ffi_cast (" char *" , value ) or buf
78
+ buf = value ~= nil and ffi_cast (" char *" , value ) or buf
73
79
param = C .OSSL_PARAM_construct_utf8_string (key , buf , value and # value or size )
74
80
elseif typ == OSSL_PARAM_OCTET_STRING then
75
- buf = value and ffi_cast (" char *" , value ) or buf
81
+ buf = value ~= nil and ffi_cast (" char *" , value ) or buf
76
82
param = C .OSSL_PARAM_construct_octet_string (key , ffi_cast (" void*" , buf ),
77
83
value and # value or size )
78
- elseif typ == OSSL_PARAM_UTF8_PTR then
84
+ elseif typ == OSSL_PARAM_UTF8_PTR then -- out only
79
85
buf = ffi_new (" char*[1]" )
80
86
param = C .OSSL_PARAM_construct_utf8_ptr (key , buf , 0 )
81
- elseif typ == OSSL_PARAM_OCTET_PTR then
87
+ elseif typ == OSSL_PARAM_OCTET_PTR then -- out only
82
88
buf = ffi_new (" char*[1]" )
83
89
param = C .OSSL_PARAM_construct_octet_ptr (key , ffi_cast (" void**" , buf ), 0 )
84
90
else
85
91
error (" type " .. typ .. " is not yet implemented" )
86
92
end
87
- if not value then -- out
93
+
94
+ if value == nil then -- out
88
95
buf_t [key ] = buf
96
+ else -- in
97
+ -- save value as OSSL_PARAM_construct_* doesn't copy the value
98
+ buf_anchored = buf_anchored or {}
99
+ buf_anchored [key ] = buf
89
100
end
101
+
90
102
params [i ] = param
91
103
i = i + 1
104
+
105
+ :: continue::
92
106
end
93
107
108
+ buf_t [buf_anchor_key ] = buf_anchored
109
+
94
110
buf_t [buf_param_key ] = buf_param
95
111
params [length ] = C .OSSL_PARAM_construct_end ()
96
112
@@ -112,7 +128,8 @@ local function parse(buf_t, length, types_map, types_size)
112
128
if C .OSSL_PARAM_get_BN (param , bn_t ) ~= 1 then
113
129
return nil , format_error (" param:parse: OSSL_PARAM_get_BN" )
114
130
end
115
- buf_t [key ] = bn_lib .dup (bn_t [0 ])
131
+ buf_t [key ] = assert (bn_lib .dup (bn_t [0 ]))
132
+ C .BN_free (bn_t [0 ])
116
133
elseif typ == OSSL_PARAM_INTEGER or
117
134
typ == OSSL_PARAM_UNSIGNED_INTEGER then
118
135
buf_t [key ] = tonumber (buf [0 ])
@@ -228,7 +245,7 @@ local function get_params_func(typ, field)
228
245
local cf_set = C [typ .. " _set_params" ]
229
246
local set = function (self , params )
230
247
if not param_maps_set [self [field ]] then
231
- local ok , err = self :settable_params ()
248
+ local ok , err = self :settable_params (true ) -- only query raw schema to save memory
232
249
if not ok then
233
250
return false , typ_lower .. " :set_params: " .. err
234
251
end
@@ -270,7 +287,7 @@ local function get_params_func(typ, field)
270
287
local get_buffer , get_size_map = {}, {}
271
288
local get = function (self , key , want_size , want_type )
272
289
if not param_maps_get [self [field ]] then
273
- local ok , err = self :gettable_params ()
290
+ local ok , err = self :gettable_params (true ) -- only query raw schema to save memory
274
291
if not ok then
275
292
return false , typ_lower .. " :set_params: " .. err
276
293
end
0 commit comments