forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProvisioningProfile.cpp
More file actions
executable file
·456 lines (368 loc) · 12.2 KB
/
ProvisioningProfile.cpp
File metadata and controls
executable file
·456 lines (368 loc) · 12.2 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// ProvisioningProfile.cpp
// AltSign-Windows
//
// Created by Riley Testut on 8/12/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
#include "ProvisioningProfile.hpp"
#include "Certificate.hpp"
#include "Error.hpp"
#include <winsock2.h>
#include <limits.h>
#include <stddef.h>
#include <time.h>
#include <sstream>
#if SIZE_MAX == UINT_MAX
typedef int ssize_t; /* common 32 bit case */
#elif SIZE_MAX == ULONG_MAX
typedef long ssize_t; /* linux 64 bits */
#elif SIZE_MAX == ULLONG_MAX
typedef long long ssize_t; /* windows 64 bits */
#elif SIZE_MAX == USHRT_MAX
typedef short ssize_t; /* is this even possible? */
#else
#error platform has exotic SIZE_MAX
#endif
#define ASN1_SEQUENCE 0x30
#define ASN1_CONTAINER 0xA0
#define ASN1_OBJECT_IDENTIFIER 0x06
#define ASN1_OCTET_STRING 0x04
extern std::vector<unsigned char> readFile(const char* filename);
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
#define SECONDS_FROM_1970_TO_APPLE_REFERENCE_DATE 978307200
ProvisioningProfile::ProvisioningProfile() : _entitlements(nullptr)
{
}
ProvisioningProfile::~ProvisioningProfile()
{
if (this->_entitlements != nullptr)
{
plist_free(this->_entitlements);
this->_entitlements = nullptr;
}
}
ProvisioningProfile::ProvisioningProfile(const ProvisioningProfile& other) : _entitlements(nullptr)
{
this->Copy(other);
}
ProvisioningProfile& ProvisioningProfile::operator=(const ProvisioningProfile& other)
{
if (this == &other)
{
return *this;
}
ProvisioningProfile temp(other);
this->Copy(temp);
return *this;
}
void ProvisioningProfile::Copy(const ProvisioningProfile& other)
{
this->_name = other._name;
this->_identifier = other._identifier;
this->_uuid = other._uuid;
this->_bundleIdentifier = other._bundleIdentifier;
this->_teamIdentifier = other._teamIdentifier;
this->_creationDateSeconds = other._creationDateSeconds;
this->_creationDateMicroseconds = other._creationDateMicroseconds;
this->_expirationDateSeconds = other._expirationDateSeconds;
this->_expirationDateMicroseconds = other._expirationDateMicroseconds;
this->_isFreeProvisioningProfile = other._isFreeProvisioningProfile;
this->_data = other._data;
if (this->_entitlements != nullptr)
{
plist_free(this->_entitlements);
}
// plist_copy returns nullptr if given nullptr.
this->_entitlements = plist_copy(other._entitlements);
}
ProvisioningProfile::ProvisioningProfile(plist_t plist) : _entitlements(nullptr)
{
auto identifierNode = plist_dict_get_item(plist, "provisioningProfileId");
auto dataNode = plist_dict_get_item(plist, "encodedProfile");
if (identifierNode == nullptr || dataNode == nullptr)
{
throw APIError(APIErrorCode::InvalidResponse);
}
char *bytes = nullptr;
uint64_t length = 0;
plist_get_data_val(dataNode, &bytes, &length);
std::vector<unsigned char> data;
data.reserve(length);
for (int i = 0; i < length; i++)
{
data.push_back(bytes[i]);
}
free(bytes);
try
{
this->ParseData(data);
}
catch (std::exception& exception)
{
throw APIError(APIErrorCode::InvalidResponse);
}
char *identifier = nullptr;
plist_get_string_val(identifierNode, &identifier);
_identifier = identifier;
_data = data;
free(identifier);
}
ProvisioningProfile::ProvisioningProfile(std::string filepath) : _entitlements(nullptr) /* throws */
{
auto data = readFile(filepath.c_str());
this->ParseData(data);
}
ProvisioningProfile::ProvisioningProfile(std::vector<unsigned char>& data) : _entitlements(nullptr) /* throws */
{
this->ParseData(data);
}
// Heavily inspired by libimobiledevice/ideviceprovision.c
// https://github.com/libimobiledevice/libimobiledevice/blob/ddba0b5efbcab483e80be10130c5c797f9ac8d08/tools/ideviceprovision.c#L98
void ProvisioningProfile::ParseData(std::vector<unsigned char> &encodedData)
{
// Helper blocks
auto itemSize = [](unsigned char *pointer) -> size_t
{
size_t size = -1;
char bsize = *(pointer + 1);
if (bsize & 0x80)
{
switch (bsize & 0xF)
{
case 2:
{
uint16_t value = *(uint16_t *)(pointer + 2);
size = ntohs(value);
break;
}
case 3:
{
uint32_t value = *(uint32_t *)(pointer + 2);
size = ntohl(value) >> 8;
break;
}
case 4:
{
uint32_t value = *(uint32_t *)(pointer + 2);
size = ntohl(value);
break;
}
default:
break;
}
}
else
{
size = (size_t)bsize;
}
return size;
};
auto advanceToNextItem = [](unsigned char *pointer) -> unsigned char *
{
unsigned char *nextItem = pointer;
char bsize = *(pointer + 1);
if (bsize & 0x80)
{
nextItem += 2 + (bsize & 0xF);
}
else
{
nextItem += 3;
}
return nextItem;
};
auto skipNextItem = [&itemSize](unsigned char *pointer) -> unsigned char *
{
size_t size = itemSize(pointer);
unsigned char *nextItem = pointer + 2 + size;
return nextItem;
};
/* Start parsing */
unsigned char *pointer = (unsigned char *)encodedData.data();
if (!pointer || *pointer != ASN1_SEQUENCE)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = advanceToNextItem(pointer);
if (!pointer || *pointer != ASN1_OBJECT_IDENTIFIER)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = skipNextItem(pointer);
if (!pointer || *pointer != ASN1_CONTAINER)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = advanceToNextItem(pointer);
if (!pointer || *pointer != ASN1_SEQUENCE)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = advanceToNextItem(pointer);
// Skip 2 items.
for (int i = 0; i < 2; i++)
{
pointer = skipNextItem(pointer);
}
if (!pointer || *pointer != ASN1_SEQUENCE)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = advanceToNextItem(pointer);
if (!pointer || *pointer != ASN1_OBJECT_IDENTIFIER)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = skipNextItem(pointer);
if (!pointer || *pointer != ASN1_CONTAINER)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
pointer = advanceToNextItem(pointer);
if (!pointer || *pointer != ASN1_OCTET_STRING)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
size_t length = itemSize(pointer);
pointer = advanceToNextItem(pointer);
plist_t parsedPlist = nullptr;
plist_from_memory((const char *)pointer, (unsigned int)length, &parsedPlist);
if (parsedPlist == nullptr)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
char* name = nullptr;
char* uuid = nullptr;
char* teamIdentifier = nullptr;
char* rawApplicationIdentifier = nullptr;
auto cleanUp = [&name, &uuid, &teamIdentifier, &rawApplicationIdentifier, &parsedPlist]() {
plist_free(parsedPlist);
if (name != nullptr)
{
free(name);
}
if (uuid != nullptr)
{
free(uuid);
}
if (teamIdentifier != nullptr)
{
free(teamIdentifier);
}
if (rawApplicationIdentifier != nullptr)
{
free(rawApplicationIdentifier);
}
};
try
{
auto nameNode = plist_dict_get_item(parsedPlist, "Name");
auto uuidNode = plist_dict_get_item(parsedPlist, "UUID");
auto teamIdentifiersNode = plist_dict_get_item(parsedPlist, "TeamIdentifier");
auto creationDateNode = plist_dict_get_item(parsedPlist, "CreationDate");
auto expirationDateNode = plist_dict_get_item(parsedPlist, "ExpirationDate");
auto entitlementsNode = plist_dict_get_item(parsedPlist, "Entitlements");
if (nameNode == nullptr || uuidNode == nullptr || teamIdentifiersNode == nullptr || creationDateNode == nullptr || expirationDateNode == nullptr || entitlementsNode == nullptr)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
auto teamIdentifierNode = plist_array_get_item(teamIdentifiersNode, 0);
if (teamIdentifierNode == nullptr)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
auto isFreeProvisioningProfileNode = plist_dict_get_item(parsedPlist, "LocalProvision");
if (isFreeProvisioningProfileNode != nullptr)
{
uint8_t isFreeProvisioningProfile = 0;
plist_get_bool_val(isFreeProvisioningProfileNode, &isFreeProvisioningProfile);
_isFreeProvisioningProfile = (isFreeProvisioningProfile != 0);
}
else
{
_isFreeProvisioningProfile = 0;
}
plist_get_string_val(nameNode, &name);
plist_get_string_val(uuidNode, &uuid);
plist_get_string_val(teamIdentifierNode, &teamIdentifier);
int32_t create_sec = 0;
int32_t create_usec = 0;
plist_get_date_val(creationDateNode, &create_sec, &create_usec);
int32_t expiration_sec = 0;
int32_t expiration_usec = 0;
plist_get_date_val(expirationDateNode, &expiration_sec, &expiration_usec);
plist_t bundleIdentifierNode = plist_dict_get_item(entitlementsNode, "application-identifier");
if (bundleIdentifierNode == nullptr)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
plist_get_string_val(bundleIdentifierNode, &rawApplicationIdentifier);
std::string applicationIdentifier(rawApplicationIdentifier);
size_t location = applicationIdentifier.find(".");
if (location == std::string::npos)
{
throw SignError(SignErrorCode::InvalidProvisioningProfile);
}
std::string bundleIdentifier(applicationIdentifier.begin() + location + 1, applicationIdentifier.end());
_name = name;
_uuid = uuid;
_teamIdentifier = teamIdentifier;
_bundleIdentifier = bundleIdentifier;
_creationDateSeconds = create_sec + SECONDS_FROM_1970_TO_APPLE_REFERENCE_DATE;
_creationDateMicroseconds = create_usec;
_expirationDateSeconds = expiration_sec + SECONDS_FROM_1970_TO_APPLE_REFERENCE_DATE;
_expirationDateMicroseconds = expiration_usec;
_entitlements = plist_copy(entitlementsNode);
_data = encodedData;
cleanUp();
}
catch (std::exception& e)
{
cleanUp();
throw;
}
}
#pragma mark - Getters -
std::string ProvisioningProfile::name() const
{
return _name;
}
std::optional<std::string> ProvisioningProfile::identifier() const
{
return _identifier;
}
std::string ProvisioningProfile::uuid() const
{
return _uuid;
}
std::string ProvisioningProfile::bundleIdentifier() const
{
return _bundleIdentifier;
}
std::string ProvisioningProfile::teamIdentifier() const
{
return _teamIdentifier;
}
std::vector<unsigned char> ProvisioningProfile::data() const
{
return _data;
}
timeval ProvisioningProfile::creationDate() const
{
timeval creationDate = { this->_creationDateSeconds, this->_creationDateMicroseconds };
return creationDate;
}
timeval ProvisioningProfile::expirationDate() const
{
timeval expirationDate = { this->_expirationDateSeconds, this->_expirationDateMicroseconds };
return expirationDate;
}
plist_t ProvisioningProfile::entitlements() const
{
return _entitlements;
}
bool ProvisioningProfile::isFreeProvisioningProfile() const
{
return _isFreeProvisioningProfile;
}