1
1
/*--------------------------------------------------------------------------
4
* Copyright (C) 2014 Kim Alvefur, Paul Aurich, Tobias Markmann
5
* Matthew Wild, Bruno Silvestre.
5
7
*--------------------------------------------------------------------------*/
44
47
return ((p_x509)luaL_checkudata(L, idx, "SSL:Certificate"))->cert;
51
* Return LuaSec certificate X509 representation.
53
p_x509 lsec_checkp_x509(lua_State* L, int idx)
55
return (p_x509)luaL_checkudata(L, idx, "SSL:Certificate");
47
58
/*---------------------------------------------------------------------------*/
74
85
* Push the ASN1 string on the stack.
76
static void push_asn1_string(lua_State* L, ASN1_STRING *string)
87
static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode)
79
95
lua_pushlstring(L, (char*)ASN1_STRING_data(string),
80
96
ASN1_STRING_length(string));
98
case LSEC_UTF8_STRING:
99
len = ASN1_STRING_to_UTF8(&data, string);
101
lua_pushlstring(L, (char*)data, len);
121
143
* Retrive the general names from the object.
123
static int push_x509_name(lua_State* L, X509_NAME *name)
145
static int push_x509_name(lua_State* L, X509_NAME *name, int encode)
136
158
lua_setfield(L, -2, "oid");
137
159
push_asn1_objname(L, object, 0);
138
160
lua_setfield(L, -2, "name");
139
push_asn1_string(L, X509_NAME_ENTRY_get_data(entry));
161
push_asn1_string(L, X509_NAME_ENTRY_get_data(entry), encode);
140
162
lua_setfield(L, -2, "value");
141
163
lua_rawseti(L, -2, i+1);
151
173
static int meth_subject(lua_State* L)
153
return push_x509_name(L, X509_get_subject_name(lsec_checkx509(L, 1)));
175
p_x509 px = lsec_checkp_x509(L, 1);
176
return push_x509_name(L, X509_get_subject_name(px->cert), px->encode);
159
182
static int meth_issuer(lua_State* L)
161
return push_x509_name(L, X509_get_issuer_name(lsec_checkx509(L, 1)));
184
p_x509 px = lsec_checkp_x509(L, 1);
185
return push_x509_name(L, X509_get_issuer_name(px->cert), px->encode);
173
197
X509_EXTENSION *extension;
174
198
GENERAL_NAME *general_name;
175
199
STACK_OF(GENERAL_NAME) *values;
176
X509 *peer = lsec_checkx509(L, 1);
200
p_x509 px = lsec_checkp_x509(L, 1);
201
X509 *peer = px->cert;
178
203
/* Return (ret) */
205
230
push_asn1_objname(L, otherName->type_id, 0);
206
231
lua_setfield(L, -2, "name");
208
push_asn1_string(L, otherName->value->value.asn1_string);
233
push_asn1_string(L, otherName->value->value.asn1_string, px->encode);
209
234
lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
213
238
lua_pushstring(L, "dNSName");
214
239
push_subtable(L, -2);
215
push_asn1_string(L, general_name->d.dNSName);
240
push_asn1_string(L, general_name->d.dNSName, px->encode);
216
241
lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
220
245
lua_pushstring(L, "rfc822Name");
221
246
push_subtable(L, -2);
222
push_asn1_string(L, general_name->d.rfc822Name);
247
push_asn1_string(L, general_name->d.rfc822Name, px->encode);
223
248
lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
227
252
lua_pushstring(L, "uniformResourceIdentifier");
228
253
push_subtable(L, -2);
229
push_asn1_string(L, general_name->d.uniformResourceIdentifier);
254
push_asn1_string(L, general_name->d.uniformResourceIdentifier, px->encode);
230
255
lua_rawseti(L, -2, lua_rawlen(L, -2)+1);
234
259
lua_pushstring(L, "iPAddress");
235
260
push_subtable(L, -2);
236
push_asn1_string(L, general_name->d.iPAddress);
261
push_asn1_string(L, general_name->d.iPAddress, px->encode);
237
262
lua_rawseti(L, -2, lua_rawlen(L, -2)+1);
309
lua_pushstring(L, "digest algorithm not supported");
334
lua_pushfstring(L, "digest algorithm not supported (%s)", str);
312
337
if (!X509_digest(cert, digest, buffer, &bytes)) {
314
lua_pushstring(L, "error processing the certificate");
339
lua_pushfstring(L, "error processing the certificate (%s)",
340
ERR_reason_error_string(ERR_get_error()));
317
343
to_hex((char*)buffer, bytes, hex_buffer);
413
* Set the encode for ASN.1 string.
415
static int meth_set_encode(lua_State* L)
418
p_x509 px = lsec_checkp_x509(L, 1);
419
const char *enc = luaL_checkstring(L, 2);
420
if (strncmp(enc, "ai5", 3) == 0) {
422
px->encode = LSEC_AI5_STRING;
423
} else if (strncmp(enc, "utf8", 4) == 0) {
425
px->encode = LSEC_UTF8_STRING;
427
lua_pushboolean(L, succ);
386
431
/*---------------------------------------------------------------------------*/
388
433
static int load_cert(lua_State* L)
410
455
static luaL_Reg methods[] = {
411
456
{"digest", meth_digest},
457
{"setencode", meth_set_encode},
412
458
{"extensions", meth_extensions},
413
459
{"issuer", meth_issuer},
414
460
{"notbefore", meth_notbefore},