ipfs: the swarm key, entry nodes and signed index
interzone-mirror publishes one signed index.json per run, naming the IPFS directories that hold the packages, images, git repos and binaries. Verify checks the signature and the shape of every value that later ends up in a URL or a command. The swarm key ships here on purpose: it keeps the public IPFS network from seeing who holds what, not a secret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
56edbf05b3
commit
725a1f4259
3 files changed
+143
No files matched your search
@@ -0,0 +1,98 @@
|
||||
package ipfs
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SwarmKey admits a node to interzone's private swarm. It ships with
|
||||
// interzone, so it isn't secret: it keeps the public IPFS network, and
|
||||
// whoever crawls it, from seeing which servers hold what.
|
||||
//
|
||||
//go:embed swarm.key
|
||||
var SwarmKey string
|
||||
|
||||
// Bootstrap are the swarm's entry nodes (IPFS.md in interzone-mirror).
|
||||
var Bootstrap = []string{
|
||||
"/dns4/infamous.g0th.ir/tcp/4001/p2p/12D3KooWPCRCpBuQErPEwg6Z6xTFARK15ae8vnkmsSS95EfrHduz",
|
||||
"/dns4/yeule.g0th.ir/tcp/443/wss/p2p/12D3KooWPCRCpBuQErPEwg6Z6xTFARK15ae8vnkmsSS95EfrHduz",
|
||||
}
|
||||
|
||||
const (
|
||||
// Name is the IPNS name the newest root is published under.
|
||||
Name = "k51qzi5uqu5dj8u2m9w83ce9q9yoomao9tilqz5hrm99oxnnsmflyyldrtaz3n"
|
||||
// IndexURL serves the newest index.json (and .sig) over HTTPS too.
|
||||
IndexURL = "https://infamous.g0th.ir/.ipfs/"
|
||||
// KuboVersion is the IPFS daemon to install when no index says otherwise.
|
||||
KuboVersion = "v0.43.1"
|
||||
)
|
||||
|
||||
// publicKey checks index.json's signature (secrets/ipfs-sign.pem in
|
||||
// interzone-mirror holds its private half).
|
||||
var publicKey, _ = base64.StdEncoding.DecodeString("vMZTAm9Xt1p1rgjUvHuxz/Jv94J4yxy9PXZMpWfz2Qk=")
|
||||
|
||||
// Index says what one publish holds. Its parts are IPFS directories, so
|
||||
// once the index is verified everything under them is too: IPFS checks
|
||||
// each block against its CID.
|
||||
type Index struct {
|
||||
Version int64 `json:"version"`
|
||||
Created string `json:"created"`
|
||||
Interzone string `json:"interzone"`
|
||||
Kubo string `json:"kubo"`
|
||||
Playbook struct {
|
||||
Repo string `json:"repo"`
|
||||
Commit string `json:"commit"`
|
||||
} `json:"playbook"`
|
||||
Parts struct {
|
||||
Packages string `json:"packages"`
|
||||
OCI string `json:"oci"`
|
||||
Git string `json:"git"`
|
||||
Bin string `json:"bin"`
|
||||
} `json:"parts"`
|
||||
// Bin holds the SHA-256 of each file in the bin part, so a copy
|
||||
// fetched over HTTPS can be checked too.
|
||||
Bin map[string]string `json:"bin"`
|
||||
}
|
||||
|
||||
var (
|
||||
cidRe = regexp.MustCompile(`^b[a-z2-7]{50,100}$`) // CIDv1, base32
|
||||
commitRe = regexp.MustCompile(`^[0-9a-f]{40}$`)
|
||||
versionRe = regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
|
||||
repoRe = regexp.MustCompile(`^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$`)
|
||||
sha256Re = regexp.MustCompile(`^[0-9a-f]{64}$`)
|
||||
)
|
||||
|
||||
// Verify checks index.json against its signature and returns it. Every
|
||||
// value that ends up in a URL or a command is checked for its shape too.
|
||||
func Verify(index, sig []byte) (*Index, error) {
|
||||
s, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(sig)))
|
||||
if err != nil || !ed25519.Verify(ed25519.PublicKey(publicKey), index, s) {
|
||||
return nil, errors.New("index.json isn't signed by interzone-mirror's key")
|
||||
}
|
||||
var x Index
|
||||
if err := json.Unmarshal(index, &x); err != nil {
|
||||
return nil, fmt.Errorf("index.json: %w", err)
|
||||
}
|
||||
p := x.Parts
|
||||
for _, cid := range []string{p.Packages, p.OCI, p.Git, p.Bin} {
|
||||
if !cidRe.MatchString(cid) {
|
||||
return nil, fmt.Errorf("index.json: %q isn't a CID", cid)
|
||||
}
|
||||
}
|
||||
if !commitRe.MatchString(x.Playbook.Commit) || !repoRe.MatchString(x.Playbook.Repo) ||
|
||||
!versionRe.MatchString(x.Kubo) || !versionRe.MatchString(x.Interzone) {
|
||||
return nil, errors.New("index.json: a malformed playbook, kubo or interzone version")
|
||||
}
|
||||
for name, sum := range x.Bin {
|
||||
if !sha256Re.MatchString(sum) || strings.ContainsAny(name, "/\\") {
|
||||
return nil, fmt.Errorf("index.json: bad bin entry %q", name)
|
||||
}
|
||||
}
|
||||
return &x, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package ipfs
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVerify(t *testing.T) {
|
||||
pub, priv, _ := ed25519.GenerateKey(nil)
|
||||
defer func(k []byte) { publicKey = k }(publicKey)
|
||||
publicKey = pub
|
||||
cid := "b" + strings.Repeat("a", 58)
|
||||
good := `{"version":2,"kubo":"v0.43.1","interzone":"v0.5.33",
|
||||
"playbook":{"repo":"spantaleev/matrix-docker-ansible-deploy","commit":"` + strings.Repeat("ab", 20) + `"},
|
||||
"parts":{"packages":"` + cid + `","oci":"` + cid + `","git":"` + cid + `","bin":"` + cid + `"},
|
||||
"bin":{"ipfs-linux-amd64":"` + strings.Repeat("0", 64) + `"}}`
|
||||
sign := func(s string) []byte {
|
||||
return []byte(base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(s))) + "\n")
|
||||
}
|
||||
|
||||
x, err := Verify([]byte(good), sign(good))
|
||||
if err != nil || x.Version != 2 || x.Parts.OCI != cid {
|
||||
t.Fatalf("good index: %v %+v", err, x)
|
||||
}
|
||||
if _, err := Verify([]byte(good+" "), sign(good)); err == nil {
|
||||
t.Error("a changed index passed")
|
||||
}
|
||||
if _, err := Verify([]byte(good), []byte("not base64")); err == nil {
|
||||
t.Error("a bad signature passed")
|
||||
}
|
||||
for _, bad := range []string{
|
||||
strings.Replace(good, `"oci":"`+cid, `"oci":"`+cid+`/../x`, 1),
|
||||
strings.Replace(good, `"kubo":"v0.43.1"`, `"kubo":"v0.43.1; rm -rf /"`, 1),
|
||||
strings.Replace(good, `"ipfs-linux-amd64"`, `"../ipfs"`, 1),
|
||||
} {
|
||||
if _, err := Verify([]byte(bad), sign(bad)); err == nil {
|
||||
t.Errorf("signed but malformed index passed: %.80s", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
/key/swarm/psk/1.0.0/
|
||||
/base16/
|
||||
ab387d1f8678bf53244bf8a66e7d98ebdd688e32c05b1042c13e6eacba514f4a
|
||||
Reference in new issue
Block a user