src/ssl.c

changeset 4
718837c61318
parent 3
bd2b1836f0ba
child 5
2d5a8f963181
equal deleted inserted replaced
3:bd2b1836f0ba 4:718837c61318
370 } else { 370 } else {
371 lua_pushboolean(L, 0); 371 lua_pushboolean(L, 0);
372 return 1; 372 return 1;
373 } 373 }
374 } 374 }
375
376 /**
377 * Return the peer certificate.
378 */
379 static int meth_getpeercertificate(lua_State *L)
380 {
381 X509 *peer;
382 p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
383 peer = SSL_get_peer_certificate(ssl->ssl);
384 if (peer == NULL) {
385 /* No client certificate available */
386 lua_pushboolean(L, 0);
387 return 1;
388 } else {
389 char *buffer = NULL;
390 char length = 0;
391 BIO *bp = BIO_new(BIO_s_mem()); /* To memory */
392 i2d_X509_bio(bp, peer); /* as der */
393 if ((length = BIO_read(bp, 0, 0)) == 0) {
394 BIO_free(bp);
395 return 0;
396 }
397 if ((buffer = malloc(length)) == NULL) {
398 BIO_free(bp);
399 return 0;
400 }
401 if ((length = BIO_read(bp, buffer, length)) > length) {
402 free(buffer);
403 BIO_free(bp);
404 return 0;
405 }
406 lua_pushlstring(L, buffer, length);
407 free(buffer);
408 BIO_free(bp);
409 return 1;
410 }
411 }
375 /*---------------------------------------------------------------------------*/ 412 /*---------------------------------------------------------------------------*/
376 413
377 414
378 /** 415 /**
379 * SSL metamethods 416 * SSL metamethods
386 {"receive", meth_receive}, 423 {"receive", meth_receive},
387 {"send", meth_send}, 424 {"send", meth_send},
388 {"settimeout", meth_settimeout}, 425 {"settimeout", meth_settimeout},
389 {"want", meth_want}, 426 {"want", meth_want},
390 {"compression", meth_compression}, 427 {"compression", meth_compression},
428 {"getpeercertificate",meth_getpeercertificate},
391 {NULL, NULL} 429 {NULL, NULL}
392 }; 430 };
393 431
394 /** 432 /**
395 * SSL functions 433 * SSL functions

mercurial