Skip to content

Instantly share code, notes, and snippets.

@mhomol
Last active September 17, 2015 13:13
Show Gist options
  • Save mhomol/ad0faa0ece2972bec5ad to your computer and use it in GitHub Desktop.
Save mhomol/ad0faa0ece2972bec5ad to your computer and use it in GitHub Desktop.
Bag Labs Post - In-App Purchase Validation - Local Validation Part 1
//Load in the receipt
var receipt: NSData = NSData(contentsOfURL:receiptUrl!, options: nil, error: nil)!
var receiptBio = BIO_new(BIO_s_mem())
BIO_write(receiptBio, receipt.bytes, Int32(receipt.length))
var receiptPKCS7 = d2i_PKCS7_bio(receiptBio, nil)
//Read in Apple's Root CA
var appleRoot = NSBundle.mainBundle().URLForResource("AppleIncRootCertificate", withExtension: "cer")
var caData = NSData(contentsOfURL: appleRoot!)
var caBIO = BIO_new(BIO_s_mem())
BIO_write(caBIO, caData!.bytes, Int32(caData!.length))
var caRootX509 = d2i_X509_bio(caBIO, nil)
//Verify the receipt was signed by Apple
var caStore = X509_STORE_new()
X509_STORE_add_cert(caStore, caRootX509)
OpenSSL_add_all_digests()
var verifyResult = PKCS7_verify(receiptPKCS7, nil, caStore, nil, nil, 0)
if verifyResult != 1 {
println("Validation Fails!")
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment