The eight Go test functions H2 added become subtests of existing tests (catalogue's state test, the bundle's state test, the views test, the process state test, the testbus bus test, the seal byte-for-byte test), and four more are folded beside them (two seal, two process watch); the two TypeScript seal tests become one, and two more TypeScript tests are folded into their neighbours. No assertion is lost. Builds: design 47 H2 §819
190 lines
5.9 KiB
Go
190 lines
5.9 KiB
Go
package seal
|
|
|
|
import (
|
|
"crypto/ecdh"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/nacl/box"
|
|
)
|
|
|
|
type fixture struct {
|
|
Recipient KeyPair `json:"recipient"`
|
|
Sealed []struct {
|
|
Plaintext string `json:"plaintext"`
|
|
Box Box `json:"box"`
|
|
} `json:"sealed"`
|
|
Vectors []struct {
|
|
Plaintext string `json:"plaintext"`
|
|
EphPrivate string `json:"ephPrivate"`
|
|
IV string `json:"iv"`
|
|
Box Box `json:"box"`
|
|
} `json:"vectors"`
|
|
}
|
|
|
|
func readFixture(t *testing.T) fixture {
|
|
t.Helper()
|
|
raw, err := os.ReadFile("testdata/sealed-by-licence-manager.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var f fixture
|
|
if err := json.Unmarshal(raw, &f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.Sealed) == 0 || len(f.Vectors) == 0 {
|
|
t.Fatal("the fixture holds no boxes")
|
|
}
|
|
return f
|
|
}
|
|
|
|
// Every box the licence manager's own Seal made opens here to its plaintext.
|
|
func TestABoxTheLicenceManagerSealedOpens(t *testing.T) {
|
|
f := readFixture(t)
|
|
for _, s := range f.Sealed {
|
|
got, err := Open(s.Box, f.Recipient.PrivateKey)
|
|
if err != nil || got != s.Plaintext {
|
|
t.Fatalf("opened %q, %v; want %q", got, err, s.Plaintext)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Given the same one-time key and iv, this package seals byte for byte what the licence manager sealed:
|
|
// the reverse direction, so whatever opens the licence manager's boxes opens these.
|
|
func TestSealIsByteForByteTheLicenceManagersBox(t *testing.T) {
|
|
f := readFixture(t)
|
|
for _, v := range f.Vectors {
|
|
d := base64.StdEncoding.DecodeString
|
|
ephRaw, err1 := d(v.EphPrivate)
|
|
iv, err2 := d(v.IV)
|
|
if err1 != nil || err2 != nil {
|
|
t.Fatal(err1, err2)
|
|
}
|
|
eph, err := ecdh.X25519().NewPrivateKey(ephRaw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := sealWith(v.Plaintext, f.Recipient.PublicKey, eph, iv)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != v.Box {
|
|
t.Fatalf("sealed %+v\nthe licence manager sealed %+v", got, v.Box)
|
|
}
|
|
if plain, err := Open(got, f.Recipient.PrivateKey); err != nil || plain != v.Plaintext {
|
|
t.Fatalf("reopened %q, %v", plain, err)
|
|
}
|
|
}
|
|
// The anonymous-box fixture the TypeScript tests read is what box.SealAnonymous makes, and each opens with the
|
|
// recipient's key as the vault's intake opens it (box.OpenAnonymous). MESH_SDK_WRITE_FIXTURES=1 writes it anew.
|
|
t.Run("the anonymous box fixture is Go's bytes", func(t *testing.T) {
|
|
want := anonymousVectors(t)
|
|
if os.Getenv("MESH_SDK_WRITE_FIXTURES") == "1" {
|
|
raw, _ := json.MarshalIndent(want, "", " ")
|
|
if err := os.WriteFile(anonymousFixturePath, append(raw, '\n'), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
raw, err := os.ReadFile(anonymousFixturePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got anonymousFixture
|
|
if err := json.Unmarshal(raw, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if g, w := mustJSON(got), mustJSON(want); g != w {
|
|
t.Fatalf("the fixture is not Go's bytes:\n%s\nwant\n%s", g, w)
|
|
}
|
|
var pub, priv [32]byte
|
|
p, _ := base64.StdEncoding.DecodeString(got.Recipient.Public)
|
|
k, _ := base64.StdEncoding.DecodeString(got.Recipient.Private)
|
|
copy(pub[:], p)
|
|
copy(priv[:], k)
|
|
for _, v := range got.Vectors {
|
|
sealed, _ := base64.StdEncoding.DecodeString(v.Sealed)
|
|
if out, ok := box.OpenAnonymous(nil, sealed, &pub, &priv); !ok || string(out) != v.Plaintext {
|
|
t.Errorf("a box does not open to %q: %q, %v", v.Plaintext, out, ok)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// A box the agent module's TypeScript sealed (the fixture the licence manager keeps) opens here too.
|
|
func TestABoxSealedInTypeScriptOpens(t *testing.T) {
|
|
raw, err := os.ReadFile("testdata/sealed-by-typescript.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var f struct {
|
|
PrivateKey string `json:"privateKey"`
|
|
Box Box `json:"box"`
|
|
Plaintext string `json:"plaintext"`
|
|
}
|
|
if err := json.Unmarshal(raw, &f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := Open(f.Box, f.PrivateKey); err != nil || got != f.Plaintext {
|
|
t.Fatalf("opened %q, %v", got, err)
|
|
}
|
|
}
|
|
|
|
// What Seal makes opens with its recipient's key and no other, and a fresh box differs every time.
|
|
func TestABoxOpensOnlyForItsRecipient(t *testing.T) {
|
|
a, err := GenerateKeyPair()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := GenerateKeyPair()
|
|
box, err := Seal("a token", a.PublicKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := Open(box, a.PrivateKey); err != nil || got != "a token" {
|
|
t.Fatalf("opened %q, %v", got, err)
|
|
}
|
|
if _, err := Open(box, b.PrivateKey); err == nil {
|
|
t.Fatal("a box opened for another key")
|
|
}
|
|
again, _ := Seal("a token", a.PublicKey)
|
|
if again.Eph == box.Eph || again.IV == box.IV {
|
|
t.Fatal("two boxes share a one-time key or iv")
|
|
}
|
|
// A box changed in any field, or of another version, does not open.
|
|
t.Run("a tampered box does not open", func(t *testing.T) {
|
|
f := readFixture(t)
|
|
good := f.Sealed[1].Box
|
|
flip := func(s string) string {
|
|
b, _ := base64.StdEncoding.DecodeString(s)
|
|
b[len(b)-1] ^= 1
|
|
return base64.StdEncoding.EncodeToString(b)
|
|
}
|
|
for name, box := range map[string]Box{
|
|
"ct": {V: 1, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: flip(good.Ct)},
|
|
"tag": {V: 1, Eph: good.Eph, IV: good.IV, Tag: flip(good.Tag), Ct: good.Ct},
|
|
"iv": {V: 1, Eph: good.Eph, IV: flip(good.IV), Tag: good.Tag, Ct: good.Ct},
|
|
"eph": {V: 1, Eph: flip(good.Eph), IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
|
"version": {V: 2, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
|
} {
|
|
if _, err := Open(box, f.Recipient.PrivateKey); err == nil {
|
|
t.Fatalf("a box with a changed %s opened", name)
|
|
}
|
|
}
|
|
})
|
|
// A key that is not X25519 PEM is refused, not misread.
|
|
t.Run("a key of another kind is refused", func(t *testing.T) {
|
|
if _, err := Seal("x", "not a key"); err == nil {
|
|
t.Fatal("sealed to text")
|
|
}
|
|
f := readFixture(t)
|
|
if _, err := Open(f.Sealed[0].Box, f.Recipient.PublicKey); err == nil {
|
|
t.Fatal("opened with a public key")
|
|
}
|
|
if _, err := Seal("x", f.Recipient.PrivateKey); err == nil {
|
|
t.Fatal("sealed to a private key")
|
|
}
|
|
})
|
|
}
|