using System; using System.Buffers.Binary; using System.IO; using System.Security.Cryptography; using System.Linq; using System.Text; internal static class MacSign { private const uint MH_MAGIC_64 = 0xfeedfacf; private const uint LC_SEGMENT_64 = 0x19, LC_CODE_SIGNATURE = 0x1d; private const int PageShift = 12, PageSize = 1 << PageShift; private static int Main(string[] args) { if (args.Length == 2 && args[0] == "--verify") return Verify(args[1]); if (args.Length < 2) { Console.Error.WriteLine("usage: macsign [Info.plist]"); return 2; } try { byte[] plist = args.Length > 2 && File.Exists(args[2]) ? File.ReadAllBytes(args[2]) : null; Sign(args[0], args[1], plist); Console.WriteLine("signed " + Path.GetFileName(args[0])); return 0; } catch (Exception e) { Console.Error.WriteLine("macsign: " + e.Message); return 1; } } private static int Verify(string file) { byte[] f = File.ReadAllBytes(file); if (U32(f, 0) != MH_MAGIC_64) { Console.WriteLine("not thin mach-o"); return 1; } int ncmds = (int)U32(f, 16), p = 32, dataoff = -1, datasize = 0; for (int i = 0; i < ncmds; i++) { if (U32(f, p) == LC_CODE_SIGNATURE) { dataoff = (int)U32(f, p + 8); datasize = (int)U32(f, p + 12); } p += (int)U32(f, p + 4); } if (dataoff < 0) { Console.WriteLine("no signature"); return 1; } if (dataoff + datasize != f.Length) { Console.WriteLine("signature isn't at the end of the file"); return 1; } uint BE(int o) => BinaryPrimitives.ReadUInt32BigEndian(f.AsSpan(o)); if (BE(dataoff) != 0xfade0cc0) { Console.WriteLine("bad superblob"); return 1; } int count = (int)BE(dataoff + 8), cd = -1; for (int i = 0; i < count; i++) if (BE(dataoff + 12 + i * 8) == 0) cd = dataoff + (int)BE(dataoff + 16 + i * 8); if (cd < 0 || BE(cd) != 0xfade0c02) { Console.WriteLine("no code directory"); return 1; } int hashOff = (int)BE(cd + 16), nCode = (int)BE(cd + 28), limit = (int)BE(cd + 32), shift = f[cd + 39]; if (limit != dataoff || f[cd + 36] != 32 || f[cd + 37] != 2) { Console.WriteLine("bad code directory fields"); return 1; } string ident = Encoding.UTF8.GetString(f, cd + (int)BE(cd + 20), 64).Split('\0')[0]; for (int i = 0; i < nCode; i++) { int start = i << shift, len = Math.Min(1 << shift, limit - start); if (!SHA256.HashData(f.AsSpan(start, len)).AsSpan().SequenceEqual(f.AsSpan(cd + hashOff + i * 32, 32))) { Console.WriteLine("page " + i + " hash mismatch"); return 1; } } Console.WriteLine("ok: " + nCode + " pages, id " + ident + ", flags 0x" + BE(cd + 12).ToString("x")); return 0; } private static uint U32(byte[] b, int o) => BinaryPrimitives.ReadUInt32LittleEndian(b.AsSpan(o)); private static ulong U64(byte[] b, int o) => BinaryPrimitives.ReadUInt64LittleEndian(b.AsSpan(o)); private static void W32(byte[] b, int o, uint v) => BinaryPrimitives.WriteUInt32LittleEndian(b.AsSpan(o), v); private static void W64(byte[] b, int o, ulong v) => BinaryPrimitives.WriteUInt64LittleEndian(b.AsSpan(o), v); private static void Sign(string file, string ident, byte[] plist) { byte[] f = File.ReadAllBytes(file); if (U32(f, 0) != MH_MAGIC_64) throw new Exception("not a thin 64-bit Mach-O file"); int ncmds = (int)U32(f, 16), sizeofcmds = (int)U32(f, 20); int p = 32, linkedit = -1, text = -1; ulong firstSection = ulong.MaxValue; for (int i = 0; i < ncmds; i++) { uint cmd = U32(f, p), size = U32(f, p + 4); if (cmd == LC_CODE_SIGNATURE) throw new Exception("already signed"); if (cmd == LC_SEGMENT_64) { string name = Encoding.ASCII.GetString(f, p + 8, 16).TrimEnd('\0'); if (name == "__LINKEDIT") linkedit = p; if (name == "__TEXT") text = p; int nsects = (int)U32(f, p + 64); for (int s = 0; s < nsects; s++) { uint off = U32(f, p + 72 + s * 80 + 48); if (off != 0 && off < firstSection) firstSection = off; } } p += (int)size; } if (linkedit < 0 || text < 0) throw new Exception("missing __TEXT or __LINKEDIT"); int cmdEnd = 32 + sizeofcmds; if ((ulong)(cmdEnd + 16) > firstSection) throw new Exception("no room in the header for a signature"); for (int i = cmdEnd; i < cmdEnd + 16; i++) if (f[i] != 0) throw new Exception("header padding isn't empty"); ulong leOff = U64(f, linkedit + 40), leSize = U64(f, linkedit + 48); if (leOff + leSize > (ulong)f.Length) throw new Exception("__LINKEDIT runs past the end of the file"); int codeLimit = (f.Length + 15) & ~15; byte[] identBytes = Encoding.UTF8.GetBytes(ident + "\0"); int nSpecial = 2, nCode = (codeLimit + PageSize - 1) / PageSize; const int cdHeader = 88; int hashOffset = cdHeader + identBytes.Length + nSpecial * 32; int cdLen = hashOffset + nCode * 32; byte[] req = new byte[12]; BinaryPrimitives.WriteUInt32BigEndian(req.AsSpan(0), 0xfade0c01); BinaryPrimitives.WriteUInt32BigEndian(req.AsSpan(4), 12); byte[] cms = new byte[8]; BinaryPrimitives.WriteUInt32BigEndian(cms.AsSpan(0), 0xfade0b01); BinaryPrimitives.WriteUInt32BigEndian(cms.AsSpan(4), 8); int superHeader = 12 + 3 * 8; int sigLen = superHeader + cdLen + req.Length + cms.Length; int sigSize = (sigLen + 15) & ~15; byte[] o = new byte[codeLimit + sigSize]; Buffer.BlockCopy(f, 0, o, 0, f.Length); W32(o, 16, (uint)(ncmds + 1)); W32(o, 20, (uint)(sizeofcmds + 16)); W32(o, cmdEnd, LC_CODE_SIGNATURE); W32(o, cmdEnd + 4, 16); W32(o, cmdEnd + 8, (uint)codeLimit); W32(o, cmdEnd + 12, (uint)sigSize); ulong newLeSize = (ulong)(codeLimit + sigSize) - leOff; W64(o, linkedit + 48, newLeSize); W64(o, linkedit + 32, (newLeSize + 0x3fff) & ~0x3fffUL); byte[] cd = new byte[cdLen]; void B32(int at, uint v) => BinaryPrimitives.WriteUInt32BigEndian(cd.AsSpan(at), v); void B64(int at, ulong v) => BinaryPrimitives.WriteUInt64BigEndian(cd.AsSpan(at), v); B32(0, 0xfade0c02); B32(4, (uint)cdLen); B32(8, 0x20400); B32(12, 0x2); B32(16, (uint)hashOffset); B32(20, cdHeader); B32(24, (uint)nSpecial); B32(28, (uint)nCode); B32(32, (uint)codeLimit); cd[36] = 32; cd[37] = 2; cd[38] = 0; cd[39] = PageShift; B64(56, 0); B64(64, U64(f, text + 40)); B64(72, U64(f, text + 48)); B64(80, 1); Buffer.BlockCopy(identBytes, 0, cd, cdHeader, identBytes.Length); if (plist != null) Buffer.BlockCopy(SHA256.HashData(plist), 0, cd, hashOffset - 32, 32); Buffer.BlockCopy(SHA256.HashData(req), 0, cd, hashOffset - 64, 32); for (int i = 0; i < nCode; i++) { int start = i * PageSize, len = Math.Min(PageSize, codeLimit - start); Buffer.BlockCopy(SHA256.HashData(o.AsSpan(start, len)), 0, cd, hashOffset + i * 32, 32); } int at = codeLimit; void S32(uint v) { BinaryPrimitives.WriteUInt32BigEndian(o.AsSpan(at), v); at += 4; } S32(0xfade0cc0); S32((uint)sigLen); S32(3); S32(0); S32((uint)superHeader); S32(2); S32((uint)(superHeader + cdLen)); S32(0x10000); S32((uint)(superHeader + cdLen + req.Length)); Buffer.BlockCopy(cd, 0, o, at, cd.Length); at += cd.Length; Buffer.BlockCopy(req, 0, o, at, req.Length); at += req.Length; Buffer.BlockCopy(cms, 0, o, at, cms.Length); File.WriteAllBytes(file, o); } }