Skip to content

Commit

Permalink
Handle StoreKitTest Certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Tikhonenko committed Aug 5, 2020
1 parent 3ad5f84 commit 827fa17
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
dependencies: ["ASN1Swift"],
path: "Sources",
exclude: ["Bundle+Extension.swift"],
resources: [.process("AppleIncRootCertificate.cer")]
resources: [.process("AppleIncRootCertificate.cer"), .process("StoreKitTestCertificate.cer")]
),
.testTarget(
name: "TPInAppReceiptTests",
Expand Down
9 changes: 8 additions & 1 deletion Sources/InAppReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public class InAppReceipt

self.receipt = pkcs7
self.rawData = receiptData
self.rootCertificatePath = rootCertPath ?? Bundle.lookUp(forResource: "AppleIncRootCertificate", ofType: "cer")

#if DEBUG
let certificateName = "StoreKitTestCertificate"
#else
let certificateName = "AppleIncRootCertificate"
#endif

self.rootCertificatePath = rootCertPath ?? Bundle.lookUp(forResource: certificateName, ofType: "cer")
}
}

Expand Down
Binary file added Sources/StoreKitTestCertificate.cer
Binary file not shown.
15 changes: 12 additions & 3 deletions Sources/Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ public extension InAppReceipt
// only check certificate chain of trust and signature validity after these version
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 5.0, *)
{
try checkChainOfTrust()
try checkSignatureValidity()
#if DEBUG
try checkSignatureValidity()
#else
try checkChainOfTrust()
try checkSignatureValidity()
#endif
}
}

Expand Down Expand Up @@ -156,7 +160,7 @@ public extension InAppReceipt
}

// verify iTunes cert in the receipt is signed by worldwide developer cert, which is signed by Apple Root Cert
let iTunesCertVerifystatus = SecTrustCreateWithCertificates([iTunesCertSec, worldwideDevCertSec ,rootCertSec] as AnyObject,
let iTunesCertVerifystatus = SecTrustCreateWithCertificates([iTunesCertSec, worldwideDevCertSec, rootCertSec] as AnyObject,
policy,
&iTunesTrust)

Expand Down Expand Up @@ -199,6 +203,11 @@ public extension InAppReceipt
throw IARError.validationFailed(reason: .signatureValidation(.signatureNotFound))
}

guard let path = rootCertificatePath, let rootCertData = try? Data(contentsOf: URL(fileURLWithPath: path)) else
{
throw IARError.validationFailed(reason: .signatureValidation(.unableToLoadAppleIncRootCertificate))
}

guard let iTunesPublicKeyContainer = receipt.iTunesPublicKeyData else {
throw IARError.validationFailed(reason: .signatureValidation(.unableToLoadiTunesPublicKey))
}
Expand Down

0 comments on commit 827fa17

Please sign in to comment.