| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include "core/io/file_access.h" |
| 44 |
#include "core/object/ref_counted.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class CodeSignCodeResources { |
| 51 |
public: |
| 52 |
enum class CRMatch { |
| 53 |
CR_MATCH_NO, |
| 54 |
CR_MATCH_YES, |
| 55 |
CR_MATCH_NESTED, |
| 56 |
CR_MATCH_OPTIONAL, |
| 57 |
}; |
| 58 |
|
| 59 |
private: |
| 60 |
struct CRFile { |
| 61 |
String name; |
| 62 |
String hash; |
| 63 |
String hash2; |
| 64 |
bool optional; |
| 65 |
bool nested; |
| 66 |
String requirements; |
| 67 |
}; |
| 68 |
|
| 69 |
struct CRRule { |
| 70 |
String file_pattern; |
| 71 |
String key; |
| 72 |
int weight; |
| 73 |
bool store; |
| 74 |
CRRule() { |
| 75 |
weight = 1; |
| 76 |
store = true; |
| 77 |
} |
| 78 |
CRRule(const String &p_file_pattern, const String &p_key, int p_weight, bool p_store) { |
| 79 |
file_pattern = p_file_pattern; |
| 80 |
key = p_key; |
| 81 |
weight = p_weight; |
| 82 |
store = p_store; |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
Vector<CRRule> rules1; |
| 87 |
Vector<CRRule> rules2; |
| 88 |
|
| 89 |
Vector<CRFile> files1; |
| 90 |
Vector<CRFile> files2; |
| 91 |
|
| 92 |
String hash_sha1_base64(const String &p_path); |
| 93 |
String hash_sha256_base64(const String &p_path); |
| 94 |
|
| 95 |
public: |
| 96 |
void add_rule1(const String &p_rule, const String &p_key = "", int p_weight = 0, bool p_store = true); |
| 97 |
void add_rule2(const String &p_rule, const String &p_key = "", int p_weight = 0, bool p_store = true); |
| 98 |
|
| 99 |
CRMatch match_rules1(const String &p_path) const; |
| 100 |
CRMatch match_rules2(const String &p_path) const; |
| 101 |
|
| 102 |
bool add_file1(const String &p_root, const String &p_path); |
| 103 |
bool add_file2(const String &p_root, const String &p_path); |
| 104 |
bool add_nested_file(const String &p_root, const String &p_path, const String &p_exepath); |
| 105 |
|
| 106 |
bool add_folder_recursive(const String &p_root, const String &p_path = "", const String &p_main_exe_path = ""); |
| 107 |
|
| 108 |
bool save_to_file(const String &p_path); |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
class CodeSignBlob : public RefCounted { |
| 116 |
GDSOFTCLASS(CodeSignBlob, RefCounted); |
| 117 |
|
| 118 |
public: |
| 119 |
virtual PackedByteArray get_hash_sha1() const = 0; |
| 120 |
virtual PackedByteArray get_hash_sha256() const = 0; |
| 121 |
|
| 122 |
virtual int get_size() const = 0; |
| 123 |
virtual uint32_t get_index_type() const = 0; |
| 124 |
|
| 125 |
virtual void write_to_file(Ref<FileAccess> p_file) const = 0; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
class CodeSignRequirements : public CodeSignBlob { |
| 135 |
GDSOFTCLASS(CodeSignRequirements, CodeSignBlob); |
| 136 |
|
| 137 |
PackedByteArray blob; |
| 138 |
|
| 139 |
static inline size_t PAD(size_t s, size_t a) { |
| 140 |
return (s % a == 0) ? 0 : (a - s % a); |
| 141 |
} |
| 142 |
|
| 143 |
_FORCE_INLINE_ void _parse_certificate_slot(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 144 |
_FORCE_INLINE_ void _parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 145 |
_FORCE_INLINE_ void _parse_oid_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 146 |
_FORCE_INLINE_ void _parse_hash_string(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 147 |
_FORCE_INLINE_ void _parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 148 |
_FORCE_INLINE_ void _parse_date(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 149 |
_FORCE_INLINE_ bool _parse_match(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const; |
| 150 |
|
| 151 |
public: |
| 152 |
CodeSignRequirements(); |
| 153 |
CodeSignRequirements(const PackedByteArray &p_data); |
| 154 |
|
| 155 |
Vector<String> parse_requirements() const; |
| 156 |
|
| 157 |
virtual PackedByteArray get_hash_sha1() const override; |
| 158 |
virtual PackedByteArray get_hash_sha256() const override; |
| 159 |
|
| 160 |
virtual int get_size() const override; |
| 161 |
|
| 162 |
virtual uint32_t get_index_type() const override { return 0x00000002; } |
| 163 |
virtual void write_to_file(Ref<FileAccess> p_file) const override; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
class CodeSignEntitlementsText : public CodeSignBlob { |
| 173 |
GDSOFTCLASS(CodeSignEntitlementsText, CodeSignBlob); |
| 174 |
|
| 175 |
PackedByteArray blob; |
| 176 |
|
| 177 |
public: |
| 178 |
CodeSignEntitlementsText(); |
| 179 |
CodeSignEntitlementsText(const String &p_string); |
| 180 |
|
| 181 |
virtual PackedByteArray get_hash_sha1() const override; |
| 182 |
virtual PackedByteArray get_hash_sha256() const override; |
| 183 |
|
| 184 |
virtual int get_size() const override; |
| 185 |
|
| 186 |
virtual uint32_t get_index_type() const override { return 0x00000005; } |
| 187 |
virtual void write_to_file(Ref<FileAccess> p_file) const override; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
class CodeSignEntitlementsBinary : public CodeSignBlob { |
| 197 |
GDSOFTCLASS(CodeSignEntitlementsBinary, CodeSignBlob); |
| 198 |
|
| 199 |
PackedByteArray blob; |
| 200 |
|
| 201 |
public: |
| 202 |
CodeSignEntitlementsBinary(); |
| 203 |
CodeSignEntitlementsBinary(const String &p_string); |
| 204 |
|
| 205 |
virtual PackedByteArray get_hash_sha1() const override; |
| 206 |
virtual PackedByteArray get_hash_sha256() const override; |
| 207 |
|
| 208 |
virtual int get_size() const override; |
| 209 |
|
| 210 |
virtual uint32_t get_index_type() const override { return 0x00000007; } |
| 211 |
virtual void write_to_file(Ref<FileAccess> p_file) const override; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
class CodeSignCodeDirectory : public CodeSignBlob { |
| 221 |
GDSOFTCLASS(CodeSignCodeDirectory, CodeSignBlob); |
| 222 |
|
| 223 |
public: |
| 224 |
enum Slot { |
| 225 |
SLOT_INFO_PLIST = -1, |
| 226 |
SLOT_REQUIREMENTS = -2, |
| 227 |
SLOT_RESOURCES = -3, |
| 228 |
SLOT_APP_SPECIFIC = -4, |
| 229 |
SLOT_ENTITLEMENTS = -5, |
| 230 |
SLOT_RESERVER1 = -6, |
| 231 |
SLOT_DER_ENTITLEMENTS = -7, |
| 232 |
}; |
| 233 |
|
| 234 |
enum CodeSignExecSegFlags { |
| 235 |
EXECSEG_MAIN_BINARY = 0x1, |
| 236 |
EXECSEG_ALLOW_UNSIGNED = 0x10, |
| 237 |
EXECSEG_DEBUGGER = 0x20, |
| 238 |
EXECSEG_JIT = 0x40, |
| 239 |
EXECSEG_SKIP_LV = 0x80, |
| 240 |
EXECSEG_CAN_LOAD_CDHASH = 0x100, |
| 241 |
EXECSEG_CAN_EXEC_CDHASH = 0x200, |
| 242 |
}; |
| 243 |
|
| 244 |
enum CodeSignatureFlags { |
| 245 |
SIGNATURE_HOST = 0x0001, |
| 246 |
SIGNATURE_ADHOC = 0x0002, |
| 247 |
SIGNATURE_TASK_ALLOW = 0x0004, |
| 248 |
SIGNATURE_INSTALLER = 0x0008, |
| 249 |
SIGNATURE_FORCED_LV = 0x0010, |
| 250 |
SIGNATURE_INVALID_ALLOWED = 0x0020, |
| 251 |
SIGNATURE_FORCE_HARD = 0x0100, |
| 252 |
SIGNATURE_FORCE_KILL = 0x0200, |
| 253 |
SIGNATURE_FORCE_EXPIRATION = 0x0400, |
| 254 |
SIGNATURE_RESTRICT = 0x0800, |
| 255 |
SIGNATURE_ENFORCEMENT = 0x1000, |
| 256 |
SIGNATURE_LIBRARY_VALIDATION = 0x2000, |
| 257 |
SIGNATURE_ENTITLEMENTS_VALIDATED = 0x4000, |
| 258 |
SIGNATURE_NVRAM_UNRESTRICTED = 0x8000, |
| 259 |
SIGNATURE_RUNTIME = 0x10000, |
| 260 |
SIGNATURE_LINKER_SIGNED = 0x20000, |
| 261 |
}; |
| 262 |
|
| 263 |
private: |
| 264 |
PackedByteArray blob; |
| 265 |
|
| 266 |
struct CodeDirectoryHeader { |
| 267 |
uint32_t version; |
| 268 |
uint32_t flags; |
| 269 |
uint32_t hash_offset; |
| 270 |
uint32_t ident_offset; |
| 271 |
uint32_t special_slots; |
| 272 |
uint32_t code_slots; |
| 273 |
uint32_t code_limit; |
| 274 |
uint8_t hash_size; |
| 275 |
uint8_t hash_type; |
| 276 |
uint8_t platform; |
| 277 |
uint8_t page_size; |
| 278 |
uint32_t spare2; |
| 279 |
|
| 280 |
uint32_t scatter_vector_offset; |
| 281 |
|
| 282 |
uint32_t team_offset; |
| 283 |
|
| 284 |
uint32_t spare3; |
| 285 |
uint64_t code_limit_64; |
| 286 |
|
| 287 |
uint64_t exec_seg_base; |
| 288 |
uint64_t exec_seg_limit; |
| 289 |
uint64_t exec_seg_flags; |
| 290 |
|
| 291 |
uint32_t runtime; |
| 292 |
uint32_t pre_encrypt_offset; |
| 293 |
}; |
| 294 |
|
| 295 |
int32_t pages = 0; |
| 296 |
int32_t remain = 0; |
| 297 |
int32_t code_slots = 0; |
| 298 |
int32_t special_slots = 0; |
| 299 |
|
| 300 |
public: |
| 301 |
CodeSignCodeDirectory(); |
| 302 |
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); |
| 303 |
|
| 304 |
int32_t get_page_count(); |
| 305 |
int32_t get_page_remainder(); |
| 306 |
|
| 307 |
bool set_hash_in_slot(const PackedByteArray &p_hash, int p_slot); |
| 308 |
|
| 309 |
virtual PackedByteArray get_hash_sha1() const override; |
| 310 |
virtual PackedByteArray get_hash_sha256() const override; |
| 311 |
|
| 312 |
virtual int get_size() const override; |
| 313 |
virtual uint32_t get_index_type() const override { return 0x00000000; } |
| 314 |
|
| 315 |
virtual void write_to_file(Ref<FileAccess> p_file) const override; |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
class CodeSignSignature : public CodeSignBlob { |
| 323 |
GDSOFTCLASS(CodeSignSignature, CodeSignBlob); |
| 324 |
|
| 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 |
|
| 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 |
|
| 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 |
}; |