forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlhash.c
More file actions
231 lines (205 loc) · 5.84 KB
/
lhash.c
File metadata and controls
231 lines (205 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* lhash.c
* openssl lhash object for lua-openssl binding
*
* Author: george zhao <zhaozg(at)gmail.com>
*/
/***
lhash module for lua-openssl binding
LHASH is OpenSSL's implementation of a hash table. It provides a
generic hash table that can be used to store arbitrary data using
string keys. This module provides Lua bindings for the LHASH functionality.
@module lhash
@usage
lhash = require('openssl').lhash
*/
#include <openssl/conf.h>
#include "openssl.h"
#include "private.h"
#if 0
static void table2data(lua_State*L, int idx, BIO* bio)
{
lua_pushnil(L);
while (lua_next(L, idx))
{
const char * key = lua_tostring(L, -2);
if (lua_istable(L, -1))
{
BIO_printf(bio, "[%s]\n", key);
table2data(L, lua_gettop(L), bio);
}
else
{
const char * val = lua_tostring(L, -1);
BIO_printf(bio, "%s=%s\n", key, val);
}
lua_pop(L, 1);
}
}
#endif
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x30900000L
/***
read configuration hash from BIO object
@function read
@tparam openssl.bio bio BIO object to read from
@treturn lhash configuration hash object
*/
static int openssl_lhash_read(lua_State *L)
{
long eline = -1;
BIO *bio = load_bio_object(L, 1);
LHASH *lhash = CONF_load_bio(NULL, bio, &eline);
BIO_free(bio);
if (lhash) {
PUSH_OBJECT(lhash, "openssl.lhash");
return 1;
} else {
lua_pushfstring(L, "ERROR at LINE %d", eline);
return luaL_argerror(L, 1, lua_tostring(L, -1));
}
}
/***
load configuration hash from file
@function load
@tparam string conf_file path to configuration file
@treturn lhash configuration hash object
*/
static int openssl_lhash_load(lua_State *L)
{
long eline = -1;
const char *conf = luaL_checkstring(L, 1);
BIO *bio = BIO_new_file(conf, "r");
LHASH *lhash = CONF_load_bio(NULL, bio, &eline);
BIO_free(bio);
if (lhash)
PUSH_OBJECT(lhash, "openssl.lhash");
else {
lua_pushfstring(L, "ERROR at LINE %d", eline);
return luaL_argerror(L, 1, lua_tostring(L, -1));
}
return 1;
}
int openssl_lhash_gc(lua_State *L)
{
LHASH *lhash = CHECK_OBJECT(1, LHASH, "openssl.lhash");
CONF_free(lhash);
return 0;
}
/***
get number value from LHASH configuration
@function get_number
@tparam string group configuration group name
@tparam string name configuration key name
@treturn number configuration value as number
*/
int openssl_lhash_get_number(lua_State *L)
{
LHASH *lhash = CHECK_OBJECT(1, LHASH, "openssl.lhash");
const char *group = luaL_checkstring(L, 2);
const char *name = luaL_checkstring(L, 3);
lua_pushinteger(L, CONF_get_number(lhash, group, name));
return 1;
}
/***
get string value from LHASH configuration
@function get_string
@tparam string group configuration group name
@tparam string name configuration key name
@treturn string configuration value as string
*/
int openssl_lhash_get_string(lua_State *L)
{
LHASH *lhash = CHECK_OBJECT(1, LHASH, "openssl.lhash");
const char *group = luaL_checkstring(L, 2);
const char *name = luaL_checkstring(L, 3);
lua_pushstring(L, CONF_get_string(lhash, group, name));
return 1;
}
static void
dump_value_doall_arg(CONF_VALUE const *a, lua_State *L)
{
if (a->name) {
lua_getfield(L, -1, a->section);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_setfield(L, -2, a->section);
lua_getfield(L, -1, a->section);
}
AUXILIAR_SET(L, -1, a->name, a->value, string);
lua_pop(L, 1);
} else {
if (a->section) {
lua_getfield(L, -1, a->section);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_setfield(L, -2, a->section);
} else
lua_pop(L, 1);
} else {
AUXILIAR_SET(L, -1, a->name, a->value, string);
}
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && defined(LIBRESSL_VERSION_NUMBER) == 0
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, lua_State);
#elif OPENSSL_VERSION_NUMBER >= 0x10000002L
static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, lua_State)
#endif
#if defined(LIBRESSL_VERSION_NUMBER) == 0
#define LHM_lh_doall_arg(type, lh, fn, arg_type, arg) \
lh_doall_arg(CHECKED_LHASH_OF(type, lh), fn, CHECKED_PTR_OF(arg_type, arg))
#endif
/***
parse LHASH configuration to table
@function parse
@treturn table configuration data as key-value pairs
*/
static int openssl_lhash_parse(lua_State *L)
{
LHASH *lhash = CHECK_OBJECT(1, LHASH, "openssl.lhash");
lua_newtable(L);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && defined(LIBRESSL_VERSION_NUMBER) == 0
lh_CONF_VALUE_doall_lua_State(lhash, dump_value_doall_arg, L);
#elif OPENSSL_VERSION_NUMBER >= 0x10000002L
lh_CONF_VALUE_doall_arg(lhash, LHASH_DOALL_ARG_FN(dump_value), lua_State, L);
#else
lh_doall_arg(lhash, (LHASH_DOALL_ARG_FN_TYPE)dump_value_doall_arg, L);
#endif
return 1;
}
/***
export LHASH configuration to string
@function export
@treturn string configuration data in OpenSSL config format
*/
static int openssl_lhash_export(lua_State *L)
{
LHASH *lhash = CHECK_OBJECT(1, LHASH, "openssl.lhash");
BIO *bio = BIO_new(BIO_s_mem());
BUF_MEM *bptr = NULL;
CONF_dump_bio(lhash, bio);
BIO_get_mem_ptr(bio, &bptr);
lua_pushlstring(L, bptr->data, bptr->length);
BIO_free(bio);
return 1;
}
static luaL_Reg lhash_funs[] = {
{ "__tostring", auxiliar_tostring },
{ "__gc", openssl_lhash_gc },
{ "parse", openssl_lhash_parse },
{ "export", openssl_lhash_export },
{ "get_string", openssl_lhash_get_string },
{ "get_number", openssl_lhash_get_number },
{ NULL, NULL }
};
int
openssl_register_lhash(lua_State *L)
{
auxiliar_newclass(L, "openssl.lhash", lhash_funs);
AUXILIAR_SET(L, -1, "lhash_read", openssl_lhash_read, cfunction);
AUXILIAR_SET(L, -1, "lhash_load", openssl_lhash_load, cfunction);
return 0;
};
#endif