casualexpiesource

cu-trainer/Ultra Paradise Linux/tools/macsign/Program.cs

158 lines · 7.8 KB · plain text · download

1using System;
2using System.Buffers.Binary;
3using System.IO;
4using System.Security.Cryptography;
5using System.Linq;
6using System.Text;
7
8internal static class MacSign
9{
10 private const uint MH_MAGIC_64 = 0xfeedfacf;
11 private const uint LC_SEGMENT_64 = 0x19, LC_CODE_SIGNATURE = 0x1d;
12 private const int PageShift = 12, PageSize = 1 << PageShift;
13
14 private static int Main(string[] args)
15 {
16 if (args.Length == 2 && args[0] == "--verify") return Verify(args[1]);
17 if (args.Length < 2) { Console.Error.WriteLine("usage: macsign <file> <identifier> [Info.plist]"); return 2; }
18 try
19 {
20 byte[] plist = args.Length > 2 && File.Exists(args[2]) ? File.ReadAllBytes(args[2]) : null;
21 Sign(args[0], args[1], plist);
22 Console.WriteLine("signed " + Path.GetFileName(args[0]));
23 return 0;
24 }
25 catch (Exception e) { Console.Error.WriteLine("macsign: " + e.Message); return 1; }
26 }
27
28 private static int Verify(string file)
29 {
30 byte[] f = File.ReadAllBytes(file);
31 if (U32(f, 0) != MH_MAGIC_64) { Console.WriteLine("not thin mach-o"); return 1; }
32 int ncmds = (int)U32(f, 16), p = 32, dataoff = -1, datasize = 0;
33 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); }
34 if (dataoff < 0) { Console.WriteLine("no signature"); return 1; }
35 if (dataoff + datasize != f.Length) { Console.WriteLine("signature isn't at the end of the file"); return 1; }
36 uint BE(int o) => BinaryPrimitives.ReadUInt32BigEndian(f.AsSpan(o));
37 if (BE(dataoff) != 0xfade0cc0) { Console.WriteLine("bad superblob"); return 1; }
38 int count = (int)BE(dataoff + 8), cd = -1;
39 for (int i = 0; i < count; i++) if (BE(dataoff + 12 + i * 8) == 0) cd = dataoff + (int)BE(dataoff + 16 + i * 8);
40 if (cd < 0 || BE(cd) != 0xfade0c02) { Console.WriteLine("no code directory"); return 1; }
41 int hashOff = (int)BE(cd + 16), nCode = (int)BE(cd + 28), limit = (int)BE(cd + 32), shift = f[cd + 39];
42 if (limit != dataoff || f[cd + 36] != 32 || f[cd + 37] != 2) { Console.WriteLine("bad code directory fields"); return 1; }
43 string ident = Encoding.UTF8.GetString(f, cd + (int)BE(cd + 20), 64).Split('\0')[0];
44 for (int i = 0; i < nCode; i++)
45 {
46 int start = i << shift, len = Math.Min(1 << shift, limit - start);
47 if (!SHA256.HashData(f.AsSpan(start, len)).AsSpan().SequenceEqual(f.AsSpan(cd + hashOff + i * 32, 32))) { Console.WriteLine("page " + i + " hash mismatch"); return 1; }
48 }
49 Console.WriteLine("ok: " + nCode + " pages, id " + ident + ", flags 0x" + BE(cd + 12).ToString("x"));
50 return 0;
51 }
52
53 private static uint U32(byte[] b, int o) => BinaryPrimitives.ReadUInt32LittleEndian(b.AsSpan(o));
54 private static ulong U64(byte[] b, int o) => BinaryPrimitives.ReadUInt64LittleEndian(b.AsSpan(o));
55 private static void W32(byte[] b, int o, uint v) => BinaryPrimitives.WriteUInt32LittleEndian(b.AsSpan(o), v);
56 private static void W64(byte[] b, int o, ulong v) => BinaryPrimitives.WriteUInt64LittleEndian(b.AsSpan(o), v);
57
58 private static void Sign(string file, string ident, byte[] plist)
59 {
60 byte[] f = File.ReadAllBytes(file);
61 if (U32(f, 0) != MH_MAGIC_64) throw new Exception("not a thin 64-bit Mach-O file");
62 int ncmds = (int)U32(f, 16), sizeofcmds = (int)U32(f, 20);
63 int p = 32, linkedit = -1, text = -1;
64 ulong firstSection = ulong.MaxValue;
65 for (int i = 0; i < ncmds; i++)
66 {
67 uint cmd = U32(f, p), size = U32(f, p + 4);
68 if (cmd == LC_CODE_SIGNATURE) throw new Exception("already signed");
69 if (cmd == LC_SEGMENT_64)
70 {
71 string name = Encoding.ASCII.GetString(f, p + 8, 16).TrimEnd('\0');
72 if (name == "__LINKEDIT") linkedit = p;
73 if (name == "__TEXT") text = p;
74 int nsects = (int)U32(f, p + 64);
75 for (int s = 0; s < nsects; s++)
76 {
77 uint off = U32(f, p + 72 + s * 80 + 48);
78 if (off != 0 && off < firstSection) firstSection = off;
79 }
80 }
81 p += (int)size;
82 }
83 if (linkedit < 0 || text < 0) throw new Exception("missing __TEXT or __LINKEDIT");
84 int cmdEnd = 32 + sizeofcmds;
85 if ((ulong)(cmdEnd + 16) > firstSection) throw new Exception("no room in the header for a signature");
86 for (int i = cmdEnd; i < cmdEnd + 16; i++) if (f[i] != 0) throw new Exception("header padding isn't empty");
87
88 ulong leOff = U64(f, linkedit + 40), leSize = U64(f, linkedit + 48);
89 if (leOff + leSize > (ulong)f.Length) throw new Exception("__LINKEDIT runs past the end of the file");
90 int codeLimit = (f.Length + 15) & ~15;
91
92 byte[] identBytes = Encoding.UTF8.GetBytes(ident + "\0");
93 int nSpecial = 2, nCode = (codeLimit + PageSize - 1) / PageSize;
94 const int cdHeader = 88;
95 int hashOffset = cdHeader + identBytes.Length + nSpecial * 32;
96 int cdLen = hashOffset + nCode * 32;
97 byte[] req = new byte[12];
98 BinaryPrimitives.WriteUInt32BigEndian(req.AsSpan(0), 0xfade0c01);
99 BinaryPrimitives.WriteUInt32BigEndian(req.AsSpan(4), 12);
100 byte[] cms = new byte[8];
101 BinaryPrimitives.WriteUInt32BigEndian(cms.AsSpan(0), 0xfade0b01);
102 BinaryPrimitives.WriteUInt32BigEndian(cms.AsSpan(4), 8);
103 int superHeader = 12 + 3 * 8;
104 int sigLen = superHeader + cdLen + req.Length + cms.Length;
105 int sigSize = (sigLen + 15) & ~15;
106
107 byte[] o = new byte[codeLimit + sigSize];
108 Buffer.BlockCopy(f, 0, o, 0, f.Length);
109 W32(o, 16, (uint)(ncmds + 1));
110 W32(o, 20, (uint)(sizeofcmds + 16));
111 W32(o, cmdEnd, LC_CODE_SIGNATURE);
112 W32(o, cmdEnd + 4, 16);
113 W32(o, cmdEnd + 8, (uint)codeLimit);
114 W32(o, cmdEnd + 12, (uint)sigSize);
115 ulong newLeSize = (ulong)(codeLimit + sigSize) - leOff;
116 W64(o, linkedit + 48, newLeSize);
117 W64(o, linkedit + 32, (newLeSize + 0x3fff) & ~0x3fffUL);
118
119 byte[] cd = new byte[cdLen];
120 void B32(int at, uint v) => BinaryPrimitives.WriteUInt32BigEndian(cd.AsSpan(at), v);
121 void B64(int at, ulong v) => BinaryPrimitives.WriteUInt64BigEndian(cd.AsSpan(at), v);
122 B32(0, 0xfade0c02);
123 B32(4, (uint)cdLen);
124 B32(8, 0x20400);
125 B32(12, 0x2);
126 B32(16, (uint)hashOffset);
127 B32(20, cdHeader);
128 B32(24, (uint)nSpecial);
129 B32(28, (uint)nCode);
130 B32(32, (uint)codeLimit);
131 cd[36] = 32; cd[37] = 2; cd[38] = 0; cd[39] = PageShift;
132 B64(56, 0);
133 B64(64, U64(f, text + 40));
134 B64(72, U64(f, text + 48));
135 B64(80, 1);
136 Buffer.BlockCopy(identBytes, 0, cd, cdHeader, identBytes.Length);
137 if (plist != null) Buffer.BlockCopy(SHA256.HashData(plist), 0, cd, hashOffset - 32, 32);
138 Buffer.BlockCopy(SHA256.HashData(req), 0, cd, hashOffset - 64, 32);
139 for (int i = 0; i < nCode; i++)
140 {
141 int start = i * PageSize, len = Math.Min(PageSize, codeLimit - start);
142 Buffer.BlockCopy(SHA256.HashData(o.AsSpan(start, len)), 0, cd, hashOffset + i * 32, 32);
143 }
144
145 int at = codeLimit;
146 void S32(uint v) { BinaryPrimitives.WriteUInt32BigEndian(o.AsSpan(at), v); at += 4; }
147 S32(0xfade0cc0);
148 S32((uint)sigLen);
149 S32(3);
150 S32(0); S32((uint)superHeader);
151 S32(2); S32((uint)(superHeader + cdLen));
152 S32(0x10000); S32((uint)(superHeader + cdLen + req.Length));
153 Buffer.BlockCopy(cd, 0, o, at, cd.Length); at += cd.Length;
154 Buffer.BlockCopy(req, 0, o, at, req.Length); at += req.Length;
155 Buffer.BlockCopy(cms, 0, o, at, cms.Length);
156 File.WriteAllBytes(file, o);
157 }
158}