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 112372f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ int openssl_pkey_is_private(EVP_PKEY* pkey)
int ret = 0;
int typ;
assert(pkey != NULL);

typ = EVP_PKEY_type(EVP_PKEY_id(pkey));
switch (typ)
{
Expand Down Expand Up @@ -67,6 +68,14 @@ int openssl_pkey_is_private(EVP_PKEY* pkey)
}
#endif
default:
#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);
}
#endif
break;
}

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')
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 112372f

Please sign in to comment.