auths-core 0.1.1

Core cryptography and keychain integration for Auths
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
//! Application-level runtime API for managing the identity agent and keys.
//!
//! Provides functions to interact with core components: secure key storage (`KeyStorage`),
//! cryptographic operations, the in-memory agent (`AgentCore`), and the agent listener.
//! Uses `AgentHandle` for lifecycle management of agent instances.
//! Also includes functions for interacting with the platform's SSH agent (on macOS).

use crate::agent::AgentCore;
use crate::agent::AgentHandle;
#[cfg(unix)]
use crate::agent::AgentSession;
use crate::crypto::provider_bridge;
use crate::crypto::signer::extract_seed_from_key_bytes;
use crate::crypto::signer::{decrypt_keypair, encrypt_keypair};
use crate::error::AgentError;
use crate::signing::{PassphraseProvider, PrefilledPassphraseProvider};
use crate::storage::keychain::{KeyAlias, KeyRole, KeyStorage};
use log::{debug, error, info, warn};
#[cfg(target_os = "macos")]
use p256::pkcs8::DecodePrivateKey;
#[cfg(target_os = "macos")]
use pkcs8::PrivateKeyInfo;
#[cfg(target_os = "macos")]
use pkcs8::der::Decode;
#[cfg(target_os = "macos")]
use pkcs8::der::asn1::OctetString;
use serde::Serialize;
#[cfg(unix)]
use ssh_agent_lib;
#[cfg(unix)]
use ssh_agent_lib::agent::listen;
use ssh_key::private::{Ed25519Keypair as SshEdKeypair, KeypairData};
use ssh_key::{
    self, LineEnding, PrivateKey as SshPrivateKey, PublicKey as SshPublicKey,
    public::Ed25519PublicKey as SshEd25519PublicKey,
};
#[cfg(unix)]
use std::io;
#[cfg(unix)]
use std::sync::Arc;
#[cfg(unix)]
use tokio::net::UnixListener;
use zeroize::Zeroizing;

#[cfg(target_os = "macos")]
use std::io::Write;

#[cfg(target_os = "macos")]
use {
    std::fs::{self, Permissions},
    std::os::unix::fs::PermissionsExt,
    tempfile::Builder as TempFileBuilder,
};

#[cfg(target_os = "macos")]
#[derive(Debug)]
enum SshRegError {
    Agent(crate::ports::ssh_agent::SshAgentError),
    Io(std::io::Error),
    Conversion(String),
    BadSeedLength(usize),
}

#[cfg(target_os = "macos")]
impl std::fmt::Display for SshRegError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Agent(e) => write!(f, "ssh agent error: {e}"),
            Self::Io(e) => write!(f, "I/O error: {e}"),
            Self::Conversion(s) => write!(f, "key conversion failed: {s}"),
            Self::BadSeedLength(n) => {
                write!(f, "invalid PKCS#8 seed length: expected 32 bytes, got {n}")
            }
        }
    }
}

/// Compute the SSH fingerprint for a public key of the given curve.
#[cfg(target_os = "macos")]
fn compute_ssh_fingerprint(pubkey_bytes: &[u8], curve: auths_crypto::CurveType) -> String {
    let key_data = match curve {
        auths_crypto::CurveType::Ed25519 => SshEd25519PublicKey::try_from(pubkey_bytes)
            .map(ssh_key::public::KeyData::Ed25519)
            .ok(),
        auths_crypto::CurveType::P256 => {
            ssh_key::public::EcdsaPublicKey::from_sec1_bytes(pubkey_bytes)
                .ok()
                .map(ssh_key::public::KeyData::Ecdsa)
        }
    };
    match key_data {
        Some(kd) => SshPublicKey::new(kd, "")
            .fingerprint(Default::default())
            .to_string(),
        None => {
            warn!("Could not build public key for fingerprint computation");
            "unknown_fingerprint".to_string()
        }
    }
}

/// Parse a PKCS#8 key blob into an `ssh_key::private::KeypairData` variant matching the curve.
#[cfg(target_os = "macos")]
fn build_ssh_keypair_data(
    pkcs8_bytes: &[u8],
    curve: auths_crypto::CurveType,
) -> Result<KeypairData, SshRegError> {
    match curve {
        auths_crypto::CurveType::Ed25519 => {
            let private_key_info = PrivateKeyInfo::from_der(pkcs8_bytes)
                .map_err(|e| SshRegError::Conversion(e.to_string()))?;
            let seed_octet_string = OctetString::from_der(private_key_info.private_key)
                .map_err(|e| SshRegError::Conversion(e.to_string()))?;
            let seed_bytes = seed_octet_string.as_bytes();
            if seed_bytes.len() != 32 {
                return Err(SshRegError::BadSeedLength(seed_bytes.len()));
            }
            #[allow(clippy::expect_used)]
            // INVARIANT: length validated by the 32-byte check above.
            let seed_array: [u8; 32] = seed_bytes.try_into().expect("Length checked");
            Ok(KeypairData::Ed25519(SshEdKeypair::from_seed(&seed_array)))
        }
        auths_crypto::CurveType::P256 => {
            use p256::elliptic_curve::sec1::ToEncodedPoint;
            use ssh_key::private::{EcdsaKeypair, EcdsaPrivateKey};

            let secret = p256::SecretKey::from_pkcs8_der(pkcs8_bytes)
                .map_err(|e| SshRegError::Conversion(format!("P-256 PKCS#8 parse failed: {e}")))?;
            let public = secret.public_key();
            Ok(KeypairData::Ecdsa(EcdsaKeypair::NistP256 {
                public: public.to_encoded_point(false),
                private: EcdsaPrivateKey::from(secret),
            }))
        }
    }
}

// --- Public Structs ---

/// Represents the result of trying to load a single key into the agent core.
#[derive(Serialize, Debug, Clone)]
pub struct KeyLoadStatus {
    /// Key alias.
    pub alias: KeyAlias,
    /// Whether the key was successfully loaded.
    pub loaded: bool,
    /// Load error message, if any.
    pub error: Option<String>,
}

/// Represents the outcome of attempting to register a key with the system SSH agent.
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub enum RegistrationOutcome {
    /// Key was successfully added to the agent.
    Added,
    /// Key already exists in the agent.
    AlreadyExists,
    /// The SSH agent process was not found.
    AgentNotFound,
    /// The agent command failed.
    CommandFailed,
    /// The key type is not supported by this agent.
    UnsupportedKeyType,
    /// Key format conversion failed.
    ConversionFailed,
    /// An I/O error occurred.
    IoError,
    /// An unexpected internal error occurred.
    InternalError,
}

/// Represents the status of registering a single key with the system SSH agent.
#[derive(Serialize, Debug, Clone)]
pub struct KeyRegistrationStatus {
    /// Key fingerprint.
    pub fingerprint: String,
    /// Registration outcome.
    pub status: RegistrationOutcome,
    /// Additional message, if any.
    pub message: Option<String>,
}

// --- Public API Functions ---

/// Clears all unlocked keys from the specified agent handle.
///
/// This effectively locks the agent until keys are reloaded.
///
/// # Arguments
/// * `handle` - The agent handle to clear keys from
///
/// # Example
/// ```rust,ignore
/// use auths_core::AgentHandle;
/// use auths_core::api::clear_agent_keys_with_handle;
///
/// let handle = AgentHandle::new(socket_path);
/// clear_agent_keys_with_handle(&handle)?;
/// ```
pub fn clear_agent_keys_with_handle(handle: &AgentHandle) -> Result<(), AgentError> {
    info!("Clearing all keys from agent handle.");
    let mut agent_guard = handle.lock()?;
    agent_guard.clear_keys();
    debug!("Agent keys cleared.");
    Ok(())
}

/// Loads specific keys (by alias) from secure storage into the specified agent handle.
///
/// Requires the correct passphrase for each key, obtained via the `passphrase_provider`.
/// Replaces any keys currently loaded in the agent. Stores decrypted PKCS#8 bytes securely
/// in memory using `zeroize`.
///
/// # Arguments
/// * `handle` - The agent handle to load keys into
/// * `aliases`: A list of key aliases to load from secure storage.
/// * `passphrase_provider`: A component responsible for securely obtaining passphrases.
/// * `keychain`: The key storage backend to load keys from.
///
/// # Returns
/// A `Result` containing a list of `KeyLoadStatus` structs, indicating the outcome
/// for each requested alias, or an `AgentError` if a fatal error occurs.
pub fn load_keys_into_agent_with_handle(
    handle: &AgentHandle,
    aliases: Vec<String>,
    passphrase_provider: &dyn PassphraseProvider,
    keychain: &(dyn KeyStorage + Send + Sync),
) -> Result<Vec<KeyLoadStatus>, AgentError> {
    info!(
        "Attempting to load keys into agent handle for aliases: {:?}",
        aliases
    );
    if aliases.is_empty() {
        warn!("load_keys_into_agent_with_handle called with empty alias list. Clearing agent.");
        clear_agent_keys_with_handle(handle)?;
        return Ok(vec![]);
    }

    let mut load_statuses = Vec::new();
    let mut temp_unlocked_core = AgentCore::default();

    for alias in aliases {
        debug!("Processing alias for agent load: {}", alias);
        let key_alias = KeyAlias::new_unchecked(&alias);
        let mut status = KeyLoadStatus {
            alias: key_alias.clone(),
            loaded: false,
            error: None,
        };

        let load_result = || -> Result<Zeroizing<Vec<u8>>, AgentError> {
            if keychain.is_hardware_backend() {
                return Err(AgentError::HardwareKeyNotExportable {
                    operation: "agent key load".to_string(),
                });
            }
            let (_controller_did, _role, encrypted_pkcs8) = keychain.load_key(&key_alias)?;
            let prompt = format!(
                "Enter passphrase to unlock key '{}' for agent session:",
                key_alias
            );
            let passphrase = passphrase_provider.get_passphrase(&prompt)?;
            let pkcs8_bytes = decrypt_keypair(&encrypted_pkcs8, &passphrase)?;
            let _ = extract_seed_from_key_bytes(&pkcs8_bytes).map_err(|e| {
                AgentError::KeyDeserializationError(format!(
                    "Failed to parse key for alias '{}' after decryption: {}",
                    key_alias, e
                ))
            })?;
            Ok(pkcs8_bytes)
        }();

        match load_result {
            Ok(pkcs8_bytes) => {
                info!("Successfully unlocked key for alias '{}'", key_alias);
                match temp_unlocked_core.register_key(pkcs8_bytes) {
                    Ok(()) => status.loaded = true,
                    Err(e) => {
                        error!(
                            "Failed to register key '{}' in agent core state after successful unlock/parse: {}",
                            key_alias, e
                        );
                        status.error = Some(format!(
                            "Internal error: Failed to register key in agent core state: {}",
                            e
                        ));
                    }
                }
            }
            Err(e) => {
                error!(
                    "Failed to load/decrypt key for alias '{}': {}",
                    key_alias, e
                );
                match e {
                    AgentError::IncorrectPassphrase => {
                        status.error = Some("Incorrect passphrase".to_string())
                    }
                    AgentError::KeyNotFound => status.error = Some("Key not found".to_string()),
                    AgentError::UserInputCancelled => {
                        status.error = Some("Operation cancelled by user".to_string())
                    }
                    AgentError::KeyDeserializationError(_) => {
                        status.error = Some(format!("Failed to parse key after decryption: {}", e))
                    }
                    _ => status.error = Some(e.to_string()),
                }
            }
        }
        load_statuses.push(status);
    }

    // Atomically update the agent state
    let mut agent_guard = handle.lock()?;
    info!(
        "Replacing agent core with {} unlocked keys ({} aliases attempted).",
        temp_unlocked_core.key_count(),
        load_statuses.len()
    );
    *agent_guard = temp_unlocked_core;

    Ok(load_statuses)
}

/// Rotates the keypair for a given alias *in the secure storage only*.
///
/// This generates a new Ed25519 keypair, encrypts it with the `new_passphrase`,
/// and overwrites the existing entry for `alias` in the platform's keychain or
/// secure storage. The key remains associated with the *same Controller DID*
/// as the original key.
///
/// **Warning:** This function does *not* update any corresponding identity
/// representation in a Git repository (e.g., changing the Controller DID stored
/// in an identity commit or creating a KERI rotation event). Using this function
/// alone may lead to inconsistencies if the identity representation relies on the
/// public key associated with the Controller DID. It also does not automatically
/// update the key loaded in the running agent; `load_keys_into_agent` or restarting
/// the agent may be required.
///
/// # Arguments
/// * `alias`: The alias of the key entry in secure storage to rotate.
/// * `new_passphrase`: The passphrase to encrypt the *new* private key with.
///
/// # Returns
/// `Ok(())` on success, or an `AgentError` if the alias is not found, key generation
/// fails, encryption fails, or storage fails.
pub fn rotate_key(
    alias: &str,
    new_passphrase: &str,
    keychain: &(dyn KeyStorage + Send + Sync),
) -> Result<(), AgentError> {
    info!(
        "[API] Attempting secure storage key rotation for local alias: {}",
        alias
    );
    if alias.trim().is_empty() {
        return Err(AgentError::InvalidInput(
            "Alias cannot be empty".to_string(),
        ));
    }
    if new_passphrase.is_empty() {
        return Err(AgentError::InvalidInput(
            "New passphrase cannot be empty".to_string(),
        ));
    }

    // 1. Verify the alias exists and retrieve its associated Controller DID
    let key_alias = KeyAlias::new_unchecked(alias);
    let existing_did = keychain.get_identity_for_alias(&key_alias)?;
    info!(
        "Found existing key for alias '{}', associated with Controller DID '{}'. Proceeding with rotation.",
        alias, existing_did
    );

    // 2. Generate new keypair via CryptoProvider
    let (seed, pubkey) = provider_bridge::generate_ed25519_keypair_sync()
        .map_err(|e| AgentError::CryptoError(format!("Failed to generate new keypair: {}", e)))?;
    // Build PKCS#8 v2 DER for storage compatibility
    let new_pkcs8_bytes = auths_crypto::build_ed25519_pkcs8_v2(seed.as_bytes(), &pubkey);
    debug!("Generated new keypair via CryptoProvider.");

    // 3. Encrypt the new keypair with the new passphrase
    let encrypted_new_key = encrypt_keypair(&new_pkcs8_bytes, new_passphrase)?;
    debug!("Encrypted new keypair with provided passphrase.");

    // 4. Overwrite the existing entry in secure storage with the new encrypted key,
    //    keeping the original Controller DID association.
    keychain.store_key(
        &key_alias,
        &existing_did,
        KeyRole::Primary,
        &encrypted_new_key,
    )?;
    info!(
        "Successfully overwrote secure storage for alias '{}' with new encrypted key.",
        alias
    );

    warn!(
        "Secure storage key rotated for alias '{}'. This did NOT update any Git identity representation. The running agent may still hold the old decrypted key. Consider reloading keys into the agent.",
        alias
    );
    Ok(())
}

/// Signs a message using a key currently loaded in the specified agent handle.
///
/// This retrieves the decrypted key material from the agent handle based on the
/// provided public key bytes and performs the signing operation. It does *not*
/// require a passphrase as the key is assumed to be already unlocked.
///
/// # Arguments
/// * `handle` - The agent handle containing the loaded keys
/// * `pubkey`: The public key bytes of the key to use for signing.
/// * `data`: The data bytes to sign.
///
/// # Returns
/// The raw signature bytes, or an `AgentError` if the key is not found in the
/// agent core or if the signing operation fails internally.
pub fn agent_sign_with_handle(
    handle: &AgentHandle,
    pubkey: &[u8],
    data: &[u8],
) -> Result<Vec<u8>, AgentError> {
    debug!(
        "Agent sign request for pubkey starting with: {:x?}...",
        &pubkey[..core::cmp::min(pubkey.len(), 8)]
    );

    // Use the handle's sign method which includes lock check
    handle.sign(pubkey, data)
}

/// Exports the decrypted private key in OpenSSH PEM format.
///
/// Retrieves the encrypted key from secure storage, decrypts it using the
/// provided passphrase, and formats it as a standard OpenSSH PEM private key string.
///
/// # Arguments
/// * `alias`: The alias of the key in secure storage.
/// * `passphrase`: The passphrase to decrypt the key.
///
/// # Returns
/// A `Zeroizing<String>` containing the PEM data on success, or an `AgentError`.
pub fn export_key_openssh_pem(
    alias: &str,
    passphrase: &str,
    keychain: &(dyn KeyStorage + Send + Sync),
) -> Result<Zeroizing<String>, AgentError> {
    info!("Exporting PEM for local alias: {}", alias);
    if alias.trim().is_empty() {
        return Err(AgentError::InvalidInput(
            "Alias cannot be empty".to_string(),
        ));
    }
    if keychain.is_hardware_backend() {
        return Err(AgentError::HardwareKeyNotExportable {
            operation: "OpenSSH private key export".to_string(),
        });
    }
    // 1. Load encrypted key data
    let key_alias = KeyAlias::new_unchecked(alias);
    let (_controller_did, _role, encrypted_pkcs8) = keychain.load_key(&key_alias)?;

    // 2. Decrypt key data
    let pkcs8_bytes = decrypt_keypair(&encrypted_pkcs8, passphrase)?;

    // 3. Parse the key material (auto-detects curve)
    let parsed = auths_crypto::parse_key_material(&pkcs8_bytes[..]).map_err(|e| {
        AgentError::KeyDeserializationError(format!(
            "Failed to parse key material for alias '{}': {}",
            alias, e
        ))
    })?;

    let keypair_data = build_openssh_keypair_data(&parsed).map_err(|e| {
        AgentError::CryptoError(format!(
            "Failed to build SSH keypair for alias '{}': {}",
            alias, e
        ))
    })?;

    let ssh_private_key = SshPrivateKey::new(keypair_data, "").map_err(|e| {
        AgentError::CryptoError(format!(
            "Failed to create ssh_key::PrivateKey for alias '{}': {}",
            alias, e
        ))
    })?;

    let pem = ssh_private_key.to_openssh(LineEnding::LF).map_err(|e| {
        AgentError::CryptoError(format!(
            "Failed to encode OpenSSH PEM for alias '{}': {}",
            alias, e
        ))
    })?;

    debug!("Successfully generated PEM for alias '{}'", alias);
    Ok(pem)
}

/// Build `ssh_key::private::KeypairData` from a parsed key material, dispatching on curve.
fn build_openssh_keypair_data(parsed: &auths_crypto::ParsedKey) -> Result<KeypairData, String> {
    match parsed.seed.curve() {
        auths_crypto::CurveType::Ed25519 => Ok(KeypairData::Ed25519(SshEdKeypair::from_seed(
            parsed.seed.as_bytes(),
        ))),
        auths_crypto::CurveType::P256 => {
            use p256::elliptic_curve::sec1::ToEncodedPoint;
            use ssh_key::private::{EcdsaKeypair, EcdsaPrivateKey};

            let secret = p256::SecretKey::from_slice(parsed.seed.as_bytes())
                .map_err(|e| format!("P-256 secret key parse: {e}"))?;
            let public = secret.public_key();
            Ok(KeypairData::Ecdsa(EcdsaKeypair::NistP256 {
                public: public.to_encoded_point(false),
                private: EcdsaPrivateKey::from(secret),
            }))
        }
    }
}

/// Exports the public key in OpenSSH `.pub` format.
///
/// Retrieves the encrypted key from secure storage, decrypts it using the
/// provided passphrase, derives the public key, and formats it as a standard
/// OpenSSH `.pub` line (including the alias as a comment).
///
/// # Arguments
/// * `alias`: The alias of the key in secure storage.
/// * `passphrase`: The passphrase to decrypt the key.
///
/// # Returns
/// A `String` containing the public key line on success, or an `AgentError`.
pub fn export_key_openssh_pub(
    alias: &str,
    passphrase: &str,
    keychain: &(dyn KeyStorage + Send + Sync),
) -> Result<String, AgentError> {
    info!("Exporting OpenSSH public key for local alias: {}", alias);
    if alias.trim().is_empty() {
        return Err(AgentError::InvalidInput(
            "Alias cannot be empty".to_string(),
        ));
    }
    // 1. Obtain public key bytes (hardware-aware; SE returns pubkey without decryption)
    let key_alias = KeyAlias::new_unchecked(alias);
    let passphrase_provider = PrefilledPassphraseProvider::new(passphrase);
    let (pubkey_bytes, curve) = crate::storage::keychain::extract_public_key_bytes(
        keychain,
        &key_alias,
        &passphrase_provider,
    )?;
    let key_data = match curve {
        auths_crypto::CurveType::Ed25519 => {
            let pk = SshEd25519PublicKey::try_from(pubkey_bytes.as_slice()).map_err(|e| {
                AgentError::CryptoError(format!(
                    "Failed to create Ed25519PublicKey from bytes: {}",
                    e
                ))
            })?;
            ssh_key::public::KeyData::Ed25519(pk)
        }
        auths_crypto::CurveType::P256 => {
            let pk = ssh_key::public::EcdsaPublicKey::from_sec1_bytes(pubkey_bytes.as_slice())
                .map_err(|e| {
                    AgentError::CryptoError(format!(
                        "Failed to create EcdsaPublicKey from bytes: {}",
                        e
                    ))
                })?;
            ssh_key::public::KeyData::Ecdsa(pk)
        }
    };

    // 5. Create the ssh-key PublicKey object (comment is optional here)
    let ssh_pub_key = SshPublicKey::new(key_data, ""); // Use empty comment for base formatting

    // 6. Format the base public key string (type and key material)
    let pubkey_base = ssh_pub_key.to_openssh().map_err(|e| {
        // Use CryptoError for formatting failure
        AgentError::CryptoError(format!("Failed to format OpenSSH pubkey base: {}", e))
    })?;

    // 7. Manually append the alias as the comment part of the .pub line
    let formatted_pubkey = format!("{} {}", pubkey_base, alias);

    debug!(
        "Successfully generated OpenSSH public key string for alias '{}'",
        alias
    );
    Ok(formatted_pubkey)
}

/// Returns the number of keys currently loaded in the specified agent handle.
///
/// # Arguments
/// * `handle` - The agent handle to query
///
/// # Returns
/// The number of keys currently loaded.
pub fn get_agent_key_count_with_handle(handle: &AgentHandle) -> Result<usize, AgentError> {
    handle.key_count()
}

/// Attempts to register all keys currently loaded in the specified agent handle
/// with the system's running SSH agent via the injected `SshAgentPort`.
///
/// This iterates through the unlocked keys in the agent core, converts each to
/// OpenSSH PEM format, writes it to a temporary file, and delegates to the
/// provided `ssh_agent` port for the actual registration.
///
/// Args:
/// * `handle` - The agent handle containing the keys to register.
/// * `ssh_agent_socket` - Optional path to the SSH agent socket (for diagnostics).
/// * `ssh_agent` - Port implementation that registers keys with the system agent.
///
/// Usage:
/// ```ignore
/// use auths_core::api::runtime::register_keys_with_macos_agent_with_handle;
///
/// let statuses = register_keys_with_macos_agent_with_handle(&handle, None, &adapter)?;
/// ```
#[cfg(target_os = "macos")]
#[allow(clippy::disallowed_methods)]
// INVARIANT: macOS SSH agent registration — temp file creation and permissions are inherently I/O
#[allow(clippy::disallowed_types)]
pub fn register_keys_with_macos_agent_with_handle(
    handle: &AgentHandle,
    ssh_agent_socket: Option<&std::path::Path>,
    ssh_agent: &dyn crate::ports::ssh_agent::SshAgentPort,
) -> Result<Vec<KeyRegistrationStatus>, AgentError> {
    info!("Attempting to register keys from agent handle with system ssh-agent...");
    if ssh_agent_socket.is_none() {
        warn!("SSH_AUTH_SOCK not configured. System ssh-agent may not be running or configured.");
    }

    let keys_to_register: Vec<(Vec<u8>, auths_crypto::CurveType, Zeroizing<Vec<u8>>)> = {
        let agent_guard = handle.lock()?;
        agent_guard
            .keys
            .iter()
            .filter_map(|(pubkey, stored)| {
                let typed_seed = match stored.curve {
                    auths_crypto::CurveType::Ed25519 => {
                        auths_crypto::TypedSeed::Ed25519(*stored.seed.as_bytes())
                    }
                    auths_crypto::CurveType::P256 => {
                        auths_crypto::TypedSeed::P256(*stored.seed.as_bytes())
                    }
                };
                let signer = auths_crypto::TypedSignerKey::from_seed(typed_seed).ok()?;
                let pkcs8 = signer.to_pkcs8().ok()?;
                Some((
                    pubkey.clone(),
                    stored.curve,
                    Zeroizing::new(pkcs8.as_ref().to_vec()),
                ))
            })
            .collect()
    };

    register_keys_with_macos_agent_internal(keys_to_register, ssh_agent)
}

/// Stub function for non-macOS platforms.
#[cfg(not(target_os = "macos"))]
pub fn register_keys_with_macos_agent_with_handle(
    _handle: &AgentHandle,
    _ssh_agent_socket: Option<&std::path::Path>,
    _ssh_agent: &dyn crate::ports::ssh_agent::SshAgentPort,
) -> Result<Vec<KeyRegistrationStatus>, AgentError> {
    info!("Not on macOS, skipping system ssh-agent registration.");
    Ok(vec![])
}

/// Internal helper that performs the actual system SSH agent registration.
///
/// Converts each PKCS#8 key to OpenSSH PEM, writes to a temp file, and
/// delegates to the injected `SshAgentPort` for the actual `ssh-add` call.
#[cfg(target_os = "macos")]
#[allow(clippy::too_many_lines)]
fn register_keys_with_macos_agent_internal(
    keys_to_register: Vec<(Vec<u8>, auths_crypto::CurveType, Zeroizing<Vec<u8>>)>,
    ssh_agent: &dyn crate::ports::ssh_agent::SshAgentPort,
) -> Result<Vec<KeyRegistrationStatus>, AgentError> {
    use crate::ports::ssh_agent::SshAgentError;

    if keys_to_register.is_empty() {
        info!("No keys to register with system agent.");
        return Ok(vec![]);
    }
    info!(
        "Found {} keys to attempt registration with system agent.",
        keys_to_register.len()
    );

    let mut results = Vec::with_capacity(keys_to_register.len());

    for (pubkey_bytes, curve, pkcs8_bytes_zeroizing) in keys_to_register.into_iter() {
        let fingerprint_str = compute_ssh_fingerprint(&pubkey_bytes, curve);

        let mut status = KeyRegistrationStatus {
            fingerprint: fingerprint_str.clone(),
            status: RegistrationOutcome::InternalError,
            message: None,
        };

        let result: Result<(), SshRegError> = (|| {
            let pkcs8_bytes = pkcs8_bytes_zeroizing.as_ref();
            let keypair_data = build_ssh_keypair_data(pkcs8_bytes, curve)?;
            let ssh_private_key = SshPrivateKey::new(keypair_data, "")
                .map_err(|e| SshRegError::Conversion(e.to_string()))?;
            let pem_zeroizing = ssh_private_key
                .to_openssh(LineEnding::LF)
                .map_err(|e| SshRegError::Conversion(e.to_string()))?;
            let pem_string = pem_zeroizing.to_string();

            let mut temp_file_guard = TempFileBuilder::new()
                .prefix("auths-key-")
                .suffix(".pem")
                .rand_bytes(5)
                .tempfile()
                .map_err(SshRegError::Io)?;
            if let Err(e) =
                fs::set_permissions(temp_file_guard.path(), Permissions::from_mode(0o600))
            {
                warn!(
                    "Failed to set 600 permissions on temp file {:?}: {}. Continuing...",
                    temp_file_guard.path(),
                    e
                );
            }
            temp_file_guard
                .write_all(pem_string.as_bytes())
                .map_err(SshRegError::Io)?;
            temp_file_guard.flush().map_err(SshRegError::Io)?;
            let temp_file_path = temp_file_guard.path().to_path_buf();

            debug!(
                "Attempting ssh-add for temporary key file: {:?}",
                temp_file_path
            );
            ssh_agent
                .register_key(&temp_file_path)
                .map_err(SshRegError::Agent)?;
            debug!("ssh-add finished for {:?}", temp_file_path);
            Ok(())
        })();

        match result {
            Ok(()) => {
                info!(
                    "ssh-add successful for {}: Identity added.",
                    fingerprint_str
                );
                status.status = RegistrationOutcome::Added;
                status.message = Some("Identity added via ssh-agent port".to_string());
            }
            Err(e) => {
                match &e {
                    SshRegError::Agent(SshAgentError::NotAvailable(_)) => {
                        status.status = RegistrationOutcome::AgentNotFound;
                    }
                    SshRegError::Agent(SshAgentError::CommandFailed(_)) => {
                        status.status = RegistrationOutcome::CommandFailed;
                    }
                    SshRegError::Agent(SshAgentError::IoError(_)) | SshRegError::Io(_) => {
                        status.status = RegistrationOutcome::IoError;
                    }
                    SshRegError::Conversion(_) | SshRegError::BadSeedLength(_) => {
                        status.status = RegistrationOutcome::ConversionFailed;
                    }
                }
                error!(
                    "Error during registration process for {}: {:?}",
                    fingerprint_str, e
                );
                status.message = Some(format!("Registration error: {}", e));
            }
        }
        results.push(status);
    }

    info!(
        "Finished attempting system agent registration for {} keys.",
        results.len()
    );
    Ok(results)
}

/// Starts the SSH agent listener using the provided `AgentHandle`.
///
/// Binds to the socket path from the handle, cleans up any old socket file if present,
/// and enters an asynchronous loop (`ssh_agent_lib::listen`) to accept and handle
/// incoming agent connections using `AgentSession`.
///
/// Requires a `tokio` runtime context. Runs indefinitely on success.
///
/// # Arguments
/// * `handle`: The agent handle containing the socket path and agent core.
///
/// # Returns
/// - `Ok(())` if the listener starts successfully (runs indefinitely).
/// - `Err(AgentError)` if binding/setup fails or the listener loop exits with an error.
#[cfg(unix)]
#[allow(clippy::disallowed_methods)] // INVARIANT: Unix socket lifecycle — socket dir creation and cleanup is inherently I/O
pub async fn start_agent_listener_with_handle(handle: Arc<AgentHandle>) -> Result<(), AgentError> {
    let socket_path = handle.socket_path();
    info!("Attempting to start agent listener at {:?}", socket_path);

    // --- Ensure parent directory exists ---
    if let Some(parent) = socket_path.parent()
        && !parent.exists()
    {
        debug!("Creating parent directory for socket: {:?}", parent);
        if let Err(e) = std::fs::create_dir_all(parent) {
            error!("Failed to create parent directory {:?}: {}", parent, e);
            return Err(AgentError::IO(e));
        }
    }

    // --- Clean up existing socket file (if any) ---
    match std::fs::remove_file(socket_path) {
        Ok(()) => info!("Removed existing socket file at {:?}", socket_path),
        Err(e) if e.kind() == io::ErrorKind::NotFound => {
            debug!(
                "No existing socket file found at {:?}, proceeding.",
                socket_path
            );
        }
        Err(e) => {
            warn!(
                "Failed to remove existing socket file at {:?}: {}. Binding might fail.",
                socket_path, e
            );
        }
    }

    // --- Bind the listener ---
    let listener = UnixListener::bind(socket_path).map_err(|e| {
        error!("Failed to bind listener socket at {:?}: {}", socket_path, e);
        AgentError::IO(e)
    })?;

    // --- Listener started successfully ---
    let actual_path = socket_path
        .canonicalize()
        .unwrap_or_else(|_| socket_path.to_path_buf());
    info!(
        "🚀 Agent listener started successfully at {:?}",
        actual_path
    );
    info!("   Set SSH_AUTH_SOCK={:?} to use this agent.", actual_path);

    // Mark agent as running
    handle.set_running(true);

    // --- Create the agent session handler using the provided handle ---
    let session = AgentSession::new(handle.clone());

    // --- Start the main listener loop from ssh_agent_lib ---
    let result = listen(listener, session).await;

    // Mark agent as no longer running
    handle.set_running(false);

    if let Err(e) = result {
        error!("SSH Agent listener failed: {:?}", e);
        return Err(AgentError::IO(io::Error::other(format!(
            "SSH Agent listener failed: {}",
            e
        ))));
    }

    warn!("Agent listener loop exited unexpectedly without error.");
    Ok(())
}

/// Starts the SSH agent listener on the specified Unix domain socket path.
///
/// This is a convenience function that creates an `AgentHandle` internally.
/// For more control over the agent lifecycle, use `start_agent_listener_with_handle`
/// with your own `AgentHandle`.
///
/// Requires a `tokio` runtime context. Runs indefinitely on success.
///
/// # Arguments
/// * `socket_path_str`: The filesystem path for the Unix domain socket.
///
/// # Returns
/// - `Ok(())` if the listener starts successfully (runs indefinitely).
/// - `Err(AgentError)` if binding/setup fails or the listener loop exits with an error.
#[cfg(unix)]
pub async fn start_agent_listener(socket_path_str: String) -> Result<(), AgentError> {
    use std::path::PathBuf;
    let handle = Arc::new(AgentHandle::new(PathBuf::from(&socket_path_str)));
    start_agent_listener_with_handle(handle).await
}