Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This module is the **coordination protocol** of a JFA **substrate**: the depende
| **Fee legibility** | Fees exist on the wire only as a coordinator-signed `FeeDeclaration` — authored, legible, contestable | `fees/`; golden vectors |
| **Legibility is an output, not a comment** | `SPEC.md` is normative and a build deliverable: a foreign implementer builds from it without reading this source | `testdata/vectors.json` (ground truth) + `testdata/reproduce_spec.py` (independent reproducer) + `vectors/` golden test — drift between code and committed vectors fails the build |
| **Canonical, deterministic encoding** | `canon/` length-prefixed, domain-separated signing encoder | golden vectors regenerated-and-compared byte-for-byte on every test run |
| **Representable timestamps** (SPEC §3) | Every timestamp-bearing message verifier checks `canon.ValidTime` before checking the signature; a signature over incomplete canonical bytes cannot authorize an invalid timestamp | `TestVerifyRejectsUnrepresentableTime`, `TestVerifyRepresentableTimeBoundaries`, and `FuzzMessageVerification` |

## JFA registry bindings

Expand Down
4 changes: 2 additions & 2 deletions canon/canon.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func (buf *Buffer) Err() error {
// Sum returns a copy of the accumulated canonical bytes. The caller signs or
// verifies over exactly these bytes. When Err() is non-nil the returned bytes
// are incomplete by construction (the offending field was skipped, not
// wrapped), so any signature over them will fail to verify — callers should
// check Err() rather than relying on that.
// wrapped). They can still be signed: verifiers must explicitly reject invalid
// timestamps rather than trusting a signature over incomplete bytes.
func (buf *Buffer) Sum() []byte {
out := make([]byte, len(buf.b))
copy(out, buf.b)
Expand Down
55 changes: 55 additions & 0 deletions canon/verify_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package canon

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"os"
"testing"
)

func FuzzVerifySig(f *testing.F) {
// Seed with the normative signatures, not just random invalid byte strings.
data, err := os.ReadFile("../testdata/vectors.json")
if err != nil {
f.Fatal(err)
}
var fixture struct {
Messages []struct {
PublicKey string `json:"public_key_hex"`
Payload string `json:"canonical_bytes_hex"`
Signature string `json:"signature_hex"`
}
}
if err := json.Unmarshal(data, &fixture); err != nil {
f.Fatal(err)
}
for _, m := range fixture.Messages {
pub, err := hex.DecodeString(m.PublicKey)
if err != nil {
f.Fatal(err)
}
payload, err := hex.DecodeString(m.Payload)
if err != nil {
f.Fatal(err)
}
sig, err := hex.DecodeString(m.Signature)
if err != nil {
f.Fatal(err)
}
f.Add(pub, payload, sig)
}
f.Add([]byte{}, []byte{}, []byte{})
f.Fuzz(func(t *testing.T, pub, payload, sig []byte) {
got := VerifySig(pub, payload, sig)
if len(pub) != 32 || len(sig) != 64 {
if got {
t.Fatal("accepted malformed key/signature")
}
return
}
if want := ed25519.Verify(pub, payload, sig); got != want {
t.Fatalf("signature result %v, want %v", got, want)
}
})
}
9 changes: 6 additions & 3 deletions employment/employment.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func (a *Assignment) Sign(priv ed25519.PrivateKey) {
// Verify reports whether Signature is a valid coordinator signature over the
// assignment.
func (a Assignment) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, a.CanonicalBytes(), a.Signature)
return canon.ValidTime(a.OfferedAt) &&
canon.VerifySig(pub, a.CanonicalBytes(), a.Signature)
}

// CanonicalBytes returns the deterministic signing payload for the decline,
Expand All @@ -132,7 +133,8 @@ func (d *Decline) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid node signature over the decline.
func (d Decline) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, d.CanonicalBytes(), d.Signature)
return canon.ValidTime(d.DeclinedAt) &&
canon.VerifySig(pub, d.CanonicalBytes(), d.Signature)
}

// CanonicalBytes returns the deterministic signing payload for the job report,
Expand All @@ -156,5 +158,6 @@ func (r *JobReport) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid node signature over the report.
func (r JobReport) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, r.CanonicalBytes(), r.Signature)
return canon.ValidTime(r.StartedAt) && canon.ValidTime(r.FinishedAt) &&
canon.VerifySig(pub, r.CanonicalBytes(), r.Signature)
}
3 changes: 2 additions & 1 deletion fees/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ func (f *FeeDeclaration) Sign(priv ed25519.PrivateKey) {
// Verify reports whether Signature is a valid coordinator signature over the
// declaration.
func (f FeeDeclaration) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, f.CanonicalBytes(), f.Signature)
return canon.ValidTime(f.EffectiveAt) &&
canon.VerifySig(pub, f.CanonicalBytes(), f.Signature)
}
6 changes: 4 additions & 2 deletions identity/keylife.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func (k *KeyRotation) Sign(priv ed25519.PrivateKey) {
// malformed key to ed25519.Verify and panic. Validating the successor here is
// the same discipline the operator rotation path already applies.
func (k KeyRotation) Verify(currentPub ed25519.PublicKey) bool {
return k.Algo == "ed25519" &&
return canon.ValidTime(k.RotatedAt) &&
k.Algo == "ed25519" &&
len(k.NewPublicKey) == ed25519.PublicKeySize &&
canon.VerifySig(currentPub, k.CanonicalBytes(), k.Signature)
}
Expand Down Expand Up @@ -102,7 +103,8 @@ func (k *KeyRevocation) Sign(priv ed25519.PrivateKey) {
// is rejected. A revocation is only ever honored against the key it actually
// kills.
func (k KeyRevocation) Verify(trustedPub ed25519.PublicKey) bool {
return len(k.RevokedPublicKey) == ed25519.PublicKeySize &&
return canon.ValidTime(k.RevokedAt) &&
len(k.RevokedPublicKey) == ed25519.PublicKeySize &&
bytes.Equal(k.RevokedPublicKey, trustedPub) &&
canon.VerifySig(k.RevokedPublicKey, k.CanonicalBytes(), k.Signature)
}
12 changes: 8 additions & 4 deletions lease/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (l *StorageLease) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid coordinator signature.
func (l StorageLease) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, l.CanonicalBytes(), l.Signature)
return canon.ValidTime(l.IssuedAt) && canon.ValidTime(l.ExpiresAt) &&
canon.VerifySig(pub, l.CanonicalBytes(), l.Signature)
}

// CanonicalBytes for the decline (SPEC.md §4.8).
Expand All @@ -195,7 +196,8 @@ func (d *LeaseDecline) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid node signature.
func (d LeaseDecline) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, d.CanonicalBytes(), d.Signature)
return canon.ValidTime(d.DeclinedAt) &&
canon.VerifySig(pub, d.CanonicalBytes(), d.Signature)
}

// CanonicalBytes for the release (SPEC.md §4.9).
Expand All @@ -214,7 +216,8 @@ func (r *LeaseRelease) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid node signature.
func (r LeaseRelease) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, r.CanonicalBytes(), r.Signature)
return canon.ValidTime(r.ReleasedAt) &&
canon.VerifySig(pub, r.CanonicalBytes(), r.Signature)
}

// CanonicalBytes for the proof response (SPEC.md §4.10).
Expand All @@ -237,5 +240,6 @@ func (p *ProofResponse) Sign(priv ed25519.PrivateKey) {

// Verify reports whether Signature is a valid node signature.
func (p ProofResponse) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, p.CanonicalBytes(), p.Signature)
return canon.ValidTime(p.RespondedAt) &&
canon.VerifySig(pub, p.CanonicalBytes(), p.Signature)
}
3 changes: 2 additions & 1 deletion listing/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,6 @@ func (l *CapabilityListing) Sign(priv ed25519.PrivateKey) {
// pub is the node's public key, resolved out-of-band by the coordinator; the
// protocol does not distribute keys.
func (l CapabilityListing) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, l.CanonicalBytes(), l.Signature)
return canon.ValidTime(l.IssuedAt) &&
canon.VerifySig(pub, l.CanonicalBytes(), l.Signature)
}
3 changes: 2 additions & 1 deletion liveness/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ func (h *Heartbeat) Sign(priv ed25519.PrivateKey) {
// Verify reports whether Signature is a valid node signature over the
// heartbeat. pub is the node's public key, resolved out-of-band.
func (h Heartbeat) Verify(pub ed25519.PublicKey) bool {
return canon.VerifySig(pub, h.CanonicalBytes(), h.Signature)
return canon.ValidTime(h.SentAt) &&
canon.VerifySig(pub, h.CanonicalBytes(), h.Signature)
}
68 changes: 68 additions & 0 deletions operator/verify_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package operator

import (
"bytes"
"crypto/ed25519"
"encoding/json"
"testing"
)

func FuzzOperatorVerification(f *testing.F) {
privs, keys := sevenKeys()
tx := sampleTransmission()
tx.Sign(privs[2], privs[5], 2, 5)
next := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, 32)).Public().(ed25519.PublicKey)
rotation := OperatorRotation{OperatorID: "cloudy", KeyIndex: 3, NewPublicKey: next, Algo: AlgoEd25519, Nonce: bytes.Repeat([]byte{1}, 16), Seq: 1}
rotation.Sign(privs[2], privs[5], 2, 5)
response := ConformanceResponse{OperatorID: "cloudy", Challenge: []byte("challenge"), Algo: AlgoEd25519}
response.Sign(privs[2], privs[5], 2, 5)
for i, m := range []any{tx, rotation, response} {
b, err := json.Marshal(m)
if err != nil {
f.Fatal(err)
}
f.Add(uint8(i), string(b), keys[2].PublicKey)
}
f.Add(uint8(0), `{"Idx0":-1,"Idx1":7,"Algo":"unknown"}`, []byte{})
f.Fuzz(func(t *testing.T, kind uint8, payload string, key []byte) {
// A registry supplied by an external coordinator may contain malformed
// key material; verification must fail safely rather than panic.
_, km := sevenKeys()
km[2] = KeyRecord{PublicKey: key, Algo: AlgoEd25519}
switch kind % 3 {
case 0:
var m OperatorTransmission
if json.Unmarshal([]byte(payload), &m) != nil {
return
}
if m.Verify(km) == nil {
m.OperatorID += "changed"
if m.Verify(km) == nil {
t.Fatal("transmission identity not signed")
}
}
case 1:
var m OperatorRotation
if json.Unmarshal([]byte(payload), &m) != nil {
return
}
if m.Verify(km) == nil {
m.OperatorID += "changed"
if m.Verify(km) == nil {
t.Fatal("rotation identity not signed")
}
}
case 2:
var m ConformanceResponse
if json.Unmarshal([]byte(payload), &m) != nil {
return
}
if m.Verify(km) == nil {
m.Challenge = append(m.Challenge, 1)
if m.Verify(km) == nil {
t.Fatal("challenge not signed")
}
}
}
})
}
110 changes: 110 additions & 0 deletions transport/httpjson/decode_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package httpjson

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/NTARI-RAND/sohocloud-protocol/employment"
"github.com/NTARI-RAND/sohocloud-protocol/fees"
"github.com/NTARI-RAND/sohocloud-protocol/listing"
"github.com/NTARI-RAND/sohocloud-protocol/liveness"
)

func FuzzHandlerJSON(f *testing.F) {
now := time.Unix(1700000000, 123).UTC()
seeds := []any{listing.CapabilityListing{NodeID: "node", IssuedAt: now, Seq: 1<<63 + 1}, liveness.Heartbeat{NodeID: "node", SentAt: now, Seq: 1<<63 + 2}, employment.Decline{NodeID: "node", JobID: "job", DeclinedAt: now}, employment.JobReport{NodeID: "node", JobID: "job", StartedAt: now, FinishedAt: now}}
for i, v := range seeds {
b, err := json.Marshal(v)
if err != nil {
f.Fatal(err)
}
f.Add(uint8(i), string(b))
}
for _, s := range []string{"", "{", "[]", "null", `{"SentAt":"2500-01-01T00:00:00Z"}`, `{"Seq":18446744073709551615}`, `{"Seq":18446744073709551616}`, `{"Signature":"!"}`} {
f.Add(uint8(1), s)
}
f.Fuzz(func(t *testing.T, kind uint8, body string) {
paths := []string{"/v0/listing", "/v0/heartbeat", "/v0/decline", "/v0/report"}
c := &testCoordinator{}
w := httptest.NewRecorder()
Handler(c).ServeHTTP(w, httptest.NewRequest(http.MethodPost, paths[int(kind)%len(paths)], strings.NewReader(body)))
switch w.Code {
case 204:
if len(c.received) != 1 {
t.Fatal("decoded request was lost")
}
case 400:
if len(c.received) != 0 {
t.Fatal("bad JSON reached coordinator")
}
default:
t.Fatalf("unexpected status %d", w.Code)
}
// The reference handler is not a semantic validator/authenticator. Decode
// success (including null/unknown fields) does not authorize a message.
})
}

func FuzzClientJSON(f *testing.F) {
now := time.Unix(1700000000, 123).UTC()
for i, v := range []any{[]employment.Assignment{{NodeID: "node", JobID: "job", OfferedAt: now}}, fees.FeeDeclaration{CoordinatorID: "coord", EffectiveAt: now, Seq: 1<<63 + 1}} {
b, err := json.Marshal(v)
if err != nil {
f.Fatal(err)
}
f.Add(i == 0, true, string(b))
}
for _, s := range []string{"", "{", "[]", "null", `{"Seq":18446744073709551616}`, `{"EffectiveAt":"bad-time"}`, strings.Repeat("x", 8192)} {
f.Add(false, true, s)
f.Add(true, false, s)
}
f.Fuzz(func(t *testing.T, jobs, success bool, payload string) {
code := 503
if success {
code = 200
}
body := &trackedBody{Reader: strings.NewReader(payload)}
c := &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: code, Header: make(http.Header), Body: body}, nil
})}}
var err error
if jobs {
_, err = c.PollJobs(context.Background(), "node")
} else {
_, err = c.Fees(context.Background())
}
if !body.closed {
t.Fatal("response body leaked")
}
if !success && (err == nil || body.read > 4096) {
t.Fatalf("HTTP failure lost or read unbounded: read %d, err %v", body.read, err)
}

})
}

func TestClientRejectsMalformedResponses(t *testing.T) {
for _, operation := range []string{"jobs", "fees"} {
bad := []string{"", "{", `{"Signature":"!"}`, `{"EffectiveAt":"not a timestamp"}`, `{"Seq":18446744073709551616}`}
if operation == "jobs" {
bad = []string{"", "{", `{}`, `[{"Signature":"!"}]`, `[{"OfferedAt":"not a timestamp"}]`}
} else {
bad = append(bad, `[]`)
}
for _, payload := range bad {
t.Run(operation+"/"+payload, func(t *testing.T) {
c := &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: &trackedBody{Reader: strings.NewReader(payload)}}, nil
})}}
if err := clientCalls(c, context.Background())[operation](); err == nil {
t.Fatal("malformed response accepted")
}
})
}
}
}
Loading
Loading