From f13aee5dacc263e6318652197230aa1437818760 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 13:20:47 +0200 Subject: [PATCH] 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;