我正在我的应用程序中使用应用内购买...现在我想验证收据,并且为了验证收据签名,我在AppDelegate中使用以下代码(以了解用户是否在推出应用程序时已经购买了它)
实际上,代码来自WWDC 2013 - 会话308; - )
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
NSData *certificateData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"AppleIncRootCertificate" withExtension:@"cer"]];
/* The PKCS #7 container (the receipt) and The Apple root certificate. */
BIO *b_receipt = BIO_new_mem_buf((void *) [receiptData bytes], (int)[receiptData length]);
BIO *b_x509 = BIO_new_mem_buf((void *)[certificateData bytes], (int)[certificateData length]);
// Convert receipt data to PKCS #7 Representation
PKCS7 *p7 = d2i_PKCS7_bio(b_receipt, NULL);
/* Create the root certificate */
X509_STORE *store = X509_STORE_new();
X509 *appleRootCA = d2i_X509_bio(b_x509, NULL);
X509_STORE_add_cert(store, appleRootCA);
/* Verify the signature. If the verification is correct, b_receiptPayload will contain the PKCS #7 payload and result will be 1. */
BIO *b_receiptPayload = BIO_new(BIO_s_mem());
int result = PKCS7_verify(p7, NULL, store, NULL, b_receiptPayload, 0);
if (result == 1)
{
NSLog(@"Receipt Signature is valid");
} else {
unsigned long error = ERR_get_error();
const char* error_str = ERR_error_string(error, NULL);
NSLog(@"OpenSSL Error: %s",error_str);
}
但我总是得到以下OpenSSL错误:错误:2006F079:lib(32):func(111):reason(121)
我已经谷歌了但没有任何结果......有任何建议吗?
验证收据签名的另一种方法是什么?
提前感谢您的合作......
答案 0 :(得分:0)
<强>解决强>
这里有两件事:
1)我忘记将“AppleIncRootCertificate.cer”添加到我的应用程序的资源包中,这是我的项目,显然这是强制性的。
2)以下代码行也是必需的:
OpenSSL_add_all_digests();
(或OpenSSL_add_all_algorithms();相反?)
并且必须放在
之前int result = PKCS7_verify(p7, NULL, store, NULL, b_receiptPayload, 0);
现在正在运作......
PS:顺便说一句,在终端中使用下面的代码行是个好主意(为了知道错误引用的内容):
openssl errstr "errornumber" (i.e.openssl errstr 2006F079)