Documentation
¶
Overview ¶
Package crypto provides authenticated encryption for secrets at rest, key file management, random tokens and token hashing.
Index ¶
- Constants
- Variables
- func ABEntryHashAD(entryID int64) string
- func ABEntryPasswordAD(entryID int64) string
- func AD(scope string, id int64) string
- func AddressBookDefaultPasswordAD(bookID int64) string
- func DecodeKey(s string) ([]byte, error)
- func Equal(a, b []byte) bool
- func EqualString(a, b string) bool
- func FederationPeerTokenAD(peerID int64) string
- func HashToken(token string) []byte
- func ICEServersAD() string
- func IdentityProviderClientSecretAD(providerID int64) string
- func LDAPBindPasswordAD() string
- func OAuthStatePKCEVerifierAD(stateID int64) string
- func RandomBytes(n int) ([]byte, error)
- func RandomToken(n int) (string, error)
- func SMTPPasswordAD() string
- func ServerShareLinkTokenAD(linkID int64) string
- func ShareLinkPasswordAD(linkID int64) string
- func TOTPSecretAD(userID int64) string
- type AEAD
- type KeySource
Constants ¶
const ( ScopeABEntryPassword = "ab_entry.password" ScopeABEntryHash = "ab_entry.hash" ScopeAddressBookDefaultPassword = "address_book.password" ScopeIdentityProviderClientSecret = "identity_provider.client_secret" ScopeOAuthStatePKCEVerifier = "oauth_state.pkce_verifier" ScopeTOTPSecret = "totp" )
Associated-data (AD) conventions for AEAD-encrypted columns.
Every *_enc column is sealed with AD "<scope>:<row id>", which binds the ciphertext to one column of one row: copying it into another row or column makes Open fail. The scope strings below are part of the on-disk format and MUST NOT change; every writer (client API, admin API, legacy importer) and reader uses these helpers instead of formatting AD strings by hand.
column AD address_book_entries.password_enc "ab_entry.password:<entry id>" address_book_entries.hash_enc "ab_entry.hash:<entry id>" address_books.default_password_enc "address_book.password:<book id>" identity_providers.client_secret_enc "identity_provider.client_secret:<provider id>" share_links.password_enc "share_link.password:<link id>" server_share_links.token_enc "server_share_link.token:<link id>" oauth_states.pkce_verifier_enc "oauth_state.pkce_verifier:<state id>" totp_secrets.secret_enc "totp:<user id>" settings["mail.smtp"].password_enc "settings.smtp_password:0" settings["client.options"].ice_servers_enc "settings.ice_servers:0" settings["federation.peer.<id>.token"].token_enc "settings.federation_peer_token:<peer id>"
Because the AD contains the row id, a row must be inserted first (to learn its id) and the ciphertext written afterwards, or the id must be known in advance (preserved legacy ids).
const KeySize = chacha20poly1305.KeySize
KeySize is the AEAD key length in bytes.
const MinSecretLen = 16
MinSecretLen is the shortest secret LoadOrCreateHexSecret accepts from an existing file.
const ScopeFederationPeerToken = "settings.federation_peer_token"
ScopeFederationPeerToken seals the token this panel presents to a federation peer, stored in the panel settings (key "federation.peer.<peer id>.token"; the id is the federation_peers row).
const ScopeICEServers = "settings.ice_servers"
ScopeICEServers seals the client ICE server list (TURN credentials) stored in the panel settings (settings key "client.options"; a single value, id 0).
const ScopeLDAPBindPassword = "settings.ldap_bind_password"
ScopeLDAPBindPassword seals the LDAP bind password stored in ldap_settings (a single value, id 0).
const ScopeSMTPPassword = "settings.smtp_password"
ScopeSMTPPassword seals the SMTP password stored in the panel settings (settings key "mail.smtp"; a single value, so the id is always 0).
Variables ¶
var ErrDecrypt = errors.New("crypto: decryption failed")
ErrDecrypt is returned when a ciphertext is malformed, was produced with a different key, or its associated data does not match.
Functions ¶
func ABEntryHashAD ¶
ABEntryHashAD is the AD of address_book_entries.hash_enc.
func ABEntryPasswordAD ¶
ABEntryPasswordAD is the AD of address_book_entries.password_enc.
func AddressBookDefaultPasswordAD ¶
AddressBookDefaultPasswordAD is the AD of address_books.default_password_enc.
func EqualString ¶
EqualString compares two strings in constant time (for equal lengths).
func FederationPeerTokenAD ¶ added in v0.11.0
FederationPeerTokenAD is the AD of a stored federation peer token.
func HashToken ¶
HashToken returns sha256(token). Tokens (client JWTs, session tokens, share links, OAuth states) are stored only as this digest, never in plain text. A fast hash is adequate because every token has >=128 bits of entropy.
func ICEServersAD ¶ added in v0.3.0
func ICEServersAD() string
ICEServersAD is the AD of the stored client ICE server list.
func IdentityProviderClientSecretAD ¶
IdentityProviderClientSecretAD is the AD of identity_providers.client_secret_enc.
func LDAPBindPasswordAD ¶ added in v0.7.0
func LDAPBindPasswordAD() string
LDAPBindPasswordAD is the AD of the stored LDAP bind password.
func OAuthStatePKCEVerifierAD ¶
OAuthStatePKCEVerifierAD is the AD of oauth_states.pkce_verifier_enc.
func RandomBytes ¶
RandomBytes returns n cryptographically random bytes.
func RandomToken ¶
RandomToken returns n random bytes encoded as unpadded base64url (URL-, header- and cookie-safe).
func SMTPPasswordAD ¶
func SMTPPasswordAD() string
SMTPPasswordAD is the AD of the stored SMTP password.
func ServerShareLinkTokenAD ¶ added in v0.14.0
ServerShareLinkTokenAD is the AD of server_share_links.token_enc.
func ShareLinkPasswordAD ¶
ShareLinkPasswordAD is the AD of share_links.password_enc.
func TOTPSecretAD ¶
TOTPSecretAD is the AD of totp_secrets.secret_enc.
Types ¶
type AEAD ¶
type AEAD struct {
// contains filtered or unexported fields
}
AEAD seals and opens small secrets (OIDC client secrets, address-book passwords, TOTP seeds) with XChaCha20-Poly1305. It is safe for concurrent use.
func (*AEAD) OpenString ¶
OpenString is Open returning a string.
type KeySource ¶
type KeySource string
KeySource tells where a key came from (for logging; never log the key itself).
const ( KeyFromEnv KeySource = "env" KeyFromFile KeySource = "file" KeyGeneratedFile KeySource = "generated" )
Key sources.
func LoadAEADKey ¶
LoadAEADKey resolves the 32-byte secrets-at-rest key: a base64 value (from RDPANEL_SECRET_KEY) wins; otherwise the key file is read, or created with mode 0600 when it does not exist yet.
func LoadOrCreateHexSecret ¶ added in v0.12.0
LoadOrCreateHexSecret reads a shared-secret file (e.g. the hbbs authorization secret), creating it with n random bytes as lowercase hex when missing. The new file is written to a temporary name, synced and then linked into place, so a concurrent reader (another process) never sees a partial secret and an existing file is never replaced. The secret is the file content without surrounding white space; it must be at least MinSecretLen characters long.
func LoadOrCreateTextKey ¶
LoadOrCreateTextKey reads a text key file (e.g. the generated JWT key used when RUSTDESK_API_JWT_KEY is unset), creating it with n random bytes encoded as unpadded base64url when missing. The file content (trimmed) is the key.