262 { |
262 { |
263 X509_free(luasec_to_x509(L, 1)); |
263 X509_free(luasec_to_x509(L, 1)); |
264 return 0; |
264 return 0; |
265 } |
265 } |
266 |
266 |
|
267 int meth_tostring(lua_State *L) |
|
268 { |
|
269 X509 *cert = luasec_to_x509(L, 1); |
|
270 lua_pushfstring(L, "X509 certificate: %p", cert); |
|
271 return 1; |
|
272 } |
|
273 |
267 int cert_from_pem(lua_State* L) |
274 int cert_from_pem(lua_State* L) |
268 { |
275 { |
269 X509 *cert; |
276 X509 *cert; |
270 BIO *bio = BIO_new(BIO_s_mem()); |
277 BIO *bio = BIO_new(BIO_s_mem()); |
271 const char* data; size_t bytes; |
278 const char* data; size_t bytes; |
281 } |
288 } |
282 |
289 |
283 /** |
290 /** |
284 * Certificate metamethods |
291 * Certificate metamethods |
285 */ |
292 */ |
286 static luaL_Reg meta[] = { |
293 static luaL_Reg methods[] = { |
287 {"subject", meth_subject}, |
294 {"subject", meth_subject}, |
288 {"issuer", meth_issuer}, |
295 {"issuer", meth_issuer}, |
289 {"extensions", meth_extensions}, |
296 {"extensions", meth_extensions}, |
290 {"valid_at", meth_valid_at}, |
297 {"valid_at", meth_valid_at}, |
291 {"pem", meth_pem}, |
298 {"pem", meth_pem}, |
299 static luaL_Reg funcs[] = { |
306 static luaL_Reg funcs[] = { |
300 {"cert_from_pem", cert_from_pem}, |
307 {"cert_from_pem", cert_from_pem}, |
301 {NULL, NULL} |
308 {NULL, NULL} |
302 }; |
309 }; |
303 |
310 |
|
311 /** |
|
312 * Context metamethods. |
|
313 */ |
|
314 static luaL_Reg meta[] = { |
|
315 {"__gc", meth_destroy}, |
|
316 {"__tostring", meth_tostring}, |
|
317 {NULL, NULL} |
|
318 }; |
|
319 |
304 LUASEC_API int luaopen_ssl_x509(lua_State *L) |
320 LUASEC_API int luaopen_ssl_x509(lua_State *L) |
305 { |
321 { |
306 /* Register the functions and tables */ |
322 /* Register the functions and tables */ |
307 luaL_newmetatable(L, "SSL:Certificate"); |
323 luaL_newmetatable(L, "SSL:Certificate"); |
|
324 luaL_register(L, NULL, meta); |
|
325 |
308 lua_newtable(L); |
326 lua_newtable(L); |
309 luaL_register(L, NULL, meta); |
327 luaL_register(L, NULL, methods); |
310 lua_setfield(L, -2, "__index"); |
328 lua_setfield(L, -2, "__index"); |
311 lua_pushcfunction(L, meth_destroy); |
|
312 lua_setfield(L, -2, "__gc"); |
|
313 |
329 |
314 luaL_register(L, "ssl.x509", funcs); |
330 luaL_register(L, "ssl.x509", funcs); |
315 return 1; |
331 return 1; |
316 } |
332 } |