yb-core 0.7.1

Secure blob storage on a YubiKey — core library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// SPDX-FileCopyrightText: 2025 - 2026 Frederic Ruget <fred@atlant.is> (GitHub: @douzebis)
//
// SPDX-License-Identifier: MIT

//! High-level blob operations: store, fetch, remove, list.
//!
//! Sub-modules:
//! - [`types`]       — `Encryption`, `Compression`, `StoreOptions`, `BlobInfo`, constants
//! - [`compression`] — compression before encryption
//! - [`protection`]  — what a store kind does to protect blobs (spec 0028)
//! - [`signature`]   — trailers (spec 0017), signatures and chain assembly
//! - this module     — `store_blob`, `fetch_blob`, `remove_blob`, `list_blobs`

mod compression;
// Post-quantum stores need the FIDO2 module, not built on Windows.
#[cfg(not(windows))]
pub mod post_quantum;
mod protection;
mod signature;
mod types;

pub use protection::{BlobMeta, LegacyProtection, Protected, Protection};
pub use signature::{check_blob_signature, collect_blob_chain, SigVerdict, SIG_TRAILER_LEN};
pub(crate) use types::BLOB_SIZE_C_BIT;
pub use types::{BlobInfo, Compression, Encryption, StoreOptions};

use crate::crypto;
use crate::piv::PivBackend;
use crate::store::{constants::MAX_NAME_LEN, Object, ObjectParams, Store};
use anyhow::{bail, Result};

use compression::{compress, decompress_payload};
use signature::append_trailer;

// ---------------------------------------------------------------------------
// chunks_needed
// ---------------------------------------------------------------------------

/// Calculate how many objects a blob of `data_len` bytes with a name of
/// `name_len` bytes needs in a store with `object_size`-byte objects.
pub fn chunks_needed(data_len: usize, name_len: usize, object_size: usize) -> usize {
    let head_cap = Object::head_payload_capacity(object_size, name_len);
    let cont_cap = Object::continuation_payload_capacity(object_size);
    if data_len <= head_cap {
        1
    } else {
        1 + (data_len - head_cap).div_ceil(cont_cap)
    }
}

/// How many objects a stored blob of `data_len` bytes may occupy: its data
/// plus the signature trailer, which can need an extra chunk (spec 0017).
/// Both capacity checks use it, so they cannot disagree (spec 0031).
pub fn objects_needed(data_len: usize, name_len: usize, object_size: usize) -> usize {
    chunks_needed(data_len + SIG_TRAILER_LEN, name_len, object_size)
}

// ---------------------------------------------------------------------------
// store_blob
// ---------------------------------------------------------------------------

/// Store a blob on the YubiKey, in a legacy store (PIV encryption and
/// signature, with `pin`).
///
/// Returns `false` if the store is full.
pub fn store_blob(
    store: &mut Store,
    piv: &dyn PivBackend,
    name: &str,
    payload: &[u8],
    options: StoreOptions<'_>,
    management_key: &str,
    pin: Option<&str>,
) -> Result<bool> {
    store_blob_checked(
        store,
        piv,
        name,
        payload,
        options,
        management_key,
        pin,
        None,
    )
}

/// [`store_blob`], with `before_write` checked on the store read under the
/// card lock (see [`store_blob_with_checked`]).
#[allow(clippy::too_many_arguments)]
pub fn store_blob_checked(
    store: &mut Store,
    piv: &dyn PivBackend,
    name: &str,
    payload: &[u8],
    options: StoreOptions<'_>,
    management_key: &str,
    pin: Option<&str>,
    before_write: Option<BeforeWrite<'_>>,
) -> Result<bool> {
    let (encrypt, encryption_key) = match options.encryption {
        Encryption::None => (false, None),
        Encryption::Encrypted(pk) => (true, Some(pk)),
    };
    let reader = store.reader.clone();
    let protection = LegacyProtection {
        piv,
        reader: &reader,
        slot: store.store_key_slot,
        encryption_key,
        verifying_key: None,
        pin,
        debug: false,
    };
    store_blob_with_checked(
        store,
        piv,
        &protection,
        name,
        payload,
        encrypt,
        options.compression,
        Some(management_key),
        before_write,
    )
}

/// Store a blob on the YubiKey, protected by `protection` (spec 0028).
///
/// Returns `false` if the store is full, found before anything else.  The
/// blob is compressed, then protected (which may ask for the PIN), and only
/// then written: a failure before the writes stores nothing.  The writes
/// use the management key the protection provides (post-quantum stores),
/// else `management_key`.
#[allow(clippy::too_many_arguments)]
pub fn store_blob_with(
    store: &mut Store,
    piv: &dyn PivBackend,
    protection: &dyn Protection,
    name: &str,
    payload: &[u8],
    encrypt: bool,
    compression: Compression,
    management_key: Option<&str>,
) -> Result<bool> {
    store_blob_with_checked(
        store,
        piv,
        protection,
        name,
        payload,
        encrypt,
        compression,
        management_key,
        None,
    )
}

/// What a write checks on the store it reads under the card lock, before
/// writing anything: an error stops the write (spec 0049, S4).
pub type BeforeWrite<'a> = &'a dyn Fn(&Store) -> Result<()>;

/// The blob changed on the YubiKey since the snapshot a write was asked
/// to check against (spec 0049, S4): the write wrote nothing.
#[derive(Debug)]
pub struct BlobChanged(pub String);

impl std::fmt::Display for BlobChanged {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "'{}' changed on the YubiKey since it was read; nothing was written",
            self.0
        )
    }
}

impl std::error::Error for BlobChanged {}

/// A [`BeforeWrite`] check (spec 0049, S4): fails with [`BlobChanged`] if
/// the blob `name` in the store read under the lock (its head object,
/// bytes and age) differs from the blob in `snapshot`: rewritten, removed,
/// or created since.
pub fn unchanged_since<'a>(snapshot: &Store, name: &'a str) -> impl Fn(&Store) -> Result<()> + 'a {
    let head = |store: &Store| store.find_head(name).map(|h| (h.index(), h.to_bytes()));
    let before = head(snapshot);
    move |now: &Store| {
        if head(now) == before {
            Ok(())
        } else {
            Err(BlobChanged(name.to_owned()).into())
        }
    }
}

/// Read the whole store again, under the card lock the caller holds, and
/// plan from that read (spec 0048, S2): `store` becomes the store as it is.
/// Fails, writing nothing, if it is no longer the same store (formatted
/// again since `store` was read).
fn reload_for_write(store: &mut Store, piv: &dyn PivBackend) -> Result<()> {
    let fresh = Store::from_device(&store.reader, piv)?;
    let identity = |s: &Store| {
        (
            s.kind.is_post_quantum(),
            s.object_count,
            s.store_key_slot,
            s.descriptor().map(|d| d.to_bytes()),
        )
    };
    if identity(&fresh) != identity(store) {
        bail!("the store was formatted again meanwhile; nothing was written");
    }
    *store = fresh;
    Ok(())
}

/// [`store_blob_with`], with `before_write` checked on the store read
/// under the card lock.
///
/// The order (spec 0048, S2): the room, from `store` as the caller read
/// it; the protection, which may wait for the user (PIN, touch); then,
/// under the card lock, a whole read of the store, `before_write`, the
/// room again, and the writes.  `store` is left as that read found it,
/// with this write applied.
#[allow(clippy::too_many_arguments)]
pub fn store_blob_with_checked(
    store: &mut Store,
    piv: &dyn PivBackend,
    protection: &dyn Protection,
    name: &str,
    payload: &[u8],
    encrypt: bool,
    compression: Compression,
    management_key: Option<&str>,
    before_write: Option<BeforeWrite<'_>>,
) -> Result<bool> {
    validate_name(name)?;

    // blob_plain_size is a u24 field whose bit 23 is the C flag;
    // the size must not touch that bit or any higher bit.
    if payload.len() & BLOB_SIZE_C_BIT != 0 {
        anyhow::bail!(
            "blob '{}': plaintext too large ({} bytes, max 0x7FFFFF)",
            name,
            payload.len()
        );
    }

    let (candidate, is_compressed) = compress(name, payload, compression)?;
    let (encryption_overhead, trailer_len) = protection.overhead(encrypt);
    let data_len = candidate.len() + encryption_overhead;
    // blob_size is a u24 field; bit 23 is the C flag and must not be set.
    if data_len & BLOB_SIZE_C_BIT != 0 {
        anyhow::bail!("blob '{name}': stored size too large ({data_len} bytes, max 0x7FFFFF)");
    }

    let name_len = name.len();

    // The old copy, if any, stays occupied until the new one is written
    // (spec 0031): the new copy, trailer included, goes into objects that
    // are free now, so a YubiKey pulled out mid-write leaves the old copy
    // intact.
    let needed = chunks_needed(data_len + trailer_len, name_len, store.object_size);
    if store.free_count() < needed {
        return Ok(false);
    }
    store.ensure_age_room(needed)?;

    // Protect before any write (spec 0028): signing or sealing may fail
    // (PIN, touch), and then nothing is written, not even the cleanup.
    let mtime = Store::now_unix();
    let plain_size = payload.len() as u32;
    let protected =
        protection.protect(name, &candidate, encrypt, is_compressed, plain_size, mtime)?;
    if protected.stored.len() != data_len || protected.trailer.len() != trailer_len {
        bail!("blob '{name}': protected size differs from the announced overhead (bug)");
    }
    let protected_key = protected.management_key;
    let Some(management_key) = protected_key
        .as_deref()
        .map(String::as_str)
        .or(management_key)
    else {
        bail!("blob '{name}': no management key to write it");
    };
    let data = protected.stored;
    let blob_key_slot = protected.key_byte;
    let blob_size = data.len() as u32;

    // Under the card lock, plan from the store as it is (spec 0048): the
    // protection above does not depend on where the blob goes.
    let _lock = piv.exclusive(&store.reader)?;
    reload_for_write(store, piv)?;
    if let Some(check) = before_write {
        check(store)?;
    }
    if store.free_count() < needed {
        return Ok(false);
    }
    store.ensure_age_room(needed)?;

    // Clean up first (spec 0038): the leftovers loading reset, and objects
    // a card out of memory deleted, are written before any new data, so
    // that the memory they held is free for it.
    store.sync(piv, management_key)?;
    let old_chain: Vec<u8> = store
        .find_head(name)
        .map(|head| store.chunk_chain(head.index))
        .unwrap_or_default();

    // Calculate how many chunks we need.
    let head_cap = Object::head_payload_capacity(store.object_size, name_len);
    let cont_cap = Object::continuation_payload_capacity(store.object_size);

    let chunks_needed = chunks_needed(data.len(), name_len, store.object_size);

    // Allocate chunks and fill them.
    let mut chunk_indices: Vec<u8> = Vec::with_capacity(chunks_needed);
    for _ in 0..chunks_needed {
        let idx = store.alloc_free().unwrap();
        chunk_indices.push(idx);
        // Mark as provisionally occupied so alloc_free doesn't pick it again.
        store.objects[idx as usize].age = 1; // temporary marker
    }

    // Head chunk.
    let head_idx = chunk_indices[0];
    let head_payload_end = head_cap.min(data.len());
    let age = store.next_age();
    let next = if chunks_needed == 1 {
        head_idx
    } else {
        chunk_indices[1]
    };
    let mut head_obj = store.make_object(ObjectParams {
        index: head_idx,
        age,
        chunk_pos: 0,
        next_chunk: next,
    });
    head_obj.blob_mtime = mtime;
    head_obj.blob_size = blob_size;
    head_obj.blob_key_slot = blob_key_slot;
    head_obj.blob_plain_size = plain_size;
    head_obj.is_compressed = is_compressed;
    head_obj.blob_name = name.to_owned();
    head_obj.payload = data[..head_payload_end].to_vec();
    store.objects[head_idx as usize] = head_obj;

    // Continuation chunks.
    let mut offset = head_payload_end;
    for (i, &idx) in chunk_indices[1..].iter().enumerate() {
        let chunk_num = i + 1;
        let end = (offset + cont_cap).min(data.len());
        let age = store.next_age();
        let next = if chunk_num + 1 < chunks_needed {
            chunk_indices[chunk_num + 1]
        } else {
            idx // self-reference = last chunk
        };
        let mut cont_obj = store.make_object(ObjectParams {
            index: idx,
            age,
            chunk_pos: chunk_num as u8,
            next_chunk: next,
        });
        cont_obj.payload = data[offset..end].to_vec();
        store.objects[idx as usize] = cont_obj;
        offset = end;
    }

    // Append the trailer (specs 0017, 0033: the signature), in a spill
    // chunk if needed.
    append_trailer(store, &protected.trailer)?;

    // Write the new copy: its continuation chunks (and signature spill
    // chunk) first, its head last, so that the head never reaches the card
    // before its chain is complete (spec 0031).
    let mut order = store.chunk_chain(head_idx);
    order.rotate_left(1);
    store.sync_ordered(piv, management_key, &order)?;

    // Only now free the old copy.  If this is interrupted, the new copy is
    // complete and newer, and the leftovers go at the next write.
    for idx in old_chain {
        store.objects[idx as usize].reset();
    }
    store.sync(piv, management_key)?;
    Ok(true)
}

// ---------------------------------------------------------------------------
// fetch_blob
// ---------------------------------------------------------------------------

/// Whether the blob of `head` is encrypted with the legacy, unauthenticated
/// AES-CBC scheme (before spec 0006); its first stored bytes are in the head.
pub fn uses_legacy_encryption(head: &Object) -> bool {
    head.is_encrypted() && crypto::is_legacy_ciphertext(head.payload())
}

/// Fetch a blob by name from a legacy store (`pin` decrypts).  Returns
/// None if not found.
pub fn fetch_blob(
    store: &Store,
    piv: &dyn PivBackend,
    reader: &str,
    name: &str,
    pin: Option<&str>,
    debug: bool,
) -> Result<Option<Vec<u8>>> {
    let protection = LegacyProtection {
        piv,
        reader,
        slot: store.store_key_slot,
        encryption_key: None,
        verifying_key: None,
        pin,
        debug,
    };
    fetch_blob_with(store, &protection, name)
}

/// Why a fetch without PIN refused a blob (spec 0044).
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum NoPinFetchError {
    /// Encrypted blobs need the PIN; nothing was returned.
    #[error("'{0}' is encrypted: it cannot be read without the PIN")]
    Encrypted(String),
    /// A legacy blob whose signature does not verify.
    #[error("'{0}' is corrupted or was tampered with: its signature does not verify")]
    Corrupted(String),
}

/// Fetch an unencrypted blob without any PIN (spec 0044), with the check
/// that needs none:
/// - legacy stores: its signature, against the slot's certificate;
/// - post-quantum stores: its checksum only, not its seal nor the
///   management key, so corruption is detected, not forgery.
///
/// Encrypted blobs are refused ([`NoPinFetchError::Encrypted`]).  Returns
/// the content and the verdict, or None if not found.  Writes nothing.
pub fn fetch_unencrypted(
    store: &Store,
    piv: &dyn PivBackend,
    reader: &str,
    name: &str,
) -> Result<Option<(Vec<u8>, SigVerdict)>> {
    let Some(head) = store.find_head(name) else {
        return Ok(None);
    };
    if head.is_encrypted() {
        return Err(NoPinFetchError::Encrypted(name.to_owned()).into());
    }
    if store.kind.is_post_quantum() {
        #[cfg(not(windows))]
        {
            let data = fetch_blob_with(store, &post_quantum::ChecksumOnly, name)?;
            return Ok(data.map(|d| (d, SigVerdict::ChecksumOk)));
        }
        #[cfg(windows)]
        bail!("post-quantum stores are not supported on Windows");
    }
    let verifying_key = piv
        .read_certificate(reader, store.store_key_slot)
        .ok()
        .and_then(|der| crate::parse_ec_public_key_from_cert_der(&der).ok())
        .map(|pk| p256::ecdsa::VerifyingKey::from(&pk));
    let verdict = check_blob_signature(head, store, verifying_key.as_ref());
    if verdict == SigVerdict::Corrupted {
        return Err(NoPinFetchError::Corrupted(name.to_owned()).into());
    }
    let data = fetch_blob(store, piv, reader, name, None, false)?;
    Ok(data.map(|d| (d, verdict)))
}

/// Fetch a blob by name, opened by `protection` (spec 0028).  Returns None
/// if not found.
pub fn fetch_blob_with(
    store: &Store,
    protection: &dyn Protection,
    name: &str,
) -> Result<Option<Vec<u8>>> {
    let Some(head) = store.find_head(name) else {
        return Ok(None);
    };
    let meta = BlobMeta::from_head(head);
    let (stored, trailer, _) = collect_blob_chain(head, store);
    let payload = protection.open(&meta, &stored, &trailer)?;
    if meta.compressed {
        Ok(Some(decompress_payload(name, &payload)?))
    } else {
        Ok(Some(payload))
    }
}

// ---------------------------------------------------------------------------
// remove_blob
// ---------------------------------------------------------------------------

/// Remove a blob by name.  Returns false if not found.
pub fn remove_blob(
    store: &mut Store,
    piv: &dyn PivBackend,
    name: &str,
    management_key: &str,
) -> Result<bool> {
    Ok(remove_blobs(store, piv, &[name], management_key)? == 1)
}

/// Remove blobs by name in one write pass, and return how many were found.
///
/// All heads are written first: once a head is gone, the rest of its chain
/// is a leftover, so an interruption leaves each blob either whole or gone
/// (spec 0031).
pub fn remove_blobs<S: AsRef<str>>(
    store: &mut Store,
    piv: &dyn PivBackend,
    names: &[S],
    management_key: &str,
) -> Result<usize> {
    remove_blobs_checked(store, piv, names, management_key, None)
}

/// [`remove_blobs`], under the card lock from a whole read of the store
/// (spec 0048), with `before_write` checked on that read.  `store` is left
/// as that read found it, with the removal applied.
pub fn remove_blobs_checked<S: AsRef<str>>(
    store: &mut Store,
    piv: &dyn PivBackend,
    names: &[S],
    management_key: &str,
    before_write: Option<BeforeWrite<'_>>,
) -> Result<usize> {
    let _lock = piv.exclusive(&store.reader)?;
    reload_for_write(store, piv)?;
    if let Some(check) = before_write {
        check(store)?;
    }
    let heads: Vec<u8> = names
        .iter()
        .filter_map(|name| store.find_head(name.as_ref()).map(|h| h.index))
        .collect();
    if heads.is_empty() {
        return Ok(0);
    }
    // Clean up first (spec 0038), as in `store_blob`.
    store.sync(piv, management_key)?;
    for &head_idx in &heads {
        for idx in store.chunk_chain(head_idx) {
            store.objects[idx as usize].reset();
        }
    }
    store.sync_ordered(piv, management_key, &heads)?;
    Ok(heads.len())
}

// ---------------------------------------------------------------------------
// list_blobs
// ---------------------------------------------------------------------------

/// Return metadata for all blobs, sorted by name.
pub fn list_blobs(store: &Store) -> Vec<BlobInfo> {
    let mut blobs: Vec<BlobInfo> = store
        .objects
        .iter()
        .filter(|o| o.is_head())
        .map(|head| {
            let chain = store.chunk_chain(head.index);
            BlobInfo {
                name: head.blob_name.clone(),
                encrypted_size: head.blob_size,
                plain_size: head.blob_plain_size,
                is_encrypted: head.is_encrypted(),
                is_legacy_encryption: uses_legacy_encryption(head),
                mtime: head.blob_mtime,
                chunk_count: chain.len(),
            }
        })
        .collect();

    blobs.sort_by(|a, b| a.name.cmp(&b.name));
    blobs
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn validate_name(name: &str) -> Result<()> {
    if name.is_empty() {
        bail!("blob name must not be empty");
    }
    if name.len() > MAX_NAME_LEN {
        bail!("blob name too long ({} > {MAX_NAME_LEN} bytes)", name.len());
    }
    if name.contains('\0') || name.contains('/') {
        bail!("blob name must not contain null bytes or '/'");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_name_invalid_chars() {
        // Null byte and slash are forbidden.
        assert!(validate_name("foo\0bar").is_err());
        assert!(validate_name("foo/bar").is_err());
        assert!(validate_name("/leading").is_err());
        assert!(validate_name("trailing/").is_err());

        // Everything else is fine: spaces, Unicode, emoji, other punctuation.
        assert!(validate_name("hello world").is_ok());
        assert!(validate_name("café").is_ok());
        assert!(validate_name("blob-name_v2.bin").is_ok());
        assert!(validate_name("密钥").is_ok());
        assert!(validate_name("🔑").is_ok());
        assert!(validate_name("tab\there").is_ok());
    }

    /// T6: validate_name length boundaries — empty, max-length, and one over max.
    #[test]
    fn validate_name_length_boundaries() {
        assert!(validate_name("").is_err(), "empty name must be rejected");
        assert!(
            validate_name(&"x".repeat(MAX_NAME_LEN)).is_ok(),
            "max-length name must be accepted"
        );
        assert!(
            validate_name(&"x".repeat(MAX_NAME_LEN + 1)).is_err(),
            "name one byte over max must be rejected"
        );
    }

    /// T14: store_blob returns false (not an error) when the store is full,
    /// and leaves the store unchanged.
    #[test]
    fn store_blob_returns_false_when_full() {
        use crate::piv::VirtualPiv;
        use crate::store::Store;
        use std::path::Path;

        let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/with_key.yaml");
        let piv = VirtualPiv::from_fixture(&fixture).unwrap();
        let mgmt = "010203040506070801020304050607080102030405060708";
        let reader = piv.reader_name();

        // Format a tiny store: 2 objects.
        let mut store = Store::format(&reader, &piv, 2, 0x82, mgmt).unwrap();

        // Fill both slots with single-chunk blobs.
        let ok1 = store_blob(
            &mut store,
            &piv,
            "a",
            b"aaa",
            StoreOptions {
                encryption: Encryption::None,
                compression: Compression::None,
            },
            mgmt,
            None,
        )
        .unwrap();
        assert!(ok1, "first blob should fit");
        let ok2 = store_blob(
            &mut store,
            &piv,
            "b",
            b"bbb",
            StoreOptions {
                encryption: Encryption::None,
                compression: Compression::None,
            },
            mgmt,
            None,
        )
        .unwrap();
        assert!(ok2, "second blob should fit");

        assert_eq!(store.free_count(), 0, "store should be full");

        // Attempt to store a third blob — must return false, not an error.
        let result = store_blob(
            &mut store,
            &piv,
            "c",
            b"ccc",
            StoreOptions {
                encryption: Encryption::None,
                compression: Compression::None,
            },
            mgmt,
            None,
        );
        assert!(result.is_ok(), "full store must not error");
        assert!(!result.unwrap(), "full store must return false");

        // Store must be unchanged — still two blobs.
        assert_eq!(list_blobs(&store).len(), 2, "store must be unmodified");
    }
}