protondrive: make the golden capture fail closed on foreign keys
The capture names its folder with -capture-remote and refuses -remote, so a mistyped -run can't start the integration tests against the account. Every key written has a placeholder user ID; a node key holding another user's email, or a signature by or intended for a key not being replaced, fails the capture without naming it. Salt notations are written only where the original had one. TestGoldenFixtures now also walks every key, key packet and signature of a fixture, inside encrypted messages too, and checks folder modification times; tampered fixtures are shown to fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
af6fdd7899
commit
1b2fcbd212
2 files changed
+496
-112
No files matched your search
@@ -3,6 +3,7 @@ package protondrive
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
stdcrypto "crypto"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -35,8 +37,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
goldenCaptureDir = flag.String("capture", "", "Capture golden fixtures of the folder -remote names into this directory")
|
||||
goldenCaptureLabel = flag.String("capture-label", "", "Client which wrote the folder -capture captures, such as web or cli")
|
||||
goldenCaptureDir = flag.String("capture", "", "Capture golden fixtures of the folder -capture-remote names into this directory")
|
||||
goldenCaptureRemote = flag.String("capture-remote", "", "Folder to capture golden fixtures of, as NAME:PATH")
|
||||
goldenCaptureLabel = flag.String("capture-label", "", "Client which wrote the folder -capture captures, such as web or cli")
|
||||
)
|
||||
|
||||
// goldenDir holds folder trees written by Proton's own clients, one
|
||||
@@ -104,13 +107,19 @@ func goldenBlockFile(id string, i int) string {
|
||||
}
|
||||
|
||||
// checkCaptureFlags returns whether a capture was asked for, or an
|
||||
// error if its flags are incomplete.
|
||||
func checkCaptureFlags(dir, label, remote string) (bool, error) {
|
||||
// error if its flags are incomplete or -remote is set too: with -remote
|
||||
// set, a mistyped -run would run the integration tests, which write,
|
||||
// against the captured account.
|
||||
func checkCaptureFlags(dir, label, captureRemote, remote string) (bool, error) {
|
||||
switch {
|
||||
case dir == "":
|
||||
case dir == "" && captureRemote == "":
|
||||
return false, nil
|
||||
case remote == "":
|
||||
return false, errors.New("-capture needs -remote NAME:PATH naming the folder to capture")
|
||||
case remote != "":
|
||||
return false, errors.New("-capture must not be used with -remote: name the folder with -capture-remote")
|
||||
case dir == "":
|
||||
return false, errors.New("-capture-remote needs -capture naming the directory to capture into")
|
||||
case captureRemote == "":
|
||||
return false, errors.New("-capture needs -capture-remote NAME:PATH naming the folder to capture")
|
||||
case label == "":
|
||||
return false, errors.New("-capture needs -capture-label naming the client which wrote the folder")
|
||||
case label != filepath.Base(label) || label == "." || label == "..":
|
||||
@@ -119,24 +128,24 @@ func checkCaptureFlags(dir, label, remote string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TestGoldenCapture captures the folder -remote names, written by
|
||||
// another client, into the directory -capture/-capture-label, then
|
||||
// TestGoldenCapture captures the folder -capture-remote names, written
|
||||
// by another client, into the directory -capture/-capture-label, then
|
||||
// reads the fixture back as TestGoldenFixtures does. It contacts the
|
||||
// remote's real server, so it runs only when asked:
|
||||
//
|
||||
// go test ./backend/protondrive/ -v -run TestGoldenCapture -capture DIR -capture-label web -remote NAME:PATH
|
||||
// go test ./backend/protondrive/ -v -run '^TestGoldenCapture$' -capture DIR -capture-label web -capture-remote NAME:PATH
|
||||
func TestGoldenCapture(t *testing.T) {
|
||||
run, err := checkCaptureFlags(*goldenCaptureDir, *goldenCaptureLabel, *fstest.RemoteName)
|
||||
run, err := checkCaptureFlags(*goldenCaptureDir, *goldenCaptureLabel, *goldenCaptureRemote, *fstest.RemoteName)
|
||||
require.NoError(t, err)
|
||||
if !run {
|
||||
t.Skip("set -capture, -capture-label and -remote to capture golden fixtures")
|
||||
t.Skip("set -capture, -capture-label and -capture-remote to capture golden fixtures")
|
||||
}
|
||||
fstest.Initialise()
|
||||
ctx := context.Background()
|
||||
fsys, err := fs.NewFs(ctx, *fstest.RemoteName)
|
||||
fsys, err := fs.NewFs(ctx, *goldenCaptureRemote)
|
||||
require.NoError(t, err)
|
||||
f, ok := fsys.(*Fs)
|
||||
require.True(t, ok, "%s is not a protondrive remote", *fstest.RemoteName)
|
||||
require.True(t, ok, "%s is not a protondrive remote", *goldenCaptureRemote)
|
||||
defer func() { _ = f.Shutdown(ctx) }()
|
||||
out := filepath.Join(*goldenCaptureDir, *goldenCaptureLabel)
|
||||
require.NoError(t, captureGolden(ctx, f, *goldenCaptureLabel, out, goldenMaxFiles, t.Logf))
|
||||
@@ -156,6 +165,7 @@ func TestGoldenFixtures(t *testing.T) {
|
||||
dir := filepath.Dir(p)
|
||||
t.Run(filepath.Base(dir), func(t *testing.T) {
|
||||
keys, fx := loadGolden(t, dir)
|
||||
checkGoldenPackets(t, keys, fx)
|
||||
checkGolden(t, keys, fx)
|
||||
})
|
||||
}
|
||||
@@ -163,16 +173,25 @@ func TestGoldenFixtures(t *testing.T) {
|
||||
|
||||
// TestGoldenCaptureFlags checks a capture runs only with all its flags.
|
||||
func TestGoldenCaptureFlags(t *testing.T) {
|
||||
run, err := checkCaptureFlags("", "", "")
|
||||
run, err := checkCaptureFlags("", "", "", "")
|
||||
assert.False(t, run)
|
||||
assert.NoError(t, err)
|
||||
_, err = checkCaptureFlags("dir", "web", "")
|
||||
assert.ErrorContains(t, err, "-remote")
|
||||
_, err = checkCaptureFlags("dir", "", "remote:path")
|
||||
run, err = checkCaptureFlags("", "", "", "TestProtonDrive:")
|
||||
assert.False(t, run, "-remote alone is for the integration tests")
|
||||
assert.NoError(t, err)
|
||||
_, err = checkCaptureFlags("dir", "web", "", "")
|
||||
assert.ErrorContains(t, err, "-capture-remote")
|
||||
_, err = checkCaptureFlags("", "web", "remote:path", "")
|
||||
assert.ErrorContains(t, err, "needs -capture")
|
||||
_, err = checkCaptureFlags("dir", "web", "remote:path", "remote:path")
|
||||
assert.ErrorContains(t, err, "must not be used with -remote")
|
||||
_, err = checkCaptureFlags("dir", "web", "", "remote:path")
|
||||
assert.ErrorContains(t, err, "must not be used with -remote")
|
||||
_, err = checkCaptureFlags("dir", "", "remote:path", "")
|
||||
assert.ErrorContains(t, err, "-capture-label")
|
||||
_, err = checkCaptureFlags("dir", "../web", "remote:path")
|
||||
_, err = checkCaptureFlags("dir", "../web", "remote:path", "")
|
||||
assert.ErrorContains(t, err, "plain name")
|
||||
run, err = checkCaptureFlags("dir", "web", "remote:path")
|
||||
run, err = checkCaptureFlags("dir", "web", "remote:path", "")
|
||||
assert.True(t, run)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -254,7 +273,14 @@ func TestGoldenRoundTrip(t *testing.T) {
|
||||
assert.Equal(t, "link-5", byName["d anonymous.bin"].ParentID)
|
||||
assert.Contains(t, byName["d anonymous.bin"].Rekeyed, "Name: v4 signature (type 0, SHA-256) by the node key of link-5 made again")
|
||||
|
||||
// Nothing of the source account is in the capture.
|
||||
t.Run("tampered", func(t *testing.T) {
|
||||
testGoldenTampered(t, out)
|
||||
})
|
||||
|
||||
// Nothing of the source account is in the capture: no key, key
|
||||
// packet or signature names a key but the test keys...
|
||||
checkGoldenPackets(t, keys, fx)
|
||||
// ...and no secret, ID or email of the account is in it as text.
|
||||
var all []byte
|
||||
entries, err := os.ReadDir(out)
|
||||
require.NoError(t, err)
|
||||
@@ -271,13 +297,29 @@ func TestGoldenRoundTrip(t *testing.T) {
|
||||
secrets = append(secrets, ids...)
|
||||
secrets = append(secrets, goldenSecretForms([]byte(u.KeyPassword()))...)
|
||||
secrets = append(secrets, srv.IssuedSecrets(protontest.SessionToken, protontest.StorageToken)...)
|
||||
sourceKeys := []string{source.Keys.AddressKey, source.Keys.RootKey}
|
||||
for _, id := range ids {
|
||||
stored := u.StoredFile(id)
|
||||
sourceKeys = append(sourceKeys, stored.Link.NodeKey)
|
||||
secrets = append(secrets, stored.Link.NodeKey, stored.Link.NodePassphrase, stored.Link.NodePassphraseSignature)
|
||||
passphrase, sessionKey := u.NodeSecrets(id)
|
||||
secrets = append(secrets, goldenSecretForms(passphrase)...)
|
||||
secrets = append(secrets, goldenSecretForms(sessionKey)...)
|
||||
}
|
||||
for _, armored := range sourceKeys {
|
||||
key, err := crypto.NewKeyFromArmored(armored)
|
||||
require.NoError(t, err)
|
||||
e := key.GetEntity()
|
||||
pks := []*packet.PublicKey{e.PrimaryKey}
|
||||
for _, sub := range e.Subkeys {
|
||||
pks = append(pks, sub.PublicKey)
|
||||
}
|
||||
for _, pk := range pks {
|
||||
id := fmt.Sprintf("%016x", pk.KeyId)
|
||||
fp := hex.EncodeToString(pk.Fingerprint)
|
||||
secrets = append(secrets, id, strings.ToUpper(id), fp, strings.ToUpper(fp), base64.StdEncoding.EncodeToString(pk.Fingerprint))
|
||||
}
|
||||
}
|
||||
for _, secret := range secrets {
|
||||
require.NotEmpty(t, secret)
|
||||
assert.False(t, bytes.Contains(all, []byte(secret)), "the capture holds %.40q", secret)
|
||||
@@ -318,13 +360,11 @@ func loadGolden(t testing.TB, dir string) (goldenKeys, goldenFixture) {
|
||||
return keys, fx
|
||||
}
|
||||
|
||||
// checkGolden stores the fixture on a fake server for a user holding
|
||||
// its keys and reads every node through the backend, failing on any
|
||||
// signature which doesn't verify. It returns the content of each file
|
||||
// by path.
|
||||
func checkGolden(t *testing.T, keys goldenKeys, fx goldenFixture) map[string][]byte {
|
||||
// serveGolden stores the fixture on a fake server for a user holding
|
||||
// its keys and returns an Fs of that user with ignore_signature_errors
|
||||
// off and the path of each node by placeholder.
|
||||
func serveGolden(t *testing.T, keys goldenKeys, fx goldenFixture) (*Fs, map[string]string) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUserWithKeys(keys.Email, "pass", keys.Keys)
|
||||
ids := map[string]string{"": u.RootID()}
|
||||
@@ -340,39 +380,58 @@ func checkGolden(t *testing.T, keys goldenKeys, fx goldenFixture) map[string][]b
|
||||
t.Fatalf("node %s is neither a folder nor a file", n.ID)
|
||||
}
|
||||
}
|
||||
|
||||
m := sessionConfig(srv, u)
|
||||
m["ignore_signature_errors"] = "false"
|
||||
f := newTestFs(t, m)
|
||||
paths := map[string]string{}
|
||||
children := map[string][]string{"": nil}
|
||||
paths := map[string]string{"": ""}
|
||||
for _, n := range fx.Nodes {
|
||||
p := path.Join(paths[n.ParentID], f.opt.Enc.ToStandardName(n.Want.Name))
|
||||
paths[n.ID] = p
|
||||
children[n.ParentID] = append(children[n.ParentID], p)
|
||||
paths[n.ID] = path.Join(paths[n.ParentID], f.opt.Enc.ToStandardName(n.Want.Name))
|
||||
}
|
||||
return f, paths
|
||||
}
|
||||
|
||||
// checkGolden stores the fixture on a fake server for a user holding
|
||||
// its keys and reads every node through the backend, failing on any
|
||||
// signature which doesn't verify. It returns the content of each file
|
||||
// by path.
|
||||
func checkGolden(t *testing.T, keys goldenKeys, fx goldenFixture) map[string][]byte {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
f, paths := serveGolden(t, keys, fx)
|
||||
children := map[string][]string{}
|
||||
for _, n := range fx.Nodes {
|
||||
children[paths[n.ParentID]] = append(children[paths[n.ParentID]], paths[n.ID])
|
||||
if n.Folder != nil {
|
||||
// An empty folder is listed too.
|
||||
if _, ok := children[paths[n.ID]]; !ok {
|
||||
children[paths[n.ID]] = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// Every folder holding nodes of the fixture is listed, the root
|
||||
// included.
|
||||
listed := map[string]fs.DirEntry{}
|
||||
for dir, want := range children {
|
||||
entries, err := f.List(ctx, dir)
|
||||
require.NoError(t, err, "list %q", dir)
|
||||
var got []string
|
||||
for _, e := range entries {
|
||||
got = append(got, e.Remote())
|
||||
listed[e.Remote()] = e
|
||||
}
|
||||
assert.ElementsMatch(t, want, got, "listing of %q", dir)
|
||||
}
|
||||
read := map[string][]byte{}
|
||||
for _, n := range fx.Nodes {
|
||||
p := paths[n.ID]
|
||||
if n.Folder != nil {
|
||||
entries, err := f.List(ctx, p)
|
||||
require.NoError(t, err, "list %s", p)
|
||||
var got []string
|
||||
for _, e := range entries {
|
||||
got = append(got, e.Remote())
|
||||
if e := listed[p]; assert.NotNil(t, e, "%s listed", p) {
|
||||
assert.True(t, n.Want.ModTime.Equal(e.ModTime(ctx)), "modification time of %s: %v, want %v", p, e.ModTime(ctx), n.Want.ModTime)
|
||||
}
|
||||
assert.ElementsMatch(t, children[n.ID], got, "listing of %s", p)
|
||||
continue
|
||||
}
|
||||
read[p] = checkGoldenFile(ctx, t, f, p, n.Want)
|
||||
}
|
||||
entries, err := f.List(ctx, "")
|
||||
require.NoError(t, err)
|
||||
var got []string
|
||||
for _, e := range entries {
|
||||
got = append(got, e.Remote())
|
||||
}
|
||||
assert.Equal(t, children[""], got, "listing of the root")
|
||||
f.c.mu.Lock()
|
||||
defer f.c.mu.Unlock()
|
||||
assert.Empty(t, f.c.reported, "problems reported")
|
||||
@@ -502,7 +561,7 @@ type goldenCapture struct {
|
||||
// origRoot, the key of the captured folder's parent, with the root
|
||||
// key's ring and its hash key.
|
||||
func (gc *goldenCapture) testKeys(origRoot *crypto.Key) (keys goldenKeys, rootKR *crypto.KeyRing, rootHashKey []byte, err error) {
|
||||
addrKey, err := generateKeyLike(gc.c.addr.keyRing.GetKeys()[0], "golden", goldenEmail)
|
||||
addrKey, err := generateKeyLike(gc.c.addr.keyRing.GetKeys()[0])
|
||||
if err != nil {
|
||||
return keys, nil, nil, err
|
||||
}
|
||||
@@ -511,8 +570,10 @@ func (gc *goldenCapture) testKeys(origRoot *crypto.Key) (keys goldenKeys, rootKR
|
||||
gc.rk.replace(k, addrKey, "address key")
|
||||
}
|
||||
}
|
||||
name, email := gc.rk.identity(origRoot)
|
||||
rootKey, err := generateKeyLike(origRoot, name, email)
|
||||
if err := gc.rk.checkUserIDs(origRoot); err != nil {
|
||||
return keys, nil, nil, err
|
||||
}
|
||||
rootKey, err := generateKeyLike(origRoot)
|
||||
if err != nil {
|
||||
return keys, nil, nil, err
|
||||
}
|
||||
@@ -547,9 +608,14 @@ func lockNewKey(key *crypto.Key) (armored, passphrase string, err error) {
|
||||
return armored, string(pass), err
|
||||
}
|
||||
|
||||
// goldenUserName is the user ID name of every key a capture writes,
|
||||
// with goldenEmail.
|
||||
const goldenUserName = "golden"
|
||||
|
||||
// generateKeyLike returns a new key with the algorithm, version,
|
||||
// creation time and AEAD support of orig, and user ID name and email.
|
||||
func generateKeyLike(orig *crypto.Key, name, email string) (*crypto.Key, error) {
|
||||
// creation time and AEAD support of orig, and the user ID goldenUserName
|
||||
// <goldenEmail>.
|
||||
func generateKeyLike(orig *crypto.Key) (*crypto.Key, error) {
|
||||
pk := orig.GetEntity().PrimaryKey
|
||||
algo := pk.PubKeyAlgo
|
||||
bits, err := pk.BitLength()
|
||||
@@ -569,7 +635,7 @@ func generateKeyLike(orig *crypto.Key, name, email string) (*crypto.Key, error)
|
||||
if sig, _ := orig.GetEntity().PrimarySelfSignature(time.Now(), nil); sig != nil && sig.SEIPDv2 {
|
||||
p.AeadEncryption = &packet.AEADConfig{DefaultMode: packet.AEADModeGCM}
|
||||
}
|
||||
key, err := crypto.PGPWithProfile(p).KeyGeneration().AddUserId(name, email).GenerationTime(pk.CreationTime.Unix()).New().GenerateKey()
|
||||
key, err := crypto.PGPWithProfile(p).KeyGeneration().AddUserId(goldenUserName, goldenEmail).GenerationTime(pk.CreationTime.Unix()).New().GenerateKey()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("key like %s: %w", orig.GetHexKeyID(), err)
|
||||
}
|
||||
@@ -598,8 +664,10 @@ func (gc *goldenCapture) node(ctx context.Context, n *node, origParentKR, parent
|
||||
id := "link-" + strconv.Itoa(len(gc.fixture.Nodes)+1)
|
||||
l := &n.link.Link
|
||||
origKey := n.keys.keyRing.GetKeys()[0]
|
||||
name, email := rk.identity(origKey)
|
||||
newKey, err := generateKeyLike(origKey, name, email)
|
||||
if err := rk.checkUserIDs(origKey); err != nil {
|
||||
return fmt.Errorf("%q: %w", n.Name, err)
|
||||
}
|
||||
newKey, err := generateKeyLike(origKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -612,7 +680,7 @@ func (gc *goldenCapture) node(ctx context.Context, n *node, origParentKR, parent
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rk.note("NodeKey: new key of the same algorithm, version and creation time")
|
||||
rk.note("NodeKey: new key of the same algorithm, version and creation time, user ID %s <%s>", goldenUserName, goldenEmail)
|
||||
stored := protontest.StoredLink{
|
||||
NodeKey: armoredKey,
|
||||
NameHash: l.NameHash,
|
||||
@@ -893,18 +961,27 @@ func (rk *rekeyer) email(email string) (string, error) {
|
||||
return "", fmt.Errorf("signed as %q, which is not the account's: its keys can't be replaced", email)
|
||||
}
|
||||
|
||||
// identity returns the user ID name and email of a key replacing orig,
|
||||
// the account's emails replaced.
|
||||
func (rk *rekeyer) identity(orig *crypto.Key) (name, email string) {
|
||||
_, id := orig.GetEntity().PrimaryIdentity(time.Now(), nil)
|
||||
if id == nil || id.UserId == nil {
|
||||
return nodeKeyUserID, ""
|
||||
// emailPattern matches what looks like an email in a user ID.
|
||||
var emailPattern = regexp.MustCompile(`[^\s<>()"]+@[^\s<>()"]+`)
|
||||
|
||||
// genericKeyEmails are the emails Proton's clients give every node key
|
||||
// in its user ID: the C# SDK (and so the official CLI) and the old Go
|
||||
// library.
|
||||
var genericKeyEmails = []string{"no-reply@proton.me", "noreply@protonmail.com"}
|
||||
|
||||
// checkUserIDs returns an error if a user ID of orig holds an email
|
||||
// other than one of the account's or a generic one: a key of someone
|
||||
// else can't be replaced. Every key written has goldenUserName
|
||||
// <goldenEmail> instead.
|
||||
func (rk *rekeyer) checkUserIDs(orig *crypto.Key) error {
|
||||
for _, id := range orig.GetEntity().Identities {
|
||||
for _, email := range emailPattern.FindAllString(id.Name, -1) {
|
||||
if !rk.owns(email) && !slices.Contains(genericKeyEmails, strings.ToLower(email)) {
|
||||
return errors.New("a node key's user ID holds an email which is not the account's")
|
||||
}
|
||||
}
|
||||
}
|
||||
name, email = id.UserId.Name, id.UserId.Email
|
||||
if email != "" && rk.owns(email) {
|
||||
email = goldenEmail
|
||||
}
|
||||
return name, email
|
||||
return nil
|
||||
}
|
||||
|
||||
// rewrapContentKey returns the base64 content key packet with its
|
||||
@@ -952,48 +1029,15 @@ func (rk *rekeyer) reseal(armored string, fromKR, toKR *crypto.KeyRing, plain []
|
||||
if armored == "" {
|
||||
return "", nil
|
||||
}
|
||||
msg, err := crypto.NewPGPMessageFromArmored(armored)
|
||||
m, err := openMessage(armored, fromKR)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
keyPackets, data := msg.BinaryKeyPacket(), msg.BinaryDataPacket()
|
||||
if n, err := countPackets(keyPackets); err != nil || n != 1 {
|
||||
if n, err := countPackets(m.keyPackets); err != nil || n != 1 {
|
||||
rk.note("%d key packets (%v), one kept", n, err)
|
||||
}
|
||||
dh, err := crypto.PGP().Decryption().DecryptionKeys(fromKR).New()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sk, err := dh.DecryptSessionKey(keyPackets)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
p, err := packet.Read(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
se, ok := p.(*packet.SymmetricallyEncrypted)
|
||||
if !ok || !se.IntegrityProtected {
|
||||
return "", fmt.Errorf("data packet %T is not SEIPD", p)
|
||||
}
|
||||
cipherFunc := se.Cipher
|
||||
if se.Version == 1 {
|
||||
if cipherFunc, err = sk.GetCipherFunc(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
rc, err := se.Decrypt(cipherFunc, sk.Key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
inner, err := io.ReadAll(rc)
|
||||
if closeErr := rc.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
inner, signatures, err := rk.resignPackets(inner, plain)
|
||||
sk, se, data, cipherFunc := m.sk, m.se, m.data, m.cipherFunc
|
||||
inner, signatures, err := rk.resignPackets(m.inner, plain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -1033,6 +1077,57 @@ func (rk *rekeyer) reseal(armored string, fromKR, toKR *crypto.KeyRing, plain []
|
||||
return armorMessage(out.Bytes(), sk)
|
||||
}
|
||||
|
||||
// openedMessage is an armored message decrypted down to its inner
|
||||
// packets.
|
||||
type openedMessage struct {
|
||||
keyPackets []byte // the packets before the data packet
|
||||
data []byte // the data packet
|
||||
sk *crypto.SessionKey
|
||||
se *packet.SymmetricallyEncrypted
|
||||
cipherFunc packet.CipherFunction
|
||||
inner []byte // the decrypted packets of the data packet
|
||||
}
|
||||
|
||||
// openMessage decrypts the armored message, which must be SEIPD, with
|
||||
// kr.
|
||||
func openMessage(armored string, kr *crypto.KeyRing) (*openedMessage, error) {
|
||||
msg, err := crypto.NewPGPMessageFromArmored(armored)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := &openedMessage{keyPackets: msg.BinaryKeyPacket(), data: msg.BinaryDataPacket()}
|
||||
dh, err := crypto.PGP().Decryption().DecryptionKeys(kr).New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m.sk, err = dh.DecryptSessionKey(m.keyPackets); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := packet.Read(bytes.NewReader(m.data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
se, ok := p.(*packet.SymmetricallyEncrypted)
|
||||
if !ok || !se.IntegrityProtected {
|
||||
return nil, fmt.Errorf("data packet %T is not SEIPD", p)
|
||||
}
|
||||
m.se, m.cipherFunc = se, se.Cipher
|
||||
if se.Version == 1 {
|
||||
if m.cipherFunc, err = m.sk.GetCipherFunc(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rc, err := se.Decrypt(m.cipherFunc, m.sk.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.inner, err = io.ReadAll(rc)
|
||||
if closeErr := rc.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
|
||||
// countPackets returns the number of packets in data.
|
||||
func countPackets(data []byte) (int, error) {
|
||||
r := packet.NewOpaqueReader(bytes.NewReader(data))
|
||||
@@ -1136,8 +1231,8 @@ func (rk *rekeyer) resignPackets(inner, plain []byte) ([]byte, int, error) {
|
||||
if len(sigs) > 0 && literal == nil {
|
||||
return nil, 0, errors.New("signature without literal data")
|
||||
}
|
||||
if len(ops) > len(sigs) {
|
||||
return nil, 0, errors.New("one-pass signature without its signature")
|
||||
if len(ops) != 0 && len(ops) != len(sigs) {
|
||||
return nil, 0, errors.New("one-pass signatures mixed with signatures without one: not supported")
|
||||
}
|
||||
newSigs := make([]*packet.Signature, len(sigs))
|
||||
for i, sig := range sigs {
|
||||
@@ -1227,15 +1322,17 @@ func (rk *rekeyer) resignDetached(armoredSig string, data []byte) (string, error
|
||||
}
|
||||
|
||||
// resignSignature returns orig made again over data by the key
|
||||
// replacing its signer, with orig's type, hash, creation time,
|
||||
// lifetime and notations, and its intended recipients replaced.
|
||||
// replacing its signer, with orig's type, hash, creation time, lifetime
|
||||
// and notations (a salt notation with a new salt), and its intended
|
||||
// recipients replaced. An intended recipient which is not a key being
|
||||
// replaced is an error, so no other key is named in a fixture.
|
||||
func (rk *rekeyer) resignSignature(orig *packet.Signature, data []byte) (*packet.Signature, error) {
|
||||
if orig.IssuerKeyId == nil {
|
||||
return nil, errors.New("signature without issuer")
|
||||
}
|
||||
signer, ok := rk.signers[*orig.IssuerKeyId]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("signature by unknown key %016X", *orig.IssuerKeyId)
|
||||
return nil, errors.New("signature by a key which is not one being replaced")
|
||||
}
|
||||
priv := signer.key.GetEntity().PrivateKey
|
||||
sig := &packet.Signature{
|
||||
@@ -1245,9 +1342,18 @@ func (rk *rekeyer) resignSignature(orig *packet.Signature, data []byte) (*packet
|
||||
Hash: orig.Hash,
|
||||
CreationTime: orig.CreationTime,
|
||||
SigLifetimeSecs: orig.SigLifetimeSecs,
|
||||
Notations: orig.Notations,
|
||||
IssuerKeyId: &priv.KeyId,
|
||||
}
|
||||
// Signing adds a salt notation unless told not to, so it is there
|
||||
// only if it was.
|
||||
salted := false
|
||||
for _, n := range orig.Notations {
|
||||
if n.Name == packet.SaltNotationName {
|
||||
salted = true
|
||||
} else {
|
||||
sig.Notations = append(sig.Notations, n)
|
||||
}
|
||||
}
|
||||
if orig.SignerUserId != nil {
|
||||
email := goldenEmail
|
||||
sig.SignerUserId = &email
|
||||
@@ -1255,8 +1361,7 @@ func (rk *rekeyer) resignSignature(orig *packet.Signature, data []byte) (*packet
|
||||
for _, r := range orig.IntendedRecipients {
|
||||
fp, ok := rk.fingerprints[string(r.Fingerprint)]
|
||||
if !ok {
|
||||
rk.note("intended recipient %X, an unknown key, left out", r.Fingerprint)
|
||||
continue
|
||||
return nil, errors.New("signature intended for a key which is not one being replaced")
|
||||
}
|
||||
sig.IntendedRecipients = append(sig.IntendedRecipients, &packet.Recipient{KeyVersion: r.KeyVersion, Fingerprint: fp})
|
||||
}
|
||||
@@ -1272,7 +1377,7 @@ func (rk *rekeyer) resignSignature(orig *packet.Signature, data []byte) (*packet
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := sig.Sign(h, priv, nil); err != nil {
|
||||
if err := sig.Sign(h, priv, &packet.Config{NonDeterministicSignaturesViaNotation: &salted}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rk.note("v%d signature (type %d, %s) by the %s made again", orig.Version, orig.SigType, orig.Hash, signer.role)
|
||||
@@ -1281,3 +1386,255 @@ func (rk *rekeyer) resignSignature(orig *packet.Signature, data []byte) (*packet
|
||||
}
|
||||
return sig, nil
|
||||
}
|
||||
|
||||
// checkGoldenPackets checks at packet level that every key, key packet
|
||||
// and signature of the fixture, those inside encrypted messages too,
|
||||
// names only its test keys and every user ID is the placeholder.
|
||||
func checkGoldenPackets(t *testing.T, keys goldenKeys, fx goldenFixture) {
|
||||
t.Helper()
|
||||
known := map[uint64]bool{}
|
||||
fingerprints := map[string]bool{}
|
||||
addKey := func(armored string, passphrase []byte) *crypto.KeyRing {
|
||||
t.Helper()
|
||||
key, err := crypto.NewKeyFromArmored(armored)
|
||||
require.NoError(t, err)
|
||||
e := key.GetEntity()
|
||||
known[e.PrimaryKey.KeyId], fingerprints[string(e.PrimaryKey.Fingerprint)] = true, true
|
||||
for _, sub := range e.Subkeys {
|
||||
known[sub.PublicKey.KeyId], fingerprints[string(sub.PublicKey.Fingerprint)] = true, true
|
||||
}
|
||||
for _, id := range e.Identities {
|
||||
assert.Equal(t, goldenUserName+" <"+goldenEmail+">", id.Name, "user ID")
|
||||
}
|
||||
if passphrase == nil {
|
||||
return nil
|
||||
}
|
||||
unlocked, err := key.Unlock(passphrase)
|
||||
require.NoError(t, err)
|
||||
kr, err := crypto.NewKeyRing(unlocked)
|
||||
require.NoError(t, err)
|
||||
return kr
|
||||
}
|
||||
checkSig := func(sig *packet.Signature, what string) {
|
||||
t.Helper()
|
||||
require.NotNil(t, sig.IssuerKeyId, "%s: issuer", what)
|
||||
assert.True(t, known[*sig.IssuerKeyId], "%s: issuer %016X is not a test key", what, *sig.IssuerKeyId)
|
||||
if sig.IssuerFingerprint != nil {
|
||||
assert.True(t, fingerprints[string(sig.IssuerFingerprint)], "%s: issuer fingerprint is not a test key's", what)
|
||||
}
|
||||
for _, r := range sig.IntendedRecipients {
|
||||
assert.True(t, fingerprints[string(r.Fingerprint)], "%s: intended recipient is not a test key", what)
|
||||
}
|
||||
if sig.SignerUserId != nil {
|
||||
assert.Equal(t, goldenEmail, *sig.SignerUserId, "%s: signer user ID", what)
|
||||
}
|
||||
}
|
||||
var checkPackets func(data []byte, what string)
|
||||
checkPackets = func(data []byte, what string) {
|
||||
t.Helper()
|
||||
r := packet.NewReader(bytes.NewReader(data))
|
||||
for {
|
||||
p, err := r.Next()
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
require.NoError(t, err, what)
|
||||
switch p := p.(type) {
|
||||
case *packet.EncryptedKey:
|
||||
if p.Version == 6 {
|
||||
assert.True(t, fingerprints[string(p.KeyFingerprint)], "%s: key packet for a key which is not a test key", what)
|
||||
} else {
|
||||
assert.True(t, known[p.KeyId], "%s: key packet for %016X, not a test key", what, p.KeyId)
|
||||
}
|
||||
case *packet.Signature:
|
||||
checkSig(p, what)
|
||||
case *packet.OnePassSignature:
|
||||
assert.True(t, known[p.KeyId], "%s: one-pass signature by %016X, not a test key", what, p.KeyId)
|
||||
case *packet.Compressed:
|
||||
body, err := io.ReadAll(p.Body)
|
||||
require.NoError(t, err, what)
|
||||
checkPackets(body, what)
|
||||
case *packet.LiteralData:
|
||||
_, err := io.Copy(io.Discard, p.Body)
|
||||
require.NoError(t, err, what)
|
||||
case *packet.SymmetricallyEncrypted:
|
||||
// The data packet of a message, checked decrypted.
|
||||
default:
|
||||
t.Errorf("%s: unexpected %T", what, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
checkMessage := func(armored string, kr *crypto.KeyRing, what string) {
|
||||
t.Helper()
|
||||
if armored == "" {
|
||||
return
|
||||
}
|
||||
m, err := openMessage(armored, kr)
|
||||
require.NoError(t, err, what)
|
||||
checkPackets(m.keyPackets, what)
|
||||
checkPackets(m.inner, what)
|
||||
}
|
||||
checkDetached := func(armored, what string) {
|
||||
t.Helper()
|
||||
if armored == "" {
|
||||
return
|
||||
}
|
||||
raw, err := armor.Unarmor(armored)
|
||||
require.NoError(t, err, what)
|
||||
checkPackets(raw, what)
|
||||
}
|
||||
|
||||
assert.Equal(t, goldenEmail, keys.Email)
|
||||
addKey(keys.Keys.AddressKey, []byte(keys.Keys.AddressKeyPassphrase))
|
||||
rootKR := addKey(keys.Keys.RootKey, []byte(keys.Keys.RootKeyPassphrase))
|
||||
checkMessage(keys.Keys.RootHashKey, rootKR, "root hash key")
|
||||
// Every node key is known before any signature is checked, as an
|
||||
// anonymous node is signed by its parent's.
|
||||
for _, n := range fx.Nodes {
|
||||
link := goldenLink(n)
|
||||
addKey(link.NodeKey, nil)
|
||||
}
|
||||
krs := map[string]*crypto.KeyRing{"": rootKR}
|
||||
for _, n := range fx.Nodes {
|
||||
link := goldenLink(n)
|
||||
parentKR := krs[n.ParentID]
|
||||
require.NotNil(t, parentKR, "parent of %s", n.ID)
|
||||
what := n.ID + " "
|
||||
dh, err := crypto.PGP().Decryption().DecryptionKeys(parentKR).New()
|
||||
require.NoError(t, err)
|
||||
dec, err := dh.Decrypt([]byte(link.NodePassphrase), crypto.Armor)
|
||||
require.NoError(t, err, what+"passphrase")
|
||||
kr := addKey(link.NodeKey, dec.Bytes())
|
||||
krs[n.ID] = kr
|
||||
for _, s := range []struct{ armored, what string }{{link.NodePassphrase, "passphrase"}, {link.Name, "name"}} {
|
||||
checkMessage(s.armored, parentKR, what+s.what)
|
||||
}
|
||||
checkDetached(link.NodePassphraseSignature, what+"passphrase signature")
|
||||
for _, email := range []string{link.SignatureEmail, link.NameSignatureEmail} {
|
||||
assert.Contains(t, []string{"", goldenEmail}, email, what+"signature email")
|
||||
}
|
||||
if n.Folder != nil {
|
||||
checkMessage(n.Folder.HashKey, kr, what+"hash key")
|
||||
checkMessage(n.Folder.XAttr, kr, what+"extended attributes")
|
||||
continue
|
||||
}
|
||||
keyPacket, err := base64.StdEncoding.DecodeString(n.File.Link.ContentKeyPacket)
|
||||
require.NoError(t, err)
|
||||
checkPackets(keyPacket, what+"content key packet")
|
||||
checkDetached(n.File.Link.ContentKeyPacketSignature, what+"content key signature")
|
||||
checkMessage(n.File.Revision.XAttr, kr, what+"extended attributes")
|
||||
checkDetached(n.File.Revision.ManifestSignature, what+"manifest signature")
|
||||
assert.Contains(t, []string{"", goldenEmail}, n.File.Revision.SignatureEmail, what+"revision signature email")
|
||||
for i, b := range n.File.Revision.Blocks {
|
||||
assert.Empty(t, b.EncSignature, "%sblock %d signature", what, i+1)
|
||||
assert.Empty(t, b.SignatureEmail, "%sblock %d signature email", what, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// goldenLink returns the link of n.
|
||||
func goldenLink(n goldenNode) protontest.StoredLink {
|
||||
if n.Folder != nil {
|
||||
return n.Folder.Link
|
||||
}
|
||||
return n.File.Link
|
||||
}
|
||||
|
||||
// testGoldenTampered checks that reading the fixture in dir fails once
|
||||
// a byte of a block, a name or a signature is changed.
|
||||
func testGoldenTampered(t *testing.T, dir string) {
|
||||
ctx := context.Background()
|
||||
// flip returns the armored text with a byte near the end of its
|
||||
// last packet changed.
|
||||
flip := func(t *testing.T, armored, header string) string {
|
||||
raw, err := armor.Unarmor(armored)
|
||||
require.NoError(t, err)
|
||||
raw[len(raw)-3] ^= 0x40
|
||||
out, err := armor.ArmorWithType(raw, header)
|
||||
require.NoError(t, err)
|
||||
return out
|
||||
}
|
||||
firstFile := func(fx goldenFixture) *goldenNode {
|
||||
for i := range fx.Nodes {
|
||||
if fx.Nodes[i].File != nil {
|
||||
return &fx.Nodes[i]
|
||||
}
|
||||
}
|
||||
t.Fatal("no file in the fixture")
|
||||
return nil
|
||||
}
|
||||
read := func(t *testing.T, keys goldenKeys, fx goldenFixture, n *goldenNode) error {
|
||||
f, paths := serveGolden(t, keys, fx)
|
||||
o, err := f.NewObject(ctx, paths[n.ID])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rc, err := o.Open(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = rc.Close() }()
|
||||
_, err = io.ReadAll(rc)
|
||||
return err
|
||||
}
|
||||
t.Run("block", func(t *testing.T) {
|
||||
keys, fx := loadGolden(t, dir)
|
||||
n := firstFile(fx)
|
||||
n.File.Revision.Blocks[0].Data[100] ^= 1
|
||||
assert.Error(t, read(t, keys, fx, n))
|
||||
})
|
||||
t.Run("manifest signature", func(t *testing.T) {
|
||||
keys, fx := loadGolden(t, dir)
|
||||
n := firstFile(fx)
|
||||
n.File.Revision.ManifestSignature = flip(t, n.File.Revision.ManifestSignature, constants.PGPSignatureHeader)
|
||||
assert.ErrorContains(t, read(t, keys, fx, n), "manifest")
|
||||
})
|
||||
t.Run("name", func(t *testing.T) {
|
||||
keys, fx := loadGolden(t, dir)
|
||||
n := firstFile(fx)
|
||||
n.File.Link.Name = flip(t, n.File.Link.Name, constants.PGPMessageHeader)
|
||||
assert.Error(t, read(t, keys, fx, n))
|
||||
})
|
||||
}
|
||||
|
||||
// TestGoldenRefusesForeignKeys checks a capture fails, naming no key,
|
||||
// on a node key holding someone else's email or a signature intended
|
||||
// for a key not being replaced.
|
||||
func TestGoldenRefusesForeignKeys(t *testing.T) {
|
||||
rk := &rekeyer{signers: map[uint64]rekeyedKey{}, fingerprints: map[string][]byte{}, owns: func(email string) bool { return email == "me@proton.test" }}
|
||||
keyFor := func(name, email string) *crypto.Key {
|
||||
key, err := crypto.PGP().KeyGeneration().AddUserId(name, email).New().GenerateKey()
|
||||
require.NoError(t, err)
|
||||
return key
|
||||
}
|
||||
assert.NoError(t, rk.checkUserIDs(keyFor("Drive key", "no-reply@proton.me")))
|
||||
assert.NoError(t, rk.checkUserIDs(keyFor("me@proton.test", "me@proton.test")))
|
||||
assert.ErrorContains(t, rk.checkUserIDs(keyFor("Drive key", "other@proton.test")), "not the account's")
|
||||
err := rk.checkUserIDs(keyFor("other@proton.test", "me@proton.test"))
|
||||
require.Error(t, err)
|
||||
assert.NotContains(t, err.Error(), "other@")
|
||||
|
||||
orig, repl, stranger := keyFor("a", ""), keyFor("b", ""), keyFor("c", "")
|
||||
rk.replace(orig, repl, "node key")
|
||||
priv := orig.GetEntity().PrivateKey
|
||||
sign := func(recipient *crypto.Key) *packet.Signature {
|
||||
sig := &packet.Signature{
|
||||
Version: 4, SigType: packet.SigTypeBinary, PubKeyAlgo: priv.PubKeyAlgo, Hash: stdcrypto.SHA256,
|
||||
CreationTime: time.Now(), IssuerKeyId: &priv.KeyId,
|
||||
IntendedRecipients: []*packet.Recipient{{KeyVersion: 4, Fingerprint: recipient.GetEntity().PrimaryKey.Fingerprint}},
|
||||
}
|
||||
h, err := sig.PrepareSign(nil)
|
||||
require.NoError(t, err)
|
||||
h.Write([]byte("data"))
|
||||
require.NoError(t, sig.Sign(h, priv, nil))
|
||||
return sig
|
||||
}
|
||||
resigned, err := rk.resignSignature(sign(orig), []byte("data"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, repl.GetEntity().PrimaryKey.Fingerprint, resigned.IntendedRecipients[0].Fingerprint)
|
||||
_, err = rk.resignSignature(sign(stranger), []byte("data"))
|
||||
require.ErrorContains(t, err, "not one being replaced")
|
||||
fp := stranger.GetEntity().PrimaryKey.Fingerprint
|
||||
assert.NotContains(t, strings.ToLower(err.Error()), hex.EncodeToString(fp))
|
||||
assert.NotContains(t, strings.ToLower(strings.Join(rk.notes, "\n")), hex.EncodeToString(fp), "the foreign key is not recorded")
|
||||
}
|
||||
@@ -32,6 +32,33 @@ elsewhere takes about 7 s longer to fail. A lag longer than 7 s still fails the
|
||||
now with an error saying it was committed. See `docs/TODO.md` for the
|
||||
read-after-write spots not yet tolerant.
|
||||
|
||||
## 2026-11-12 - Proton Drive golden captures fail closed
|
||||
|
||||
**Status:** accepted; amends "Proton Drive golden fixtures are re-keyed
|
||||
captures" below
|
||||
**Context:** Review found the capture could copy account data into a
|
||||
fixture: an intended-recipient fingerprint of a key not being replaced (in
|
||||
the notes), and the user ID of a node key. It also reused `-remote`, so a
|
||||
mistyped `-run` would start the writing integration suite against the
|
||||
account.
|
||||
**Decision:** The capture names its folder with `-capture-remote` and
|
||||
refuses to run with `-remote` set. Every key written has the user ID
|
||||
`golden <golden@example.invalid>`; a node key whose user ID holds an email
|
||||
other than the account's or the generic ones Proton's clients use
|
||||
(`no-reply@proton.me`, `noreply@protonmail.com`) fails the capture. A
|
||||
signature by, or intended for, a key not being replaced fails the capture,
|
||||
and the error names no key. A salt notation is written only where the
|
||||
original had one (with a new salt). `TestGoldenFixtures` also walks every
|
||||
key, key packet and signature, inside encrypted messages too, and fails
|
||||
on any key ID, fingerprint or user ID not of the test keys.
|
||||
**Consequences:** What the committed fixtures expose, by design: with the
|
||||
test keys in `keys.json`, anyone can decrypt the captured names, extended
|
||||
attributes (as the client wrote them: check them for camera or location
|
||||
fields before committing), literal-packet file names and dates, folder
|
||||
hash keys (so whether a guessed name exists in those real folders can be
|
||||
tested) and file contents; and the address key's creation time is kept.
|
||||
Node passphrases and the account's keys are not exposed.
|
||||
|
||||
## 2026-11-12 - Proton Drive golden fixtures are re-keyed captures
|
||||
|
||||
**Status:** accepted
|
||||
|
||||
Reference in new issue
Block a user