-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.m
316 lines (248 loc) · 12.7 KB
/
main.m
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
// main.m
// appleDRM
//
// Created by failbr34k on 2021-08-27.
//
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import "sysGroupPath.h"
#import "libMobileGestalt.h"
#import <Block.h>
#import <Security/Security.h>
#import <mach-o/dyld.h>
#import <sys/types.h>
#import <mach-o/nlist.h>
#import "SecItemPriv.h"
#import "SecKeyPriv.h"
#import "SecPolicyPriv.h"
#import "SecTrustPriv.h"
#import "SecAccessControlPriv.h"
static void createRK(void)
{
NSError *error = nil;
CFAllocatorRef allocator = kCFAllocatorDefault;
CFTypeRef protection = kSecAttrAccessibleAlwaysThisDeviceOnlyPrivate;
CFErrorRef cferror = NULL;
SecAccessControlRef AccessControl = SecAccessControlCreateWithFlags(allocator, protection, kSecAccessControlPrivateKeyUsage, &cferror);
if (!AccessControl) {
NSLog(@"Failed to create access control.");
return ;
}
CFStringRef systemGroupPath = NULL;
const char* systemGroupName = NULL;
systemGroupName = "systemgroup.com.apple.mobileactivationd";
systemGroupPath = GetSystemGroupPath(systemGroupName);
NSString *uikPath = nil;
uikPath = (__bridge NSString *)systemGroupPath;
uikPath = [uikPath stringByAppendingPathComponent: @"Library/uik/"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL isDir;
BOOL isFound;
NSString *uikCertificate = [uikPath stringByAppendingPathComponent: @"uik.pem"];
isFound = [fileManager fileExistsAtPath: uikCertificate isDirectory: &isDir];
if (isFound == YES) {
SecAccessControlRef AccessControlRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlwaysThisDeviceOnlyPrivate, kSecAccessControlPrivateKeyUsage, NULL);
if (!AccessControlRef) {
NSLog(@"Failed to create access control.");
return;
}
NSMutableDictionary *keyAttributesRef = [[NSMutableDictionary alloc] init];
[keyAttributesRef setObject:(id)kSecAttrTokenIDSecureEnclave forKey:(id)kSecAttrTokenID ];
[keyAttributesRef setObject:(id)kSecAttrKeyTypeECSECPrimeRandom forKey:(id)kSecAttrKeyType ];
[keyAttributesRef setObject:CFBridgingRelease(AccessControlRef) forKey:(id)kSecAttrAccessControl ];
[keyAttributesRef setObject:[NSNumber numberWithBool:NO] forKey:(id)kSecAttrIsPermanent ];
NSDictionary *UIKkeyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDSecureEnclave,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO};
NSDictionary *keyAttributesSign = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDSecureEnclave,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControlRef,
(id)kSecAttrIsPermanent : @NO
};
BOOL HasPKA = MGGetBoolAnswer(CFSTR("HasPKA"));
if ( HasPKA == false ) {
NSLog(@"PKA return Null or false, using non PKA EC key.");
UIKkeyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDSecureEnclave,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO
};
[keyAttributesRef setObject:(id)kSecAttrKeyTypeECSECPrimeRandom forKey:(id)kSecAttrKeyType ];
keyAttributesSign = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDSecureEnclave,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControlRef,
(id)kSecAttrIsPermanent : @NO
};
} else {
NSLog(@"HasPKA true");
}
NSDictionary *keyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDAppleKeyStore,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeSecureEnclaveAttestation,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO
};
SecKeyRef uikAttestationKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)keyAttributes, (void *)&error);
if(!uikAttestationKey) {
NSLog(@"Failed to create AttesationKey.");
return;
}
NSLog(@"uikAttestationKey: %@",uikAttestationKey);
NSData* uikCertificateData = [NSData dataWithContentsOfFile:uikCertificate];
if(!uikCertificateData) {
NSLog(@"Failed to load %@.", uikCertificate);
return;
}
NSLog(@"uikCertificate: %@",uikCertificate);
NSDictionary *storekeyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDSecureEnclave,
(id)kSecAttrTokenOID : (id)uikCertificateData,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO
};
SecKeyRef uikPrivateKey = SecKeyCreateWithData((__bridge CFDataRef)uikCertificateData, (__bridge CFDictionaryRef)storekeyAttributes, (void *)&error);
if(!uikPrivateKey) {
NSLog(@"Failed to convert UIK.");
return;
}
NSLog(@"uikPrivateKey (signing key): %@",uikPrivateKey);
SecKeyRef UIKPub = SecKeyCopyPublicKey(uikPrivateKey);
if (!UIKPub)
{
NSLog(@"Failed to get uik pub.");
return;
}
NSLog(@"uikPublic : %@",UIKPub);
NSData *UIKPubKey = CFBridgingRelease(SecKeyCopyExternalRepresentation(UIKPub, (void *)&error));
NSLog(@"UIKPublicKey: %@",[UIKPubKey base64EncodedStringWithOptions:0]);
SecKeyRef SIKAttestationKey = SecKeyCopyAttestationKey(kSecKeyAttestationKeyTypeSIK, (void *)&error);
if (!SIKAttestationKey) {
NSLog(@"Failed to copy SIK attestation key.");
return;
}
NSData* SIKattestation = (__bridge NSData *)SecKeyCreateAttestation(SIKAttestationKey, uikAttestationKey, &cferror);
NSString* b64SIKatt = [SIKattestation base64EncodedStringWithOptions:0];
NSLog(@"SIKAttestation: %@",b64SIKatt);
NSData* UIKAttestation = (__bridge NSData *)SecKeyCreateAttestation(uikPrivateKey, uikAttestationKey, &cferror);
NSString* b64UIKatt = [UIKAttestation base64EncodedStringWithOptions:0];
NSLog(@"UIKAttestation: %@",b64UIKatt);
AccessControlRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlwaysThisDeviceOnlyPrivate, kSecAccessControlPrivateKeyUsage, NULL);
if (!AccessControlRef) {
NSLog(@"Failed to create access control.");
return;
}
SecKeyRef RefKey = SecKeyCreateRandomKey((CFDictionaryRef)keyAttributesRef, (void *)&error);
if (!RefKey) {
NSLog(@"Failed to create ref key. %@", [error localizedDescription] );
return;
}
NSLog(@"RK signing key: %p",RefKey);
}else{
SecAccessControlRef AccessControlRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlwaysThisDeviceOnlyPrivate, kSecAccessControlPrivateKeyUsage, NULL);
if (!AccessControlRef) {
NSLog(@"Failed to create access control.");
return;
}
NSMutableDictionary *keyAttributesRef = [[NSMutableDictionary alloc] init];
[keyAttributesRef setObject:(id)kSecAttrTokenIDAppleKeyStore forKey:(id)kSecAttrTokenID ];
[keyAttributesRef setObject:(id)kSecAttrKeyTypeECSECPrimeRandom forKey:(id)kSecAttrKeyType ];
[keyAttributesRef setObject:CFBridgingRelease(AccessControlRef) forKey:(id)kSecAttrAccessControl ];
[keyAttributesRef setObject:[NSNumber numberWithBool:NO] forKey:(id)kSecAttrIsPermanent ];
NSDictionary *UIKkeyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDAppleKeyStore,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO};
BOOL HasPKA = MGGetBoolAnswer(CFSTR("HasPKA"));
if ( HasPKA == false ) {
NSLog(@"PKA return Null or false, using non PKA EC key.");
UIKkeyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDAppleKeyStore,
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO
};
[keyAttributesRef setObject:(id)kSecAttrKeyTypeECSECPrimeRandom forKey:(id)kSecAttrKeyType ];
} else {
NSLog(@"HasPKA true");
}
NSDictionary *keyAttributes = @{
(id)kSecAttrTokenID : (id)kSecAttrTokenIDAppleKeyStore,
(id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeSecureEnclaveAttestation,
(id)kSecAttrAccessControl : (__bridge id)AccessControl,
(id)kSecAttrIsPermanent : @NO
};
SecKeyRef uikAttestationKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)keyAttributes, (void *)&error);
if(!uikAttestationKey) {
NSLog(@"Failed to create AttesationKey.");
return;
}
NSLog(@"uikAttestationKey: %@",uikAttestationKey);
SecKeyRef uikPrivateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef) UIKkeyAttributes, &cferror);
NSLog(@"uikPriv: %@",uikPrivateKey);
SecKeyRef UIKPub = SecKeyCopyPublicKey(uikPrivateKey);
if (!UIKPub)
{
NSLog(@"Failed to get uik pub.");
return;
}
NSData *UIKPubKey = CFBridgingRelease(SecKeyCopyExternalRepresentation(UIKPub, (void *)&error));
NSLog(@"UIKPublicKey: %@",[UIKPubKey base64EncodedStringWithOptions:0]);
SecKeyRef SIKAttestationKey = SecKeyCopyAttestationKey(kSecKeyAttestationKeyTypeSIK, (void *)&error);
if (!SIKAttestationKey) {
NSLog(@"Failed to copy SIK attestation key.");
return;
}
NSLog(@"SIK: %@",SIKAttestationKey);
NSDictionary* sikattr = (__bridge NSDictionary*)SecKeyCopyAttributes(SIKAttestationKey);
[sikattr writeToFile:@"/tmp/sikattributes.plist" atomically:YES];
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:@"/tmp/sikattributes.plist"];
NSLog(@"dict:\n%@",dict);
NSData* SIKattestation = (__bridge NSData *)SecKeyCreateAttestation(SIKAttestationKey, uikAttestationKey, &cferror);
NSString* b64SIKatt = [SIKattestation base64EncodedStringWithOptions:0];
NSLog(@"SIKAttestation: %@",b64SIKatt);
NSData* UIKAttestation = (__bridge NSData *)SecKeyCreateAttestation(SIKAttestationKey, uikPrivateKey, &cferror);
NSString* b64UIKatt = [UIKAttestation base64EncodedStringWithOptions:0];
NSLog(@"UIKAttestation: %@",b64UIKatt);
SecKeyRef RefKey = SecKeyCreateRandomKey((CFDictionaryRef)keyAttributesRef, (void *)&error);
if (!RefKey) {
NSLog(@"Failed to create ref key. %@", [error localizedDescription] );
return;
}
NSLog(@"refkey: %@", RefKey );
}
return;
}
int main(int argc, const char * argv[])
{
@autoreleasepool
{
createRK();
}
}
AccessControlRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlwaysThisDeviceOnlyPrivate, kSecAccessControlPrivateKeyUsage, NULL);
if (!AccessControlRef) {
NSLog(@"Failed to create access control.");
return;
}
SecKeyRef RefKey = SecKeyCreateRandomKey((CFDictionaryRef)keyAttributesRef, (void *)&error);
if (!RefKey) {
NSLog(@"Failed to create ref key. %@", [error localizedDescription] );
return;
}
se64EncodedStringWithOptions:0]);
SecKeyRef SIKAttestationKey = SecKeyCopyAttestationKey(kSecKeyAttestationKeyTypeSIK, (void *)&error);
if (!SIKAttestationKey) {
NSLog(@"Failed to copy SIK attestation key.");
return;
}
NSData* SIKattestation = (__bridge NSData *)SecKeyCreateAttestation(SIKAttestationKey, uikAttestationKey, &cferror);
NSString* b64SIKatt = [SIKattestation base64EncodedStringWithOptions:0];
NSLog(@"SIKAttestation: %@",b64SIKatt);
NSData* UIKAttestation = (__bridge NSData *)SecKeyCreateAttestation(uikPrivateKey, uikAttestationKey, &cferror);
NSString* b64UIKatt = [UIKAttestation base64EncodedStringWithOptions: