Skip to content

Commit 2b86d68

Browse files
committed
Add branch for each key type in pk_new field marshalling
1 parent 93b1587 commit 2b86d68

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/openssl.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,23 +3241,41 @@ static int pk_new(lua_State *L) {
32413241
luaL_argcheck(L, type != NID_undef, 1, lua_pushfstring(L, "%s: invalid key type", id));
32423242
}
32433243

3244-
if (loadfield(L, 1, "bits", LUA_TNUMBER, &n)) {
3245-
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `bits' invalid", n));
3246-
bits = (unsigned)n;
3247-
}
3244+
switch(type) {
3245+
case EVP_PKEY_RSA:
3246+
if (loadfield(L, 1, "bits", LUA_TNUMBER, &n)) {
3247+
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `bits' invalid", n));
3248+
bits = (unsigned)n;
3249+
}
32483250

3249-
if (loadfield(L, 1, "exp", LUA_TNUMBER, &n)) {
3250-
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `exp' invalid", n));
3251-
exp = (unsigned)n;
3252-
}
3251+
if (loadfield(L, 1, "exp", LUA_TNUMBER, &n)) {
3252+
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `exp' invalid", n));
3253+
exp = (unsigned)n;
3254+
}
3255+
break;
3256+
case EVP_PKEY_DH:
3257+
/* dhparam field can contain a PEM encoded string.
3258+
The "dhparam" field takes precedence over "bits" */
3259+
if (loadfield(L, 1, "dhparam", LUA_TSTRING, &dhparam))
3260+
break;
32533261

3254-
if (loadfield(L, 1, "curve", LUA_TSTRING, &id)) {
3255-
if (!auxS_txt2nid(&curve, id))
3256-
luaL_argerror(L, 1, lua_pushfstring(L, "%s: invalid curve", id));
3257-
}
3262+
if (loadfield(L, 1, "bits", LUA_TNUMBER, &n)) {
3263+
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `bits' invalid", n));
3264+
bits = (unsigned)n;
3265+
}
32583266

3259-
/* dhparam field can contain a PEM encoded string. */
3260-
loadfield(L, 1, "dhparam", LUA_TSTRING, &dhparam);
3267+
if (loadfield(L, 1, "exp", LUA_TNUMBER, &n)) {
3268+
luaL_argcheck(L, n > 0 && n < UINT_MAX, 1, lua_pushfstring(L, "%f: `exp' invalid", n));
3269+
exp = (unsigned)n;
3270+
}
3271+
break;
3272+
case EVP_PKEY_EC:
3273+
if (loadfield(L, 1, "curve", LUA_TSTRING, &id)) {
3274+
if (!auxS_txt2nid(&curve, id))
3275+
luaL_argerror(L, 1, lua_pushfstring(L, "%s: invalid curve", id));
3276+
}
3277+
break;
3278+
}
32613279

32623280
creat:
32633281
if (!(*ud = EVP_PKEY_new()))

0 commit comments

Comments
 (0)