From bfd750138c8c970cade9ad40dd525acd1ca88276 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 14 May 2015 11:59:20 +0300 Subject: CRL extensions --- src/openssl.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index 99a1ec0..086b4fa 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -4318,6 +4318,18 @@ error: } /* xx_add() */ +static int xx_addExtension(lua_State *L) { + X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); + X509_EXTENSION *ext = checksimple(L, 2, X509_EXT_CLASS); + + if (!X509_CRL_add_ext(crl, ext, -1)) + return auxL_error(L, auxL_EOPENSSL, "x509.crl:addExtension"); + + lua_pushboolean(L, 1); + + return 1; +} /* xx_addExtension() */ + static int xx_sign(lua_State *L) { X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); EVP_PKEY *key = checksimple(L, 2, PKEY_CLASS); @@ -4396,6 +4408,7 @@ static const luaL_Reg xx_methods[] = { { "getIssuer", &xx_getIssuer }, { "setIssuer", &xx_setIssuer }, { "add", &xx_add }, + { "addExtension", &xx_addExtension }, { "sign", &xx_sign }, { "text", &xx_text }, { "tostring", &xx__tostring }, -- cgit v1.2.3-59-g8ed1b From 4ca7cbf40e85f79f4864d462d7defd5469d282e5 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 14 May 2015 14:28:00 +0300 Subject: get named extension from certificate --- src/openssl.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index 086b4fa..1c0dfbd 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -3632,6 +3632,37 @@ static int xc_addExtension(lua_State *L) { } /* xc_addExtension() */ +static int xc_getExtension(lua_State *L) { + X509 *crt = checksimple(L, 1, X509_CERT_CLASS); + const char *name = luaL_checkstring(L, 2); + + X509_EXTENSION *ext, **ud; + ASN1_OBJECT *obj = NULL; + + if (!(obj = OBJ_txt2obj(name, 0))) + goto error; + + int i = X509_get_ext_by_OBJ(crt, obj, -1); + if (i > -1) { + ud = prepsimple(L, X509_EXT_CLASS); + if (!(ext = X509_get_ext(crt, i))) + goto error; + if (!(*ud = X509_EXTENSION_dup(ext))) + goto error; + } + else lua_pushnil(L); + + ASN1_OBJECT_free(obj); + return 1; + +error: + if (obj) + ASN1_OBJECT_free(obj); + + return auxL_error(L, auxL_EOPENSSL, "x509.cert:getExtension"); +} /* xc_getExtension() */ + + static int xc_isIssuedBy(lua_State *L) { X509 *crt = checksimple(L, 1, X509_CERT_CLASS); X509 *issuer = checksimple(L, 2, X509_CERT_CLASS); @@ -3850,6 +3881,7 @@ static const luaL_Reg xc_methods[] = { { "getBasicConstraintsCritical", &xc_getBasicConstraintsCritical }, { "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical }, { "addExtension", &xc_addExtension }, + { "getExtension", &xc_getExtension }, { "isIssuedBy", &xc_isIssuedBy }, { "getPublicKey", &xc_getPublicKey }, { "setPublicKey", &xc_setPublicKey }, -- cgit v1.2.3-59-g8ed1b From 057aefa60887b4599edba7f32b9bcd83d926c3af Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 14 May 2015 14:50:22 +0300 Subject: get extension data in DER format --- src/openssl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index 1c0dfbd..c3b9145 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2889,6 +2889,13 @@ static int xe_interpose(lua_State *L) { } /* xe_interpose() */ +static int xe_getData(lua_State *L) { + ASN1_STRING *data = X509_EXTENSION_get_data(checksimple(L, 1, X509_EXT_CLASS)); + lua_pushlstring(L, (char *) ASN1_STRING_data(data), ASN1_STRING_length(data)); + return 1; +} /* xe_getData() */ + + static int xe__gc(lua_State *L) { X509_EXTENSION **ud = luaL_checkudata(L, 1, X509_EXT_CLASS); @@ -2902,7 +2909,8 @@ static int xe__gc(lua_State *L) { static const luaL_Reg xe_methods[] = { - { NULL, NULL }, + { "getData", &xe_getData }, + { NULL, NULL }, }; static const luaL_Reg xe_metatable[] = { -- cgit v1.2.3-59-g8ed1b From 615d4aa6b5a42df259e41a041768b2a1ec07da35 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 14 May 2015 15:24:05 +0300 Subject: initialize extension data in DER format without intermediate hex encoding --- src/openssl.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index c3b9145..3b75e63 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2841,12 +2841,31 @@ static int xe_new(lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *value = luaL_checkstring(L, 2); + ASN1_OBJECT *obj = NULL; + ASN1_STRING *oct = NULL; CONF *conf = NULL; X509V3_CTX cbuf = { 0 }, *ctx = NULL; X509_EXTENSION *ext = NULL; if (!lua_isnil(L, 3)) { - const char *cdata = luaL_checkstring(L, 3); + size_t len; + const char *cdata = luaL_checklstring(L, 3, &len); + int crit = !strcmp(value, "critical,DER"); + + if (crit || !strcmp(value, "DER")) { + if (!(obj = OBJ_txt2obj(name, 0))) + goto error; + if (!(oct = ASN1_STRING_new())) + goto error; + if (!ASN1_STRING_set(oct, cdata, len)) + goto error; + if (!(*ud = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct))) + goto error; + ASN1_OBJECT_free(obj); + ASN1_STRING_free(oct); + return 1; + } + BIO *bio = getbio(L); if (BIO_puts(bio, cdata) < 0) goto error; @@ -2877,6 +2896,12 @@ static int xe_new(lua_State *L) { return 1; error: + if (obj) + ASN1_OBJECT_free(obj); + + if (oct) + ASN1_STRING_free(oct); + if (conf) NCONF_free(conf); -- cgit v1.2.3-59-g8ed1b