Skip to content

Commit

Permalink
Support loading raw EC keys
Browse files Browse the repository at this point in the history
Either public or private key.
  • Loading branch information
singpolyma committed Sep 19, 2023
1 parent 95f2ee3 commit c920d31
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4109,7 +4109,7 @@ static int pk_new(lua_State *L) {
EVP_PKEY **ud;

/* #1 table or key; if key, #2 format and #3 type */
lua_settop(L, 3);
lua_settop(L, 4);

if (lua_istable(L, 1) || lua_isnil(L, 1)) {
int type = EVP_PKEY_RSA;
Expand Down Expand Up @@ -4361,6 +4361,12 @@ static int pk_new(lua_State *L) {
}
}

int curve_id = -1;
if ((opt = luaL_optstring(L, 4, NULL))) {
if (!auxS_txt2nid(&curve_id, opt))
luaL_argerror(L, 4, lua_pushfstring(L, "%s: invalid curve", opt));
}

data = luaL_checklstring(L, 1, &len);

ud = prepsimple(L, PKEY_CLASS);
Expand Down Expand Up @@ -4405,6 +4411,31 @@ static int pk_new(lua_State *L) {
}
}

if (type == X509_ANY && (pubonly && !pub) && curve_id >= 0) {
EC_KEY *ec = EC_KEY_new();

if (!EC_KEY_set_group(ec, EC_GROUP_new_by_curve_name(curve_id))) {
goterr = 1;
} else if (!EC_KEY_oct2key(ec, data, len, getctx(L))) {
goterr = 1;
} else {
pub = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(pub, ec);
}
}

if (type == X509_ANY && (!pubonly && !prvt) && curve_id >= 0) {
EC_KEY *ec = EC_KEY_new();
if (!EC_KEY_set_group(ec, EC_GROUP_new_by_curve_name(curve_id))) {
goterr = 1;
} else if (!EC_KEY_oct2priv(ec, data, len)) {
goterr = 1;
} else {
prvt = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(prvt, ec);
}
}

if (prvt) {
#if 0
/* TODO: Determine if this is necessary. */
Expand Down

0 comments on commit c920d31

Please sign in to comment.