package session import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/base64" "errors" "fmt" "io/fs" "os" "path/filepath" "github.com/zalando/go-keyring" "git.security-command.org/Overlord/dibs/internal/fetch" "git.security-command.org/Overlord/dibs/internal/fsutil" ) const ( keyringService = "dibs" keyringUser = "session-key" ) // Vault keeps each shop's signed-in cookies in a file of their own. The // files are encrypted with a key kept in the OS keyring. Without a keyring, // they are plain files only the user can read. // // The keyring holds only the key: Windows limits a keyring entry to 2560 // bytes, which a shop's cookies can exceed. type Vault struct { dir string codec fetch.Codec // nil without a keyring } // OpenVault returns the vault in dir/sessions. When the keyring cannot be // used, it also returns an error saying so, and the vault stores plain // files. func OpenVault(dir string) (*Vault, error) { v := &Vault{dir: filepath.Join(dir, "sessions")} key, err := v.key() if err != nil { return v, fmt.Errorf("no usable OS keyring, so signed-in cookies are stored unencrypted in %s: %w", v.dir, err) } block, err := aes.NewCipher(key) if err != nil { return v, err } aead, err := cipher.NewGCM(block) if err != nil { return v, err } v.codec = sealer{aead} return v, nil } // Encrypted reports whether the files are encrypted. func (v *Vault) Encrypted() bool { return v.codec != nil } // key returns the vault's key from the keyring, creating it on first use. func (v *Vault) key() ([]byte, error) { // Two processes creating a key at once would each encrypt with their // own. unlock, err := fsutil.Lock(filepath.Join(v.dir, "key.lock")) if err != nil { return nil, err } defer unlock() s, err := keyring.Get(keyringService, keyringUser) switch { case err == nil: key, err := base64.StdEncoding.DecodeString(s) if err != nil || len(key) != 32 { return nil, errors.New("the session key in the keyring is not a 256-bit key") } return key, nil case errors.Is(err, keyring.ErrNotFound): key := make([]byte, 32) _, _ = rand.Read(key) if err := keyring.Set(keyringService, keyringUser, base64.StdEncoding.EncodeToString(key)); err != nil { return nil, err } return key, nil default: return nil, err } } func (v *Vault) path(shop string) string { if v.codec != nil { return filepath.Join(v.dir, shop+".cookies") } return v.plainPath(shop) } func (v *Vault) plainPath(shop string) string { return filepath.Join(v.dir, shop+".json") } // Load reads shop's cookies into jar. Cookies stored unencrypted before a // keyring was available are encrypted now. func (v *Vault) Load(shop string, jar *fetch.Jar) error { if err := jar.LoadWith(v.path(shop), v.codec); err != nil { return fmt.Errorf("reading %s: %w", v.path(shop), err) } if v.codec == nil { return nil } plain := v.plainPath(shop) if _, err := os.Stat(plain); errors.Is(err, fs.ErrNotExist) { return nil } if err := jar.Load(plain); err != nil { return fmt.Errorf("reading %s: %w", plain, err) } if err := v.Save(shop, jar); err != nil { return err } return os.Remove(plain) } // Save writes jar as shop's cookies, after taking in newer cookies other // dibs processes saved. func (v *Vault) Save(shop string, jar *fetch.Jar) error { return jar.SaveWith(v.path(shop), v.codec) } // Forget removes shop's file, such as one that cannot be decrypted. func (v *Vault) Forget(shop string) error { for _, p := range []string{v.path(shop), v.plainPath(shop)} { if err := os.Remove(p); err != nil && !errors.Is(err, fs.ErrNotExist) { return err } } return nil } // sealer encrypts with AES-GCM, with the nonce in front of the ciphertext. type sealer struct{ aead cipher.AEAD } var additionalData = []byte("dibs session cookies v1") func (s sealer) Encode(data []byte) ([]byte, error) { nonce := make([]byte, s.aead.NonceSize()) _, _ = rand.Read(nonce) return s.aead.Seal(nonce, nonce, data, additionalData), nil } func (s sealer) Decode(data []byte) ([]byte, error) { n := s.aead.NonceSize() if len(data) < n { return nil, errors.New("the file is too short to be encrypted cookies") } out, err := s.aead.Open(nil, data[:n], data[n:], additionalData) if err != nil { return nil, errors.New("the file cannot be decrypted with the key in the keyring") } return out, nil }