codesign.cpp 53 KB

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