This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event verification (EXPOSUREAPP-5423) (#2524)
* Basic setup * Test verification * Update DefaultQRCodeVerifierTest.kt * Invert boolean to reflect method name * Adjustments to prepare for refactoring of "Verificationkeys" class. * Add more tests and modify encoded event data * Update ConfirmCheckInViewModelTest.kt * Consider warning case * Add test cases for warning * Fix test * Show error * Add todo * lint * Move time warnings into result * Update ConfirmCheckInViewModelTest.kt Co-authored-by: Matthias Urhahn <matthias.urhahn@sap.com>
- Loading branch information
Showing
13 changed files
with
220 additions
and
55 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
.../java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
import de.rki.coronawarnapp.environment.EnvironmentSetup | ||
import de.rki.coronawarnapp.util.security.SignatureValidation | ||
import io.kotest.assertions.throwables.shouldNotThrowAny | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.joda.time.Instant | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import testhelpers.BaseTestInstrumentation | ||
|
||
@RunWith(JUnit4::class) | ||
class DefaultQRCodeVerifierTest : BaseTestInstrumentation() { | ||
|
||
@MockK lateinit var environmentSetup: EnvironmentSetup | ||
private lateinit var qrCodeVerifier: QRCodeVerifier | ||
|
||
@Before | ||
fun setUp() { | ||
MockKAnnotations.init(this) | ||
every { environmentSetup.appConfigVerificationKey } returns PUB_KEY | ||
qrCodeVerifier = DefaultQRCodeVerifier(SignatureValidation(environmentSetup)) | ||
} | ||
|
||
@Test | ||
fun verifyEventSuccess() = runBlockingTest { | ||
val time = 2687960 * 1_000L | ||
val instant = Instant.ofEpochMilli(time) | ||
shouldNotThrowAny { | ||
val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT) | ||
verifyResult.apply { | ||
singedTraceLocation.event.description shouldBe "CWA Launch Party" | ||
verifyResult.isBeforeStartTime(instant) shouldBe false | ||
verifyResult.isAfterEndTime(instant) shouldBe false | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun verifyEventStartTimeWaning() = runBlockingTest { | ||
val time = 2687940 * 1_000L | ||
val instant = Instant.ofEpochMilli(time) | ||
shouldNotThrowAny { | ||
val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT) | ||
verifyResult.apply { | ||
singedTraceLocation.event.description shouldBe "CWA Launch Party" | ||
} | ||
verifyResult.isBeforeStartTime(instant) shouldBe true | ||
verifyResult.isAfterEndTime(instant) shouldBe false | ||
} | ||
} | ||
|
||
@Test | ||
fun verifyEventEndTimeWarning() = runBlockingTest { | ||
val instant = Instant.now() | ||
shouldNotThrowAny { | ||
val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT) | ||
verifyResult.apply { | ||
singedTraceLocation.event.description shouldBe "CWA Launch Party" | ||
} | ||
verifyResult.isBeforeStartTime(instant) shouldBe false | ||
verifyResult.isAfterEndTime(instant) shouldBe true | ||
} | ||
} | ||
|
||
@Test | ||
fun verifyEventWithInvalidKey() = runBlockingTest { | ||
every { environmentSetup.appConfigVerificationKey } returns INVALID_PUB_KEY | ||
shouldThrow<InvalidQRCodeSignatureException> { | ||
qrCodeVerifier.verify(ENCODED_EVENT) | ||
} | ||
} | ||
|
||
@Test | ||
fun eventHasMalformedData() = runBlockingTest { | ||
shouldThrow<InvalidQRCodeDataException> { | ||
qrCodeVerifier.verify(INVALID_ENCODED_EVENT) | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private const val INVALID_PUB_KEY = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEc7DEstcUIRcyk35OYDJ95/hTg" + | ||
"3UVhsaDXKT0zK7NhHPXoyzipEnOp3GyNXDVpaPi3cAfQmxeuFMZAIX2+6A5Xg==" | ||
|
||
private const val PUB_KEY = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEafIKZOiRPuJWjKOUmKv7OTJWTyii" + | ||
"4oCQLcGn3FgYoLQaJIvAM3Pl7anFDPPY/jxfqqrLyGc0f6hWQ9JPR3QjBw==" | ||
|
||
private const val INVALID_ENCODED_EVENT = | ||
"BIPEY33SMVWSA2LQON2W2IDEN5WG64RAONUXIIDBNVSXILBAMNXRBCM4UQARRKM6UQASAHRKCC7CTDWGQ" + | ||
"4JCO7RVZSWVIMQK4UPA.GBCAEIA7TEORBTUA25QHBOCWT26BCA5PORBS2E4FFWMJ3U" + | ||
"U3P6SXOL7SHUBCA7UEZBDDQ2R6VRJH7WBJKVF7GZYJA6YMRN27IPEP7NKGGJSWX3XQ" | ||
|
||
private const val ENCODED_EVENT = | ||
"BIYAUEDBZY6EIWF7QX6JOKSRPAGEB3H7CIIEGV2BEBGGC5LOMNUCAUDBOJ2" + | ||
"HSGGTQ6SACIHXQ6SACKA6CJEDARQCEEAPHGEZ5JI2K2T422L5U3SMZY5DGC" + | ||
"PUZ2RQACAYEJ3HQYMAFFBU2SQCEEAJAUCJSQJ7WDM675MCMOD3L2UL7ECJU" + | ||
"7TYERH23B746RQTABO3CTI=" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="qr_code_input" translatable="false">"HTTPS://CORONAWARN.APP/E1/BIPEY33SMVWSA2LQON2W2IDEN5WG64RAONUXIIDBNVSXILBAMNXRBCM4UQARRKM6UQASAHRKCC7CTDWGQ4JCO7RVZSWVIMQK4UPA.GBCAEIA7TEORBTUA25QHBOCWT26BCA5PORBS2E4FFWMJ3UU3P6SXOL7SHUBCA7UEZBDDQ2R6VRJH7WBJKVF7GZYJA6YMRN27IPEP7NKGGJSWX3XQ"</string> | ||
<string name="qr_code_input" translatable="false">"HTTPS://CORONAWARN.APP/E1/BIYAUEDBZY6EIWF7QX6JOKSRPAGEB3H7CIIEGV2BEBGGC5LOMNUCAUDBOJ2HSGGTQ6SACIHXQ6SACKA6CJEDARQCEEAPHGEZ5JI2K2T422L5U3SMZY5DGCPUZ2RQACAYEJ3HQYMAFFBU2SQCEEAJAUCJSQJ7WDM675MCMOD3L2UL7ECJU7TYERH23B746RQTABO3CTI="</string> | ||
</resources> |
8 changes: 6 additions & 2 deletions
8
...-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/EventRegistrationModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package de.rki.coronawarnapp.eventregistration | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import de.rki.coronawarnapp.eventregistration.checkins.qrcode.DefaultQRCodeVerifier | ||
import de.rki.coronawarnapp.eventregistration.checkins.qrcode.QRCodeVerifier | ||
|
||
@Suppress("EmptyClassBlock") | ||
@Module | ||
class EventRegistrationModule { | ||
// TODO | ||
abstract class EventRegistrationModule { | ||
@Binds | ||
abstract fun qrCodeVerifier(qrCodeVerifier: DefaultQRCodeVerifier): QRCodeVerifier | ||
} |
42 changes: 42 additions & 0 deletions
42
...main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
import de.rki.coronawarnapp.eventregistration.common.decodeBase32 | ||
import de.rki.coronawarnapp.server.protocols.internal.evreg.SignedEventOuterClass | ||
import de.rki.coronawarnapp.util.security.SignatureValidation | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class DefaultQRCodeVerifier @Inject constructor( | ||
private val signatureValidation: SignatureValidation | ||
) : QRCodeVerifier { | ||
|
||
override suspend fun verify(encodedEvent: String): QRCodeVerifyResult { | ||
Timber.tag(TAG).v("Verifying: %s", encodedEvent) | ||
|
||
val signedEvent = try { | ||
SignedEventOuterClass.SignedEvent.parseFrom(encodedEvent.decodeBase32().toByteArray()) | ||
} catch (e: Exception) { | ||
throw InvalidQRCodeDataException(cause = e, message = "QR-code data could not be parsed.") | ||
} | ||
Timber.tag(TAG).d("Parsed to signed event: %s", signedEvent) | ||
|
||
val isValid = try { | ||
signatureValidation.hasValidSignature( | ||
signedEvent.event.toByteArray(), | ||
sequenceOf(signedEvent.signature.toByteArray()) | ||
) | ||
} catch (e: Exception) { | ||
throw InvalidQRCodeDataException(cause = e, message = "Verification failed.") | ||
} | ||
|
||
if (!isValid) { | ||
throw InvalidQRCodeSignatureException(message = "QR-code did not match signature.") | ||
} | ||
|
||
return QRCodeVerifyResult(signedEvent) | ||
} | ||
|
||
companion object { | ||
private const val TAG = "DefaultQRCodeVerifier" | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
...n-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/EventQRCode.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/InvalidQRCodeDataException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
class InvalidQRCodeDataException constructor( | ||
message: String? = null, | ||
cause: Throwable? = null | ||
) : QRCodeException(message, cause) |
6 changes: 6 additions & 0 deletions
6
...de/rki/coronawarnapp/eventregistration/checkins/qrcode/InvalidQRCodeSignatureException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
class InvalidQRCodeSignatureException constructor( | ||
message: String? = null, | ||
cause: Throwable? = null | ||
) : QRCodeException(message, cause) |
6 changes: 6 additions & 0 deletions
6
...p/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
open class QRCodeException constructor( | ||
message: String? = null, | ||
cause: Throwable? = null | ||
) : Exception(message, cause) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...rc/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeVerifyResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.rki.coronawarnapp.eventregistration.checkins.qrcode | ||
|
||
import de.rki.coronawarnapp.server.protocols.internal.evreg.SignedEventOuterClass | ||
import de.rki.coronawarnapp.util.TimeAndDateExtensions.seconds | ||
import org.joda.time.Instant | ||
|
||
data class QRCodeVerifyResult( | ||
val singedTraceLocation: SignedEventOuterClass.SignedEvent | ||
) { | ||
fun isBeforeStartTime(now: Instant): Boolean = | ||
singedTraceLocation.event.start != 0 && singedTraceLocation.event.start > now.seconds | ||
|
||
fun isAfterEndTime(now: Instant): Boolean = | ||
singedTraceLocation.event.end != 0 && singedTraceLocation.event.end < now.seconds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters