Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/codesign.h
20860 views
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
#pragma once
32
33
// macOS code signature creation utility.
34
//
35
// Current implementation has the following limitation:
36
// - Only version 11.3.0 signatures are supported.
37
// - Only "framework" and "app" bundle types are supported.
38
// - Page hash array scattering is not supported.
39
// - 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).
40
// - Requirements code generator is not implemented (only hard-coded requirements for the ad-hoc signing is supported).
41
// - RFC5652/CMS blob generation is not implemented, supports ad-hoc signing only.
42
43
#include "core/io/file_access.h"
44
#include "core/object/ref_counted.h"
45
46
/*************************************************************************/
47
/* CodeSignCodeResources */
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
/* CodeSignBlob */
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
/* CodeSignRequirements */
130
/*************************************************************************/
131
132
// 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.
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
/* CodeSignEntitlementsText */
168
/*************************************************************************/
169
170
// PList formatted entitlements.
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
/* CodeSignEntitlementsBinary */
192
/*************************************************************************/
193
194
// ASN.1 serialized entitlements.
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
/* CodeSignCodeDirectory */
216
/*************************************************************************/
217
218
// Code Directory, runtime options, code segment and special structure hashes.
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, // Unused.
229
SLOT_ENTITLEMENTS = -5,
230
SLOT_RESERVER1 = -6, // Unused.
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; // Using version 0x0020500.
268
uint32_t flags; // // Option flags.
269
uint32_t hash_offset; // Slot zero offset.
270
uint32_t ident_offset; // Identifier string offset.
271
uint32_t special_slots; // Nr. of slots with negative index.
272
uint32_t code_slots; // Nr. of slots with index >= 0, (code_limit / page_size).
273
uint32_t code_limit; // Everything before code signature load command offset.
274
uint8_t hash_size; // 20 (SHA-1) or 32 (SHA-256).
275
uint8_t hash_type; // 1 (SHA-1) or 2 (SHA-256).
276
uint8_t platform; // Not used.
277
uint8_t page_size; // Page size, power of two, 2^12 (4096).
278
uint32_t spare2; // Not used.
279
// Version 0x20100
280
uint32_t scatter_vector_offset; // Set to 0 and ignore.
281
// Version 0x20200
282
uint32_t team_offset; // Team id string offset.
283
// Version 0x20300
284
uint32_t spare3; // Not used.
285
uint64_t code_limit_64; // Set to 0 and ignore.
286
// Version 0x20400
287
uint64_t exec_seg_base; // Start of the signed code segment.
288
uint64_t exec_seg_limit; // Code segment (__TEXT) vmsize.
289
uint64_t exec_seg_flags; // Executable segment flags.
290
// Version 0x20500
291
uint32_t runtime; // Runtime version.
292
uint32_t pre_encrypt_offset; // Set to 0 and ignore.
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
/* CodeSignSignature */
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
/* 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