src/x509.c

changeset 26
bbff42d46512
parent 25
4bc25168aa1c
child 27
3e0325d39a61
equal deleted inserted replaced
25:4bc25168aa1c 26:bbff42d46512
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 cert_from_pem(lua_State* L)
268 {
269 X509 *cert;
270 BIO *bio = BIO_new(BIO_s_mem());
271 const char* data; size_t bytes;
272 data = luaL_checklstring(L, 1, &bytes);
273 BIO_write(bio, data, bytes);
274 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
275 if(cert)
276 luasec_push_x509(L, cert);
277 else
278 lua_pushnil(L);
279 BIO_free(bio);
280 return 1;
281 }
282
267 /** 283 /**
268 * Certificate metamethods 284 * Certificate metamethods
269 */ 285 */
270 static luaL_Reg meta[] = { 286 static luaL_Reg meta[] = {
271 {"subject", meth_subject}, 287 {"subject", meth_subject},
275 {"pem", meth_pem}, 291 {"pem", meth_pem},
276 {"digest", meth_digest}, 292 {"digest", meth_digest},
277 {NULL, NULL} 293 {NULL, NULL}
278 }; 294 };
279 295
296 /**
297 * ssl.x509 functions
298 */
299 static luaL_Reg funcs[] = {
300 {"cert_from_pem", cert_from_pem},
301 {NULL, NULL}
302 };
303
280 LUASEC_API int luaopen_ssl_x509(lua_State *L) 304 LUASEC_API int luaopen_ssl_x509(lua_State *L)
281 { 305 {
282 /* Register the functions and tables */ 306 /* Register the functions and tables */
283 luaL_newmetatable(L, "SSL:Certificate"); 307 luaL_newmetatable(L, "SSL:Certificate");
284 lua_newtable(L); 308 lua_newtable(L);
285 luaL_register(L, NULL, meta); 309 luaL_register(L, NULL, meta);
286 lua_setfield(L, -2, "__index"); 310 lua_setfield(L, -2, "__index");
287 lua_pushcfunction(L, meth_destroy); 311 lua_pushcfunction(L, meth_destroy);
288 lua_setfield(L, -2, "__gc"); 312 lua_setfield(L, -2, "__gc");
289 lua_newtable(L); 313
290 return 1; 314 luaL_register(L, "ssl.x509", funcs);
291 } 315 return 1;
316 }

mercurial