forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxname.c
More file actions
485 lines (420 loc) · 12.1 KB
/
xname.c
File metadata and controls
485 lines (420 loc) · 12.1 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/***
x509.name module to mapping X509_NAME to lua object.
@module x509.name
@usage
name = require('openssl').x509.name
*/
#include "openssl.h"
#include "private.h"
#include "sk.h"
int
openssl_push_xname_asobject(lua_State *L, X509_NAME *xname)
{
const X509_NAME *dup = X509_NAME_dup(xname);
PUSH_OBJECT(dup, "openssl.x509_name");
return 1;
}
static X509_NAME *
openssl_new_xname(lua_State *L, int idx, int utf8)
{
int i, n, ret;
X509_NAME *xn = X509_NAME_new();
for (i = 0, n = lua_rawlen(L, idx), ret = 1; i < n && ret == 1; i++) {
lua_rawgeti(L, idx, i + 1);
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
size_t size;
ASN1_OBJECT *obj = openssl_get_asn1object(L, -2, 1);
const char *value = luaL_checklstring(L, -1, &size);
if (obj == NULL) {
ret = 0;
break;
}
ret = X509_NAME_add_entry_by_OBJ(
xn, obj, utf8 ? MBSTRING_UTF8 : MBSTRING_ASC, (unsigned char *)value, (int)size, -1, 0);
ASN1_OBJECT_free(obj);
if (ret != 1) break;
lua_pop(L, 1);
}
}
if (ret != 1) {
X509_NAME_free(xn);
xn = NULL;
lua_pushfstring(L,
"can't add to openssl.x509_name with value (%s=%s) at %d of #%d table arg",
lua_tostring(L, -2),
lua_tostring(L, -1),
i + 1,
idx);
}
return xn;
}
/***
Create x509_name object
@function new
@tparam table array include name node
@tparam[opt] boolean utf8 encode will be use default
@treturn openssl.x509_name mapping to X509_EXTENSION in openssl
@usage
name = require'openssl'.x509.name
subject = name.new{
{C='CN'},
{O='kkhub.com'},
{CN='zhaozg'}
}
*/
static int
openssl_xname_new(lua_State *L)
{
X509_NAME *xn;
int utf8, ret = 1;
luaL_checktable(L, 1);
luaL_argcheck(L, lua_rawlen(L, 1) > 0, 1, "must be not empty table as array");
utf8 = lua_isnone(L, 2) ? 1 : lua_toboolean(L, 2);
xn = openssl_new_xname(L, 1, utf8);
if (xn) {
PUSH_OBJECT(xn, "openssl.x509_name");
} else {
lua_pushnil(L);
lua_insert(L, lua_gettop(L) - 1);
ret = 2;
}
/* if xn is NULL, top will be error string */
return ret;
};
/***
Create x509_name from der string
@function d2i
@tparam string content DER encoded string
@treturn openssl.x509_name mapping to X509_NAME in openssl
*/
static int
openssl_xname_d2i(lua_State *L)
{
int ret = 0;
size_t len;
const unsigned char *dat = (const unsigned char *)luaL_checklstring(L, 1, &len);
X509_NAME *xn = d2i_X509_NAME(NULL, &dat, len);
if (xn) {
PUSH_OBJECT(xn, "openssl.x509_name");
ret = 1;
}
return ret ? ret : openssl_pushresult(L, 0);
};
static luaL_Reg R[] = {
{ "new", openssl_xname_new },
{ "d2i", openssl_xname_d2i },
{ NULL, NULL },
};
/***
x509_name infomation table
other field is number type, and value table is alter name.(I not understand clearly)
@table x509_extension_info_table
@tfield asn1_object|object object of x509_name
@tfield boolean|critical true for critical value
@tfield string|value as octet string
*/
/***
openssl.x509_name object
@type x509_name
*/
static int
openssl_xname_gc(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
X509_NAME_free(xn);
return 0;
}
/***
get oneline of x509_name.
@function oneline
@treturn string line, name as oneline text
*/
static int
openssl_xname_oneline(lua_State *L)
{
X509_NAME *xname = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
char *p = X509_NAME_oneline(xname, NULL, 0);
lua_pushstring(L, p);
OPENSSL_free(p);
return 1;
};
/***
get hash code of x509_name
@function hash
@treturn integer hash hash code of x509_name
*/
static int
openssl_xname_hash(lua_State *L)
{
X509_NAME *xname = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
#if OPENSSL_VERSION_NUMBER < 0x30000000
unsigned long hash = X509_NAME_hash(xname);
#else
unsigned long hash = X509_NAME_hash_ex(xname, NULL, NULL, NULL);
#endif
lua_pushnumber(L, hash);
return 1;
};
/***
get digest of x509_name
@function digest
@tparam string|nid|openssl.evp_md md method of digest
@treturn string digest digest value by given alg of x509_name
*/
static int
openssl_xname_digest(lua_State *L)
{
X509_NAME *xname = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
const EVP_MD *md = get_digest(L, 2, NULL);
unsigned char buf[EVP_MAX_MD_SIZE];
unsigned int len = sizeof(buf);
int ret = X509_NAME_digest(xname, md, buf, &len);
if (ret == 1) lua_pushlstring(L, (const char *)buf, len);
return ret == 1 ? ret : openssl_pushresult(L, ret);
};
/***
print x509_name to bio object
@function print
@tparam openssl.bio out output bio object
@tparam[opt] integer indent for output
@tparam[opt] integer flags for output
@treturn boolean result, follow by error message
*/
static int
openssl_xname_toprint(lua_State *L)
{
X509_NAME *xname = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
int indent = luaL_optint(L, 2, 0);
unsigned long flags = luaL_optinteger(L, 3, 0);
BIO *out = BIO_new(BIO_s_mem());
int ret = X509_NAME_print_ex(out, xname, indent, flags);
if (ret == 1) {
BUF_MEM *mem;
BIO_get_mem_ptr(out, &mem);
lua_pushlstring(L, mem->data, mem->length);
}
BIO_free(out);
return ret == 1 ? ret : openssl_pushresult(L, ret);
};
static int
openssl_push_xname_entry(lua_State *L, X509_NAME_ENTRY *ne, int obj)
{
ASN1_OBJECT *object = X509_NAME_ENTRY_get_object(ne);
ASN1_STRING *value = X509_NAME_ENTRY_get_data(ne);
lua_newtable(L);
if (obj) {
openssl_push_asn1object(L, object);
PUSH_ASN1_STRING(L, value);
} else {
lua_pushstring(L, OBJ_nid2sn(OBJ_obj2nid(object)));
lua_pushlstring(L, (const char *)ASN1_STRING_get0_data(value), ASN1_STRING_length(value));
}
lua_settable(L, -3);
return 1;
}
/***
return x509_name as table
@function info
@tparam[opt=false] boolean asobject table key will use asn1_object or short name of asn1_object
@treturn table names
-- @see OpenSSL function: X509_NAME_get_index_by_NID
*/
static int
openssl_xname_info(lua_State *L)
{
X509_NAME *name = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
int obj = lua_isnone(L, 2) ? 0 : lua_toboolean(L, 2);
int i, n;
lua_newtable(L);
for (i = 0, n = X509_NAME_entry_count(name); i < n; i++) {
X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
openssl_push_xname_entry(L, entry, obj);
lua_rawseti(L, -2, i + 1);
}
return 1;
};
/***
compare two x509_name
@function cmp
@tparam openssl.x509_name another to compare with
@treturn boolean result true for equal or false
@usage
name1 = name.new({...})
name2 = name1:dup()
assert(name1:cmp(name2)==(name1==name2))
*/
static int
openssl_xname_cmp(lua_State *L)
{
X509_NAME *a = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
X509_NAME *b = CHECK_OBJECT(2, X509_NAME, "openssl.x509_name");
int ret = X509_NAME_cmp(a, b);
lua_pushboolean(L, ret == 0);
return 1;
};
/***
make a clone of x509_name
@function dup
@treturn openssl.x509_name clone
*/
static int
openssl_xname_dup(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
X509_NAME *dup = X509_NAME_dup(xn);
PUSH_OBJECT(dup, "openssl.x509_name");
return 1;
};
/***
get DER encoded string of x509_name.
@function i2d
@treturn string der
*/
static int
openssl_xname_i2d(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
unsigned char *out = NULL;
int ret = i2d_X509_NAME(xn, &out);
if (ret > 0) {
lua_pushlstring(L, (const char *)out, ret);
OPENSSL_free(out);
ret = 1;
}
return ret > 0 ? ret : openssl_pushresult(L, ret);
};
/***
get count in x509_name.
@function entry_count
@treturn integer count of x509_name
*/
static int
openssl_xname_entry_count(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
int len = X509_NAME_entry_count(xn);
lua_pushinteger(L, len);
return 1;
};
/***
get text by given asn1_object or nid
@function get_text
@tparam string|integer|asn1_object identid for asn1_object
@tparam[opt=-1] number lastpos retrieve the next index after lastpos
@treturn string text and followed by lastpos
*/
static int
openssl_xname_get_text(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
ASN1_OBJECT *obj = openssl_get_asn1object(L, 2, 0);
int lastpos = luaL_optint(L, 3, -1);
X509_NAME_ENTRY *e;
ASN1_STRING *s;
int ret = 0;
lastpos = X509_NAME_get_index_by_OBJ(xn, obj, lastpos);
ASN1_OBJECT_free(obj);
if (lastpos != -1) {
e = X509_NAME_get_entry(xn, lastpos);
s = X509_NAME_ENTRY_get_data(e);
lua_pushlstring(L, (const char *)ASN1_STRING_get0_data(s), ASN1_STRING_length(s));
lua_pushinteger(L, lastpos);
ret = 2;
}
return ret;
};
/***
get x509 name entry by index
@function get_entry
@tparam integer index start from 0, and less than xn:entry_count()
@tparam[opt=false] boolean asobject table key will use asn1_object or short name of asn1_object
@treturn openssl.x509 name entry table
*/
static int
openssl_xname_get_entry(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
int lastpos = luaL_checkint(L, 2);
int obj = lua_isnone(L, 3) ? 0 : lua_toboolean(L, 3);
X509_NAME_ENTRY *e = X509_NAME_get_entry(xn, lastpos);
int ret = 0;
if (e) {
ret = openssl_push_xname_entry(L, e, obj);
}
return ret;
};
/***
add name entry
@function add_entry
@tparam string|integer|asn1_object identid for asn1_object
@tparam string data to add
@tparam[opt=true] boolean utf8 true use utf8 encode, or use ascii encode
@treturn boolean result true for success or follow by error message
*/
static int
openssl_xname_add_entry(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
ASN1_OBJECT *obj = openssl_get_asn1object(L, 2, 0);
size_t size;
const char *value = luaL_checklstring(L, 3, &size);
int utf8 = lua_isnone(L, 4) ? 1 : lua_toboolean(L, 4);
int ret = X509_NAME_add_entry_by_OBJ(
xn, obj, utf8 ? MBSTRING_UTF8 : MBSTRING_ASC, (unsigned char *)value, (int)size, -1, 0);
ASN1_OBJECT_free(obj);
return openssl_pushresult(L, ret);
};
/***
get index by give asn1_object or nid
@function delete_entry
@tparam integer location which name entry to delete
@treturn[1] asn1_object object that delete name entry
@treturn[1] asn1_string value that delete name entry
@treturn[2] nil delete nothing
*/
static int
openssl_xname_delete_entry(lua_State *L)
{
X509_NAME *xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
int loc = luaL_checkint(L, 2);
int ret = 0;
X509_NAME_ENTRY *xe = X509_NAME_delete_entry(xn, loc);
if (xe) {
openssl_push_asn1object(L, X509_NAME_ENTRY_get_object(xe));
PUSH_ASN1_STRING(L, X509_NAME_ENTRY_get_data(xe));
X509_NAME_ENTRY_free(xe);
ret = 2;
}
return ret;
};
static luaL_Reg xname_funcs[] = {
{ "oneline", openssl_xname_oneline },
{ "hash", openssl_xname_hash },
{ "digest", openssl_xname_digest },
{ "toprint", openssl_xname_toprint },
{ "info", openssl_xname_info },
{ "dup", openssl_xname_dup },
{ "i2d", openssl_xname_i2d },
{ "entry_count", openssl_xname_entry_count },
{ "get_text", openssl_xname_get_text },
{ "get_entry", openssl_xname_get_entry },
{ "add_entry", openssl_xname_add_entry },
{ "delete_entry", openssl_xname_delete_entry },
{ "cmp", openssl_xname_cmp },
{ "tostring", openssl_xname_oneline },
{ "__eq", openssl_xname_cmp },
{ "__len", openssl_xname_entry_count },
{ "__tostring", auxiliar_tostring },
{ "__gc", openssl_xname_gc },
{ NULL, NULL },
};
IMP_LUA_SK(X509_NAME, x509_name)
int
openssl_register_xname(lua_State *L)
{
auxiliar_newclass(L, "openssl.x509_name", xname_funcs);
lua_newtable(L);
luaL_setfuncs(L, R, 0);
return 1;
}