codesign.h 14 KB

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