summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c1077
1 files changed, 851 insertions, 226 deletions
diff --git a/src/openssl.c b/src/openssl.c
index dba7c75..11d02a0 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -148,6 +148,7 @@
148 148
149#define BIGNUM_CLASS "BIGNUM*" 149#define BIGNUM_CLASS "BIGNUM*"
150#define PKEY_CLASS "EVP_PKEY*" 150#define PKEY_CLASS "EVP_PKEY*"
151#define EC_GROUP_CLASS "EVP_GROUP*"
151#define X509_NAME_CLASS "X509_NAME*" 152#define X509_NAME_CLASS "X509_NAME*"
152#define X509_GENS_CLASS "GENERAL_NAMES*" 153#define X509_GENS_CLASS "GENERAL_NAMES*"
153#define X509_EXT_CLASS "X509_EXTENSION*" 154#define X509_EXT_CLASS "X509_EXTENSION*"
@@ -284,65 +285,30 @@ static void *testsimple(lua_State *L, int index, const char *tname) {
284} /* testsimple() */ 285} /* testsimple() */
285 286
286 287
287static int interpose(lua_State *L, const char *mt) { 288static int auxL_swapmetatable(lua_State *, const char *);
288 luaL_getmetatable(L, mt); 289static int auxL_swapmetasubtable(lua_State *, const char *, const char *);
289
290 if (!strncmp("__", luaL_checkstring(L, 1), 2))
291 lua_pushvalue(L, -1);
292 else
293 lua_getfield(L, -1, "__index");
294
295 lua_pushvalue(L, -4); /* push method name */
296 lua_gettable(L, -2); /* push old method */
297
298 lua_pushvalue(L, -5); /* push method name */
299 lua_pushvalue(L, -5); /* push new method */
300 lua_settable(L, -4); /* replace old method */
301 290
302 return 1; /* return old method */ 291static int interpose(lua_State *L, const char *mt) {
303} /* interpose() */ 292 if (!strncmp("__", luaL_checkstring(L, lua_absindex(L, -2)), 2)) {
304 293 return auxL_swapmetatable(L, mt);
305 294 } else {
306static void addclass(lua_State *L, const char *name, const luaL_Reg *methods, const luaL_Reg *metamethods) { 295 return auxL_swapmetasubtable(L, mt, "__index");
307 if (luaL_newmetatable(L, name)) {
308 luaL_setfuncs(L, metamethods, 0);
309 lua_newtable(L);
310 luaL_setfuncs(L, methods, 0);
311 lua_setfield(L, -2, "__index");
312 lua_pop(L, 1);
313 }
314} /* addclass() */
315
316
317static int badoption(lua_State *L, int index, const char *opt) {
318 opt = (opt)? opt : luaL_checkstring(L, index);
319
320 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option %s", opt));
321} /* badoption() */
322
323static int checkoption(lua_State *L, int index, const char *def, const char *const opts[]) {
324 const char *opt = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
325 int i;
326
327 for (i = 0; opts[i]; i++) {
328 if (strieq(opts[i], opt))
329 return i;
330 } 296 }
297} /* interpose() */
331 298
332 return badoption(L, index, opt); 299static int auxL_checkoption(lua_State *, int, const char *, const char *const *, _Bool);
333} /* checkoption() */
334
335 300
336#define X509_ANY 0x01 301#define X509_ANY 0x01
337#define X509_PEM 0x02 302#define X509_PEM 0x02
338#define X509_DER 0x04 303#define X509_DER 0x04
304#define X509_TXT 0x08 /* "pretty" */
339#define X509_ALL (X509_PEM|X509_DER) 305#define X509_ALL (X509_PEM|X509_DER)
340 306
341static int optencoding(lua_State *L, int index, const char *def, int allow) { 307static int optencoding(lua_State *L, int index, const char *def, int allow) {
342 static const char *const opts[] = { "*", "pem", "der", NULL }; 308 static const char *const opts[] = { "*", "pem", "der", "pretty", NULL };
343 int type = 0; 309 int type = 0;
344 310
345 switch (checkoption(L, index, def, opts)) { 311 switch (auxL_checkoption(L, index, def, opts, 1)) {
346 case 0: 312 case 0:
347 type = X509_ANY; 313 type = X509_ANY;
348 break; 314 break;
@@ -352,6 +318,9 @@ static int optencoding(lua_State *L, int index, const char *def, int allow) {
352 case 2: 318 case 2:
353 type = X509_DER; 319 type = X509_DER;
354 break; 320 break;
321 case 3:
322 type = X509_TXT;
323 break;
355 } 324 }
356 325
357 if (!(type & allow)) 326 if (!(type & allow))
@@ -608,6 +577,21 @@ static _Bool auxS_txt2obj(ASN1_OBJECT **obj, const char *txt) {
608 } 577 }
609} /* auxS_txt2obj() */ 578} /* auxS_txt2obj() */
610 579
580static _Bool auxS_txt2nid(int *nid, const char *txt) {
581 /* try builtins first */
582 if ((*nid = OBJ_sn2nid(txt)) != NID_undef
583 || (*nid = OBJ_ln2nid(txt)) != NID_undef) {
584 return 1;
585 }
586
587 /* OBJ_txt2nid creates a temporary ASN1_OBJECT; call sparingly */
588 if (auxS_isoid(txt) && (*nid = OBJ_txt2nid(txt)) != NID_undef) {
589 return 1;
590 }
591
592 return 0;
593} /* auxS_txt2nid() */
594
611 595
612/* 596/*
613 * Auxiliary Lua API routines 597 * Auxiliary Lua API routines
@@ -638,6 +622,27 @@ NOTUSED static auxtype_t auxL_getref(lua_State *L, auxref_t ref) {
638 return lua_type(L, -1); 622 return lua_type(L, -1);
639} /* auxL_getref() */ 623} /* auxL_getref() */
640 624
625static int auxL_testoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) {
626 const char *optname = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
627 int (*optcmp)() = (nocase)? &strcasecmp : &strcmp;
628
629 for (int i = 0; optlist[i]; i++) {
630 if (0 == optcmp(optlist[i], optname))
631 return i;
632 }
633
634 return -1;
635} /* auxL_testoption() */
636
637static int auxL_checkoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) {
638 int i;
639
640 if ((i = auxL_testoption(L, index, def, optlist, nocase)) >= 0)
641 return i;
642
643 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option '%s'", luaL_optstring(L, index, def)));
644} /* auxL_checkoption() */
645
641/* 646/*
642 * Lua 5.3 distinguishes integers and numbers, and by default uses 64-bit 647 * Lua 5.3 distinguishes integers and numbers, and by default uses 64-bit
643 * integers. The following routines try to preserve this distinction and 648 * integers. The following routines try to preserve this distinction and
@@ -733,6 +738,13 @@ static auxL_Unsigned (auxL_optunsigned)(lua_State *L, int index, auxL_Unsigned d
733 return (lua_isnoneornil(L, index))? def : auxL_checkunsigned(L, index, min, max); 738 return (lua_isnoneornil(L, index))? def : auxL_checkunsigned(L, index, min, max);
734} /* auxL_optunsigned() */ 739} /* auxL_optunsigned() */
735 740
741static int auxL_size2int(lua_State *L, size_t n) {
742 if (n > INT_MAX)
743 luaL_error(L, "integer value out of range (%zu > INT_MAX)", n);
744
745 return (int)n;
746} /* auxL_size2int() */
747
736typedef struct { 748typedef struct {
737 const char *name; 749 const char *name;
738 auxL_Integer value; 750 auxL_Integer value;
@@ -745,6 +757,148 @@ static void auxL_setintegers(lua_State *L, const auxL_IntegerReg *l) {
745 } 757 }
746} /* auxL_setintegers() */ 758} /* auxL_setintegers() */
747 759
760#define AUXL_REG_NULL (&(auxL_Reg[]){ 0 })
761
762typedef struct {
763 const char *name;
764 lua_CFunction func;
765 unsigned nups; /* in addition to nups specified to auxL_setfuncs */
766} auxL_Reg;
767
768static inline size_t auxL_liblen(const auxL_Reg *l) {
769 size_t n = 0;
770
771 while ((l++)->name)
772 n++;
773
774 return n;
775} /* auxL_liblen() */
776
777#define auxL_newlibtable(L, l) \
778 lua_createtable((L), 0, countof((l)) - 1)
779
780#define auxL_newlib(L, l, nups) \
781 (auxL_newlibtable((L), (l)), lua_insert((L), -(nups + 1)), auxL_setfuncs((L), (l), (nups)))
782
783static void auxL_setfuncs(lua_State *L, const auxL_Reg *l, int nups) {
784 for (; l->name; l++) {
785 /* copy shared upvalues */
786 luaL_checkstack(L, nups, "too many upvalues");
787 for (int i = 0; i < nups; i++)
788 lua_pushvalue(L, -nups);
789
790 /* nil-fill local upvalues */
791 luaL_checkstack(L, l->nups, "too many upvalues");
792 lua_settop(L, lua_gettop(L) + l->nups);
793
794 /* set closure */
795 luaL_checkstack(L, 1, "too many upvalues");
796 lua_pushcclosure(L, l->func, nups + l->nups);
797 lua_setfield(L, -(nups + 2), l->name);
798 }
799
800 lua_pop(L, nups);
801
802 return;
803} /* auxL_setfuncs() */
804
805static void auxL_clear(lua_State *L, int tindex) {
806 tindex = lua_absindex(L, tindex);
807
808 lua_pushnil(L);
809 while (lua_next(L, tindex)) {
810 lua_pop(L, 1);
811 lua_pushvalue(L, -1);
812 lua_pushnil(L);
813 lua_rawset(L, tindex);
814 }
815} /* auxL_clear() */
816
817static _Bool auxL_newmetatable(lua_State *L, const char *name, _Bool reset) {
818 if (luaL_newmetatable(L, name))
819 return 1;
820 if (!reset)
821 return 0;
822
823 /*
824 * NB: Keep existing table as it may be cached--e.g. in
825 * another module that isn't being reloaded. But scrub it
826 * clean so function interposition--which will presumably
827 * run again if the C module is being reloaded--doesn't
828 * result in loops.
829 */
830 auxL_clear(L, -1);
831 lua_pushnil(L);
832 lua_setmetatable(L, -2);
833#if LUA_VERSION_NUM >= 502
834 lua_pushnil(L);
835 lua_setuservalue(L, -2);
836#endif
837
838 return 0;
839} /* auxL_newmetatable() */
840
841static _Bool auxL_newclass(lua_State *L, const char *name, const auxL_Reg *methods, const auxL_Reg *metamethods, _Bool reset) {
842 _Bool fresh = auxL_newmetatable(L, name, reset);
843 int n;
844
845 auxL_setfuncs(L, metamethods, 0);
846
847 if ((n = auxL_liblen(methods))) {
848 lua_createtable(L, 0, auxL_size2int(L, n));
849 auxL_setfuncs(L, methods, 0);
850 lua_setfield(L, -2, "__index");
851 }
852
853 return fresh;
854} /* auxL_newclass() */
855
856#define auxL_addclass(L, ...) \
857 (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1))
858
859static 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
875static 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
888static 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
748#define auxL_EDYLD -2 902#define auxL_EDYLD -2
749#define auxL_EOPENSSL -1 903#define auxL_EOPENSSL -1
750 904
@@ -1348,7 +1502,7 @@ static int ossl_version(lua_State *L) {
1348 return 1; 1502 return 1;
1349} /* ossl_version() */ 1503} /* ossl_version() */
1350 1504
1351static const luaL_Reg ossl_globals[] = { 1505static const auxL_Reg ossl_globals[] = {
1352 { "version", &ossl_version }, 1506 { "version", &ossl_version },
1353 { NULL, NULL }, 1507 { NULL, NULL },
1354}; 1508};
@@ -1520,7 +1674,7 @@ static const auxL_IntegerReg ssleay_version[] = {
1520int luaopen__openssl(lua_State *L) { 1674int luaopen__openssl(lua_State *L) {
1521 size_t i; 1675 size_t i;
1522 1676
1523 luaL_newlib(L, ossl_globals); 1677 auxL_newlib(L, ossl_globals, 0);
1524 1678
1525 for (i = 0; i < countof(opensslconf_no); i++) { 1679 for (i = 0; i < countof(opensslconf_no); i++) {
1526 if (*opensslconf_no[i]) { 1680 if (*opensslconf_no[i]) {
@@ -1577,6 +1731,11 @@ static BIGNUM *bn_dup(lua_State *L, const BIGNUM *src) {
1577} /* bn_dup() */ 1731} /* bn_dup() */
1578 1732
1579 1733
1734static BIGNUM *bn_dup_nil(lua_State *L, const BIGNUM *src) {
1735 return (src)? bn_dup(L, src) : (lua_pushnil(L), (BIGNUM *)0);
1736} /* bn_dup_nil() */
1737
1738
1580#define checkbig_(a, b, c, ...) checkbig((a), (b), (c)) 1739#define checkbig_(a, b, c, ...) checkbig((a), (b), (c))
1581#define checkbig(...) checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0) 1740#define checkbig(...) checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0)
1582 1741
@@ -2010,7 +2169,7 @@ static int bn__gc(lua_State *L) {
2010 BIGNUM **ud = luaL_checkudata(L, 1, BIGNUM_CLASS); 2169 BIGNUM **ud = luaL_checkudata(L, 1, BIGNUM_CLASS);
2011 2170
2012 if (*ud) { 2171 if (*ud) {
2013 BN_free(*ud); 2172 BN_clear_free(*ud);
2014 *ud = NULL; 2173 *ud = NULL;
2015 } 2174 }
2016 2175
@@ -2106,7 +2265,7 @@ sslerr:
2106} /* bn_tohex() */ 2265} /* bn_tohex() */
2107 2266
2108 2267
2109static const luaL_Reg bn_methods[] = { 2268static const auxL_Reg bn_methods[] = {
2110 { "add", &bn__add }, 2269 { "add", &bn__add },
2111 { "sub", &bn__sub }, 2270 { "sub", &bn__sub },
2112 { "mul", &bn__mul }, 2271 { "mul", &bn__mul },
@@ -2125,7 +2284,7 @@ static const luaL_Reg bn_methods[] = {
2125 { NULL, NULL }, 2284 { NULL, NULL },
2126}; 2285};
2127 2286
2128static const luaL_Reg bn_metatable[] = { 2287static const auxL_Reg bn_metatable[] = {
2129 { "__add", &bn__add }, 2288 { "__add", &bn__add },
2130 { "__sub", &bn__sub }, 2289 { "__sub", &bn__sub },
2131 { "__mul", &bn__mul }, 2290 { "__mul", &bn__mul },
@@ -2145,7 +2304,7 @@ static const luaL_Reg bn_metatable[] = {
2145}; 2304};
2146 2305
2147 2306
2148static const luaL_Reg bn_globals[] = { 2307static const auxL_Reg bn_globals[] = {
2149 { "new", &bn_new }, 2308 { "new", &bn_new },
2150 { "interpose", &bn_interpose }, 2309 { "interpose", &bn_interpose },
2151 { "fromBinary", &bn_fromBinary }, 2310 { "fromBinary", &bn_fromBinary },
@@ -2156,7 +2315,7 @@ static const luaL_Reg bn_globals[] = {
2156int luaopen__openssl_bignum(lua_State *L) { 2315int luaopen__openssl_bignum(lua_State *L) {
2157 initall(L); 2316 initall(L);
2158 2317
2159 luaL_newlib(L, bn_globals); 2318 auxL_newlib(L, bn_globals, 0);
2160 2319
2161 return 1; 2320 return 1;
2162} /* luaopen__openssl_bignum() */ 2321} /* luaopen__openssl_bignum() */
@@ -2257,8 +2416,8 @@ static int pk_new(lua_State *L) {
2257 } 2416 }
2258 2417
2259 if (loadfield(L, 1, "curve", LUA_TSTRING, &id)) { 2418 if (loadfield(L, 1, "curve", LUA_TSTRING, &id)) {
2260 curve = OBJ_sn2nid(id); 2419 if (!auxS_txt2nid(&curve, id))
2261 luaL_argcheck(L, curve != NID_undef, 1, lua_pushfstring(L, "%s: invalid curve", id)); 2420 luaL_argerror(L, 1, lua_pushfstring(L, "%s: invalid curve", id));
2262 } 2421 }
2263 2422
2264creat: 2423creat:
@@ -2463,7 +2622,19 @@ done:
2463 2622
2464 2623
2465static int pk_interpose(lua_State *L) { 2624static int pk_interpose(lua_State *L) {
2466 return interpose(L, PKEY_CLASS); 2625 lua_settop(L, 2);
2626
2627 luaL_getmetatable(L, PKEY_CLASS);
2628 if (!strncmp("__", luaL_checkstring(L, 1), 2)) {
2629 lua_insert(L, 1);
2630 } else {
2631 lua_getfield(L, -1, "__index");
2632 lua_getupvalue(L, -1, 1);
2633 lua_insert(L, 1);
2634 lua_pop(L, 2);
2635 }
2636
2637 return auxL_swaptable(L, 1);
2467} /* pk_interpose() */ 2638} /* pk_interpose() */
2468 2639
2469 2640
@@ -2609,7 +2780,7 @@ static int pk_toPEM(lua_State *L) {
2609 NULL, 2780 NULL,
2610 }; 2781 };
2611 2782
2612 switch (checkoption(L, i, NULL, opts)) { 2783 switch (auxL_checkoption(L, i, NULL, opts, 1)) {
2613 case 0: case 1: /* public, PublicKey */ 2784 case 0: case 1: /* public, PublicKey */
2614 if (!PEM_write_bio_PUBKEY(bio, key)) 2785 if (!PEM_write_bio_PUBKEY(bio, key))
2615 return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring"); 2786 return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");
@@ -2721,8 +2892,14 @@ enum pk_param {
2721 PK_DH_PUB_KEY, 2892 PK_DH_PUB_KEY,
2722 PK_DH_PRIV_KEY, 2893 PK_DH_PRIV_KEY,
2723 2894
2724#define PK_EC_OPTLIST { "pub_key", "priv_key", NULL } 2895/*
2725#define PK_EC_OPTOFFSET PK_EC_PUB_KEY 2896 * NB: group MUST come before pub_key as setting pub_key requires the group
2897 * to be defined. :setParameters will do the requested assignments in the
2898 * order defined by this array.
2899 */
2900#define PK_EC_OPTLIST { "group", "pub_key", "priv_key", NULL }
2901#define PK_EC_OPTOFFSET PK_EC_GROUP
2902 PK_EC_GROUP,
2726 PK_EC_PUB_KEY, 2903 PK_EC_PUB_KEY,
2727 PK_EC_PRIV_KEY, 2904 PK_EC_PRIV_KEY,
2728}; /* enum pk_param */ 2905}; /* enum pk_param */
@@ -2732,22 +2909,50 @@ static const char *const pk_dsa_optlist[] = PK_DSA_OPTLIST;
2732static const char *const pk_dh_optlist[] = PK_DH_OPTLIST; 2909static const char *const pk_dh_optlist[] = PK_DH_OPTLIST;
2733static const char *const pk_ec_optlist[] = PK_EC_OPTLIST; 2910static const char *const pk_ec_optlist[] = PK_EC_OPTLIST;
2734 2911
2735static int pk_checkparam(lua_State *L, int type, int index) { 2912const char *const *pk_getoptlist(int type, int *_nopts, int *_optoffset) {
2913 const char *const *optlist = NULL;
2914 int nopts = 0, optoffset = 0;
2915
2736 switch (type) { 2916 switch (type) {
2737 case EVP_PKEY_RSA: 2917 case EVP_PKEY_RSA:
2738 return luaL_checkoption(L, index, NULL, pk_rsa_optlist) + PK_RSA_OPTOFFSET; 2918 optlist = pk_rsa_optlist;
2919 nopts = countof(pk_rsa_optlist) - 1;
2920 optoffset = PK_RSA_OPTOFFSET;
2921
2922 break;
2739 case EVP_PKEY_DSA: 2923 case EVP_PKEY_DSA:
2740 return luaL_checkoption(L, index, NULL, pk_dsa_optlist) + PK_DSA_OPTOFFSET; 2924 optlist = pk_dsa_optlist;
2925 nopts = countof(pk_dsa_optlist) - 1;
2926 optoffset = PK_DSA_OPTOFFSET;
2927
2928 break;
2741 case EVP_PKEY_DH: 2929 case EVP_PKEY_DH:
2742 return luaL_checkoption(L, index, NULL, pk_dh_optlist) + PK_DH_OPTOFFSET; 2930 optlist = pk_dh_optlist;
2931 nopts = countof(pk_dh_optlist) - 1;
2932 optoffset = PK_DH_OPTOFFSET;
2933
2934 break;
2743 case EVP_PKEY_EC: 2935 case EVP_PKEY_EC:
2744 return luaL_checkoption(L, index, NULL, pk_ec_optlist) + PK_EC_OPTOFFSET; 2936 optlist = pk_ec_optlist;
2745 default: 2937 nopts = countof(pk_ec_optlist) - 1;
2746 return luaL_error(L, "%d: unsupported EVP_PKEY base type", type); 2938 optoffset = PK_EC_OPTOFFSET;
2939
2940 break;
2747 } 2941 }
2748} /* pk_checkparam() */
2749 2942
2750static void pk_pushparam(lua_State *L, void *_key, enum pk_param which) { 2943 if (_nopts)
2944 *_nopts = nopts;
2945 if (_optoffset)
2946 *_optoffset = optoffset;
2947
2948 return optlist;
2949} /* pk_getoptlist() */
2950
2951#ifndef OPENSSL_NO_EC
2952static EC_GROUP *ecg_dup_nil(lua_State *, const EC_GROUP *);
2953#endif
2954
2955static void pk_pushparam(lua_State *L, void *base_key, enum pk_param which) {
2751 union { 2956 union {
2752 RSA *rsa; 2957 RSA *rsa;
2753 DH *dh; 2958 DH *dh;
@@ -2755,98 +2960,104 @@ static void pk_pushparam(lua_State *L, void *_key, enum pk_param which) {
2755#ifndef OPENSSL_NO_EC 2960#ifndef OPENSSL_NO_EC
2756 EC_KEY *ec; 2961 EC_KEY *ec;
2757#endif 2962#endif
2758 } key = { _key }; 2963 } key = { base_key };
2759 2964
2760 switch (which) { 2965 switch (which) {
2761 case PK_RSA_N: 2966 case PK_RSA_N:
2762 /* RSA public modulus n */ 2967 /* RSA public modulus n */
2763 bn_dup(L, key.rsa->n); 2968 bn_dup_nil(L, key.rsa->n);
2764 2969
2765 break; 2970 break;
2766 case PK_RSA_E: 2971 case PK_RSA_E:
2767 /* RSA public exponent e */ 2972 /* RSA public exponent e */
2768 bn_dup(L, key.rsa->e); 2973 bn_dup_nil(L, key.rsa->e);
2769 2974
2770 break; 2975 break;
2771 case PK_RSA_D: 2976 case PK_RSA_D:
2772 /* RSA secret exponent d */ 2977 /* RSA secret exponent d */
2773 bn_dup(L, key.rsa->d); 2978 bn_dup_nil(L, key.rsa->d);
2774 2979
2775 break; 2980 break;
2776 case PK_RSA_P: 2981 case PK_RSA_P:
2777 /* RSA secret prime p */ 2982 /* RSA secret prime p */
2778 bn_dup(L, key.rsa->p); 2983 bn_dup_nil(L, key.rsa->p);
2779 2984
2780 break; 2985 break;
2781 case PK_RSA_Q: 2986 case PK_RSA_Q:
2782 /* RSA secret prime q with p < q */ 2987 /* RSA secret prime q with p < q */
2783 bn_dup(L, key.rsa->q); 2988 bn_dup_nil(L, key.rsa->q);
2784 2989
2785 break; 2990 break;
2786 case PK_RSA_DMP1: 2991 case PK_RSA_DMP1:
2787 /* exponent1 */ 2992 /* exponent1 */
2788 bn_dup(L, key.rsa->dmp1); 2993 bn_dup_nil(L, key.rsa->dmp1);
2789 2994
2790 break; 2995 break;
2791 case PK_RSA_DMQ1: 2996 case PK_RSA_DMQ1:
2792 /* exponent2 */ 2997 /* exponent2 */
2793 bn_dup(L, key.rsa->dmq1); 2998 bn_dup_nil(L, key.rsa->dmq1);
2794 2999
2795 break; 3000 break;
2796 case PK_RSA_IQMP: 3001 case PK_RSA_IQMP:
2797 /* coefficient */ 3002 /* coefficient */
2798 bn_dup(L, key.rsa->iqmp); 3003 bn_dup_nil(L, key.rsa->iqmp);
2799 3004
2800 break; 3005 break;
2801 case PK_DSA_P: 3006 case PK_DSA_P:
2802 bn_dup(L, key.dsa->p); 3007 bn_dup_nil(L, key.dsa->p);
2803 3008
2804 break; 3009 break;
2805 case PK_DSA_Q: 3010 case PK_DSA_Q:
2806 bn_dup(L, key.dsa->q); 3011 bn_dup_nil(L, key.dsa->q);
2807 3012
2808 break; 3013 break;
2809 case PK_DSA_G: 3014 case PK_DSA_G:
2810 bn_dup(L, key.dsa->g); 3015 bn_dup_nil(L, key.dsa->g);
2811 3016
2812 break; 3017 break;
2813 case PK_DSA_PUB_KEY: 3018 case PK_DSA_PUB_KEY:
2814 bn_dup(L, key.dsa->pub_key); 3019 bn_dup_nil(L, key.dsa->pub_key);
2815 3020
2816 break; 3021 break;
2817 case PK_DSA_PRIV_KEY: 3022 case PK_DSA_PRIV_KEY:
2818 bn_dup(L, key.dsa->priv_key); 3023 bn_dup_nil(L, key.dsa->priv_key);
2819 3024
2820 break; 3025 break;
2821 case PK_DH_P: 3026 case PK_DH_P:
2822 bn_dup(L, key.dh->p); 3027 bn_dup_nil(L, key.dh->p);
2823 3028
2824 break; 3029 break;
2825 case PK_DH_G: 3030 case PK_DH_G:
2826 bn_dup(L, key.dh->g); 3031 bn_dup_nil(L, key.dh->g);
2827 3032
2828 break; 3033 break;
2829 case PK_DH_PUB_KEY: 3034 case PK_DH_PUB_KEY:
2830 bn_dup(L, key.dh->pub_key); 3035 bn_dup_nil(L, key.dh->pub_key);
2831 3036
2832 break; 3037 break;
2833 case PK_DH_PRIV_KEY: 3038 case PK_DH_PRIV_KEY:
2834 bn_dup(L, key.dh->priv_key); 3039 bn_dup_nil(L, key.dh->priv_key);
2835 3040
2836 break; 3041 break;
2837#ifndef OPENSSL_NO_EC 3042#ifndef OPENSSL_NO_EC
3043 case PK_EC_GROUP:
3044 ecg_dup_nil(L, EC_KEY_get0_group(key.ec));
3045
3046 break;
2838 case PK_EC_PUB_KEY: { 3047 case PK_EC_PUB_KEY: {
2839 const EC_GROUP *group; 3048 const EC_GROUP *group;
2840 const EC_POINT *public_key; 3049 const EC_POINT *pub_key;
2841 3050
2842 if (!(group = EC_KEY_get0_group(key.ec)) || !(public_key = EC_KEY_get0_public_key(key.ec))) 3051 if ((group = EC_KEY_get0_group(key.ec)) && (pub_key = EC_KEY_get0_public_key(key.ec))) {
2843 goto sslerr; 3052 bn_dup_nil(L, EC_POINT_point2bn(group, pub_key, EC_KEY_get_conv_form(key.ec), NULL, getctx(L)));
2844 bn_dup(L, EC_POINT_point2bn(group, public_key, EC_KEY_get_conv_form(key.ec), NULL, getctx(L))); 3053 } else {
3054 lua_pushnil(L);
3055 }
2845 3056
2846 break; 3057 break;
2847 } 3058 }
2848 case PK_EC_PRIV_KEY: 3059 case PK_EC_PRIV_KEY:
2849 bn_dup(L, EC_KEY_get0_private_key(key.ec)); 3060 bn_dup_nil(L, EC_KEY_get0_private_key(key.ec));
2850 3061
2851 break; 3062 break;
2852#endif 3063#endif
@@ -2855,84 +3066,204 @@ static void pk_pushparam(lua_State *L, void *_key, enum pk_param which) {
2855 } 3066 }
2856 3067
2857 return; 3068 return;
2858sslerr:
2859 auxL_error(L, auxL_EOPENSSL, "pkey:getParameters");
2860
2861 return;
2862} /* pk_pushparam() */ 3069} /* pk_pushparam() */
2863 3070
2864 3071
2865static int pk_getParameters(lua_State *L) { 3072static _Bool pk_bn_set_nothrow(BIGNUM **dst, BIGNUM *src) {
2866 EVP_PKEY *_key = checksimple(L, 1, PKEY_CLASS); 3073 BIGNUM *tmp;
2867 int type = EVP_PKEY_base_id(_key);
2868 void *key;
2869 int otop, index, tindex;
2870 3074
2871 if (!(key = EVP_PKEY_get0(_key))) 3075 if (!(tmp = BN_dup(src)))
2872 goto sslerr; 3076 return 0;
2873 3077
2874 if (lua_isnoneornil(L, 2)) { 3078 if (*dst)
2875 const char *const *optlist; 3079 BN_clear_free(*dst);
2876 const char *const *opt; 3080 *dst = tmp;
2877 3081
2878 switch (type) { 3082 return 1;
2879 case EVP_PKEY_RSA: 3083} /* pk_bn_set_nothrow() */
2880 optlist = pk_rsa_optlist;
2881 luaL_checkstack(L, countof(pk_rsa_optlist), "");
2882 3084
2883 break; 3085#define pk_bn_set(L, dst, index) do { \
2884 case EVP_PKEY_DSA: 3086 BIGNUM *n = checkbig((L), (index)); \
2885 optlist = pk_dsa_optlist; 3087 if (!pk_bn_set_nothrow((dst), n)) \
2886 luaL_checkstack(L, countof(pk_dsa_optlist), ""); 3088 goto sslerr; \
3089} while (0)
2887 3090
2888 break; 3091static void pk_setparam(lua_State *L, void *base_key, enum pk_param which, int index) {
2889 case EVP_PKEY_DH: 3092 union {
2890 optlist = pk_dh_optlist; 3093 RSA *rsa;
2891 luaL_checkstack(L, countof(pk_dh_optlist), ""); 3094 DH *dh;
3095 DSA *dsa;
3096#ifndef OPENSSL_NO_EC
3097 EC_KEY *ec;
3098#endif
3099 } key = { base_key };
2892 3100
2893 break; 3101 switch (which) {
2894 case EVP_PKEY_EC: 3102 case PK_RSA_N:
2895 optlist = pk_ec_optlist; 3103 pk_bn_set(L, &key.rsa->n, index);
2896 luaL_checkstack(L, countof(pk_ec_optlist), "");
2897 3104
2898 break; 3105 break;
2899 default: 3106 case PK_RSA_E:
2900 return luaL_error(L, "%d: unsupported EVP_PKEY base type", EVP_PKEY_base_id(key)); 3107 pk_bn_set(L, &key.rsa->e, index);
2901 } 3108
3109 break;
3110 case PK_RSA_D:
3111 pk_bn_set(L, &key.rsa->d, index);
3112
3113 break;
3114 case PK_RSA_P:
3115 pk_bn_set(L, &key.rsa->p, index);
3116
3117 break;
3118 case PK_RSA_Q:
3119 pk_bn_set(L, &key.rsa->q, index);
2902 3120
3121 break;
3122 case PK_RSA_DMP1:
3123 pk_bn_set(L, &key.rsa->dmp1, index);
3124
3125 break;
3126 case PK_RSA_DMQ1:
3127 pk_bn_set(L, &key.rsa->dmq1, index);
3128
3129 break;
3130 case PK_RSA_IQMP:
3131 pk_bn_set(L, &key.rsa->iqmp, index);
3132
3133 break;
3134 case PK_DSA_P:
3135 pk_bn_set(L, &key.dsa->p, index);
3136
3137 break;
3138 case PK_DSA_Q:
3139 pk_bn_set(L, &key.dsa->q, index);
3140
3141 break;
3142 case PK_DSA_G:
3143 pk_bn_set(L, &key.dsa->g, index);
3144
3145 break;
3146 case PK_DSA_PUB_KEY:
3147 pk_bn_set(L, &key.dsa->pub_key, index);
3148
3149 break;
3150 case PK_DSA_PRIV_KEY:
3151 pk_bn_set(L, &key.dsa->priv_key, index);
3152
3153 break;
3154 case PK_DH_P:
3155 pk_bn_set(L, &key.dh->p, index);
3156
3157 break;
3158 case PK_DH_G:
3159 pk_bn_set(L, &key.dh->g, index);
3160
3161 break;
3162 case PK_DH_PUB_KEY:
3163 pk_bn_set(L, &key.dh->pub_key, index);
3164
3165 break;
3166 case PK_DH_PRIV_KEY:
3167 pk_bn_set(L, &key.dh->priv_key, index);
3168
3169 break;
3170#ifndef OPENSSL_NO_EC
3171 case PK_EC_GROUP: {
3172 const EC_GROUP *group = checksimple(L, index, EC_GROUP_CLASS);
3173
3174 if (!EC_KEY_set_group(key.ec, group))
3175 goto sslerr;
3176
3177 break;
3178 }
3179 case PK_EC_PUB_KEY: {
3180 const BIGNUM *n = checkbig(L, index);
3181 const EC_GROUP *group;
3182 EC_POINT *pub_key;
3183 _Bool okay;
3184
3185 if (!(group = EC_KEY_get0_group(key.ec)))
3186 luaL_error(L, "unable to set EC pub_key (no group defined)");
3187
3188 if (!(pub_key = EC_POINT_bn2point(group, n, NULL, getctx(L))))
3189 goto sslerr;
3190
3191 /* NB: copies key, doesn't share or take ownership */
3192 okay = EC_KEY_set_public_key(key.ec, pub_key);
3193 EC_POINT_free(pub_key);
3194 if (!okay)
3195 goto sslerr;
3196
3197 break;
3198 }
3199 case PK_EC_PRIV_KEY: {
3200 const BIGNUM *n = checkbig(L, index);
3201
3202 /* NB: copies key, doesn't share or take ownership */
3203 if (!EC_KEY_set_private_key(key.ec, n))
3204 goto sslerr;
3205
3206 break;
3207 }
3208#endif
3209 default:
3210 luaL_error(L, "%d: invalid EVP_PKEY parameter", which);
3211 }
3212
3213 return;
3214sslerr:
3215 auxL_error(L, auxL_EOPENSSL, "pkey:setParameters");
3216
3217 return;
3218} /* pk_setparam() */
3219
3220
3221static int pk_getParameters(lua_State *L) {
3222 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3223 int base_type = EVP_PKEY_base_id(key);
3224 void *base_key;
3225 const char *const *optlist;
3226 int nopts, optoffset, otop, index, tindex;
3227
3228 if (!(base_key = EVP_PKEY_get0(key)))
3229 goto sslerr;
3230
3231 if (!(optlist = pk_getoptlist(base_type, &nopts, &optoffset)))
3232 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type);
3233
3234 if (lua_isnoneornil(L, 2)) {
2903 /* 3235 /*
2904 * Use special "{" parameter to tell loop to push table. 3236 * Use special "{" parameter to tell loop to push table.
2905 * Subsequent parameters will be assigned as fields. 3237 * Subsequent parameters will be assigned as fields.
2906 *
2907 * NOTE: optlist arrays are NULL-terminated. luaL_checkstack()
2908 * calls above left room for "{".
2909 */ 3238 */
2910 lua_pushstring(L, "{"); 3239 lua_pushstring(L, "{");
2911 3240 luaL_checkstack(L, nopts, "too many arguments");
2912 for (opt = optlist; *opt; opt++) { 3241 for (const char *const *optname = optlist; *optname; optname++) {
2913 lua_pushstring(L, *opt); 3242 lua_pushstring(L, *optname);
2914 } 3243 }
2915 } 3244 }
2916 3245
2917 otop = lua_gettop(L); 3246 otop = lua_gettop(L);
2918 3247
2919 /* provide space for results and working area */ 3248 /* provide space for results and working area */
2920 luaL_checkstack(L, (otop - 1) + LUA_MINSTACK, ""); 3249 luaL_checkstack(L, (otop - 1) + LUA_MINSTACK, "too many arguments");
2921 3250
2922 /* no table index, yet */ 3251 /* no table index, yet */
2923 tindex = 0; 3252 tindex = 0;
2924 3253
2925 for (index = 2; index <= otop; index++) { 3254 for (index = 2; index <= otop; index++) {
2926 const char *opt = luaL_checkstring(L, index); 3255 const char *optname = luaL_checkstring(L, index);
3256 int optid;
2927 3257
2928 if (*opt == '{') { 3258 if (*optname == '{') {
2929 lua_newtable(L); 3259 lua_newtable(L);
2930 tindex = lua_gettop(L); 3260 tindex = lua_gettop(L);
2931 } else { 3261 } else {
2932 pk_pushparam(L, key, pk_checkparam(L, type, index)); 3262 optid = luaL_checkoption(L, index, NULL, optlist) + optoffset;
3263 pk_pushparam(L, base_key, optid);
2933 3264
2934 if (tindex) { 3265 if (tindex) {
2935 lua_setfield(L, tindex, opt); 3266 lua_setfield(L, tindex, optname);
2936 } 3267 }
2937 } 3268 }
2938 } 3269 }
@@ -2943,6 +3274,34 @@ sslerr:
2943} /* pk_getParameters() */ 3274} /* pk_getParameters() */
2944 3275
2945 3276
3277static int pk_setParameters(lua_State *L) {
3278 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3279 int base_type = EVP_PKEY_base_id(key);
3280 void *base_key;
3281 const char *const *optlist;
3282 int optindex, optoffset;
3283
3284 luaL_checktype(L, 2, LUA_TTABLE);
3285
3286 if (!(base_key = EVP_PKEY_get0(key)))
3287 goto sslerr;
3288
3289 if (!(optlist = pk_getoptlist(base_type, NULL, &optoffset)))
3290 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type);
3291
3292 for (optindex = 0; optlist[optindex]; optindex++) {
3293 if (getfield(L, 2, optlist[optindex])) {
3294 pk_setparam(L, base_key, optindex + optoffset, -1);
3295 lua_pop(L, 1);
3296 }
3297 }
3298
3299 return 0;
3300sslerr:
3301 return auxL_error(L, auxL_EOPENSSL, "pkey:setParameters");
3302} /* pk_setParameters() */
3303
3304
2946static int pk__tostring(lua_State *L) { 3305static int pk__tostring(lua_State *L) {
2947 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); 3306 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
2948 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); 3307 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
@@ -2969,6 +3328,55 @@ static int pk__tostring(lua_State *L) {
2969} /* pk__tostring() */ 3328} /* pk__tostring() */
2970 3329
2971 3330
3331static int pk__index(lua_State *L) {
3332 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3333 void *base_key;
3334 const char *const *optlist;
3335 int optoffset, listoffset;
3336
3337 lua_pushvalue(L, lua_upvalueindex(1));
3338 lua_pushvalue(L, 2);
3339 lua_gettable(L, -2);
3340
3341 if (!lua_isnil(L, -1))
3342 return 1;
3343
3344 if (!lua_isstring(L, 2))
3345 return 0;
3346 if (!(base_key = EVP_PKEY_get0(key)))
3347 return 0;
3348 if (!(optlist = pk_getoptlist(EVP_PKEY_base_id(key), NULL, &optoffset)))
3349 return 0;
3350 if (-1 == (listoffset = auxL_testoption(L, 2, NULL, optlist, 0)))
3351 return 0;
3352
3353 pk_pushparam(L, base_key, listoffset + optoffset);
3354
3355 return 1;
3356} /* pk__index() */
3357
3358
3359static int pk__newindex(lua_State *L) {
3360 EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
3361 void *base_key;
3362 const char *const *optlist;
3363 int optoffset, listoffset;
3364
3365 if (!lua_isstring(L, 2))
3366 return 0;
3367 if (!(base_key = EVP_PKEY_get0(key)))
3368 return 0;
3369 if (!(optlist = pk_getoptlist(EVP_PKEY_base_id(key), NULL, &optoffset)))
3370 return 0;
3371 if (-1 == (listoffset = auxL_testoption(L, 2, NULL, optlist, 0)))
3372 return 0;
3373
3374 pk_setparam(L, base_key, listoffset + optoffset, 3);
3375
3376 return 0;
3377} /* pk__newindex() */
3378
3379
2972static int pk__gc(lua_State *L) { 3380static int pk__gc(lua_State *L) {
2973 EVP_PKEY **ud = luaL_checkudata(L, 1, PKEY_CLASS); 3381 EVP_PKEY **ud = luaL_checkudata(L, 1, PKEY_CLASS);
2974 3382
@@ -2981,7 +3389,7 @@ static int pk__gc(lua_State *L) {
2981} /* pk__gc() */ 3389} /* pk__gc() */
2982 3390
2983 3391
2984static const luaL_Reg pk_methods[] = { 3392static const auxL_Reg pk_methods[] = {
2985 { "type", &pk_type }, 3393 { "type", &pk_type },
2986 { "setPublicKey", &pk_setPublicKey }, 3394 { "setPublicKey", &pk_setPublicKey },
2987 { "setPrivateKey", &pk_setPrivateKey }, 3395 { "setPrivateKey", &pk_setPrivateKey },
@@ -2989,26 +3397,42 @@ static const luaL_Reg pk_methods[] = {
2989 { "verify", &pk_verify }, 3397 { "verify", &pk_verify },
2990 { "toPEM", &pk_toPEM }, 3398 { "toPEM", &pk_toPEM },
2991 { "getParameters", &pk_getParameters }, 3399 { "getParameters", &pk_getParameters },
3400 { "setParameters", &pk_setParameters },
2992 { NULL, NULL }, 3401 { NULL, NULL },
2993}; 3402};
2994 3403
2995static const luaL_Reg pk_metatable[] = { 3404static const auxL_Reg pk_metatable[] = {
2996 { "__tostring", &pk__tostring }, 3405 { "__tostring", &pk__tostring },
3406 { "__index", &pk__index, 1 },
3407 { "__newindex", &pk__newindex, 1 },
2997 { "__gc", &pk__gc }, 3408 { "__gc", &pk__gc },
2998 { NULL, NULL }, 3409 { NULL, NULL },
2999}; 3410};
3000 3411
3001 3412
3002static const luaL_Reg pk_globals[] = { 3413static const auxL_Reg pk_globals[] = {
3003 { "new", &pk_new }, 3414 { "new", &pk_new },
3004 { "interpose", &pk_interpose }, 3415 { "interpose", &pk_interpose },
3005 { NULL, NULL }, 3416 { NULL, NULL },
3006}; 3417};
3007 3418
3419static void pk_luainit(lua_State *L, _Bool reset) {
3420 if (!auxL_newmetatable(L, PKEY_CLASS, reset))
3421 return;
3422 auxL_setfuncs(L, pk_metatable, 0);
3423 auxL_newlib(L, pk_methods, 0);
3424 for (char **k = (char *[]){ "__index", "__newindex", 0 }; *k; k++) {
3425 lua_getfield(L, -2, *k); /* closure */
3426 lua_pushvalue(L, -2); /* method table */
3427 lua_setupvalue(L, -2, 1);
3428 }
3429 lua_pop(L, 2);
3430} /* pk_luainit() */
3431
3008int luaopen__openssl_pkey(lua_State *L) { 3432int luaopen__openssl_pkey(lua_State *L) {
3009 initall(L); 3433 initall(L);
3010 3434
3011 luaL_newlib(L, pk_globals); 3435 auxL_newlib(L, pk_globals, 0);
3012 3436
3013 return 1; 3437 return 1;
3014} /* luaopen__openssl_pkey() */ 3438} /* luaopen__openssl_pkey() */
@@ -3023,6 +3447,205 @@ int luaopen__openssl_pubkey(lua_State *L) {
3023 3447
3024 3448
3025/* 3449/*
3450 * EC_GROUP - openssl.ec.group
3451 *
3452 * NOTE: Ensure copy-by-value semantics when passing EC_GROUP objects as it
3453 * doesn't support reference counting. The only persistent reference should
3454 * be the Lua userdata value.
3455 *
3456 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3457
3458#ifndef OPENSSL_NO_EC
3459
3460static EC_GROUP *ecg_dup(lua_State *L, const EC_GROUP *src) {
3461 EC_GROUP **ud = prepsimple(L, EC_GROUP_CLASS);
3462
3463 if (!(*ud = EC_GROUP_dup(src)))
3464 auxL_error(L, auxL_EOPENSSL, "group");
3465
3466 return *ud;
3467} /* ecg_dup() */
3468
3469static EC_GROUP *ecg_dup_nil(lua_State *L, const EC_GROUP *src) {
3470 return (src)? ecg_dup(L, src) : (lua_pushnil(L), (EC_GROUP *)0);
3471} /* ecg_dup_nil() */
3472
3473static EC_GROUP *ecg_new_by_nid(int nid) {
3474 EC_GROUP *group;
3475
3476 if (!(group = EC_GROUP_new_by_curve_name(nid)))
3477 return NULL;
3478
3479 /* flag as named for benefit of __tostring */
3480 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
3481
3482 /* compressed points may be patented */
3483 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
3484
3485 return group;
3486} /* ecg_new_by_nid() */
3487
3488static EC_GROUP *ecg_push_by_nid(lua_State *L, int nid) {
3489 EC_GROUP **group = prepsimple(L, EC_GROUP_CLASS);
3490
3491 if (!(*group = EC_GROUP_new_by_curve_name(nid)))
3492 goto oops;
3493
3494 EC_GROUP_set_asn1_flag(*group, OPENSSL_EC_NAMED_CURVE);
3495
3496 /* compressed points may be patented */
3497 EC_GROUP_set_point_conversion_form(*group, POINT_CONVERSION_UNCOMPRESSED);
3498
3499 return *group;
3500oops:
3501 lua_pop(L, 1);
3502
3503 return NULL;
3504} /* ecg_push_by_nid() */
3505
3506static int ecg_new(lua_State *L) {
3507 switch (lua_type(L, 1)) {
3508 case LUA_TSTRING: {
3509 const char *data;
3510 size_t datalen;
3511 int nid, type, goterr;
3512 BIO *bio;
3513 EC_GROUP **group;
3514
3515 data = luaL_checklstring(L, 1, &datalen);
3516
3517 if (auxS_txt2nid(&nid, data)) {
3518 if (!ecg_push_by_nid(L, nid))
3519 goto sslerr;
3520 } else {
3521 type = optencoding(L, 2, "*", X509_ANY|X509_PEM|X509_DER);
3522 group = prepsimple(L, EC_GROUP_CLASS);
3523
3524 luaL_argcheck(L, datalen < INT_MAX, 1, "string too long");
3525 if (!(bio = BIO_new_mem_buf((void *)data, datalen)))
3526 return auxL_error(L, auxL_EOPENSSL, "group.new");
3527
3528 goterr = 0;
3529
3530 if (type == X509_PEM || type == X509_ANY) {
3531 goterr |= !(*group = PEM_read_bio_ECPKParameters(bio, NULL, 0, ""));
3532 }
3533
3534 if (!*group && (type == X509_DER || type == X509_ANY)) {
3535 BIO_reset(bio);
3536 goterr |= !(*group = d2i_ECPKParameters_bio(bio, NULL));
3537 }
3538
3539 BIO_free(bio);
3540
3541 if (!*group)
3542 return auxL_error(L, auxL_EOPENSSL, "group.new");
3543 if (goterr)
3544 ERR_clear_error();
3545 }
3546
3547 return 1;
3548 }
3549 case LUA_TNUMBER: {
3550 int nid = luaL_checkint(L, 2);
3551
3552 if (!ecg_push_by_nid(L, nid))
3553 goto sslerr;
3554
3555 return 1;
3556 }
3557 default:
3558 return luaL_error(L, "%s: unknown group initializer", lua_typename(L, lua_type(L, 1)));
3559 } /* switch() */
3560
3561 return 0;
3562sslerr:
3563 return auxL_error(L, auxL_EOPENSSL, "group.new");
3564} /* ecg_new() */
3565
3566static int ecg_interpose(lua_State *L) {
3567 return interpose(L, EC_GROUP_CLASS);
3568} /* ecg_interpose() */
3569
3570static int ecg_tostring(lua_State *L) {
3571 EC_GROUP *group = checksimple(L, 1, EC_GROUP_CLASS);
3572 int how = optencoding(L, 2, "pem", X509_PEM|X509_DER|X509_TXT);
3573 BIO *bio = getbio(L);
3574 char *bytes;
3575 int len, indent;
3576
3577 switch (how) {
3578 case X509_PEM:
3579 if (!PEM_write_bio_ECPKParameters(bio, group))
3580 goto sslerr;
3581 break;
3582 case X509_DER:
3583 if (!i2d_ECPKParameters_bio(bio, group))
3584 goto sslerr;
3585 break;
3586 case X509_TXT:
3587 indent = auxL_optinteger(L, 3, 0, 0, INT_MAX);
3588 if (!ECPKParameters_print(bio, group, indent))
3589 goto sslerr;
3590 break;
3591 }
3592
3593 len = BIO_get_mem_data(bio, &bytes);
3594 lua_pushlstring(L, bytes, len);
3595
3596 return 1;
3597sslerr:
3598 return auxL_error(L, auxL_EOPENSSL, "group:__tostring");
3599} /* ecg_tostring() */
3600
3601static int ecg__tostring(lua_State *L) {
3602 return ecg_tostring(L);
3603} /* ecg__tostring() */
3604
3605static int ecg__gc(lua_State *L) {
3606 EC_GROUP **ud = luaL_checkudata(L, 1, EC_GROUP_CLASS);
3607
3608 if (*ud) {
3609 EC_GROUP_clear_free(*ud);
3610 *ud = NULL;
3611 }
3612
3613 return 0;
3614} /* ecg__gc() */
3615
3616static const auxL_Reg ecg_methods[] = {
3617 { "tostring", &ecg_tostring },
3618 { NULL, NULL },
3619};
3620
3621static const auxL_Reg ecg_metatable[] = {
3622 { "__tostring", &ecg__tostring },
3623 { "__gc", &ecg__gc },
3624 { NULL, NULL },
3625};
3626
3627static const auxL_Reg ecg_globals[] = {
3628 { "new", &ecg_new },
3629 { "interpose", &ecg_interpose },
3630 { NULL, NULL },
3631};
3632
3633#endif /* OPENSSL_NO_EC */
3634
3635int luaopen__openssl_ec_group(lua_State *L) {
3636#ifndef OPENSSL_NO_EC
3637 initall(L);
3638
3639 auxL_newlib(L, ecg_globals, 0);
3640
3641 return 1;
3642#else
3643 return 0;
3644#endif
3645} /* luaopen__openssl_ec_group() */
3646
3647
3648/*
3026 * X509_NAME - openssl.x509.name 3649 * X509_NAME - openssl.x509.name
3027 * 3650 *
3028 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 3651 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -3196,13 +3819,13 @@ static int xn__tostring(lua_State *L) {
3196} /* xn__tostring() */ 3819} /* xn__tostring() */
3197 3820
3198 3821
3199static const luaL_Reg xn_methods[] = { 3822static const auxL_Reg xn_methods[] = {
3200 { "add", &xn_add }, 3823 { "add", &xn_add },
3201 { "all", &xn_all }, 3824 { "all", &xn_all },
3202 { NULL, NULL }, 3825 { NULL, NULL },
3203}; 3826};
3204 3827
3205static const luaL_Reg xn_metatable[] = { 3828static const auxL_Reg xn_metatable[] = {
3206 { "__pairs", &xn__pairs }, 3829 { "__pairs", &xn__pairs },
3207 { "__gc", &xn__gc }, 3830 { "__gc", &xn__gc },
3208 { "__tostring", &xn__tostring }, 3831 { "__tostring", &xn__tostring },
@@ -3210,7 +3833,7 @@ static const luaL_Reg xn_metatable[] = {
3210}; 3833};
3211 3834
3212 3835
3213static const luaL_Reg xn_globals[] = { 3836static const auxL_Reg xn_globals[] = {
3214 { "new", &xn_new }, 3837 { "new", &xn_new },
3215 { "interpose", &xn_interpose }, 3838 { "interpose", &xn_interpose },
3216 { NULL, NULL }, 3839 { NULL, NULL },
@@ -3219,7 +3842,7 @@ static const luaL_Reg xn_globals[] = {
3219int luaopen__openssl_x509_name(lua_State *L) { 3842int luaopen__openssl_x509_name(lua_State *L) {
3220 initall(L); 3843 initall(L);
3221 3844
3222 luaL_newlib(L, xn_globals); 3845 auxL_newlib(L, xn_globals, 0);
3223 3846
3224 return 1; 3847 return 1;
3225} /* luaopen__openssl_x509_name() */ 3848} /* luaopen__openssl_x509_name() */
@@ -3452,19 +4075,19 @@ static int gn__gc(lua_State *L) {
3452} /* gn__gc() */ 4075} /* gn__gc() */
3453 4076
3454 4077
3455static const luaL_Reg gn_methods[] = { 4078static const auxL_Reg gn_methods[] = {
3456 { "add", &gn_add }, 4079 { "add", &gn_add },
3457 { NULL, NULL }, 4080 { NULL, NULL },
3458}; 4081};
3459 4082
3460static const luaL_Reg gn_metatable[] = { 4083static const auxL_Reg gn_metatable[] = {
3461 { "__pairs", &gn__pairs }, 4084 { "__pairs", &gn__pairs },
3462 { "__gc", &gn__gc }, 4085 { "__gc", &gn__gc },
3463 { NULL, NULL }, 4086 { NULL, NULL },
3464}; 4087};
3465 4088
3466 4089
3467static const luaL_Reg gn_globals[] = { 4090static const auxL_Reg gn_globals[] = {
3468 { "new", &gn_new }, 4091 { "new", &gn_new },
3469 { "interpose", &gn_interpose }, 4092 { "interpose", &gn_interpose },
3470 { NULL, NULL }, 4093 { NULL, NULL },
@@ -3473,7 +4096,7 @@ static const luaL_Reg gn_globals[] = {
3473int luaopen__openssl_x509_altname(lua_State *L) { 4096int luaopen__openssl_x509_altname(lua_State *L) {
3474 initall(L); 4097 initall(L);
3475 4098
3476 luaL_newlib(L, gn_globals); 4099 auxL_newlib(L, gn_globals, 0);
3477 4100
3478 return 1; 4101 return 1;
3479} /* luaopen__openssl_x509_altname() */ 4102} /* luaopen__openssl_x509_altname() */
@@ -3676,7 +4299,7 @@ static int xe__gc(lua_State *L) {
3676} /* xe__gc() */ 4299} /* xe__gc() */
3677 4300
3678 4301
3679static const luaL_Reg xe_methods[] = { 4302static const auxL_Reg xe_methods[] = {
3680 { "getID", &xe_getID }, 4303 { "getID", &xe_getID },
3681 { "getName", &xe_getName }, 4304 { "getName", &xe_getName },
3682 { "getShortName", &xe_getShortName }, 4305 { "getShortName", &xe_getShortName },
@@ -3687,13 +4310,13 @@ static const luaL_Reg xe_methods[] = {
3687 { NULL, NULL }, 4310 { NULL, NULL },
3688}; 4311};
3689 4312
3690static const luaL_Reg xe_metatable[] = { 4313static const auxL_Reg xe_metatable[] = {
3691 { "__gc", &xe__gc }, 4314 { "__gc", &xe__gc },
3692 { NULL, NULL }, 4315 { NULL, NULL },
3693}; 4316};
3694 4317
3695 4318
3696static const luaL_Reg xe_globals[] = { 4319static const auxL_Reg xe_globals[] = {
3697 { "new", &xe_new }, 4320 { "new", &xe_new },
3698 { "interpose", &xe_interpose }, 4321 { "interpose", &xe_interpose },
3699 { NULL, NULL }, 4322 { NULL, NULL },
@@ -3710,7 +4333,7 @@ static const auxL_IntegerReg xe_textopts[] = {
3710int luaopen__openssl_x509_extension(lua_State *L) { 4333int luaopen__openssl_x509_extension(lua_State *L) {
3711 initall(L); 4334 initall(L);
3712 4335
3713 luaL_newlib(L, xe_globals); 4336 auxL_newlib(L, xe_globals, 0);
3714 auxL_setintegers(L, xe_textopts); 4337 auxL_setintegers(L, xe_textopts);
3715 4338
3716 return 1; 4339 return 1;
@@ -4282,7 +4905,7 @@ static int xc_getBasicConstraint(lua_State *L) {
4282 int n = 0, i, top; 4905 int n = 0, i, top;
4283 4906
4284 for (i = 2, top = lua_gettop(L); i <= top; i++) { 4907 for (i = 2, top = lua_gettop(L); i <= top; i++) {
4285 switch (checkoption(L, i, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL })) { 4908 switch (auxL_checkoption(L, i, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL }, 1)) {
4286 case 0: 4909 case 0:
4287 lua_pushboolean(L, CA); 4910 lua_pushboolean(L, CA);
4288 n++; 4911 n++;
@@ -4338,7 +4961,7 @@ static int xc_setBasicConstraint(lua_State *L) {
4338 } else { 4961 } else {
4339 lua_settop(L, 3); 4962 lua_settop(L, 3);
4340 4963
4341 switch (checkoption(L, 2, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL })) { 4964 switch (auxL_checkoption(L, 2, 0, (const char *[]){ "CA", "pathLen", "pathLenConstraint", NULL }, 1)) {
4342 case 0: 4965 case 0:
4343 luaL_checktype(L, 3, LUA_TBOOLEAN); 4966 luaL_checktype(L, 3, LUA_TBOOLEAN);
4344 CA = lua_toboolean(L, 3); 4967 CA = lua_toboolean(L, 3);
@@ -4679,7 +5302,7 @@ static int xc__gc(lua_State *L) {
4679} /* xc__gc() */ 5302} /* xc__gc() */
4680 5303
4681 5304
4682static const luaL_Reg xc_methods[] = { 5305static const auxL_Reg xc_methods[] = {
4683 { "getVersion", &xc_getVersion }, 5306 { "getVersion", &xc_getVersion },
4684 { "setVersion", &xc_setVersion }, 5307 { "setVersion", &xc_setVersion },
4685 { "getSerial", &xc_getSerial }, 5308 { "getSerial", &xc_getSerial },
@@ -4718,14 +5341,14 @@ static const luaL_Reg xc_methods[] = {
4718 { NULL, NULL }, 5341 { NULL, NULL },
4719}; 5342};
4720 5343
4721static const luaL_Reg xc_metatable[] = { 5344static const auxL_Reg xc_metatable[] = {
4722 { "__tostring", &xc__tostring }, 5345 { "__tostring", &xc__tostring },
4723 { "__gc", &xc__gc }, 5346 { "__gc", &xc__gc },
4724 { NULL, NULL }, 5347 { NULL, NULL },
4725}; 5348};
4726 5349
4727 5350
4728static const luaL_Reg xc_globals[] = { 5351static const auxL_Reg xc_globals[] = {
4729 { "new", &xc_new }, 5352 { "new", &xc_new },
4730 { "interpose", &xc_interpose }, 5353 { "interpose", &xc_interpose },
4731 { NULL, NULL }, 5354 { NULL, NULL },
@@ -4734,7 +5357,7 @@ static const luaL_Reg xc_globals[] = {
4734int luaopen__openssl_x509_cert(lua_State *L) { 5357int luaopen__openssl_x509_cert(lua_State *L) {
4735 initall(L); 5358 initall(L);
4736 5359
4737 luaL_newlib(L, xc_globals); 5360 auxL_newlib(L, xc_globals, 0);
4738 5361
4739 return 1; 5362 return 1;
4740} /* luaopen__openssl_x509_cert() */ 5363} /* luaopen__openssl_x509_cert() */
@@ -4914,7 +5537,7 @@ static int xr__gc(lua_State *L) {
4914 return 0; 5537 return 0;
4915} /* xr__gc() */ 5538} /* xr__gc() */
4916 5539
4917static const luaL_Reg xr_methods[] = { 5540static const auxL_Reg xr_methods[] = {
4918 { "getVersion", &xr_getVersion }, 5541 { "getVersion", &xr_getVersion },
4919 { "setVersion", &xr_setVersion }, 5542 { "setVersion", &xr_setVersion },
4920 { "getSubject", &xr_getSubject }, 5543 { "getSubject", &xr_getSubject },
@@ -4926,14 +5549,14 @@ static const luaL_Reg xr_methods[] = {
4926 { NULL, NULL }, 5549 { NULL, NULL },
4927}; 5550};
4928 5551
4929static const luaL_Reg xr_metatable[] = { 5552static const auxL_Reg xr_metatable[] = {
4930 { "__tostring", &xr__tostring }, 5553 { "__tostring", &xr__tostring },
4931 { "__gc", &xr__gc }, 5554 { "__gc", &xr__gc },
4932 { NULL, NULL }, 5555 { NULL, NULL },
4933}; 5556};
4934 5557
4935 5558
4936static const luaL_Reg xr_globals[] = { 5559static const auxL_Reg xr_globals[] = {
4937 { "new", &xr_new }, 5560 { "new", &xr_new },
4938 { "interpose", &xr_interpose }, 5561 { "interpose", &xr_interpose },
4939 { NULL, NULL }, 5562 { NULL, NULL },
@@ -4942,7 +5565,7 @@ static const luaL_Reg xr_globals[] = {
4942int luaopen__openssl_x509_csr(lua_State *L) { 5565int luaopen__openssl_x509_csr(lua_State *L) {
4943 initall(L); 5566 initall(L);
4944 5567
4945 luaL_newlib(L, xr_globals); 5568 auxL_newlib(L, xr_globals, 0);
4946 5569
4947 return 1; 5570 return 1;
4948} /* luaopen__openssl_x509_csr() */ 5571} /* luaopen__openssl_x509_csr() */
@@ -5305,7 +5928,7 @@ static int xx__gc(lua_State *L) {
5305 return 0; 5928 return 0;
5306} /* xx__gc() */ 5929} /* xx__gc() */
5307 5930
5308static const luaL_Reg xx_methods[] = { 5931static const auxL_Reg xx_methods[] = {
5309 { "getVersion", &xx_getVersion }, 5932 { "getVersion", &xx_getVersion },
5310 { "setVersion", &xx_setVersion }, 5933 { "setVersion", &xx_setVersion },
5311 { "getLastUpdate", &xx_getLastUpdate }, 5934 { "getLastUpdate", &xx_getLastUpdate },
@@ -5324,14 +5947,14 @@ static const luaL_Reg xx_methods[] = {
5324 { NULL, NULL }, 5947 { NULL, NULL },
5325}; 5948};
5326 5949
5327static const luaL_Reg xx_metatable[] = { 5950static const auxL_Reg xx_metatable[] = {
5328 { "__tostring", &xx__tostring }, 5951 { "__tostring", &xx__tostring },
5329 { "__gc", &xx__gc }, 5952 { "__gc", &xx__gc },
5330 { NULL, NULL }, 5953 { NULL, NULL },
5331}; 5954};
5332 5955
5333 5956
5334static const luaL_Reg xx_globals[] = { 5957static const auxL_Reg xx_globals[] = {
5335 { "new", &xx_new }, 5958 { "new", &xx_new },
5336 { "interpose", &xx_interpose }, 5959 { "interpose", &xx_interpose },
5337 { NULL, NULL }, 5960 { NULL, NULL },
@@ -5340,7 +5963,7 @@ static const luaL_Reg xx_globals[] = {
5340int luaopen__openssl_x509_crl(lua_State *L) { 5963int luaopen__openssl_x509_crl(lua_State *L) {
5341 initall(L); 5964 initall(L);
5342 5965
5343 luaL_newlib(L, xx_globals); 5966 auxL_newlib(L, xx_globals, 0);
5344 5967
5345 return 1; 5968 return 1;
5346} /* luaopen__openssl_x509_crl() */ 5969} /* luaopen__openssl_x509_crl() */
@@ -5477,19 +6100,19 @@ static int xl__gc(lua_State *L) {
5477} /* xl__gc() */ 6100} /* xl__gc() */
5478 6101
5479 6102
5480static const luaL_Reg xl_methods[] = { 6103static const auxL_Reg xl_methods[] = {
5481 { "add", &xl_add }, 6104 { "add", &xl_add },
5482 { NULL, NULL }, 6105 { NULL, NULL },
5483}; 6106};
5484 6107
5485static const luaL_Reg xl_metatable[] = { 6108static const auxL_Reg xl_metatable[] = {
5486 { "__pairs", &xl__pairs }, 6109 { "__pairs", &xl__pairs },
5487 { "__ipairs", &xl__pairs }, 6110 { "__ipairs", &xl__pairs },
5488 { "__gc", &xl__gc }, 6111 { "__gc", &xl__gc },
5489 { NULL, NULL }, 6112 { NULL, NULL },
5490}; 6113};
5491 6114
5492static const luaL_Reg xl_globals[] = { 6115static const auxL_Reg xl_globals[] = {
5493 { "new", &xl_new }, 6116 { "new", &xl_new },
5494 { "interpose", &xl_interpose }, 6117 { "interpose", &xl_interpose },
5495 { NULL, NULL }, 6118 { NULL, NULL },
@@ -5498,7 +6121,7 @@ static const luaL_Reg xl_globals[] = {
5498int luaopen__openssl_x509_chain(lua_State *L) { 6121int luaopen__openssl_x509_chain(lua_State *L) {
5499 initall(L); 6122 initall(L);
5500 6123
5501 luaL_newlib(L, xl_globals); 6124 auxL_newlib(L, xl_globals, 0);
5502 6125
5503 return 1; 6126 return 1;
5504} /* luaopen__openssl_x509_chain() */ 6127} /* luaopen__openssl_x509_chain() */
@@ -5642,18 +6265,18 @@ static int xs__gc(lua_State *L) {
5642} /* xs__gc() */ 6265} /* xs__gc() */
5643 6266
5644 6267
5645static const luaL_Reg xs_methods[] = { 6268static const auxL_Reg xs_methods[] = {
5646 { "add", &xs_add }, 6269 { "add", &xs_add },
5647 { "verify", &xs_verify }, 6270 { "verify", &xs_verify },
5648 { NULL, NULL }, 6271 { NULL, NULL },
5649}; 6272};
5650 6273
5651static const luaL_Reg xs_metatable[] = { 6274static const auxL_Reg xs_metatable[] = {
5652 { "__gc", &xs__gc }, 6275 { "__gc", &xs__gc },
5653 { NULL, NULL }, 6276 { NULL, NULL },
5654}; 6277};
5655 6278
5656static const luaL_Reg xs_globals[] = { 6279static const auxL_Reg xs_globals[] = {
5657 { "new", &xs_new }, 6280 { "new", &xs_new },
5658 { "interpose", &xs_interpose }, 6281 { "interpose", &xs_interpose },
5659 { NULL, NULL }, 6282 { NULL, NULL },
@@ -5662,7 +6285,7 @@ static const luaL_Reg xs_globals[] = {
5662int luaopen__openssl_x509_store(lua_State *L) { 6285int luaopen__openssl_x509_store(lua_State *L) {
5663 initall(L); 6286 initall(L);
5664 6287
5665 luaL_newlib(L, xs_globals); 6288 auxL_newlib(L, xs_globals, 0);
5666 6289
5667 return 1; 6290 return 1;
5668} /* luaopen__openssl_x509_store() */ 6291} /* luaopen__openssl_x509_store() */
@@ -5713,17 +6336,17 @@ static int stx__gc(lua_State *L) {
5713} /* stx__gc() */ 6336} /* stx__gc() */
5714 6337
5715 6338
5716static const luaL_Reg stx_methods[] = { 6339static const auxL_Reg stx_methods[] = {
5717 { "add", &stx_add }, 6340 { "add", &stx_add },
5718 { NULL, NULL }, 6341 { NULL, NULL },
5719}; 6342};
5720 6343
5721static const luaL_Reg stx_metatable[] = { 6344static const auxL_Reg stx_metatable[] = {
5722 { "__gc", &stx__gc }, 6345 { "__gc", &stx__gc },
5723 { NULL, NULL }, 6346 { NULL, NULL },
5724}; 6347};
5725 6348
5726static const luaL_Reg stx_globals[] = { 6349static const auxL_Reg stx_globals[] = {
5727 { "new", &stx_new }, 6350 { "new", &stx_new },
5728 { "interpose", &stx_interpose }, 6351 { "interpose", &stx_interpose },
5729 { NULL, NULL }, 6352 { NULL, NULL },
@@ -5732,7 +6355,7 @@ static const luaL_Reg stx_globals[] = {
5732int luaopen__openssl_x509_store_context(lua_State *L) { 6355int luaopen__openssl_x509_store_context(lua_State *L) {
5733 initall(L); 6356 initall(L);
5734 6357
5735 luaL_newlib(L, stx_globals); 6358 auxL_newlib(L, stx_globals, 0);
5736 6359
5737 return 1; 6360 return 1;
5738} /* luaopen__openssl_x509_store_context() */ 6361} /* luaopen__openssl_x509_store_context() */
@@ -5833,18 +6456,18 @@ static int p12__gc(lua_State *L) {
5833} /* p12__gc() */ 6456} /* p12__gc() */
5834 6457
5835 6458
5836static const luaL_Reg p12_methods[] = { 6459static const auxL_Reg p12_methods[] = {
5837 { "tostring", &p12__tostring }, 6460 { "tostring", &p12__tostring },
5838 { NULL, NULL }, 6461 { NULL, NULL },
5839}; 6462};
5840 6463
5841static const luaL_Reg p12_metatable[] = { 6464static const auxL_Reg p12_metatable[] = {
5842 { "__tostring", &p12__tostring }, 6465 { "__tostring", &p12__tostring },
5843 { "__gc", &p12__gc }, 6466 { "__gc", &p12__gc },
5844 { NULL, NULL }, 6467 { NULL, NULL },
5845}; 6468};
5846 6469
5847static const luaL_Reg p12_globals[] = { 6470static const auxL_Reg p12_globals[] = {
5848 { "new", &p12_new }, 6471 { "new", &p12_new },
5849 { "interpose", &p12_interpose }, 6472 { "interpose", &p12_interpose },
5850 { NULL, NULL }, 6473 { NULL, NULL },
@@ -5853,7 +6476,7 @@ static const luaL_Reg p12_globals[] = {
5853int luaopen__openssl_pkcs12(lua_State *L) { 6476int luaopen__openssl_pkcs12(lua_State *L) {
5854 initall(L); 6477 initall(L);
5855 6478
5856 luaL_newlib(L, p12_globals); 6479 auxL_newlib(L, p12_globals, 0);
5857 6480
5858 return 1; 6481 return 1;
5859} /* luaopen__openssl_pkcs12() */ 6482} /* luaopen__openssl_pkcs12() */
@@ -5893,7 +6516,7 @@ static int sx_new(lua_State *L) {
5893 lua_settop(L, 2); 6516 lua_settop(L, 2);
5894 srv = lua_toboolean(L, 2); 6517 srv = lua_toboolean(L, 2);
5895 6518
5896 switch (checkoption(L, 1, "TLS", opts)) { 6519 switch (auxL_checkoption(L, 1, "TLS", opts, 1)) {
5897 case 0: /* SSL */ 6520 case 0: /* SSL */
5898 method = (srv)? &SSLv23_server_method : &SSLv23_client_method; 6521 method = (srv)? &SSLv23_server_method : &SSLv23_client_method;
5899 options = SSL_OP_NO_SSLv2; 6522 options = SSL_OP_NO_SSLv2;
@@ -5949,7 +6572,7 @@ static int sx_new(lua_State *L) {
5949 break; 6572 break;
5950#endif 6573#endif
5951 default: 6574 default:
5952 return badoption(L, 1, NULL); 6575 return luaL_argerror(L, 1, "invalid option");
5953 } 6576 }
5954 6577
5955 ud = prepsimple(L, SSL_CTX_CLASS); 6578 ud = prepsimple(L, SSL_CTX_CLASS);
@@ -6270,7 +6893,7 @@ static int sx__gc(lua_State *L) {
6270} /* sx__gc() */ 6893} /* sx__gc() */
6271 6894
6272 6895
6273static const luaL_Reg sx_methods[] = { 6896static const auxL_Reg sx_methods[] = {
6274 { "setOptions", &sx_setOptions }, 6897 { "setOptions", &sx_setOptions },
6275 { "getOptions", &sx_getOptions }, 6898 { "getOptions", &sx_getOptions },
6276 { "clearOptions", &sx_clearOptions }, 6899 { "clearOptions", &sx_clearOptions },
@@ -6290,12 +6913,12 @@ static const luaL_Reg sx_methods[] = {
6290 { NULL, NULL }, 6913 { NULL, NULL },
6291}; 6914};
6292 6915
6293static const luaL_Reg sx_metatable[] = { 6916static const auxL_Reg sx_metatable[] = {
6294 { "__gc", &sx__gc }, 6917 { "__gc", &sx__gc },
6295 { NULL, NULL }, 6918 { NULL, NULL },
6296}; 6919};
6297 6920
6298static const luaL_Reg sx_globals[] = { 6921static const auxL_Reg sx_globals[] = {
6299 { "new", &sx_new }, 6922 { "new", &sx_new },
6300 { "interpose", &sx_interpose }, 6923 { "interpose", &sx_interpose },
6301 { NULL, NULL }, 6924 { NULL, NULL },
@@ -6358,7 +6981,7 @@ static const auxL_IntegerReg sx_option[] = {
6358int luaopen__openssl_ssl_context(lua_State *L) { 6981int luaopen__openssl_ssl_context(lua_State *L) {
6359 initall(L); 6982 initall(L);
6360 6983
6361 luaL_newlib(L, sx_globals); 6984 auxL_newlib(L, sx_globals, 0);
6362 auxL_setintegers(L, sx_verify); 6985 auxL_setintegers(L, sx_verify);
6363 auxL_setintegers(L, sx_option); 6986 auxL_setintegers(L, sx_option);
6364 6987
@@ -6604,7 +7227,7 @@ static int ssl__gc(lua_State *L) {
6604} /* ssl__gc() */ 7227} /* ssl__gc() */
6605 7228
6606 7229
6607static const luaL_Reg ssl_methods[] = { 7230static const auxL_Reg ssl_methods[] = {
6608 { "setOptions", &ssl_setOptions }, 7231 { "setOptions", &ssl_setOptions },
6609 { "getOptions", &ssl_getOptions }, 7232 { "getOptions", &ssl_getOptions },
6610 { "clearOptions", &ssl_clearOptions }, 7233 { "clearOptions", &ssl_clearOptions },
@@ -6624,12 +7247,12 @@ static const luaL_Reg ssl_methods[] = {
6624 { NULL, NULL }, 7247 { NULL, NULL },
6625}; 7248};
6626 7249
6627static const luaL_Reg ssl_metatable[] = { 7250static const auxL_Reg ssl_metatable[] = {
6628 { "__gc", &ssl__gc }, 7251 { "__gc", &ssl__gc },
6629 { NULL, NULL }, 7252 { NULL, NULL },
6630}; 7253};
6631 7254
6632static const luaL_Reg ssl_globals[] = { 7255static const auxL_Reg ssl_globals[] = {
6633 { "new", &ssl_new }, 7256 { "new", &ssl_new },
6634 { "interpose", &ssl_interpose }, 7257 { "interpose", &ssl_interpose },
6635 { NULL, NULL }, 7258 { NULL, NULL },
@@ -6652,7 +7275,7 @@ static const auxL_IntegerReg ssl_version[] = {
6652int luaopen__openssl_ssl(lua_State *L) { 7275int luaopen__openssl_ssl(lua_State *L) {
6653 initall(L); 7276 initall(L);
6654 7277
6655 luaL_newlib(L, ssl_globals); 7278 auxL_newlib(L, ssl_globals, 0);
6656 auxL_setintegers(L, ssl_version); 7279 auxL_setintegers(L, ssl_version);
6657 auxL_setintegers(L, sx_verify); 7280 auxL_setintegers(L, sx_verify);
6658 auxL_setintegers(L, sx_option); 7281 auxL_setintegers(L, sx_option);
@@ -6748,18 +7371,18 @@ static int md__gc(lua_State *L) {
6748} /* md__gc() */ 7371} /* md__gc() */
6749 7372
6750 7373
6751static const luaL_Reg md_methods[] = { 7374static const auxL_Reg md_methods[] = {
6752 { "update", &md_update }, 7375 { "update", &md_update },
6753 { "final", &md_final }, 7376 { "final", &md_final },
6754 { NULL, NULL }, 7377 { NULL, NULL },
6755}; 7378};
6756 7379
6757static const luaL_Reg md_metatable[] = { 7380static const auxL_Reg md_metatable[] = {
6758 { "__gc", &md__gc }, 7381 { "__gc", &md__gc },
6759 { NULL, NULL }, 7382 { NULL, NULL },
6760}; 7383};
6761 7384
6762static const luaL_Reg md_globals[] = { 7385static const auxL_Reg md_globals[] = {
6763 { "new", &md_new }, 7386 { "new", &md_new },
6764 { "interpose", &md_interpose }, 7387 { "interpose", &md_interpose },
6765 { NULL, NULL }, 7388 { NULL, NULL },
@@ -6768,7 +7391,7 @@ static const luaL_Reg md_globals[] = {
6768int luaopen__openssl_digest(lua_State *L) { 7391int luaopen__openssl_digest(lua_State *L) {
6769 initall(L); 7392 initall(L);
6770 7393
6771 luaL_newlib(L, md_globals); 7394 auxL_newlib(L, md_globals, 0);
6772 7395
6773 return 1; 7396 return 1;
6774} /* luaopen__openssl_digest() */ 7397} /* luaopen__openssl_digest() */
@@ -6850,18 +7473,18 @@ static int hmac__gc(lua_State *L) {
6850} /* hmac__gc() */ 7473} /* hmac__gc() */
6851 7474
6852 7475
6853static const luaL_Reg hmac_methods[] = { 7476static const auxL_Reg hmac_methods[] = {
6854 { "update", &hmac_update }, 7477 { "update", &hmac_update },
6855 { "final", &hmac_final }, 7478 { "final", &hmac_final },
6856 { NULL, NULL }, 7479 { NULL, NULL },
6857}; 7480};
6858 7481
6859static const luaL_Reg hmac_metatable[] = { 7482static const auxL_Reg hmac_metatable[] = {
6860 { "__gc", &hmac__gc }, 7483 { "__gc", &hmac__gc },
6861 { NULL, NULL }, 7484 { NULL, NULL },
6862}; 7485};
6863 7486
6864static const luaL_Reg hmac_globals[] = { 7487static const auxL_Reg hmac_globals[] = {
6865 { "new", &hmac_new }, 7488 { "new", &hmac_new },
6866 { "interpose", &hmac_interpose }, 7489 { "interpose", &hmac_interpose },
6867 { NULL, NULL }, 7490 { NULL, NULL },
@@ -6870,7 +7493,7 @@ static const luaL_Reg hmac_globals[] = {
6870int luaopen__openssl_hmac(lua_State *L) { 7493int luaopen__openssl_hmac(lua_State *L) {
6871 initall(L); 7494 initall(L);
6872 7495
6873 luaL_newlib(L, hmac_globals); 7496 auxL_newlib(L, hmac_globals, 0);
6874 7497
6875 return 1; 7498 return 1;
6876} /* luaopen__openssl_hmac() */ 7499} /* luaopen__openssl_hmac() */
@@ -7051,7 +7674,7 @@ static int cipher__gc(lua_State *L) {
7051} /* cipher__gc() */ 7674} /* cipher__gc() */
7052 7675
7053 7676
7054static const luaL_Reg cipher_methods[] = { 7677static const auxL_Reg cipher_methods[] = {
7055 { "encrypt", &cipher_encrypt }, 7678 { "encrypt", &cipher_encrypt },
7056 { "decrypt", &cipher_decrypt }, 7679 { "decrypt", &cipher_decrypt },
7057 { "update", &cipher_update }, 7680 { "update", &cipher_update },
@@ -7059,12 +7682,12 @@ static const luaL_Reg cipher_methods[] = {
7059 { NULL, NULL }, 7682 { NULL, NULL },
7060}; 7683};
7061 7684
7062static const luaL_Reg cipher_metatable[] = { 7685static const auxL_Reg cipher_metatable[] = {
7063 { "__gc", &cipher__gc }, 7686 { "__gc", &cipher__gc },
7064 { NULL, NULL }, 7687 { NULL, NULL },
7065}; 7688};
7066 7689
7067static const luaL_Reg cipher_globals[] = { 7690static const auxL_Reg cipher_globals[] = {
7068 { "new", &cipher_new }, 7691 { "new", &cipher_new },
7069 { "interpose", &cipher_interpose }, 7692 { "interpose", &cipher_interpose },
7070 { NULL, NULL }, 7693 { NULL, NULL },
@@ -7073,7 +7696,7 @@ static const luaL_Reg cipher_globals[] = {
7073int luaopen__openssl_cipher(lua_State *L) { 7696int luaopen__openssl_cipher(lua_State *L) {
7074 initall(L); 7697 initall(L);
7075 7698
7076 luaL_newlib(L, cipher_globals); 7699 auxL_newlib(L, cipher_globals, 0);
7077 7700
7078 return 1; 7701 return 1;
7079} /* luaopen__openssl_cipher() */ 7702} /* luaopen__openssl_cipher() */
@@ -7402,7 +8025,7 @@ static int rand_uniform(lua_State *L) {
7402} /* rand_uniform() */ 8025} /* rand_uniform() */
7403 8026
7404 8027
7405static const luaL_Reg rand_globals[] = { 8028static const auxL_Reg rand_globals[] = {
7406 { "stir", &rand_stir }, 8029 { "stir", &rand_stir },
7407 { "add", &rand_add }, 8030 { "add", &rand_add },
7408 { "bytes", &rand_bytes }, 8031 { "bytes", &rand_bytes },
@@ -7416,10 +8039,9 @@ int luaopen__openssl_rand(lua_State *L) {
7416 8039
7417 initall(L); 8040 initall(L);
7418 8041
7419 luaL_newlibtable(L, rand_globals);
7420 st = lua_newuserdata(L, sizeof *st); 8042 st = lua_newuserdata(L, sizeof *st);
7421 memset(st, 0, sizeof *st); 8043 memset(st, 0, sizeof *st);
7422 luaL_setfuncs(L, rand_globals, 1); 8044 auxL_newlib(L, rand_globals, 1);
7423 8045
7424 return 1; 8046 return 1;
7425} /* luaopen__openssl_rand() */ 8047} /* luaopen__openssl_rand() */
@@ -7454,7 +8076,7 @@ static int de5_set_odd_parity(lua_State *L) {
7454 return 1; 8076 return 1;
7455} /* de5_set_odd_parity() */ 8077} /* de5_set_odd_parity() */
7456 8078
7457static const luaL_Reg des_globals[] = { 8079static const auxL_Reg des_globals[] = {
7458 { "string_to_key", &de5_string_to_key }, 8080 { "string_to_key", &de5_string_to_key },
7459 { "set_odd_parity", &de5_set_odd_parity }, 8081 { "set_odd_parity", &de5_set_odd_parity },
7460 { NULL, NULL }, 8082 { NULL, NULL },
@@ -7463,7 +8085,7 @@ static const luaL_Reg des_globals[] = {
7463int luaopen__openssl_des(lua_State *L) { 8085int luaopen__openssl_des(lua_State *L) {
7464 initall(L); 8086 initall(L);
7465 8087
7466 luaL_newlib(L, des_globals); 8088 auxL_newlib(L, des_globals, 0);
7467 8089
7468 return 1; 8090 return 1;
7469} /* luaopen__openssl_des() */ 8091} /* luaopen__openssl_des() */
@@ -7608,21 +8230,24 @@ static void initall(lua_State *L) {
7608 8230
7609 ex_newstate(L); 8231 ex_newstate(L);
7610 8232
7611 addclass(L, BIGNUM_CLASS, bn_methods, bn_metatable); 8233 auxL_addclass(L, BIGNUM_CLASS, bn_methods, bn_metatable, 0);
7612 addclass(L, PKEY_CLASS, pk_methods, pk_metatable); 8234 pk_luainit(L, 0);
7613 addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable); 8235#ifndef OPENSSL_NO_EC
7614 addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable); 8236 auxL_addclass(L, EC_GROUP_CLASS, ecg_methods, ecg_metatable, 0);
7615 addclass(L, X509_EXT_CLASS, xe_methods, xe_metatable); 8237#endif
7616 addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable); 8238 auxL_addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable, 0);
7617 addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable); 8239 auxL_addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable, 0);
7618 addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); 8240 auxL_addclass(L, X509_EXT_CLASS, xe_methods, xe_metatable, 0);
7619 addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable); 8241 auxL_addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable, 0);
7620 addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable); 8242 auxL_addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable, 0);
7621 addclass(L, PKCS12_CLASS, p12_methods, p12_metatable); 8243 auxL_addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable, 0);
7622 addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable); 8244 auxL_addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable, 0);
7623 addclass(L, SSL_CLASS, ssl_methods, ssl_metatable); 8245 auxL_addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable, 0);
7624 addclass(L, DIGEST_CLASS, md_methods, md_metatable); 8246 auxL_addclass(L, PKCS12_CLASS, p12_methods, p12_metatable, 0);
7625 addclass(L, HMAC_CLASS, hmac_methods, hmac_metatable); 8247 auxL_addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable, 0);
7626 addclass(L, CIPHER_CLASS, cipher_methods, cipher_metatable); 8248 auxL_addclass(L, SSL_CLASS, ssl_methods, ssl_metatable, 0);
8249 auxL_addclass(L, DIGEST_CLASS, md_methods, md_metatable, 0);
8250 auxL_addclass(L, HMAC_CLASS, hmac_methods, hmac_metatable, 0);
8251 auxL_addclass(L, CIPHER_CLASS, cipher_methods, cipher_metatable, 0);
7627} /* initall() */ 8252} /* initall() */
7628 8253