DagrA guide to binary formats, and to Dagrdraft

11 Chapter eleven

Headers, and a token you can trust

Some data has to prove where it came from. A login token says who you are and what you may do, and a server that receives one must know that nobody changed it on the way. That takes a signature, and the signature has to live somewhere. Dagr's place for it is a header in front of the data.

A token, the usual way

The usual form of such a token is a JSON Web Token, a JWT. It is three pieces of text joined by dots:

  1. A header, in JSON: which algorithm made the signature, and with which key.
  2. The claims, in JSON: who the token is about, who issued it, when it expires, what it allows.
  3. A signature over the first two pieces, made with a secret key.

Each piece is then written in base64, an alphabet of 64 harmless characters, so that the whole token can travel anywhere text can. Base64 writes every three bytes as four characters.

Below is such a token, and the same claims as a Dagr graph. Change the values and watch both.

header, tagstextnumberslengths and countssignatureframing

The claims are the same and the signature is the same kind, 32 bytes from the same algorithm. What differs is everything around them.

The Dagr token is binary. If it has to travel where only text can, in a web request's header for example, it needs base64 too, once, over the whole token. The panel shows that size as well, because that is the fair comparison there.

Where the signature goes

Every Dagr buffer in this guide has started with a framing byte, and until now it was a dull one. Its two lowest bits say what follows. One of them means: before the data, there is a header. This is a header for the whole buffer, and not the byte or two in front of a union's value that chapter 8 called by the same name.

Point at the first bytes of the Dagr token above. After the framing comes the header, a small packed node with a length in front, and after the header come the claims, exactly as they would be without one.

TOKEN = DataGraph("Token", root_type=t.ref("Claims"),
    node_types=[
        Node("Claims", fields=[
            "subject"   >> t.utf8 >> required,
            "issuer"    >> t.utf8 >> required,
            "issuedAt"  >> t.u64 >> required,
            "expiresAt" >> t.u64 >> required,
            "scopes"    >> t.utf8.array >> required,
        ], packed=True),
    ],
    header=Node("Signature", fields=[
        "algorithm" >> t.utf8 >> required,
        "keyId"     >> t.utf8 >> required,
        "signature" >> t.data >> required,
    ]))

The header is a node like any other, and what goes in it is up to you. Dagr brings no cryptography. When writing, it hands your code the finished bytes of the data and asks for the header to put in front. Your code computes a signature over those bytes, with whatever algorithm you chose, and returns it.

That this works at all rests on something from chapter 4: every pointer in Dagr is a distance, never an address. Nothing inside the data knows where the data begins, so bytes can be put in front of it without changing one byte of it. A header can be added, replaced or taken away, and the signed bytes stay what they were.

Check first, read afterwards

The other half is reading. A token arrives from a stranger. Its contents are not to be believed, and ideally not even looked at, until the signature has been checked. Dagr's reader takes a function of yours that is given the header and the bytes of the data, and decides. Only if it agrees is the data read at all.

Here is a verifier doing that. It holds one key, named k1, and accepts one algorithm. Try to get a changed token past it: use the buttons, or click any byte to change it.

    header, tagstextnumberslengthssignaturedashed: changed on the wayfaded: never read

    The same bytes every time

    Chapter 10 ended with a warning: the same value can be encoded in several ways, and a signature is over bytes, not over values. There are two ways out of that, and tokens use the simple one. A verifier never encodes anything. It checks the bytes it received, and reads the claims from those same bytes. A JWT does this by carrying the exact text that was signed. A Dagr token does it by signing the bytes of the data as they are, and reading them in place.

    The harder way out is to demand that every writer produce identical bytes for the same value. Dagr's writers are held to that, by tests that compare the output of all of them byte for byte, and it matters as soon as two parties each compute a hash or a signature of what should be the same data.

    Next to the alternatives

    A fuller version of this token, with free-form claims using the union from chapter 10, exists as a project of its own. It builds the token in ten languages, checks that all ten produce the same bytes and can verify each other's, and measures it against JWT and CBOR tokens: dagr-signed-token.

    Headers are not only for signatures

    A header is a place for anything that should be known about the data before the data is read:

    Because the header is a packed node, it can grow new fields later, and a tool that knows nothing about your schema can still see from the first byte that a header is there, and step over it by its length.

    What these panels are.

    Try it

    1. In the first panel, change one letter of the secret key. Which bytes of the Dagr token change, and which do not?
    2. Add a scope. By how many characters does the JWT grow, and by how many bytes the Dagr token? Where does the difference come from?
    3. In the second panel, click a byte of the key's name in the header. The signature is still correct. Why is the token refused anyway, and should it be?
    4. Click a byte, then click the same byte again. What does the verifier say now?
    5. Find the earliest byte you can change so that the verifier refuses without computing a signature at all.

    What comes next

    A token is one small message. Chapter 12 is about the opposite: data that never ends, such as a log or a stream of events, where records are added at the end forever and a reader often wants only the newest ones. Dagr's name for that is a DataSink.