revault_vault_api 0.0.2

reVault API for creating and managing vaults
Documentation
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
use super::{
    encode_control_err_response, encode_control_ok_response, encode_err_response, encode_forget,
    encode_forget_all, encode_get, encode_key_response, encode_list, encode_list_response,
    encode_miss_response, encode_ok_response, encode_put, encode_register_secret_activity,
    encode_registered_response, encode_stop, encode_unregister_secret_activity, frame_header_len,
    frame_message_type, frame_payload_len, is_control_message_type, max_message_bytes,
    parse_control_request, parse_control_response, parse_request, parse_response, AgentRequest,
    AgentResponse, CachedLockbox, ControlRequest, ControlResponse, SecretActivityKind, SecretVec,
    DEFAULT_TTL_SECONDS,
};
use crate::active_secret::ActiveSecretRegistry;
use crate::agent_config::AgentConfig;
use crate::agent_log::log_agent_event;
use crate::sleep_watcher::{SleepEvent, SleepWatcher};
use revault_lockbox_api::LockboxId;
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;
use std::env;
use std::ffi::c_void;
use std::hash::{Hash, Hasher};
use std::io;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::process::{Command, Stdio};
use std::ptr::{null, null_mut};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use windows_sys::Win32::Foundation::{
    CloseHandle, GetLastError, LocalFree, ERROR_FILE_NOT_FOUND, ERROR_NO_TOKEN, ERROR_PIPE_BUSY,
    ERROR_PIPE_CONNECTED, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::Security::Authorization::{
    ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
};
use windows_sys::Win32::Security::{
    EqualSid, GetTokenInformation, RevertToSelf, TokenUser, PSECURITY_DESCRIPTOR,
    SECURITY_ATTRIBUTES, TOKEN_QUERY, TOKEN_USER,
};
use windows_sys::Win32::Storage::FileSystem::{
    CreateFileW, ReadFile, WriteFile, FILE_ATTRIBUTE_NORMAL, OPEN_EXISTING, PIPE_ACCESS_DUPLEX,
};
use windows_sys::Win32::System::Pipes::{
    ConnectNamedPipe, CreateNamedPipeW, DisconnectNamedPipe, ImpersonateNamedPipeClient,
    WaitNamedPipeW, PIPE_READMODE_BYTE, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, PIPE_WAIT,
};
use windows_sys::Win32::System::Threading::{
    GetCurrentProcess, GetCurrentThread, OpenProcessToken, OpenThreadToken,
};

const IDLE_EXIT_SECONDS: u64 = 10 * 60;
const PIPE_OPEN_TIMEOUT: Duration = Duration::from_secs(3);
const PIPE_BUFFER_BYTES: u32 = 64 * 1024;

struct CacheEntry {
    key: SecretVec,
    path: Option<String>,
    ttl_seconds: u64,
    expires_at: Instant,
}

pub(crate) fn serve_agent() -> io::Result<()> {
    log_agent_event("agent starting");
    let current_user_sid = current_process_user_sid()?;
    let cache = Arc::new(Mutex::new(BTreeMap::<String, CacheEntry>::new()));
    let config = AgentConfig::load();
    log_agent_event(format!(
        "agent config prevent_sleep={} terminate_on_suspend={}",
        config.prevent_sleep, config.terminate_on_suspend
    ));
    let active = Arc::new(Mutex::new(ActiveSecretRegistry::new(config)));
    let mut last_activity = Instant::now();
    start_sleep_cache_clearer(cache.clone(), active.clone());

    loop {
        {
            let mut cache = lock_cache(&cache)?;
            log_pruned_expired(&mut cache);
            let active_empty = lock_active(&active)?.is_empty();
            if cache.is_empty()
                && active_empty
                && last_activity.elapsed() > Duration::from_secs(IDLE_EXIT_SECONDS)
            {
                log_agent_event("agent exiting after idle timeout");
                return Ok(());
            }
        }

        let pipe = create_pipe()?;
        let connected = connect_pipe(pipe.as_raw());
        if let Err(err) = connected {
            return Err(err);
        }
        log_agent_event("agent client connected");

        last_activity = Instant::now();
        let stop = {
            let mut cache = match lock_cache(&cache) {
                Ok(cache) => cache,
                Err(err) => {
                    log_agent_event(format!("agent cache lock failed: {err}"));
                    return Err(err);
                }
            };
            log_agent_event("agent cache locked");
            match catch_unwind(AssertUnwindSafe(|| {
                handle_client(pipe.as_raw(), &current_user_sid, &mut cache, &active)
            })) {
                Ok(Ok(stop)) => {
                    log_agent_event("agent request handled");
                    stop
                }
                Ok(Err(err)) => {
                    log_agent_event(format!("agent request failed: {err}"));
                    false
                }
                Err(_) => {
                    log_agent_event("agent request panicked");
                    false
                }
            }
        };
        disconnect_pipe(pipe.as_raw());
        if stop {
            log_agent_event("agent stopped by request");
            return Ok(());
        }
    }
}

fn start_sleep_cache_clearer(
    cache: Arc<Mutex<BTreeMap<String, CacheEntry>>>,
    active: Arc<Mutex<ActiveSecretRegistry>>,
) {
    let Ok(watcher) = SleepWatcher::start() else {
        log_agent_event("sleep watcher unavailable");
        return;
    };
    log_agent_event("sleep watcher started");
    let _ = thread::Builder::new()
        .name("lockbox-sleep-cache-clearer".to_string())
        .spawn(move || {
            while let Ok(event) = watcher.recv() {
                if event == SleepEvent::SuspendRequested {
                    if let Ok(mut cache) = cache.lock() {
                        let count = cache.len();
                        cache.clear();
                        log_agent_event(format!(
                            "suspend requested; cleared {count} cached lockboxes"
                        ));
                    }
                    if let Ok(mut active) = active.lock() {
                        active.suspend_requested();
                    }
                } else if event == SleepEvent::Resumed {
                    log_agent_event("resume observed");
                }
            }
        });
}

fn lock_cache(
    cache: &Arc<Mutex<BTreeMap<String, CacheEntry>>>,
) -> io::Result<std::sync::MutexGuard<'_, BTreeMap<String, CacheEntry>>> {
    cache
        .lock()
        .map_err(|_| io::Error::other("session agent cache lock was poisoned"))
}

fn lock_active(
    active: &Arc<Mutex<ActiveSecretRegistry>>,
) -> io::Result<std::sync::MutexGuard<'_, ActiveSecretRegistry>> {
    active
        .lock()
        .map_err(|_| io::Error::other("session agent activity lock was poisoned"))
}

pub(crate) fn verify_agent_transport_security() -> io::Result<()> {
    let _security = PipeSecurity::current_owner_only()?;
    let _sid = current_process_user_sid()?;
    Ok(())
}

pub(crate) fn get(lockbox_id: LockboxId) -> io::Result<Option<SecretVec>> {
    match request_existing(&encode_get(lockbox_id)?)? {
        Some(AgentResponse::Key(key)) => Ok(Some(key)),
        Some(AgentResponse::Miss) | None => Ok(None),
        Some(response) => invalid_agent_response(response),
    }
}

pub(crate) fn put(
    lockbox_id: LockboxId,
    key: &SecretVec,
    path: Option<&str>,
    ttl_seconds: Option<u64>,
) -> io::Result<()> {
    expect_ok(request(&encode_put(lockbox_id, key, path, ttl_seconds)?)?)
}

pub(crate) fn forget(lockbox_id: LockboxId) -> io::Result<()> {
    match request_existing(&encode_forget(lockbox_id)?)? {
        Some(response) => expect_ok(response),
        None => Ok(()),
    }
}

pub(crate) fn forget_all() -> io::Result<()> {
    match request_existing(&encode_forget_all()?)? {
        Some(response) => expect_ok(response),
        None => Ok(()),
    }
}

pub(crate) fn stop() -> io::Result<()> {
    match request_existing(&encode_stop()?)? {
        Some(response) => expect_ok(response),
        None => Ok(()),
    }
}

pub(crate) fn list() -> io::Result<Vec<CachedLockbox>> {
    match request_existing(&encode_list()?)? {
        Some(AgentResponse::List(ids)) => Ok(ids),
        None => Ok(Vec::new()),
        Some(response) => invalid_agent_response(response),
    }
}

pub(crate) fn register_secret_activity(kind: SecretActivityKind) -> io::Result<u64> {
    match request_control(&encode_register_secret_activity(std::process::id(), kind)?)? {
        ControlResponse::Registered(token) => Ok(token),
        response => invalid_control_response(response),
    }
}

pub(crate) fn unregister_secret_activity(pid: u32, token: u64) -> io::Result<()> {
    match request_control_existing(&encode_unregister_secret_activity(pid, token)?)? {
        Some(response) => expect_control_ok(response),
        None => Ok(()),
    }
}

pub(crate) fn is_running() -> bool {
    open_pipe(&wide_pipe_name()).is_ok()
}

fn request(message: &SecretVec) -> io::Result<AgentResponse> {
    let pipe_name = wide_pipe_name();
    let handle = match open_pipe(&pipe_name) {
        Ok(handle) => handle,
        Err(_) => {
            start_agent()?;
            open_pipe(&pipe_name)?
        }
    };
    request_with_handle(handle, message)
}

fn request_existing(message: &SecretVec) -> io::Result<Option<AgentResponse>> {
    let pipe_name = wide_pipe_name();
    let Ok(handle) = open_pipe(&pipe_name) else {
        return Ok(None);
    };
    request_with_handle(handle, message).map(Some)
}

fn request_control(message: &[u8]) -> io::Result<ControlResponse> {
    let pipe_name = wide_pipe_name();
    let handle = match open_pipe(&pipe_name) {
        Ok(handle) => handle,
        Err(_) => {
            start_agent()?;
            open_pipe(&pipe_name)?
        }
    };
    request_control_with_handle(handle, message)
}

fn request_control_existing(message: &[u8]) -> io::Result<Option<ControlResponse>> {
    let pipe_name = wide_pipe_name();
    let Ok(handle) = open_pipe(&pipe_name) else {
        return Ok(None);
    };
    request_control_with_handle(handle, message).map(Some)
}

fn request_with_handle(handle: OwnedHandle, message: &SecretVec) -> io::Result<AgentResponse> {
    message
        .with_bytes(|message| write_all(handle.as_raw(), message))
        .map_err(io::Error::other)?
        .map_err(|err| io::Error::new(err.kind(), format!("agent pipe write failed: {err}")))?;
    parse_response(
        read_secure_frame(handle.as_raw(), max_message_bytes())
            .map_err(|err| io::Error::new(err.kind(), format!("agent pipe read failed: {err}")))?,
    )
}

fn request_control_with_handle(handle: OwnedHandle, message: &[u8]) -> io::Result<ControlResponse> {
    write_all(handle.as_raw(), message)
        .map_err(|err| io::Error::new(err.kind(), format!("agent pipe write failed: {err}")))?;
    parse_control_response(
        &read_plain_frame(handle.as_raw(), max_message_bytes())
            .map_err(|err| io::Error::new(err.kind(), format!("agent pipe read failed: {err}")))?,
    )
}

fn start_agent() -> io::Result<()> {
    let exe = env::current_exe()?;
    Command::new(exe)
        .arg("__agent")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;

    Ok(())
}

fn create_pipe() -> io::Result<OwnedHandle> {
    let name = wide_pipe_name();
    let mut security = PipeSecurity::current_owner_only()?;
    // SAFETY: `name` is a null-terminated UTF-16 string, `security` owns a
    // valid `SECURITY_ATTRIBUTES` for the duration of the call, and no pointer
    // is retained by Rust after the OS returns.
    let handle = unsafe {
        CreateNamedPipeW(
            name.as_ptr(),
            PIPE_ACCESS_DUPLEX,
            PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
            PIPE_UNLIMITED_INSTANCES,
            PIPE_BUFFER_BYTES,
            PIPE_BUFFER_BYTES,
            0,
            security.as_mut_ptr(),
        )
    };
    if handle == INVALID_HANDLE_VALUE {
        Err(io::Error::last_os_error())
    } else {
        Ok(OwnedHandle::new(handle))
    }
}

struct OwnedHandle {
    handle: HANDLE,
}

impl OwnedHandle {
    fn new(handle: HANDLE) -> Self {
        Self { handle }
    }

    fn as_raw(&self) -> HANDLE {
        self.handle
    }
}

impl Drop for OwnedHandle {
    fn drop(&mut self) {
        close_handle(self.handle);
    }
}

fn close_handle(handle: HANDLE) {
    if handle.is_null() || handle == INVALID_HANDLE_VALUE {
        return;
    }
    // SAFETY: `handle` is owned by an `OwnedHandle` or explicit close site and
    // is not used after this call.
    unsafe {
        CloseHandle(handle);
    }
}

struct PipeSecurity {
    descriptor: PSECURITY_DESCRIPTOR,
    attributes: SECURITY_ATTRIBUTES,
}

impl PipeSecurity {
    fn current_owner_only() -> io::Result<Self> {
        let sddl = to_wide("D:P(A;;GA;;;OW)");
        let mut descriptor: PSECURITY_DESCRIPTOR = null_mut();
        // SAFETY: `sddl` is a null-terminated UTF-16 SDDL string and
        // `descriptor` is a valid out pointer initialized by the OS on success.
        let ok = unsafe {
            ConvertStringSecurityDescriptorToSecurityDescriptorW(
                sddl.as_ptr(),
                SDDL_REVISION_1,
                &mut descriptor,
                null_mut(),
            )
        };
        if ok == 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(Self {
            descriptor,
            attributes: SECURITY_ATTRIBUTES {
                nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
                lpSecurityDescriptor: descriptor.cast(),
                bInheritHandle: 0,
            },
        })
    }

    fn as_mut_ptr(&mut self) -> *mut SECURITY_ATTRIBUTES {
        &mut self.attributes
    }
}

impl Drop for PipeSecurity {
    fn drop(&mut self) {
        if !self.descriptor.is_null() {
            // SAFETY: `descriptor` was allocated by
            // `ConvertStringSecurityDescriptorToSecurityDescriptorW` and is
            // released exactly once by this owner.
            unsafe {
                LocalFree(self.descriptor.cast());
            }
        }
    }
}

fn connect_pipe(pipe: HANDLE) -> io::Result<()> {
    // SAFETY: `pipe` is a named-pipe handle returned by `CreateNamedPipeW`;
    // `null_mut` means synchronous operation without overlapped state.
    let ok = unsafe { ConnectNamedPipe(pipe, null_mut()) };
    if ok != 0 {
        return Ok(());
    }
    let err = last_error();
    if err == ERROR_PIPE_CONNECTED {
        Ok(())
    } else {
        Err(io::Error::from_raw_os_error(err as i32))
    }
}

fn disconnect_pipe(pipe: HANDLE) {
    // SAFETY: Disconnection does not close the handle; `OwnedHandle` closes it
    // later. The response frame has already been written synchronously; forcing
    // a server-side flush can deadlock with clients that issue a follow-up
    // control request while the agent is still waiting for the pipe to drain.
    unsafe {
        DisconnectNamedPipe(pipe);
    }
}

fn last_error() -> u32 {
    // SAFETY: `GetLastError` reads thread-local OS error state and has no
    // Rust-side memory invariants.
    unsafe { GetLastError() }
}

fn open_pipe(pipe_name: &[u16]) -> io::Result<OwnedHandle> {
    let deadline = Instant::now() + PIPE_OPEN_TIMEOUT;
    loop {
        // SAFETY: `pipe_name` is a null-terminated UTF-16 string and all
        // pointer arguments either reference valid data for the call or are
        // intentionally null per the Windows API contract.
        let handle = unsafe {
            CreateFileW(
                pipe_name.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                0,
                null(),
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                null_mut(),
            )
        };
        if handle != INVALID_HANDLE_VALUE {
            return Ok(OwnedHandle::new(handle));
        }
        let err = last_error();
        if err == ERROR_FILE_NOT_FOUND {
            if Instant::now() >= deadline {
                return Err(io::Error::from_raw_os_error(err as i32));
            }
            thread::sleep(Duration::from_millis(25));
            continue;
        }
        if err != ERROR_PIPE_BUSY {
            return Err(io::Error::from_raw_os_error(err as i32));
        }
        // SAFETY: `pipe_name` is a null-terminated UTF-16 string valid for the
        // duration of the wait call.
        let waited = unsafe { WaitNamedPipeW(pipe_name.as_ptr(), 3000) };
        if waited == 0 {
            return Err(io::Error::last_os_error());
        }
    }
}

fn handle_client(
    pipe: HANDLE,
    current_user_sid: &[u8],
    cache: &mut BTreeMap<String, CacheEntry>,
    active: &Arc<Mutex<ActiveSecretRegistry>>,
) -> io::Result<bool> {
    let request = read_agent_frame(pipe, max_message_bytes())?;
    if !client_matches_current_user(pipe, current_user_sid)? {
        log_agent_event("rejected agent request from different user");
        return Ok(false);
    }
    log_agent_event("agent request user accepted");
    let mut stop = false;
    let response = match request {
        AgentFrame::Cache(request) => {
            let response = handle_cache_request(&request, cache, &mut stop)?;
            AgentReply::Cache(response)
        }
        AgentFrame::Control(request) => {
            let response = handle_control_request(&request, active)?;
            AgentReply::Control(response)
        }
    };
    write_agent_reply(pipe, response)?;
    Ok(stop)
}

fn handle_cache_request(
    request: &SecretVec,
    cache: &mut BTreeMap<String, CacheEntry>,
    stop: &mut bool,
) -> io::Result<SecretVec> {
    let response = match parse_request(request) {
        Ok(AgentRequest::Get(lockbox_id)) => {
            log_agent_event(format!("agent request get {lockbox_id}"));
            let now = Instant::now();
            match cache.get_mut(&lockbox_id) {
                Some(entry) if entry.expires_at > now => {
                    entry.expires_at = now + Duration::from_secs(entry.ttl_seconds);
                    log_agent_event(format!("cache hit {lockbox_id}"));
                    encode_key_response(&entry.key)?
                }
                _ => {
                    log_agent_event(format!("cache miss {lockbox_id}"));
                    encode_miss_response()?
                }
            }
        }
        Ok(AgentRequest::Put(lockbox_id, key, path, ttl_seconds)) => {
            log_agent_event(format!("agent request put {lockbox_id}"));
            let ttl_seconds = ttl_seconds.unwrap_or(DEFAULT_TTL_SECONDS);
            log_agent_event(format!(
                "cached lockbox {lockbox_id} ttl_seconds={ttl_seconds} path={}",
                path.as_deref().unwrap_or("")
            ));
            cache.insert(
                lockbox_id.clone(),
                CacheEntry {
                    key,
                    path,
                    ttl_seconds,
                    expires_at: Instant::now() + Duration::from_secs(ttl_seconds),
                },
            );
            encode_ok_response()?
        }
        Ok(AgentRequest::Forget(lockbox_id)) => {
            log_agent_event(format!("agent request forget {lockbox_id}"));
            cache.remove(&lockbox_id);
            log_agent_event(format!("forgot lockbox {lockbox_id}"));
            encode_ok_response()?
        }
        Ok(AgentRequest::ForgetAll) => {
            log_agent_event("agent request forget-all");
            let count = cache.len();
            cache.clear();
            log_agent_event(format!("forgot all cached lockboxes count={count}"));
            encode_ok_response()?
        }
        Ok(AgentRequest::Stop) => {
            log_agent_event("agent request stop");
            let count = cache.len();
            cache.clear();
            *stop = true;
            log_agent_event(format!("stop requested; cleared {count} cached lockboxes"));
            encode_ok_response()?
        }
        Ok(AgentRequest::List) => {
            log_agent_event("agent request list");
            log_agent_event(format!("listed cached lockboxes count={}", cache.len()));
            encode_list_response(cache.iter().map(|(id, entry)| CachedLockbox {
                id: id.clone(),
                path: entry.path.clone(),
            }))?
        }
        Err(err) => {
            log_agent_event(format!("agent request parse failed: {err}"));
            encode_err_response("invalid request")?
        }
    };
    Ok(response)
}

fn handle_control_request(
    request: &[u8],
    active: &Arc<Mutex<ActiveSecretRegistry>>,
) -> io::Result<Vec<u8>> {
    match parse_control_request(request) {
        Ok(ControlRequest::RegisterSecretActivity(pid, kind)) => {
            log_agent_event("agent request register-secret-activity");
            let token = lock_active(active)?.register(pid, kind)?;
            encode_registered_response(token)
        }
        Ok(ControlRequest::UnregisterSecretActivity(pid, token)) => {
            log_agent_event("agent request unregister-secret-activity");
            lock_active(active)?.unregister(pid, token);
            encode_control_ok_response()
        }
        Err(err) => {
            log_agent_event(format!("agent control request parse failed: {err}"));
            encode_control_err_response("invalid control request")
        }
    }
}

fn client_matches_current_user(pipe: HANDLE, current_user_sid: &[u8]) -> io::Result<bool> {
    let _impersonation = ImpersonationGuard::new(pipe)?;
    let Some(token) = open_thread_token()? else {
        return Ok(false);
    };
    let client_sid = token_user_sid(token.as_raw())?;
    Ok(equal_sid(&client_sid, current_user_sid))
}

fn current_process_user_sid() -> io::Result<Vec<u8>> {
    let token = open_process_token()?;
    token_user_sid(token.as_raw())
}

struct ImpersonationGuard;

impl ImpersonationGuard {
    fn new(pipe: HANDLE) -> io::Result<Self> {
        // SAFETY: `pipe` is the connected named-pipe server handle for the
        // current client request.
        let impersonated = unsafe { ImpersonateNamedPipeClient(pipe) };
        if impersonated == 0 {
            Err(io::Error::last_os_error())
        } else {
            Ok(Self)
        }
    }
}

impl Drop for ImpersonationGuard {
    fn drop(&mut self) {
        // SAFETY: If this guard exists, the current thread successfully
        // impersonated a pipe client and must be reverted before leaving scope.
        unsafe {
            RevertToSelf();
        }
    }
}

fn open_thread_token() -> io::Result<Option<OwnedHandle>> {
    let mut token: HANDLE = null_mut();
    // SAFETY: `token` is a valid out pointer and the current thread may have
    // an impersonation token after `ImpersonateNamedPipeClient`.
    let opened = unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, &mut token) };
    if opened == 0 {
        let err = last_error();
        if err == ERROR_NO_TOKEN {
            Ok(None)
        } else {
            Err(io::Error::from_raw_os_error(err as i32))
        }
    } else {
        Ok(Some(OwnedHandle::new(token)))
    }
}

fn open_process_token() -> io::Result<OwnedHandle> {
    let mut token: HANDLE = null_mut();
    // SAFETY: `token` is a valid out pointer and the process pseudo-handle is
    // valid for querying the current process token.
    let ok = unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) };
    if ok == 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(OwnedHandle::new(token))
    }
}

fn token_user_sid(token: HANDLE) -> io::Result<Vec<u8>> {
    let mut needed = 0u32;
    // SAFETY: This first call intentionally passes a null buffer to query the
    // required size in `needed`, which is the documented Windows API pattern.
    unsafe {
        GetTokenInformation(token, TokenUser, null_mut(), 0, &mut needed);
    }
    if needed == 0 {
        return Err(io::Error::last_os_error());
    }
    let mut buffer = vec![0u8; needed as usize];
    // SAFETY: `buffer` has `needed` bytes of writable storage and `needed`
    // points to the size value for the duration of the call.
    let ok = unsafe {
        GetTokenInformation(
            token,
            TokenUser,
            buffer.as_mut_ptr() as *mut c_void,
            needed,
            &mut needed,
        )
    };
    if ok == 0 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: On success, `buffer` contains a valid `TOKEN_USER` structure as
    // documented for `GetTokenInformation(TokenUser, ...)`.
    let token_user = unsafe { &*(buffer.as_ptr() as *const TOKEN_USER) };
    let sid = token_user.User.Sid;
    let sid_len = sid_length(sid)?;
    let mut out = vec![0u8; sid_len];
    // SAFETY: `sid` points inside the successful token information buffer and
    // `out` has exactly `sid_len` writable bytes. The ranges do not overlap.
    unsafe {
        std::ptr::copy_nonoverlapping(sid as *const u8, out.as_mut_ptr(), sid_len);
    }
    Ok(out)
}

fn sid_length(sid: *mut c_void) -> io::Result<usize> {
    if sid.is_null() {
        return Err(io::Error::new(io::ErrorKind::InvalidData, "null SID"));
    }
    let revision_ptr = sid as *const u8;
    // SAFETY: `sid` is expected to point to a valid SID returned by Windows.
    // The second byte is the SID sub-authority count in the documented layout.
    let sub_authority_count = unsafe { *revision_ptr.add(1) as usize };
    Ok(8 + sub_authority_count * 4)
}

fn equal_sid(left: &[u8], right: &[u8]) -> bool {
    // SAFETY: `left` and `right` are SID byte buffers copied from Windows token
    // data and remain valid for the duration of the comparison call.
    unsafe { EqualSid(left.as_ptr() as *mut c_void, right.as_ptr() as *mut c_void) != 0 }
}

fn write_all(handle: HANDLE, mut bytes: &[u8]) -> io::Result<()> {
    while !bytes.is_empty() {
        let mut written = 0u32;
        let chunk_len = bytes.len().min(PIPE_BUFFER_BYTES as usize) as u32;
        // SAFETY: `bytes` points to at least `chunk_len` readable bytes,
        // `written` is a valid out pointer, and the pipe handle is valid for
        // writing while this function owns the operation.
        let ok = unsafe { WriteFile(handle, bytes.as_ptr(), chunk_len, &mut written, null_mut()) };
        if ok == 0 {
            return Err(io::Error::last_os_error());
        }
        bytes = &bytes[written as usize..];
    }
    Ok(())
}

enum AgentFrame {
    Cache(SecretVec),
    Control(Vec<u8>),
}

enum AgentReply {
    Cache(SecretVec),
    Control(Vec<u8>),
}

fn read_agent_frame(handle: HANDLE, max_bytes: usize) -> io::Result<AgentFrame> {
    let mut header = vec![0u8; frame_header_len()];
    read_exact_plain(handle, &mut header)?;
    let payload_len = frame_payload_len(&header)?;
    let frame_len = checked_frame_len(header.len(), payload_len, max_bytes)?;
    let message_type = frame_message_type(&header)?;
    if is_control_message_type(message_type) {
        let mut frame = header;
        frame.resize(frame_len, 0);
        read_exact_plain(handle, &mut frame[frame_header_len()..])?;
        Ok(AgentFrame::Control(frame))
    } else {
        let mut out = SecretVec::new();
        out.try_extend_from_slice(&header)
            .map_err(io::Error::other)?;
        read_exact_secure(handle, payload_len, &mut out)?;
        Ok(AgentFrame::Cache(out))
    }
}

fn read_secure_frame(handle: HANDLE, max_bytes: usize) -> io::Result<SecretVec> {
    let mut header = vec![0u8; frame_header_len()];
    read_exact_plain(handle, &mut header)?;
    let payload_len = frame_payload_len(&header)?;
    checked_frame_len(header.len(), payload_len, max_bytes)?;
    let mut out = SecretVec::new();
    out.try_extend_from_slice(&header)
        .map_err(io::Error::other)?;
    read_exact_secure(handle, payload_len, &mut out)?;
    Ok(out)
}

fn read_plain_frame(handle: HANDLE, max_bytes: usize) -> io::Result<Vec<u8>> {
    let mut header = vec![0u8; frame_header_len()];
    read_exact_plain(handle, &mut header)?;
    let payload_len = frame_payload_len(&header)?;
    let frame_len = checked_frame_len(header.len(), payload_len, max_bytes)?;
    let mut frame = header;
    frame.resize(frame_len, 0);
    read_exact_plain(handle, &mut frame[frame_header_len()..])?;
    Ok(frame)
}

fn checked_frame_len(header_len: usize, payload_len: usize, max_bytes: usize) -> io::Result<usize> {
    let frame_len = header_len
        .checked_add(payload_len)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "message too large"))?;
    if frame_len > max_bytes {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "message too large",
        ));
    }
    Ok(frame_len)
}

fn write_agent_reply(pipe: HANDLE, response: AgentReply) -> io::Result<()> {
    match response {
        AgentReply::Cache(response) => response
            .with_bytes(|response| write_all(pipe, response))
            .map_err(io::Error::other)?,
        AgentReply::Control(response) => write_all(pipe, &response),
    }
}

fn read_exact_plain(handle: HANDLE, mut out: &mut [u8]) -> io::Result<()> {
    while !out.is_empty() {
        let read = read_chunk(handle, out)?;
        if read == 0 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "message ended before frame header",
            ));
        }
        let remaining = out.split_at_mut(read).1;
        out = remaining;
    }
    Ok(())
}

fn read_exact_secure(handle: HANDLE, mut remaining: usize, out: &mut SecretVec) -> io::Result<()> {
    let mut buffer = [0u8; 4096];
    while remaining > 0 {
        let read = read_chunk(handle, &mut buffer[..remaining.min(4096)])?;
        if read == 0 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "message ended before frame body",
            ));
        }
        out.try_extend_from_slice(&buffer[..read])
            .map_err(io::Error::other)?;
        buffer[..read].fill(0);
        remaining -= read;
    }
    Ok(())
}

fn read_chunk(handle: HANDLE, buffer: &mut [u8]) -> io::Result<usize> {
    let mut read = 0u32;
    // SAFETY: `buffer` and `read` are valid writable storage for the duration
    // of the call, and the pipe handle is valid for reading.
    let ok = unsafe {
        ReadFile(
            handle,
            buffer.as_mut_ptr(),
            buffer.len() as u32,
            &mut read,
            null_mut(),
        )
    };
    if ok == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(read as usize)
}

fn prune_expired(cache: &mut BTreeMap<String, CacheEntry>) {
    let now = Instant::now();
    cache.retain(|_, entry| entry.expires_at > now);
}

fn log_pruned_expired(cache: &mut BTreeMap<String, CacheEntry>) {
    let before = cache.len();
    prune_expired(cache);
    let pruned = before.saturating_sub(cache.len());
    if pruned != 0 {
        log_agent_event(format!("pruned expired cached lockboxes count={pruned}"));
    }
}

fn expect_ok(response: AgentResponse) -> io::Result<()> {
    match response {
        AgentResponse::Ok => Ok(()),
        AgentResponse::Err(message) => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("agent rejected request: {message}"),
        )),
        other => invalid_agent_response(other),
    }
}

fn expect_control_ok(response: ControlResponse) -> io::Result<()> {
    match response {
        ControlResponse::Ok => Ok(()),
        ControlResponse::Err(message) => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("agent rejected control request: {message}"),
        )),
        other => invalid_control_response(other),
    }
}

fn invalid_agent_response<T>(response: AgentResponse) -> io::Result<T> {
    let label = match response {
        AgentResponse::Ok => "OK",
        AgentResponse::Miss => "MISS",
        AgentResponse::Key(_) => "KEY",
        AgentResponse::List(_) => "LIST",
        AgentResponse::Err(_) => "ERR",
    };
    Err(io::Error::new(
        io::ErrorKind::InvalidData,
        format!("unexpected agent response: {label}"),
    ))
}

fn invalid_control_response<T>(response: ControlResponse) -> io::Result<T> {
    let label = match response {
        ControlResponse::Ok => "OK",
        ControlResponse::Registered(_) => "REGISTERED",
        ControlResponse::Err(_) => "ERR",
    };
    Err(io::Error::new(
        io::ErrorKind::InvalidData,
        format!("unexpected agent control response: {label}"),
    ))
}

fn wide_pipe_name() -> Vec<u16> {
    to_wide(&format!(r"\\.\pipe\lockbox-agent-{}", pipe_scope()))
}

fn pipe_scope() -> String {
    let user = sanitize_name(&current_user());
    let Ok(agent_dir) = env::var("LOCKBOX_SESSION_AGENT_DIR") else {
        return user;
    };
    format!("{user}-{:016x}", hash_value(&agent_dir))
}

fn hash_value(value: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    value.hash(&mut hasher);
    hasher.finish()
}

fn current_user() -> String {
    env::var("USERNAME")
        .or_else(|_| env::var("USER"))
        .unwrap_or_else(|_| "unknown".to_string())
}

fn sanitize_name(name: &str) -> String {
    name.chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

fn to_wide(value: &str) -> Vec<u16> {
    value.encode_utf16().chain(std::iter::once(0)).collect()
}

#[cfg(test)]
mod tests {
    #[test]
    fn pipe_security_descriptor_can_be_built() {
        super::verify_agent_transport_security().unwrap();
    }
}