summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwilliam <william@25thandclement.com>2015-04-22 17:57:43 -0700
committerwilliam <william@25thandclement.com>2015-04-22 17:57:43 -0700
commit782ee9a568b34ae2c2e0837d36ac048b811becc5 (patch)
tree2f81d1dd31d1679533eb379a83e98036a1dc3b33
parent753b60b0f479e226ecdf7e781804906aa8ee369f (diff)
downloadluaossl-782ee9a568b34ae2c2e0837d36ac048b811becc5.tar.gz
luaossl-782ee9a568b34ae2c2e0837d36ac048b811becc5.tar.bz2
luaossl-782ee9a568b34ae2c2e0837d36ac048b811becc5.zip
Add openssl.version to permit querying runtime OpenSSL version.
Fix external app data in LuaJIT, which doesn't permit us to store state using a function pointer because of function equivalence issues.
-rw-r--r--src/openssl.c92
1 files changed, 83 insertions, 9 deletions
diff --git a/src/openssl.c b/src/openssl.c
index 5331501..6f41bda 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -23,7 +23,7 @@
23 * USE OR OTHER DEALINGS IN THE SOFTWARE. 23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * ========================================================================== 24 * ==========================================================================
25 */ 25 */
26#include <limits.h> /* INT_MAX INT_MIN UCHAR_MAX */ 26#include <limits.h> /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */
27#include <stdint.h> /* uintptr_t */ 27#include <stdint.h> /* uintptr_t */
28#include <string.h> /* memset(3) strerror_r(3) */ 28#include <string.h> /* memset(3) strerror_r(3) */
29#include <strings.h> /* strcasecmp(3) */ 29#include <strings.h> /* strcasecmp(3) */
@@ -519,7 +519,11 @@ static auxtype_t auxL_getref(lua_State *L, auxref_t ref) {
519 * is typically used. 519 * is typically used.
520 */ 520 */
521#define auxL_Integer long long 521#define auxL_Integer long long
522#define auxL_IntegerMin LLONG_MIN
523#define auxL_IntegerMax LLONG_MAX
522#define auxL_Unsigned unsigned long long 524#define auxL_Unsigned unsigned long long
525#define auxL_UnsignedMin 0
526#define auxL_UnsignedMax ULLONG_MAX
523 527
524#define lua_IntegerMax ((1ULL << (sizeof (lua_Integer) * 8 - 1)) - 1) 528#define lua_IntegerMax ((1ULL << (sizeof (lua_Integer) * 8 - 1)) - 1)
525#define lua_IntegerMin (-lua_IntegerMax - 1) 529#define lua_IntegerMin (-lua_IntegerMax - 1)
@@ -547,15 +551,45 @@ NOTUSED static void auxL_pushunsigned(lua_State *L, auxL_Unsigned i) {
547 } 551 }
548} /* auxL_pushunsigned() */ 552} /* auxL_pushunsigned() */
549 553
550static auxL_Integer auxL_checkinteger(lua_State *L, int index) { 554#define auxL_checkinteger_(a, b, c, d, ...) auxL_checkinteger((a), (b), (c), (d))
555#define auxL_checkinteger(...) auxL_checkinteger_(__VA_ARGS__, auxL_IntegerMin, auxL_IntegerMax, 0)
556
557static auxL_Integer (auxL_checkinteger)(lua_State *L, int index, auxL_Integer min, auxL_Integer max) {
558 auxL_Integer i;
559
551 if (sizeof (lua_Integer) >= sizeof (auxL_Integer)) { 560 if (sizeof (lua_Integer) >= sizeof (auxL_Integer)) {
552 return luaL_checkinteger(L, index); 561 i = luaL_checkinteger(L, index);
553 } else { 562 } else {
554 /* TODO: Check overflow. */ 563 /* TODO: Check overflow. */
555 return (auxL_Integer)luaL_checknumber(L, index); 564 i = (auxL_Integer)luaL_checknumber(L, index);
556 } 565 }
566
567 if (i < min || i > max)
568 luaL_error(L, "integer value out of range");
569
570 return i;
557} /* auxL_checkinteger() */ 571} /* auxL_checkinteger() */
558 572
573#define auxL_checkunsigned_(a, b, c, d, ...) auxL_checkunsigned((a), (b), (c), (d))
574#define auxL_checkunsigned(...) auxL_checkunsigned_(__VA_ARGS__, auxL_UnsignedMin, auxL_UnsignedMax, 0)
575
576static auxL_Unsigned (auxL_checkunsigned)(lua_State *L, int index, auxL_Unsigned min, auxL_Unsigned max) {
577 auxL_Unsigned i;
578
579 if (sizeof (lua_Integer) >= sizeof (auxL_Unsigned)) {
580 /* TODO: Check sign. */
581 i = luaL_checkinteger(L, index);
582 } else {
583 /* TODO: Check sign and overflow. */
584 i = (auxL_Integer)luaL_checknumber(L, index);
585 }
586
587 if (i < min || i > max)
588 luaL_error(L, "integer value out of range");
589
590 return i;
591} /* auxL_checkunsigned() */
592
559typedef struct { 593typedef struct {
560 const char *name; 594 const char *name;
561 auxL_Integer value; 595 auxL_Integer value;
@@ -1008,7 +1042,7 @@ static void ex_newstate(lua_State *L) {
1008 state->L = thr; 1042 state->L = thr;
1009#endif 1043#endif
1010 1044
1011 lua_pushcfunction(L, &ex__gc); 1045 lua_pushlightuserdata(L, (void *)&ex__gc);
1012 lua_pushvalue(L, -2); 1046 lua_pushvalue(L, -2);
1013 lua_settable(L, LUA_REGISTRYINDEX); 1047 lua_settable(L, LUA_REGISTRYINDEX);
1014 1048
@@ -1018,7 +1052,7 @@ static void ex_newstate(lua_State *L) {
1018static struct ex_state *ex_getstate(lua_State *L) { 1052static struct ex_state *ex_getstate(lua_State *L) {
1019 struct ex_state *state; 1053 struct ex_state *state;
1020 1054
1021 lua_pushcfunction(L, &ex__gc); 1055 lua_pushlightuserdata(L, (void *)&ex__gc);
1022 lua_gettable(L, LUA_REGISTRYINDEX); 1056 lua_gettable(L, LUA_REGISTRYINDEX);
1023 1057
1024 luaL_checktype(L, -1, LUA_TUSERDATA); 1058 luaL_checktype(L, -1, LUA_TUSERDATA);
@@ -1119,6 +1153,21 @@ int luaopen__openssl_compat(lua_State *L) {
1119 * 1153 *
1120 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 1154 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1121 1155
1156static int ossl_version(lua_State *L) {
1157 if (lua_isnoneornil(L, 1)) {
1158 auxL_pushunsigned(L, SSLeay());
1159 } else {
1160 lua_pushstring(L, SSLeay_version(auxL_checkinteger(L, 1, INT_MIN, INT_MAX)));
1161 }
1162
1163 return 1;
1164} /* ossl_version() */
1165
1166static const luaL_Reg ossl_globals[] = {
1167 { "version", &ossl_version },
1168 { NULL, NULL },
1169};
1170
1122/* 1171/*
1123 * NOTE: Compile-time cipher exclusions from openssl-1.0.1i/util/mkdef.pl. 1172 * NOTE: Compile-time cipher exclusions from openssl-1.0.1i/util/mkdef.pl.
1124 */ 1173 */
@@ -1258,11 +1307,34 @@ static const char opensslconf_no[][20] = {
1258 { "" } /* in case nothing is defined above */ 1307 { "" } /* in case nothing is defined above */
1259}; /* opensslconf_no[] */ 1308}; /* opensslconf_no[] */
1260 1309
1310static const auxL_IntegerReg ssleay_version[] = {
1311#ifdef SSLEAY_VERSION_NUMBER
1312 { "SSLEAY_VERSION_NUMBER", SSLEAY_VERSION_NUMBER },
1313#endif
1314#ifdef SSLEAY_VERSION
1315 { "SSLEAY_VERSION", SSLEAY_VERSION },
1316#endif
1317#ifdef SSLEAY_OPTIONS
1318 { "SSLEAY_OPTIONS", SSLEAY_OPTIONS },
1319#endif
1320#ifdef SSLEAY_CFLAGS
1321 { "SSLEAY_CFLAGS", SSLEAY_CFLAGS },
1322#endif
1323#ifdef SSLEAY_BUILT_ON
1324 { "SSLEAY_BUILT_ON", SSLEAY_BUILT_ON },
1325#endif
1326#ifdef SSLEAY_PLATFORM
1327 { "SSLEAY_PLATFORM", SSLEAY_PLATFORM },
1328#endif
1329#ifdef SSLEAY_DIR
1330 { "SSLEAY_DIR", SSLEAY_DIR },
1331#endif
1332};
1261 1333
1262int luaopen__openssl(lua_State *L) { 1334int luaopen__openssl(lua_State *L) {
1263 size_t i; 1335 size_t i;
1264 1336
1265 lua_newtable(L); 1337 luaL_newlib(L, ossl_globals);
1266 1338
1267 for (i = 0; i < countof(opensslconf_no); i++) { 1339 for (i = 0; i < countof(opensslconf_no); i++) {
1268 if (*opensslconf_no[i]) { 1340 if (*opensslconf_no[i]) {
@@ -1271,6 +1343,8 @@ int luaopen__openssl(lua_State *L) {
1271 } 1343 }
1272 } 1344 }
1273 1345
1346 auxL_setintegers(L, ssleay_version);
1347
1274 auxL_pushinteger(L, OPENSSL_VERSION_NUMBER); 1348 auxL_pushinteger(L, OPENSSL_VERSION_NUMBER);
1275 lua_setfield(L, -2, "VERSION_NUMBER"); 1349 lua_setfield(L, -2, "VERSION_NUMBER");
1276 1350
@@ -1278,10 +1352,10 @@ int luaopen__openssl(lua_State *L) {
1278 lua_setfield(L, -2, "VERSION_TEXT"); 1352 lua_setfield(L, -2, "VERSION_TEXT");
1279 1353
1280 lua_pushstring(L, SHLIB_VERSION_HISTORY); 1354 lua_pushstring(L, SHLIB_VERSION_HISTORY);
1281 lua_setfield(L, -2, "SSHLIB_VERSION_HISTORY"); 1355 lua_setfield(L, -2, "SHLIB_VERSION_HISTORY");
1282 1356
1283 lua_pushstring(L, SHLIB_VERSION_NUMBER); 1357 lua_pushstring(L, SHLIB_VERSION_NUMBER);
1284 lua_setfield(L, -2, "SSHLIB_VERSION_NUMBER"); 1358 lua_setfield(L, -2, "SHLIB_VERSION_NUMBER");
1285 1359
1286 return 1; 1360 return 1;
1287} /* luaopen__openssl() */ 1361} /* luaopen__openssl() */