// Microsoft Money (.mny) files: MSISAM, the Jet 4 variant Money writes, whose // pages 1 to 14 (the system tables) are RC4-encrypted. Copied by build.mjs to // codec-handler/handlers/msisam.js in mdb-reader's code, and used by // codec-handler/create.js for the MSISAM codec type, which upstream reads as // unencrypted. // // The algorithm follows jackcess-encrypt 4.0.2 (MSISAMCryptCodecHandler, // BaseJetCryptCodecHandler and BaseCryptCodecHandler.applyPageNumber; // Apache License 2.0, Copyright (c) 2010 Vladimir Berezniker), written anew // here for mdb-reader: // - Money 2002 and later (flag bits 0x06 at 0x298 of the decrypted header): // the page key is the MD5 (SHA-1 when bit 0x20 is set) of the password, // upper-cased, as 40 bytes of UTF-16, cut to 16 bytes, followed by the first // 4 bytes of the salt at 0x72, with the page number XORed into those 4. // The password is checked by decrypting 4 bytes at 0x2E9 + salt[0] with the // digest and the full 8-byte salt, which must give the salt's first 4 bytes; // all zeros there means there is no password. // - Money 2000 and 2001: the key is the salt hashed with the header's stored // password bytes and engine name, so no password is needed to read the // pages. jackcess-encrypt does not check the password at all for these; this // site does (John's decision): the password stored in the header must match // the one typed, upper- or lower-case alike, so a file with a password still // asks for it. The stored one is never handed out. import { decryptRC4 } from "../../crypto/index.js"; import { createHash } from "../../environment/index.js"; const SALT_OFFSET = 0x72; const CRYPT_CHECK_START = 0x2e9; const FLAGS_OFFSET = 0x298; const NEW_ENCRYPTION = 0x6; const USE_SHA1 = 0x20; const MAX_ENCRYPTED_PAGE = 0xe; const PASSWORD_OFFSET = 0x42; const PASSWORD_LENGTH = 40; const TRAILING_PASSWORD_LENGTH = 20; const ENGINE_NAME_OFFSET = 4; const ENGINE_NAME_LENGTH = 15; /** The base key with the page number XORed into the 4 bytes at `offset`. */ function pageKey(base, offset, page) { const key = Buffer.from(base); key.writeUInt32LE(page >>> 0, offset); for (let i = offset; i < offset + 4; i++) key[i] ^= base[i]; return key; } /** Folds bytes into a 4-byte salt, as Jet does for the old key. */ function hashSalt(salt, data) { let h = salt.readInt32LE(0); for (let pos = 0; pos < data.length; pos++) h ^= (data[pos] << (pos % 24)) | 0; salt.writeInt32LE(h | 0, 0); } /** The header's password bytes (and the 20 at the end of the 80) with the creation-date mask taken off. */ function unmaskedPasswordBytes(header) { const full = Buffer.from(header.subarray(PASSWORD_OFFSET, PASSWORD_OFFSET + PASSWORD_LENGTH * 2)); const mask = Buffer.alloc(4); mask.writeInt32LE(Math.trunc(header.readDoubleLE(SALT_OFFSET)) | 0); for (let i = 0; i < PASSWORD_LENGTH; i++) full[i] ^= mask[i % 4]; const trailing = full.length - TRAILING_PASSWORD_LENGTH; for (let i = 0; i < TRAILING_PASSWORD_LENGTH; i++) full[trailing + i] ^= mask[i % 4]; return full; } /** The password an old-scheme file keeps, as text; "" when there is none. */ function storedPassword(full) { let out = ""; for (let i = 0; i + 1 < PASSWORD_LENGTH; i += 2) { const c = full[i] | (full[i + 1] << 8); if (!c) break; out += String.fromCharCode(c); } return out; } export function createMsisamCodecHandler(header, password) { const flags = header[FLAGS_OFFSET]; let key; let keyOffset; let passwordOk; if (flags & NEW_ENCRYPTION) { const pwd = Buffer.alloc(PASSWORD_LENGTH); if (password) Buffer.from(String(password).toUpperCase(), "utf16le").copy(pwd, 0, 0, PASSWORD_LENGTH); const digest = createHash(flags & USE_SHA1 ? "sha1" : "md5").update(pwd).digest().subarray(0, 16); const salt = header.subarray(SALT_OFFSET, SALT_OFFSET + 8); const checkAt = CRYPT_CHECK_START + header[SALT_OFFSET]; const check = header.subarray(checkAt, checkAt + 4); passwordOk = check.every((b) => b === 0) || decryptRC4(Buffer.concat([digest, salt]), check).equals(salt.subarray(0, 4)); key = Buffer.concat([digest, salt.subarray(0, 4)]); keyOffset = 16; } else { const full = unmaskedPasswordBytes(header); const stored = storedPassword(full); passwordOk = stored === "" || (!!password && String(password).toUpperCase() === stored.toUpperCase()); key = Buffer.from(header.subarray(SALT_OFFSET, SALT_OFFSET + 4)); const hashData = Buffer.alloc(PASSWORD_LENGTH); for (let i = 0; i < PASSWORD_LENGTH; i++) hashData[i] = full[i * 2]; hashSalt(key, hashData); hashSalt(key, header.subarray(ENGINE_NAME_OFFSET, ENGINE_NAME_OFFSET + ENGINE_NAME_LENGTH)); keyOffset = 0; } return { decryptPage: (pageBuffer, page) => (page > 0 && page <= MAX_ENCRYPTED_PAGE ? decryptRC4(pageKey(key, keyOffset, page), pageBuffer) : pageBuffer), verifyPassword: () => passwordOk, }; }