const IV_BYTES = 12; const KEY_BYTES = 32; /** Where a secret belongs. It is bound into the ciphertext, so a sealed value copied to another row does not open. */ export interface SecretScope { /** `repo:` for a repository secret, `namespace:` for an organization secret. */ readonly owner: string; readonly environment: string | null; readonly name: string; } export interface SealedSecret { readonly ciphertext: string; readonly wrappedKey: string; } export class InvalidMasterKey extends Error { constructor() { super(`CI_SECRETS_KEY must be ${KEY_BYTES} random bytes in base64.`); } } export class SecretUnreadable extends Error { readonly scope: SecretScope; constructor(scope: SecretScope) { super(`The secret ${scope.name} could not be opened. The master key or the stored value changed.`); this.scope = scope; } } /** * Envelope encryption with AES-256-GCM. Each secret has its own data key, and the master key only wraps data keys, * so a new master key re-wraps keys without touching values. The scope is the additional data of both layers. */ export class SecretBox { readonly #master: CryptoKey; private constructor(master: CryptoKey) { this.#master = master; } static async fromBase64(encoded: string): Promise { let raw: Uint8Array; try { raw = fromBase64(encoded); } catch { throw new InvalidMasterKey(); } if (raw.length !== KEY_BYTES) throw new InvalidMasterKey(); return new SecretBox(await crypto.subtle.importKey("raw", raw, "AES-GCM", false, ["encrypt", "decrypt"])); } async seal(value: string, scope: SecretScope): Promise { const aad = scopeBytes(scope); const dataKey = crypto.getRandomValues(new Uint8Array(KEY_BYTES)); const key = await crypto.subtle.importKey("raw", dataKey, "AES-GCM", false, ["encrypt"]); const ciphertext = await encrypt(key, new TextEncoder().encode(value), aad); const wrappedKey = await encrypt(this.#master, dataKey, aad); return { ciphertext: toBase64(ciphertext), wrappedKey: toBase64(wrappedKey) }; } async open(sealed: SealedSecret, scope: SecretScope): Promise { const aad = scopeBytes(scope); try { const dataKey = await decrypt(this.#master, fromBase64(sealed.wrappedKey), aad); const key = await crypto.subtle.importKey("raw", dataKey, "AES-GCM", false, ["decrypt"]); return new TextDecoder().decode(await decrypt(key, fromBase64(sealed.ciphertext), aad)); } catch { throw new SecretUnreadable(scope); } } } async function encrypt(key: CryptoKey, plaintext: Uint8Array, aad: Uint8Array): Promise> { const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES)); const body = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: aad }, key, plaintext)); const out = new Uint8Array(IV_BYTES + body.length); out.set(iv); out.set(body, IV_BYTES); return out; } async function decrypt(key: CryptoKey, sealed: Uint8Array, aad: Uint8Array): Promise> { const iv = sealed.subarray(0, IV_BYTES); return new Uint8Array(await crypto.subtle.decrypt({ name: "AES-GCM", iv, additionalData: aad }, key, sealed.subarray(IV_BYTES))); } function scopeBytes(scope: SecretScope): Uint8Array { return new TextEncoder().encode(JSON.stringify(["refs-ci-secret/2", scope.owner, scope.environment, scope.name])); } function toBase64(bytes: Uint8Array): string { let binary = ""; for (const byte of bytes) binary += String.fromCharCode(byte); return btoa(binary); } function fromBase64(encoded: string): Uint8Array { return Uint8Array.from(atob(encoded), (char) => char.charCodeAt(0)); }