Skip to content

Commit b36ccba

Browse files
committed
feat(openssl) list functions can now optionally drop provider name
1 parent 5381f10 commit b36ccba

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -460,33 +460,37 @@ Sets the default properties for all future EVP algorithm fetches, implicit as we
460460

461461
### openssl.list_cipher_algorithms
462462

463-
**syntax**: *ret = openssl.list_cipher_algorithms()*
463+
**syntax**: *ret = openssl.list_cipher_algorithms(hide_provider?)*
464464

465-
Return available cipher algorithms in an array.
465+
Return available cipher algorithms in an array. Set `hide_provider` to `true` to
466+
hide provider name from the result.
466467

467468
[Back to TOC](#table-of-contents)
468469

469470
### openssl.list_digest_algorithms
470471

471-
**syntax**: *ret = openssl.list_digest_algorithms()*
472+
**syntax**: *ret = openssl.list_digest_algorithms(hide_provider?)*
472473

473-
Return available digest algorithms in an array.
474+
Return available digest algorithms in an array. Set `hide_provider` to `true` to
475+
hide provider name from the result.
474476

475477
[Back to TOC](#table-of-contents)
476478

477479
### openssl.list_mac_algorithms
478480

479-
**syntax**: *ret = openssl.list_mac_algorithms()*
481+
**syntax**: *ret = openssl.list_mac_algorithms(hide_provider?)*
480482

481-
Return available MAC algorithms in an array.
483+
Return available MAC algorithms in an array. Set `hide_provider` to `true` to
484+
hide provider name from the result.
482485

483486
[Back to TOC](#table-of-contents)
484487

485488
### openssl.list_kdf_algorithms
486489

487-
**syntax**: *ret = openssl.list_kdf_algorithms()*
490+
**syntax**: *ret = openssl.list_kdf_algorithms(hide_provider?)*
488491

489-
Return available KDF algorithms in an array.
492+
Return available KDF algorithms in an array. Set `hide_provider` to `true` to
493+
hide provider name from the result.
490494

491495
[Back to TOC](#table-of-contents)
492496

lib/resty/openssl.lua

+20-26
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ local function list_legacy(typ, get_nid_cf)
326326
return ret
327327
end
328328

329-
local function list_provided(typ)
329+
local function list_provided(typ, hide_provider)
330330
local typ_lower = string.lower(typ:sub(5)) -- cut off EVP_
331331
local typ_ptr = typ .. "*"
332332
require ("resty.openssl.include.evp." .. typ_lower)
@@ -338,9 +338,13 @@ local function list_provided(typ)
338338
function(elem, _)
339339
elem = ffi_cast(typ_ptr, elem)
340340
local name = ffi_str(C[typ .. "_get0_name"](elem))
341-
-- alternate names are ignored, retrieve use TYPE_names_do_all
342-
local prov = ffi_str(C.OSSL_PROVIDER_get0_name(C[typ .. "_get0_provider"](elem)))
343-
table.insert(ret, name .. " @ " .. prov)
341+
if hide_provider then
342+
table.insert(ret, name)
343+
else
344+
-- alternate names are ignored, retrieve use TYPE_names_do_all
345+
local prov = ffi_str(C.OSSL_PROVIDER_get0_name(C[typ .. "_get0_provider"](elem)))
346+
table.insert(ret, name .. " @ " .. prov)
347+
end
344348
end)
345349

346350
C[typ .. "_do_all_provided"](ctx_lib.get_libctx(), fn, nil)
@@ -350,50 +354,40 @@ local function list_provided(typ)
350354
return ret
351355
end
352356

353-
function _M.list_cipher_algorithms()
357+
function _M.list_cipher_algorithms(hide_provider)
354358
require "resty.openssl.include.evp.cipher"
355-
local ret = list_legacy("EVP_CIPHER",
356-
OPENSSL_3X and C.EVP_CIPHER_get_nid or C.EVP_CIPHER_nid)
357359

358360
if OPENSSL_3X then
359-
local ret_provided = list_provided("EVP_CIPHER")
360-
for _, r in ipairs(ret_provided) do
361-
table.insert(ret, r)
362-
end
361+
return list_provided("EVP_CIPHER", hide_provider)
362+
else
363+
return list_legacy("EVP_CIPHER", C.EVP_CIPHER_nid)
363364
end
364-
365-
return ret
366365
end
367366

368-
function _M.list_digest_algorithms()
367+
function _M.list_digest_algorithms(hide_provider)
369368
require "resty.openssl.include.evp.md"
370-
local ret = list_legacy("EVP_MD",
371-
OPENSSL_3X and C.EVP_MD_get_type or C.EVP_MD_type)
372369

373370
if OPENSSL_3X then
374-
local ret_provided = list_provided("EVP_MD")
375-
for _, r in ipairs(ret_provided) do
376-
table.insert(ret, r)
377-
end
371+
return list_provided("EVP_MD", hide_provider)
372+
else
373+
return list_legacy("EVP_MD", C.EVP_MD_type)
378374
end
379-
380-
return ret
381375
end
382376

383-
function _M.list_mac_algorithms()
377+
function _M.list_mac_algorithms(hide_provider)
384378
if not OPENSSL_3X then
385379
return nil, "openssl.list_mac_algorithms is only supported from OpenSSL 3.0"
386380
end
387381

388-
return list_provided("EVP_MAC")
382+
return list_provided("EVP_MAC", hide_provider)
389383
end
390384

391-
function _M.list_kdf_algorithms()
385+
function _M.list_kdf_algorithms(hide_provider)
392386
if not OPENSSL_3X then
393387
return nil, "openssl.list_kdf_algorithms is only supported from OpenSSL 3.0"
394388
end
395389

396-
return list_provided("EVP_KDF")
390+
return list_provided("EVP_KDF", hide_provider)
397391
end
398392

399393
local valid_ssl_protocols = {

t/openssl.t

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ false
7373
location =/t {
7474
content_by_lua_block {
7575
local openssl = require("resty.openssl")
76-
ngx.say(require("cjson").encode(openssl.list_cipher_algorithms()))
76+
ngx.say(require("cjson").encode(openssl.list_cipher_algorithms(true)))
7777
local version = require("resty.openssl.version")
7878
if not version.OPENSSL_3X then
7979
ngx.say("[\"AES-256-GCM @ default\"]")
@@ -98,7 +98,7 @@ false
9898
location =/t {
9999
content_by_lua_block {
100100
local openssl = require("resty.openssl")
101-
ngx.say(require("cjson").encode(openssl.list_digest_algorithms()))
101+
ngx.say(require("cjson").encode(openssl.list_digest_algorithms(true)))
102102
local version = require("resty.openssl.version")
103103
if not version.OPENSSL_3X then
104104
ngx.say("[\"SHA2-256 @ default\"]")

0 commit comments

Comments
 (0)