From 0cf2e33f417d2ffc1ea6ee28e2846d382518c530 Mon Sep 17 00:00:00 2001 From: Zhefeng Chen Date: Fri, 17 Jan 2025 16:03:24 +0800 Subject: [PATCH 1/5] chore(spec): generate_keys supports EC key --- spec/internal/misc.lua | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/spec/internal/misc.lua b/spec/internal/misc.lua index c02d3683c2c..407ba537834 100644 --- a/spec/internal/misc.lua +++ b/spec/internal/misc.lua @@ -166,14 +166,23 @@ end -- @function generate_keys -- @param fmt format to receive the public and private pair -- @return `pub, priv` key tuple or `nil + err` on failure -local function generate_keys(fmt) +local function generate_keys(fmt, typ) fmt = string.upper(fmt) or "JWK" - local key, err = pkey.new({ - -- only support RSA for now - type = 'RSA', - bits = 2048, - exp = 65537 - }) + typ = string.upper(typ) or "RSA" + local key, err + -- only support RSA and EC for now + if typ == "RSA" then + key, err = pkey.new({ + type = 'RSA', + bits = 2048, + exp = 65537 + }) + else if typ == "EC" then + key, err = pkey.new({ + type = 'EC', + curve = 'prime256v1', + }) + end assert(key) assert(err == nil, err) local pub = key:tostring("public", fmt) From 10aea233f2fe26dc2bbbee80c18d59111be5b519 Mon Sep 17 00:00:00 2001 From: Zhefeng Chen Date: Fri, 17 Jan 2025 20:12:57 +0800 Subject: [PATCH 2/5] fixup --- spec/internal/misc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/internal/misc.lua b/spec/internal/misc.lua index 407ba537834..818f13cf75d 100644 --- a/spec/internal/misc.lua +++ b/spec/internal/misc.lua @@ -177,7 +177,7 @@ local function generate_keys(fmt, typ) bits = 2048, exp = 65537 }) - else if typ == "EC" then + elseif typ == "EC" then key, err = pkey.new({ type = 'EC', curve = 'prime256v1', From cd9145ef566694df82c5bdb08c120b3aef0c5cc0 Mon Sep 17 00:00:00 2001 From: Zhefeng Chen Date: Fri, 17 Jan 2025 22:51:43 +0800 Subject: [PATCH 3/5] fix the generate_keys error --- spec/internal/misc.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/internal/misc.lua b/spec/internal/misc.lua index 818f13cf75d..f6cf710e4a9 100644 --- a/spec/internal/misc.lua +++ b/spec/internal/misc.lua @@ -165,10 +165,11 @@ end --- Generate asymmetric keys -- @function generate_keys -- @param fmt format to receive the public and private pair +-- @param typ (optional) the type of key to generate, default: RSA -- @return `pub, priv` key tuple or `nil + err` on failure local function generate_keys(fmt, typ) fmt = string.upper(fmt) or "JWK" - typ = string.upper(typ) or "RSA" + typ = typ or string.upper(typ) or "RSA" local key, err -- only support RSA and EC for now if typ == "RSA" then From c59edcb55eabea79d4a71c4f703449a3a42a2b31 Mon Sep 17 00:00:00 2001 From: Zhefeng Chen Date: Tue, 21 Jan 2025 11:06:35 +0800 Subject: [PATCH 4/5] fixup --- spec/internal/misc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/internal/misc.lua b/spec/internal/misc.lua index f6cf710e4a9..c8d5fe169eb 100644 --- a/spec/internal/misc.lua +++ b/spec/internal/misc.lua @@ -169,7 +169,7 @@ end -- @return `pub, priv` key tuple or `nil + err` on failure local function generate_keys(fmt, typ) fmt = string.upper(fmt) or "JWK" - typ = typ or string.upper(typ) or "RSA" + typ = typ and string.upper(typ) or "RSA" local key, err -- only support RSA and EC for now if typ == "RSA" then From 88f06bb8095a493386eb8296e8332b01b6f6eb4e Mon Sep 17 00:00:00 2001 From: Zhefeng Chen Date: Tue, 21 Jan 2025 14:30:50 +0800 Subject: [PATCH 5/5] add an else for unsupported key type --- spec/internal/misc.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/internal/misc.lua b/spec/internal/misc.lua index c8d5fe169eb..978706bd7b8 100644 --- a/spec/internal/misc.lua +++ b/spec/internal/misc.lua @@ -178,11 +178,15 @@ local function generate_keys(fmt, typ) bits = 2048, exp = 65537 }) + elseif typ == "EC" then key, err = pkey.new({ type = 'EC', curve = 'prime256v1', }) + + else + return nil, "unsupported key type" end assert(key) assert(err == nil, err)