Last active
November 17, 2021 12:38
-
-
Save lxcid/4441003 to your computer and use it in GitHub Desktop.
Apple's In-App Purchase transaction receipt decoding in Ruby. http://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKPaymentTransaction_Class/Reference/Reference.html#//apple_ref/occ/instp/SKPaymentTransaction/transactionReceipt
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
# Copyright (c) 2013 Stan Chang Khin Boon (http://lxcid.com/) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# References | |
# - In-App Purchase Receipt Validation on iOS (http://developer.apple.com/library/ios/#releasenotes/StoreKit/IAP_ReceiptValidation/_index.html) | |
# - CargoBay (https://github.com/mattt/CargoBay) | |
require 'test/unit' | |
require 'base64' | |
require 'openssl' | |
require 'osx/plist' | |
class ReceiptDecodingTest < Test::Unit::TestCase | |
def test_receipt_decoding | |
the_root_certificate_pem = <<-CERTIFICATE |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MIHDBggrBgEFBQcCAjCBthqBs1JlbGlhbmNl | |
IG9uIHRoaXMgY2VydGlmaWNhdGUgYnkgYW55IHBhcnR5IGFzc3VtZXMgYWNjZXB0 | |
YW5jZSBvZiB0aGUgdGhlbiBhcHBsaWNhYmxlIHN0YW5kYXJkIHRlcm1zIGFuZCBj | |
b25kaXRpb25zIG9mIHVzZSwgY2VydGlmaWNhdGUgcG9saWN5IGFuZCBjZXJ0aWZp | |
Y2F0aW9uIHByYWN0aWNlIHN0YXRlbWVudHMuMA0GCSqGSIb3DQEBBQUAA4IBAQBc | |
NplMLXi37Yyb3PN3m/J20ncwT8EfhYOFG5k9RzfyqZtAjizUsZAS2L70c5vu0mQP | |
y3lPNNiiPvl4/2vIB+x9OYOLUyDTOMSxv5pPCmv/K/xZpwUJfBdAVhEedNO3iyM7 | |
R6PVbyTi69G3cN8PReEnyvFteO3ntRcXqNx+IjXKJdXZD9Zr1KIkIxH3oayPc4Fg | |
xhtbCS+SsvhESPBgOJ4V9T0mZyCKM2r3DYLP3uujL/lTaltkwGMzd/c6ByxW69oP | |
IQ7aunMZT7XZNn/Bh1XZp5m5MkL72NVxnn6hUrcbvZNCJBIqxw8dtk2cXmPIS4AX | |
UKqK1drk/NAJBzewdXUh | |
-----END CERTIFICATE----- | |
CERTIFICATE | |
the_root_certificate = OpenSSL::X509::Certificate.new(the_root_certificate_pem) | |
assert_not_nil the_root_certificate, 'Root certificate could not be instantiated' | |
the_intermediate_certificate_pem = <<-CERTIFICATE | |
-----BEGIN CERTIFICATE----- | |
MIIECzCCAvOgAwIBAgIBGjANBgkqhkiG9w0BAQUFADBiMQswCQYDVQQGEwJVUzET | |
MBEGA1UEChMKQXBwbGUgSW5jLjEmMCQGA1UECxMdQXBwbGUgQ2VydGlmaWNhdGlv | |
biBBdXRob3JpdHkxFjAUBgNVBAMTDUFwcGxlIFJvb3QgQ0EwHhcNMDkwNTE5MTgz | |
MTMwWhcNMTYwNTE4MTgzMTMwWjB/MQswCQYDVQ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ZHH6TEhPIZ5nC6oPjoowOuDsq3xy/S4tv | |
Grjul2dK2Kuvi6TaXIceILjF87HEmKI3+J7GmmulrfZ4lg6CjwRGHLKl/ZowUSj9 | |
UgQVA9U8rf72eODqNe9ltSF226Tvy3LvVGsBDcfdGg== | |
-----END CERTIFICATE----- | |
CERTIFICATE | |
the_intermediate_certificate = OpenSSL::X509::Certificate.new(the_intermediate_certificate_pem) | |
assert_not_nil the_intermediate_certificate, 'Intermediate certificate could not be instantiated' | |
# Base 64 encoded transaction receipt | |
the_transaction_receipt_encoded = "ewoJInNpZ25hdHVyZSIgPSAiQWtZdVBNRGc1bjl5NDBRL2pXT08vVU5KeUZBbzNjTytvUmpJWklLWXQ3L00wNUV5WHFKTkhKR1BRbm1kYTRaeTBCcUdzejFtMmZwU0pRYXRUMDNWL2IwVGZBcjQrcDhib2ZVUmpDTFk5TlgzNkxDZ1dEandTMVN4UmFvKzRlazcycTUzTWVHVlNrR295NUUyN2pTejVQMmZRZHM4UHZ3UGlkM0R4M081OTQvd0FBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVVrVTNaV0FTMU1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEE1TURZeE5USXlNRFUxTmxvWERURTBNRFl4TkRJeU1EVTFObG93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pXTmxhWEIwUTJWeWRHbG1hV05oZEdVeEd6QVpCZ05WQkFzTUVrRndjR3hsSUdsVWRXNWxjeUJUZEc5eVpURVRNQkVHQTFVRUNnd0tRWEJ3YkdVZ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TSTdDZ2tpY0hWeVkyaGhjMlV0WkdGMFpTSWdQU0FpTWpBeE1pMHhNaTB3TWlBd056b3hOVG8xTkNCRmRHTXZSMDFVSWpzS0NTSnZjbWxuYVc1aGJDMXdkWEpqYUdGelpTMWtZWFJsSWlBOUlDSXlNREV5TFRFeUxUQXlJREEzT2pFMU9qVTBJRVYwWXk5SFRWUWlPd29KSW1KcFpDSWdQU0FpWTI5dExtUXRMV0oxZW5vdVoyRm5MWEJzZFhNdWFXOXpMakF3TVNJN0Nna2ljSFZ5WTJoaGMyVXRaR0YwWlMxd2MzUWlJRDBnSWpJd01USXRNVEl0TURFZ01qTTZNVFU2TlRRZ1FXMWxjbWxqWVM5TWIzTmZRVzVuWld4bGN5STdDZ2tpY1hWaGJuUnBkSGtpSUQwZ0lqRWlPd3A5IjsKCSJlbnZpcm9ubWVudCIgPSAiU2FuZGJveCI7CgkicG9kIiA9ICIxMDAiOwoJInNpZ25pbmctc3RhdHVzIiA9ICIwIjsKfQ==" | |
the_transaction_receipt_decoded = Base64.decode64(the_transaction_receipt_encoded) | |
the_transaction_receipt = OSX::PropertyList.load(the_transaction_receipt_decoded) # Property list in OpenStep format | |
the_purchase_info_encoded = the_transaction_receipt['purchase-info'] | |
the_purchase_info_decoded = Base64.decode64(the_purchase_info_encoded) | |
the_purchase_info = OSX::PropertyList.load(the_purchase_info_decoded) # Property list in OpenStep format | |
# Binary format looks as follows: | |
# | |
# +-----------------+-----------+------------------+-------------+ | |
# | RECEIPT VERSION | SIGNATURE | CERTIFICATE SIZE | CERTIFICATE | | |
# +-----------------+-----------+------------------+-------------+ | |
# | 1 byte | 128 bytes | 4 bytes | | | |
# +-----------------+-----------+------------------+-------------+ | |
# | big endian | | |
# +--------------------------------------------------------------+ | |
# | |
# 1. Extract receipt version, signature and certificate(s). | |
# 2. Check receipt version == 2. | |
# 3. Sanity check that signature is 128 bytes. | |
# 4. Sanity check certification size <= remaining payload data. | |
the_signature_encoded = the_transaction_receipt['signature'] | |
the_signature_decoded = Base64.decode64(the_signature_encoded) | |
the_receipt_version, the_signature, the_certificate_size, the_certificate_der = the_signature_decoded.unpack('CA128L>A*') | |
the_certificate = OpenSSL::X509::Certificate.new(the_certificate_der) | |
assert_equal the_receipt_version, 2, 'The receipt version must be 2' | |
assert_equal the_signature.length, 128, 'The signature must be 128 bytes in size' | |
assert_equal the_certificate_size, the_certificate_der.length, 'The certificate expected and actual size must match' | |
assert_not_nil the_certificate, 'Certificate could not be instantiated' | |
# Make sure that the certificate is issued by Apple. | |
the_store = OpenSSL::X509::Store.new | |
the_store.add_cert(the_root_certificate) | |
the_store.add_cert(the_intermediate_certificate) | |
assert the_store.verify(the_certificate), the_store.error_string | |
# Another way to verify that the certificate is signed/issued by the intermediate certificate | |
assert the_certificate.verify(the_intermediate_certificate.public_key), 'Certificate is not signed/issued by intermediate certificate' | |
# Verify the signature | |
the_public_key = the_certificate.public_key | |
assert the_public_key.verify(OpenSSL::Digest::SHA1.new, the_signature, [the_receipt_version, the_purchase_info_decoded].pack('CA*')), 'Signature verification failed' | |
# The transaction receipt is not compromised and is from Apple. | |
# Accompanied receipt (returned by Apple) | |
the_receipt = { | |
"original_purchase_date_ms"=> "1354432554000", | |
"original_purchase_date_pst"=> "2012-12-01 23=>15=>54 America\/Los_Angeles", | |
"transaction_id"=> "1000000059632385", | |
"quantity"=> "1", | |
"bid"=> "com.d--buzz.gag-plus.ios.001", | |
"original_transaction_id"=> "1000000059632385", | |
"bvrs"=> "7", | |
"expires_date_formatted"=> "2012-12-02 08=>15=>54 Etc\/GMT", | |
"purchase_date"=> "2012-12-02 07=>15=>54 Etc\/GMT", | |
"expires_date"=> "1354436154000", | |
"product_id"=> "com.d__buzz.gag_plus.ios.001.ars.premium.1y", | |
"purchase_date_ms"=> "1354432554000", | |
"expires_date_formatted_pst"=> "2012-12-02 00=>15=>54 America\/Los_Angeles", | |
"purchase_date_pst"=> "2012-12-01 23=>15=>54 America\/Los_Angeles", | |
"original_purchase_date"=> "2012-12-02 07=>15=>54 Etc\/GMT", | |
"item_id"=> "580193937", | |
"web_order_line_item_id"=> "1000000026436290", | |
"unique_identifier"=> "0000b0092818" | |
} | |
assert_equal the_receipt['bid'], the_purchase_info['bid'], 'Mismatch bundle ID' | |
assert_equal the_receipt['product_id'], the_purchase_info['product-id'], 'Mismatch product ID' | |
assert_equal the_receipt['quantity'], the_purchase_info['quantity'], 'Mismatch quantity' | |
assert_equal the_receipt['item-id'], the_purchase_info['item_id'], 'Mismatch item ID' | |
# This is the device unique vendor identifier (iOS 6 and above) | |
assert_equal the_receipt['unique-vendor-identifier'], the_purchase_info['unique_vendor_identifier'], 'Mismatch unique vendor identifier' | |
# This is the device unique identifier (iOS 5 and below) | |
assert_equal the_receipt['unique-identifier'], the_purchase_info['unique_identifier'], 'Mismatch unique identifier' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment