JFLib

plat/secret.rs#

Source: lib/plat/secret.rs

secret -- passwords, tokens and keys in the system's secret store instead of files

  secret_available() -> bool          a store is there and the app may use it
  secret_set(service, account, &[u8]) -> Result<(), SecretError>        create or replace
  secret_get(service, account) -> Option<Vec<u8>>                       None: missing / error
  secret_get_checked(service, account) -> Result<Option<Vec<u8>>, SecretError>
  secret_delete(service, account) -> Result<bool, SecretError>          true: one was removed

An entry is named by the app (vfs::app_id()), a service ("github", "smtp.example.org") and
an account ("alice", or "" for one value per service), so apps do not reach each other's
entries through this API.  The app needs `secrets` in its manifest (policy.rs); without it
every call is refused (SecretError::Denied, PERMDENY secrets <service>).  Values are bytes,
at most SECRET_MAX of them.  Without a store the calls fail: nothing is ever written to a
plain file instead.

Windows (win_secret.rs): generic credentials of the Credential Manager, target
"jflib:<app>/<service>/<account>" ('%' and '/' of the names as %25 / %2F), persisted on this
computer only (CRED_PERSIST_LOCAL_MACHINE: tokens do not roam to other machines with the
profile).  Values above 2560 bytes (the Credential Manager's limit) go DPAPI-encrypted into
data://secrets/; the credential keeps the file's SHA-256, so neither a stale nor a foreign
file is accepted.  Linux (linux_secret.rs): items of the Secret Service on the session bus
(gnome-keyring, KWallet, KeePassXC) in the default collection, attributes app / service /
account; the values travel encrypted (dh-ietf1024-sha256-aes128-cbc-pkcs7; plain only when
the service lacks it); a locked collection is unlocked through the service's prompt.
Android (android_secret.rs): an AES-256-GCM key of the Android Keystore (one per app, made on
the first set, not exportable, no user authentication) encrypts each value into a file of
the app's no_backup folder.  Browser (web_secret.rs, web/secrets.js): a non-extractable
WebCrypto AES-GCM key per app in IndexedDB encrypts the values kept there; the host decrypts
the app's entries before it starts, plat answers from that copy and the host commits each
change asynchronously.  macOS and iOS (apple_secret.rs): generic passwords of the keychain,
service "jflib:<app>/<service>" and the account.

The OS does not separate the programs of one user: other native programs of the same user
can read these entries (the Credential Manager, an unlocked keyring); in the browser every
script of the page's origin can use the key.  The calls block -- on Linux until the user
answers a prompt (at most PROMPT_SECONDS): call them from a worker thread when the UI must
stay responsive.
/// the largest value secret_set stores
SECRET_MAX: usize = 64 * 1024
/// the longest service or account name (bytes of UTF-8)
SECRET_NAME_MAX: usize = 256
/// how long a call waits for the user to answer an unlock prompt (Linux)
PROMPT_SECONDS: u32 = 300
  • enum SecretError why a secret call failed

    • Unavailable -- no secret store here (Linux: no Secret Service on the session bus; browser: no IndexedDB or WebCrypto, e.g. a page served over plain http)
    • Denied -- the app's permissions lack secrets
    • Dismissed -- the user dismissed the store's prompt (unlock, confirmation), or it timed out
    • Invalid -- an empty or too long service, a name with NUL, or a value above SECRET_MAX
    • Failed(String) -- the store refused or failed: what it said
  • fn secret_available() -> bool whether this system has a secret store the app may use (secrets in its manifest)

  • fn secret_set(service: &str, account: &str, secret: &[u8]) -> Result<(), SecretError> store secret under (service, account), replacing what was there

  • fn secret_get(service: &str, account: &str) -> Option<Vec<u8>> the secret of (service, account); None when there is none, and on any error

  • fn secret_get_checked(service: &str, account: &str) -> Result<Option<Vec<u8>>, SecretError> the secret of (service, account): Ok(None) when there is none

  • fn secret_delete(service: &str, account: &str) -> Result<bool, SecretError> remove the secret of (service, account) -> whether there was one

Source: docs/API.md in the JFLib repository.