Skip to content

Move the encoding and crypto of channel tokens to Rust - #8824

Draft
bartlomieju wants to merge 4 commits into
mainfrom
biwanczuk/rust-channel-token
Draft

bartlomieju wants to merge 4 commits into
mainfrom
biwanczuk/rust-channel-token

Conversation

@bartlomieju

Copy link
Copy Markdown
Collaborator

Warning

This PR is not ready for review yet.

The motivation for this change is to move channel tokens, the bytes
that stand for a service, an actor class, an actor or a restored stub,
to Rust along with the rest of the server. This PR moves the part that
needs no live channels: the token key, derived from the cluster key or
from random bytes, its key ID, the AES-256-GCM envelope of RPC tokens,
the plain envelope of storage tokens, and building and reading the
ChannelToken message. It lives in a new crate, channel-token-codec, and
ChannelTokenHandler calls it through a cxx bridge. The handler's public
methods keep their signatures, so the factory and the container client
are unchanged.

The codec is a crate of its own rather than part of workerd-server
because the server crate's bridge header includes worker-factory.h,
which includes channel-token.h, so :channel-token cannot depend on that
bridge. The codec needs nothing from the server.

The handler keeps what touches live channels: the cap tables of the
Frankenvalues in a token, whose entries are tokens of live channels,
which cross the bridge as a flat capnp message; the restored channels,
which send the restore custom events; and the IVs and random keys,
which still come from getEntropy(). Moving those to Rust is the next
step. It needs a few more Frankenvalue shims (its capnp form and its
cap table) and a shim for the restore events, and the code would join
the server crate, which already resolves tokens. That waits for the
Rust container client (#8822): today container-client.c++ calls the
handler, which keeps the handler in C++ below the server crate, and
#8822 removes it, leaving only the factory's calls.

Tokens must not change: storage tokens are kept in Durable Object
storage, and the instances of a cluster, which can run different
versions, redeem each other's RPC tokens. Every token reads the same in
the new code and in the old, both ways. A token is also the same bytes
as before for the same contents and IV, with one exception: capnp-rust
grows the segments of a message after the first faster than capnp-cpp,
and fills any segment with room where capnp-cpp fills only the last, so
a token of three or more segments, a few KiB with nested tokens, can be
laid out differently. Nothing compares token bytes; they are only
carried and decoded, so a Worker can't observe this. Matching it would
take a patched capnp-rust arena. Two things make the rest identical:
the first segment is 128 words, as in MallocMessageBuilder(128), and a
Frankenvalue is copied in the order Frankenvalue::toCapnp() writes it,
each property's name before its value. A restored actor class token
now fails before its vendor token is decoded rather than after, with
the same error.

For testing, channel-token-legacy-test keeps a copy of the C++ codec
that this replaces, with the BoringSSL envelope and the message built
and read by capnp-cpp, and runs it against the handler for each kind of
token and a dozen shapes of props, from empty to six segments. The
legacy code reads every new token as the message it would have written,
the handler reads every legacy token, and single-segment tokens are the
same bytes; of 249 tokens, the 36 that differ all have three or more
segments. Built against the C++ handler before this change, the same
test passes with every token required to be the same bytes, so the copy
is faithful. golden-tokens.txt holds 45 tokens the old C++ wrote,
including V8-serialized, ArrayBuffer, wrapped-capability, nested,
asynchronously encoded and multi-segment ones: the codec writes each
again from its contents and IV, 39 to the same bytes and the other 6,
all of three or more segments, to bytes that read the same, and the
handler reads all of them. The codec has its own unit tests of the key
derivation, the envelope and the message, including tampered, truncated
and wrongly typed tokens. channel-token-test passes unchanged, as do
the server, cluster, stub-storage, persistent-stubs, worker-loader,
JS RPC and container client tests.

channel-token-codec is the encoding and crypto of channel tokens in
Rust: the token key, derived from the cluster key or from random
bytes, its key ID, the AES-256-GCM envelope of RPC tokens, the plain
envelope of storage tokens, and building and reading the ChannelToken
message. Nothing uses it yet; ChannelTokenHandler switches to it next.

It is a crate of its own rather than part of workerd-server because
the server crate's bridge header includes worker-factory.h, which
includes channel-token.h, so :channel-token cannot depend on that
bridge. The codec needs nothing from the server.

What touches live channels stays with the C++ handler: the cap tables
of the Frankenvalues in a token, whose entries are tokens of live
channels, and the restored channels. A Frankenvalue crosses the bridge
as a flat capnp message. The IVs still come from getEntropy(), so C++
passes them in, which also lets a test fix them.

Tokens must not change: storage tokens are kept in Durable Object
storage, and the instances of a cluster redeem each other's RPC
tokens. Every token reads the same in the codec and in the C++, and
the codec writes the same bytes as the C++ for the same contents and
IV wherever capnp-rust lays the message out as capnp-cpp does. Two
things get it there beyond the format: the first segment of the
message is 128 words, as in MallocMessageBuilder(128), and a
Frankenvalue is copied in the order Frankenvalue::toCapnp() writes it,
each property's name before its value, since a capnp deep copy lays
out a value with properties differently.

capnp-rust does not lay out every message as capnp-cpp does: it grows
the segments after the first faster, and fills any segment with room
where capnp-cpp fills only the last. That can show once a message has
three or more segments, a token of a few KiB with nested tokens, which
is then different bytes that read the same. Matching it would take a
patched capnp-rust arena.

channel-token-codec-test runs the C++ handler and the codec against
each other with a shared cluster key, for each token kind, usage and
shape of props: the codec reads the handler's tokens, writes the same
bytes from what it read and the handler's IV, and the handler reads
the codec's tokens. Both reject a changed magic, IV, key ID,
ciphertext or MAC, a truncated token, the wrong type and the wrong
usage with the same errors.
ChannelTokenHandler now calls channel-token-codec for the token key,
the AES-256-GCM envelope and the ChannelToken message, and its own
BoringSSL code and message building are gone. Its public methods keep
their signatures, so the factory, the container client and the
restored channels are unchanged.

The handler still serializes the cap tables of the Frankenvalues in a
token, into a message of the Frankenvalue's own that it hands the
codec once any promised nested tokens resolve, and still decodes them
from the Frankenvalue the codec returns. Because the codec owns that
message, an asynchronous encoding copies the service name and
entrypoint, which the ChannelToken message used to hold. IVs and
random keys still come from getEntropy().

The codec now checks that a token's type may point at what it points
at, so a restored actor class token fails before its vendor token is
decoded rather than after; the error is the same.

The previous commit's differential test ran the C++ and Rust encoders
against each other. Existing tokens, in Durable Object storage or from
a cluster peer on the previous version, still decode, and new tokens
read the same on the previous version. A token is the same bytes as
before, except that one of three or more capnp segments can be laid
out differently (see the previous commit). channel-token-test passes
unchanged.
With the handler on the Rust codec, channel-token-codec-test ran Rust
against itself. golden-tokens.txt replaces it: 45 tokens that the C++
encoder wrote at the first commit of this series, with a fixed cluster
key. They cover each kind, usage and shape of props: empty, JSON,
V8-serialized, ArrayBuffer, a wrapped capability, properties nested
in properties, nested tokens, long names, an asynchronously encoded
token, and tokens of up to six segments.

The codec's tests read each and write it again from what they read
and the token's IV. 39 come out the same bytes. The other 6, marked
`different` in the file, have three or more segments, which capnp-rust
lays out differently (see the first commit of this series); for those
the tests require that the new bytes read the same.

channel-token-golden-test has the handler read every token, check the
contents of some, and write some storage tokens, which have no IV, to
the same bytes.
Workers of the previous version encode and decode channel tokens with
the C++ that the Rust codec replaced. channel-token-legacy-test keeps
a copy of it, LegacyCodec: the BoringSSL envelope and key derivation,
and the ChannelToken message as capnp-cpp builds and reads it. For
each kind of token and each of a dozen shapes of props and restore
arguments, from empty to six segments, with short and long names:

- LegacyCodec reads the handler's token as the message it would have
  written itself, so the previous version reads new tokens;
- the handler reads LegacyCodec's token as written, so new code reads
  the previous version's tokens;
- a token of one segment is the same bytes from both.

Of the 249 tokens it checks, the 36 that are not the same bytes all
have three or more segments, as capnp-rust lays those out differently.
The test requires that the shapes include tokens of one, two and more
segments.

The file uses only the handler's public interface. Built at
2a90085, against the C++ handler, with the same-bytes check applied
to every token, it passes: LegacyCodec is the same as the code it
copies, multi-segment tokens included.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant