Files
MikrotikManager/backend/src/services/ipsec-certificate-dn.ts
T

19 lines
911 B
TypeScript

/** RouterOS active-peers.id uses the certificate subject in this field order.
* Values are printed verbatim, including commas/plus signs, so parsing the ID
* or matching a CN substring would misidentify subjects. Compare the complete
* rendered subject and reject collisions in the caller instead.
*/
export function routerOsCertificateDn(cert: Record<string, string | undefined>): string | undefined {
const fields = [["C", "country"], ["S", "state"], ["L", "locality"],
["O", "organization"], ["OU", "unit"], ["CN", "common-name"]] as const
if (!cert["common-name"]) return undefined
const parts: string[] = []
for (const [attribute, field] of fields) {
const value = cert[field]
if (value === undefined || value === "") continue
if (typeof value !== "string" || /[\u0000\r\n]/.test(value)) return undefined
parts.push(`${attribute}=${value}`)
}
return parts.join(", ")
}