From 1ade1542d712c95af41b4e9d93f8a115a9b52c7b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:38:52 +0200 Subject: [PATCH 1/4] Push nil if unable to encode ASN1 string as UTF-8 --- src/x509.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/x509.c b/src/x509.c index fd893db..e89b602 100644 --- a/src/x509.c +++ b/src/x509.c @@ -101,6 +101,8 @@ static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode) lua_pushlstring(L, (char*)data, len); OPENSSL_free(data); } + else + lua_pushnil(L); } } From c276e9ff608cc9bad19e599044ec238a6eb890ab Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:41:20 +0200 Subject: [PATCH 2/4] Return early if ASN1 string is invalid --- src/x509.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index e89b602..d34353c 100644 --- a/src/x509.c +++ b/src/x509.c @@ -88,8 +88,10 @@ static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode) { int len; unsigned char *data; - if (!string) + if (!string) { lua_pushnil(L); + return; + } switch (encode) { case LSEC_AI5_STRING: lua_pushlstring(L, (char*)ASN1_STRING_data(string), From b83d2c6a91680848325498d5eee5c9c4f5eff00d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:47:58 +0200 Subject: [PATCH 3/4] Don't try to encode IP addresses as UTF-8 --- src/x509.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index d34353c..dcaca90 100644 --- a/src/x509.c +++ b/src/x509.c @@ -262,7 +262,7 @@ int meth_extensions(lua_State* L) case GEN_IPADD: lua_pushstring(L, "iPAddress"); push_subtable(L, -2); - push_asn1_string(L, general_name->d.iPAddress, px->encode); + push_asn1_string(L, general_name->d.iPAddress, LSEC_AI5_STRING); lua_rawseti(L, -2, lua_rawlen(L, -2)+1); lua_pop(L, 1); break; From f13aee5dacc263e6318652197230aa1437818760 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 13:20:47 +0200 Subject: [PATCH 4/4] Encode iPAddress fields in human readable form --- src/x509.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index dcaca90..6e09237 100644 --- a/src/x509.c +++ b/src/x509.c @@ -20,6 +20,8 @@ #include #include +#include + #include #include @@ -123,6 +125,31 @@ static int push_asn1_time(lua_State *L, ASN1_UTCTIME *tm) return 1; } +/** + * Return a human readable IP address. + */ +static void push_asn1_ip(lua_State *L, ASN1_STRING *string) +{ + unsigned char *ip = ASN1_STRING_data(string); + char dst[INET6_ADDRSTRLEN]; + int typ; + switch(ASN1_STRING_length(string)) { + case 4: + typ = AF_INET; + break; + case 16: + typ = AF_INET6; + break; + default: + lua_pushnil(L); + return; + } + if(inet_ntop(typ, ip, dst, INET6_ADDRSTRLEN)) + lua_pushstring(L, dst); + else + lua_pushnil(L); +} + /** * */ @@ -262,7 +289,7 @@ int meth_extensions(lua_State* L) case GEN_IPADD: lua_pushstring(L, "iPAddress"); push_subtable(L, -2); - push_asn1_string(L, general_name->d.iPAddress, LSEC_AI5_STRING); + push_asn1_ip(L, general_name->d.iPAddress); lua_rawseti(L, -2, lua_rawlen(L, -2)+1); lua_pop(L, 1); break;