codesign.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**************************************************************************/
  2. /* codesign.h */
  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. #pragma once
  31. // macOS code signature creation utility.
  32. //
  33. // Current implementation has the following limitation:
  34. // - Only version 11.3.0 signatures are supported.
  35. // - Only "framework" and "app" bundle types are supported.
  36. // - Page hash array scattering is not supported.
  37. // - Reading and writing binary property lists i snot supported (third-party frameworks with binary Info.plist will not work unless .plist is converted to text format).
  38. // - Requirements code generator is not implemented (only hard-coded requirements for the ad-hoc signing is supported).
  39. // - RFC5652/CMS blob generation is not implemented, supports ad-hoc signing only.
  40. #include "core/io/file_access.h"
  41. #include "core/object/ref_counted.h"
  42. /*************************************************************************/
  43. /* CodeSignCodeResources */
  44. /*************************************************************************/
  45. class CodeSignCodeResources {
  46. public:
  47. enum class CRMatch {
  48. CR_MATCH_NO,
  49. CR_MATCH_YES,
  50. CR_MATCH_NESTED,
  51. CR_MATCH_OPTIONAL,
  52. };
  53. private:
  54. struct CRFile {
  55. String name;
  56. String hash;
  57. String hash2;
  58. bool optional;
  59. bool nested;
  60. String requirements;
  61. };
  62. struct CRRule {
  63. String file_pattern;
  64. String key;
  65. int weight;
  66. bool store;
  67. CRRule() {
  68. weight = 1;
  69. store = true;
  70. }
  71. CRRule(const String &p_file_pattern, const String &p_key, int p_weight, bool p_store) {
  72. file_pattern = p_file_pattern;
  73. key = p_key;
  74. weight = p_weight;
  75. store = p_store;
  76. }
  77. };
  78. Vector<CRRule> rules1;
  79. Vector<CRRule> rules2;
  80. Vector<CRFile> files1;
  81. Vector<CRFile> files2;
  82. String hash_sha1_base64(const String &p_path);
  83. String hash_sha256_base64(const String &p_path);
  84. public:
  85. void add_rule1(const String &p_rule, const String &p_key = "", int p_weight = 0, bool p_store = true);
  86. void add_rule2(const String &p_rule, const String &p_key = "", int p_weight = 0, bool p_store = true);
  87. CRMatch match_rules1(const String &p_path) const;
  88. CRMatch match_rules2(const String &p_path) const;
  89. bool add_file1(const String &p_root, const String &p_path);
  90. bool add_file2(const String &p_root, const String &p_path);
  91. bool add_nested_file(const String &p_root, const String &p_path, const String &p_exepath);
  92. bool add_folder_recursive(const String &p_root, const String &p_path = "", const String &p_main_exe_path = "");
  93. bool save_to_file(const String &p_path);
  94. };
  95. /*************************************************************************/
  96. /* CodeSignBlob */
  97. /*************************************************************************/
  98. class CodeSignBlob : public RefCounted {
  99. GDSOFTCLASS(CodeSignBlob, RefCounted);
  100. public:
  101. virtual PackedByteArray get_hash_sha1() const = 0;
  102. virtual PackedByteArray get_hash_sha256() const = 0;
  103. virtual int get_size() const = 0;
  104. virtual uint32_t get_index_type() const = 0;
  105. virtual void write_to_file(Ref<FileAccess> p_file) const = 0;
  106. };
  107. /*************************************************************************/
  108. /* CodeSignRequirements */
  109. /*************************************************************************/
  110. // Note: Proper code generator is not implemented (any we probably won't ever need it), just a hardcoded bytecode for the limited set of cases.
  111. class CodeSignRequirements : public CodeSignBlob {
  112. GDSOFTCLASS(CodeSignRequirements, CodeSignBlob);
  113. PackedByteArray blob;
  114. static inline size_t PAD(size_t s, size_t a) {
  115. return (s % a == 0) ? 0 : (a - s % a);
  116. }
  117. _FORCE_INLINE_ void _parse_certificate_slot(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  118. _FORCE_INLINE_ void _parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  119. _FORCE_INLINE_ void _parse_oid_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  120. _FORCE_INLINE_ void _parse_hash_string(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  121. _FORCE_INLINE_ void _parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  122. _FORCE_INLINE_ void _parse_date(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  123. _FORCE_INLINE_ bool _parse_match(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  124. public:
  125. CodeSignRequirements();
  126. CodeSignRequirements(const PackedByteArray &p_data);
  127. Vector<String> parse_requirements() const;
  128. virtual PackedByteArray get_hash_sha1() const override;
  129. virtual PackedByteArray get_hash_sha256() const override;
  130. virtual int get_size() const override;
  131. virtual uint32_t get_index_type() const override { return 0x00000002; }
  132. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  133. };
  134. /*************************************************************************/
  135. /* CodeSignEntitlementsText */
  136. /*************************************************************************/
  137. // PList formatted entitlements.
  138. class CodeSignEntitlementsText : public CodeSignBlob {
  139. GDSOFTCLASS(CodeSignEntitlementsText, CodeSignBlob);
  140. PackedByteArray blob;
  141. public:
  142. CodeSignEntitlementsText();
  143. CodeSignEntitlementsText(const String &p_string);
  144. virtual PackedByteArray get_hash_sha1() const override;
  145. virtual PackedByteArray get_hash_sha256() const override;
  146. virtual int get_size() const override;
  147. virtual uint32_t get_index_type() const override { return 0x00000005; }
  148. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  149. };
  150. /*************************************************************************/
  151. /* CodeSignEntitlementsBinary */
  152. /*************************************************************************/
  153. // ASN.1 serialized entitlements.
  154. class CodeSignEntitlementsBinary : public CodeSignBlob {
  155. GDSOFTCLASS(CodeSignEntitlementsBinary, CodeSignBlob);
  156. PackedByteArray blob;
  157. public:
  158. CodeSignEntitlementsBinary();
  159. CodeSignEntitlementsBinary(const String &p_string);
  160. virtual PackedByteArray get_hash_sha1() const override;
  161. virtual PackedByteArray get_hash_sha256() const override;
  162. virtual int get_size() const override;
  163. virtual uint32_t get_index_type() const override { return 0x00000007; }
  164. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  165. };
  166. /*************************************************************************/
  167. /* CodeSignCodeDirectory */
  168. /*************************************************************************/
  169. // Code Directory, runtime options, code segment and special structure hashes.
  170. class CodeSignCodeDirectory : public CodeSignBlob {
  171. GDSOFTCLASS(CodeSignCodeDirectory, CodeSignBlob);
  172. public:
  173. enum Slot {
  174. SLOT_INFO_PLIST = -1,
  175. SLOT_REQUIREMENTS = -2,
  176. SLOT_RESOURCES = -3,
  177. SLOT_APP_SPECIFIC = -4, // Unused.
  178. SLOT_ENTITLEMENTS = -5,
  179. SLOT_RESERVER1 = -6, // Unused.
  180. SLOT_DER_ENTITLEMENTS = -7,
  181. };
  182. enum CodeSignExecSegFlags {
  183. EXECSEG_MAIN_BINARY = 0x1,
  184. EXECSEG_ALLOW_UNSIGNED = 0x10,
  185. EXECSEG_DEBUGGER = 0x20,
  186. EXECSEG_JIT = 0x40,
  187. EXECSEG_SKIP_LV = 0x80,
  188. EXECSEG_CAN_LOAD_CDHASH = 0x100,
  189. EXECSEG_CAN_EXEC_CDHASH = 0x200,
  190. };
  191. enum CodeSignatureFlags {
  192. SIGNATURE_HOST = 0x0001,
  193. SIGNATURE_ADHOC = 0x0002,
  194. SIGNATURE_TASK_ALLOW = 0x0004,
  195. SIGNATURE_INSTALLER = 0x0008,
  196. SIGNATURE_FORCED_LV = 0x0010,
  197. SIGNATURE_INVALID_ALLOWED = 0x0020,
  198. SIGNATURE_FORCE_HARD = 0x0100,
  199. SIGNATURE_FORCE_KILL = 0x0200,
  200. SIGNATURE_FORCE_EXPIRATION = 0x0400,
  201. SIGNATURE_RESTRICT = 0x0800,
  202. SIGNATURE_ENFORCEMENT = 0x1000,
  203. SIGNATURE_LIBRARY_VALIDATION = 0x2000,
  204. SIGNATURE_ENTITLEMENTS_VALIDATED = 0x4000,
  205. SIGNATURE_NVRAM_UNRESTRICTED = 0x8000,
  206. SIGNATURE_RUNTIME = 0x10000,
  207. SIGNATURE_LINKER_SIGNED = 0x20000,
  208. };
  209. private:
  210. PackedByteArray blob;
  211. struct CodeDirectoryHeader {
  212. uint32_t version; // Using version 0x0020500.
  213. uint32_t flags; // // Option flags.
  214. uint32_t hash_offset; // Slot zero offset.
  215. uint32_t ident_offset; // Identifier string offset.
  216. uint32_t special_slots; // Nr. of slots with negative index.
  217. uint32_t code_slots; // Nr. of slots with index >= 0, (code_limit / page_size).
  218. uint32_t code_limit; // Everything before code signature load command offset.
  219. uint8_t hash_size; // 20 (SHA-1) or 32 (SHA-256).
  220. uint8_t hash_type; // 1 (SHA-1) or 2 (SHA-256).
  221. uint8_t platform; // Not used.
  222. uint8_t page_size; // Page size, power of two, 2^12 (4096).
  223. uint32_t spare2; // Not used.
  224. // Version 0x20100
  225. uint32_t scatter_vector_offset; // Set to 0 and ignore.
  226. // Version 0x20200
  227. uint32_t team_offset; // Team id string offset.
  228. // Version 0x20300
  229. uint32_t spare3; // Not used.
  230. uint64_t code_limit_64; // Set to 0 and ignore.
  231. // Version 0x20400
  232. uint64_t exec_seg_base; // Start of the signed code segment.
  233. uint64_t exec_seg_limit; // Code segment (__TEXT) vmsize.
  234. uint64_t exec_seg_flags; // Executable segment flags.
  235. // Version 0x20500
  236. uint32_t runtime; // Runtime version.
  237. uint32_t pre_encrypt_offset; // Set to 0 and ignore.
  238. };
  239. int32_t pages = 0;
  240. int32_t remain = 0;
  241. int32_t code_slots = 0;
  242. int32_t special_slots = 0;
  243. public:
  244. CodeSignCodeDirectory();
  245. 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);
  246. int32_t get_page_count();
  247. int32_t get_page_remainder();
  248. bool set_hash_in_slot(const PackedByteArray &p_hash, int p_slot);
  249. virtual PackedByteArray get_hash_sha1() const override;
  250. virtual PackedByteArray get_hash_sha256() const override;
  251. virtual int get_size() const override;
  252. virtual uint32_t get_index_type() const override { return 0x00000000; }
  253. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  254. };
  255. /*************************************************************************/
  256. /* CodeSignSignature */
  257. /*************************************************************************/
  258. class CodeSignSignature : public CodeSignBlob {
  259. GDSOFTCLASS(CodeSignSignature, CodeSignBlob);
  260. PackedByteArray blob;
  261. public:
  262. CodeSignSignature();
  263. virtual PackedByteArray get_hash_sha1() const override;
  264. virtual PackedByteArray get_hash_sha256() const override;
  265. virtual int get_size() const override;
  266. virtual uint32_t get_index_type() const override { return 0x00010000; }
  267. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  268. };
  269. /*************************************************************************/
  270. /* CodeSignSuperBlob */
  271. /*************************************************************************/
  272. class CodeSignSuperBlob {
  273. Vector<Ref<CodeSignBlob>> blobs;
  274. public:
  275. bool add_blob(const Ref<CodeSignBlob> &p_blob);
  276. int get_size() const;
  277. void write_to_file(Ref<FileAccess> p_file) const;
  278. };
  279. /*************************************************************************/
  280. /* CodeSign */
  281. /*************************************************************************/
  282. class CodeSign {
  283. static PackedByteArray file_hash_sha1(const String &p_path);
  284. static PackedByteArray file_hash_sha256(const String &p_path);
  285. static Error _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);
  286. public:
  287. static Error codesign(bool p_use_hardened_runtime, bool p_force, const String &p_path, const String &p_ent_path, String &r_error_msg);
  288. };