Skip to content
← All posts
· Delta1 Labs KeyrightLicensing

When an old license is still valid

A downgraded or renewed license leaves the previous one cryptographically intact, so signature-plus-expiry checks still accept it. Keyright adds a monotonic epoch and a tamper-evident high-water mark so a client rejects any license older than the newest it has ever trusted — without touching the clock.

A customer on your Pro plan with ten seats decides to downgrade to Basic with two. You issue a new license, the billing webhook fires, everyone moves on. Months later you notice that account is still running ten seats. Nothing is broken, no key was cracked, no signature was forged. The old Pro license is simply still sitting in the client’s cache — and it still verifies.

This is the uncomfortable truth about signed licenses: a signature proves a payload is authentic, not that it is current. The Pro license you signed is a genuine artifact you created. Its expiry is months out. Every check an ordinary SDK runs — signature valid, not expired, audience matches — passes. The client has every incentive to keep presenting it and no obligation to fetch the replacement.

The real question

Expiry does not help here, because both licenses are unexpired. Revocation does not help either: revocation blocklists are keyed by license identity, and after a downgrade the identity is unchanged. It is the same customer and the same license id; only the entitlements shrank. Blocklisting that id would reject the new license too.

So the question is narrower than “is this license valid?” It is: is this the newest license I have ever seen for this identity, or has it been superseded? That is a freshness question, and signatures say nothing about freshness. We have to add it.

The naive fix is a timestamp — stamp issuedAt into the payload and reject anything older than the last one. But the client owns its clock, and a stale payload carries a stale timestamp that looks perfectly consistent to a rolled-back clock. Anything you compare against wall-clock time inherits the clock-tampering problem. The defense has to be clock-independent.

A monotonic epoch

Keyright gives every license an epoch: a per-identity counter the issuing service increments every time it mints a new license for that id. The first license is epoch 1, the downgrade is epoch 2, next year’s renewal is epoch 3. The epoch lives inside the signed, canonical payload, so it is covered by the signature and cannot be edited after the fact.

public sealed record LicensePayload
{
    public required string LicenseId { get; init; }    // stable identity of the grant
    public required long Epoch { get; init; }          // server-assigned, strictly increasing
    public required DateTimeOffset NotAfter { get; init; }
    public required IReadOnlyList<string> Entitlements { get; init; }
    // audience, subject, seats, ... all canonicalized and signed
}

The epoch has no relationship to time. It is a sequence number, and sequence numbers are immune to clock games. A client that has trusted epoch 3 should never again accept epoch 2 for the same id, whatever the clock says, because a lower epoch is by construction an older grant.

To enforce that, the SDK keeps a high-water mark: the highest epoch it has ever accepted, per license id. Verification gains two steps after the usual signature and expiry checks.

public LicenseStatus Verify(SignedLicense signed)
{
    if (!_signatures.Verify(signed.CanonicalBytes, signed.Signature, _pinnedKey))
        return LicenseStatus.Forged;

    var payload = signed.Payload;
    if (_clock.GetUtcNow() > payload.NotAfter)
        return LicenseStatus.Expired;

    long floor = _watermark.Read(payload.LicenseId);   // 0 if never seen
    if (payload.Epoch < floor)
        return LicenseStatus.Superseded;               // a newer license exists; this one is stale

    if (payload.Epoch > floor)
        _watermark.Advance(payload.LicenseId, payload.Epoch);

    return LicenseStatus.Valid;
}

The first time a client sees epoch 3 it advances its mark to 3. From then on the downgraded epoch 2 fails with Superseded even though its signature is impeccable and it is nowhere near expiry. The replay is dead.

Issuing servicePro, 10 seatsepoch 1Basic, 2 seatsepoch 2replayed oldepoch 1Client high-water markfloor = 1floor = 2floor = 2accept, advanceaccept, advance1 < 2: SupersededThe mark only ever rises; an epoch below it is rejected regardless of signature or expiry.
The high-water mark advances with each newer license and refuses any epoch below it, with no reference to the clock.

Making the mark tamper-evident

The mark is only as good as its storage. If it lives in a plain file, a user can hand-edit it back to 0 and re-admit the old license. Keyright authenticates each row with a machine-bound HMAC key, so a value that was not written by the SDK on this machine is rejected rather than trusted.

public long Read(string licenseId)
{
    if (!TryLoad(out var rows) || !rows.TryGetValue(licenseId, out var row))
        return 0;

    // a row whose MAC does not recompute was not written by us: do not trust its value
    if (!CryptographicOperations.FixedTimeEquals(row.Mac, Mac(licenseId, row.Epoch)))
        return 0;

    return row.Epoch;
}

public void Advance(string licenseId, long epoch)
{
    var rows = TryLoad(out var m) ? m : new Dictionary<string, Row>();
    rows[licenseId] = new Row(epoch, Mac(licenseId, epoch));
    AtomicWrite(rows);   // write temp + File.Move(overwrite) so a crash cannot corrupt the store
}

private byte[] Mac(string licenseId, long epoch)
{
    using var mac = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, _macKey);
    mac.AppendData(Encoding.UTF8.GetBytes(licenseId));
    Span<byte> e = stackalloc byte[8];
    BinaryPrimitives.WriteInt64BigEndian(e, epoch);
    mac.AppendData(e);
    return mac.GetHashAndReset();
}

Editing the stored epoch invalidates its MAC, and Read treats a failed MAC as “never seen” (floor 0) — or, in fail-closed mode, as tampering worth refusing outright. The attacker cannot forge a lower valid row; the best they can do is delete the store.

That residual gap is closed the moment the client goes online. Keyright’s revalidation response carries the server’s authoritative epoch for the license, and the server is the one place the counter cannot be rolled back.

if (revalidation.ServerEpoch > cached.Payload.Epoch)
    return LicenseStatus.Superseded;   // the server has minted a newer license; refetch before continuing

An online client therefore cannot win with a deleted watermark: the server floor dominates. An offline client with local admin and a deleted watermark is the one case the client alone cannot stop — and that is a deliberate boundary, not an oversight.

When it breaks, and how to close it

Be honest about the limit. The epoch floor shrinks the window of a stale license; it does not, by itself, eliminate it offline. If a downgraded license carries a far-future NotAfter, an offline attacker who wipes the watermark re-admits it until that expiry. The fix is architectural, not cryptographic: pair the epoch with a short NotAfter and routine refresh. The downgrade issues epoch 2 and a near-term expiry; the old epoch 1 then dies on its own timetable even if the floor is erased, and the epoch stops the replay everywhere in between. Short-lived licenses and anti-rollback are complementary, not alternatives.

Two more sharp edges. First, the epoch must be assigned by the issuing service, never derived on the client — a client-chosen epoch is not monotonic across machines. Second, the mark is per license id; if you reuse one id across unrelated products the floor conflates them, so scope the id to a single grant. Keyright’s bundle model already does this, giving each product claim its own identity.

Takeaway

A signature answers “did you sign this?” and expiry answers “is it still in date?” Neither answers “is this the current grant?” A monotonic, server-assigned epoch plus a tamper-evident high-water mark adds exactly that third check, and it does so without trusting the client clock. Verify signature, verify expiry, then verify the epoch is not below the newest you have ever accepted — and the Pro license a customer quietly kept after downgrading stops working the first time the Basic license arrives. Keyright’s Community Edition ships the epoch field and the watermark store; see the Keyright docs for configuring fail-closed watermark handling.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.