Skip to content

Instantly share code, notes, and snippets.

@mhomol
Created September 17, 2015 13:14
Show Gist options
  • Select an option

  • Save mhomol/0982bc7290df91c6e300 to your computer and use it in GitHub Desktop.

Select an option

Save mhomol/0982bc7290df91c6e300 to your computer and use it in GitHub Desktop.
Bag Labs Post - In-App Purchase Validation - Receipt Validation Part 3
func getProductIdFromReceipt(data:NSData) -> String?
{
var p = UnsafePointer<UInt8>(data.bytes)
var dataLength = data.length
var type:Int32 = 0
var tag:Int32 = 0
var length = 0
var end = p + dataLength
ASN1_get_object(&p, &length, &type, &tag, end - p)
if type != V_ASN1_SET {
return nil
}
var integer: UnsafeMutablePointer<ASN1_INTEGER>
while p < end
{
// Expecting an attribute sequence
ASN1_get_object(&p, &length, &type, &tag, end - p)
if type != V_ASN1_SEQUENCE {
println("ASN1 error: expected an attribute sequence")
}
let seq_end = p.advancedBy(length)
var attr_type = 0
var attr_version = 0
// The attribute is an integer
ASN1_get_object(&p, &length, &type, &tag, end - p)
if type != V_ASN1_INTEGER {
println("ASN1 error: attribute not an integer")
return nil
}
integer = c2i_ASN1_INTEGER(nil, &p, length)
attr_type = ASN1_INTEGER_get(integer)
ASN1_INTEGER_free(integer)
// The version is an integer
ASN1_get_object(&p, &length, &type, &tag, end - p)
if type != V_ASN1_INTEGER {
println("ASN1 error: version not an integer")
return nil
}
integer = c2i_ASN1_INTEGER(nil, &p, length);
attr_version = ASN1_INTEGER_get(integer);
ASN1_INTEGER_free(integer);
// The attribute value is an octet string
ASN1_get_object(&p, &length, &type, &tag, end - p)
if type != V_ASN1_OCTET_STRING {
println("ASN1 error: value not an octet string")
return nil
}
//For Product Id
if attr_type == 1702
{
if type == V_ASN1_OCTET_STRING
{
ASN1_get_object(&p, &length, &type, &tag, end - p)
var productId = NSString(bytes: p, length: length, encoding: NSUTF8StringEncoding)
return productId as? String
}
}
p = p.advancedBy(length)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment