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