Changeset

12698:999663b4e39d

util.crypto: Friendlier error message on incorrect key types
author Matthew Wild <mwild1@gmail.com>
date Sat, 02 Jul 2022 11:51:24 +0100
parents 12697:916871447b2f
children 12699:b3d0c1457584
files util-src/crypto.c
diffstat 1 files changed, 6 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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);
 		}