Skip to content

Commit b671676

Browse files
committed
feat(pkey) add pkey:get_size and allow only return NID of key type
1 parent ff0d02c commit b671676

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Table of Contents
6060
+ [pkey:set_parameters](#pkeyset_parameters)
6161
+ [pkey:is_private](#pkeyis_private)
6262
+ [pkey:get_key_type](#pkeyget_key_type)
63+
+ [pkey:get_size](#pkeyget_size)
6364
+ [pkey:get_default_digest_type](#pkeyget_default_digest_type)
6465
+ [pkey:sign](#pkeysign)
6566
+ [pkey:verify](#pkeyverify)
@@ -1120,19 +1121,37 @@ it's a public key.
11201121

11211122
### pkey:get_key_type
11221123

1123-
**syntax**: *obj, err = pk:get_key_type()*
1124+
**syntax**: *obj, err = pk:get_key_type(nid_only?)*
11241125

11251126
Returns a ASN1_OBJECT of key type of the private key as a table.
11261127

1128+
Starting from lua-resty-openssl 1.6.0, an optional argument `nid_only` can be set to `true`
1129+
to only return the numeric NID of the key.
1130+
11271131
```lua
11281132
local pkey, err = require("resty.openssl.pkey").new({type="X448"})
11291133

11301134
ngx.say(require("cjson").encode(pkey:get_key_type()))
11311135
-- outputs '{"ln":"X448","nid":1035,"sn":"X448","id":"1.3.101.111"}'
1136+
ngx.say(pkey:get_key_type(true))
1137+
-- outputs 1035
11321138
```
11331139

11341140
[Back to TOC](#table-of-contents)
11351141

1142+
### pkey:get_size
1143+
1144+
**syntax**: *size, err = pk:get_size()*
1145+
1146+
Returns the maximum suitable size for the output buffers for almost all
1147+
operations that can be done with pkey.
1148+
1149+
For RSA key, this is the size of the modulus.
1150+
For EC, Ed25519 and Ed448 keys, this is the size of the private key.
1151+
For DH key, this is the size of the prime modulus.
1152+
1153+
[Back to TOC](#table-of-contents)
1154+
11361155
### pkey:get_default_digest_type
11371156

11381157
**syntax**: *obj, err = pk:get_default_digest_type()*

lib/resty/openssl/pkey.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,12 @@ function _M.istype(l)
625625
return l and l.ctx and ffi.istype(evp_pkey_ptr_ct, l.ctx)
626626
end
627627

628-
function _M:get_key_type()
629-
return objects_lib.nid2table(self.key_type)
628+
function _M:get_key_type(nid_only)
629+
return nid_only and self.key_type or objects_lib.nid2table(self.key_type)
630+
end
631+
632+
function _M:get_size()
633+
return OPENSSL_3X and C.EVP_PKEY_get_size(self.ctx) or C.EVP_PKEY_size(self.ctx)
630634
end
631635

632636
function _M:get_default_digest_type()

t/openssl/pkey.t

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,18 +1310,40 @@ true
13101310
type = 'RSA',
13111311
}))
13121312
ngx.say(encode_sorted_json(p:get_key_type()))
1313+
ngx.say(p:get_key_type(true))
13131314
}
13141315
}
13151316
--- request
13161317
GET /t
13171318
--- response_body_like eval
1318-
'{"id":"1.2.840.113549.1.1.1","ln":"rsaEncryption","nid":6,"sn":"rsaEncryption"}'
1319+
'{"id":"1.2.840.113549.1.1.1","ln":"rsaEncryption","nid":6,"sn":"rsaEncryption"}
1320+
6'
13191321
--- no_error_log
13201322
[error]
13211323

13221324

13231325

1324-
=== TEST 38: misc: Checks if it's private key
1326+
=== TEST 38: misc: get size
1327+
--- http_config eval: $::HttpConfig
1328+
--- config
1329+
location =/t {
1330+
content_by_lua_block {
1331+
local p, err = myassert(require("resty.openssl.pkey").new({
1332+
type = 'EC',
1333+
}))
1334+
ngx.say(p:get_size())
1335+
}
1336+
}
1337+
--- request
1338+
GET /t
1339+
--- response_body
1340+
56
1341+
--- no_error_log
1342+
[error]
1343+
1344+
1345+
1346+
=== TEST 39: misc: Checks if it's private key
13251347
--- http_config eval: $::HttpConfig
13261348
--- config
13271349
location =/t {
@@ -1359,7 +1381,7 @@ true
13591381

13601382

13611383

1362-
=== TEST 39: misc: Checks if it's private key: ecx
1384+
=== TEST 40: misc: Checks if it's private key: ecx
13631385
--- http_config eval: $::HttpConfig
13641386
--- config
13651387
location =/t {
@@ -1395,7 +1417,7 @@ true
13951417

13961418

13971419

1398-
=== TEST 40: misc: Returns provider
1420+
=== TEST 41: misc: Returns provider
13991421
--- http_config eval: $::HttpConfig
14001422
--- config
14011423
location =/t {
@@ -1419,7 +1441,7 @@ default
14191441

14201442

14211443

1422-
=== TEST 41: params: Returns gettable, settable params
1444+
=== TEST 42: params: Returns gettable, settable params
14231445
--- http_config eval: $::HttpConfig
14241446
--- config
14251447
location =/t {
@@ -1445,7 +1467,7 @@ default
14451467

14461468

14471469

1448-
=== TEST 42: params: Get params, set params
1470+
=== TEST 43: params: Get params, set params
14491471
--- http_config eval: $::HttpConfig
14501472
--- config
14511473
location =/t {

0 commit comments

Comments
 (0)