UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

137 lines (136 loc) • 5.72 kB
import crypto from "node:crypto"; //#region src/infra/ed25519-signature.ts const ED25519_RAW_KEY_LENGTH = 32; const ED25519_SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex"); const ED25519_PKCS8_PRIVATE_PREFIX = Buffer.from("302e020100300506032b657004220420", "hex"); const MAX_BASE64URL_DECODE_INPUT_LENGTH = 4096; function assertBoundedBase64Input(e) { if (e.length > MAX_BASE64URL_DECODE_INPUT_LENGTH) throw new Error("base64url input exceeds the maximum allowed length"); if (e.length === 0) throw new Error("base64 input must not be empty"); } /** Decode the existing permissive base64url wire shape. */ function base64UrlDecode(e) { if (e.length > MAX_BASE64URL_DECODE_INPUT_LENGTH) throw new Error("base64url input exceeds the maximum allowed length"); const t = e.replaceAll("-", "+").replaceAll("_", "/"); const n = t + "=".repeat((4 - t.length % 4) % 4); return Buffer.from(n, "base64"); } /** Decode a canonical standard-base64 or unpadded-base64url value. */ function decodeCanonicalBase64OrBase64Url(e) { assertBoundedBase64Input(e); const t = /^[A-Za-z0-9_-]+$/.test(e) ? "base64url" : "base64"; if (t === "base64" && (!/^[A-Za-z0-9+/]+={0,2}$/.test(e) || e.length % 4 !== 0)) throw new Error("invalid canonical base64 input"); const n = Buffer.from(e, t); if (n.toString(t) !== e) throw new Error(`invalid canonical ${t} input`); return n; } function pemEncode(e, t) { return `-----BEGIN ${e}-----\n${t.toString("base64").match(/.{1,64}/g)?.join("\n") ?? ""}\n-----END ${e}-----\n`; } function decodeCanonicalPem(e, t) { const n = `-----BEGIN ${e}-----\n`; const r = `\n-----END ${e}-----\n`; if (!t.startsWith(n) || !t.endsWith(r)) throw new Error(`${e} must use canonical PEM framing`); const i = t.slice(n.length, -r.length); if (!i || !/^[A-Za-z0-9+/=\n]+$/.test(i)) throw new Error(`${e} contains invalid PEM body bytes`); const a = Buffer.from(i.replaceAll("\n", ""), "base64"); if (pemEncode(e, a) !== t) throw new Error(`${e} must use canonical PEM base64 encoding`); return a; } function assertRawKeyLength(e, t) { if (e.length !== ED25519_RAW_KEY_LENGTH) throw new Error(`${t} must contain exactly ${ED25519_RAW_KEY_LENGTH} bytes`); } function ed25519PublicKeyPemFromRaw(e) { assertRawKeyLength(e, "Ed25519 public key"); return pemEncode("PUBLIC KEY", Buffer.concat([ED25519_SPKI_PREFIX, e])); } function ed25519PrivateKeyPemFromRaw(e) { assertRawKeyLength(e, "Ed25519 private key"); return pemEncode("PRIVATE KEY", Buffer.concat([ED25519_PKCS8_PRIVATE_PREFIX, e])); } function assertEd25519KeyType(e, t) { if (e.asymmetricKeyType !== "ed25519") throw new Error(`${t} must be an Ed25519 key`); } function deriveRawKeyFromDer(e, t, n) { const r = n.length + ED25519_RAW_KEY_LENGTH; if (e.length !== r || !e.subarray(0, n.length).equals(n)) throw new Error(`${t} has a noncanonical Ed25519 encoding`); return e.subarray(n.length); } function deriveCanonicalEd25519PublicKeyRaw(e) { const t = decodeCanonicalPem("PUBLIC KEY", e); assertEd25519KeyType(crypto.createPublicKey({ key: t, type: "spki", format: "der" }), "public key"); return deriveRawKeyFromDer(t, "public key", ED25519_SPKI_PREFIX); } function deriveCanonicalEd25519PrivateKeyRaw(e) { const t = decodeCanonicalPem("PRIVATE KEY", e); assertEd25519KeyType(crypto.createPrivateKey({ key: t, type: "pkcs8", format: "der" }), "private key"); return deriveRawKeyFromDer(t, "private key", ED25519_PKCS8_PRIVATE_PREFIX); } /** Parse any Node-compatible Ed25519 PEM and return its canonical raw public key. */ function deriveEd25519PublicKeyRaw(e) { const t = crypto.createPublicKey(e); assertEd25519KeyType(t, "public key"); return deriveRawKeyFromDer(t.export({ type: "spki", format: "der" }), "public key", ED25519_SPKI_PREFIX); } /** Parse any Node-compatible Ed25519 PEM and return its canonical raw private key. */ function deriveEd25519PrivateKeyRaw(e) { const t = crypto.createPrivateKey(e); assertEd25519KeyType(t, "private key"); return deriveRawKeyFromDer(t.export({ type: "pkcs8", format: "der" }), "private key", ED25519_PKCS8_PRIVATE_PREFIX); } function publicKeyRawBase64UrlFromEd25519Pem(e) { return deriveEd25519PublicKeyRaw(e).toString("base64url"); } function normalizeEd25519PublicKeyBase64Url(e) { try { const t = e.includes("BEGIN") ? deriveEd25519PublicKeyRaw(e) : base64UrlDecode(e); if (t.length === 0) return null; return t.toString("base64url"); } catch { return null; } } function signEd25519Payload(e, t) { const n = crypto.createPrivateKey(e); return crypto.sign(null, Buffer.from(t, "utf8"), n).toString("base64url"); } function createEd25519PublicKey(e) { if (e.includes("BEGIN")) return crypto.createPublicKey(e); return crypto.createPublicKey({ key: Buffer.concat([ED25519_SPKI_PREFIX, base64UrlDecode(e)]), type: "spki", format: "der" }); } function verifyEd25519Signature(e) { return verifyEd25519SignatureBytes({ publicKey: e.publicKey, payload: Buffer.from(e.payload, "utf8"), signatureBase64Url: e.signatureBase64Url }); } function verifyEd25519SignatureBytes(e) { try { const t = createEd25519PublicKey(e.publicKey); const n = base64UrlDecode(e.signatureBase64Url); return crypto.verify(null, e.payload, t, n); } catch { return false; } } //#endregion export { deriveEd25519PublicKeyRaw as a, normalizeEd25519PublicKeyBase64Url as c, verifyEd25519Signature as d, verifyEd25519SignatureBytes as f, deriveEd25519PrivateKeyRaw as i, publicKeyRawBase64UrlFromEd25519Pem as l, deriveCanonicalEd25519PrivateKeyRaw as n, ed25519PrivateKeyPemFromRaw as o, deriveCanonicalEd25519PublicKeyRaw as r, ed25519PublicKeyPemFromRaw as s, decodeCanonicalBase64OrBase64Url as t, signEd25519Payload as u };