-
Notifications
You must be signed in to change notification settings - Fork 13
/
Signing.m
323 lines (259 loc) · 8.87 KB
/
Signing.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
317
318
319
320
321
322
323
//
// File: Signing.m
// Project: Proc Info
//
// Created by: Patrick Wardle
// Copyright: 2017 Objective-See
// License: Creative Commons Attribution-NonCommercial 4.0 International License
//
#import "Consts.h"
#import "Signing.h"
#import "Utilities.h"
#import <Security/Security.h>
#import <SystemConfiguration/SystemConfiguration.h>
//get the signing info of a item
// pid specified: extract dynamic code signing info
// path specified: generate static code signing info
NSMutableDictionary* extractSigningInfo(pid_t pid, NSString* path, SecCSFlags flags)
{
//info dictionary
NSMutableDictionary* signingInfo = nil;
//status
OSStatus status = !errSecSuccess;
//static code ref
SecStaticCodeRef staticCode = NULL;
//dynamic code ref
SecCodeRef dynamicCode = NULL;
//signing details
CFDictionaryRef signingDetails = NULL;
//signing authorities
NSMutableArray* signingAuths = nil;
//init signing status
signingInfo = [NSMutableDictionary dictionary];
//dynamic code checks
// no path, dynamic check via pid
if(nil == path)
{
//generate dynamic code ref via pid
status = SecCodeCopyGuestWithAttributes(NULL, (__bridge CFDictionaryRef _Nullable)(@{(__bridge NSString *)kSecGuestAttributePid : [NSNumber numberWithInt:pid]}), kSecCSDefaultFlags, &dynamicCode);
if(errSecSuccess != status)
{
//set error
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:status];
//bail
goto bail;
}
//validate code
status = SecCodeCheckValidity(dynamicCode, flags, NULL);
if(errSecSuccess != status)
{
//set error
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:status];
//bail
goto bail;
}
//happily signed
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:errSecSuccess];
//determine signer
// apple, app store, dev id, adhoc, etc...
signingInfo[KEY_SIGNATURE_SIGNER] = extractSigner(dynamicCode, flags, YES);
//extract signing info
status = SecCodeCopySigningInformation(dynamicCode, kSecCSSigningInformation, &signingDetails);
if(errSecSuccess != status)
{
//bail
goto bail;
}
}
//static code checks
else
{
//create static code ref via path
status = SecStaticCodeCreateWithPath((__bridge CFURLRef)([NSURL fileURLWithPath:path]), kSecCSDefaultFlags, &staticCode);
if(errSecSuccess != status)
{
//set error
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:status];
//bail
goto bail;
}
//check signature
status = SecStaticCodeCheckValidity(staticCode, flags, NULL);
if(errSecSuccess != status)
{
//set error
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:status];
//bail
goto bail;
}
//happily signed
signingInfo[KEY_SIGNATURE_STATUS] = [NSNumber numberWithInt:errSecSuccess];
//determine signer
// apple, app store, dev id, adhoc, etc...
signingInfo[KEY_SIGNATURE_SIGNER] = extractSigner(staticCode, flags, NO);
//extract signing info
status = SecCodeCopySigningInformation(staticCode, kSecCSSigningInformation, &signingDetails);
if(errSecSuccess != status)
{
//bail
goto bail;
}
}
//extract code signing id
if(nil != [(__bridge NSDictionary*)signingDetails objectForKey:(__bridge NSString*)kSecCodeInfoIdentifier])
{
//extract/save
signingInfo[KEY_SIGNATURE_IDENTIFIER] = [(__bridge NSDictionary*)signingDetails objectForKey:(__bridge NSString*)kSecCodeInfoIdentifier];
}
//extract entitlements
if(nil != [(__bridge NSDictionary*)signingDetails objectForKey:(__bridge NSString*)kSecCodeInfoEntitlementsDict])
{
//extract/save
signingInfo[KEY_SIGNATURE_ENTITLEMENTS] = [(__bridge NSDictionary*)signingDetails objectForKey:(__bridge NSString*)kSecCodeInfoEntitlementsDict];
}
//extract signing authorities
signingAuths = extractSigningAuths((__bridge NSDictionary *)(signingDetails));
if(0 != signingAuths.count)
{
//save
signingInfo[KEY_SIGNATURE_AUTHORITIES] = signingAuths;
}
bail:
//free signing info
if(NULL != signingDetails)
{
//free
CFRelease(signingDetails);
//unset
signingDetails = NULL;
}
//free dynamic code
if(NULL != dynamicCode)
{
//free
CFRelease(dynamicCode);
//unset
dynamicCode = NULL;
}
//free static code
if(NULL != staticCode)
{
//free
CFRelease(staticCode);
//unset
staticCode = NULL;
}
return signingInfo;
}
//determine who signed item
NSNumber* extractSigner(SecStaticCodeRef code, SecCSFlags flags, BOOL isDynamic)
{
//result
NSNumber* signer = nil;
//"anchor apple"
static SecRequirementRef isApple = nil;
//"anchor apple generic"
static SecRequirementRef isDevID = nil;
//"anchor apple generic and certificate leaf [subject.CN] = \"Apple Mac OS Application Signing\""
static SecRequirementRef isAppStore = nil;
//token
static dispatch_once_t onceToken = 0;
//only once
// init requirements
dispatch_once(&onceToken, ^{
//init apple signing requirement
SecRequirementCreateWithString(CFSTR("anchor apple"), kSecCSDefaultFlags, &isApple);
//init dev id signing requirement
SecRequirementCreateWithString(CFSTR("anchor apple generic"), kSecCSDefaultFlags, &isDevID);
//init app store signing requirement
SecRequirementCreateWithString(CFSTR("anchor apple generic and certificate leaf [subject.CN] = \"Apple Mac OS Application Signing\""), kSecCSDefaultFlags, &isAppStore);
});
//check 1: "is apple" (proper)
if(errSecSuccess == validateRequirement(code, isApple, flags, isDynamic))
{
//set signer to apple
signer = [NSNumber numberWithInt:Apple];
}
//check 2: "is app store"
// note: this is more specific than dev id, so do it first
else if(errSecSuccess == validateRequirement(code, isAppStore, flags, isDynamic))
{
//set signer to app store
signer = [NSNumber numberWithInt:AppStore];
}
//check 3: "is dev id"
else if(errSecSuccess == validateRequirement(code, isDevID, flags, isDynamic))
{
//set signer to dev id
signer = [NSNumber numberWithInt:DevID];
}
//otherwise
// has to be adhoc?
else
{
//set signer to ad hoc
signer = [NSNumber numberWithInt:AdHoc];
}
return signer;
}
//validate a requirement
OSStatus validateRequirement(SecStaticCodeRef code, SecRequirementRef requirement, SecCSFlags flags, BOOL isDynamic)
{
//result
OSStatus result = -1;
//dynamic check?
if(YES == isDynamic)
{
//validate dynamically
result = SecCodeCheckValidity((SecCodeRef)code, flags, requirement);
}
//static check
else
{
//validate statically
result = SecStaticCodeCheckValidity(code, flags, requirement);
}
return result;
}
//extract (names) of signing auths
NSMutableArray* extractSigningAuths(NSDictionary* signingDetails)
{
//signing auths
NSMutableArray* authorities = nil;
//cert chain
NSArray* certificateChain = nil;
//index
NSUInteger index = 0;
//cert
SecCertificateRef certificate = NULL;
//common name on chert
CFStringRef commonName = NULL;
//init array for certificate names
authorities = [NSMutableArray array];
//get cert chain
certificateChain = [signingDetails objectForKey:(__bridge NSString*)kSecCodeInfoCertificates];
if(0 == certificateChain.count)
{
//no certs
goto bail;
}
//extract/save name of all certs
for(index = 0; index < certificateChain.count; index++)
{
//reset
commonName = NULL;
//extract cert
certificate = (__bridge SecCertificateRef)([certificateChain objectAtIndex:index]);
//get common name
if( (errSecSuccess == SecCertificateCopyCommonName(certificate, &commonName)) &&
(NULL != commonName) )
{
//save
[authorities addObject:(__bridge id _Nonnull)(commonName)];
//release
CFRelease(commonName);
}
}
bail:
return authorities;
}