|
| 1 | +// Package fee_test's integration test is the FEE (FilOne File Encryption |
| 2 | +// Envelope) insurance-recovery artifact. It is the v1 confidence check for the |
| 3 | +// Hilt recovery path (see FIL-474): it proves the composed fee API recovers |
| 4 | +// plaintext from an envelope using only an archived tenant X25519 private key — |
| 5 | +// no region KEK and no Ingot database. |
| 6 | +// |
| 7 | +// Unlike the sub-package unit tests, this exercises the whole round trip through |
| 8 | +// the top-level fee package (fee.Encrypt / fee.Decrypt, FIL-569), which composes |
| 9 | +// the COSE envelope, the chunked AES-256-GCM-STREAM body cipher and the |
| 10 | +// ECDH-ES+A256KW tenant key wrap. The one place it drops to a sub-package is the |
| 11 | +// explicit kid assertion: the issue calls for reading the recipient entry's kid |
| 12 | +// off the wire and confirming it names the tenant key before recovery, so the |
| 13 | +// test decodes the envelope header with fee/cose to check that directly rather |
| 14 | +// than relying on the kid match fee.Decrypt performs internally. |
| 15 | +// |
| 16 | +// The envelope is not a fixture: it is built at run time by fee.Encrypt. Only |
| 17 | +// the tenant keypair is checked in, for determinism. |
| 18 | +package fee_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "crypto/ecdh" |
| 23 | + "encoding/hex" |
| 24 | + "io" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/fil-forge/ingot/fee" |
| 28 | + "github.com/fil-forge/ingot/fee/aeskw" |
| 29 | + "github.com/fil-forge/ingot/fee/aesstream" |
| 30 | + "github.com/fil-forge/ingot/fee/cose" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + // streamChunkSize is the STREAM plaintext chunk size passed to |
| 36 | + // fee.WithChunkSize. The issue asks for a small chunk (vs the 256 KiB |
| 37 | + // default) so the multi-chunk path runs against a few-KB sample without a |
| 38 | + // large fixture; aesstream rejects anything below MinChunkSize (4 KiB), so |
| 39 | + // that minimum is the smallest legal "small" value. fee records it in the |
| 40 | + // envelope, so recovery recovers it without the test tracking it. |
| 41 | + streamChunkSize = aesstream.MinChunkSize |
| 42 | + |
| 43 | + // samplePlaintextSize is a few KB: large enough to span several |
| 44 | + // streamChunkSize chunks with a partial final chunk, small enough to stay a |
| 45 | + // trivial in-test fixture. |
| 46 | + samplePlaintextSize = 10_000 |
| 47 | +) |
| 48 | + |
| 49 | +// tenantPrivateKeyHex is a fixed, non-secret X25519 private scalar, checked in |
| 50 | +// for determinism (the issue's "fixed test tenant keypair"). It is test-only |
| 51 | +// key material — never a real tenant key — so committing it is safe. Any 32 |
| 52 | +// bytes form a valid X25519 private key; these are the ASCII bytes of |
| 53 | +// "fil-474-fee-tenant-recovery-test". The derived public key is |
| 54 | +// 69228bafb04870ffe3b19fcdd604a9aa3e12c53b7c0e86301dc38b2a317cf37b. |
| 55 | +const tenantPrivateKeyHex = "66696c2d3437342d6665652d74656e616e742d7265636f766572792d74657374" |
| 56 | + |
| 57 | +// tenantKey loads the fixed tenant recipient keypair. The public key is derived |
| 58 | +// from the private scalar, so the archived private key is the single source of |
| 59 | +// truth — exactly what a real recovery would start from. |
| 60 | +func tenantKey(t *testing.T) *ecdh.PrivateKey { |
| 61 | + t.Helper() |
| 62 | + seed, err := hex.DecodeString(tenantPrivateKeyHex) |
| 63 | + require.NoError(t, err) |
| 64 | + priv, err := ecdh.X25519().NewPrivateKey(seed) |
| 65 | + require.NoError(t, err) |
| 66 | + return priv |
| 67 | +} |
| 68 | + |
| 69 | +// samplePlaintext returns n deterministic bytes. The period (251, prime) does |
| 70 | +// not align to streamChunkSize, so chunk boundaries fall on varying byte values |
| 71 | +// rather than a repeating phase. |
| 72 | +func samplePlaintext(n int) []byte { |
| 73 | + b := make([]byte, n) |
| 74 | + for i := range b { |
| 75 | + b[i] = byte(i % 251) |
| 76 | + } |
| 77 | + return b |
| 78 | +} |
| 79 | + |
| 80 | +// encryptToEnvelope seals plaintext to a single ECDH-ES tenant recipient via the |
| 81 | +// composed fee.Encrypt API and returns the complete wire blob |
| 82 | +// (envelope || ciphertext). |
| 83 | +func encryptToEnvelope(t *testing.T, recipientPub *ecdh.PublicKey, kid, plaintext []byte) []byte { |
| 84 | + t.Helper() |
| 85 | + r, err := fee.Encrypt( |
| 86 | + bytes.NewReader(plaintext), |
| 87 | + []fee.Recipient{fee.NewECDHESRecipient(kid, recipientPub)}, |
| 88 | + fee.WithChunkSize(streamChunkSize), |
| 89 | + ) |
| 90 | + require.NoError(t, err) |
| 91 | + blob, err := io.ReadAll(r) |
| 92 | + require.NoError(t, err) |
| 93 | + require.NoError(t, r.Close()) |
| 94 | + return blob |
| 95 | +} |
| 96 | + |
| 97 | +// TestInsuranceRecoveryRoundTrip is the primary acceptance criterion: encrypt a |
| 98 | +// plaintext sample to the test tenant key, confirm the envelope's recipient kid |
| 99 | +// names that key, then recover with only the archived tenant private key and |
| 100 | +// check the plaintext matches the original exactly — all through the composed |
| 101 | +// fee API. |
| 102 | +func TestInsuranceRecoveryRoundTrip(t *testing.T) { |
| 103 | + tenant := tenantKey(t) |
| 104 | + kid := tenant.PublicKey().Bytes() |
| 105 | + |
| 106 | + plaintext := samplePlaintext(samplePlaintextSize) |
| 107 | + require.Greater(t, len(plaintext), streamChunkSize, |
| 108 | + "sample must span multiple chunks so the multi-chunk STREAM path runs") |
| 109 | + |
| 110 | + blob := encryptToEnvelope(t, tenant.PublicKey(), kid, plaintext) |
| 111 | + |
| 112 | + // Confirm the envelope tags the right key for the right recipient before |
| 113 | + // recovery. With a single recipient the unwrap would still succeed if the |
| 114 | + // kid were wrong or dropped, so this explicit on-wire check is the only |
| 115 | + // thing that binds the recipient entry to the tenant key. fee.Decrypt also |
| 116 | + // matches on kid internally, but the issue calls for asserting it directly. |
| 117 | + env, _, err := cose.DecodeEncrypt(blob, cose.WithExpectedType(fee.EnvelopeType)) |
| 118 | + require.NoError(t, err) |
| 119 | + require.Len(t, env.Recipients, 1) |
| 120 | + gotKid, ok := env.Recipients[0].Headers.Unprotected.Bytes(cose.HeaderLabelKID) |
| 121 | + require.True(t, ok, "recipient kid present") |
| 122 | + require.Equal(t, kid, gotKid, "recipient kid matches the tenant key") |
| 123 | + |
| 124 | + // Recover using only the archived tenant private key. |
| 125 | + pr, err := fee.Decrypt(bytes.NewReader(blob), fee.NewECDHESUnwrapper(kid, tenant)) |
| 126 | + require.NoError(t, err) |
| 127 | + recovered, err := io.ReadAll(pr) |
| 128 | + require.NoError(t, err) |
| 129 | + require.Equal(t, plaintext, recovered, "recovered plaintext matches the original exactly") |
| 130 | +} |
| 131 | + |
| 132 | +// TestInsuranceRecoveryWrongPrivateKeyFailsBeforeDecrypt is the second |
| 133 | +// acceptance criterion: unwrapping with the wrong private key returns an error, |
| 134 | +// and decryption is never attempted on the still-encrypted data. The unwrapper |
| 135 | +// carries the real tenant kid (so the recipient still matches), isolating the |
| 136 | +// wrong-key behaviour to the unwrap step. |
| 137 | +func TestInsuranceRecoveryWrongPrivateKeyFailsBeforeDecrypt(t *testing.T) { |
| 138 | + tenant := tenantKey(t) |
| 139 | + kid := tenant.PublicKey().Bytes() |
| 140 | + blob := encryptToEnvelope(t, tenant.PublicKey(), kid, samplePlaintext(samplePlaintextSize)) |
| 141 | + |
| 142 | + // A different archived X25519 key. Deriving it from a distinct fixed scalar |
| 143 | + // keeps the test deterministic. |
| 144 | + wrongSeed := []byte("fil-474-fee-WRONG-tenant-key-xxx") |
| 145 | + require.Len(t, wrongSeed, 32) |
| 146 | + wrong, err := ecdh.X25519().NewPrivateKey(wrongSeed) |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + pr, err := fee.Decrypt(bytes.NewReader(blob), fee.NewECDHESUnwrapper(kid, wrong)) |
| 150 | + require.Nil(t, pr, "no plaintext reader is produced") |
| 151 | + require.Error(t, err) |
| 152 | + require.ErrorIs(t, err, aeskw.ErrIntegrity, "recovery fails at the ECDH-ES+A256KW unwrap") |
| 153 | + // A STREAM (aesstream) error would mean decryption had been attempted; its |
| 154 | + // absence shows fee.Decrypt stopped at the unwrap, before opening the body. |
| 155 | + require.NotErrorIs(t, err, aesstream.ErrCorrupted, "STREAM decryption is never attempted") |
| 156 | +} |
| 157 | + |
| 158 | +// TestInsuranceRecoveryCorruptedProtectedHeaderFailsDecode is the third |
| 159 | +// acceptance criterion: flipping a byte in the protected-header bytes makes |
| 160 | +// decode return an error, rather than yielding a structure that would decrypt to |
| 161 | +// garbage plaintext. fee.Decrypt surfaces the decode failure as a wrapped |
| 162 | +// cose.ErrMalformed and produces no plaintext reader. |
| 163 | +func TestInsuranceRecoveryCorruptedProtectedHeaderFailsDecode(t *testing.T) { |
| 164 | + tenant := tenantKey(t) |
| 165 | + kid := tenant.PublicKey().Bytes() |
| 166 | + blob := encryptToEnvelope(t, tenant.PublicKey(), kid, samplePlaintext(samplePlaintextSize)) |
| 167 | + |
| 168 | + // Decode once cleanly to locate the protected-header bytes within the blob. |
| 169 | + env, _, err := cose.DecodeEncrypt(blob, cose.WithExpectedType(fee.EnvelopeType)) |
| 170 | + require.NoError(t, err) |
| 171 | + require.NotEmpty(t, env.Headers.RawProtected) |
| 172 | + |
| 173 | + off := bytes.Index(blob, env.Headers.RawProtected) |
| 174 | + require.GreaterOrEqual(t, off, 0, "protected-header bytes located in the blob") |
| 175 | + |
| 176 | + // Flip the first protected-header byte (the CBOR map head), so the protected |
| 177 | + // bytes are no longer a CBOR map and a strict decode rejects the envelope |
| 178 | + // outright instead of returning a structure to decrypt. |
| 179 | + corrupted := bytes.Clone(blob) |
| 180 | + corrupted[off] ^= 0xFF |
| 181 | + |
| 182 | + pr, err := fee.Decrypt(bytes.NewReader(corrupted), fee.NewECDHESUnwrapper(kid, tenant)) |
| 183 | + require.Nil(t, pr, "no plaintext reader is produced") |
| 184 | + require.Error(t, err) |
| 185 | + require.ErrorIs(t, err, cose.ErrMalformed) |
| 186 | +} |
0 commit comments