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