diff options
| author | William Ahern <william@25thandClement.com> | 2016-01-08 01:01:37 +0800 |
|---|---|---|
| committer | William Ahern <william@25thandClement.com> | 2016-01-08 01:01:37 +0800 |
| commit | 9362e313739931ca8f62aa94eb6b11325e95bdb2 (patch) | |
| tree | 2fd3c642b640c27a3cf4952beec8c582748a0df6 | |
| parent | 2180c60e159edc097fa9197a717dc66141e11eb4 (diff) | |
| download | luaossl-9362e313739931ca8f62aa94eb6b11325e95bdb2.tar.gz luaossl-9362e313739931ca8f62aa94eb6b11325e95bdb2.tar.bz2 luaossl-9362e313739931ca8f62aa94eb6b11325e95bdb2.zip | |
fix EVP_PKEY method interposing
| -rw-r--r-- | src/openssl.c | 80 |
1 files changed, 64 insertions, 16 deletions
diff --git a/src/openssl.c b/src/openssl.c index 7f368ee..b539d87 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -285,22 +285,15 @@ static void *testsimple(lua_State *L, int index, const char *tname) { | |||
| 285 | } /* testsimple() */ | 285 | } /* testsimple() */ |
| 286 | 286 | ||
| 287 | 287 | ||
| 288 | static int interpose(lua_State *L, const char *mt) { | 288 | static int auxL_swapmetatable(lua_State *, const char *); |
| 289 | luaL_getmetatable(L, mt); | 289 | static int auxL_swapmetasubtable(lua_State *, const char *, const char *); |
| 290 | |||
| 291 | if (!strncmp("__", luaL_checkstring(L, 1), 2)) | ||
| 292 | lua_pushvalue(L, -1); | ||
| 293 | else | ||
| 294 | lua_getfield(L, -1, "__index"); | ||
| 295 | 290 | ||
| 296 | lua_pushvalue(L, -4); /* push method name */ | 291 | static int interpose(lua_State *L, const char *mt) { |
| 297 | lua_gettable(L, -2); /* push old method */ | 292 | if (!strncmp("__", luaL_checkstring(L, -2), 2)) { |
| 298 | 293 | return auxL_swapmetatable(L, mt); | |
| 299 | lua_pushvalue(L, -5); /* push method name */ | 294 | } else { |
| 300 | lua_pushvalue(L, -5); /* push new method */ | 295 | return auxL_swapmetasubtable(L, mt, "__index"); |
| 301 | lua_settable(L, -4); /* replace old method */ | 296 | } |
| 302 | |||
| 303 | return 1; /* return old method */ | ||
| 304 | } /* interpose() */ | 297 | } /* interpose() */ |
| 305 | 298 | ||
| 306 | static int auxL_checkoption(lua_State *, int, const char *, const char *const *, _Bool); | 299 | static int auxL_checkoption(lua_State *, int, const char *, const char *const *, _Bool); |
| @@ -863,6 +856,49 @@ static _Bool auxL_newclass(lua_State *L, const char *name, const auxL_Reg *metho | |||
| 863 | #define auxL_addclass(L, ...) \ | 856 | #define auxL_addclass(L, ...) \ |
| 864 | (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1)) | 857 | (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1)) |
| 865 | 858 | ||
| 859 | static int auxL_swaptable(lua_State *L, int index) { | ||
| 860 | index = lua_absindex(L, index); | ||
| 861 | |||
| 862 | lua_pushvalue(L, -2); /* push key */ | ||
| 863 | lua_gettable(L, index); /* push old value */ | ||
| 864 | |||
| 865 | lua_pushvalue(L, -3); /* push key */ | ||
| 866 | lua_pushvalue(L, -3); /* push new value */ | ||
| 867 | lua_settable(L, index); /* replace old value */ | ||
| 868 | |||
| 869 | lua_replace(L, -3); | ||
| 870 | lua_pop(L, 1); | ||
| 871 | |||
| 872 | return 1; /* return old value */ | ||
| 873 | } /* auxL_swaptable() */ | ||
| 874 | |||
| 875 | static int auxL_swapmetatable(lua_State *L, const char *name) { | ||
| 876 | luaL_getmetatable(L, name); | ||
| 877 | |||
| 878 | lua_pushvalue(L, -3); | ||
| 879 | lua_pushvalue(L, -3); | ||
| 880 | auxL_swaptable(L, -3); | ||
| 881 | |||
| 882 | lua_replace(L, -4); | ||
| 883 | lua_pop(L, 2); | ||
| 884 | |||
| 885 | return 1; | ||
| 886 | } /* auxL_swapmetatable() */ | ||
| 887 | |||
| 888 | static int auxL_swapmetasubtable(lua_State *L, const char *name, const char *subname) { | ||
| 889 | luaL_getmetatable(L, name); | ||
| 890 | lua_getfield(L, -1, subname); | ||
| 891 | |||
| 892 | lua_pushvalue(L, -4); | ||
| 893 | lua_pushvalue(L, -4); | ||
| 894 | auxL_swaptable(L, -3); | ||
| 895 | |||
| 896 | lua_replace(L, -5); | ||
| 897 | lua_pop(L, 3); | ||
| 898 | |||
| 899 | return 1; | ||
| 900 | } /* auxL_swapmetasubtable() */ | ||
| 901 | |||
| 866 | #define auxL_EDYLD -2 | 902 | #define auxL_EDYLD -2 |
| 867 | #define auxL_EOPENSSL -1 | 903 | #define auxL_EOPENSSL -1 |
| 868 | 904 | ||
| @@ -2551,7 +2587,19 @@ done: | |||
| 2551 | 2587 | ||
| 2552 | 2588 | ||
| 2553 | static int pk_interpose(lua_State *L) { | 2589 | static int pk_interpose(lua_State *L) { |
| 2554 | return interpose(L, PKEY_CLASS); | 2590 | lua_settop(L, 2); |
| 2591 | |||
| 2592 | luaL_getmetatable(L, PKEY_CLASS); | ||
| 2593 | if (!strncmp("__", luaL_checkstring(L, 1), 2)) { | ||
| 2594 | lua_insert(L, 1); | ||
| 2595 | } else { | ||
| 2596 | lua_getfield(L, -1, "__index"); | ||
| 2597 | lua_getupvalue(L, -1, 1); | ||
| 2598 | lua_insert(L, 1); | ||
| 2599 | lua_pop(L, 2); | ||
| 2600 | } | ||
| 2601 | |||
| 2602 | return auxL_swaptable(L, 1); | ||
| 2555 | } /* pk_interpose() */ | 2603 | } /* pk_interpose() */ |
| 2556 | 2604 | ||
| 2557 | 2605 | ||
