Skip to content

Commit

Permalink
fix: Tolerate clock drift when creating VeraId signatures (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Jun 11, 2024
1 parent c5fa6a9 commit 281a96e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.relaycorp.veraid.pki.MemberIdBundle
import java.security.PrivateKey
import java.time.ZonedDateTime
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

typealias BundleGenerator = (
Expand All @@ -26,6 +27,7 @@ class VeraidSignatureProcessor(
private val bundleGenerator: BundleGenerator = SignatureBundle.Companion::generate,
private val bundleDeserialiser: BundleDeserialiser = SignatureBundle.Companion::deserialise,
) {
private val bundleClockDriftTolerance = 5.minutes.toJavaDuration()
private val bundleTtl = 90.days.toJavaDuration()

@Throws(VeraidSignatureException::class)
Expand All @@ -34,15 +36,15 @@ class VeraidSignatureProcessor(
memberIdBundle: MemberIdBundle,
memberPrivateKey: PrivateKey,
): ByteArray {
val creationDate = ZonedDateTime.now()
val now = ZonedDateTime.now()
val signatureBundle = try {
bundleGenerator(
plaintext,
LetroOids.LETRO_VERAID_OID,
memberIdBundle,
memberPrivateKey,
creationDate.plus(bundleTtl),
creationDate,
now.plus(bundleTtl),
now.minus(bundleClockDriftTolerance),
true,
)
} catch (exc: SignatureException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import tech.relaycorp.veraid.SignatureException
import tech.relaycorp.veraid.pki.MemberIdBundle
import java.time.ZonedDateTime
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

class VeraidSignatureProcessorTest {
private val stubPlaintext = "plaintext".toByteArray()

val clockDriftTolerance = 5.minutes.toJavaDuration()
val ninetyDays = 90.days.toJavaDuration()

@Nested
Expand Down Expand Up @@ -96,6 +98,35 @@ class VeraidSignatureProcessorTest {
}
}

@Test
fun `Signature creation date should be within a few minutes in the past`() {
val generator = mockSignatureBundleGenerator(Result.success(stubSignatureBundle))
val processor = VeraidSignatureProcessor(generator)
val timeBefore = ZonedDateTime.now()

processor.produce(
stubPlaintext,
mockMemberIdBundle,
VERAID_MEMBER_KEY_PAIR.private,
)

val timeAfter = ZonedDateTime.now()
verify {
generator(
any(),
any(),
any(),
any(),
any(),
match {
timeBefore.minus(clockDriftTolerance) <= it &&
it <= timeAfter.minus(clockDriftTolerance)
},
any(),
)
}
}

@Test
fun `Signature should expire in 90 days`() {
val generator = mockSignatureBundleGenerator(Result.success(stubSignatureBundle))
Expand All @@ -118,7 +149,7 @@ class VeraidSignatureProcessorTest {
match {
timeBefore.plus(ninetyDays) <= it && it <= timeAfter.plus(ninetyDays)
},
match { timeBefore <= it && it <= timeAfter },
any(),
any(),
)
}
Expand Down

0 comments on commit 281a96e

Please sign in to comment.