Last active
February 9, 2017 17:18
-
-
Save baileysh9/7e06b785779df6495d07 to your computer and use it in GitHub Desktop.
Reading in receipt Prt 1 in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get the Path to the receipt | |
let receiptUrl = Bundle.main.appStoreReceiptURL //Swift2 : NSBundle.mainBundle().appStoreReceiptURL | |
//Check if it's actually there | |
if FileManager.default.fileExists(atPath: receiptUrl!.path) //Swift 2: NSFileManager.defaultManager().fileExistsAtPath(receiptUrl!.path!) | |
{ | |
//Load in the receipt | |
//Swift 2: let receipt: NSData = try! NSData(contentsOfURL:receiptUrl!, options: []) | |
//Swift 3 | |
let receipt: Data = try! Data(contentsOf: receiptUrl!, options: []) | |
let receiptBio = BIO_new(BIO_s_mem()) | |
//Swift 2: BIO_write(receiptBio, receipt.bytes, Int32(receipt.length)) | |
//Swift 3 | |
BIO_write(receiptBio, (receipt as NSData).bytes, Int32(receipt.count)) | |
let receiptPKCS7 = d2i_PKCS7_bio(receiptBio, nil) | |
//Verify receiptPKCS7 is not nil | |
//Read in Apple's Root CA | |
//Swift 2 | |
//let appleRoot = NSBundle.mainBundle().URLForResource("AppleIncRootCertificate", withExtension: "cer") | |
//Swift 3 | |
let appleRoot = Bundle.main.url(forResource: "AppleIncRootCertificate", withExtension: "cer") | |
let caData = try? Data(contentsOf: appleRoot!) //Swift 2: let caData = Data(contentsOfURL: appleRoot!) | |
let caBIO = BIO_new(BIO_s_mem()) | |
BIO_write(caBIO, (caData! as NSData).bytes, Int32(caData!.count)) //Swift 2: BIO_write(caBIO, caData!.bytes, Int32(caData!.length)) | |
let caRootX509 = d2i_X509_bio(caBIO, nil) | |
//Verify the receipt was signed by Apple | |
let caStore = X509_STORE_new() | |
X509_STORE_add_cert(caStore, caRootX509) | |
OpenSSL_add_all_digests() | |
let verifyResult = PKCS7_verify(receiptPKCS7, nil, caStore, nil, nil, 0) | |
if verifyResult != 1 { | |
print("Validation Fails!") | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment