feat(masterkey): decrypt Chromium v12 portal keys (#707)

This commit is contained in:
Roger authored and GitHub committed 2026-11-20 10:10:32 +08:00
1 parent 066dd15be1
commit fe8a1bbabb
11 files changed
+398 -27

No files matched your search

+2
View File
@@ -15,6 +15,7 @@ type Version string
const (
V10 Version = "v10"
V11 Version = "v11"
V12 Version = "v12"
V20 Version = "v20"
)
@@ -36,6 +37,7 @@ var errStorageNotFound = errors.New("not found in credential store") //nolint:un
type Locator struct {
LocalState string // the copied Local State; "" when the user data dir has none
SecretLabel string // macOS Keychain account / Linux D-Bus Secret Service label
PortalAppID string // Linux: the XDG portal app id whose v12 secret decrypts this dir; "" = no v12
Executable string
ExecutableFallbacks []string
ABE ABEKind
+133 -13
View File
@@ -5,6 +5,7 @@ package masterkey
import (
"context"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"strings"
@@ -12,6 +13,7 @@ import (
"github.com/godbus/dbus/v5"
"github.com/moond4rk/hackbrowserdata/internal/crypto"
"github.com/moond4rk/hackbrowserdata/internal/log"
)
@@ -23,8 +25,13 @@ var linuxParams = pbkdf2Params{
hashFunc: sha1.New,
}
// platformRetrievers run both: a profile holds v10 and v11 values once its host moved between headless and keyring.
var platformRetrievers = []keyRetriever{posixRetriever{}, dbusRetriever{}}
// portalKey derives the v12 key from the XDG portal app secret, matching Chromium's SecretPortalKeyProvider.
func portalKey(secret []byte) []byte {
return crypto.HKDFKey(secret, []byte("fdo_portal_secret_salt"), []byte("HKDF-SHA-256 AES-256-GCM"), 32, sha256.New)
}
// platformRetrievers run in order: v10 headless, v11 keyring, v12 XDG portal; a profile can hold values of each.
var platformRetrievers = []keyRetriever{posixRetriever{}, dbusRetriever{}, portalRetriever{}}
type dbusRetriever struct{}
@@ -59,12 +66,14 @@ func (dbusRetriever) Retrieve(ctx context.Context, locator Locator) ([]byte, err
return linuxParams.deriveKey(secret), nil
}
// secretStore is what findStorageSecret needs from the Secret Service, so tests can stand in for D-Bus.
// secretStore is what the lookups need from the Secret Service, so tests can stand in for D-Bus. itemSecrets reads a
// batch in one call, the only way to read gnome-keyring's portal items, whose D-Bus objects it never exports.
type secretStore interface {
searchItems(ctx context.Context, attributes map[string]string) (unlocked, locked []dbus.ObjectPath, err error)
unlock(ctx context.Context, items []dbus.ObjectPath) ([]dbus.ObjectPath, error)
itemLabel(ctx context.Context, item dbus.ObjectPath) (string, error)
itemSecret(ctx context.Context, item dbus.ObjectPath) ([]byte, error)
itemSecrets(ctx context.Context, items []dbus.ObjectPath) (map[dbus.ObjectPath][]byte, error)
}
// searchQueries find the storage item by libsecret's schema (GNOME), KWallet's schema, and KWallet's entry-name
@@ -103,18 +112,10 @@ func findStorageSecret(ctx context.Context, store secretStore, storage string) (
// searchStorageSecret relies on SearchItems, which resolves only live items, and reads each match on its own, so a
// stale path that fails is skipped (a whole-collection read aborts on it instead).
func searchStorageSecret(ctx context.Context, store secretStore, attributes map[string]string, storage string) ([]byte, error) {
items, locked, err := store.searchItems(ctx, attributes)
items, stillLocked, err := searchAndUnlock(ctx, store, attributes)
if err != nil {
return nil, err
}
var opened []dbus.ObjectPath
if len(locked) > 0 {
opened, err = store.unlock(ctx, locked)
if err != nil {
log.Debugf("secret service: %v", err)
}
items = append(items, opened...)
}
for _, item := range items {
label, err := store.itemLabel(ctx, item)
if err != nil {
@@ -133,7 +134,79 @@ func searchStorageSecret(ctx context.Context, store secretStore, attributes map[
return secret, nil
}
}
if len(opened) < len(locked) {
if stillLocked {
return nil, errKeyringLocked
}
return nil, errStorageNotFound
}
// searchAndUnlock resolves a query to live item paths, unlocking any that open without a prompt; stillLocked is set
// when a locked item stayed locked, which the callers report as errKeyringLocked rather than as not found.
func searchAndUnlock(ctx context.Context, store secretStore, attributes map[string]string) ([]dbus.ObjectPath, bool, error) {
unlocked, locked, err := store.searchItems(ctx, attributes)
if err != nil {
return nil, false, err
}
if len(locked) == 0 {
return unlocked, false, nil
}
opened, err := store.unlock(ctx, locked)
if err != nil {
log.Debugf("secret service: %v", err)
}
return append(unlocked, opened...), len(opened) < len(locked), nil
}
// findPortalSecret looks up the XDG portal app secret by its app id; a secret behind a wallet that stays locked reads
// as locked, and no match steps aside so a profile whose browser never wrote a v12 value gets no error row.
func findPortalSecret(ctx context.Context, store secretStore, appID string) ([]byte, error) {
locked := false
for _, attributes := range portalQueries(appID) {
secret, err := searchPortalSecret(ctx, store, attributes)
switch {
case err == nil:
return secret, nil
case errors.Is(err, errKeyringLocked):
locked = true
case !errors.Is(err, errStorageNotFound):
log.Debugf("portal secret %v: %v", attributes, err)
}
}
switch {
case ctx.Err() != nil:
return nil, fmt.Errorf("portal secret lookup: %w", ctx.Err())
case locked:
return nil, errKeyringLocked
}
return nil, errStorageNotFound
}
// portalQueries find the app secret by gnome-keyring's portal schema and app_id, then by ksecretd's synthesized
// folder and entry, which carry no schema.
func portalQueries(appID string) []map[string]string {
return []map[string]string{
{"xdg:schema": "org.freedesktop.portal.Secret", "app_id": appID},
{"server": "xdg-desktop-portal", "user": appID},
}
}
// searchPortalSecret reads matches with GetSecrets, the one call both gnome-keyring (whose portal item objects are
// never exported) and ksecretd answer; GetSecrets is mandatory in the Secret Service API, so there is no fallback.
func searchPortalSecret(ctx context.Context, store secretStore, attributes map[string]string) ([]byte, error) {
items, stillLocked, err := searchAndUnlock(ctx, store, attributes)
if err != nil {
return nil, err
}
secrets, err := store.itemSecrets(ctx, items)
if err != nil {
return nil, err
}
for _, item := range items {
if secret := secrets[item]; len(secret) > 0 {
return secret, nil
}
}
if stillLocked {
return nil, errKeyringLocked
}
return nil, errStorageNotFound
@@ -223,6 +296,23 @@ func (s *secretService) itemSecret(ctx context.Context, item dbus.ObjectPath) ([
return reply.Value, nil
}
// itemSecrets reads several items in one call; gnome-keyring answers here for portal items it never exports as objects.
func (s *secretService) itemSecrets(ctx context.Context, items []dbus.ObjectPath) (map[dbus.ObjectPath][]byte, error) {
if len(items) == 0 {
return nil, nil
}
call := s.object.CallWithContext(ctx, secretServiceInterface+".GetSecrets", 0, items, s.session)
var replies map[dbus.ObjectPath]dbusSecret
if err := call.Store(&replies); err != nil {
return nil, fmt.Errorf("get secrets: %w", err)
}
values := make(map[dbus.ObjectPath][]byte, len(replies))
for path, reply := range replies {
values[path] = reply.Value
}
return values, nil
}
// dbusSecret is the org.freedesktop.Secret.Item.GetSecret reply; a plain session leaves Value unencrypted.
type dbusSecret struct {
Session dbus.ObjectPath
@@ -231,6 +321,36 @@ type dbusSecret struct {
ContentType string
}
// portalRetriever reads the XDG desktop portal app secret (Chromium's SecretPortalKeyProvider, the v12 scheme). It
// reads only a secret the portal already stored; it never calls RetrieveSecret, which would create one when missing.
type portalRetriever struct{}
func (portalRetriever) Version() Version { return V12 }
func (portalRetriever) Retrieve(ctx context.Context, locator Locator) ([]byte, error) {
if locator.PortalAppID == "" {
return nil, errNotApplicable
}
ctx, cancel := context.WithTimeout(ctx, secretCallTimeout)
defer cancel()
service, err := newSecretService(ctx)
if err != nil {
log.Debugf("portal secret: %v", err)
return nil, errNotApplicable
}
defer service.Close()
secret, err := findPortalSecret(ctx, service, locator.PortalAppID)
switch {
case errors.Is(err, errStorageNotFound):
return nil, errNotApplicable
case err != nil:
return nil, err
}
return portalKey(secret), nil
}
// posixRetriever covers hosts without a keyring (headless, Docker, CI), mirroring Chromium's PosixKeyProvider.
type posixRetriever struct{}
+88 -8
View File
@@ -4,6 +4,7 @@ package masterkey
import (
"context"
"encoding/hex"
"errors"
"strconv"
"testing"
@@ -48,11 +49,13 @@ func TestPosixRetriever_MatchesChromiumKV10Key(t *testing.T) {
}
func TestPlatformRetrievers_Linux(t *testing.T) {
require.Len(t, platformRetrievers, 2)
require.Len(t, platformRetrievers, 3)
assert.IsType(t, posixRetriever{}, platformRetrievers[0], "v10 is the peanuts kV10Key")
assert.IsType(t, dbusRetriever{}, platformRetrievers[1], "v11 is the D-Bus keyring kV11Key")
assert.IsType(t, portalRetriever{}, platformRetrievers[2], "v12 is the XDG portal app key")
assert.Equal(t, V10, platformRetrievers[0].Version())
assert.Equal(t, V11, platformRetrievers[1].Version())
assert.Equal(t, V12, platformRetrievers[2].Version())
}
func TestLabelMatches(t *testing.T) {
@@ -139,6 +142,71 @@ func TestFindStorageSecret(t *testing.T) {
}
}
func TestFindPortalSecret(t *testing.T) {
const appID = "com.google.Chrome"
gnome := map[string]string{"xdg:schema": "org.freedesktop.portal.Secret", "app_id": appID}
kde := map[string]string{"server": "xdg-desktop-portal", "user": appID}
tests := []struct {
name string
store fakeSecretStore
appID string
want string
wantErr error
}{
{
// gnome-keyring never exports the object, so a per-item read fails (stale); only GetSecrets returns it.
name: "gnome portal item read via GetSecrets",
store: fakeSecretStore{{attributes: gnome, secret: "gnome", stale: true, batchReadable: true}},
appID: appID,
want: "gnome",
},
{
name: "kde portal item read via GetSecrets",
store: fakeSecretStore{{attributes: kde, label: "xdg-desktop-portal/" + appID, secret: "kde", batchReadable: true}},
appID: appID,
want: "kde",
},
{
name: "portal item that stays locked reads as locked",
store: fakeSecretStore{{attributes: gnome, secret: "hidden", stale: true, batchReadable: true, locked: true}},
appID: appID,
wantErr: errKeyringLocked,
},
{
name: "no matching app id is not found",
store: fakeSecretStore{{attributes: gnome, secret: "x", stale: true, batchReadable: true}},
appID: "org.chromium.Chromium",
wantErr: errStorageNotFound,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
secret, err := findPortalSecret(context.Background(), testCase.store, testCase.appID)
if testCase.wantErr != nil {
require.ErrorIs(t, err, testCase.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, testCase.want, string(secret))
})
}
}
// TestPortalKey_Vector pins the v12 key derivation to an independently computed HKDF-SHA256 output.
func TestPortalKey_Vector(t *testing.T) {
secret := make([]byte, 64)
for i := range secret {
secret[i] = byte(i)
}
want := "39ba5df998f8f1c032fac27b67d7359f8feda53482637745b35e4778295d32e7"
assert.Equal(t, want, hex.EncodeToString(portalKey(secret)))
}
func TestPortalRetriever_StepsAside(t *testing.T) {
_, err := portalRetriever{}.Retrieve(context.Background(), Locator{})
require.ErrorIs(t, err, errNotApplicable, "no portal app id means no v12, so the retriever must step aside")
}
func TestFindStorageSecret_Timeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
@@ -149,14 +217,16 @@ func TestFindStorageSecret_Timeout(t *testing.T) {
assert.NotErrorIs(t, err, errStorageNotFound, "a lookup that ran out of time must not read as not found")
}
// fakeItem is one Secret Service item: a locked one reads only once unlocked, and a stale one never does.
// fakeItem is one Secret Service item: a locked one reads only once unlocked, and a stale one never does. A
// batchReadable item answers GetSecrets but not a per-item read, as gnome-keyring's unexported portal items do.
type fakeItem struct {
attributes map[string]string
label string
secret string
locked bool
unlockable bool
stale bool
attributes map[string]string
label string
secret string
locked bool
unlockable bool
stale bool
batchReadable bool
}
type fakeSecretStore []fakeItem
@@ -223,6 +293,16 @@ func (s fakeSecretStore) itemSecret(_ context.Context, path dbus.ObjectPath) ([]
return []byte(item.secret), nil
}
func (s fakeSecretStore) itemSecrets(_ context.Context, items []dbus.ObjectPath) (map[dbus.ObjectPath][]byte, error) {
values := make(map[dbus.ObjectPath][]byte)
for _, path := range items {
if item := s.item(path); item.batchReadable && !item.locked {
values[path] = []byte(item.secret)
}
}
return values, nil
}
func hasAttributes(attributes, query map[string]string) bool {
for key, value := range query {
if attributes[key] != value {