Skip to content

feat(credentials): add CCache.Marshal and v4 builders - #94

Open
hstern wants to merge 3 commits into
go-krb5:masterfrom
hstern:ccache-marshal
Open

feat(credentials): add CCache.Marshal and v4 builders#94
hstern wants to merge 3 commits into
go-krb5:masterfrom
hstern:ccache-marshal

Conversation

@hstern

@hstern hstern commented Jun 22, 2026

Copy link
Copy Markdown

What

Adds CCache.Marshal() — the inverse of Unmarshal() — plus v4 construction
helpers (NewV4CCache, AddCredential, SetDefaultPrincipal, NewPrincipal).
Lands as two new files (credentials/ccache_marshal.go, ccache_marshal_test.go);
no existing files are changed.

Why

The library can read credential caches but not write them; marshalling is needed
to issue/persist ccaches programmatically.

Relationship to jcmturner/gokrb5#423

This revives @kula's stalled #423 (original maintainer inactive ~3 years), ported
to the current go-krb5/krb5 tree. It is not a verbatim port — it includes two
changes from the original:

  • v3 keyblock correctness fix: the original encoded the version-3 key type as
    a 16-bit value followed by a 32-bit value, but parseCredential reads it as
    two 16-bit integers. As written, Marshal was therefore not a true inverse
    of Unmarshal for v3 caches. This PR writes two 16-bit integers to match the
    parser.
  • Style/lint adjustments to match this tree's conventions (whitespace, comment
    formatting, and splitting writeCredential into smaller helpers).

Test

  • TestMarshalRoundTrip decodes the existing testdata.CCACHE_TEST (v4) vector,
    Unmarshals it, Marshals it back, and asserts the bytes are identical — a
    byte-exact round trip.
  • TestMarshalRoundTripV3 builds a v3 cache and asserts its bytes survive an
    Unmarshal/Marshal round trip with the key type intact (there is no v3 test
    vector upstream, so this is built in memory). This guards the v3 keyblock fix
    above.

hstern and others added 3 commits June 22, 2026 07:35
Add CCache.Marshal (the inverse of Unmarshal) plus NewV4CCache,
AddCredential, SetDefaultPrincipal and NewPrincipal helpers, with a
byte-exact round-trip test against the existing ccache test vector.

Revives jcmturner/gokrb5#423 by @kula, ported to the go-krb5/krb5 tree
as new files so no existing code changes.

Signed-off-by: Henry Stern <henry@stern.ca>
The version-3 keyblock encodes the key type as two 16-bit integers
(matching parseCredential), not a 16-bit value followed by a 32-bit one.
Without this, Marshal was not a true inverse of Unmarshal for v3 caches.

Add TestMarshalRoundTripV3, which builds a v3 cache and asserts its bytes
survive an Unmarshal/Marshal round trip with the key type intact.

Signed-off-by: Henry Stern <henry@stern.ca>
writeCredential wrote TicketFlags.Bytes verbatim, but parseCredential
reads a fixed 4 bytes. A caller-built Credential with unset flags (nil
Bytes) emitted zero bytes, shifting every following field by four so the
parser read a garbage authorization-data count and panicked in
readAuthDataEntry on Unmarshal. Always emit 4 bytes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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