|
| 1 | +// SPDX-FileCopyrightText: 2026 OpenWaymark contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package verify |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "openwaymark.org/owm/core" |
| 12 | + owmlog "openwaymark.org/owm/log" |
| 13 | + "openwaymark.org/owm/trust" |
| 14 | +) |
| 15 | + |
| 16 | +// httpTrustSource implements trust.Source over the public API, mirroring |
| 17 | +// node/attestation.go's own logSource — same-issuer-only revocation |
| 18 | +// matching (OWM-6 §6), one history fetch per key — but fetching over HTTP |
| 19 | +// instead of reading the local log directly. |
| 20 | +// |
| 21 | +// Known scope limit, stated plainly rather than silently assumed away: every |
| 22 | +// fetch in a chain walk goes to the same base URL the top-level VerifySubject |
| 23 | +// call was given. An attestation chain that crosses node boundaries (an |
| 24 | +// accreditation body attesting an entity from a different node's log) is not |
| 25 | +// followed — the chain simply stops there, which trust.Compute treats as "no |
| 26 | +// further contribution," the same as a chain with no attestation at all. |
| 27 | +// Extending this to follow Options.LogURLs the way VerifySubject itself does |
| 28 | +// for parent references is future work, not solved here. |
| 29 | +// |
| 30 | +// Also unlike VerifySubject's own top-level entries, an attestation's own |
| 31 | +// signature and inclusion proof are not re-verified here — only its |
| 32 | +// commitment against its fetched payload. Re-verifying every hop of a trust |
| 33 | +// chain in full would mean fetching and checking an STH and inclusion proof |
| 34 | +// per attestation, not just per top-level entry; the commitment check alone |
| 35 | +// already rules out a payload that does not match what was actually |
| 36 | +// committed to, which is the failure mode a dishonest *node* (as opposed to |
| 37 | +// a dishonest attestation issuer, already covered by the signature check |
| 38 | +// baked into commitment-independent trust) could otherwise exploit. |
| 39 | +type httpTrustSource struct { |
| 40 | + c *client |
| 41 | +} |
| 42 | + |
| 43 | +func (s *httpTrustSource) AttestationsOf(ctx context.Context, subject core.KeyID) ([]trust.Attestation, error) { |
| 44 | + hist, err := s.c.fetchHistory(ctx, core.SubjectID(subject)) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + type revocation struct { |
| 50 | + target core.Digest |
| 51 | + issuer core.KeyID |
| 52 | + } |
| 53 | + revoked := map[revocation]bool{} |
| 54 | + type parsed struct { |
| 55 | + id core.Digest |
| 56 | + e *core.Entry |
| 57 | + } |
| 58 | + var attestations []parsed |
| 59 | + for _, lv := range hist.Entries { |
| 60 | + leaf, err := owmlog.ParseLeaf(lv.Leaf) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("owm/client/verify: trust source: parse leaf: %w", err) |
| 63 | + } |
| 64 | + se, err := leaf.SignedEntry() |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("owm/client/verify: trust source: parse entry: %w", err) |
| 67 | + } |
| 68 | + e, err := se.Entry() |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("owm/client/verify: trust source: decode entry: %w", err) |
| 71 | + } |
| 72 | + switch e.Type { |
| 73 | + case core.EntryTypeAttestation: |
| 74 | + attestations = append(attestations, parsed{id: leaf.EntryID(), e: e}) |
| 75 | + case core.EntryTypeRevocation: |
| 76 | + if e.Target != nil { |
| 77 | + revoked[revocation{target: e.Target.Entry, issuer: e.Issuer}] = true |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + out := make([]trust.Attestation, 0, len(attestations)) |
| 83 | + for _, a := range attestations { |
| 84 | + salt, payload, err := s.c.fetchPayload(ctx, a.id) |
| 85 | + if err != nil { |
| 86 | + var ae *APIError |
| 87 | + if errors.As(err, &ae) && ae.Erased() { |
| 88 | + // The evidentiary basis is gone under Art. 17 GDPR; nothing |
| 89 | + // left to recompute a level from — no contribution, the |
| 90 | + // same as an attestation nobody issued (mirrors |
| 91 | + // node/attestation.go's logSource exactly). |
| 92 | + continue |
| 93 | + } |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + if !core.VerifyCommitment(a.e.Commitment, salt, payload) { |
| 97 | + return nil, fmt.Errorf("owm/client/verify: trust source: attestation %s: payload does not match its commitment", a.id) |
| 98 | + } |
| 99 | + p, err := trust.ParsePayload(payload) |
| 100 | + if err != nil { |
| 101 | + // A malformed payload cannot happen for an entry a compliant |
| 102 | + // node accepted (checkAttestationPayload runs at Submit time), |
| 103 | + // but this source must not assume every node is compliant. |
| 104 | + continue |
| 105 | + } |
| 106 | + out = append(out, trust.Attestation{ |
| 107 | + Entry: *a.e, |
| 108 | + Payload: p, |
| 109 | + Revoked: revoked[revocation{target: a.id, issuer: a.e.Issuer}], |
| 110 | + }) |
| 111 | + } |
| 112 | + return out, nil |
| 113 | +} |
0 commit comments