Skip to content

Commit

Permalink
2.0: add JIT-less mode
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Apr 18, 2024
1 parent ee06075 commit 8f04376
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 21 deletions.
24 changes: 24 additions & 0 deletions AltStoreCore/ALTAccount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ALTAccount.h
// AltSign
//
// Created by Riley Testut on 5/10/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ALTAccount : NSObject

@property (nonatomic, copy) NSString *appleID;
@property (nonatomic, copy) NSString *identifier;

@property (nonatomic, readonly) NSString *name;
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;

@end

NS_ASSUME_NONNULL_END
33 changes: 33 additions & 0 deletions AltStoreCore/ALTCertificate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// ALTCertificate.h
// AltSign
//
// Created by Riley Testut on 5/10/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ALTCertificate : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *serialNumber;

@property (nonatomic, copy, nullable) NSString *identifier;
@property (nonatomic, copy, nullable) NSString *machineName;
@property (nonatomic, copy, nullable) NSString *machineIdentifier;

@property (nonatomic, copy, nullable) NSData *data;
@property (nonatomic, copy, nullable) NSData *privateKey;

- (nullable instancetype)initWithData:(NSData *)data;
- (nullable instancetype)initWithP12Data:(NSData *)p12Data password:(nullable NSString *)password;

- (nullable NSData *)p12Data;
- (nullable NSData *)encryptedP12DataWithPassword:(NSString *)password;

@end

NS_ASSUME_NONNULL_END
45 changes: 45 additions & 0 deletions AltStoreCore/ALTProvisioningProfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ALTProvisioningProfile.h
// AltSign
//
// Created by Riley Testut on 5/22/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

//#import "ALTCapabilities.h"
#import "ALTCertificate.h"

@class ALTAppID;

NS_ASSUME_NONNULL_BEGIN

@interface ALTProvisioningProfile : NSObject <NSCopying>

@property (copy, nonatomic, readonly) NSString *name;
@property (copy, nonatomic, readonly, nullable) NSString *identifier;
@property (copy, nonatomic, readonly) NSUUID *UUID;

@property (copy, nonatomic, readonly) NSString *bundleIdentifier;
@property (copy, nonatomic, readonly) NSString *teamIdentifier;

@property (copy, nonatomic, readonly) NSDate *creationDate;
@property (copy, nonatomic, readonly) NSDate *expirationDate;

@property (copy, nonatomic, readonly) NSDictionary<NSString *, id> *entitlements;
@property (copy, nonatomic, readonly) NSArray<ALTCertificate *> *certificates;
@property (copy, nonatomic, readonly) NSArray<NSString *> *deviceIDs;

@property (readonly) BOOL isFreeProvisioningProfile;

@property (copy, nonatomic, readonly) NSData *data;

- (nullable instancetype)initWithData:(NSData *)data NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithURL:(NSURL *)fileURL;

- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
38 changes: 38 additions & 0 deletions AltStoreCore/ALTProvisioningProfileWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ALTProvisioningProfile.h
// AltSign
//
// Created by Riley Testut on 5/22/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "ALTProvisioningProfile.h"

NS_ASSUME_NONNULL_BEGIN

@interface ALTProvisioningProfileWrapper : NSObject

@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic, nullable) NSString *identifier;
@property (copy, nonatomic) NSUUID *UUID;

@property (copy, nonatomic) NSString *bundleIdentifier;
@property (copy, nonatomic) NSString *teamIdentifier;

@property (copy, nonatomic) NSDate *creationDate;
@property (copy, nonatomic) NSDate *expirationDate;

@property (copy, nonatomic) NSDictionary<NSString *, id> *entitlements;
@property (copy, nonatomic) NSArray<ALTCertificate *> *certificates;
@property (copy, nonatomic) NSArray<NSString *> *deviceIDs;

@property BOOL isFreeProvisioningProfile;

@property (copy, nonatomic) NSData *data;

- (nullable instancetype)initWithProfile:(ALTProvisioningProfile *)profile;
@end

NS_ASSUME_NONNULL_END
23 changes: 23 additions & 0 deletions AltStoreCore/ALTProvisioningProfileWrapper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import "ALTProvisioningProfileWrapper.h"

@implementation ALTProvisioningProfileWrapper

- (nullable instancetype)initWithProfile:(ALTProvisioningProfile *)profile {
self = [self init];
self.name = profile.name;
self.identifier = profile.identifier;
self.UUID = profile.UUID;
self.name = profile.name;
self.bundleIdentifier = profile.bundleIdentifier;
self.teamIdentifier = profile.teamIdentifier;
self.creationDate = profile.creationDate;
self.expirationDate = profile.expirationDate;
self.entitlements = profile.entitlements;
self.certificates = profile.certificates;
self.deviceIDs = profile.deviceIDs;
self.isFreeProvisioningProfile = profile.isFreeProvisioningProfile;
self.data = profile.data;
return self;
}

@end
30 changes: 30 additions & 0 deletions AltStoreCore/ALTSigner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ALTSigner.h
// AltSign
//
// Created by Riley Testut on 5/22/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "ALTCertificate.h"
#import "ALTProvisioningProfile.h"
#import "ALTTeam.h"

@class ALTAppID;

NS_ASSUME_NONNULL_BEGIN

@interface ALTSigner : NSObject

@property (nonatomic) ALTTeam *team;
@property (nonatomic) ALTCertificate *certificate;

- (instancetype)initWithTeam:(ALTTeam *)team certificate:(ALTCertificate *)certificate;

- (NSProgress *)signAppAtURL:(NSURL *)appURL provisioningProfiles:(NSArray<ALTProvisioningProfile *> *)profiles completionHandler:(void (^)(BOOL success, NSError * error))completionHandler;

@end

NS_ASSUME_NONNULL_END
36 changes: 36 additions & 0 deletions AltStoreCore/ALTTeam.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ALTTeam.h
// AltSign
//
// Created by Riley Testut on 5/10/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "ALTAccount.h"

typedef NS_ENUM(int16_t, ALTTeamType)
{
ALTTeamTypeUnknown = 0,
ALTTeamTypeFree = 1,
ALTTeamTypeIndividual = 2,
ALTTeamTypeOrganization = 3,
};

NS_ASSUME_NONNULL_BEGIN

@interface ALTTeam : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic) ALTTeamType type;

@property (nonatomic) ALTAccount *account;

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithName:(NSString *)name identifier:(NSString *)identifier type:(ALTTeamType)type account:(ALTAccount *)account NS_DESIGNATED_INITIALIZER;

@end

NS_ASSUME_NONNULL_END
4 changes: 2 additions & 2 deletions LCJITLessSetupViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ - (void)loadView {
self.view.backgroundColor = UIColor.systemBackgroundColor;
self.title = @"LiveContainer JIT-less setup";

if (!LCUtils.storeCertPassword) {
if (![LCUtils sidestoreKeychainItem:@"signingCertificate"]) {
[self showDialogTitle:@"Error" message:@"Failed to find certificate password" handler:nil];
return;
}

[LCUtils updateCertPassword];
[LCUtils updateCertificate];
[LCUtils changeMainExecutableTo:@"LiveContainer_PleaseDoNotShortenTheExecutableNameBecauseItIsUsedToReserveSpaceForOverwritingThankYou"];

NSError *error;
Expand Down
60 changes: 55 additions & 5 deletions LCRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ - (void)loadView {
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)]
];

if (!LCUtils.certPassword) {
if (!LCUtils.certificateData) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Setup JIT-less" style:UIBarButtonItemStylePlain target:self action:@selector(setupJITLessTapped)];
}
} /* else {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test JIT-less" style:UIBarButtonItemStylePlain target:self action:@selector(testJITLessTapped)];
} */
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -357,7 +359,7 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocum
return;
}
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self patchExecAtIndexPathIfNeed:indexPath];
[self patchExecAndSignIfNeed:indexPath];
});
}

Expand Down Expand Up @@ -482,10 +484,10 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
self.navigationItem.leftBarButtonItems[0].enabled = YES;
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
[NSUserDefaults.standardUserDefaults setObject:self.objects[indexPath.row] forKey:@"selected"];
[self patchExecAtIndexPathIfNeed:indexPath];
[self patchExecAndSignIfNeed:indexPath];
}

- (void)patchExecAtIndexPathIfNeed:(NSIndexPath *)indexPath {
- (void)patchExecAndSignIfNeed:(NSIndexPath *)indexPath {
NSString *appPath = [NSString stringWithFormat:@"%@/%@", self.bundlePath, self.objects[indexPath.row]];
NSString *infoPath = [NSString stringWithFormat:@"%@/Info.plist", appPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath];
Expand All @@ -506,6 +508,54 @@ - (void)patchExecAtIndexPathIfNeed:(NSIndexPath *)indexPath {
info[@"LCPatchRevision"] = @(currentPatchRev);
[info writeToFile:infoPath atomically:YES];
}

// Sign app if JIT-less is set up
if (LCUtils.certificateData) {
NSUInteger signID = LCUtils.certificateData.hash;
if ([info[@"LCJITLessSignID"] unsignedLongValue] != signID) {
// We need to temporarily change bundle ID to LiveContainer to sign properly
info[@"LCBundleIdentifier"] = info[@"CFBundleIdentifier"];
info[@"CFBundleIdentifier"] = NSBundle.mainBundle.bundleIdentifier;
[info writeToFile:infoPath atomically:YES];
info[@"CFBundleIdentifier"] = info[@"LCBundleIdentifier"];
[info removeObjectForKey:@"LCBundleIdentifier"];

// Don't let main executable get entitlements
NSString *appExecPath = [appPath stringByAppendingPathComponent:info[@"CFBundleExecutable"]];
NSString *tmpExecPath = [appPath stringByAppendingPathComponent:@"LiveContainer.tmp"];
[NSFileManager.defaultManager copyItemAtPath:appExecPath toPath:tmpExecPath error:nil];

__block NSProgress *progress = [LCUtils signAppBundle:appPath

completionHandler:^(BOOL success, NSError *_Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[self showDialogTitle:@"Error while signing app" message:error.localizedDescription];
} else {
info[@"LCJITLessSignID"] = @(signID);
}

// Restore main executable
[NSFileManager.defaultManager removeItemAtPath:appExecPath error:nil];
[NSFileManager.defaultManager moveItemAtPath:tmpExecPath toPath:appExecPath error:nil];

// Save sign ID and restore bundle ID
[info writeToFile:infoPath atomically:YES];

[progress removeObserver:self forKeyPath:@"fractionCompleted"];
[self.progressView removeFromSuperview];
[self.tableView reloadData];
});
}];
if (progress) {
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = @"Signing";
[cell.imageView addSubview:self.progressView];
cell.userInteractionEnabled = NO;
}
}
}
}

- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point {
Expand Down
8 changes: 5 additions & 3 deletions LCUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

@interface LCUtils : NSObject

+ (NSString *)certPassword;
+ (void)updateCertPassword;
+ (NSData *)storeCertPassword;
+ (NSData *)certificateData;
+ (void)updateCertificate;

+ (NSData *)sidestoreKeychainItem:(NSString *)key;
+ (NSProgress *)signAppBundle:(NSString *)path completionHandler:(void (^)(BOOL success, NSError *error))completionHandler;

+ (BOOL)isAppGroupSideStore;
+ (NSError *)changeMainExecutableTo:(NSString *)exec;
Expand Down
Loading

0 comments on commit 8f04376

Please sign in to comment.