# HG changeset patch # User Matthew Wild # Date 1656759084 -3600 # Node ID 999663b4e39d24c4136101a2620461ba4b33a82a # Parent 916871447b2fd4c2b4a32a06e6ad2ff5102761c5 util.crypto: Friendlier error message on incorrect key types diff -r 916871447b2f -r 999663b4e39d util-src/crypto.c --- a/util-src/crypto.c Sat Jul 02 11:50:56 2022 +0100 +++ b/util-src/crypto.c Sat Jul 02 11:51:24 2022 +0100 @@ -45,6 +45,8 @@ MANAGED_POINTER_ALLOCATOR(new_managed_BIO_s_mem, BIO*, new_memory_BIO, BIO_free) MANAGED_POINTER_ALLOCATOR(new_managed_EVP_CIPHER_CTX, EVP_CIPHER_CTX*, EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_free) +#define CRYPTO_KEY_TYPE_ERR "unexpected key type: got '%s', expected '%s'" + static EVP_PKEY* pkey_from_arg(lua_State *L, int idx, const int type, const int require_private) { EVP_PKEY *pkey = *(EVP_PKEY**)luaL_checkudata(L, idx, PKEY_MT_TAG); int got_type; @@ -54,7 +56,10 @@ lua_getfield(L, -1, "type"); got_type = lua_tointeger(L, -1); if(got_type != type) { - luaL_argerror(L, idx, "unexpected key type"); + const char *got_key_type_name = OBJ_nid2sn(got_type); + const char *want_key_type_name = OBJ_nid2sn(type); + lua_pushfstring(L, CRYPTO_KEY_TYPE_ERR, got_key_type_name, want_key_type_name); + luaL_argerror(L, idx, lua_tostring(L, -1)); } lua_pop(L, 1); }