summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/openssl.c153
1 files changed, 39 insertions, 114 deletions
diff --git a/src/openssl.c b/src/openssl.c
index b14090e..e5b55ca 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -201,14 +201,6 @@
201#define HAVE_EVP_PKEY_ID OPENSSL_PREREQ(1,1,0) 201#define HAVE_EVP_PKEY_ID OPENSSL_PREREQ(1,1,0)
202#endif 202#endif
203 203
204#ifndef HAVE_GENERAL_NAME_GET0_VALUE
205#define HAVE_GENERAL_NAME_GET0_VALUE OPENSSL_PREREQ(1,1,0)
206#endif
207
208#ifndef HAVE_GENERAL_NAME_SET0_VALUE
209#define HAVE_GENERAL_NAME_SET0_VALUE OPENSSL_PREREQ(1,1,0)
210#endif
211
212#ifndef HAVE_HMAC_CTX_FREE 204#ifndef HAVE_HMAC_CTX_FREE
213#define HAVE_HMAC_CTX_FREE OPENSSL_PREREQ(1,1,0) 205#define HAVE_HMAC_CTX_FREE OPENSSL_PREREQ(1,1,0)
214#endif 206#endif
@@ -830,8 +822,9 @@ NOTUSED static auxtype_t auxL_getref(lua_State *L, auxref_t ref) {
830static int auxL_testoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) { 822static int auxL_testoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) {
831 const char *optname = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index); 823 const char *optname = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
832 int (*optcmp)() = (nocase)? &strcasecmp : &strcmp; 824 int (*optcmp)() = (nocase)? &strcasecmp : &strcmp;
825 int i;
833 826
834 for (int i = 0; optlist[i]; i++) { 827 for (i = 0; optlist[i]; i++) {
835 if (0 == optcmp(optlist[i], optname)) 828 if (0 == optcmp(optlist[i], optname))
836 return i; 829 return i;
837 } 830 }
@@ -987,9 +980,11 @@ static inline size_t auxL_liblen(const auxL_Reg *l) {
987 980
988static void auxL_setfuncs(lua_State *L, const auxL_Reg *l, int nups) { 981static void auxL_setfuncs(lua_State *L, const auxL_Reg *l, int nups) {
989 for (; l->name; l++) { 982 for (; l->name; l++) {
983 int i;
984
990 /* copy shared upvalues */ 985 /* copy shared upvalues */
991 luaL_checkstack(L, nups, "too many upvalues"); 986 luaL_checkstack(L, nups, "too many upvalues");
992 for (int i = 0; i < nups; i++) 987 for (i = 0; i < nups; i++)
993 lua_pushvalue(L, -nups); 988 lua_pushvalue(L, -nups);
994 989
995 /* nil-fill local upvalues */ 990 /* nil-fill local upvalues */
@@ -1450,68 +1445,6 @@ static void *compat_EVP_PKEY_get0(EVP_PKEY *key) {
1450} /* compat_EVP_PKEY_get0() */ 1445} /* compat_EVP_PKEY_get0() */
1451#endif 1446#endif
1452 1447
1453#if !HAVE_GENERAL_NAME_GET0_VALUE
1454#define GENERAL_NAME_get0_value(...) \
1455 compat_GENERAL_NAME_get0_value(__VA_ARGS__)
1456
1457static void *GENERAL_NAME_get0_value(GENERAL_NAME *name, int *type) {
1458 if (type)
1459 *type = name->type;
1460 switch (name->type) {
1461 case GEN_X400:
1462 case GEN_EDIPARTY:
1463 return name->d.other;
1464 case GEN_OTHERNAME:
1465 return name->d.otherName;
1466 case GEN_EMAIL:
1467 case GEN_DNS:
1468 case GEN_URI:
1469 return name->d.ia5;
1470 case GEN_DIRNAME:
1471 return name->d.dirn;
1472 case GEN_IPADD:
1473 return name->d.ip;
1474 case GEN_RID:
1475 return name->d.rid;
1476 default:
1477 return NULL;
1478 }
1479} /* compat_GENERAL_NAME_get0_value() */
1480#endif
1481
1482#if !HAVE_GENERAL_NAME_SET0_VALUE
1483#define GENERAL_NAME_set0_value(...) \
1484 compat_GENERAL_NAME_set0_value(__VA_ARGS__)
1485
1486static void GENERAL_NAME_set0_value(GENERAL_NAME *name, int type, void *value) {
1487 switch ((name->type = type)) {
1488 case GEN_X400:
1489 case GEN_EDIPARTY:
1490 name->d.other = value;
1491 break;
1492 case GEN_OTHERNAME:
1493 name->d.otherName = value;
1494 break;
1495 case GEN_EMAIL:
1496 case GEN_DNS:
1497 case GEN_URI:
1498 name->d.ia5 = value;
1499 break;
1500 case GEN_DIRNAME:
1501 name->d.dirn = value;
1502 break;
1503 case GEN_IPADD:
1504 name->d.ip = value;
1505 break;
1506 case GEN_RID:
1507 name->d.rid = value;
1508 break;
1509 default:
1510 break;
1511 }
1512} /* compat_GENERAL_NAME_set0_value() */
1513#endif
1514
1515#if !HAVE_HMAC_CTX_FREE 1448#if !HAVE_HMAC_CTX_FREE
1516#define HMAC_CTX_free(ctx) compat_HMAC_CTX_free((ctx)) 1449#define HMAC_CTX_free(ctx) compat_HMAC_CTX_free((ctx))
1517 1450
@@ -2568,7 +2501,7 @@ static BN_CTX *getctx(lua_State *L) {
2568} /* getctx() */ 2501} /* getctx() */
2569 2502
2570 2503
2571static int bn_tobin(lua_State *L) { 2504static int bn_toBinary(lua_State *L) {
2572 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); 2505 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS);
2573 size_t len; 2506 size_t len;
2574 void *dst; 2507 void *dst;
@@ -2579,7 +2512,7 @@ static int bn_tobin(lua_State *L) {
2579 lua_pushlstring(L, dst, len); 2512 lua_pushlstring(L, dst, len);
2580 2513
2581 return 1; 2514 return 1;
2582} /* bn_tobin() */ 2515} /* bn_toBinary() */
2583 2516
2584 2517
2585static int bn__add(lua_State *L) { 2518static int bn__add(lua_State *L) {
@@ -2810,7 +2743,7 @@ static int bn_isPrime(lua_State *L) {
2810 2743
2811static BIO *getbio(lua_State *); 2744static BIO *getbio(lua_State *);
2812 2745
2813static int bn_todec(lua_State *L) { 2746static int bn_toDecimal(lua_State *L) {
2814 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); 2747 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS);
2815 char *txt = NULL; 2748 char *txt = NULL;
2816 BIO *bio; 2749 BIO *bio;
@@ -2835,11 +2768,11 @@ static int bn_todec(lua_State *L) {
2835sslerr: 2768sslerr:
2836 OPENSSL_free(txt); 2769 OPENSSL_free(txt);
2837 2770
2838 return auxL_error(L, auxL_EOPENSSL, "bignum:todec"); 2771 return auxL_error(L, auxL_EOPENSSL, "bignum:toDecimal");
2839} /* bn_todec() */ 2772} /* bn_toDecimal() */
2840 2773
2841 2774
2842static int bn_tohex(lua_State *L) { 2775static int bn_toHex(lua_State *L) {
2843 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); 2776 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS);
2844 char *txt = NULL; 2777 char *txt = NULL;
2845 BIO *bio; 2778 BIO *bio;
@@ -2864,27 +2797,31 @@ static int bn_tohex(lua_State *L) {
2864sslerr: 2797sslerr:
2865 OPENSSL_free(txt); 2798 OPENSSL_free(txt);
2866 2799
2867 return auxL_error(L, auxL_EOPENSSL, "bignum:tohex"); 2800 return auxL_error(L, auxL_EOPENSSL, "bignum:toHex");
2868} /* bn_tohex() */ 2801} /* bn_toHex() */
2869 2802
2870 2803
2871static const auxL_Reg bn_methods[] = { 2804static const auxL_Reg bn_methods[] = {
2872 { "add", &bn__add }, 2805 { "add", &bn__add },
2873 { "sub", &bn__sub }, 2806 { "sub", &bn__sub },
2874 { "mul", &bn__mul }, 2807 { "mul", &bn__mul },
2875 { "sqr", &bn_sqr }, 2808 { "sqr", &bn_sqr },
2876 { "idiv", &bn__idiv }, 2809 { "idiv", &bn__idiv },
2877 { "mod", &bn__mod }, 2810 { "mod", &bn__mod },
2878 { "nnmod", &bn_nnmod }, 2811 { "nnmod", &bn_nnmod },
2879 { "exp", &bn__pow }, 2812 { "exp", &bn__pow },
2880 { "gcd", &bn_gcd }, 2813 { "gcd", &bn_gcd },
2881 { "lshift", &bn__shl }, 2814 { "lshift", &bn__shl },
2882 { "rshift", &bn__shr }, 2815 { "rshift", &bn__shr },
2883 { "isPrime", &bn_isPrime }, 2816 { "isPrime", &bn_isPrime },
2884 { "tobin", &bn_tobin }, 2817 { "toBinary", &bn_toBinary },
2885 { "todec", &bn_todec }, 2818 { "toDecimal", &bn_toDecimal },
2886 { "tohex", &bn_tohex }, 2819 { "toHex", &bn_toHex },
2887 { NULL, NULL }, 2820 /* deprecated */
2821 { "tobin", &bn_toBinary },
2822 { "todec", &bn_toDecimal },
2823 { "tohex", &bn_toHex },
2824 { NULL, NULL },
2888}; 2825};
2889 2826
2890static const auxL_Reg bn_metatable[] = { 2827static const auxL_Reg bn_metatable[] = {
@@ -2902,7 +2839,7 @@ static const auxL_Reg bn_metatable[] = {
2902 { "__lt", &bn__lt }, 2839 { "__lt", &bn__lt },
2903 { "__le", &bn__le }, 2840 { "__le", &bn__le },
2904 { "__gc", &bn__gc }, 2841 { "__gc", &bn__gc },
2905 { "__tostring", &bn_todec }, 2842 { "__tostring", &bn_toDecimal },
2906 { NULL, NULL }, 2843 { NULL, NULL },
2907}; 2844};
2908 2845
@@ -3871,13 +3808,15 @@ static int pk_getParameters(lua_State *L) {
3871 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type); 3808 return luaL_error(L, "%d: unsupported EVP_PKEY base type", base_type);
3872 3809
3873 if (lua_isnoneornil(L, 2)) { 3810 if (lua_isnoneornil(L, 2)) {
3811 const char *const *optname;
3812
3874 /* 3813 /*
3875 * Use special "{" parameter to tell loop to push table. 3814 * Use special "{" parameter to tell loop to push table.
3876 * Subsequent parameters will be assigned as fields. 3815 * Subsequent parameters will be assigned as fields.
3877 */ 3816 */
3878 lua_pushstring(L, "{"); 3817 lua_pushstring(L, "{");
3879 luaL_checkstack(L, nopts, "too many arguments"); 3818 luaL_checkstack(L, nopts, "too many arguments");
3880 for (const char *const *optname = optlist; *optname; optname++) { 3819 for (optname = optlist; *optname; optname++) {
3881 lua_pushstring(L, *optname); 3820 lua_pushstring(L, *optname);
3882 } 3821 }
3883 } 3822 }
@@ -4057,11 +3996,12 @@ static const auxL_Reg pk_globals[] = {
4057}; 3996};
4058 3997
4059static void pk_luainit(lua_State *L, _Bool reset) { 3998static void pk_luainit(lua_State *L, _Bool reset) {
3999 char **k;
4060 if (!auxL_newmetatable(L, PKEY_CLASS, reset)) 4000 if (!auxL_newmetatable(L, PKEY_CLASS, reset))
4061 return; 4001 return;
4062 auxL_setfuncs(L, pk_metatable, 0); 4002 auxL_setfuncs(L, pk_metatable, 0);
4063 auxL_newlib(L, pk_methods, 0); 4003 auxL_newlib(L, pk_methods, 0);
4064 for (char **k = (char *[]){ "__index", "__newindex", 0 }; *k; k++) { 4004 for (k = (char *[]){ "__index", "__newindex", 0 }; *k; k++) {
4065 lua_getfield(L, -2, *k); /* closure */ 4005 lua_getfield(L, -2, *k); /* closure */
4066 lua_pushvalue(L, -2); /* method table */ 4006 lua_pushvalue(L, -2); /* method table */
4067 lua_setupvalue(L, -2, 1); 4007 lua_setupvalue(L, -2, 1);
@@ -4110,21 +4050,6 @@ static EC_GROUP *ecg_dup_nil(lua_State *L, const EC_GROUP *src) {
4110 return (src)? ecg_dup(L, src) : (lua_pushnil(L), (EC_GROUP *)0); 4050 return (src)? ecg_dup(L, src) : (lua_pushnil(L), (EC_GROUP *)0);
4111} /* ecg_dup_nil() */ 4051} /* ecg_dup_nil() */
4112 4052
4113static EC_GROUP *ecg_new_by_nid(int nid) {
4114 EC_GROUP *group;
4115
4116 if (!(group = EC_GROUP_new_by_curve_name(nid)))
4117 return NULL;
4118
4119 /* flag as named for benefit of __tostring */
4120 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
4121
4122 /* compressed points may be patented */
4123 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
4124
4125 return group;
4126} /* ecg_new_by_nid() */
4127
4128static EC_GROUP *ecg_push_by_nid(lua_State *L, int nid) { 4053static EC_GROUP *ecg_push_by_nid(lua_State *L, int nid) {
4129 EC_GROUP **group = prepsimple(L, EC_GROUP_CLASS); 4054 EC_GROUP **group = prepsimple(L, EC_GROUP_CLASS);
4130 4055