Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/codesign.h
9896 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
PackedByteArray blob;
136
137
static inline size_t PAD(size_t s, size_t a) {
138
return (s % a == 0) ? 0 : (a - s % a);
139
}
140
141
_FORCE_INLINE_ void _parse_certificate_slot(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
142
_FORCE_INLINE_ void _parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
143
_FORCE_INLINE_ void _parse_oid_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
144
_FORCE_INLINE_ void _parse_hash_string(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
145
_FORCE_INLINE_ void _parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
146
_FORCE_INLINE_ void _parse_date(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
147
_FORCE_INLINE_ bool _parse_match(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const;
148
149
public:
150
CodeSignRequirements();
151
CodeSignRequirements(const PackedByteArray &p_data);
152
153
Vector<String> parse_requirements() const;
154
155
virtual PackedByteArray get_hash_sha1() const override;
156
virtual PackedByteArray get_hash_sha256() const override;
157
158
virtual int get_size() const override;
159
160
virtual uint32_t get_index_type() const override { return 0x00000002; }
161
virtual void write_to_file(Ref<FileAccess> p_file) const override;
162
};
163
164
/*************************************************************************/
165
/* CodeSignEntitlementsText */
166
/*************************************************************************/
167
168
// PList formatted entitlements.
169
170
class CodeSignEntitlementsText : public CodeSignBlob {
171
PackedByteArray blob;
172
173
public:
174
CodeSignEntitlementsText();
175
CodeSignEntitlementsText(const String &p_string);
176
177
virtual PackedByteArray get_hash_sha1() const override;
178
virtual PackedByteArray get_hash_sha256() const override;
179
180
virtual int get_size() const override;
181
182
virtual uint32_t get_index_type() const override { return 0x00000005; }
183
virtual void write_to_file(Ref<FileAccess> p_file) const override;
184
};
185
186
/*************************************************************************/
187
/* CodeSignEntitlementsBinary */
188
/*************************************************************************/
189
190
// ASN.1 serialized entitlements.
191
192
class CodeSignEntitlementsBinary : public CodeSignBlob {
193
PackedByteArray blob;
194
195
public:
196
CodeSignEntitlementsBinary();
197
CodeSignEntitlementsBinary(const String &p_string);
198
199
virtual PackedByteArray get_hash_sha1() const override;
200
virtual PackedByteArray get_hash_sha256() const override;
201
202
virtual int get_size() const override;
203
204
virtual uint32_t get_index_type() const override { return 0x00000007; }
205
virtual void write_to_file(Ref<FileAccess> p_file) const override;
206
};
207
208
/*************************************************************************/
209
/* CodeSignCodeDirectory */
210
/*************************************************************************/
211
212
// Code Directory, runtime options, code segment and special structure hashes.
213
214
class CodeSignCodeDirectory : public CodeSignBlob {
215
public:
216
enum Slot {
217
SLOT_INFO_PLIST = -1,
218
SLOT_REQUIREMENTS = -2,
219
SLOT_RESOURCES = -3,
220
SLOT_APP_SPECIFIC = -4, // Unused.
221
SLOT_ENTITLEMENTS = -5,
222
SLOT_RESERVER1 = -6, // Unused.
223
SLOT_DER_ENTITLEMENTS = -7,
224
};
225
226
enum CodeSignExecSegFlags {
227
EXECSEG_MAIN_BINARY = 0x1,
228
EXECSEG_ALLOW_UNSIGNED = 0x10,
229
EXECSEG_DEBUGGER = 0x20,
230
EXECSEG_JIT = 0x40,
231
EXECSEG_SKIP_LV = 0x80,
232
EXECSEG_CAN_LOAD_CDHASH = 0x100,
233
EXECSEG_CAN_EXEC_CDHASH = 0x200,
234
};
235
236
enum CodeSignatureFlags {
237
SIGNATURE_HOST = 0x0001,
238
SIGNATURE_ADHOC = 0x0002,
239
SIGNATURE_TASK_ALLOW = 0x0004,
240
SIGNATURE_INSTALLER = 0x0008,
241
SIGNATURE_FORCED_LV = 0x0010,
242
SIGNATURE_INVALID_ALLOWED = 0x0020,
243
SIGNATURE_FORCE_HARD = 0x0100,
244
SIGNATURE_FORCE_KILL = 0x0200,
245
SIGNATURE_FORCE_EXPIRATION = 0x0400,
246
SIGNATURE_RESTRICT = 0x0800,
247
SIGNATURE_ENFORCEMENT = 0x1000,
248
SIGNATURE_LIBRARY_VALIDATION = 0x2000,
249
SIGNATURE_ENTITLEMENTS_VALIDATED = 0x4000,
250
SIGNATURE_NVRAM_UNRESTRICTED = 0x8000,
251
SIGNATURE_RUNTIME = 0x10000,
252
SIGNATURE_LINKER_SIGNED = 0x20000,
253
};
254
255
private:
256
PackedByteArray blob;
257
258
struct CodeDirectoryHeader {
259
uint32_t version; // Using version 0x0020500.
260
uint32_t flags; // // Option flags.
261
uint32_t hash_offset; // Slot zero offset.
262
uint32_t ident_offset; // Identifier string offset.
263
uint32_t special_slots; // Nr. of slots with negative index.
264
uint32_t code_slots; // Nr. of slots with index >= 0, (code_limit / page_size).
265
uint32_t code_limit; // Everything before code signature load command offset.
266
uint8_t hash_size; // 20 (SHA-1) or 32 (SHA-256).
267
uint8_t hash_type; // 1 (SHA-1) or 2 (SHA-256).
268
uint8_t platform; // Not used.
269
uint8_t page_size; // Page size, power of two, 2^12 (4096).
270
uint32_t spare2; // Not used.
271
// Version 0x20100
272
uint32_t scatter_vector_offset; // Set to 0 and ignore.
273
// Version 0x20200
274
uint32_t team_offset; // Team id string offset.
275
// Version 0x20300
276
uint32_t spare3; // Not used.
277
uint64_t code_limit_64; // Set to 0 and ignore.
278
// Version 0x20400
279
uint64_t exec_seg_base; // Start of the signed code segment.
280
uint64_t exec_seg_limit; // Code segment (__TEXT) vmsize.
281
uint64_t exec_seg_flags; // Executable segment flags.
282
// Version 0x20500
283
uint32_t runtime; // Runtime version.
284
uint32_t pre_encrypt_offset; // Set to 0 and ignore.
285
};
286
287
int32_t pages = 0;
288
int32_t remain = 0;
289
int32_t code_slots = 0;
290
int32_t special_slots = 0;
291
292
public:
293
CodeSignCodeDirectory();
294
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);
295
296
int32_t get_page_count();
297
int32_t get_page_remainder();
298
299
bool set_hash_in_slot(const PackedByteArray &p_hash, int p_slot);
300
301
virtual PackedByteArray get_hash_sha1() const override;
302
virtual PackedByteArray get_hash_sha256() const override;
303
304
virtual int get_size() const override;
305
virtual uint32_t get_index_type() const override { return 0x00000000; }
306
307
virtual void write_to_file(Ref<FileAccess> p_file) const override;
308
};
309
310
/*************************************************************************/
311
/* CodeSignSignature */
312
/*************************************************************************/
313
314
class CodeSignSignature : public CodeSignBlob {
315
PackedByteArray blob;
316
317
public:
318
CodeSignSignature();
319
320
virtual PackedByteArray get_hash_sha1() const override;
321
virtual PackedByteArray get_hash_sha256() const override;
322
323
virtual int get_size() const override;
324
virtual uint32_t get_index_type() const override { return 0x00010000; }
325
326
virtual void write_to_file(Ref<FileAccess> p_file) const override;
327
};
328
329
/*************************************************************************/
330
/* CodeSignSuperBlob */
331
/*************************************************************************/
332
333
class CodeSignSuperBlob {
334
Vector<Ref<CodeSignBlob>> blobs;
335
336
public:
337
bool add_blob(const Ref<CodeSignBlob> &p_blob);
338
339
int get_size() const;
340
void write_to_file(Ref<FileAccess> p_file) const;
341
};
342
343
/*************************************************************************/
344
/* CodeSign */
345
/*************************************************************************/
346
347
class CodeSign {
348
static PackedByteArray file_hash_sha1(const String &p_path);
349
static PackedByteArray file_hash_sha256(const String &p_path);
350
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);
351
352
public:
353
static Error codesign(bool p_use_hardened_runtime, bool p_force, const String &p_path, const String &p_ent_path, String &r_error_msg);
354
};
355
356