summaryrefslogtreecommitdiff
path: root/src/openssl.c
diff options
context:
space:
mode:
authorWilliam Ahern <william@25thandClement.com>2016-01-07 23:51:25 +0800
committerWilliam Ahern <william@25thandClement.com>2016-01-07 23:51:25 +0800
commit2180c60e159edc097fa9197a717dc66141e11eb4 (patch)
treeebe721eb7e658a029ce13944de4411800ebd058a /src/openssl.c
parent4cb69e49c4f11046ba05863f326a35538c135a29 (diff)
downloadluaossl-2180c60e159edc097fa9197a717dc66141e11eb4.tar.gz
luaossl-2180c60e159edc097fa9197a717dc66141e11eb4.tar.bz2
luaossl-2180c60e159edc097fa9197a717dc66141e11eb4.zip
permit direct indexing of EVP_KEY as alternative to getParameters/setParameters
Diffstat (limited to 'src/openssl.c')
-rw-r--r--src/openssl.c574
1 files changed, 356 insertions, 218 deletions
diff --git a/src/openssl.c b/src/openssl.c
index 78eef68..7f368ee 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -303,36 +303,7 @@ static int interpose(lua_State *L, const char *mt) {
303 return 1; /* return old method */ 303 return 1; /* return old method */
304} /* interpose() */ 304} /* interpose() */
305 305
306 306static int auxL_checkoption(lua_State *, int, const char *, const char *const *, _Bool);
307static void addclass(lua_State *L, const char *name, const luaL_Reg *methods, const luaL_Reg *metamethods) {
308 if (luaL_newmetatable(L, name)) {
309 luaL_setfuncs(L, metamethods, 0);
310 lua_newtable(L);
311 luaL_setfuncs(L, methods, 0);
312 lua_setfield(L, -2, "__index");
313 lua_pop(L, 1);
314 }
315} /* addclass() */
316
317
318static int badoption(lua_State *L, int index, const char *opt) {
319 opt = (opt)? opt : luaL_checkstring(L, index);
320
321 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option %s", opt));
322} /* badoption() */
323
324static int checkoption(lua_State *L, int index, const char *def, const char *const opts[]) {
325 const char *opt = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
326 int i;
327
328 for (i = 0; opts[i]; i++) {
329 if (strieq(opts[i], opt))
330 return i;
331 }
332
333 return badoption(L, index, opt);
334} /* checkoption() */
335
336 307
337#define X509_ANY 0x01 308#define X509_ANY 0x01
338#define X509_PEM 0x02 309#define X509_PEM 0x02
@@ -344,7 +315,7 @@ static int optencoding(lua_State *L, int index, const char *def, int allow) {
344 static const char *const opts[] = { "*", "pem", "der", "pretty", NULL }; 315 static const char *const opts[] = { "*", "pem", "der", "pretty", NULL };
345 int type = 0; 316 int type = 0;
346 317
347 switch (checkoption(L, index, def, opts)) { 318 switch (auxL_checkoption(L, index, def, opts, 1)) {
348 case 0: 319 case 0:
349 type = X509_ANY; 320 type = X509_ANY;
350 break; 321 break;
@@ -658,6 +629,27 @@ NOTUSED static auxtype_t auxL_getref(lua_State *L, auxref_t ref) {
658 return lua_type(L, -1); 629 return lua_type(L, -1);
659} /* auxL_getref() */ 630} /* auxL_getref() */
660 631
632static int auxL_testoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) {
633 const char *optname = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
634 int (*optcmp)() = (nocase)? &strcasecmp : &strcmp;
635
636 for (int i = 0; optlist[i]; i++) {
637 if (0 == optcmp(optlist[i], optname))
638 return i;
639 }
640
641 return -1;
642} /* auxL_testoption() */
643
644static int auxL_checkoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) {
645 int i;
646
647 if ((i = auxL_testoption(L, index, def, optlist, nocase)) >= 0)
648 return i;
649
650 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option '%s'", luaL_optstring(L, index, def)));
651} /* auxL_checkoption() */
652
661/* 653/*
662 * Lua 5.3 distinguishes integers and numbers, and by default uses 64-bit 654 * Lua 5.3 distinguishes integers and numbers, and by default uses 64-bit
663 * integers. The following routines try to preserve this distinction and 655 * integers. The following routines try to preserve this distinction and
@@ -753,6 +745,13 @@ static auxL_Unsigned (auxL_optunsigned)(lua_State *L, int index, auxL_Unsigned d
753 return (lua_isnoneornil(L, index))? def : auxL_checkunsigned(L, index, min, max); 745 return (lua_isnoneornil(L, index))? def : auxL_checkunsigned(L, index, min, max);
754} /* auxL_optunsigned() */ 746} /* auxL_optunsigned() */
755 747
748static int auxL_size2int(lua_State *L, size_t n) {
749 if (n > INT_MAX)
750 luaL_error(L, "integer value out of range (%zu > INT_MAX)", n);
751
752 return (int)n;
753} /* auxL_size2int() */
754
756typedef struct { 755typedef struct {
757 const char *name; 756 const char *name;
758 auxL_Integer value; 757 auxL_Integer value;
@@ -765,6 +764,105 @@ static void auxL_setintegers(lua_State *L, const auxL_IntegerReg *l) {
765 } 764 }
766} /* auxL_setintegers() */ 765} /* auxL_setintegers() */
767 766
767#define AUXL_REG_NULL (&(auxL_Reg[]){ 0 })
768
769typedef struct {
770 const char *name;
771 lua_CFunction func;
772 unsigned nups; /* in addition to nups specified to auxL_setfuncs */
773} auxL_Reg;
774
775static inline size_t auxL_liblen(const auxL_Reg *l) {
776 size_t n = 0;
777
778 while ((l++)->name)
779 n++;
780
781 return n;
782} /* auxL_liblen() */
783
784#define auxL_newlibtable(L, l) \
785 lua_createtable((L), 0, countof((l)) - 1)
786
787#define auxL_newlib(L, l, nups) \
788 (auxL_newlibtable((L), (l)), auxL_setfuncs((L), (l), (nups)))
789
790static void auxL_setfuncs(lua_State *L, const auxL_Reg *l, int nups) {
791 for (; l->name; l++) {
792 /* copy shared upvalues */
793 luaL_checkstack(L, nups, "too many upvalues");
794 for (int i = 0; i < nups; i++)
795 lua_pushvalue(L, -nups);
796
797 /* nil-fill local upvalues */
798 luaL_checkstack(L, l->nups, "too many upvalues");
799 lua_settop(L, lua_gettop(L) + l->nups);
800
801 /* set closure */
802 luaL_checkstack(L, 1, "too many upvalues");
803 lua_pushcclosure(L, l->func, nups + l->nups);
804 lua_setfield(L, -(nups + 2), l->name);
805 }
806
807 lua_pop(L, nups);
808
809 return;
810} /* auxL_setfuncs() */
811
812static void auxL_clear(lua_State *L, int tindex) {
813 tindex = lua_absindex(L, tindex);
814
815 lua_pushnil(L);
816 while (lua_next(L, tindex)) {
817 lua_pop(L, 1);
818 lua_pushvalue(L, -1);
819 lua_pushnil(L);
820 lua_rawset(L, tindex);
821 }
822} /* auxL_clear() */
823
824static _Bool auxL_newmetatable(lua_State *L, const char *name, _Bool reset) {
825 if (luaL_newmetatable(L, name))
826 return 1;
827 if (!reset)
828 return 0;
829
830 /*
831 * NB: Keep existing table as it may be cached--e.g. in
832 * another module that isn't being reloaded. But scrub it
833 * clean so function interposition--which will presumably
834 * run again if the C module is being reloaded--doesn't
835 * result in loops.
836 */
837 auxL_clear(L, -1);
838 lua_pushnil(L);
839 lua_setmetatable(L, -2);
840#if LUA_VERSION_NUM >= 502
841 lua_pushnil(L);
842 lua_setuservalue(L, -2);
843#endif
844
845 return 0;
846} /* auxL_newmetatable() */
847
848static _Bool auxL_newclass(lua_State *L, const char *name, const auxL_Reg *methods, const auxL_Reg *metamethods, _Bool reset) {
849 _Bool fresh = auxL_newmetatable(L, name, reset);
850 int n;
851
852 auxL_setfuncs(L, metamethods, 0);
853
854 if ((n = auxL_liblen(methods))) {
855 lua_createtable(L, 0, auxL_size2int(L, n));
856 auxL_setfuncs(L, methods, 0);
857 lua_setfield(L, -2, "__index");
858 }
859
860 return fresh;
861} /* auxL_newclass() */
862
863#define auxL_addclass(L, ...) \
864 (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1))
865
768#define auxL_EDYLD -2 866#define auxL_EDYLD -2
769#define auxL_EOPENSSL -1 867#define auxL_EOPENSSL -1
770 868
@@ -1368,7 +1466,7 @@ static int ossl_version(lua_State *L) {
1368 return 1; 1466 return 1;
1369} /* ossl_version() */ 1467} /* ossl_version() */
1370 1468
1371static const luaL_Reg ossl_globals[] = { 1469static const auxL_Reg ossl_globals[] = {
1372 { "version", &ossl_version }, 1470 { "version", &ossl_version },
1373 { NULL, NULL }, 1471 { NULL, NULL },
1374}; 1472};
@@ -1540,7 +1638,7 @@ static const auxL_IntegerReg ssleay_version[] = {
1540int luaopen__openssl(lua_State *L) { 1638int luaopen__openssl(lua_State *L) {
1541 size_t i; 1639 size_t i;
1542 1640
1543 luaL_newlib(L, ossl_globals); 1641 auxL_newlib(L, ossl_globals, 0);
1544 1642
1545 for (i = 0; i < countof(opensslconf_no); i++) { 1643 for (i = 0; i < countof(opensslconf_no); i++) {
1546 if (*opensslconf_no[i]) { 1644 if (*opensslconf_no[i]) {
@@ -2097,7 +2195,7 @@ sslerr:
2097} /* bn_tohex() */ 2195} /* bn_tohex() */
2098 2196
2099 2197
2100static const luaL_Reg bn_methods[] = { 2198static const auxL_Reg bn_methods[] = {
2101 { "add", &bn__add }, 2199 { "add", &bn__add },
2102 { "sub", &bn__sub }, 2200 { "sub", &bn__sub },
2103 { "mul", &bn__mul }, 2201 { "mul", &bn__mul },
@@ -2116,7 +2214,7 @@ static const luaL_Reg bn_methods[] = {
2116 { NULL, NULL }, 2214 { NULL, NULL },
2117}; 2215};
2118 2216
2119static const luaL_Reg bn_metatable[] = { 2217static const auxL_Reg bn_metatable[] = {
2120 { "__add", &bn__add }, 2218 { "__add", &bn__add },
2121 { "__sub", &bn__sub }, 2219 { "__sub", &bn__sub },
2122 { "__mul", &bn__mul }, 2220 { "__mul", &bn__mul },
@@ -2136,7 +2234,7 @@ static const luaL_Reg bn_metatable[] = {
2136}; 2234};
2137 2235
2138 2236
2139static const luaL_Reg bn_globals[] = { 2237static const auxL_Reg bn_globals[] = {
2140 { "new", &bn_new }, 2238 { "new", &bn_new },
2141 { "interpose", &bn_interpose }, 2239 { "interpose", &bn_interpose },
2142 { "generatePrime", &bn_generatePrime }, 2240 { "generatePrime", &bn_generatePrime },
@@ -2146,7 +2244,7 @@ static const luaL_Reg bn_globals[] = {
2146int luaopen__openssl_bignum(lua_State *L) { 2244int luaopen__openssl_bignum(lua_State *L) {
2147 initall(L); 2245 initall(L);
2148 2246
2149 luaL_newlib(L, bn_globals); 2247 auxL_newlib(L, bn_globals, 0);
2150 2248
2151 return 1; 2249 return 1;
2152} /* luaopen__openssl_bignum() */ 2250} /* luaopen__openssl_bignum() */
@@ -2599,7 +2697,7 @@ static int pk_toPEM(lua_State *L) {
2599 NULL, 2697 NULL,
2600 }; 2698 };
2601 2699
2602 switch (checkoption(L, i, NULL, opts)) { 2700 switch (auxL_checkoption(L, i, NULL, opts, 1)) {
2603 case 0: case 1: /* public, PublicKey */ 2701 case 0: case 1: /* public, PublicKey */
2604 if (!PEM_write_bio_PUBKEY(bio, key)) 2702 if (!PEM_write_bio_PUBKEY(bio, key))
2605 return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring"); 2703 return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");
@@ -2728,26 +2826,50 @@ static const char *const pk_dsa_optlist[] = PK_DSA_OPTLIST;
2728static const char *const pk_dh_optlist[] = PK_DH_OPTLIST; 2826static const char *const pk_dh_optlist[] = PK_DH_OPTLIST;
2729static const char *const pk_ec_optlist[] = PK_EC_OPTLIST; 2827static const char *const pk_ec_optlist[] = PK_EC_OPTLIST;
2730 2828
2731static int pk_checkparam(lua_State *L, int type, int index) { 2829const char *const *pk_getoptlist(int type, int *_nopts, int *_optoffset) {
2830 const char *const *optlist = NULL;
2831 int nopts = 0, optoffset = 0;
2832
2732 switch (type) { 2833 switch (type) {
2733 case EVP_PKEY_RSA: 2834 case EVP_PKEY_RSA:
2734 return luaL_checkoption(L, index, NULL, pk_rsa_optlist) + PK_RSA_OPTOFFSET; 2835 optlist = pk_rsa_optlist;
2836 nopts = countof(pk_rsa_optlist) - 1;
2837 optoffset = PK_RSA_OPTOFFSET;
2838
2839 break;
2735 case EVP_PKEY_DSA: 2840 case EVP_PKEY_DSA:
2736 return luaL_checkoption(L, index, NULL, pk_dsa_optlist) + PK_DSA_OPTOFFSET; 2841 optlist = pk_dsa_optlist;
2842 nopts = countof(pk_dsa_optlist) - 1;
2843 optoffset = PK_DSA_OPTOFFSET;
2844
2845 break;
2737 case EVP_PKEY_DH: 2846 case EVP_PKEY_DH:
2738 return luaL_checkoption(L, index, NULL, pk_dh_optlist) + PK_DH_OPTOFFSET; 2847 optlist = pk_dh_optlist;
2848 nopts = countof(pk_dh_optlist) - 1;
2849 optoffset = PK_DH_OPTOFFSET;
2850
2851 break;
2739 case EVP_PKEY_EC: 2852 case EVP_PKEY_EC:
2740 return luaL_checkoption(L, index, NULL, pk_ec_optlist) + PK_EC_OPTOFFSET; 2853 optlist = pk_ec_optlist;
2741 default: 2854 nopts = countof(pk_ec_optlist) - 1;
2742 return luaL_error(L, "%d: unsupported EVP_PKEY base type", type); 2855 optoffset = PK_EC_OPTOFFSET;
2856
2857 break;
2743 } 2858 }
2744} /* pk_checkparam() */ 2859
2860 if (_nopts)
2861 *_nopts = nopts;
2862 if (_optoffset)
2863 *_optoffset = optoffset;
2864
2865 return optlist;
2866} /* pk_getoptlist() */
2745 2867
2746#ifndef OPENSSL_NO_EC 2868#ifndef OPENSSL_NO_EC
2747static EC_GROUP *ecg_dup_nil(lua_State *, const EC_GROUP *); 2869static EC_GROUP *ecg_dup_nil(lua_State *, const EC_GROUP *);
2748#endif 2870#endif
2749 2871
2750static void pk_pushparam(lua_State *L, void *_key, enum pk_param which) { 2872static void pk_pushparam(lua_State *L, void *base_key, enum pk_param which) {
2751 union { 2873 union {
2752 RSA *rsa; 2874 RSA *rsa;
2753 DH *dh; 2875 DH *dh;
@@ -2755,7 +2877,7 @@ static void pk_pushparam(lua_State *L, void *_key, enum pk_param which) {
2755#ifndef OPENSSL_NO_EC 2877#ifndef OPENSSL_NO_EC
2756 EC_KEY *ec; 2878 EC_KEY *ec;
2757#endif 2879#endif
2758 } key = { _key }; 2880 } key = { base_key };
2759 2881
2760 switch (which) { 2882 switch (which) {
2761 case PK_RSA_N: 2883 case PK_RSA_N:
@@ -2883,7 +3005,7 @@ static _Bool pk_bn_set_nothrow(BIGNUM **dst, BIGNUM *src) {
2883 goto sslerr; \ 3005 goto sslerr; \
2884} while (0) 3006} while (0)
2885 3007
2886static void pk_setparam(lua_State *L, void *_key, enum pk_param which, int index) { 3008static void pk_setparam(lua_State *L, void *base_key, enum pk_param which, int index) {
2887 union { 3009 union {
2888 RSA *rsa; 3010 RSA *rsa;
2889 DH *dh; 3011 DH *dh;
@@ -2891,7 +3013,7 @@ static void pk_setparam(lua_State *L, void *_key, enum pk_param which, int index
2891#ifndef OPENSSL_NO_EC 3013#ifndef OPENSSL_NO_EC
2892 EC_KEY *ec; 3014 EC_KEY *ec;
2893#endif 3015#endif
2894 } key = { _key }; 3016 } key = { base_key };
2895 3017
2896 switch (which) { 3018 switch (which) {
2897 case PK_RSA_N: 3019 case PK_RSA_N:
@@ -3014,76 +3136,51 @@ sslerr:
3014 3136
3015 3137
3016static int pk_getParameters(lua_State *L) { 3138static int pk_getParameters(lua_State *L) {
3017 EVP_PKEY *_key = checksimple(L, 1, PKEY_CLASS); 3139 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3018 int type = EVP_PKEY_base_id(_key); 3140 int base_type = EVP_PKEY_base_id(key);
3019 void *key; 3141 void *base_key;
3020 int otop, index, tindex; 3142 const char *const *optlist;
3143 int nopts, optoffset, otop, index, tindex;
3021 3144
3022 if (!(key = EVP_PKEY_get0(_key))) 3145 if (!(base_key = EVP_PKEY_get0(key)))
3023 goto sslerr; 3146 goto sslerr;
3024 3147
3025 if (lua_isnoneornil(L, 2)) { 3148 if (!(optlist = pk_getoptlist(base_type, &nopts, &optoffset)))
3026 const char *const *optlist; 3149 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type);
3027 const char *const *opt;
3028
3029 switch (type) {
3030 case EVP_PKEY_RSA:
3031 optlist = pk_rsa_optlist;
3032 luaL_checkstack(L, countof(pk_rsa_optlist), "");
3033
3034 break;
3035 case EVP_PKEY_DSA:
3036 optlist = pk_dsa_optlist;
3037 luaL_checkstack(L, countof(pk_dsa_optlist), "");
3038
3039 break;
3040 case EVP_PKEY_DH:
3041 optlist = pk_dh_optlist;
3042 luaL_checkstack(L, countof(pk_dh_optlist), "");
3043
3044 break;
3045 case EVP_PKEY_EC:
3046 optlist = pk_ec_optlist;
3047 luaL_checkstack(L, countof(pk_ec_optlist), "");
3048
3049 break;
3050 default:
3051 return luaL_error(L, "%d: unsupported EVP_PKEY base type", type);
3052 }
3053 3150
3151 if (lua_isnoneornil(L, 2)) {
3054 /* 3152 /*
3055 * Use special "{" parameter to tell loop to push table. 3153 * Use special "{" parameter to tell loop to push table.
3056 * Subsequent parameters will be assigned as fields. 3154 * Subsequent parameters will be assigned as fields.
3057 *
3058 * NOTE: optlist arrays are NULL-terminated. luaL_checkstack()
3059 * calls above left room for "{".
3060 */ 3155 */
3061 lua_pushstring(L, "{"); 3156 lua_pushstring(L, "{");
3062 3157 luaL_checkstack(L, nopts, NULL);
3063 for (opt = optlist; *opt; opt++) { 3158 for (const char *const *optname = optlist; *optname; optname++) {
3064 lua_pushstring(L, *opt); 3159 lua_pushstring(L, *optname);
3065 } 3160 }
3066 } 3161 }
3067 3162
3068 otop = lua_gettop(L); 3163 otop = lua_gettop(L);
3069 3164
3070 /* provide space for results and working area */ 3165 /* provide space for results and working area */
3071 luaL_checkstack(L, (otop - 1) + LUA_MINSTACK, ""); 3166 luaL_checkstack(L, (otop - 1) + LUA_MINSTACK, NULL);
3072 3167
3073 /* no table index, yet */ 3168 /* no table index, yet */
3074 tindex = 0; 3169 tindex = 0;
3075 3170
3076 for (index = 2; index <= otop; index++) { 3171 for (index = 2; index <= otop; index++) {
3077 const char *opt = luaL_checkstring(L, index); 3172 const char *optname = luaL_checkstring(L, index);
3173 int optid;
3078 3174
3079 if (*opt == '{') { 3175 if (*optname == '{') {
3080 lua_newtable(L); 3176 lua_newtable(L);
3081 tindex = lua_gettop(L); 3177 tindex = lua_gettop(L);
3082 } else { 3178 } else {
3083 pk_pushparam(L, key, pk_checkparam(L, type, index)); 3179 optid = luaL_checkoption(L, index, NULL, optlist) + optoffset;
3180 pk_pushparam(L, base_key, optid);
3084 3181
3085 if (tindex) { 3182 if (tindex) {
3086 lua_setfield(L, tindex, opt); 3183 lua_setfield(L, tindex, optname);
3087 } 3184 }
3088 } 3185 }
3089 } 3186 }
@@ -3095,45 +3192,23 @@ sslerr:
3095 3192
3096 3193
3097static int pk_setParameters(lua_State *L) { 3194static int pk_setParameters(lua_State *L) {
3098 EVP_PKEY *_key = checksimple(L, 1, PKEY_CLASS); 3195 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3099 int type = EVP_PKEY_base_id(_key); 3196 int base_type = EVP_PKEY_base_id(key);
3100 void *key; 3197 void *base_key;
3101 const char *const *optlist; 3198 const char *const *optlist;
3102 int optindex, optoffset; 3199 int optindex, optoffset;
3103 3200
3104 luaL_checktype(L, 2, LUA_TTABLE); 3201 luaL_checktype(L, 2, LUA_TTABLE);
3105 3202
3106 if (!(key = EVP_PKEY_get0(_key))) 3203 if (!(base_key = EVP_PKEY_get0(key)))
3107 goto sslerr; 3204 goto sslerr;
3108 3205
3109 switch (type) { 3206 if (!(optlist = pk_getoptlist(base_type, NULL, &optoffset)))
3110 case EVP_PKEY_RSA: 3207 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type);
3111 optlist = pk_rsa_optlist;
3112 optoffset = PK_RSA_OPTOFFSET;
3113
3114 break;
3115 case EVP_PKEY_DSA:
3116 optlist = pk_dsa_optlist;
3117 optoffset = PK_DSA_OPTOFFSET;
3118
3119 break;
3120 case EVP_PKEY_DH:
3121 optlist = pk_dh_optlist;
3122 optoffset = PK_DH_OPTOFFSET;
3123
3124 break;
3125 case EVP_PKEY_EC:
3126 optlist = pk_ec_optlist;
3127 optoffset = PK_EC_OPTOFFSET;
3128
3129 break;
3130 default:
3131 return luaL_error(L, "%d: unsupported EVP_PKEY base type", type);
3132 }
3133 3208
3134 for (optindex = 0; optlist[optindex]; optindex++) { 3209 for (optindex = 0; optlist[optindex]; optindex++) {
3135 if (getfield(L, 2, optlist[optindex])) { 3210 if (getfield(L, 2, optlist[optindex])) {
3136 pk_setparam(L, key, optindex + optoffset, -1); 3211 pk_setparam(L, base_key, optindex + optoffset, -1);
3137 lua_pop(L, 1); 3212 lua_pop(L, 1);
3138 } 3213 }
3139 } 3214 }
@@ -3170,6 +3245,55 @@ static int pk__tostring(lua_State *L) {
3170} /* pk__tostring() */ 3245} /* pk__tostring() */
3171 3246
3172 3247
3248static int pk__index(lua_State *L) {
3249 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3250 void *base_key;
3251 const char *const *optlist;
3252 int optoffset, listoffset;
3253
3254 lua_pushvalue(L, lua_upvalueindex(1));
3255 lua_pushvalue(L, 2);
3256 lua_gettable(L, -2);
3257
3258 if (!lua_isnil(L, -1))
3259 return 1;
3260
3261 if (!lua_isstring(L, 2))
3262 return 0;
3263 if (!(base_key = EVP_PKEY_get0(key)))
3264 return 0;
3265 if (!(optlist = pk_getoptlist(EVP_PKEY_base_id(key), NULL, &optoffset)))
3266 return 0;
3267 if (-1 == (listoffset = auxL_testoption(L, 2, NULL, optlist, 0)))
3268 return 0;
3269
3270 pk_pushparam(L, base_key, listoffset + optoffset);
3271
3272 return 1;
3273} /* pk__index() */
3274
3275
3276static int pk__newindex(lua_State *L) {
3277 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3278 void *base_key;
3279 const char *const *optlist;
3280 int optoffset, listoffset;
3281
3282 if (!lua_isstring(L, 2))
3283 return 0;
3284 if (!(base_key = EVP_PKEY_get0(key)))
3285 return 0;
3286 if (!(optlist = pk_getoptlist(EVP_PKEY_base_id(key), NULL, &optoffset)))
3287 return 0;
3288 if (-1 == (listoffset = auxL_testoption(L, 2, NULL, optlist, 0)))
3289 return 0;
3290
3291 pk_setparam(L, base_key, listoffset + optoffset, 3);
3292
3293 return 0;
3294} /* pk__newindex() */
3295
3296
3173static int pk__gc(lua_State *L) { 3297static int pk__gc(lua_State *L) {
3174 EVP_PKEY **ud = luaL_checkudata(L, 1, PKEY_CLASS); 3298 EVP_PKEY **ud = luaL_checkudata(L, 1, PKEY_CLASS);
3175 3299
@@ -3182,7 +3306,7 @@ static int pk__gc(lua_State *L) {
3182} /* pk__gc() */ 3306} /* pk__gc() */
3183 3307
3184 3308
3185static const luaL_Reg pk_methods[] = { 3309static const auxL_Reg pk_methods[] = {
3186 { "type", &pk_type }, 3310 { "type", &pk_type },
3187 { "setPublicKey", &pk_setPublicKey }, 3311 { "setPublicKey", &pk_setPublicKey },
3188 { "setPrivateKey", &pk_setPrivateKey }, 3312 { "setPrivateKey", &pk_setPrivateKey },
@@ -3194,23 +3318,38 @@ static const luaL_Reg pk_methods[] = {
3194 { NULL, NULL }, 3318 { NULL, NULL },
3195}; 3319};
3196 3320
3197static const luaL_Reg pk_metatable[] = { 3321static const auxL_Reg pk_metatable[] = {
3198 { "__tostring", &pk__tostring }, 3322 { "__tostring", &pk__tostring },
3323 { "__index", &pk__index, 1 },
3324 { "__newindex", &pk__newindex, 1 },
3199 { "__gc", &pk__gc }, 3325 { "__gc", &pk__gc },
3200 { NULL, NULL }, 3326 { NULL, NULL },
3201}; 3327};
3202 3328
3203 3329
3204static const luaL_Reg pk_globals[] = { 3330static const auxL_Reg pk_globals[] = {
3205 { "new", &pk_new }, 3331 { "new", &pk_new },
3206 { "interpose", &pk_interpose }, 3332 { "interpose", &pk_interpose },
3207 { NULL, NULL }, 3333 { NULL, NULL },
3208}; 3334};
3209 3335
3336static void pk_luainit(lua_State *L, _Bool reset) {
3337 if (!auxL_newmetatable(L, PKEY_CLASS, reset))
3338 return;
3339 auxL_setfuncs(L, pk_metatable, 0);
3340 auxL_newlib(L, pk_methods, 0);
3341 for (char **k = (char *[]){ "__index", "__newindex", 0 }; *k; k++) {
3342 lua_getfield(L, -2, *k); /* closure */
3343 lua_pushvalue(L, -2); /* method table */
3344 lua_setupvalue(L, -2, 1);
3345 }
3346 lua_pop(L, 2);
3347} /* pk_luainit() */
3348
3210int luaopen__openssl_pkey(lua_State *L) { 3349int luaopen__openssl_pkey(lua_State *L) {
3211 initall(L); 3350 initall(L);
3212 3351
3213 luaL_newlib(L, pk_globals); 3352 auxL_newlib(L, pk_globals, 0);
3214 3353
3215 return 1; 3354 return 1;
3216} /* luaopen__openssl_pkey() */ 3355} /* luaopen__openssl_pkey() */
@@ -3391,18 +3530,18 @@ static int ecg__gc(lua_State *L) {
3391 return 0; 3530 return 0;
3392} /* ecg__gc() */ 3531} /* ecg__gc() */
3393 3532
3394static const luaL_Reg ecg_methods[] = { 3533static const auxL_Reg ecg_methods[] = {
3395 { "tostring", &ecg_tostring }, 3534 { "tostring", &ecg_tostring },
3396 { NULL, NULL }, 3535 { NULL, NULL },
3397}; 3536};
3398 3537
3399static const luaL_Reg ecg_metatable[] = { 3538static const auxL_Reg ecg_metatable[] = {
3400 { "__tostring", &ecg__tostring }, 3539 { "__tostring", &ecg__tostring },
3401 { "__gc", &ecg__gc }, 3540 { "__gc", &ecg__gc },
3402 { NULL, NULL }, 3541 { NULL, NULL },
3403}; 3542};
3404 3543
3405static const luaL_Reg ecg_globals[] = { 3544static const auxL_Reg ecg_globals[] = {
3406 { "new", &ecg_new }, 3545 { "new", &ecg_new },
3407 { "interpose", &ecg_interpose }, 3546 { "interpose", &ecg_interpose },
3408 { NULL, NULL }, 3547 { NULL, NULL },
@@ -3414,7 +3553,7 @@ int luaopen__openssl_ec_group(lua_State *L) {
3414#ifndef OPENSSL_NO_EC 3553#ifndef OPENSSL_NO_EC
3415 initall(L); 3554 initall(L);
3416 3555
3417 luaL_newlib(L, ecg_globals); 3556 auxL_newlib(L, ecg_globals, 0);
3418 3557
3419 return 1; 3558 return 1;
3420#else 3559#else
@@ -3597,13 +3736,13 @@ static int xn__tostring(lua_State *L) {
3597} /* xn__tostring() */ 3736} /* xn__tostring() */
3598 3737
3599 3738
3600static const luaL_Reg xn_methods[] = { 3739static const auxL_Reg xn_methods[] = {
3601 { "add", &xn_add }, 3740 { "add", &xn_add },
3602 { "all", &xn_all }, 3741 { "all", &xn_all },
3603 { NULL, NULL }, 3742 { NULL, NULL },
3604}; 3743};
3605 3744
3606static const luaL_Reg xn_metatable[] = { 3745static const auxL_Reg xn_metatable[] = {
3607 { "__pairs", &xn__pairs }, 3746 { "__pairs", &xn__pairs },
3608 { "__gc", &xn__gc }, 3747 { "__gc", &xn__gc },
3609 { "__tostring", &xn__tostring }, 3748 { "__tostring", &xn__tostring },
@@ -3611,7 +3750,7 @@ static const luaL_Reg xn_metatable[] = {
3611}; 3750};
3612 3751
3613 3752
3614static const luaL_Reg xn_globals[] = { 3753static const auxL_Reg xn_globals[] = {
3615 { "new", &xn_new }, 3754 { "new", &xn_new },
3616 { "interpose", &xn_interpose }, 3755 { "interpose", &xn_interpose },
3617 { NULL, NULL }, 3756 { NULL, NULL },
@@ -3620,7 +3759,7 @@ static const luaL_Reg xn_globals[] = {
3620int luaopen__openssl_x509_name(lua_State *L) { 3759int luaopen__openssl_x509_name(lua_State *L) {
3621 initall(L); 3760 initall(L);
3622 3761
3623 luaL_newlib(L, xn_globals); 3762 auxL_newlib(L, xn_globals, 0);
3624 3763
3625 return 1; 3764 return 1;
3626} /* luaopen__openssl_x509_name() */ 3765} /* luaopen__openssl_x509_name() */
@@ -3853,19 +3992,19 @@ static int gn__gc(lua_State *L) {
3853} /* gn__gc() */ 3992} /* gn__gc() */
3854 3993
3855 3994
3856static const luaL_Reg gn_methods[] = { 3995static const auxL_Reg gn_methods[] = {
3857 { "add", &gn_add }, 3996 { "add", &gn_add },
3858 { NULL, NULL }, 3997 { NULL, NULL },
3859}; 3998};
3860 3999
3861static const luaL_Reg gn_metatable[] = { 4000static const auxL_Reg gn_metatable[] = {
3862 { "__pairs", &gn__pairs }, 4001 { "__pairs", &gn__pairs },
3863 { "__gc", &gn__gc }, 4002 { "__gc", &gn__gc },
3864 { NULL, NULL }, 4003 { NULL, NULL },
3865}; 4004};
3866 4005
3867 4006
3868static const luaL_Reg gn_globals[] = { 4007static const auxL_Reg gn_globals[] = {
3869 { "new", &gn_new }, 4008 { "new", &gn_new },
3870 { "interpose", &gn_interpose }, 4009 { "interpose", &gn_interpose },
3871 { NULL, NULL }, 4010 { NULL, NULL },
@@ -3874,7 +4013,7 @@ static const luaL_Reg gn_globals[] = {
3874int luaopen__openssl_x509_altname(lua_State *L) { 4013int luaopen__openssl_x509_altname(lua_State *L) {
3875 initall(L); 4014 initall(L);
3876 4015
3877 luaL_newlib(L, gn_globals); 4016 auxL_newlib(L, gn_globals, 0);
3878 4017
3879 return 1; 4018 return 1;
3880} /* luaopen__openssl_x509_altname() */ 4019} /* luaopen__openssl_x509_altname() */
@@ -4077,7 +4216,7 @@ static int xe__gc(lua_State *L) {
4077} /* xe__gc() */ 4216} /* xe__gc() */
4078 4217
4079 4218
4080static const luaL_Reg xe_methods[] = { 4219static const auxL_Reg xe_methods[] = {
4081 { "getID", &xe_getID }, 4220 { "getID", &xe_getID },
4082 { "getName", &xe_getName }, 4221 { "getName", &xe_getName },
4083 { "getShortName", &xe_getShortName }, 4222 { "getShortName", &xe_getShortName },
@@ -4088,13 +4227,13 @@ static const luaL_Reg xe_methods[] = {
4088 { NULL, NULL }, 4227 { NULL, NULL },
4089}; 4228};
4090 4229
4091static const luaL_Reg xe_metatable[] = { 4230static const auxL_Reg xe_metatable[] = {
4092 { "__gc", &xe__gc }, 4231 { "__gc", &xe__gc },
4093 { NULL, NULL }, 4232 { NULL, NULL },
4094}; 4233};
4095 4234
4096 4235
4097static const luaL_Reg xe_globals[] = { 4236static const auxL_Reg xe_globals[] = {
4098 { "new", &xe_new }, 4237 { "new", &xe_new },
4099 { "interpose", &xe_interpose }, 4238 { "interpose", &xe_interpose },
4100 { NULL, NULL }, 4239 { NULL, NULL },
@@ -4111,7 +4250,7 @@ static const auxL_IntegerReg xe_textopts[] = {
4111int luaopen__openssl_x509_extension(lua_State *L) { 4250int luaopen__openssl_x509_extension(lua_State *L) {
4112 initall(L); 4251 initall(L);
4113 4252
4114 luaL_newlib(L, xe_globals); 4253 auxL_newlib(L, xe_globals, 0);
4115 auxL_setintegers(L, xe_textopts); 4254 auxL_setintegers(L, xe_textopts);
4116 4255
4117 return 1; 4256 return 1;
@@ -4683,7 +4822,7 @@ static int xc_getBasicConstraint(lua_State *L) {
4683 int n = 0, i, top; 4822 int n = 0, i, top;
4684 4823
4685 for (i = 2, top = lua_gettop(L); i <= top; i++) { 4824 for (i = 2, top = lua_gettop(L); i <= top; i++) {
4686 switch (checkoption(L, i, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL })) { 4825 switch (auxL_checkoption(L, i, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL }, 1)) {
4687 case 0: 4826 case 0:
4688 lua_pushboolean(L, CA); 4827 lua_pushboolean(L, CA);
4689 n++; 4828 n++;
@@ -4739,7 +4878,7 @@ static int xc_setBasicConstraint(lua_State *L) {
4739 } else { 4878 } else {
4740 lua_settop(L, 3); 4879 lua_settop(L, 3);
4741 4880
4742 switch (checkoption(L, 2, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL })) { 4881 switch (auxL_checkoption(L, 2, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL }, 1)) {
4743 case 0: 4882 case 0:
4744 luaL_checktype(L, 3, LUA_TBOOLEAN); 4883 luaL_checktype(L, 3, LUA_TBOOLEAN);
4745 CA = lua_toboolean(L, 3); 4884 CA = lua_toboolean(L, 3);
@@ -5080,7 +5219,7 @@ static int xc__gc(lua_State *L) {
5080} /* xc__gc() */ 5219} /* xc__gc() */
5081 5220
5082 5221
5083static const luaL_Reg xc_methods[] = { 5222static const auxL_Reg xc_methods[] = {
5084 { "getVersion", &xc_getVersion }, 5223 { "getVersion", &xc_getVersion },
5085 { "setVersion", &xc_setVersion }, 5224 { "setVersion", &xc_setVersion },
5086 { "getSerial", &xc_getSerial }, 5225 { "getSerial", &xc_getSerial },
@@ -5119,14 +5258,14 @@ static const luaL_Reg xc_methods[] = {
5119 { NULL, NULL }, 5258 { NULL, NULL },
5120}; 5259};
5121 5260
5122static const luaL_Reg xc_metatable[] = { 5261static const auxL_Reg xc_metatable[] = {
5123 { "__tostring", &xc__tostring }, 5262 { "__tostring", &xc__tostring },
5124 { "__gc", &xc__gc }, 5263 { "__gc", &xc__gc },
5125 { NULL, NULL }, 5264 { NULL, NULL },
5126}; 5265};
5127 5266
5128 5267
5129static const luaL_Reg xc_globals[] = { 5268static const auxL_Reg xc_globals[] = {
5130 { "new", &xc_new }, 5269 { "new", &xc_new },
5131 { "interpose", &xc_interpose }, 5270 { "interpose", &xc_interpose },
5132 { NULL, NULL }, 5271 { NULL, NULL },
@@ -5135,7 +5274,7 @@ static const luaL_Reg xc_globals[] = {
5135int luaopen__openssl_x509_cert(lua_State *L) { 5274int luaopen__openssl_x509_cert(lua_State *L) {
5136 initall(L); 5275 initall(L);
5137 5276
5138 luaL_newlib(L, xc_globals); 5277 auxL_newlib(L, xc_globals, 0);
5139 5278
5140 return 1; 5279 return 1;
5141} /* luaopen__openssl_x509_cert() */ 5280} /* luaopen__openssl_x509_cert() */
@@ -5315,7 +5454,7 @@ static int xr__gc(lua_State *L) {
5315 return 0; 5454 return 0;
5316} /* xr__gc() */ 5455} /* xr__gc() */
5317 5456
5318static const luaL_Reg xr_methods[] = { 5457static const auxL_Reg xr_methods[] = {
5319 { "getVersion", &xr_getVersion }, 5458 { "getVersion", &xr_getVersion },
5320 { "setVersion", &xr_setVersion }, 5459 { "setVersion", &xr_setVersion },
5321 { "getSubject", &xr_getSubject }, 5460 { "getSubject", &xr_getSubject },
@@ -5327,14 +5466,14 @@ static const luaL_Reg xr_methods[] = {
5327 { NULL, NULL }, 5466 { NULL, NULL },
5328}; 5467};
5329 5468
5330static const luaL_Reg xr_metatable[] = { 5469static const auxL_Reg xr_metatable[] = {
5331 { "__tostring", &xr__tostring }, 5470 { "__tostring", &xr__tostring },
5332 { "__gc", &xr__gc }, 5471 { "__gc", &xr__gc },
5333 { NULL, NULL }, 5472 { NULL, NULL },
5334}; 5473};
5335 5474
5336 5475
5337static const luaL_Reg xr_globals[] = { 5476static const auxL_Reg xr_globals[] = {
5338 { "new", &xr_new }, 5477 { "new", &xr_new },
5339 { "interpose", &xr_interpose }, 5478 { "interpose", &xr_interpose },
5340 { NULL, NULL }, 5479 { NULL, NULL },
@@ -5343,7 +5482,7 @@ static const luaL_Reg xr_globals[] = {
5343int luaopen__openssl_x509_csr(lua_State *L) { 5482int luaopen__openssl_x509_csr(lua_State *L) {
5344 initall(L); 5483 initall(L);
5345 5484
5346 luaL_newlib(L, xr_globals); 5485 auxL_newlib(L, xr_globals, 0);
5347 5486
5348 return 1; 5487 return 1;
5349} /* luaopen__openssl_x509_csr() */ 5488} /* luaopen__openssl_x509_csr() */
@@ -5706,7 +5845,7 @@ static int xx__gc(lua_State *L) {
5706 return 0; 5845 return 0;
5707} /* xx__gc() */ 5846} /* xx__gc() */
5708 5847
5709static const luaL_Reg xx_methods[] = { 5848static const auxL_Reg xx_methods[] = {
5710 { "getVersion", &xx_getVersion }, 5849 { "getVersion", &xx_getVersion },
5711 { "setVersion", &xx_setVersion }, 5850 { "setVersion", &xx_setVersion },
5712 { "getLastUpdate", &xx_getLastUpdate }, 5851 { "getLastUpdate", &xx_getLastUpdate },
@@ -5725,14 +5864,14 @@ static const luaL_Reg xx_methods[] = {
5725 { NULL, NULL }, 5864 { NULL, NULL },
5726}; 5865};
5727 5866
5728static const luaL_Reg xx_metatable[] = { 5867static const auxL_Reg xx_metatable[] = {
5729 { "__tostring", &xx__tostring }, 5868 { "__tostring", &xx__tostring },
5730 { "__gc", &xx__gc }, 5869 { "__gc", &xx__gc },
5731 { NULL, NULL }, 5870 { NULL, NULL },
5732}; 5871};
5733 5872
5734 5873
5735static const luaL_Reg xx_globals[] = { 5874static const auxL_Reg xx_globals[] = {
5736 { "new", &xx_new }, 5875 { "new", &xx_new },
5737 { "interpose", &xx_interpose }, 5876 { "interpose", &xx_interpose },
5738 { NULL, NULL }, 5877 { NULL, NULL },
@@ -5741,7 +5880,7 @@ static const luaL_Reg xx_globals[] = {
5741int luaopen__openssl_x509_crl(lua_State *L) { 5880int luaopen__openssl_x509_crl(lua_State *L) {
5742 initall(L); 5881 initall(L);
5743 5882
5744 luaL_newlib(L, xx_globals); 5883 auxL_newlib(L, xx_globals, 0);
5745 5884
5746 return 1; 5885 return 1;
5747} /* luaopen__openssl_x509_crl() */ 5886} /* luaopen__openssl_x509_crl() */
@@ -5878,19 +6017,19 @@ static int xl__gc(lua_State *L) {
5878} /* xl__gc() */ 6017} /* xl__gc() */
5879 6018
5880 6019
5881static const luaL_Reg xl_methods[] = { 6020static const auxL_Reg xl_methods[] = {
5882 { "add", &xl_add }, 6021 { "add", &xl_add },
5883 { NULL, NULL }, 6022 { NULL, NULL },
5884}; 6023};
5885 6024
5886static const luaL_Reg xl_metatable[] = { 6025static const auxL_Reg xl_metatable[] = {
5887 { "__pairs", &xl__pairs }, 6026 { "__pairs", &xl__pairs },
5888 { "__ipairs", &xl__pairs }, 6027 { "__ipairs", &xl__pairs },
5889 { "__gc", &xl__gc }, 6028 { "__gc", &xl__gc },
5890 { NULL, NULL }, 6029 { NULL, NULL },
5891}; 6030};
5892 6031
5893static const luaL_Reg xl_globals[] = { 6032static const auxL_Reg xl_globals[] = {
5894 { "new", &xl_new }, 6033 { "new", &xl_new },
5895 { "interpose", &xl_interpose }, 6034 { "interpose", &xl_interpose },
5896 { NULL, NULL }, 6035 { NULL, NULL },
@@ -5899,7 +6038,7 @@ static const luaL_Reg xl_globals[] = {
5899int luaopen__openssl_x509_chain(lua_State *L) { 6038int luaopen__openssl_x509_chain(lua_State *L) {
5900 initall(L); 6039 initall(L);
5901 6040
5902 luaL_newlib(L, xl_globals); 6041 auxL_newlib(L, xl_globals, 0);
5903 6042
5904 return 1; 6043 return 1;
5905} /* luaopen__openssl_x509_chain() */ 6044} /* luaopen__openssl_x509_chain() */
@@ -6043,18 +6182,18 @@ static int xs__gc(lua_State *L) {
6043} /* xs__gc() */ 6182} /* xs__gc() */
6044 6183
6045 6184
6046static const luaL_Reg xs_methods[] = { 6185static const auxL_Reg xs_methods[] = {
6047 { "add", &xs_add }, 6186 { "add", &xs_add },
6048 { "verify", &xs_verify }, 6187 { "verify", &xs_verify },
6049 { NULL, NULL }, 6188 { NULL, NULL },
6050}; 6189};
6051 6190
6052static const luaL_Reg xs_metatable[] = { 6191static const auxL_Reg xs_metatable[] = {
6053 { "__gc", &xs__gc }, 6192 { "__gc", &xs__gc },
6054 { NULL, NULL }, 6193 { NULL, NULL },
6055}; 6194};
6056 6195
6057static const luaL_Reg xs_globals[] = { 6196static const auxL_Reg xs_globals[] = {
6058 { "new", &xs_new }, 6197 { "new", &xs_new },
6059 { "interpose", &xs_interpose }, 6198 { "interpose", &xs_interpose },
6060 { NULL, NULL }, 6199 { NULL, NULL },
@@ -6063,7 +6202,7 @@ static const luaL_Reg xs_globals[] = {
6063int luaopen__openssl_x509_store(lua_State *L) { 6202int luaopen__openssl_x509_store(lua_State *L) {
6064 initall(L); 6203 initall(L);
6065 6204
6066 luaL_newlib(L, xs_globals); 6205 auxL_newlib(L, xs_globals, 0);
6067 6206
6068 return 1; 6207 return 1;
6069} /* luaopen__openssl_x509_store() */ 6208} /* luaopen__openssl_x509_store() */
@@ -6114,17 +6253,17 @@ static int stx__gc(lua_State *L) {
6114} /* stx__gc() */ 6253} /* stx__gc() */
6115 6254
6116 6255
6117static const luaL_Reg stx_methods[] = { 6256static const auxL_Reg stx_methods[] = {
6118 { "add", &stx_add }, 6257 { "add", &stx_add },
6119 { NULL, NULL }, 6258 { NULL, NULL },
6120}; 6259};
6121 6260
6122static const luaL_Reg stx_metatable[] = { 6261static const auxL_Reg stx_metatable[] = {
6123 { "__gc", &stx__gc }, 6262 { "__gc", &stx__gc },
6124 { NULL, NULL }, 6263 { NULL, NULL },
6125}; 6264};
6126 6265
6127static const luaL_Reg stx_globals[] = { 6266static const auxL_Reg stx_globals[] = {
6128 { "new", &stx_new }, 6267 { "new", &stx_new },
6129 { "interpose", &stx_interpose }, 6268 { "interpose", &stx_interpose },
6130 { NULL, NULL }, 6269 { NULL, NULL },
@@ -6133,7 +6272,7 @@ static const luaL_Reg stx_globals[] = {
6133int luaopen__openssl_x509_store_context(lua_State *L) { 6272int luaopen__openssl_x509_store_context(lua_State *L) {
6134 initall(L); 6273 initall(L);
6135 6274
6136 luaL_newlib(L, stx_globals); 6275 auxL_newlib(L, stx_globals, 0);
6137 6276
6138 return 1; 6277 return 1;
6139} /* luaopen__openssl_x509_store_context() */ 6278} /* luaopen__openssl_x509_store_context() */
@@ -6234,18 +6373,18 @@ static int p12__gc(lua_State *L) {
6234} /* p12__gc() */ 6373} /* p12__gc() */
6235 6374
6236 6375
6237static const luaL_Reg p12_methods[] = { 6376static const auxL_Reg p12_methods[] = {
6238 { "tostring", &p12__tostring }, 6377 { "tostring", &p12__tostring },
6239 { NULL, NULL }, 6378 { NULL, NULL },
6240}; 6379};
6241 6380
6242static const luaL_Reg p12_metatable[] = { 6381static const auxL_Reg p12_metatable[] = {
6243 { "__tostring", &p12__tostring }, 6382 { "__tostring", &p12__tostring },
6244 { "__gc", &p12__gc }, 6383 { "__gc", &p12__gc },
6245 { NULL, NULL }, 6384 { NULL, NULL },
6246}; 6385};
6247 6386
6248static const luaL_Reg p12_globals[] = { 6387static const auxL_Reg p12_globals[] = {
6249 { "new", &p12_new }, 6388 { "new", &p12_new },
6250 { "interpose", &p12_interpose }, 6389 { "interpose", &p12_interpose },
6251 { NULL, NULL }, 6390 { NULL, NULL },
@@ -6254,7 +6393,7 @@ static const luaL_Reg p12_globals[] = {
6254int luaopen__openssl_pkcs12(lua_State *L) { 6393int luaopen__openssl_pkcs12(lua_State *L) {
6255 initall(L); 6394 initall(L);
6256 6395
6257 luaL_newlib(L, p12_globals); 6396 auxL_newlib(L, p12_globals, 0);
6258 6397
6259 return 1; 6398 return 1;
6260} /* luaopen__openssl_pkcs12() */ 6399} /* luaopen__openssl_pkcs12() */
@@ -6294,7 +6433,7 @@ static int sx_new(lua_State *L) {
6294 lua_settop(L, 2); 6433 lua_settop(L, 2);
6295 srv = lua_toboolean(L, 2); 6434 srv = lua_toboolean(L, 2);
6296 6435
6297 switch (checkoption(L, 1, "TLS", opts)) { 6436 switch (auxL_checkoption(L, 1, "TLS", opts, 1)) {
6298 case 0: /* SSL */ 6437 case 0: /* SSL */
6299 method = (srv)? &SSLv23_server_method : &SSLv23_client_method; 6438 method = (srv)? &SSLv23_server_method : &SSLv23_client_method;
6300 options = SSL_OP_NO_SSLv2; 6439 options = SSL_OP_NO_SSLv2;
@@ -6350,7 +6489,7 @@ static int sx_new(lua_State *L) {
6350 break; 6489 break;
6351#endif 6490#endif
6352 default: 6491 default:
6353 return badoption(L, 1, NULL); 6492 return luaL_argerror(L, 1, "invalid option");
6354 } 6493 }
6355 6494
6356 ud = prepsimple(L, SSL_CTX_CLASS); 6495 ud = prepsimple(L, SSL_CTX_CLASS);
@@ -6671,7 +6810,7 @@ static int sx__gc(lua_State *L) {
6671} /* sx__gc() */ 6810} /* sx__gc() */
6672 6811
6673 6812
6674static const luaL_Reg sx_methods[] = { 6813static const auxL_Reg sx_methods[] = {
6675 { "setOptions", &sx_setOptions }, 6814 { "setOptions", &sx_setOptions },
6676 { "getOptions", &sx_getOptions }, 6815 { "getOptions", &sx_getOptions },
6677 { "clearOptions", &sx_clearOptions }, 6816 { "clearOptions", &sx_clearOptions },
@@ -6691,12 +6830,12 @@ static const luaL_Reg sx_methods[] = {
6691 { NULL, NULL }, 6830 { NULL, NULL },
6692}; 6831};
6693 6832
6694static const luaL_Reg sx_metatable[] = { 6833static const auxL_Reg sx_metatable[] = {
6695 { "__gc", &sx__gc }, 6834 { "__gc", &sx__gc },
6696 { NULL, NULL }, 6835 { NULL, NULL },
6697}; 6836};
6698 6837
6699static const luaL_Reg sx_globals[] = { 6838static const auxL_Reg sx_globals[] = {
6700 { "new", &sx_new }, 6839 { "new", &sx_new },
6701 { "interpose", &sx_interpose }, 6840 { "interpose", &sx_interpose },
6702 { NULL, NULL }, 6841 { NULL, NULL },
@@ -6759,7 +6898,7 @@ static const auxL_IntegerReg sx_option[] = {
6759int luaopen__openssl_ssl_context(lua_State *L) { 6898int luaopen__openssl_ssl_context(lua_State *L) {
6760 initall(L); 6899 initall(L);
6761 6900
6762 luaL_newlib(L, sx_globals); 6901 auxL_newlib(L, sx_globals, 0);
6763 auxL_setintegers(L, sx_verify); 6902 auxL_setintegers(L, sx_verify);
6764 auxL_setintegers(L, sx_option); 6903 auxL_setintegers(L, sx_option);
6765 6904
@@ -7005,7 +7144,7 @@ static int ssl__gc(lua_State *L) {
7005} /* ssl__gc() */ 7144} /* ssl__gc() */
7006 7145
7007 7146
7008static const luaL_Reg ssl_methods[] = { 7147static const auxL_Reg ssl_methods[] = {
7009 { "setOptions", &ssl_setOptions }, 7148 { "setOptions", &ssl_setOptions },
7010 { "getOptions", &ssl_getOptions }, 7149 { "getOptions", &ssl_getOptions },
7011 { "clearOptions", &ssl_clearOptions }, 7150 { "clearOptions", &ssl_clearOptions },
@@ -7025,12 +7164,12 @@ static const luaL_Reg ssl_methods[] = {
7025 { NULL, NULL }, 7164 { NULL, NULL },
7026}; 7165};
7027 7166
7028static const luaL_Reg ssl_metatable[] = { 7167static const auxL_Reg ssl_metatable[] = {
7029 { "__gc", &ssl__gc }, 7168 { "__gc", &ssl__gc },
7030 { NULL, NULL }, 7169 { NULL, NULL },
7031}; 7170};
7032 7171
7033static const luaL_Reg ssl_globals[] = { 7172static const auxL_Reg ssl_globals[] = {
7034 { "new", &ssl_new }, 7173 { "new", &ssl_new },
7035 { "interpose", &ssl_interpose }, 7174 { "interpose", &ssl_interpose },
7036 { NULL, NULL }, 7175 { NULL, NULL },
@@ -7053,7 +7192,7 @@ static const auxL_IntegerReg ssl_version[] = {
7053int luaopen__openssl_ssl(lua_State *L) { 7192int luaopen__openssl_ssl(lua_State *L) {
7054 initall(L); 7193 initall(L);
7055 7194
7056 luaL_newlib(L, ssl_globals); 7195 auxL_newlib(L, ssl_globals, 0);
7057 auxL_setintegers(L, ssl_version); 7196 auxL_setintegers(L, ssl_version);
7058 auxL_setintegers(L, sx_verify); 7197 auxL_setintegers(L, sx_verify);
7059 auxL_setintegers(L, sx_option); 7198 auxL_setintegers(L, sx_option);
@@ -7149,18 +7288,18 @@ static int md__gc(lua_State *L) {
7149} /* md__gc() */ 7288} /* md__gc() */
7150 7289
7151 7290
7152static const luaL_Reg md_methods[] = { 7291static const auxL_Reg md_methods[] = {
7153 { "update", &md_update }, 7292 { "update", &md_update },
7154 { "final", &md_final }, 7293 { "final", &md_final },
7155 { NULL, NULL }, 7294 { NULL, NULL },
7156}; 7295};
7157 7296
7158static const luaL_Reg md_metatable[] = { 7297static const auxL_Reg md_metatable[] = {
7159 { "__gc", &md__gc }, 7298 { "__gc", &md__gc },
7160 { NULL, NULL }, 7299 { NULL, NULL },
7161}; 7300};
7162 7301
7163static const luaL_Reg md_globals[] = { 7302static const auxL_Reg md_globals[] = {
7164 { "new", &md_new }, 7303 { "new", &md_new },
7165 { "interpose", &md_interpose }, 7304 { "interpose", &md_interpose },
7166 { NULL, NULL }, 7305 { NULL, NULL },
@@ -7169,7 +7308,7 @@ static const luaL_Reg md_globals[] = {
7169int luaopen__openssl_digest(lua_State *L) { 7308int luaopen__openssl_digest(lua_State *L) {
7170 initall(L); 7309 initall(L);
7171 7310
7172 luaL_newlib(L, md_globals); 7311 auxL_newlib(L, md_globals, 0);
7173 7312
7174 return 1; 7313 return 1;
7175} /* luaopen__openssl_digest() */ 7314} /* luaopen__openssl_digest() */
@@ -7251,18 +7390,18 @@ static int hmac__gc(lua_State *L) {
7251} /* hmac__gc() */ 7390} /* hmac__gc() */
7252 7391
7253 7392
7254static const luaL_Reg hmac_methods[] = { 7393static const auxL_Reg hmac_methods[] = {
7255 { "update", &hmac_update }, 7394 { "update", &hmac_update },
7256 { "final", &hmac_final }, 7395 { "final", &hmac_final },
7257 { NULL, NULL }, 7396 { NULL, NULL },
7258}; 7397};
7259 7398
7260static const luaL_Reg hmac_metatable[] = { 7399static const auxL_Reg hmac_metatable[] = {
7261 { "__gc", &hmac__gc }, 7400 { "__gc", &hmac__gc },
7262 { NULL, NULL }, 7401 { NULL, NULL },
7263}; 7402};
7264 7403
7265static const luaL_Reg hmac_globals[] = { 7404static const auxL_Reg hmac_globals[] = {
7266 { "new", &hmac_new }, 7405 { "new", &hmac_new },
7267 { "interpose", &hmac_interpose }, 7406 { "interpose", &hmac_interpose },
7268 { NULL, NULL }, 7407 { NULL, NULL },
@@ -7271,7 +7410,7 @@ static const luaL_Reg hmac_globals[] = {
7271int luaopen__openssl_hmac(lua_State *L) { 7410int luaopen__openssl_hmac(lua_State *L) {
7272 initall(L); 7411 initall(L);
7273 7412
7274 luaL_newlib(L, hmac_globals); 7413 auxL_newlib(L, hmac_globals, 0);
7275 7414
7276 return 1; 7415 return 1;
7277} /* luaopen__openssl_hmac() */ 7416} /* luaopen__openssl_hmac() */
@@ -7452,7 +7591,7 @@ static int cipher__gc(lua_State *L) {
7452} /* cipher__gc() */ 7591} /* cipher__gc() */
7453 7592
7454 7593
7455static const luaL_Reg cipher_methods[] = { 7594static const auxL_Reg cipher_methods[] = {
7456 { "encrypt", &cipher_encrypt }, 7595 { "encrypt", &cipher_encrypt },
7457 { "decrypt", &cipher_decrypt }, 7596 { "decrypt", &cipher_decrypt },
7458 { "update", &cipher_update }, 7597 { "update", &cipher_update },
@@ -7460,12 +7599,12 @@ static const luaL_Reg cipher_methods[] = {
7460 { NULL, NULL }, 7599 { NULL, NULL },
7461}; 7600};
7462 7601
7463static const luaL_Reg cipher_metatable[] = { 7602static const auxL_Reg cipher_metatable[] = {
7464 { "__gc", &cipher__gc }, 7603 { "__gc", &cipher__gc },
7465 { NULL, NULL }, 7604 { NULL, NULL },
7466}; 7605};
7467 7606
7468static const luaL_Reg cipher_globals[] = { 7607static const auxL_Reg cipher_globals[] = {
7469 { "new", &cipher_new }, 7608 { "new", &cipher_new },
7470 { "interpose", &cipher_interpose }, 7609 { "interpose", &cipher_interpose },
7471 { NULL, NULL }, 7610 { NULL, NULL },
@@ -7474,7 +7613,7 @@ static const luaL_Reg cipher_globals[] = {
7474int luaopen__openssl_cipher(lua_State *L) { 7613int luaopen__openssl_cipher(lua_State *L) {
7475 initall(L); 7614 initall(L);
7476 7615
7477 luaL_newlib(L, cipher_globals); 7616 auxL_newlib(L, cipher_globals, 0);
7478 7617
7479 return 1; 7618 return 1;
7480} /* luaopen__openssl_cipher() */ 7619} /* luaopen__openssl_cipher() */
@@ -7803,7 +7942,7 @@ static int rand_uniform(lua_State *L) {
7803} /* rand_uniform() */ 7942} /* rand_uniform() */
7804 7943
7805 7944
7806static const luaL_Reg rand_globals[] = { 7945static const auxL_Reg rand_globals[] = {
7807 { "stir", &rand_stir }, 7946 { "stir", &rand_stir },
7808 { "add", &rand_add }, 7947 { "add", &rand_add },
7809 { "bytes", &rand_bytes }, 7948 { "bytes", &rand_bytes },
@@ -7817,10 +7956,9 @@ int luaopen__openssl_rand(lua_State *L) {
7817 7956
7818 initall(L); 7957 initall(L);
7819 7958
7820 luaL_newlibtable(L, rand_globals);
7821 st = lua_newuserdata(L, sizeof *st); 7959 st = lua_newuserdata(L, sizeof *st);
7822 memset(st, 0, sizeof *st); 7960 memset(st, 0, sizeof *st);
7823 luaL_setfuncs(L, rand_globals, 1); 7961 auxL_newlib(L, rand_globals, 1);
7824 7962
7825 return 1; 7963 return 1;
7826} /* luaopen__openssl_rand() */ 7964} /* luaopen__openssl_rand() */
@@ -7855,7 +7993,7 @@ static int de5_set_odd_parity(lua_State *L) {
7855 return 1; 7993 return 1;
7856} /* de5_set_odd_parity() */ 7994} /* de5_set_odd_parity() */
7857 7995
7858static const luaL_Reg des_globals[] = { 7996static const auxL_Reg des_globals[] = {
7859 { "string_to_key", &de5_string_to_key }, 7997 { "string_to_key", &de5_string_to_key },
7860 { "set_odd_parity", &de5_set_odd_parity }, 7998 { "set_odd_parity", &de5_set_odd_parity },
7861 { NULL, NULL }, 7999 { NULL, NULL },
@@ -7864,7 +8002,7 @@ static const luaL_Reg des_globals[] = {
7864int luaopen__openssl_des(lua_State *L) { 8002int luaopen__openssl_des(lua_State *L) {
7865 initall(L); 8003 initall(L);
7866 8004
7867 luaL_newlib(L, des_globals); 8005 auxL_newlib(L, des_globals, 0);
7868 8006
7869 return 1; 8007 return 1;
7870} /* luaopen__openssl_des() */ 8008} /* luaopen__openssl_des() */
@@ -8009,24 +8147,24 @@ static void initall(lua_State *L) {
8009 8147
8010 ex_newstate(L); 8148 ex_newstate(L);
8011 8149
8012 addclass(L, BIGNUM_CLASS, bn_methods, bn_metatable); 8150 auxL_addclass(L, BIGNUM_CLASS, bn_methods, bn_metatable, 0);
8013 addclass(L, PKEY_CLASS, pk_methods, pk_metatable); 8151 pk_luainit(L, 0);
8014#ifndef OPENSSL_NO_EC 8152#ifndef OPENSSL_NO_EC
8015 addclass(L, EC_GROUP_CLASS, ecg_methods, ecg_metatable); 8153 auxL_addclass(L, EC_GROUP_CLASS, ecg_methods, ecg_metatable, 0);
8016#endif 8154#endif
8017 addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable); 8155 auxL_addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable, 0);
8018 addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable); 8156 auxL_addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable, 0);
8019 addclass(L, X509_EXT_CLASS, xe_methods, xe_metatable); 8157 auxL_addclass(L, X509_EXT_CLASS, xe_methods, xe_metatable, 0);
8020 addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable); 8158 auxL_addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable, 0);
8021 addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable); 8159 auxL_addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable, 0);
8022 addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); 8160 auxL_addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable, 0);
8023 addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable); 8161 auxL_addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable, 0);
8024 addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable); 8162 auxL_addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable, 0);
8025 addclass(L, PKCS12_CLASS, p12_methods, p12_metatable); 8163 auxL_addclass(L, PKCS12_CLASS, p12_methods, p12_metatable, 0);
8026 addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable); 8164 auxL_addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable, 0);
8027 addclass(L, SSL_CLASS, ssl_methods, ssl_metatable); 8165 auxL_addclass(L, SSL_CLASS, ssl_methods, ssl_metatable, 0);
8028 addclass(L, DIGEST_CLASS, md_methods, md_metatable); 8166 auxL_addclass(L, DIGEST_CLASS, md_methods, md_metatable, 0);
8029 addclass(L, HMAC_CLASS, hmac_methods, hmac_metatable); 8167 auxL_addclass(L, HMAC_CLASS, hmac_methods, hmac_metatable, 0);
8030 addclass(L, CIPHER_CLASS, cipher_methods, cipher_metatable); 8168 auxL_addclass(L, CIPHER_CLASS, cipher_methods, cipher_metatable, 0);
8031} /* initall() */ 8169} /* initall() */
8032 8170