From a374350f27f5c41b318017650e0f515e6281a2ca Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Mon, 5 Aug 2024 20:16:10 +0700 Subject: [PATCH] Fix: handle both cert cases --- LCJITLessSetupViewController.m | 10 ++++++++-- LCUtils.h | 1 + LCUtils.m | 32 +++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/LCJITLessSetupViewController.m b/LCJITLessSetupViewController.m index 3144378..fd840ef 100644 --- a/LCJITLessSetupViewController.m +++ b/LCJITLessSetupViewController.m @@ -23,17 +23,23 @@ - (void)loadView { return; } - NSData *certPassword = [LCUtils keychainItem:@"signingCertificatePassword" ofStore:@"com.SideStore.SideStore"]; /* TODO: support AltStore if (!certData) { certData = [LCUtils keychainItem:@"signingCertificate" ofStore:@"com.rileytestut.AltStore"]; } */ + NSData *certData = [LCUtils keychainItem:@"signingCertificate" ofStore:@"com.SideStore.SideStore"]; + if (!certData) { + [self showDialogTitle:@"Error" message:@"Failed to find certificate data" handler:nil]; + return; + } + LCUtils.certificateData = certData; + + NSData *certPassword = [LCUtils keychainItem:@"signingCertificatePassword" ofStore:@"com.SideStore.SideStore"]; if (!certPassword) { [self showDialogTitle:@"Error" message:@"Failed to find certificate password" handler:nil]; return; } - LCUtils.certificatePassword = [NSString stringWithUTF8String:certPassword.bytes]; NSError *error; diff --git a/LCUtils.h b/LCUtils.h index e006f93..8e7740a 100644 --- a/LCUtils.h +++ b/LCUtils.h @@ -10,6 +10,7 @@ + (NSData *)certificateData; + (NSString *)certificatePassword; ++ (void)setCertificateData:(NSData *)data; + (void)setCertificatePassword:(NSString *)password; + (NSData *)keychainItem:(NSString *)key ofStore:(NSString *)store; diff --git a/LCUtils.m b/LCUtils.m index 06e7798..6b78bfc 100644 --- a/LCUtils.m +++ b/LCUtils.m @@ -9,6 +9,10 @@ @implementation LCUtils #pragma mark Certificate password ++ (NSString *)appGroupPath { + return [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:self.appGroupID].path; +} + + (NSData *)keychainItem:(NSString *)key ofStore:(NSString *)store { NSDictionary *dict = @{ (id)kSecClass: (id)kSecClassGenericPassword, @@ -27,22 +31,28 @@ + (NSData *)keychainItem:(NSString *)key ofStore:(NSString *)store { } } -+ (void)setCertificatePassword:(NSString *)certPassword { - [NSUserDefaults.standardUserDefaults setObject:certPassword forKey:@"LCCertificatePassword"]; ++ (void)setCertificateData:(NSData *)certData { + [NSUserDefaults.standardUserDefaults setObject:certData forKey:@"LCCertificateData"]; } -+ (NSString *)certificatePassword { - return [NSUserDefaults.standardUserDefaults objectForKey:@"LCCertificatePassword"]; ++ (NSData *)certificateDataFile { + NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:self.appGroupID]; + NSURL *url = [appGroupPath URLByAppendingPathComponent:@"Apps/com.SideStore.SideStore/App.app/ALTCertificate.p12"]; + return [NSData dataWithContentsOfURL:url]; } + (NSData *)certificateData { - static NSData *result; - if (!result) { - NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:self.appGroupID]; - NSURL *url = [appGroupPath URLByAppendingPathComponent:@"Apps/com.SideStore.SideStore/App.app/ALTCertificate.p12"]; - result = [NSData dataWithContentsOfURL:url]; - } - return result; + // Prefer certificate file over keychain data + return self.certificateDataFile ?: [NSUserDefaults.standardUserDefaults objectForKey:@"LCCertificateData"]; +} + ++ (void)setCertificatePassword:(NSString *)certPassword { + [NSUserDefaults.standardUserDefaults setObject:certPassword forKey:@"LCCertificatePassword"]; +} + ++ (NSString *)certificatePassword { + // Certificate file requires password, whereas data doesn't + return self.certificateDataFile ? [NSUserDefaults.standardUserDefaults objectForKey:@"LCCertificatePassword"] : @""; } + (void)removeCodeSignatureFromBundleURL:(NSURL *)appURL {