133 std::array<SecureStringError, MAX_SECURE_CLEANUP_ERRORS>
errors{};
152 void add(std::optional<SecureStringError> failure)
noexcept {
168 for (std::size_t index = 0; index < other.count; ++index) {
169 add(other.errors[index]);
176 [[nodiscard]]
bool empty() const noexcept {
198 [[nodiscard]]
const char *
data() const noexcept {
199 return bytes_.data();
203 [[nodiscard]] std::size_t
size() const noexcept {
204 return bytes_.size();
208 [[nodiscard]]
bool empty() const noexcept {
209 return bytes_.empty();
213 explicit SecretView(std::span<const char> bytes) noexcept : bytes_(bytes) {
216 std::span<const char> bytes_;
217 friend class SecureString;
234struct SecureStringState;
235class SecureStringTestAccess;
237template <
class T>
inline constexpr bool IS_REFERENCE_WRAPPER =
false;
238template <
class T>
inline constexpr bool IS_REFERENCE_WRAPPER<std::reference_wrapper<T>> =
true;
241concept OwningSecretError =
242 !std::is_reference_v<E> && !std::is_pointer_v<E> && !IS_REFERENCE_WRAPPER<std::remove_cv_t<E>> &&
243 !std::same_as<std::remove_cv_t<E>,
SecretView> && !std::ranges::view<E> && !std::ranges::borrowed_range<E> &&
244 std::is_nothrow_move_constructible_v<E> && std::is_nothrow_destructible_v<E>;
246template <
class R>
struct SecretCallbackResult {
247 static constexpr bool VALID =
false;
248 using Error = std::monostate;
251template <>
struct SecretCallbackResult<void> {
252 static constexpr bool VALID =
true;
253 using Error = std::monostate;
256template <
class E>
struct SecretCallbackResult<std::expected<void, E>> {
257 static constexpr bool VALID = OwningSecretError<E>;
262concept SecretCallback =
263 std::constructible_from<std::decay_t<F>, F> && std::is_nothrow_destructible_v<std::decay_t<F>> &&
264 std::invocable<std::decay_t<F> &, SecretView> &&
265 SecretCallbackResult<std::invoke_result_t<std::decay_t<F> &, SecretView>>::VALID;
324 [[nodiscard]] std::expected<void, SecureStringFailure>
assign(std::string_view bytes);
329 [[nodiscard]] std::expected<void, SecureStringFailure>
append(std::string_view bytes);
333 [[nodiscard]] std::expected<void, SecureStringFailure>
clear();
337 [[nodiscard]] std::expected<void, SecureStringFailure>
close() noexcept;
339 [[nodiscard]] std::
size_t size() const noexcept;
341 [[nodiscard]]
bool empty() const noexcept;
359 requires detail::SecretCallback<F>
362 std::invoke_result_t<std::decay_t<F> &,
SecretView>>::Error>> {
363 using Return = std::invoke_result_t<std::decay_t<F> &,
SecretView>;
367 auto entered = beginBorrow(lease);
370 return std::unexpected(Failure{entered.error(), {}, {}});
378 std::optional<std::decay_t<F>> frame;
381 frame.emplace(std::forward<F>(callback));
382 }
catch (
const std::bad_alloc &) {
389 auto view = openBorrow(lease);
392 failure.primary = view.error().primary;
394 failure.cleanup.append(view.error().cleanup);
397 if constexpr (std::is_void_v<Return>) {
398 std::invoke(*frame, *view);
400 auto result = std::invoke(*frame, *view);
405 failure.callbackError.emplace(std::move(result.error()));
416 auto cleanup = finishBorrow(lease);
419 failure.cleanup.add(cleanup->primary);
420 failure.cleanup.append(cleanup->cleanup);
423 if (failure.primary || !failure.cleanup.empty()) {
424 return std::unexpected(std::move(failure));
431 explicit SecureString(std::shared_ptr<detail::SecureStringState> state)
noexcept;
432 void detach() noexcept;
436 beginBorrow(const std::shared_ptr<detail::SecureStringState> &state) noexcept;
438 openBorrow(const std::shared_ptr<detail::SecureStringState> &state) noexcept;
440 finishBorrow(const std::shared_ptr<detail::SecureStringState> &state) noexcept;
442 std::shared_ptr<detail::SecureStringState> state_;
443 friend class detail::SecureStringTestAccess;
Synchronous read-only byte borrow, valid only during its callback.
Definition secure_string.hpp:195
std::size_t size() const noexcept
Definition secure_string.hpp:203
const char * data() const noexcept
Definition secure_string.hpp:198
bool empty() const noexcept
Definition secure_string.hpp:208
std::expected< void, SecureStringFailure > append(std::string_view bytes)
Append bytes via an exact-length candidate; no exponential growth or string intermediate.
bool available() const noexcept
std::expected< void, SecureStringFailure > clear()
Replace with a valid empty value under the same requested policy.
std::expected< void, SecureStringFailure > assign(std::string_view bytes)
Replace bytes via an exact-length candidate; preparation failure preserves the old value.
SecureStringCapabilities capabilities() const noexcept
SecureString(const SecureString &)=delete
Copying secret ownership/bytes is not supported.
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.
Definition secure_string.hpp:360
SecureString & operator=(SecureString &&other) noexcept
Detach the old destination and transfer the source handle; self-move is a no-op. An active lease keep...
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.
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.
~SecureString() noexcept
Detach ownership; wipe/release immediately or after the final active lease.
std::size_t size() const noexcept
std::expected< void, SecureStringFailure > close() noexcept
Wipe and release; idempotent after success, retryable after release failure, BUSY during borrow.
bool empty() const noexcept
Public Portals API.
Definition action.hpp:8
@ TOO_LARGE
Configured envelope, entry, identity or count limit exceeded.
Definition navigation_checkpoint.hpp:29
@ VALID
The checked candidate satisfied all configured synchronous rules.
Definition form_field.hpp:18
@ RELEASE
Key release reported by a native adapter.
Definition input.hpp:209
constexpr std::size_t MAX_SECURE_CLEANUP_ERRORS
Maximum separately retained cleanup diagnostics; excess failures are counted.
Definition secure_string.hpp:128
constexpr std::size_t DEFAULT_MAX_SECRET_BYTES
Default bound for application bytes, without Unicode interpretation.
Definition secure_string.hpp:25
SecureStringOperation
Operation recorded without secret bytes or exception messages.
Definition secure_string.hpp:66
@ APPEND
Append application bytes to a live value.
Definition secure_string.hpp:72
@ ENCRYPT
Encrypt payload bytes for idle storage.
Definition secure_string.hpp:86
@ ENTROPY
Obtain native cryptographic entropy for a key or fresh nonce.
Definition secure_string.hpp:92
@ CLEAR
Replace a live value with an empty value under the same policy.
Definition secure_string.hpp:74
@ ALLOCATE
Allocate payload storage and establish its exterior guards.
Definition secure_string.hpp:80
@ BORROW
Admit plaintext access, capture a callable or invoke its callback.
Definition secure_string.hpp:78
@ LOCK
Apply the requested native resident-page lock.
Definition secure_string.hpp:82
@ DECRYPT
Decrypt payload bytes before scoped access or copying.
Definition secure_string.hpp:88
@ DUMP
Apply the requested native core-dump exclusion.
Definition secure_string.hpp:84
@ ASSIGN
Replace the logical bytes of a live value.
Definition secure_string.hpp:70
@ CALLBACK_FAILED
A non-allocation exception escaped into a runner callback boundary.
Definition application.hpp:34
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
@ INVALID_ARGUMENT
Empty task, callback or immutable data owner, or invalid policy.
Definition async.hpp:26
@ UNAVAILABLE
A hosted session requires a borrowed executor.
Definition background_options.hpp:13
SecureStringErrorCode
Technical failure categories independent of UI/tree errors.
Definition secure_string.hpp:96
@ CALLBACK_FAILED
Callable capture/invocation threw or a callback returned its typed error.
Definition secure_string.hpp:112
@ SECURITY_FAILURE
Native guard, lock, dump-exclusion or crypto setup/access failed.
Definition secure_string.hpp:106
@ RELEASE_FAILED
Backend release failed; zeroed allocation metadata may remain for retry.
Definition secure_string.hpp:114
@ BUSY
The same storage already has an active plaintext borrow.
Definition secure_string.hpp:108
@ NO_MEMORY
Metadata, callable capture or payload allocation failed.
Definition secure_string.hpp:102
@ UNSUPPORTED
The application explicitly excludes this annotated phase.
Definition key_binding.hpp:44
constexpr std::size_t MAX_SECRET_BYTES
Hard upper bound for an individual secret, in bytes.
Definition secure_string.hpp:27
SecureStringStorage
Explicit storage policy; unsupported native protection never falls back.
Definition secure_string.hpp:30
@ NATIVE_GUARDED
Dedicated native RW payload surrounded by inaccessible pages.
Definition secure_string.hpp:32
@ WIPE_ONLY
Explicit weaker heap/linear-memory policy with wiping alone.
Definition secure_string.hpp:34
@ CREATE
Validate options and construct the application and its core services.
Definition application.hpp:16
@ CLOSE
Close the transport and attempt restoration of terminal state.
Definition application.hpp:25
Callback and cleanup diagnostics; opaque user errors are never logged.
Definition secure_string.hpp:223
SecureStringCleanupErrors cleanup
Reprotection/retirement failures independent of the primary error.
Definition secure_string.hpp:229
std::optional< SecureStringError > primary
Capture/decryption/callback failure; absent when only cleanup failed.
Definition secure_string.hpp:225
std::optional< E > callbackError
Opaque owning callback error, never formatted by the library.
Definition secure_string.hpp:227
Applied guarantees, rather than potential platform features.
Definition secure_string.hpp:52
bool encryptedWhileIdle
Native encryption is applied between plaintext borrows.
Definition secure_string.hpp:62
bool guardPages
Two inaccessible exterior pages surround the RW payload.
Definition secure_string.hpp:56
bool lockedPages
Native page locking was requested and applied.
Definition secure_string.hpp:58
bool wiping
Entire accessible payload and key capacity is explicitly wiped before release.
Definition secure_string.hpp:54
bool excludedFromDumps
Linux core-dump exclusion was requested and applied.
Definition secure_string.hpp:60
Allocation-free cleanup diagnostics in chronological order.
Definition secure_string.hpp:131
std::size_t omittedCleanupCount
Additional failures omitted because the fixed buffer is full.
Definition secure_string.hpp:137
SecureStringCleanupErrors()=default
Construct an empty diagnostic collection.
std::array< SecureStringError, MAX_SECURE_CLEANUP_ERRORS > errors
First count errors, in actual cleanup order.
Definition secure_string.hpp:133
bool empty() const noexcept
Definition secure_string.hpp:176
SecureStringCleanupErrors(std::optional< SecureStringError > failure) noexcept
Construct from a possibly absent first failure.
Definition secure_string.hpp:145
std::size_t count
Number of valid entries in errors.
Definition secure_string.hpp:135
void append(const SecureStringCleanupErrors &other) noexcept
Append retained failures and overflow counts from another collection.
Definition secure_string.hpp:167
void add(std::optional< SecureStringError > failure) noexcept
Append an existing diagnostic without allocation; absent errors are ignored.
Definition secure_string.hpp:152
Owning, secret-free library diagnostic; nativeCode is zero when inapplicable.
Definition secure_string.hpp:118
SecureStringOperation operation
Library operation that failed.
Definition secure_string.hpp:120
SecureStringErrorCode code
Stable technical failure category.
Definition secure_string.hpp:122
std::uint32_t nativeCode
Captured GetLastError/errno, or zero without a native error.
Definition secure_string.hpp:124
A primary failure and independently preserved bounded cleanup diagnostics.
Definition secure_string.hpp:182
SecureStringCleanupErrors cleanup
Subsequent cleanup/security failures and overflow count.
Definition secure_string.hpp:186
SecureStringError primary
Original operation failure, never displaced by cleanup.
Definition secure_string.hpp:184
Requested mandatory guarantees, established even for an empty value.
Definition secure_string.hpp:38
std::size_t maxBytes
Maximum application bytes, from zero through MAX_SECRET_BYTES.
Definition secure_string.hpp:40
bool lockPages
Require native resident page locking for payload and any key page before copying input.
Definition secure_string.hpp:44
bool excludeFromDumps
Require Linux core-dump exclusion for payload and any key page; unsupported on Windows/WASM.
Definition secure_string.hpp:46
SecureStringStorage storage
Mandatory allocation policy; no implicit platform fallback.
Definition secure_string.hpp:42
bool encryptWhileIdle
Require native idle encryption; true by default, unsupported with WIPE_ONLY or on WASM.
Definition secure_string.hpp:48