Comparison

util-src/crypto.c @ 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
parent 12697:916871447b2f
child 12702:f63176781940
comparison
equal deleted inserted replaced
12697:916871447b2f 12698:999663b4e39d
42 } 42 }
43 43
44 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_MD_CTX, EVP_MD_CTX*, EVP_MD_CTX_new, EVP_MD_CTX_free) 44 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_MD_CTX, EVP_MD_CTX*, EVP_MD_CTX_new, EVP_MD_CTX_free)
45 MANAGED_POINTER_ALLOCATOR(new_managed_BIO_s_mem, BIO*, new_memory_BIO, BIO_free) 45 MANAGED_POINTER_ALLOCATOR(new_managed_BIO_s_mem, BIO*, new_memory_BIO, BIO_free)
46 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_CIPHER_CTX, EVP_CIPHER_CTX*, EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_free) 46 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_CIPHER_CTX, EVP_CIPHER_CTX*, EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_free)
47
48 #define CRYPTO_KEY_TYPE_ERR "unexpected key type: got '%s', expected '%s'"
47 49
48 static EVP_PKEY* pkey_from_arg(lua_State *L, int idx, const int type, const int require_private) { 50 static EVP_PKEY* pkey_from_arg(lua_State *L, int idx, const int type, const int require_private) {
49 EVP_PKEY *pkey = *(EVP_PKEY**)luaL_checkudata(L, idx, PKEY_MT_TAG); 51 EVP_PKEY *pkey = *(EVP_PKEY**)luaL_checkudata(L, idx, PKEY_MT_TAG);
50 int got_type; 52 int got_type;
51 if(type || require_private) { 53 if(type || require_private) {
52 lua_getuservalue(L, idx); 54 lua_getuservalue(L, idx);
53 if(type != 0) { 55 if(type != 0) {
54 lua_getfield(L, -1, "type"); 56 lua_getfield(L, -1, "type");
55 got_type = lua_tointeger(L, -1); 57 got_type = lua_tointeger(L, -1);
56 if(got_type != type) { 58 if(got_type != type) {
57 luaL_argerror(L, idx, "unexpected key type"); 59 const char *got_key_type_name = OBJ_nid2sn(got_type);
60 const char *want_key_type_name = OBJ_nid2sn(type);
61 lua_pushfstring(L, CRYPTO_KEY_TYPE_ERR, got_key_type_name, want_key_type_name);
62 luaL_argerror(L, idx, lua_tostring(L, -1));
58 } 63 }
59 lua_pop(L, 1); 64 lua_pop(L, 1);
60 } 65 }
61 if(require_private != 0) { 66 if(require_private != 0) {
62 lua_getfield(L, -1, "private"); 67 lua_getfield(L, -1, "private");