diff --git a/src/x509.c b/src/x509.c index 4c8ef65..6e09237 100644 --- a/src/x509.c +++ b/src/x509.c @@ -20,6 +20,8 @@ #include #include +#include + #include #include @@ -103,6 +105,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); } } @@ -121,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); +} + /** * */ @@ -260,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, px->encode); + push_asn1_ip(L, general_name->d.iPAddress); lua_rawseti(L, -2, lua_rawlen(L, -2)+1); lua_pop(L, 1); break;