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:
| Field | Bytes |
|---|---|
| issuer | its 33-byte compressed point |
| subject | its 33-byte compressed point |
| scope | 0x01 then the room id's UTF-8 for GrantScope.Room; 0x02 alone for GrantScope.AnyRoomOfIssuer |
| issuedAt | 8 bytes, big-endian epoch milliseconds |
| notAfter | 8 bytes, big-endian epoch milliseconds; NO_EXPIRY for none |
| id | ID_SIZE random bytes |
| parent grant id | empty: 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)Functions
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.
isSignedByIssuer, checked in scheme rather than always in KeyScheme.P256. A room's GrantTrust passes the room's own DeviceKeyStore.keyScheme.