10

我正在尝试使用 Xcode 12 和 StoreKit 配置文件验证收据,但不断收到 21002 错误。根据文档,我需要使用不同的证书。我生成了证书,但不清楚如何处理它?

#if DEBUG
        let certificate = “StoreKitTestCertificate” 
#else
        let certificate = “AppleIncRootCertificate” 
#endif

这很好,但有什么用certificate

4

2 回答 2

2

以下代码改编自 Ray Wenderlich的 Receipt Validation 教程

笔记; 您需要首先将 OpenSSL 静态链接到您的项目中,然后完全按照教程进行操作。这只是为了显示使用 Apple 代码示例的上下文。

  private func validateSigning(_ receipt: UnsafeMutablePointer<PKCS7>?) -> Bool {
    #if DEBUG
    let certificateName = "StoreKitTestCertificate"
    #else
    let certificateName = "AppleIncRootCertificate"
    #endif
    
    guard let rootCertURL = Bundle.main.url(forResource: certificateName, withExtension: "cer"),
          let rootCertData = try? Data(contentsOf: rootCertURL) else {
      receiptStatus = .invalidAppleRootCertificate
      return false
    }
    let rootCertBio = BIO_new(BIO_s_mem())
    let rootCertBytes: [UInt8] = .init(rootCertData)
    BIO_write(rootCertBio, rootCertBytes, Int32(rootCertData.count))
    let rootCertX509 = d2i_X509_bio(rootCertBio, nil)
    BIO_free(rootCertBio)
    let store = X509_STORE_new()
    X509_STORE_add_cert(store, rootCertX509)
    
    OPENSSL_init_crypto(UInt64(OPENSSL_INIT_ADD_ALL_DIGESTS), nil)
    
    #if DEBUG
    let verificationResult = PKCS7_verify(receipt, nil, store, nil, nil, PKCS7_NOCHAIN)
    #else
    let verificationResult = PKCS7_verify(receipt, nil, store, nil, nil, nil)
    #endif
    
    guard verificationResult == 1 else {
      receiptStatus = .failedAppleSignature
      return false
    }
    return true
  }
于 2021-01-15T05:07:35.350 回答
1

您需要指定 StoreKitTestCertificate.crt 以通过 OpenSSL 进行本地收据验证。您无法通过 Apple 的 /verifyReceipt 端点验证 xcode 生成的收据。

于 2020-10-28T10:35:19.257 回答