codesign.h 14 KB

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