Add :getpeercertificate() method to get peer's certificate

Fri, 08 Oct 2010 21:11:25 +0100

author
Tobias Markmann <tm@ayena.de>
date
Fri, 08 Oct 2010 21:11:25 +0100
changeset 4
718837c61318
parent 3
bd2b1836f0ba
child 5
2d5a8f963181

Add :getpeercertificate() method to get peer's certificate

src/ssl.c file | annotate | diff | comparison | revisions
--- a/src/ssl.c	Fri Oct 08 21:09:39 2010 +0100
+++ b/src/ssl.c	Fri Oct 08 21:11:25 2010 +0100
@@ -372,6 +372,43 @@
     return 1;
   }
 }
+
+/**
+ * Return the peer certificate.
+ */
+static int meth_getpeercertificate(lua_State *L)
+{
+  X509 *peer;
+  p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
+  peer = SSL_get_peer_certificate(ssl->ssl);
+  if (peer == NULL) {
+    /* No client certificate available */
+    lua_pushboolean(L, 0);
+    return 1;
+  } else {
+    char *buffer = NULL;
+    char length = 0;
+    BIO *bp = BIO_new(BIO_s_mem()); /* To memory */
+    i2d_X509_bio(bp, peer); /* as der */
+    if ((length = BIO_read(bp, 0, 0)) == 0) {
+      BIO_free(bp);
+      return 0;
+    }
+    if ((buffer = malloc(length)) == NULL) {
+      BIO_free(bp);
+      return 0;
+    }
+    if ((length = BIO_read(bp, buffer, length)) > length) {
+      free(buffer);
+      BIO_free(bp);
+      return 0;
+    }
+    lua_pushlstring(L, buffer, length);
+    free(buffer);
+    BIO_free(bp);
+    return 1;
+  }
+}
 /*---------------------------------------------------------------------------*/
 
 
@@ -388,6 +425,7 @@
   {"settimeout",        meth_settimeout},
   {"want",              meth_want},
   {"compression",       meth_compression},
+  {"getpeercertificate",meth_getpeercertificate},
   {NULL,                NULL}
 };
 

mercurial