Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a validation step for a given provisioning profile vs a cert. #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions iReSign/iReSign/iReSignAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NSTask *unzipTask;
NSTask *copyTask;
NSTask *provisioningTask;
NSTask *grabCertPubKeyTask;
NSTask *codesignTask;
NSTask *generateEntitlementsTask;
NSTask *verifyTask;
Expand All @@ -30,6 +31,7 @@
NSString *workingPath;
NSString *appName;
NSString *fileName;
NSString *certPubKey;

NSString *entitlementsResult;
NSString *codesigningResult;
Expand Down
90 changes: 89 additions & 1 deletion iReSign/iReSign/iReSignAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ - (void)checkProvisioning:(NSTimer *)timer {
if (identifierOK) {
NSLog(@"Provisioning completed.");
[statusLabel setStringValue:@"Provisioning completed"];
[self doEntitlementsFixing];
[self checkProvisioningSignature];
} else {
[self showAlertOfKind:NSCriticalAlertStyle WithTitle:@"Error" AndMessage:@"Product identifiers don't match"];
[self enableControls];
Expand All @@ -364,6 +364,94 @@ - (void)checkProvisioning:(NSTimer *)timer {
}
}

-(void)checkProvisioningSignature {
grabCertPubKeyTask = [[NSTask alloc] init];
[grabCertPubKeyTask setLaunchPath:@"/usr/bin/security"];
[grabCertPubKeyTask setArguments:[NSArray arrayWithObjects:@"find-certificate", @"-a", @"-c", [certComboBox objectValue], @"-p", nil]];

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(verifyProvisioningSignature:) userInfo:nil repeats:TRUE];

NSPipe *pipe=[NSPipe pipe];
[grabCertPubKeyTask setStandardOutput:pipe];
[grabCertPubKeyTask setStandardError:pipe];
NSFileHandle *handle = [pipe fileHandleForReading];

[grabCertPubKeyTask launch];

[NSThread detachNewThreadSelector:@selector(watchCheckProvisioningSignature:)
toTarget:self withObject:handle];
}

- (void)watchCheckProvisioningSignature:(NSFileHandle*)streamHandle {
@autoreleasepool {
certPubKey = [[NSString alloc] initWithData:[streamHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
}
}

- (void)verifyProvisioningSignature:(NSTimer *)timer {
if ([provisioningTask isRunning] == 0) {
[timer invalidate];
grabCertPubKeyTask = nil;
appPath = nil;

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[workingPath stringByAppendingPathComponent:kPayloadDirName] error:nil];

for (NSString *file in dirContents) {
if ([[[file pathExtension] lowercaseString] isEqualToString:@"app"]) {
appPath = [[workingPath stringByAppendingPathComponent:kPayloadDirName] stringByAppendingPathComponent:file];
if ([[NSFileManager defaultManager] fileExistsAtPath:[appPath stringByAppendingPathComponent:@"embedded.mobileprovision"]]) {

BOOL provisioningSignatureOK = FALSE;

NSString *embeddedProvisioning = [NSString stringWithContentsOfFile:[appPath stringByAppendingPathComponent:@"embedded.mobileprovision"] encoding:NSASCIIStringEncoding error:nil];
NSArray* embeddedProvisioningLines = [embeddedProvisioning componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];

for (int i = 0; i < [embeddedProvisioningLines count]; i++) {
if ([[embeddedProvisioningLines objectAtIndex:i] rangeOfString:@"DeveloperCertificates"].location != NSNotFound) {

i+=2;
while ([[embeddedProvisioningLines objectAtIndex:i] rangeOfString:@"data"].location != NSNotFound) {

NSInteger fromPosition = [[embeddedProvisioningLines objectAtIndex:i] rangeOfString:@"<data>"].location + 6;
NSInteger toPosition = [[embeddedProvisioningLines objectAtIndex:i] rangeOfString:@"</data>"].location;
NSRange range;
range.location = fromPosition;
range.length = toPosition-fromPosition;

NSString *mpPubKey = [[embeddedProvisioningLines objectAtIndex:i] substringWithRange:range];
mpPubKey = [NSString stringWithFormat:@"-----BEGIN CERTIFICATE-----%@-----END CERTIFICATE-----", mpPubKey];
mpPubKey = [mpPubKey stringByReplacingOccurrencesOfString:@"\n" withString:@""];
mpPubKey = [mpPubKey stringByReplacingOccurrencesOfString:@"\t" withString:@""];

certPubKey = [certPubKey stringByReplacingOccurrencesOfString:@"\n" withString:@""];
certPubKey = [certPubKey stringByReplacingOccurrencesOfString:@"\t" withString:@""];

if ([mpPubKey isEqualToString:certPubKey]) {
provisioningSignatureOK = true;
break;
}
i++;
}
break;
}
}

if (provisioningSignatureOK) {
NSLog(@"Provisioning signature validation completed.");
[statusLabel setStringValue:@"Provisioning signature validation completed"];
[self doEntitlementsFixing];
} else {
[self showAlertOfKind:NSCriticalAlertStyle WithTitle:@"Error" AndMessage:@"Provisioning signature validation failed"];
[self enableControls];
[statusLabel setStringValue:@"Ready"];
}
}
}
}
}
}

- (void)doEntitlementsFixing
{
if (![entitlementField.stringValue isEqualToString:@""] || [provisioningPathField.stringValue isEqualToString:@""]) {
Expand Down