Skip to content

Commit

Permalink
chore: more flexible key:is_private()
Browse files Browse the repository at this point in the history
close #307
  • Loading branch information
zhaozg committed Mar 31, 2024
1 parent 36ea101 commit 5666413
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ static const char *evp_pkey_type2name(int type);
int openssl_pkey_is_private(EVP_PKEY* pkey)
{
int ret = 0;
int typ;
assert(pkey != NULL);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (EVP_PKEY_private_check(ctx))
ret = 1;
EVP_PKEY_CTX_free(ctx);
#else
int typ;
typ = EVP_PKEY_type(EVP_PKEY_id(pkey));
switch (typ)
{
Expand Down Expand Up @@ -69,6 +75,7 @@ int openssl_pkey_is_private(EVP_PKEY* pkey)
default:
break;
}
#endif

return ret;
}
Expand Down
10 changes: 10 additions & 0 deletions test/4.pkey.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ function TestPKEYMY:testBasic()
end
end

if helper.openssl3 then
function testED25519()
local key = openssl.pkey.ctx_new('ED25519'):keygen()
assert(key:is_private())
local pem = key:export('pem', true)
key = openssl.pkey.read(pem, true, 'pem')
assert(key:is_private())
end
end

function testRSA()
local nrsa = {'rsa', 1024, 3}
local rsa = pkey.new(unpack(nrsa))
Expand Down

0 comments on commit 5666413

Please sign in to comment.