From 24f5b27eb48d8a260f0a82eadfa1ab8649bb4aee Mon Sep 17 00:00:00 2001 From: Thijs Alkemade Date: Tue, 24 Sep 2013 01:17:50 +0200 Subject: [PATCH] x509: Handle non-UTF8 ASN1 strings correctly by converting them to UTF8. --- src/x509.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/x509.c b/src/x509.c index 5f71cd0..fdc80d9 100644 --- a/src/x509.c +++ b/src/x509.c @@ -75,11 +75,19 @@ static void push_asn1_objname(lua_State* L, ASN1_OBJECT *object, int no_name) */ static void push_asn1_string(lua_State* L, ASN1_STRING *string) { - if (string) - lua_pushlstring(L, (char*)ASN1_STRING_data(string), - ASN1_STRING_length(string)); - else + if (string) { + unsigned char *data; + size_t len = ASN1_STRING_to_UTF8(&data, string); + + if (len >= 0) { + lua_pushlstring(L, (char *)data, len); + OPENSSL_free(data); + } else { + lua_pushnil(L); + } + } else { lua_pushnil(L); + } } /**