Grant

class Grant

A host's signed pass that lets one device back in without being vetted again.

When a host admits a device, it can hand the device a grant: "the device with key subject may come back to scope until notAfter", signed with the host's own key, issuer. The device keeps it, and presents it when it returns. A grant is not a secret: it names the device's key, so it is useless to anyone who cannot sign with that key. Nor is it an invite: its subject is a key the host has already seen, so only a device that was admitted once can hold one.

This type is the statement and its signature, and nothing more. Deciding whether a grant is good here, now, for this device — the right issuer, subject and room, not expired, not revoked — is the checker's job, not this type's: isSignedByIssuer checks the signature alone.

The wire form is the grant. wire is the signed layout followed by the 64-byte signature, and it is what a device stores and sends. Two grants are equal exactly when their wire bytes are.

One set of terms can carry more than one valid signature. ECDSA signatures are malleable: if (r, s) verifies, so does (r, n − s), and nothing here insists on one of the two. So two grants with the same id and the same terms can both verify and still be unequal. To deduplicate grants, key on id and issuer, never on ==.

The layout, after the kuilt/grant tag and version 1:

FieldBytes
issuerits 33-byte compressed point
subjectits 33-byte compressed point
scope0x01 then the room id's UTF-8 for GrantScope.Room; 0x02 alone for GrantScope.AnyRoomOfIssuer
issuedAt8 bytes, big-endian epoch milliseconds
notAfter8 bytes, big-endian epoch milliseconds; NO_EXPIRY for none
idID_SIZE random bytes
parent grant idempty: reserved for a grant issued on the strength of another

Times are the issuer's to supply; nothing here reads a clock. Every grant satisfies notAfter > issuedAt.

Samples

val host = DeviceKey.generate() // the host's own key
val phone = DeviceKey.generate() // the device being let in
val now = 1_700_000_000_000L // epoch ms, from your clock: a grant never reads one

// On admitting the phone, the host signs it a pass back into this room for the next hour.
val grant = Grant.issue(host, phone.id, GrantScope.Room("table-7"), issuedAt = now, notAfter = now + 3_600_000)
val kept: ByteArray = grant.wire // what the phone stores, and presents when it returns

// When the phone comes back, the host reads the pass and checks it signed exactly these terms.
val presented = checkNotNull(Grant.decode(kept))
check(presented.isSignedByIssuer() && presented.issuer == host.id)
check(presented.subject == phone.id && presented.scope == GrantScope.Room("table-7"))
check(now + 60_000 < presented.notAfter) // a minute later, still good

// Damage it — here the last byte of the signature — and it is no longer the host's pass.
val edited = kept.copyOf().also { it[it.size - 1] = (it[it.size - 1] + 1).toByte() }
check(Grant.decode(edited)?.isSignedByIssuer() == false)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

A copy of this grant's ID_SIZE-byte random id.

Link copied to clipboard

When the issuer issued it, in epoch milliseconds.

Link copied to clipboard

The key that signed this grant: the host that issued it.

Link copied to clipboard

The first epoch millisecond it is no longer good, or NO_EXPIRY. Always after issuedAt.

Link copied to clipboard

Where it is good.

Link copied to clipboard

The key of the device this grant lets back in.

Link copied to clipboard

A copy of the wire form: the signed layout, then the 64-byte signature. decode reads it back.

Functions

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
suspend fun isSignedByIssuer(): Boolean

Whether the signature on this grant is issuer's, over exactly these terms. A grant whose terms — any byte of the signed layout: its subject, its scope, its expiry — changed after signing answers false. The signature bytes themselves are another matter: see the note on malleability above.

suspend fun isSignedByIssuer(scheme: KeyScheme): Boolean

isSignedByIssuer, checked in scheme rather than always in KeyScheme.P256. A room's GrantTrust passes the room's own DeviceKeyStore.keyScheme.

Link copied to clipboard
open override fun toString(): String