codesign.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*************************************************************************/
  2. /* codesign.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "codesign.h"
  31. #include "lipo.h"
  32. #include "macho.h"
  33. #include "plist.h"
  34. #include "core/os/os.h"
  35. #include "editor/editor_paths.h"
  36. #include "editor/editor_settings.h"
  37. #include "modules/modules_enabled.gen.h" // For regex.
  38. #include <ctime>
  39. #ifdef MODULE_REGEX_ENABLED
  40. /*************************************************************************/
  41. /* CodeSignCodeResources */
  42. /*************************************************************************/
  43. String CodeSignCodeResources::hash_sha1_base64(const String &p_path) {
  44. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::READ);
  45. ERR_FAIL_COND_V_MSG(fa.is_null(), String(), vformat("CodeSign/CodeResources: Can't open file: \"%s\".", p_path));
  46. CryptoCore::SHA1Context ctx;
  47. ctx.start();
  48. unsigned char step[4096];
  49. while (true) {
  50. uint64_t br = fa->get_buffer(step, 4096);
  51. if (br > 0) {
  52. ctx.update(step, br);
  53. }
  54. if (br < 4096) {
  55. break;
  56. }
  57. }
  58. unsigned char hash[0x14];
  59. ctx.finish(hash);
  60. return CryptoCore::b64_encode_str(hash, 0x14);
  61. }
  62. String CodeSignCodeResources::hash_sha256_base64(const String &p_path) {
  63. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::READ);
  64. ERR_FAIL_COND_V_MSG(fa.is_null(), String(), vformat("CodeSign/CodeResources: Can't open file: \"%s\".", p_path));
  65. CryptoCore::SHA256Context ctx;
  66. ctx.start();
  67. unsigned char step[4096];
  68. while (true) {
  69. uint64_t br = fa->get_buffer(step, 4096);
  70. if (br > 0) {
  71. ctx.update(step, br);
  72. }
  73. if (br < 4096) {
  74. break;
  75. }
  76. }
  77. unsigned char hash[0x20];
  78. ctx.finish(hash);
  79. return CryptoCore::b64_encode_str(hash, 0x20);
  80. }
  81. void CodeSignCodeResources::add_rule1(const String &p_rule, const String &p_key, int p_weight, bool p_store) {
  82. rules1.push_back(CRRule(p_rule, p_key, p_weight, p_store));
  83. }
  84. void CodeSignCodeResources::add_rule2(const String &p_rule, const String &p_key, int p_weight, bool p_store) {
  85. rules2.push_back(CRRule(p_rule, p_key, p_weight, p_store));
  86. }
  87. CodeSignCodeResources::CRMatch CodeSignCodeResources::match_rules1(const String &p_path) const {
  88. CRMatch found = CRMatch::CR_MATCH_NO;
  89. int weight = 0;
  90. for (int i = 0; i < rules1.size(); i++) {
  91. RegEx regex = RegEx(rules1[i].file_pattern);
  92. if (regex.search(p_path).is_valid()) {
  93. if (rules1[i].key == "omit") {
  94. return CRMatch::CR_MATCH_NO;
  95. } else if (rules1[i].key == "nested") {
  96. if (weight <= rules1[i].weight) {
  97. found = CRMatch::CR_MATCH_NESTED;
  98. weight = rules1[i].weight;
  99. }
  100. } else if (rules1[i].key == "optional") {
  101. if (weight <= rules1[i].weight) {
  102. found = CRMatch::CR_MATCH_OPTIONAL;
  103. weight = rules1[i].weight;
  104. }
  105. } else {
  106. if (weight <= rules1[i].weight) {
  107. found = CRMatch::CR_MATCH_YES;
  108. weight = rules1[i].weight;
  109. }
  110. }
  111. }
  112. }
  113. return found;
  114. }
  115. CodeSignCodeResources::CRMatch CodeSignCodeResources::match_rules2(const String &p_path) const {
  116. CRMatch found = CRMatch::CR_MATCH_NO;
  117. int weight = 0;
  118. for (int i = 0; i < rules2.size(); i++) {
  119. RegEx regex = RegEx(rules2[i].file_pattern);
  120. if (regex.search(p_path).is_valid()) {
  121. if (rules2[i].key == "omit") {
  122. return CRMatch::CR_MATCH_NO;
  123. } else if (rules2[i].key == "nested") {
  124. if (weight <= rules2[i].weight) {
  125. found = CRMatch::CR_MATCH_NESTED;
  126. weight = rules2[i].weight;
  127. }
  128. } else if (rules2[i].key == "optional") {
  129. if (weight <= rules2[i].weight) {
  130. found = CRMatch::CR_MATCH_OPTIONAL;
  131. weight = rules2[i].weight;
  132. }
  133. } else {
  134. if (weight <= rules2[i].weight) {
  135. found = CRMatch::CR_MATCH_YES;
  136. weight = rules2[i].weight;
  137. }
  138. }
  139. }
  140. }
  141. return found;
  142. }
  143. bool CodeSignCodeResources::add_file1(const String &p_root, const String &p_path) {
  144. CRMatch found = match_rules1(p_path);
  145. if (found != CRMatch::CR_MATCH_YES && found != CRMatch::CR_MATCH_OPTIONAL) {
  146. return true; // No match.
  147. }
  148. CRFile f;
  149. f.name = p_path;
  150. f.optional = (found == CRMatch::CR_MATCH_OPTIONAL);
  151. f.nested = false;
  152. f.hash = hash_sha1_base64(p_root.path_join(p_path));
  153. print_verbose(vformat("CodeSign/CodeResources: File(V1) %s hash1:%s", f.name, f.hash));
  154. files1.push_back(f);
  155. return true;
  156. }
  157. bool CodeSignCodeResources::add_file2(const String &p_root, const String &p_path) {
  158. CRMatch found = match_rules2(p_path);
  159. if (found == CRMatch::CR_MATCH_NESTED) {
  160. return add_nested_file(p_root, p_path, p_root.path_join(p_path));
  161. }
  162. if (found != CRMatch::CR_MATCH_YES && found != CRMatch::CR_MATCH_OPTIONAL) {
  163. return true; // No match.
  164. }
  165. CRFile f;
  166. f.name = p_path;
  167. f.optional = (found == CRMatch::CR_MATCH_OPTIONAL);
  168. f.nested = false;
  169. f.hash = hash_sha1_base64(p_root.path_join(p_path));
  170. f.hash2 = hash_sha256_base64(p_root.path_join(p_path));
  171. print_verbose(vformat("CodeSign/CodeResources: File(V2) %s hash1:%s hash2:%s", f.name, f.hash, f.hash2));
  172. files2.push_back(f);
  173. return true;
  174. }
  175. bool CodeSignCodeResources::add_nested_file(const String &p_root, const String &p_path, const String &p_exepath) {
  176. #define CLEANUP() \
  177. if (files_to_add.size() > 1) { \
  178. for (int j = 0; j < files_to_add.size(); j++) { \
  179. da->remove(files_to_add[j]); \
  180. } \
  181. }
  182. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  183. ERR_FAIL_COND_V(da.is_null(), false);
  184. Vector<String> files_to_add;
  185. if (LipO::is_lipo(p_exepath)) {
  186. String tmp_path_name = EditorPaths::get_singleton()->get_cache_dir().path_join("_lipo");
  187. Error err = da->make_dir_recursive(tmp_path_name);
  188. ERR_FAIL_COND_V_MSG(err != OK, false, vformat("CodeSign/CodeResources: Failed to create \"%s\" subfolder.", tmp_path_name));
  189. LipO lip;
  190. if (lip.open_file(p_exepath)) {
  191. for (int i = 0; i < lip.get_arch_count(); i++) {
  192. if (!lip.extract_arch(i, tmp_path_name.path_join("_rqexe_" + itos(i)))) {
  193. CLEANUP();
  194. ERR_FAIL_V_MSG(false, "CodeSign/CodeResources: Failed to extract thin binary.");
  195. }
  196. files_to_add.push_back(tmp_path_name.path_join("_rqexe_" + itos(i)));
  197. }
  198. }
  199. } else if (MachO::is_macho(p_exepath)) {
  200. files_to_add.push_back(p_exepath);
  201. }
  202. CRFile f;
  203. f.name = p_path;
  204. f.optional = false;
  205. f.nested = true;
  206. for (int i = 0; i < files_to_add.size(); i++) {
  207. MachO mh;
  208. if (!mh.open_file(files_to_add[i])) {
  209. CLEANUP();
  210. ERR_FAIL_V_MSG(false, "CodeSign/CodeResources: Invalid executable file.");
  211. }
  212. PackedByteArray hash = mh.get_cdhash_sha256(); // Use SHA-256 variant, if available.
  213. if (hash.size() != 0x20) {
  214. hash = mh.get_cdhash_sha1(); // Use SHA-1 instead.
  215. if (hash.size() != 0x14) {
  216. CLEANUP();
  217. ERR_FAIL_V_MSG(false, "CodeSign/CodeResources: Unsigned nested executable file.");
  218. }
  219. }
  220. hash.resize(0x14); // Always clamp to 0x14 size.
  221. f.hash = CryptoCore::b64_encode_str(hash.ptr(), hash.size());
  222. PackedByteArray rq_blob = mh.get_requirements();
  223. String req_string;
  224. if (rq_blob.size() > 8) {
  225. CodeSignRequirements rq = CodeSignRequirements(rq_blob);
  226. Vector<String> rqs = rq.parse_requirements();
  227. for (int j = 0; j < rqs.size(); j++) {
  228. if (rqs[j].begins_with("designated => ")) {
  229. req_string = rqs[j].replace("designated => ", "");
  230. }
  231. }
  232. }
  233. if (req_string.is_empty()) {
  234. req_string = "cdhash H\"" + String::hex_encode_buffer(hash.ptr(), hash.size()) + "\"";
  235. }
  236. print_verbose(vformat("CodeSign/CodeResources: Nested object %s (cputype: %d) cdhash:%s designated rq:%s", f.name, mh.get_cputype(), f.hash, req_string));
  237. if (f.requirements != req_string) {
  238. if (i != 0) {
  239. f.requirements += " or ";
  240. }
  241. f.requirements += req_string;
  242. }
  243. }
  244. files2.push_back(f);
  245. CLEANUP();
  246. return true;
  247. #undef CLEANUP
  248. }
  249. bool CodeSignCodeResources::add_folder_recursive(const String &p_root, const String &p_path, const String &p_main_exe_path) {
  250. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  251. ERR_FAIL_COND_V(da.is_null(), false);
  252. Error err = da->change_dir(p_root.path_join(p_path));
  253. ERR_FAIL_COND_V(err != OK, false);
  254. bool ret = true;
  255. da->list_dir_begin();
  256. String n = da->get_next();
  257. while (n != String()) {
  258. if (n != "." && n != "..") {
  259. String path = p_root.path_join(p_path).path_join(n);
  260. if (path == p_main_exe_path) {
  261. n = da->get_next();
  262. continue; // Skip main executable.
  263. }
  264. if (da->current_is_dir()) {
  265. CRMatch found = match_rules2(p_path.path_join(n));
  266. String fmw_ver = "Current"; // Framework version (default).
  267. String info_path;
  268. String main_exe;
  269. bool bundle = false;
  270. if (da->file_exists(path.path_join("Contents/Info.plist"))) {
  271. info_path = path.path_join("Contents/Info.plist");
  272. main_exe = path.path_join("Contents/MacOS");
  273. bundle = true;
  274. } else if (da->file_exists(path.path_join(vformat("Versions/%s/Resources/Info.plist", fmw_ver)))) {
  275. info_path = path.path_join(vformat("Versions/%s/Resources/Info.plist", fmw_ver));
  276. main_exe = path.path_join(vformat("Versions/%s", fmw_ver));
  277. bundle = true;
  278. } else if (da->file_exists(path.path_join("Info.plist"))) {
  279. info_path = path.path_join("Info.plist");
  280. main_exe = path;
  281. bundle = true;
  282. }
  283. if (bundle && found == CRMatch::CR_MATCH_NESTED && !info_path.is_empty()) {
  284. // Read Info.plist.
  285. PList info_plist;
  286. if (info_plist.load_file(info_path)) {
  287. if (info_plist.get_root()->data_type == PList::PLNodeType::PL_NODE_TYPE_DICT && info_plist.get_root()->data_dict.has("CFBundleExecutable")) {
  288. main_exe = main_exe.path_join(String::utf8(info_plist.get_root()->data_dict["CFBundleExecutable"]->data_string.get_data()));
  289. } else {
  290. ERR_FAIL_V_MSG(false, "CodeSign/CodeResources: Invalid Info.plist, no exe name.");
  291. }
  292. } else {
  293. ERR_FAIL_V_MSG(false, "CodeSign/CodeResources: Invalid Info.plist, can't load.");
  294. }
  295. ret = ret && add_nested_file(p_root, p_path.path_join(n), main_exe);
  296. } else {
  297. ret = ret && add_folder_recursive(p_root, p_path.path_join(n), p_main_exe_path);
  298. }
  299. } else {
  300. ret = ret && add_file1(p_root, p_path.path_join(n));
  301. ret = ret && add_file2(p_root, p_path.path_join(n));
  302. }
  303. }
  304. n = da->get_next();
  305. }
  306. da->list_dir_end();
  307. return ret;
  308. }
  309. bool CodeSignCodeResources::save_to_file(const String &p_path) {
  310. PList pl;
  311. print_verbose(vformat("CodeSign/CodeResources: Writing to file: %s", p_path));
  312. // Write version 1 hashes.
  313. Ref<PListNode> files1_dict = PListNode::new_dict();
  314. pl.get_root()->push_subnode(files1_dict, "files");
  315. for (int i = 0; i < files1.size(); i++) {
  316. if (files1[i].optional) {
  317. Ref<PListNode> file_dict = PListNode::new_dict();
  318. files1_dict->push_subnode(file_dict, files1[i].name);
  319. file_dict->push_subnode(PListNode::new_data(files1[i].hash), "hash");
  320. file_dict->push_subnode(PListNode::new_bool(true), "optional");
  321. } else {
  322. files1_dict->push_subnode(PListNode::new_data(files1[i].hash), files1[i].name);
  323. }
  324. }
  325. // Write version 2 hashes.
  326. Ref<PListNode> files2_dict = PListNode::new_dict();
  327. pl.get_root()->push_subnode(files2_dict, "files2");
  328. for (int i = 0; i < files2.size(); i++) {
  329. Ref<PListNode> file_dict = PListNode::new_dict();
  330. files2_dict->push_subnode(file_dict, files2[i].name);
  331. if (files2[i].nested) {
  332. file_dict->push_subnode(PListNode::new_data(files2[i].hash), "cdhash");
  333. file_dict->push_subnode(PListNode::new_string(files2[i].requirements), "requirement");
  334. } else {
  335. file_dict->push_subnode(PListNode::new_data(files2[i].hash), "hash");
  336. file_dict->push_subnode(PListNode::new_data(files2[i].hash2), "hash2");
  337. if (files2[i].optional) {
  338. file_dict->push_subnode(PListNode::new_bool(true), "optional");
  339. }
  340. }
  341. }
  342. // Write version 1 rules.
  343. Ref<PListNode> rules1_dict = PListNode::new_dict();
  344. pl.get_root()->push_subnode(rules1_dict, "rules");
  345. for (int i = 0; i < rules1.size(); i++) {
  346. if (rules1[i].store) {
  347. if (rules1[i].key.is_empty() && rules1[i].weight <= 0) {
  348. rules1_dict->push_subnode(PListNode::new_bool(true), rules1[i].file_pattern);
  349. } else {
  350. Ref<PListNode> rule_dict = PListNode::new_dict();
  351. rules1_dict->push_subnode(rule_dict, rules1[i].file_pattern);
  352. if (!rules1[i].key.is_empty()) {
  353. rule_dict->push_subnode(PListNode::new_bool(true), rules1[i].key);
  354. }
  355. if (rules1[i].weight != 1) {
  356. rule_dict->push_subnode(PListNode::new_real(rules1[i].weight), "weight");
  357. }
  358. }
  359. }
  360. }
  361. // Write version 2 rules.
  362. Ref<PListNode> rules2_dict = PListNode::new_dict();
  363. pl.get_root()->push_subnode(rules2_dict, "rules2");
  364. for (int i = 0; i < rules2.size(); i++) {
  365. if (rules2[i].store) {
  366. if (rules2[i].key.is_empty() && rules2[i].weight <= 0) {
  367. rules2_dict->push_subnode(PListNode::new_bool(true), rules2[i].file_pattern);
  368. } else {
  369. Ref<PListNode> rule_dict = PListNode::new_dict();
  370. rules2_dict->push_subnode(rule_dict, rules2[i].file_pattern);
  371. if (!rules2[i].key.is_empty()) {
  372. rule_dict->push_subnode(PListNode::new_bool(true), rules2[i].key);
  373. }
  374. if (rules2[i].weight != 1) {
  375. rule_dict->push_subnode(PListNode::new_real(rules2[i].weight), "weight");
  376. }
  377. }
  378. }
  379. }
  380. String text = pl.save_text();
  381. ERR_FAIL_COND_V_MSG(text.is_empty(), false, "CodeSign/CodeResources: Generating resources PList failed.");
  382. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  383. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("CodeSign/CodeResources: Can't open file: \"%s\".", p_path));
  384. CharString cs = text.utf8();
  385. fa->store_buffer((const uint8_t *)cs.ptr(), cs.length());
  386. return true;
  387. }
  388. /*************************************************************************/
  389. /* CodeSignRequirements */
  390. /*************************************************************************/
  391. CodeSignRequirements::CodeSignRequirements() {
  392. blob.append_array({ 0xFA, 0xDE, 0x0C, 0x01 }); // Requirement set magic.
  393. blob.append_array({ 0x00, 0x00, 0x00, 0x0C }); // Length of requirements set (12 bytes).
  394. blob.append_array({ 0x00, 0x00, 0x00, 0x00 }); // Empty.
  395. }
  396. CodeSignRequirements::CodeSignRequirements(const PackedByteArray &p_data) {
  397. blob = p_data;
  398. }
  399. _FORCE_INLINE_ void CodeSignRequirements::_parse_certificate_slot(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  400. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  401. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  402. r_out += "certificate ";
  403. uint32_t tag_slot = _R(r_pos);
  404. if (tag_slot == 0x00000000) {
  405. r_out += "leaf";
  406. } else if (tag_slot == 0xffffffff) {
  407. r_out += "root";
  408. } else {
  409. r_out += itos((int32_t)tag_slot);
  410. }
  411. r_pos += 4;
  412. #undef _R
  413. }
  414. _FORCE_INLINE_ void CodeSignRequirements::_parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  415. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  416. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  417. uint32_t key_size = _R(r_pos);
  418. ERR_FAIL_COND_MSG(r_pos + key_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
  419. CharString key;
  420. key.resize(key_size);
  421. memcpy(key.ptrw(), blob.ptr() + r_pos + 4, key_size);
  422. r_pos += 4 + key_size + PAD(key_size, 4);
  423. r_out += "[" + String::utf8(key, key_size) + "]";
  424. #undef _R
  425. }
  426. _FORCE_INLINE_ void CodeSignRequirements::_parse_oid_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  427. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  428. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  429. uint32_t key_size = _R(r_pos);
  430. ERR_FAIL_COND_MSG(r_pos + key_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
  431. r_out += "[field.";
  432. r_out += itos(blob[r_pos + 4] / 40) + ".";
  433. r_out += itos(blob[r_pos + 4] % 40);
  434. uint32_t spos = r_pos + 5;
  435. while (spos < r_pos + 4 + key_size) {
  436. r_out += ".";
  437. if (blob[spos] <= 127) {
  438. r_out += itos(blob[spos]);
  439. spos += 1;
  440. } else {
  441. uint32_t x = (0x7F & blob[spos]) << 7;
  442. spos += 1;
  443. while (blob[spos] > 127) {
  444. x = (x + (0x7F & blob[spos])) << 7;
  445. spos += 1;
  446. }
  447. x = (x + (0x7F & blob[spos]));
  448. r_out += itos(x);
  449. spos += 1;
  450. }
  451. }
  452. r_out += "]";
  453. r_pos += 4 + key_size + PAD(key_size, 4);
  454. #undef _R
  455. }
  456. _FORCE_INLINE_ void CodeSignRequirements::_parse_hash_string(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  457. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  458. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  459. uint32_t tag_size = _R(r_pos);
  460. ERR_FAIL_COND_MSG(r_pos + tag_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
  461. PackedByteArray data;
  462. data.resize(tag_size);
  463. memcpy(data.ptrw(), blob.ptr() + r_pos + 4, tag_size);
  464. r_out += "H\"" + String::hex_encode_buffer(data.ptr(), data.size()) + "\"";
  465. r_pos += 4 + tag_size + PAD(tag_size, 4);
  466. #undef _R
  467. }
  468. _FORCE_INLINE_ void CodeSignRequirements::_parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  469. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  470. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  471. uint32_t key_size = _R(r_pos);
  472. ERR_FAIL_COND_MSG(r_pos + key_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
  473. CharString key;
  474. key.resize(key_size);
  475. memcpy(key.ptrw(), blob.ptr() + r_pos + 4, key_size);
  476. r_pos += 4 + key_size + PAD(key_size, 4);
  477. r_out += "\"" + String::utf8(key, key_size) + "\"";
  478. #undef _R
  479. }
  480. _FORCE_INLINE_ void CodeSignRequirements::_parse_date(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  481. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  482. ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
  483. uint32_t date = _R(r_pos);
  484. time_t t = 978307200 + date;
  485. struct tm lt;
  486. #ifdef WINDOWS_ENABLED
  487. gmtime_s(&lt, &t);
  488. #else
  489. gmtime_r(&t, &lt);
  490. #endif
  491. r_out += vformat("<%04d-%02d-%02d ", (int)(1900 + lt.tm_year), (int)(lt.tm_mon + 1), (int)(lt.tm_mday)) + vformat("%02d:%02d:%02d +0000>", (int)(lt.tm_hour), (int)(lt.tm_min), (int)(lt.tm_sec));
  492. #undef _R
  493. }
  494. _FORCE_INLINE_ bool CodeSignRequirements::_parse_match(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
  495. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  496. ERR_FAIL_COND_V_MSG(r_pos >= p_rq_size, false, "CodeSign/Requirements: Out of bounds.");
  497. uint32_t match = _R(r_pos);
  498. r_pos += 4;
  499. switch (match) {
  500. case 0x00000000: {
  501. r_out += "exists";
  502. } break;
  503. case 0x00000001: {
  504. r_out += "= ";
  505. _parse_value(r_pos, r_out, p_rq_size);
  506. } break;
  507. case 0x00000002: {
  508. r_out += "~ ";
  509. _parse_value(r_pos, r_out, p_rq_size);
  510. } break;
  511. case 0x00000003: {
  512. r_out += "= *";
  513. _parse_value(r_pos, r_out, p_rq_size);
  514. } break;
  515. case 0x00000004: {
  516. r_out += "= ";
  517. _parse_value(r_pos, r_out, p_rq_size);
  518. r_out += "*";
  519. } break;
  520. case 0x00000005: {
  521. r_out += "< ";
  522. _parse_value(r_pos, r_out, p_rq_size);
  523. } break;
  524. case 0x00000006: {
  525. r_out += "> ";
  526. _parse_value(r_pos, r_out, p_rq_size);
  527. } break;
  528. case 0x00000007: {
  529. r_out += "<= ";
  530. _parse_value(r_pos, r_out, p_rq_size);
  531. } break;
  532. case 0x00000008: {
  533. r_out += ">= ";
  534. _parse_value(r_pos, r_out, p_rq_size);
  535. } break;
  536. case 0x00000009: {
  537. r_out += "= ";
  538. _parse_date(r_pos, r_out, p_rq_size);
  539. } break;
  540. case 0x0000000A: {
  541. r_out += "< ";
  542. _parse_date(r_pos, r_out, p_rq_size);
  543. } break;
  544. case 0x0000000B: {
  545. r_out += "> ";
  546. _parse_date(r_pos, r_out, p_rq_size);
  547. } break;
  548. case 0x0000000C: {
  549. r_out += "<= ";
  550. _parse_date(r_pos, r_out, p_rq_size);
  551. } break;
  552. case 0x0000000D: {
  553. r_out += ">= ";
  554. _parse_date(r_pos, r_out, p_rq_size);
  555. } break;
  556. case 0x0000000E: {
  557. r_out += "absent";
  558. } break;
  559. default: {
  560. return false;
  561. }
  562. }
  563. return true;
  564. #undef _R
  565. }
  566. Vector<String> CodeSignRequirements::parse_requirements() const {
  567. #define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
  568. Vector<String> list;
  569. // Read requirements set header.
  570. ERR_FAIL_COND_V_MSG(blob.size() < 12, list, "CodeSign/Requirements: Blob is too small.");
  571. uint32_t magic = _R(0);
  572. ERR_FAIL_COND_V_MSG(magic != 0xfade0c01, list, "CodeSign/Requirements: Invalid set magic.");
  573. uint32_t size = _R(4);
  574. ERR_FAIL_COND_V_MSG(size != (uint32_t)blob.size(), list, "CodeSign/Requirements: Invalid set size.");
  575. uint32_t count = _R(8);
  576. for (uint32_t i = 0; i < count; i++) {
  577. String out;
  578. // Read requirement header.
  579. uint32_t rq_type = _R(12 + i * 8);
  580. uint32_t rq_offset = _R(12 + i * 8 + 4);
  581. ERR_FAIL_COND_V_MSG(rq_offset + 12 >= (uint32_t)blob.size(), list, "CodeSign/Requirements: Invalid requirement offset.");
  582. switch (rq_type) {
  583. case 0x00000001: {
  584. out += "host => ";
  585. } break;
  586. case 0x00000002: {
  587. out += "guest => ";
  588. } break;
  589. case 0x00000003: {
  590. out += "designated => ";
  591. } break;
  592. case 0x00000004: {
  593. out += "library => ";
  594. } break;
  595. case 0x00000005: {
  596. out += "plugin => ";
  597. } break;
  598. default: {
  599. ERR_FAIL_V_MSG(list, "CodeSign/Requirements: Invalid requirement type.");
  600. }
  601. }
  602. uint32_t rq_magic = _R(rq_offset);
  603. uint32_t rq_size = _R(rq_offset + 4);
  604. uint32_t rq_ver = _R(rq_offset + 8);
  605. uint32_t pos = rq_offset + 12;
  606. ERR_FAIL_COND_V_MSG(rq_magic != 0xfade0c00, list, "CodeSign/Requirements: Invalid requirement magic.");
  607. ERR_FAIL_COND_V_MSG(rq_ver != 0x00000001, list, "CodeSign/Requirements: Invalid requirement version.");
  608. // Read requirement tokens.
  609. List<String> tokens;
  610. while (pos < rq_offset + rq_size) {
  611. uint32_t rq_tag = _R(pos);
  612. pos += 4;
  613. String token;
  614. switch (rq_tag) {
  615. case 0x00000000: {
  616. token = "false";
  617. } break;
  618. case 0x00000001: {
  619. token = "true";
  620. } break;
  621. case 0x00000002: {
  622. token = "identifier ";
  623. _parse_value(pos, token, rq_offset + rq_size);
  624. } break;
  625. case 0x00000003: {
  626. token = "anchor apple";
  627. } break;
  628. case 0x00000004: {
  629. _parse_certificate_slot(pos, token, rq_offset + rq_size);
  630. token += " ";
  631. _parse_hash_string(pos, token, rq_offset + rq_size);
  632. } break;
  633. case 0x00000005: {
  634. token = "info";
  635. _parse_key(pos, token, rq_offset + rq_size);
  636. token += " = ";
  637. _parse_value(pos, token, rq_offset + rq_size);
  638. } break;
  639. case 0x00000006: {
  640. token = "and";
  641. } break;
  642. case 0x00000007: {
  643. token = "or";
  644. } break;
  645. case 0x00000008: {
  646. token = "cdhash ";
  647. _parse_hash_string(pos, token, rq_offset + rq_size);
  648. } break;
  649. case 0x00000009: {
  650. token = "!";
  651. } break;
  652. case 0x0000000A: {
  653. token = "info";
  654. _parse_key(pos, token, rq_offset + rq_size);
  655. token += " ";
  656. ERR_FAIL_COND_V_MSG(!_parse_match(pos, token, rq_offset + rq_size), list, "CodeSign/Requirements: Unsupported match suffix.");
  657. } break;
  658. case 0x0000000B: {
  659. _parse_certificate_slot(pos, token, rq_offset + rq_size);
  660. _parse_key(pos, token, rq_offset + rq_size);
  661. token += " ";
  662. ERR_FAIL_COND_V_MSG(!_parse_match(pos, token, rq_offset + rq_size), list, "CodeSign/Requirements: Unsupported match suffix.");
  663. } break;
  664. case 0x0000000C: {
  665. _parse_certificate_slot(pos, token, rq_offset + rq_size);
  666. token += " trusted";
  667. } break;
  668. case 0x0000000D: {
  669. token = "anchor trusted";
  670. } break;
  671. case 0x0000000E: {
  672. _parse_certificate_slot(pos, token, rq_offset + rq_size);
  673. _parse_oid_key(pos, token, rq_offset + rq_size);
  674. token += " ";
  675. ERR_FAIL_COND_V_MSG(!_parse_match(pos, token, rq_offset + rq_size), list, "CodeSign/Requirements: Unsupported match suffix.");
  676. } break;
  677. case 0x0000000F: {
  678. token = "anchor apple generic";
  679. } break;
  680. default: {
  681. ERR_FAIL_V_MSG(list, "CodeSign/Requirements: Invalid requirement token.");
  682. } break;
  683. }
  684. tokens.push_back(token);
  685. }
  686. // Polish to infix notation (w/o bracket optimization).
  687. for (List<String>::Element *E = tokens.back(); E; E = E->prev()) {
  688. if (E->get() == "and") {
  689. ERR_FAIL_COND_V_MSG(!E->next() || !E->next()->next(), list, "CodeSign/Requirements: Invalid token sequence.");
  690. String token = "(" + E->next()->get() + " and " + E->next()->next()->get() + ")";
  691. tokens.erase(E->next()->next());
  692. tokens.erase(E->next());
  693. E->get() = token;
  694. } else if (E->get() == "or") {
  695. ERR_FAIL_COND_V_MSG(!E->next() || !E->next()->next(), list, "CodeSign/Requirements: Invalid token sequence.");
  696. String token = "(" + E->next()->get() + " or " + E->next()->next()->get() + ")";
  697. tokens.erase(E->next()->next());
  698. tokens.erase(E->next());
  699. E->get() = token;
  700. }
  701. }
  702. if (tokens.size() == 1) {
  703. list.push_back(out + tokens.front()->get());
  704. } else {
  705. ERR_FAIL_V_MSG(list, "CodeSign/Requirements: Invalid token sequence.");
  706. }
  707. }
  708. return list;
  709. #undef _R
  710. }
  711. PackedByteArray CodeSignRequirements::get_hash_sha1() const {
  712. PackedByteArray hash;
  713. hash.resize(0x14);
  714. CryptoCore::SHA1Context ctx;
  715. ctx.start();
  716. ctx.update(blob.ptr(), blob.size());
  717. ctx.finish(hash.ptrw());
  718. return hash;
  719. }
  720. PackedByteArray CodeSignRequirements::get_hash_sha256() const {
  721. PackedByteArray hash;
  722. hash.resize(0x20);
  723. CryptoCore::SHA256Context ctx;
  724. ctx.start();
  725. ctx.update(blob.ptr(), blob.size());
  726. ctx.finish(hash.ptrw());
  727. return hash;
  728. }
  729. int CodeSignRequirements::get_size() const {
  730. return blob.size();
  731. }
  732. void CodeSignRequirements::write_to_file(Ref<FileAccess> p_file) const {
  733. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/Requirements: Invalid file handle.");
  734. p_file->store_buffer(blob.ptr(), blob.size());
  735. }
  736. /*************************************************************************/
  737. /* CodeSignEntitlementsText */
  738. /*************************************************************************/
  739. CodeSignEntitlementsText::CodeSignEntitlementsText() {
  740. blob.append_array({ 0xFA, 0xDE, 0x71, 0x71 }); // Text Entitlements set magic.
  741. blob.append_array({ 0x00, 0x00, 0x00, 0x08 }); // Length (8 bytes).
  742. }
  743. CodeSignEntitlementsText::CodeSignEntitlementsText(const String &p_string) {
  744. CharString utf8 = p_string.utf8();
  745. blob.append_array({ 0xFA, 0xDE, 0x71, 0x71 }); // Text Entitlements set magic.
  746. for (int i = 3; i >= 0; i--) {
  747. uint8_t x = ((utf8.length() + 8) >> i * 8) & 0xFF; // Size.
  748. blob.push_back(x);
  749. }
  750. for (int i = 0; i < utf8.length(); i++) { // Write data.
  751. blob.push_back(utf8[i]);
  752. }
  753. }
  754. PackedByteArray CodeSignEntitlementsText::get_hash_sha1() const {
  755. PackedByteArray hash;
  756. hash.resize(0x14);
  757. CryptoCore::SHA1Context ctx;
  758. ctx.start();
  759. ctx.update(blob.ptr(), blob.size());
  760. ctx.finish(hash.ptrw());
  761. return hash;
  762. }
  763. PackedByteArray CodeSignEntitlementsText::get_hash_sha256() const {
  764. PackedByteArray hash;
  765. hash.resize(0x20);
  766. CryptoCore::SHA256Context ctx;
  767. ctx.start();
  768. ctx.update(blob.ptr(), blob.size());
  769. ctx.finish(hash.ptrw());
  770. return hash;
  771. }
  772. int CodeSignEntitlementsText::get_size() const {
  773. return blob.size();
  774. }
  775. void CodeSignEntitlementsText::write_to_file(Ref<FileAccess> p_file) const {
  776. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/EntitlementsText: Invalid file handle.");
  777. p_file->store_buffer(blob.ptr(), blob.size());
  778. }
  779. /*************************************************************************/
  780. /* CodeSignEntitlementsBinary */
  781. /*************************************************************************/
  782. CodeSignEntitlementsBinary::CodeSignEntitlementsBinary() {
  783. blob.append_array({ 0xFA, 0xDE, 0x71, 0x72 }); // Binary Entitlements magic.
  784. blob.append_array({ 0x00, 0x00, 0x00, 0x08 }); // Length (8 bytes).
  785. }
  786. CodeSignEntitlementsBinary::CodeSignEntitlementsBinary(const String &p_string) {
  787. PList pl = PList(p_string);
  788. PackedByteArray asn1 = pl.save_asn1();
  789. blob.append_array({ 0xFA, 0xDE, 0x71, 0x72 }); // Binary Entitlements magic.
  790. uint32_t size = asn1.size() + 8;
  791. for (int i = 3; i >= 0; i--) {
  792. uint8_t x = (size >> i * 8) & 0xFF; // Size.
  793. blob.push_back(x);
  794. }
  795. blob.append_array(asn1); // Write data.
  796. }
  797. PackedByteArray CodeSignEntitlementsBinary::get_hash_sha1() const {
  798. PackedByteArray hash;
  799. hash.resize(0x14);
  800. CryptoCore::SHA1Context ctx;
  801. ctx.start();
  802. ctx.update(blob.ptr(), blob.size());
  803. ctx.finish(hash.ptrw());
  804. return hash;
  805. }
  806. PackedByteArray CodeSignEntitlementsBinary::get_hash_sha256() const {
  807. PackedByteArray hash;
  808. hash.resize(0x20);
  809. CryptoCore::SHA256Context ctx;
  810. ctx.start();
  811. ctx.update(blob.ptr(), blob.size());
  812. ctx.finish(hash.ptrw());
  813. return hash;
  814. }
  815. int CodeSignEntitlementsBinary::get_size() const {
  816. return blob.size();
  817. }
  818. void CodeSignEntitlementsBinary::write_to_file(Ref<FileAccess> p_file) const {
  819. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/EntitlementsBinary: Invalid file handle.");
  820. p_file->store_buffer(blob.ptr(), blob.size());
  821. }
  822. /*************************************************************************/
  823. /* CodeSignCodeDirectory */
  824. /*************************************************************************/
  825. CodeSignCodeDirectory::CodeSignCodeDirectory() {
  826. blob.append_array({ 0xFA, 0xDE, 0x0C, 0x02 }); // Code Directory magic.
  827. blob.append_array({ 0x00, 0x00, 0x00, 0x00 }); // Size (8 bytes).
  828. }
  829. CodeSignCodeDirectory::CodeSignCodeDirectory(uint8_t p_hash_size, uint8_t p_hash_type, bool p_main, const CharString &p_id, const CharString &p_team_id, uint32_t p_page_size, uint64_t p_exe_limit, uint64_t p_code_limit) {
  830. pages = p_code_limit / (uint64_t(1) << p_page_size);
  831. remain = p_code_limit % (uint64_t(1) << p_page_size);
  832. code_slots = pages + (remain > 0 ? 1 : 0);
  833. special_slots = 7;
  834. int cd_size = 8 + sizeof(CodeDirectoryHeader) + (code_slots + special_slots) * p_hash_size + p_id.size() + p_team_id.size();
  835. int cd_off = 8 + sizeof(CodeDirectoryHeader);
  836. blob.append_array({ 0xFA, 0xDE, 0x0C, 0x02 }); // Code Directory magic.
  837. for (int i = 3; i >= 0; i--) {
  838. uint8_t x = (cd_size >> i * 8) & 0xFF; // Size.
  839. blob.push_back(x);
  840. }
  841. blob.resize(cd_size);
  842. memset(blob.ptrw() + 8, 0x00, cd_size - 8);
  843. CodeDirectoryHeader *cd = reinterpret_cast<CodeDirectoryHeader *>(blob.ptrw() + 8);
  844. bool is_64_cl = (p_code_limit >= std::numeric_limits<uint32_t>::max());
  845. // Version and options.
  846. cd->version = BSWAP32(0x20500);
  847. cd->flags = BSWAP32(SIGNATURE_ADHOC | SIGNATURE_RUNTIME);
  848. cd->special_slots = BSWAP32(special_slots);
  849. cd->code_slots = BSWAP32(code_slots);
  850. if (is_64_cl) {
  851. cd->code_limit_64 = BSWAP64(p_code_limit);
  852. } else {
  853. cd->code_limit = BSWAP32(p_code_limit);
  854. }
  855. cd->hash_size = p_hash_size;
  856. cd->hash_type = p_hash_type;
  857. cd->page_size = p_page_size;
  858. cd->exec_seg_base = 0x00;
  859. cd->exec_seg_limit = BSWAP64(p_exe_limit);
  860. cd->exec_seg_flags = 0;
  861. if (p_main) {
  862. cd->exec_seg_flags |= EXECSEG_MAIN_BINARY;
  863. }
  864. cd->exec_seg_flags = BSWAP64(cd->exec_seg_flags);
  865. uint32_t version = (11 << 16) + (3 << 8) + 0; // Version 11.3.0
  866. cd->runtime = BSWAP32(version);
  867. // Copy ID.
  868. cd->ident_offset = BSWAP32(cd_off);
  869. memcpy(blob.ptrw() + cd_off, p_id.get_data(), p_id.size());
  870. cd_off += p_id.size();
  871. // Copy Team ID.
  872. if (p_team_id.length() > 0) {
  873. cd->team_offset = BSWAP32(cd_off);
  874. memcpy(blob.ptrw() + cd_off, p_team_id.get_data(), p_team_id.size());
  875. cd_off += p_team_id.size();
  876. } else {
  877. cd->team_offset = 0;
  878. }
  879. // Scatter vector.
  880. cd->scatter_vector_offset = 0; // Not used.
  881. // Executable hashes offset.
  882. cd->hash_offset = BSWAP32(cd_off + special_slots * cd->hash_size);
  883. }
  884. bool CodeSignCodeDirectory::set_hash_in_slot(const PackedByteArray &p_hash, int p_slot) {
  885. ERR_FAIL_COND_V_MSG((p_slot < -special_slots) || (p_slot >= code_slots), false, vformat("CodeSign/CodeDirectory: Invalid hash slot index: %d.", p_slot));
  886. CodeDirectoryHeader *cd = reinterpret_cast<CodeDirectoryHeader *>(blob.ptrw() + 8);
  887. for (int i = 0; i < cd->hash_size; i++) {
  888. blob.write[BSWAP32(cd->hash_offset) + p_slot * cd->hash_size + i] = p_hash[i];
  889. }
  890. return true;
  891. }
  892. int32_t CodeSignCodeDirectory::get_page_count() {
  893. return pages;
  894. }
  895. int32_t CodeSignCodeDirectory::get_page_remainder() {
  896. return remain;
  897. }
  898. PackedByteArray CodeSignCodeDirectory::get_hash_sha1() const {
  899. PackedByteArray hash;
  900. hash.resize(0x14);
  901. CryptoCore::SHA1Context ctx;
  902. ctx.start();
  903. ctx.update(blob.ptr(), blob.size());
  904. ctx.finish(hash.ptrw());
  905. return hash;
  906. }
  907. PackedByteArray CodeSignCodeDirectory::get_hash_sha256() const {
  908. PackedByteArray hash;
  909. hash.resize(0x20);
  910. CryptoCore::SHA256Context ctx;
  911. ctx.start();
  912. ctx.update(blob.ptr(), blob.size());
  913. ctx.finish(hash.ptrw());
  914. return hash;
  915. }
  916. int CodeSignCodeDirectory::get_size() const {
  917. return blob.size();
  918. }
  919. void CodeSignCodeDirectory::write_to_file(Ref<FileAccess> p_file) const {
  920. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/CodeDirectory: Invalid file handle.");
  921. p_file->store_buffer(blob.ptr(), blob.size());
  922. }
  923. /*************************************************************************/
  924. /* CodeSignSignature */
  925. /*************************************************************************/
  926. CodeSignSignature::CodeSignSignature() {
  927. blob.append_array({ 0xFA, 0xDE, 0x0B, 0x01 }); // Signature magic.
  928. uint32_t sign_size = 8; // Ad-hoc signature is empty.
  929. for (int i = 3; i >= 0; i--) {
  930. uint8_t x = (sign_size >> i * 8) & 0xFF; // Size.
  931. blob.push_back(x);
  932. }
  933. }
  934. PackedByteArray CodeSignSignature::get_hash_sha1() const {
  935. PackedByteArray hash;
  936. hash.resize(0x14);
  937. CryptoCore::SHA1Context ctx;
  938. ctx.start();
  939. ctx.update(blob.ptr(), blob.size());
  940. ctx.finish(hash.ptrw());
  941. return hash;
  942. }
  943. PackedByteArray CodeSignSignature::get_hash_sha256() const {
  944. PackedByteArray hash;
  945. hash.resize(0x20);
  946. CryptoCore::SHA256Context ctx;
  947. ctx.start();
  948. ctx.update(blob.ptr(), blob.size());
  949. ctx.finish(hash.ptrw());
  950. return hash;
  951. }
  952. int CodeSignSignature::get_size() const {
  953. return blob.size();
  954. }
  955. void CodeSignSignature::write_to_file(Ref<FileAccess> p_file) const {
  956. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/Signature: Invalid file handle.");
  957. p_file->store_buffer(blob.ptr(), blob.size());
  958. }
  959. /*************************************************************************/
  960. /* CodeSignSuperBlob */
  961. /*************************************************************************/
  962. bool CodeSignSuperBlob::add_blob(const Ref<CodeSignBlob> &p_blob) {
  963. if (p_blob.is_valid()) {
  964. blobs.push_back(p_blob);
  965. return true;
  966. } else {
  967. return false;
  968. }
  969. }
  970. int CodeSignSuperBlob::get_size() const {
  971. int size = 12 + blobs.size() * 8;
  972. for (int i = 0; i < blobs.size(); i++) {
  973. if (blobs[i].is_null()) {
  974. return 0;
  975. }
  976. size += blobs[i]->get_size();
  977. }
  978. return size;
  979. }
  980. void CodeSignSuperBlob::write_to_file(Ref<FileAccess> p_file) const {
  981. ERR_FAIL_COND_MSG(p_file.is_null(), "CodeSign/SuperBlob: Invalid file handle.");
  982. uint32_t size = get_size();
  983. uint32_t data_offset = 12 + blobs.size() * 8;
  984. // Write header.
  985. p_file->store_32(BSWAP32(0xfade0cc0));
  986. p_file->store_32(BSWAP32(size));
  987. p_file->store_32(BSWAP32(blobs.size()));
  988. // Write index.
  989. for (int i = 0; i < blobs.size(); i++) {
  990. if (blobs[i].is_null()) {
  991. return;
  992. }
  993. p_file->store_32(BSWAP32(blobs[i]->get_index_type()));
  994. p_file->store_32(BSWAP32(data_offset));
  995. data_offset += blobs[i]->get_size();
  996. }
  997. // Write blobs.
  998. for (int i = 0; i < blobs.size(); i++) {
  999. blobs[i]->write_to_file(p_file);
  1000. }
  1001. }
  1002. /*************************************************************************/
  1003. /* CodeSign */
  1004. /*************************************************************************/
  1005. PackedByteArray CodeSign::file_hash_sha1(const String &p_path) {
  1006. PackedByteArray file_hash;
  1007. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1008. ERR_FAIL_COND_V_MSG(f.is_null(), PackedByteArray(), vformat("CodeSign: Can't open file: \"%s\".", p_path));
  1009. CryptoCore::SHA1Context ctx;
  1010. ctx.start();
  1011. unsigned char step[4096];
  1012. while (true) {
  1013. uint64_t br = f->get_buffer(step, 4096);
  1014. if (br > 0) {
  1015. ctx.update(step, br);
  1016. }
  1017. if (br < 4096) {
  1018. break;
  1019. }
  1020. }
  1021. file_hash.resize(0x14);
  1022. ctx.finish(file_hash.ptrw());
  1023. return file_hash;
  1024. }
  1025. PackedByteArray CodeSign::file_hash_sha256(const String &p_path) {
  1026. PackedByteArray file_hash;
  1027. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1028. ERR_FAIL_COND_V_MSG(f.is_null(), PackedByteArray(), vformat("CodeSign: Can't open file: \"%s\".", p_path));
  1029. CryptoCore::SHA256Context ctx;
  1030. ctx.start();
  1031. unsigned char step[4096];
  1032. while (true) {
  1033. uint64_t br = f->get_buffer(step, 4096);
  1034. if (br > 0) {
  1035. ctx.update(step, br);
  1036. }
  1037. if (br < 4096) {
  1038. break;
  1039. }
  1040. }
  1041. file_hash.resize(0x20);
  1042. ctx.finish(file_hash.ptrw());
  1043. return file_hash;
  1044. }
  1045. Error CodeSign::_codesign_file(bool p_use_hardened_runtime, bool p_force, const String &p_info, const String &p_exe_path, const String &p_bundle_path, const String &p_ent_path, bool p_ios_bundle, String &r_error_msg) {
  1046. #define CLEANUP() \
  1047. if (files_to_sign.size() > 1) { \
  1048. for (int j = 0; j < files_to_sign.size(); j++) { \
  1049. da->remove(files_to_sign[j]); \
  1050. } \
  1051. }
  1052. print_verbose(vformat("CodeSign: Signing executable: %s, bundle: %s with entitlements %s", p_exe_path, p_bundle_path, p_ent_path));
  1053. PackedByteArray info_hash1, info_hash2;
  1054. PackedByteArray res_hash1, res_hash2;
  1055. String id;
  1056. String main_exe = p_exe_path;
  1057. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1058. if (da.is_null()) {
  1059. r_error_msg = TTR("Can't get filesystem access.");
  1060. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CodeSign: Can't get filesystem access.");
  1061. }
  1062. // Read Info.plist.
  1063. if (!p_info.is_empty()) {
  1064. print_verbose(vformat("CodeSign: Reading bundle info..."));
  1065. PList info_plist;
  1066. if (info_plist.load_file(p_info)) {
  1067. info_hash1 = file_hash_sha1(p_info);
  1068. info_hash2 = file_hash_sha256(p_info);
  1069. if (info_hash1.is_empty() || info_hash2.is_empty()) {
  1070. r_error_msg = TTR("Failed to get Info.plist hash.");
  1071. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to get Info.plist hash.");
  1072. }
  1073. if (info_plist.get_root()->data_type == PList::PLNodeType::PL_NODE_TYPE_DICT && info_plist.get_root()->data_dict.has("CFBundleExecutable")) {
  1074. main_exe = p_exe_path.path_join(String::utf8(info_plist.get_root()->data_dict["CFBundleExecutable"]->data_string.get_data()));
  1075. } else {
  1076. r_error_msg = TTR("Invalid Info.plist, no exe name.");
  1077. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid Info.plist, no exe name.");
  1078. }
  1079. if (info_plist.get_root()->data_type == PList::PLNodeType::PL_NODE_TYPE_DICT && info_plist.get_root()->data_dict.has("CFBundleIdentifier")) {
  1080. id = info_plist.get_root()->data_dict["CFBundleIdentifier"]->data_string.get_data();
  1081. } else {
  1082. r_error_msg = TTR("Invalid Info.plist, no bundle id.");
  1083. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid Info.plist, no bundle id.");
  1084. }
  1085. } else {
  1086. r_error_msg = TTR("Invalid Info.plist, can't load.");
  1087. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid Info.plist, can't load.");
  1088. }
  1089. }
  1090. // Extract fat binary.
  1091. Vector<String> files_to_sign;
  1092. if (LipO::is_lipo(main_exe)) {
  1093. print_verbose(vformat("CodeSign: Executable is fat, extracting..."));
  1094. String tmp_path_name = EditorPaths::get_singleton()->get_cache_dir().path_join("_lipo");
  1095. Error err = da->make_dir_recursive(tmp_path_name);
  1096. if (err != OK) {
  1097. r_error_msg = vformat(TTR("Failed to create \"%s\" subfolder."), tmp_path_name);
  1098. ERR_FAIL_V_MSG(FAILED, vformat("CodeSign: Failed to create \"%s\" subfolder.", tmp_path_name));
  1099. }
  1100. LipO lip;
  1101. if (lip.open_file(main_exe)) {
  1102. for (int i = 0; i < lip.get_arch_count(); i++) {
  1103. if (!lip.extract_arch(i, tmp_path_name.path_join("_exe_" + itos(i)))) {
  1104. CLEANUP();
  1105. r_error_msg = TTR("Failed to extract thin binary.");
  1106. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to extract thin binary.");
  1107. }
  1108. files_to_sign.push_back(tmp_path_name.path_join("_exe_" + itos(i)));
  1109. }
  1110. }
  1111. } else if (MachO::is_macho(main_exe)) {
  1112. print_verbose(vformat("CodeSign: Executable is thin..."));
  1113. files_to_sign.push_back(main_exe);
  1114. } else {
  1115. r_error_msg = TTR("Invalid binary format.");
  1116. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid binary format.");
  1117. }
  1118. // Check if it's already signed.
  1119. if (!p_force) {
  1120. for (int i = 0; i < files_to_sign.size(); i++) {
  1121. MachO mh;
  1122. mh.open_file(files_to_sign[i]);
  1123. if (mh.is_signed()) {
  1124. CLEANUP();
  1125. r_error_msg = TTR("Already signed!");
  1126. ERR_FAIL_V_MSG(FAILED, "CodeSign: Already signed!");
  1127. }
  1128. }
  1129. }
  1130. // Generate core resources.
  1131. if (!p_bundle_path.is_empty()) {
  1132. print_verbose(vformat("CodeSign: Generating bundle CodeResources..."));
  1133. CodeSignCodeResources cr;
  1134. if (p_ios_bundle) {
  1135. cr.add_rule1("^.*");
  1136. cr.add_rule1("^.*\\.lproj/", "optional", 100);
  1137. cr.add_rule1("^.*\\.lproj/locversion.plist$", "omit", 1100);
  1138. cr.add_rule1("^Base\\.lproj/", "", 1010);
  1139. cr.add_rule1("^version.plist$");
  1140. cr.add_rule2(".*\\.dSYM($|/)", "", 11);
  1141. cr.add_rule2("^(.*/)?\\.DS_Store$", "omit", 2000);
  1142. cr.add_rule2("^.*");
  1143. cr.add_rule2("^.*\\.lproj/", "optional", 1000);
  1144. cr.add_rule2("^.*\\.lproj/locversion.plist$", "omit", 1100);
  1145. cr.add_rule2("^Base\\.lproj/", "", 1010);
  1146. cr.add_rule2("^Info\\.plist$", "omit", 20);
  1147. cr.add_rule2("^PkgInfo$", "omit", 20);
  1148. cr.add_rule2("^embedded\\.provisionprofile$", "", 10);
  1149. cr.add_rule2("^version\\.plist$", "", 20);
  1150. cr.add_rule2("^_MASReceipt", "omit", 2000, false);
  1151. cr.add_rule2("^_CodeSignature", "omit", 2000, false);
  1152. cr.add_rule2("^CodeResources", "omit", 2000, false);
  1153. } else {
  1154. cr.add_rule1("^Resources/");
  1155. cr.add_rule1("^Resources/.*\\.lproj/", "optional", 1000);
  1156. cr.add_rule1("^Resources/.*\\.lproj/locversion.plist$", "omit", 1100);
  1157. cr.add_rule1("^Resources/Base\\.lproj/", "", 1010);
  1158. cr.add_rule1("^version.plist$");
  1159. cr.add_rule2(".*\\.dSYM($|/)", "", 11);
  1160. cr.add_rule2("^(.*/)?\\.DS_Store$", "omit", 2000);
  1161. cr.add_rule2("^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/", "nested", 10);
  1162. cr.add_rule2("^.*");
  1163. cr.add_rule2("^Info\\.plist$", "omit", 20);
  1164. cr.add_rule2("^PkgInfo$", "omit", 20);
  1165. cr.add_rule2("^Resources/", "", 20);
  1166. cr.add_rule2("^Resources/.*\\.lproj/", "optional", 1000);
  1167. cr.add_rule2("^Resources/.*\\.lproj/locversion.plist$", "omit", 1100);
  1168. cr.add_rule2("^Resources/Base\\.lproj/", "", 1010);
  1169. cr.add_rule2("^[^/]+$", "nested", 10);
  1170. cr.add_rule2("^embedded\\.provisionprofile$", "", 10);
  1171. cr.add_rule2("^version\\.plist$", "", 20);
  1172. cr.add_rule2("^_MASReceipt", "omit", 2000, false);
  1173. cr.add_rule2("^_CodeSignature", "omit", 2000, false);
  1174. cr.add_rule2("^CodeResources", "omit", 2000, false);
  1175. }
  1176. if (!cr.add_folder_recursive(p_bundle_path, "", main_exe)) {
  1177. CLEANUP();
  1178. r_error_msg = TTR("Failed to process nested resources.");
  1179. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to process nested resources.");
  1180. }
  1181. Error err = da->make_dir_recursive(p_bundle_path.path_join("_CodeSignature"));
  1182. if (err != OK) {
  1183. CLEANUP();
  1184. r_error_msg = TTR("Failed to create _CodeSignature subfolder.");
  1185. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to create _CodeSignature subfolder.");
  1186. }
  1187. cr.save_to_file(p_bundle_path.path_join("_CodeSignature").path_join("CodeResources"));
  1188. res_hash1 = file_hash_sha1(p_bundle_path.path_join("_CodeSignature").path_join("CodeResources"));
  1189. res_hash2 = file_hash_sha256(p_bundle_path.path_join("_CodeSignature").path_join("CodeResources"));
  1190. if (res_hash1.is_empty() || res_hash2.is_empty()) {
  1191. CLEANUP();
  1192. r_error_msg = TTR("Failed to get CodeResources hash.");
  1193. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to get CodeResources hash.");
  1194. }
  1195. }
  1196. // Generate common signature structures.
  1197. if (id.is_empty()) {
  1198. CryptoCore::RandomGenerator rng;
  1199. ERR_FAIL_COND_V_MSG(rng.init(), FAILED, "Failed to initialize random number generator.");
  1200. uint8_t uuid[16];
  1201. Error err = rng.get_random_bytes(uuid, 16);
  1202. ERR_FAIL_COND_V_MSG(err, err, "Failed to generate UUID.");
  1203. id = (String("a-55554944") /*a-UUID*/ + String::hex_encode_buffer(uuid, 16));
  1204. }
  1205. CharString uuid_str = id.utf8();
  1206. print_verbose(vformat("CodeSign: Used bundle ID: %s", id));
  1207. print_verbose(vformat("CodeSign: Processing entitlements..."));
  1208. Ref<CodeSignEntitlementsText> cet;
  1209. Ref<CodeSignEntitlementsBinary> ceb;
  1210. if (!p_ent_path.is_empty()) {
  1211. String entitlements = FileAccess::get_file_as_string(p_ent_path);
  1212. if (entitlements.is_empty()) {
  1213. CLEANUP();
  1214. r_error_msg = TTR("Invalid entitlements file.");
  1215. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid entitlements file.");
  1216. }
  1217. cet = Ref<CodeSignEntitlementsText>(memnew(CodeSignEntitlementsText(entitlements)));
  1218. ceb = Ref<CodeSignEntitlementsBinary>(memnew(CodeSignEntitlementsBinary(entitlements)));
  1219. }
  1220. print_verbose(vformat("CodeSign: Generating requirements..."));
  1221. Ref<CodeSignRequirements> rq;
  1222. String team_id = "";
  1223. rq = Ref<CodeSignRequirements>(memnew(CodeSignRequirements()));
  1224. // Sign executables.
  1225. for (int i = 0; i < files_to_sign.size(); i++) {
  1226. MachO mh;
  1227. if (!mh.open_file(files_to_sign[i])) {
  1228. CLEANUP();
  1229. r_error_msg = TTR("Invalid executable file.");
  1230. ERR_FAIL_V_MSG(FAILED, "CodeSign: Invalid executable file.");
  1231. }
  1232. print_verbose(vformat("CodeSign: Signing executable for cputype: %d ...", mh.get_cputype()));
  1233. print_verbose(vformat("CodeSign: Generating CodeDirectory..."));
  1234. Ref<CodeSignCodeDirectory> cd1 = memnew(CodeSignCodeDirectory(0x14, 0x01, true, uuid_str, team_id.utf8(), 12, mh.get_exe_limit(), mh.get_code_limit()));
  1235. Ref<CodeSignCodeDirectory> cd2 = memnew(CodeSignCodeDirectory(0x20, 0x02, true, uuid_str, team_id.utf8(), 12, mh.get_exe_limit(), mh.get_code_limit()));
  1236. print_verbose(vformat("CodeSign: Calculating special slot hashes..."));
  1237. if (info_hash2.size() == 0x20) {
  1238. cd2->set_hash_in_slot(info_hash2, CodeSignCodeDirectory::SLOT_INFO_PLIST);
  1239. }
  1240. if (info_hash1.size() == 0x14) {
  1241. cd1->set_hash_in_slot(info_hash1, CodeSignCodeDirectory::SLOT_INFO_PLIST);
  1242. }
  1243. cd1->set_hash_in_slot(rq->get_hash_sha1(), CodeSignCodeDirectory::Slot::SLOT_REQUIREMENTS);
  1244. cd2->set_hash_in_slot(rq->get_hash_sha256(), CodeSignCodeDirectory::Slot::SLOT_REQUIREMENTS);
  1245. if (res_hash2.size() == 0x20) {
  1246. cd2->set_hash_in_slot(res_hash2, CodeSignCodeDirectory::SLOT_RESOURCES);
  1247. }
  1248. if (res_hash1.size() == 0x14) {
  1249. cd1->set_hash_in_slot(res_hash1, CodeSignCodeDirectory::SLOT_RESOURCES);
  1250. }
  1251. if (cet.is_valid()) {
  1252. cd1->set_hash_in_slot(cet->get_hash_sha1(), CodeSignCodeDirectory::Slot::SLOT_ENTITLEMENTS); //Text variant.
  1253. cd2->set_hash_in_slot(cet->get_hash_sha256(), CodeSignCodeDirectory::Slot::SLOT_ENTITLEMENTS);
  1254. }
  1255. if (ceb.is_valid()) {
  1256. cd1->set_hash_in_slot(ceb->get_hash_sha1(), CodeSignCodeDirectory::Slot::SLOT_DER_ENTITLEMENTS); //ASN.1 variant.
  1257. cd2->set_hash_in_slot(ceb->get_hash_sha256(), CodeSignCodeDirectory::Slot::SLOT_DER_ENTITLEMENTS);
  1258. }
  1259. // Calculate signature size.
  1260. int sign_size = 12; // SuperBlob header.
  1261. sign_size += cd1->get_size() + 8;
  1262. sign_size += cd2->get_size() + 8;
  1263. sign_size += rq->get_size() + 8;
  1264. if (cet.is_valid()) {
  1265. sign_size += cet->get_size() + 8;
  1266. }
  1267. if (ceb.is_valid()) {
  1268. sign_size += ceb->get_size() + 8;
  1269. }
  1270. sign_size += 16; // Empty signature size.
  1271. // Alloc/resize signature load command.
  1272. print_verbose(vformat("CodeSign: Reallocating space for the signature superblob (%d)...", sign_size));
  1273. if (!mh.set_signature_size(sign_size)) {
  1274. CLEANUP();
  1275. r_error_msg = TTR("Can't resize signature load command.");
  1276. ERR_FAIL_V_MSG(FAILED, "CodeSign: Can't resize signature load command.");
  1277. }
  1278. print_verbose(vformat("CodeSign: Calculating executable code hashes..."));
  1279. // Calculate executable code hashes.
  1280. PackedByteArray buffer;
  1281. PackedByteArray hash1, hash2;
  1282. hash1.resize(0x14);
  1283. hash2.resize(0x20);
  1284. buffer.resize(1 << 12);
  1285. mh.get_file()->seek(0);
  1286. for (int32_t j = 0; j < cd2->get_page_count(); j++) {
  1287. mh.get_file()->get_buffer(buffer.ptrw(), (1 << 12));
  1288. CryptoCore::SHA256Context ctx2;
  1289. ctx2.start();
  1290. ctx2.update(buffer.ptr(), (1 << 12));
  1291. ctx2.finish(hash2.ptrw());
  1292. cd2->set_hash_in_slot(hash2, j);
  1293. CryptoCore::SHA1Context ctx1;
  1294. ctx1.start();
  1295. ctx1.update(buffer.ptr(), (1 << 12));
  1296. ctx1.finish(hash1.ptrw());
  1297. cd1->set_hash_in_slot(hash1, j);
  1298. }
  1299. if (cd2->get_page_remainder() > 0) {
  1300. mh.get_file()->get_buffer(buffer.ptrw(), cd2->get_page_remainder());
  1301. CryptoCore::SHA256Context ctx2;
  1302. ctx2.start();
  1303. ctx2.update(buffer.ptr(), cd2->get_page_remainder());
  1304. ctx2.finish(hash2.ptrw());
  1305. cd2->set_hash_in_slot(hash2, cd2->get_page_count());
  1306. CryptoCore::SHA1Context ctx1;
  1307. ctx1.start();
  1308. ctx1.update(buffer.ptr(), cd1->get_page_remainder());
  1309. ctx1.finish(hash1.ptrw());
  1310. cd1->set_hash_in_slot(hash1, cd1->get_page_count());
  1311. }
  1312. print_verbose(vformat("CodeSign: Generating signature..."));
  1313. Ref<CodeSignSignature> cs;
  1314. cs = Ref<CodeSignSignature>(memnew(CodeSignSignature()));
  1315. print_verbose(vformat("CodeSign: Writing signature superblob..."));
  1316. // Write signature data to the executable.
  1317. CodeSignSuperBlob sb = CodeSignSuperBlob();
  1318. sb.add_blob(cd2);
  1319. sb.add_blob(cd1);
  1320. sb.add_blob(rq);
  1321. if (cet.is_valid()) {
  1322. sb.add_blob(cet);
  1323. }
  1324. if (ceb.is_valid()) {
  1325. sb.add_blob(ceb);
  1326. }
  1327. sb.add_blob(cs);
  1328. mh.get_file()->seek(mh.get_signature_offset());
  1329. sb.write_to_file(mh.get_file());
  1330. }
  1331. if (files_to_sign.size() > 1) {
  1332. print_verbose(vformat("CodeSign: Rebuilding fat executable..."));
  1333. LipO lip;
  1334. if (!lip.create_file(main_exe, files_to_sign)) {
  1335. CLEANUP();
  1336. r_error_msg = TTR("Failed to create fat binary.");
  1337. ERR_FAIL_V_MSG(FAILED, "CodeSign: Failed to create fat binary.");
  1338. }
  1339. CLEANUP();
  1340. }
  1341. FileAccess::set_unix_permissions(main_exe, 0755); // Restore unix permissions.
  1342. return OK;
  1343. #undef CLEANUP
  1344. }
  1345. Error CodeSign::codesign(bool p_use_hardened_runtime, bool p_force, const String &p_path, const String &p_ent_path, String &r_error_msg) {
  1346. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1347. if (da.is_null()) {
  1348. r_error_msg = TTR("Can't get filesystem access.");
  1349. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CodeSign: Can't get filesystem access.");
  1350. }
  1351. if (da->dir_exists(p_path)) {
  1352. String fmw_ver = "Current"; // Framework version (default).
  1353. String info_path;
  1354. String main_exe;
  1355. String bundle_path;
  1356. bool bundle = false;
  1357. bool ios_bundle = false;
  1358. if (da->file_exists(p_path.path_join("Contents/Info.plist"))) {
  1359. info_path = p_path.path_join("Contents/Info.plist");
  1360. main_exe = p_path.path_join("Contents/MacOS");
  1361. bundle_path = p_path.path_join("Contents");
  1362. bundle = true;
  1363. } else if (da->file_exists(p_path.path_join(vformat("Versions/%s/Resources/Info.plist", fmw_ver)))) {
  1364. info_path = p_path.path_join(vformat("Versions/%s/Resources/Info.plist", fmw_ver));
  1365. main_exe = p_path.path_join(vformat("Versions/%s", fmw_ver));
  1366. bundle_path = p_path.path_join(vformat("Versions/%s", fmw_ver));
  1367. bundle = true;
  1368. } else if (da->file_exists(p_path.path_join("Info.plist"))) {
  1369. info_path = p_path.path_join("Info.plist");
  1370. main_exe = p_path;
  1371. bundle_path = p_path;
  1372. bundle = true;
  1373. ios_bundle = true;
  1374. }
  1375. if (bundle) {
  1376. return _codesign_file(p_use_hardened_runtime, p_force, info_path, main_exe, bundle_path, p_ent_path, ios_bundle, r_error_msg);
  1377. } else {
  1378. r_error_msg = TTR("Unknown bundle type.");
  1379. ERR_FAIL_V_MSG(FAILED, "CodeSign: Unknown bundle type.");
  1380. }
  1381. } else if (da->file_exists(p_path)) {
  1382. return _codesign_file(p_use_hardened_runtime, p_force, "", p_path, "", p_ent_path, false, r_error_msg);
  1383. } else {
  1384. r_error_msg = TTR("Unknown object type.");
  1385. ERR_FAIL_V_MSG(FAILED, "CodeSign: Unknown object type.");
  1386. }
  1387. }
  1388. #endif // MODULE_REGEX_ENABLED