Skip to content

Commit

Permalink
- Rebased from Riley/altSign, and other fixes/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mahee96 committed Dec 27, 2024
1 parent d64bec1 commit 9bfe3bf
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 122 deletions.
8 changes: 4 additions & 4 deletions AltSign/Apple API/ALTAppleAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -434,21 +434,21 @@ - (void)updateAppID:(ALTAppID *)appID team:(ALTTeam *)team session:(ALTAppleAPIS
{
parameters[feature] = appID.features[feature];
}
NSMutableDictionary *entitlementss = [appID.entitlements mutableCopy];

NSMutableDictionary *entitlements = [appID.entitlements mutableCopy];

if (team.type == ALTTeamTypeFree) {
for (ALTEntitlement entitlement in appID.entitlements)
{
if (!ALTFreeDeveloperCanUseEntitlement(entitlement))
{
[entitlementss removeObjectForKey:entitlement];
[entitlements removeObjectForKey:entitlement];
}
}
}

// parameters[@"capabilities"] = @[ALTCapabilityIncreasedMemoryLimit, ALTCapabilityIncreasedDebuggingMemoryLimit, ALTCapabilityExtendedVirtualAddressing];
[parameters setObject:entitlementss forKey:@"entitlements"];
[parameters setObject:entitlements forKey:@"entitlements"];
[self sendRequestWithURL:URL additionalParameters:parameters
session:session team:team completionHandler:^(NSDictionary *responseDictionary, NSError *requestError) {
if (responseDictionary == nil)
Expand Down
109 changes: 57 additions & 52 deletions AltSign/Model/Apple API/ALTCertificate.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

#import "ALTCertificate.h"

#if SWIFT_MODULE
@import OpenSSL.pem;
@import OpenSSL.pkcs12;
#else
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#endif

NSString *ALTCertificatePEMPrefix = @"-----BEGIN CERTIFICATE-----";
NSString *ALTCertificatePEMSuffix = @"-----END CERTIFICATE-----";
Expand All @@ -25,16 +30,16 @@ - (instancetype)initWithName:(NSString *)name serialNumber:(NSString *)serialNum
_serialNumber = [serialNumber copy];
_data = [data copy];
}

return self;
}

- (instancetype)initWithResponseDictionary:(NSDictionary *)responseDictionary
{
NSString *identifier = responseDictionary[@"id"];

NSDictionary *attributesDictionary = responseDictionary[@"attributes"] ?: responseDictionary;

NSData *data = nil;
if (attributesDictionary[@"certContent"] != nil)
{
Expand All @@ -45,19 +50,19 @@ - (instancetype)initWithResponseDictionary:(NSDictionary *)responseDictionary
NSString *encodedData = attributesDictionary[@"certificateContent"];
data = [[NSData alloc] initWithBase64EncodedString:encodedData options:0];
}

NSString *machineName = attributesDictionary[@"machineName"];
if ([machineName isKindOfClass:[NSNull class]])
{
machineName = nil;
}

NSString *machineIdentifier = attributesDictionary[@"machineId"];
if ([machineIdentifier isKindOfClass:[NSNull class]])
{
machineIdentifier = nil;
}

if (data != nil)
{
self = [self initWithData:data];
Expand All @@ -66,60 +71,60 @@ - (instancetype)initWithResponseDictionary:(NSDictionary *)responseDictionary
{
NSString *name = attributesDictionary[@"name"];
NSString *serialNumber = attributesDictionary[@"serialNumber"] ?: attributesDictionary[@"serialNum"];

self = [self initWithName:name serialNumber:serialNumber data:nil];
}

if (self)
{
_machineName = [machineName copy];
_machineIdentifier = [machineIdentifier copy];
_identifier = [identifier copy];
}

return self;
}

- (nullable instancetype)initWithP12Data:(NSData *)p12Data password:(nullable NSString *)password
{
BIO *inputP12Buffer = BIO_new_mem_buf((const void *)p12Data.bytes, (int)p12Data.length);
BIO *inputP12Buffer = BIO_new_mem_buf((const void *)p12Data.bytes, (int)p12Data.length);
PKCS12 *inputP12 = d2i_PKCS12_bio(inputP12Buffer, NULL);
BIO_free(inputP12Buffer);

// Extract key + certificate from .p12.
EVP_PKEY *key;
X509 *certificate;
PKCS12_parse(inputP12, password.UTF8String, &key, &certificate, NULL);

if (key == nil || certificate == nil)
{
return nil;
}

BIO *pemBuffer = BIO_new(BIO_s_mem());
PEM_write_bio_X509(pemBuffer, certificate);

BIO *privateKeyBuffer = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(privateKeyBuffer, key, NULL, NULL, 0, NULL, NULL);

char *pemBytes = NULL;
NSUInteger pemSize = BIO_get_mem_data(pemBuffer, &pemBytes);

char *privateKeyBytes = NULL;
NSUInteger privateKeySize = BIO_get_mem_data(privateKeyBuffer, &privateKeyBytes);

NSData *pemData = [NSData dataWithBytes:pemBytes length:pemSize];
NSData *privateKey = [NSData dataWithBytes:privateKeyBytes length:privateKeySize];

self = [self initWithData:pemData];
if (self)
{
_privateKey = [privateKey copy];
}

BIO_free(pemBuffer);
BIO_free(privateKeyBuffer);

return self;
}

Expand All @@ -129,51 +134,51 @@ - (nullable instancetype)initWithData:(NSData *)data
__block X509 *certificate = NULL;
__block BIGNUM *number = NULL;
__block char *cSerialNumber = NULL;

void (^cleanUp)(void) = ^{
if (certificateBuffer)
{
BIO_free(certificateBuffer);
}

if (certificate)
{
X509_free(certificate);
}

if (number)
{
BN_free(number);
}

if (cSerialNumber)
{
free(cSerialNumber);
}
};

NSData *pemData = data;

NSData *prefixData = [data subdataWithRange:NSMakeRange(0, MIN(data.length, ALTCertificatePEMPrefix.length))];
NSString *prefix = [[NSString alloc] initWithData:prefixData encoding:NSUTF8StringEncoding];

if (![prefix isEqualToString:ALTCertificatePEMPrefix])
{
// Convert to proper PEM format before storing.
NSString *base64Data = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSString *content = [NSString stringWithFormat:@"%@\n%@\n%@", ALTCertificatePEMPrefix, base64Data, ALTCertificatePEMSuffix];
pemData = [content dataUsingEncoding:NSUTF8StringEncoding];
}

certificateBuffer = BIO_new_mem_buf((const void *)pemData.bytes, (int)pemData.length);
PEM_read_bio_X509(certificateBuffer, &certificate, 0, 0);
if (certificate == nil)
{
cleanUp();
return nil;
}

/* Certificate Common Name */
X509_NAME *subject = X509_get_subject_name(certificate);
int index = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
Expand All @@ -182,12 +187,12 @@ - (nullable instancetype)initWithData:(NSData *)data
cleanUp();
return nil;
}

X509_NAME_ENTRY *nameEntry = X509_NAME_get_entry(subject, index);
ASN1_STRING *nameData = X509_NAME_ENTRY_get_data(nameEntry);
const unsigned char *cName = ASN1_STRING_get0_data(nameData);


/* Serial Number */
ASN1_INTEGER *serialNumberData = X509_get_serialNumber(certificate);
number = ASN1_INTEGER_to_BN(serialNumberData, NULL);
Expand All @@ -196,18 +201,18 @@ - (nullable instancetype)initWithData:(NSData *)data
cleanUp();
return nil;
}

cSerialNumber = BN_bn2hex(number);

if (cName == nil || cSerialNumber == nil)
{
cleanUp();
return nil;
}

NSString *name = [NSString stringWithFormat:@"%s", cName];
NSString *serialNumber = [NSString stringWithFormat:@"%s", cSerialNumber];

NSInteger location = NSNotFound;
for (int i = 0; i < serialNumber.length; i++)
{
Expand All @@ -217,17 +222,17 @@ - (nullable instancetype)initWithData:(NSData *)data
break;
}
}

cleanUp();

if (location == NSNotFound)
{
return nil;
}

// Remove leading zeros.
NSString *trimmedSerialNumber = [serialNumber substringFromIndex:location];

self = [self initWithName:name serialNumber:trimmedSerialNumber data:pemData];
return self;
}
Expand All @@ -246,7 +251,7 @@ - (BOOL)isEqual:(id)object
{
return NO;
}

BOOL isEqual = [self.serialNumber isEqualToString:certificate.serialNumber];
return isEqual;
}
Expand All @@ -267,36 +272,36 @@ - (nullable NSData *)encryptedP12DataWithPassword:(NSString *)password
{
BIO *certificateBuffer = BIO_new(BIO_s_mem());
BIO *privateKeyBuffer = BIO_new(BIO_s_mem());

BIO_write(certificateBuffer, self.data.bytes, (int)self.data.length);
BIO_write(privateKeyBuffer, self.privateKey.bytes, (int)self.privateKey.length);

X509 *certificate = nil;
PEM_read_bio_X509(certificateBuffer, &certificate, 0, 0);

EVP_PKEY *privateKey = nil;
PEM_read_bio_PrivateKey(privateKeyBuffer, &privateKey, 0, 0);

char emptyString[] = "";
PKCS12 *outputP12 = PKCS12_create((char *)password.UTF8String, emptyString, privateKey, certificate, NULL, 0, 0, 0, 0, 0);

BIO *p12Buffer = BIO_new(BIO_s_mem());
i2d_PKCS12_bio(p12Buffer, outputP12);

char *buffer = NULL;
NSUInteger size = BIO_get_mem_data(p12Buffer, &buffer);

NSData *p12Data = [NSData dataWithBytes:buffer length:size];

BIO_free(p12Buffer);
PKCS12_free(outputP12);

EVP_PKEY_free(privateKey);
X509_free(certificate);

BIO_free(privateKeyBuffer);
BIO_free(certificateBuffer);

return p12Data;
}

Expand Down
4 changes: 4 additions & 0 deletions AltSign/Model/Apple API/ALTCertificateRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

#import "ALTCertificateRequest.h"

#if SWIFT_MODULE
@import OpenSSL.pem;
#else
#include <openssl/pem.h>
#endif

@implementation ALTCertificateRequest

Expand Down
4 changes: 2 additions & 2 deletions AltSign/Signing/ALTSigner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
@import OpenSSL.pem;
@import OpenSSL.pkcs12;
#else
#import <openssl/pkcs12.h>
#import <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/pem.h>
#endif

const char *AppleRootCertificateData = ""
Expand Down
Loading

0 comments on commit 9bfe3bf

Please sign in to comment.