codesign.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. PackedByteArray blob;
  113. static inline size_t PAD(size_t s, size_t a) {
  114. return (s % a == 0) ? 0 : (a - s % a);
  115. }
  116. _FORCE_INLINE_ void _parse_certificate_slot(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  117. _FORCE_INLINE_ void _parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  118. _FORCE_INLINE_ void _parse_oid_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  119. _FORCE_INLINE_ void _parse_hash_string(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  120. _FORCE_INLINE_ void _parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  121. _FORCE_INLINE_ void _parse_date(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  122. _FORCE_INLINE_ bool _parse_match(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
  123. public:
  124. CodeSignRequirements();
  125. CodeSignRequirements(const PackedByteArray &p_data);
  126. Vector<String> parse_requirements() const;
  127. virtual PackedByteArray get_hash_sha1() const override;
  128. virtual PackedByteArray get_hash_sha256() const override;
  129. virtual int get_size() const override;
  130. virtual uint32_t get_index_type() const override { return 0x00000002; }
  131. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  132. };
  133. /*************************************************************************/
  134. /* CodeSignEntitlementsText */
  135. /*************************************************************************/
  136. // PList formatted entitlements.
  137. class CodeSignEntitlementsText : public CodeSignBlob {
  138. PackedByteArray blob;
  139. public:
  140. CodeSignEntitlementsText();
  141. CodeSignEntitlementsText(const String &p_string);
  142. virtual PackedByteArray get_hash_sha1() const override;
  143. virtual PackedByteArray get_hash_sha256() const override;
  144. virtual int get_size() const override;
  145. virtual uint32_t get_index_type() const override { return 0x00000005; }
  146. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  147. };
  148. /*************************************************************************/
  149. /* CodeSignEntitlementsBinary */
  150. /*************************************************************************/
  151. // ASN.1 serialized entitlements.
  152. class CodeSignEntitlementsBinary : public CodeSignBlob {
  153. PackedByteArray blob;
  154. public:
  155. CodeSignEntitlementsBinary();
  156. CodeSignEntitlementsBinary(const String &p_string);
  157. virtual PackedByteArray get_hash_sha1() const override;
  158. virtual PackedByteArray get_hash_sha256() const override;
  159. virtual int get_size() const override;
  160. virtual uint32_t get_index_type() const override { return 0x00000007; }
  161. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  162. };
  163. /*************************************************************************/
  164. /* CodeSignCodeDirectory */
  165. /*************************************************************************/
  166. // Code Directory, runtime options, code segment and special structure hashes.
  167. class CodeSignCodeDirectory : public CodeSignBlob {
  168. public:
  169. enum Slot {
  170. SLOT_INFO_PLIST = -1,
  171. SLOT_REQUIREMENTS = -2,
  172. SLOT_RESOURCES = -3,
  173. SLOT_APP_SPECIFIC = -4, // Unused.
  174. SLOT_ENTITLEMENTS = -5,
  175. SLOT_RESERVER1 = -6, // Unused.
  176. SLOT_DER_ENTITLEMENTS = -7,
  177. };
  178. enum CodeSignExecSegFlags {
  179. EXECSEG_MAIN_BINARY = 0x1,
  180. EXECSEG_ALLOW_UNSIGNED = 0x10,
  181. EXECSEG_DEBUGGER = 0x20,
  182. EXECSEG_JIT = 0x40,
  183. EXECSEG_SKIP_LV = 0x80,
  184. EXECSEG_CAN_LOAD_CDHASH = 0x100,
  185. EXECSEG_CAN_EXEC_CDHASH = 0x200,
  186. };
  187. enum CodeSignatureFlags {
  188. SIGNATURE_HOST = 0x0001,
  189. SIGNATURE_ADHOC = 0x0002,
  190. SIGNATURE_TASK_ALLOW = 0x0004,
  191. SIGNATURE_INSTALLER = 0x0008,
  192. SIGNATURE_FORCED_LV = 0x0010,
  193. SIGNATURE_INVALID_ALLOWED = 0x0020,
  194. SIGNATURE_FORCE_HARD = 0x0100,
  195. SIGNATURE_FORCE_KILL = 0x0200,
  196. SIGNATURE_FORCE_EXPIRATION = 0x0400,
  197. SIGNATURE_RESTRICT = 0x0800,
  198. SIGNATURE_ENFORCEMENT = 0x1000,
  199. SIGNATURE_LIBRARY_VALIDATION = 0x2000,
  200. SIGNATURE_ENTITLEMENTS_VALIDATED = 0x4000,
  201. SIGNATURE_NVRAM_UNRESTRICTED = 0x8000,
  202. SIGNATURE_RUNTIME = 0x10000,
  203. SIGNATURE_LINKER_SIGNED = 0x20000,
  204. };
  205. private:
  206. PackedByteArray blob;
  207. struct CodeDirectoryHeader {
  208. uint32_t version; // Using version 0x0020500.
  209. uint32_t flags; // // Option flags.
  210. uint32_t hash_offset; // Slot zero offset.
  211. uint32_t ident_offset; // Identifier string offset.
  212. uint32_t special_slots; // Nr. of slots with negative index.
  213. uint32_t code_slots; // Nr. of slots with index >= 0, (code_limit / page_size).
  214. uint32_t code_limit; // Everything before code signature load command offset.
  215. uint8_t hash_size; // 20 (SHA-1) or 32 (SHA-256).
  216. uint8_t hash_type; // 1 (SHA-1) or 2 (SHA-256).
  217. uint8_t platform; // Not used.
  218. uint8_t page_size; // Page size, power of two, 2^12 (4096).
  219. uint32_t spare2; // Not used.
  220. // Version 0x20100
  221. uint32_t scatter_vector_offset; // Set to 0 and ignore.
  222. // Version 0x20200
  223. uint32_t team_offset; // Team id string offset.
  224. // Version 0x20300
  225. uint32_t spare3; // Not used.
  226. uint64_t code_limit_64; // Set to 0 and ignore.
  227. // Version 0x20400
  228. uint64_t exec_seg_base; // Start of the signed code segment.
  229. uint64_t exec_seg_limit; // Code segment (__TEXT) vmsize.
  230. uint64_t exec_seg_flags; // Executable segment flags.
  231. // Version 0x20500
  232. uint32_t runtime; // Runtime version.
  233. uint32_t pre_encrypt_offset; // Set to 0 and ignore.
  234. };
  235. int32_t pages = 0;
  236. int32_t remain = 0;
  237. int32_t code_slots = 0;
  238. int32_t special_slots = 0;
  239. public:
  240. CodeSignCodeDirectory();
  241. 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);
  242. int32_t get_page_count();
  243. int32_t get_page_remainder();
  244. bool set_hash_in_slot(const PackedByteArray &p_hash, int p_slot);
  245. virtual PackedByteArray get_hash_sha1() const override;
  246. virtual PackedByteArray get_hash_sha256() const override;
  247. virtual int get_size() const override;
  248. virtual uint32_t get_index_type() const override { return 0x00000000; }
  249. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  250. };
  251. /*************************************************************************/
  252. /* CodeSignSignature */
  253. /*************************************************************************/
  254. class CodeSignSignature : public CodeSignBlob {
  255. PackedByteArray blob;
  256. public:
  257. CodeSignSignature();
  258. virtual PackedByteArray get_hash_sha1() const override;
  259. virtual PackedByteArray get_hash_sha256() const override;
  260. virtual int get_size() const override;
  261. virtual uint32_t get_index_type() const override { return 0x00010000; }
  262. virtual void write_to_file(Ref<FileAccess> p_file) const override;
  263. };
  264. /*************************************************************************/
  265. /* CodeSignSuperBlob */
  266. /*************************************************************************/
  267. class CodeSignSuperBlob {
  268. Vector<Ref<CodeSignBlob>> blobs;
  269. public:
  270. bool add_blob(const Ref<CodeSignBlob> &p_blob);
  271. int get_size() const;
  272. void write_to_file(Ref<FileAccess> p_file) const;
  273. };
  274. /*************************************************************************/
  275. /* CodeSign */
  276. /*************************************************************************/
  277. class CodeSign {
  278. static PackedByteArray file_hash_sha1(const String &p_path);
  279. static PackedByteArray file_hash_sha256(const String &p_path);
  280. 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);
  281. public:
  282. static Error codesign(bool p_use_hardened_runtime, bool p_force, const String &p_path, const String &p_ent_path, String &r_error_msg);
  283. };