Skip to content

Commit 62ec94b

Browse files
committed
src/openssl.c: Add support for raw key types to pkey:getParameters()
1 parent 64756fc commit 62ec94b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/openssl.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@
239239
#define HAVE_EVP_PKEY_KEYGEN (OPENSSL_PREREQ(1,0,0) || LIBRESSL_PREREQ(2,0,0))
240240
#endif
241241

242+
#ifndef HAVE_EVP_PKEY_RAW
243+
#define HAVE_EVP_PKEY_RAW OPENSSL_PREREQ(1,1,1)
244+
#endif
245+
242246
#ifndef HAVE_HMAC_CTX_FREE
243247
#define HAVE_HMAC_CTX_FREE (OPENSSL_PREREQ(1,1,0) || LIBRESSL_PREREQ(2,7,0))
244248
#endif
@@ -4279,12 +4283,18 @@ enum pk_param {
42794283
PK_EC_GROUP,
42804284
PK_EC_PUB_KEY,
42814285
PK_EC_PRIV_KEY,
4286+
4287+
#define PK_RAW_OPTLIST { "pub_key", "priv_key", NULL }
4288+
#define PK_RAW_OPTOFFSET PK_RAW_PUB_KEY
4289+
PK_RAW_PUB_KEY,
4290+
PK_RAW_PRIV_KEY,
42824291
}; /* enum pk_param */
42834292

42844293
static const char *const pk_rsa_optlist[] = PK_RSA_OPTLIST;
42854294
static const char *const pk_dsa_optlist[] = PK_DSA_OPTLIST;
42864295
static const char *const pk_dh_optlist[] = PK_DH_OPTLIST;
42874296
static const char *const pk_ec_optlist[] = PK_EC_OPTLIST;
4297+
static const char *const pk_raw_optlist[] = PK_RAW_OPTLIST;
42884298

42894299
const char *const *pk_getoptlist(int type, int *_nopts, int *_optoffset) {
42904300
const char *const *optlist = NULL;
@@ -4315,6 +4325,17 @@ const char *const *pk_getoptlist(int type, int *_nopts, int *_optoffset) {
43154325
optoffset = PK_EC_OPTOFFSET;
43164326

43174327
break;
4328+
#if HAVE_EVP_PKEY_RAW
4329+
case EVP_PKEY_X25519:
4330+
case EVP_PKEY_X448:
4331+
case EVP_PKEY_ED25519:
4332+
case EVP_PKEY_ED448:
4333+
optlist = pk_raw_optlist;
4334+
nopts = countof(pk_raw_optlist) - 1;
4335+
optoffset = PK_RAW_OPTOFFSET;
4336+
4337+
break;
4338+
#endif
43184339
}
43194340

43204341
if (_nopts)
@@ -4331,6 +4352,8 @@ static EC_GROUP *ecg_dup_nil(lua_State *, const EC_GROUP *);
43314352

43324353
static void pk_pushparam(lua_State *L, EVP_PKEY *pkey, enum pk_param which) {
43334354
const BIGNUM *i;
4355+
luaL_Buffer B;
4356+
size_t len;
43344357

43354358
switch (which) {
43364359
case PK_RSA_N:
@@ -4448,6 +4471,20 @@ static void pk_pushparam(lua_State *L, EVP_PKEY *pkey, enum pk_param which) {
44484471

44494472
break;
44504473
#endif
4474+
#if HAVE_EVP_PKEY_RAW
4475+
case PK_RAW_PUB_KEY:
4476+
EVP_PKEY_get_raw_public_key(pkey, NULL, &len);
4477+
EVP_PKEY_get_raw_public_key(pkey, luaL_buffinitsize(L, &B, len), &len);
4478+
luaL_pushresultsize(&B, len);
4479+
4480+
break;
4481+
case PK_RAW_PRIV_KEY:
4482+
EVP_PKEY_get_raw_private_key(pkey, NULL, &len);
4483+
EVP_PKEY_get_raw_private_key(pkey, luaL_buffinitsize(L, &B, len), &len);
4484+
luaL_pushresultsize(&B, len);
4485+
4486+
break;
4487+
#endif
44514488
default:
44524489
luaL_error(L, "%d: invalid EVP_PKEY parameter", which);
44534490
}
@@ -4589,6 +4626,13 @@ static void pk_setparam(lua_State *L, EVP_PKEY *pkey, enum pk_param which, int i
45894626

45904627
break;
45914628
}
4629+
#endif
4630+
#if HAVE_EVP_PKEY_RAW
4631+
case PK_RAW_PUB_KEY:
4632+
case PK_RAW_PRIV_KEY:
4633+
luaL_error(L, "%d: EVP_PKEY parameter is immutable", which);
4634+
4635+
break;
45924636
#endif
45934637
default:
45944638
luaL_error(L, "%d: invalid EVP_PKEY parameter", which);

0 commit comments

Comments
 (0)