From 5813bd1b8edde5ab75ba5450865b817f29de3ff1 Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Sat, 18 Dec 2021 18:58:43 -0800 Subject: [PATCH] selinux: keys: Match original PEM line lengths --- src/selinux/keys.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/selinux/keys.ts b/src/selinux/keys.ts index 90658d4..d69aea5 100644 --- a/src/selinux/keys.ts +++ b/src/selinux/keys.ts @@ -141,10 +141,9 @@ export function resolveKeys( .map(s => [s.cert as Uint8Array, seinfoToPaths.get(s.seinfoId)!])) } -function serializeCert(cert: Uint8Array) { +function serializeCert(cert: Uint8Array, lineLength: number) { let base64 = Buffer.from(cert).toString('base64') - // Wrap to 76 chars to match Google's PEMs - let wrapped = base64.replace(/(.{76})/g, '$1\n') + let wrapped = base64.replace(new RegExp(`(.{${lineLength}})`, 'g'), '$1\n') return `-----BEGIN CERTIFICATE----- ${wrapped} @@ -154,8 +153,10 @@ ${wrapped} export async function writeMappedKeys(keys: Map>) { for (let [cert, paths] of keys.entries()) { - let serialized = serializeCert(cert) for (let path of paths) { + let lineLength = (await readFile(path)).split('\n')[1].length + let serialized = serializeCert(cert, lineLength) + await fs.writeFile(path, serialized) } }