Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
portals::SecureString Class Reference

Move-only bounded secret storage with checked synchronous plaintext access. More...

#include <secure_string.hpp>

Public Member Functions

 SecureString (const SecureString &)=delete
 Copying secret ownership/bytes is not supported.
SecureString & operator= (const SecureString &)=delete
 Copy assignment is not supported.
 SecureString (SecureString &&other) noexcept
 Transfer the ownership handle and its existing busy state, without copying bytes.
SecureString & operator= (SecureString &&other) noexcept
 Detach the old destination and transfer the source handle; self-move is a no-op. An active lease keeps detached destination storage alive until checked finish.
 ~SecureString () noexcept
 Detach ownership; wipe/release immediately or after the final active lease.
std::expected< void, SecureStringFailure > assign (std::string_view bytes)
 Replace bytes via an exact-length candidate; preparation failure preserves the old value.
std::expected< void, SecureStringFailure > append (std::string_view bytes)
 Append bytes via an exact-length candidate; no exponential growth or string intermediate.
std::expected< void, SecureStringFailure > clear ()
 Replace with a valid empty value under the same requested policy.
std::expected< void, SecureStringFailure > close () noexcept
 Wipe and release; idempotent after success, retryable after release failure, BUSY during borrow.
std::size_t size () const noexcept
bool empty () const noexcept
bool available () const noexcept
SecureStringCapabilities capabilities () const noexcept
template<class F>
requires detail::SecretCallback<F>
auto withPlaintext (F &&callback) -> std::expected< void, SecureStringAccessFailure< typename detail::SecretCallbackResult< std::invoke_result_t< std::decay_t< F > &, SecretView > >::Error > >
 Invoke an owned synchronous callable, then restore protection or retire detached storage.

Static Public Member Functions

static std::expected< SecureString, SecureStringFailure > create (std::string_view bytes={}, SecureStringOptions options={})
 Prepare the requested policy before copying input; no input copies are retained on failure.

Friends

class detail::SecureStringTestAccess

Detailed Description

Move-only bounded secret storage with checked synchronous plaintext access.

Native storage has adjacent inaccessible guard pages and an always-RW payload. Windows and Linux encrypt idle bytes by default. Windows uses SAME_PROCESS; Linux uses authenticated encryption and a separate guarded key page. Optional page locking and Linux dump exclusion cover both key and payload pages. WIPE_ONLY requires explicit encryption opt-out, uses heap storage and rejects lock/dump/encryption requests. Unsupported mandatory guarantees fail without a downgrade, including on WASM. Full accessible capacity is wiped before release; a failed release retains zeroed metadata for close() retry. A destructor retries release twice and may leak zeroed pages if the OS refuses both attempts.

Candidate allocation/capability failure preserves the old value. Ambiguous crypto failure wipes the affected payload and makes that value unavailable. Replacement failure after old-storage retirement never resurrects old bytes.

This does not protect against a debugger, hostile code in the process, host, keylogger, live plaintext access or external copies. Length is not concealed. Operations, moves and destruction require caller synchronization. A callback may move/destroy the external owner; its lease stays alive. Checked mutation and nested borrowing of that same storage fail BUSY, including no-op inputs.

Constructor & Destructor Documentation

◆ SecureString()

portals::SecureString::SecureString ( SecureString && other)
noexcept

Transfer the ownership handle and its existing busy state, without copying bytes.

Parameters
otherSource handle, left moved-from without invalidating active borrows.

Member Function Documentation

◆ append()

std::expected< void, SecureStringFailure > portals::SecureString::append ( std::string_view bytes)
nodiscard

Append bytes via an exact-length candidate; no exponential growth or string intermediate.

Parameters
bytesBorrowed suffix copied synchronously; embedded zeroes are preserved.
Returns
Success or primary and cleanup failures; BUSY rejects even an empty suffix.

◆ assign()

std::expected< void, SecureStringFailure > portals::SecureString::assign ( std::string_view bytes)
nodiscard

Replace bytes via an exact-length candidate; preparation failure preserves the old value.

Parameters
bytesBorrowed input copied synchronously; embedded zeroes are preserved.
Returns
Success or primary and cleanup failures; BUSY rejects mutation during a borrow.

◆ available()

bool portals::SecureString::available ( ) const
nodiscardnoexcept
Returns
Whether this handle has a live usable value, including a valid empty value.

◆ capabilities()

SecureStringCapabilities portals::SecureString::capabilities ( ) const
nodiscardnoexcept
Returns
Current applied capabilities; all false after closing or invalidation.

◆ clear()

std::expected< void, SecureStringFailure > portals::SecureString::clear ( )
nodiscard

Replace with a valid empty value under the same requested policy.

Returns
Success or primary and cleanup failures; unavailable and busy handles reject clearing.

◆ close()

std::expected< void, SecureStringFailure > portals::SecureString::close ( )
nodiscardnoexcept

Wipe and release; idempotent after success, retryable after release failure, BUSY during borrow.

Returns
Success or primary and cleanup failures, without secret bytes.

◆ create()

std::expected< SecureString, SecureStringFailure > portals::SecureString::create ( std::string_view bytes = {},
SecureStringOptions options = {} )
staticnodiscard

Prepare the requested policy before copying input; no input copies are retained on failure.

Parameters
bytesApplication bytes; embedded zeroes are preserved.
optionsMandatory storage policy and finite bounds; maxBytes may be zero.
Returns
Owning value, or secret-free diagnostics. Input ownership is unchanged.

◆ empty()

bool portals::SecureString::empty ( ) const
nodiscardnoexcept
Returns
Whether size() is zero; use available() to distinguish a valid empty value.

◆ operator=()

SecureString & portals::SecureString::operator= ( SecureString && other)
noexcept

Detach the old destination and transfer the source handle; self-move is a no-op. An active lease keeps detached destination storage alive until checked finish.

Parameters
otherSource handle, left moved-from unless this is self-assignment.
Returns
This same destination handle.

◆ size()

std::size_t portals::SecureString::size ( ) const
nodiscardnoexcept
Returns
Logical byte count; zero for closed, moved-from or unavailable values.

◆ withPlaintext()

template<class F>
requires detail::SecretCallback<F>
auto portals::SecureString::withPlaintext ( F && callback) -> std::expected< void, SecureStringAccessFailure< typename detail::SecretCallbackResult< std::invoke_result_t< std::decay_t< F > &, SecretView > >::Error > >
inlinenodiscard

Invoke an owned synchronous callable, then restore protection or retire detached storage.

Template Parameters
FCopyable lvalue or movable rvalue callable returning void or expected<void,E>.
Parameters
callbackCallable copied/moved after the lease and busy gate, before decryption.
Returns
Callback error/exception and cleanup failure are preserved independently.

E must be owning and nothrow movable; direct pointers, reference wrappers, SecretView and standard borrowed ranges/views are rejected. Custom E and callable captures retain their explicit caller lifetime obligations. Capture bad_alloc maps to NO_MEMORY; other exceptions map to CALLBACK_FAILED. Neither exception text nor opaque E is formatted. No owner access occurs after capture.


The documentation for this class was generated from the following file: