Decode certificate authority names with OpenSSL

This commit is contained in:
Clément Notin 2026-08-09 22:07:20 +02:00
parent 24cc8e958c
commit 4b2d2c5cc5
5 changed files with 45 additions and 62 deletions

View file

@ -904,6 +904,11 @@ int l_parse_ssl_certificate(lua_State *L)
{
return luaL_error(L, "SSL is not available");
}
int l_parse_ssl_name(lua_State *L)
{
return luaL_error(L, "SSL is not available");
}
#endif
/* Set the local address for socket operations. The two optional parameters
@ -1182,6 +1187,7 @@ LUALIB_API int luaopen_nsock (lua_State *L)
{"new", l_new},
{"sleep", l_sleep},
{"parse_ssl_certificate", l_parse_ssl_certificate},
{"parse_ssl_name", l_parse_ssl_name},
{"get_stats", l_get_stats},
{NULL, NULL}
};

View file

@ -542,6 +542,40 @@ int l_parse_ssl_certificate(lua_State *L)
return parse_ssl_cert(L, cert);
}
int l_parse_ssl_name(lua_State *L)
{
BIO *out;
X509_NAME *name;
size_t l;
const char *der;
der = luaL_checklstring(L, 1, &l);
if (der == NULL) {
lua_pushnil(L);
return 1;
}
name = d2i_X509_NAME(NULL, (const unsigned char **) &der, l);
if (name == NULL) {
lua_pushnil(L);
return 1;
}
out = BIO_new(BIO_s_mem());
if (out == NULL || X509_NAME_print_ex(out, name, 0, XN_FLAG_ONELINE) < 0) {
BIO_free_all(out);
X509_NAME_free(name);
lua_pushnil(L);
return 1;
}
char *value = NULL;
long len = BIO_get_mem_data(out, &value);
lua_pushlstring(L, value, len);
BIO_free_all(out);
X509_NAME_free(name);
return 1;
}
int l_get_ssl_certificate(lua_State *L)
{
SSL *ssl;

View file

@ -65,6 +65,7 @@
int l_get_ssl_certificate(lua_State *L);
int l_parse_ssl_certificate(lua_State *L);
int l_parse_ssl_name(lua_State *L);
void nse_nsock_init_ssl_cert(lua_State *L);
#endif

View file

@ -10,12 +10,11 @@
-- @name asn1
--
-- Version 0.4
-- Version 0.3
-- Created 01/12/2010 - v0.1 - Created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/28/2010 - v0.2 - Adapted to create a framework for SNMP, LDAP and future protocols
-- Revised 02/02/2010 - v0.3 - Changes: o Re-designed so that ASN1Encoder and ASN1Decoder are separate classes
-- o Each script or library should now create its own Encoder and Decoder instance
-- Revised 08/09/2026 - v0.4 - Added X.509 Name decoding.
--
local math = require "math"
@ -31,23 +30,6 @@ BERCLASS = {
Private = 192
}
-- Common X.509 distinguished-name attribute OIDs and their RFC 4514 names.
X509_NAME_OIDS = {
["2.5.4.3"] = "CN", ["2.5.4.4"] = "SN", ["2.5.4.5"] = "serialNumber",
["2.5.4.6"] = "C", ["2.5.4.7"] = "L", ["2.5.4.8"] = "ST",
["2.5.4.10"] = "O", ["2.5.4.11"] = "OU",
["0.9.2342.19200300.100.1.25"] = "DC",
["1.2.840.113549.1.9.1"] = "emailAddress",
}
local function decode_x509_name_string(_, encoded, length, position)
return encoded:sub(position, position + length - 1), position + length
end
local function decode_x509_name_set(self, encoded, length, position)
return self:decodeSeq(encoded, length, position)
end
--- The decoder class
--
ASN1Decoder = {
@ -269,45 +251,6 @@ ASN1Decoder = {
}
---
-- Decodes a DER-encoded X.509 Name into a human-readable distinguished name.
-- Unknown attribute OIDs are rendered in dotted-decimal notation. If the Name
-- cannot be decoded, returns its hexadecimal DER representation.
-- @param der DER-encoded X.509 Name
-- @return string Distinguished name
function decodeX509Name(der)
local decoder = ASN1Decoder:new()
decoder:setStopOnError(true)
decoder:registerTagDecoders({
["\x31"] = decode_x509_name_set, -- SET OF RelativeDistinguishedName
["\x0c"] = decode_x509_name_string, -- UTF8String
["\x13"] = decode_x509_name_string, -- PrintableString
["\x14"] = decode_x509_name_string, -- T61String
["\x16"] = decode_x509_name_string, -- IA5String
})
local ok, name = pcall(decoder.decode, decoder, der, 1)
if not ok or type(name) ~= "table" then
return stdnse.tohex(der)
end
local rdns = {}
for _, rdn in ipairs(name) do
local attributes = {}
for _, attribute in ipairs(rdn) do
local oid, value = attribute[1], attribute[2]
if type(oid) == "table" and type(value) == "string" then
local oid_text = table.concat(oid, ".")
attributes[#attributes + 1] = ("%s = %s"):format(
X509_NAME_OIDS[oid_text] or oid_text, value)
end
end
if #attributes > 0 then
rdns[#rdns + 1] = table.concat(attributes, " + ")
end
end
return #rdns > 0 and table.concat(rdns, ", ") or stdnse.tohex(der)
end
--- The encoder class
--
ASN1Encoder = {

View file

@ -1,4 +1,3 @@
local asn1 = require "asn1"
local nmap = require "nmap"
local shortport = require "shortport"
local sslcert = require "sslcert"
@ -20,12 +19,12 @@ TLS 1.3 handshake decryption, so this script intentionally negotiates TLS 1.2.
-- @output
-- 443/tcp open https
-- | tls-client-cert-request:
-- | Acceptable Certificate Authorities:
-- | C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority
-- | Client Certificate Types:
-- | rsa_sign
-- | dss_sign
-- | ecdsa_sign
-- | Acceptable Certificate Authorities:
-- |_ C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority
-- | Signature Algorithms:
-- | sha512-rsa
-- | sha512-dsa
@ -107,7 +106,7 @@ action = function(host, port)
end
for _, authority in ipairs(message.certificate_authorities) do
output["Acceptable Certificate Authorities"][#output["Acceptable Certificate Authorities"] + 1] =
asn1.decodeX509Name(authority)
nmap.socket.parse_ssl_name(authority)
end
return output
elseif message.type == "server_hello_done" then