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

Validate receipt using Apple Certificate data #198

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
6 changes: 6 additions & 0 deletions RMStore/Optional/RMAppReceipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ __attribute__((availability(ios,introduced=7.0)))
*/
+ (void)setAppleRootCertificateURL:(NSURL*)url;

/**
Sets the data of the Apple Root certificate that will be used to verifiy the signature of the bundle receipt. This takes precedence over AppleRootCertificateURL if both are set. If none is provided, AppleRootCertificateURL will be used. If no certificate is available, no signature verification will be performed.
@param data containing the Apple Root certificate.
*/
+ (void)setAppleRootCertificateData:(NSData*)data;

@end

/** Represents an in-app purchase in the app receipt.
Expand Down
16 changes: 14 additions & 2 deletions RMStore/Optional/RMAppReceipt.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ static int RMASN1ReadInteger(const uint8_t **pp, long omax)

static NSURL *_appleRootCertificateURL = nil;

static NSData *_appleRootCertificateData = nil;

@implementation RMAppReceipt

- (instancetype)initWithASN1Data:(NSData*)asn1Data
Expand Down Expand Up @@ -215,6 +217,11 @@ + (void)setAppleRootCertificateURL:(NSURL*)url
_appleRootCertificateURL = url;
}

+ (void)setAppleRootCertificateData:(NSData*)data
{
_appleRootCertificateData = data;
}

#pragma mark - Utils

+ (NSData*)dataFromPCKS7Path:(NSString*)path
Expand All @@ -229,8 +236,13 @@ + (NSData*)dataFromPCKS7Path:(NSString*)path
if (!p7) return nil;

NSData *data;
NSURL *certificateURL = _appleRootCertificateURL ? : [[NSBundle mainBundle] URLForResource:@"AppleIncRootCertificate" withExtension:@"cer"];
NSData *certificateData = [NSData dataWithContentsOfURL:certificateURL];
NSData *certificateData = _appleRootCertificateData;

if( certificateData.length == 0 ) {
NSURL *certificateURL = _appleRootCertificateURL ? : [[NSBundle mainBundle] URLForResource:@"AppleIncRootCertificate" withExtension:@"cer"];
certificateData = [NSData dataWithContentsOfURL:certificateURL];
}

if (!certificateData || [self verifyPCKS7:p7 withCertificateData:certificateData])
{
struct pkcs7_st *contents = p7->d.sign->contents;
Expand Down