-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReceiptValidatorTest.kt
272 lines (248 loc) · 13.3 KB
/
ReceiptValidatorTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package ch.veehait.devicecheck.appattest.receipt
import ch.veehait.devicecheck.appattest.AppleAppAttest
import ch.veehait.devicecheck.appattest.CertUtils
import ch.veehait.devicecheck.appattest.TestExtensions.copy
import ch.veehait.devicecheck.appattest.TestExtensions.encode
import ch.veehait.devicecheck.appattest.attestation.AttestationValidator
import ch.veehait.devicecheck.appattest.common.App
import ch.veehait.devicecheck.appattest.common.AppleAppAttestEnvironment
import ch.veehait.devicecheck.appattest.util.Extensions.createAppleKeyId
import com.google.common.primitives.Bytes
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldNotThrowAnyUnit
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.throwable.shouldHaveMessage
import io.kotest.property.Exhaustive
import io.kotest.property.checkAll
import io.kotest.property.exhaustive.longs
import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoGeneratorBuilder
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.interfaces.ECPublicKey
import java.time.Clock
import java.time.Duration
import java.time.ZoneOffset
import kotlin.experimental.xor
class ReceiptValidatorTest : FreeSpec() {
init {
"Accepts valid receipt samples" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
"${bundle.sample.properties.type}/${bundle.sample.id}" {
val sample = bundle.sample
val validatedReceipt = bundle.validator.validateReceipt(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
with(validatedReceipt.payload) {
appId.value shouldBe bundle.appleAppAttest.app.appIdentifier
attestationCertificate.value.publicKey shouldBe sample.publicKey
attestationCertificate.value.publicKey.createAppleKeyId() shouldBe sample.keyId
clientHash.value shouldBe sample.properties.clientHash
creationTime.value shouldBe sample.properties.creationTime
environment?.value shouldBe sample.properties.environment
expirationTime.value shouldBe sample.properties.expirationTime
expirationTime.value shouldBe creationTime.value.plus(Duration.ofDays(90))
if (notBefore != null) {
notBefore!!.value shouldBe sample.properties.notBefore
notBefore!!.value shouldBe creationTime.value.plus(Duration.ofDays(1))
}
riskMetric?.value shouldBe sample.properties.riskMetric
token.value shouldBe sample.properties.token
type.value shouldBe sample.properties.type
}
}
}
}
"Accepts resigned valid receipt samples" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val fakeEnvironmentValue = "wurzelpfropf"
"environment=$fakeEnvironmentValue ${bundle.sample.properties.type}/${bundle.sample.id}" {
val sample = bundle.sample
val validatedReceipt = bundle.validator.validateReceipt(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
val fakeReceiptBundle = CertUtils.resignReceipt(
validatedReceipt,
payloadMutator = {
val fakeEnvironment = it.environment!!.copy(fakeEnvironmentValue)
it.copy(environment = fakeEnvironment)
},
)
val fakeReceiptValidator = bundle.appleAppAttest.createReceiptValidator(
trustAnchor = fakeReceiptBundle.trustAnchor,
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
)
val fakeReceipt = shouldNotThrowAny {
fakeReceiptValidator.validateReceipt(
receiptP7 = fakeReceiptBundle.receipt.p7,
publicKey = fakeReceiptBundle.leafCertificate.publicKey as ECPublicKey,
)
}
fakeReceipt.payload.environment?.value shouldBe fakeEnvironmentValue
}
}
}
"Throws InvalidPayload for too old receipt" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val sample = bundle.sample
"${sample.properties.type}/${sample.id}" - {
checkAll(Exhaustive.longs(-1L..1L)) { nanosOffset ->
val age = ReceiptValidator.APPLE_RECOMMENDED_MAX_AGE.plusNanos(nanosOffset)
val receiptValidator = bundle.appleAppAttest.createReceiptValidator(
clock = Clock.fixed(bundle.sample.properties.creationTime.plus(age), ZoneOffset.UTC),
)
if (nanosOffset <= 0L) {
"Accepted for $age" {
shouldNotThrowAnyUnit {
receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
}
}
} else {
"Rejected for $age" {
val exception = shouldThrow<ReceiptException.InvalidPayload> {
receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
}
val expectedTime = sample.properties.creationTime.plusNanos(nanosOffset)
exception.shouldHaveMessage("Receipt's creation time is after $expectedTime")
}
}
}
}
}
}
"Throws InvalidPayload for wrong app identifier" - {
ReceiptSample.all.forEach { sample ->
val app = App("wurzelpfro", "pf")
"appId=${app.appIdentifier} ${sample.properties.type}/${sample.id}" - {
val receiptValidator = AppleAppAttest(
app = app,
appleAppAttestEnvironment = AppleAppAttestEnvironment.DEVELOPMENT,
).createReceiptValidator(
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
)
val exception = shouldThrow<ReceiptException.InvalidPayload> {
receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
}
val unexcptedAppId = App(sample.teamIdentifier, sample.bundleIdentifier).appIdentifier
exception shouldHaveMessage "Unexpected App ID: $unexcptedAppId"
}
}
}
"Throws InvalidPayload for wrong public key" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val sample = bundle.sample
"${sample.properties.type}/${sample.id}" - {
val receiptValidator = bundle.appleAppAttest.createReceiptValidator(
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
)
val exception = shouldThrow<ReceiptException.InvalidPayload> {
receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = CertUtils.generateP256KeyPair().public as ECPublicKey,
)
}
exception shouldHaveMessage "Public key from receipt and attestation statement do not match"
}
}
}
"Throws InvalidCertificateChain for wrong root CA" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val sample = bundle.sample
"${sample.properties.type}/${sample.id}" - {
val receiptValidator = bundle.appleAppAttest.createReceiptValidator(
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
// Wrong trust anchor
trustAnchor = AttestationValidator.APPLE_APP_ATTEST_ROOT_CA_BUILTIN_TRUST_ANCHOR,
)
val exception = shouldThrow<ReceiptException.InvalidCertificateChain> {
receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
}
exception shouldHaveMessage "The receipt object does not contain a valid certificate chain"
}
}
}
"Throws InvalidSignature for invalid signature" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val sample = bundle.sample
"${sample.properties.type}/${sample.id}" - {
// Test setup
val receiptValidator = bundle.appleAppAttest.createReceiptValidator(
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
)
val receipt = receiptValidator.validateReceiptAsync(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
// Actual test
// Search for the position of the "client hash" field and negate the last byte of the value
// to invalidate the signature for this payload
val clientHashAsn1 = receipt.payload.clientHash.encode().encoded
val clientHashAsn1StartIndex = Bytes.indexOf(receipt.p7, clientHashAsn1)
val receiptP7InvalidSignature = receipt.p7.let {
val i = clientHashAsn1StartIndex + clientHashAsn1.size - 1
it[i] = it[i].xor(1)
it
}
val exception = shouldThrow<ReceiptException.InvalidSignature> {
receiptValidator.validateReceiptAsync(
receiptP7 = receiptP7InvalidSignature,
publicKey = sample.publicKey,
)
}
exception shouldHaveMessage "The receipt signature is invalid"
}
}
}
"Throws InvalidSignature for multiple signers" - {
ReceiptSample.all.map(::ReceiptSampleBundle).forEach { bundle ->
val sample = bundle.sample
"${sample.properties.type}/${sample.id}" - {
val validatedReceipt = bundle.validator.validateReceipt(
receiptP7 = sample.receipt,
publicKey = sample.publicKey,
)
val fakeReceiptBundle = CertUtils.resignReceipt(
validatedReceipt,
generatorMutator = {
val keyPair = CertUtils.generateP256KeyPair()
val certBundle = CertUtils.createCertificate(
certTemplate = ReceiptValidator
.APPLE_PUBLIC_ROOT_CA_G3_BUILTIN_TRUST_ANCHOR.trustedCert,
)
it.addSignerInfoGenerator(
JcaSimpleSignerInfoGeneratorBuilder()
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build("SHA256withECDSA", keyPair.private, certBundle.certificate),
)
},
)
val fakeReceiptValidator = bundle.appleAppAttest.createReceiptValidator(
trustAnchor = fakeReceiptBundle.trustAnchor,
clock = Clock.fixed(sample.timestamp, ZoneOffset.UTC),
)
val exception = shouldThrow<ReceiptException.InvalidSignature> {
fakeReceiptValidator.validateReceipt(
receiptP7 = fakeReceiptBundle.receipt.p7,
publicKey = fakeReceiptBundle.leafCertificate.publicKey as ECPublicKey,
)
}
exception shouldHaveMessage "The receipt contains more than one signature"
}
}
}
}
}