验证来自 iOS transactionReceipt 的签名

Verify signature from iOS transactionReceipt

我正在尝试验证已弃用的 iOS transactionReceipt;我一直在验证签名。

我已经对收据进行了 base64 解码并解析了初始 plist,结果为:

payload = {
  "signature"=>"AnTJSzQAjehWYmnqlofOYVqrXJ51UNZr9//2HXq3MB9i2aPjVilv38ixmZoO/9YfPlRHYDusXT2IpYbDs4pFZNw/mQL1TzkIIetYea4OyjuV5KluEB4LKVol7nmHfd27HI6PM6jBDZKLmpktmNVCmfnheT+jlMjLx7eZKjHSFhlRAAADVzCCA1MwggI7oAMCAQICCBup4+PAhm/LMA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSYwJAYDVQQLDB1BcHBsZSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEzMDEGA1UEAwwqQXBwbGUgaVR1bmVzIFN0b3JlIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTE0MDYwNzAwMDIyMVoXDTE2MDUxODE4MzEzMFowZDEjMCEGA1UEAwwaUHVyY2hhc2VSZWNlaXB0Q2VydGlmaWNhdGUxGzAZBgNVBAsMEkFwcGxlIGlUdW5lcyBTdG9yZTETMBEGA1UECgwKQXBwbGUgSW5jLjELMAkGA1UEBhMCVVMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMmTEuLgjimLwRJxy1oEf0esUNDVEIe6wDsnnal14hNBt1v195X6n93YO7gi3orPSux9D554SkMp+Sayg84lTc362UtmYLpWnb34nqyGx9KBVTy5OGV4ljE1OwC+oTnRM+QLRCmeNxMbPZhS47T+eZtDEhVB9usk3+JM2Cogfwo7AgMBAAGjcjBwMB0GA1UdDgQWBBSJaEeNuq9Df6ZfN68Fe+I2u22ssDAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFDYd6OKdgtIBGLUyaw7XQwuRWEM6MA4GA1UdDwEB/wQEAwIHgDAQBgoqhkiG92NkBgUBBAIFADANBgk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S1mb3JtYXR0ZWQtcHN0IiA9ICIyMDE1LTExLTIzIDIyOjM0OjQxIEFtZXJpY2EvTG9zX0FuZ2VsZXMiOwoJIml0ZW0taWQiID0gIjEwMjg5NTA3OTciOwoJImV4cGlyZXMtZGF0ZS1mb3JtYXR0ZWQiID0gIjIwMTUtMTEtMjQgMDY6MzQ6NDEgRXRjL0dNVCI7CgkicHJvZHVjdC1pZCIgPSAieWVhcmx5IjsKCSJwdXJjaGFzZS1kYXRlIiA9ICIyMDE1LTExLTE4IDA0OjM0OjQxIEV0Yy9HTVQiOwoJIm9yaWdpbmFsLXB1cmNoYXNlLWRhdGUiID0gIjIwMTUtMTEtMTcgMTY6MzQ6NDEgRXRjL0dNVCI7CgkiYmlkIiA9ICJjb20ubWJhYXN5Lmlvcy5kZW1vIjsKCSJwdXJjaGFzZS1kYXRlLXBzdCIgPSAiMjAxNS0xMS0xNyAyMDozNDo0MSBBbWVyaWNhL0xvc19BbmdlbGVzIjsKCSJxdWFudGl0eSIgPSAiMSI7Cn0=",
  "environment"=>"Sandbox",
  "pod"=>"100",
  "signing-status"=>"0"
}

所以现在我想验证签名,这是我目前拥有的:

version, sig, cert_length, cert = payload.fetch('signature')
                                  .unpack('m').first
                                  .unpack('c a128 N a*')
return false unless
  version == 2 &&
  sig.size == 128 &&
  cert.size == cert_length

cert = OpenSSL::X509::Certificate.new cert

digest = OpenSSL::Digest::SHA1.new
digest << version.to_s
digest << payload.fetch('purchase-info').unpack('m').first

cert.public_key.verify OpenSSL::Digest::SHA1.new, sig, digest.digest

cert.public_key.verify 的结果总是错误的,这不是我想要的,因为我使用的是真实收据。

我正在使用 this Quora post 作为指导,它让我深入了解了如何解压签名,并且我已经成功地从 blob 中提取了所有部分。

也许编码是个问题? sigdata 都是 ASCII-8BITversion 是一个整数。

非常感谢你在这方面的帮助。

特此根据https://gist.github.com/lxcid/4441003

回答
message = [version, payload.fetch('purchase-info').unpack('m').first].pack('CA*') 

cert.public_key.verify OpenSSL::Digest::SHA1.new, sig, message