Skip to main content

bitwarden_ssh/
lib.rs

1#![doc = include_str!("../README.md")]
2
3#[allow(missing_docs)]
4pub mod error;
5mod export;
6pub use export::export_pkcs8_der_key;
7#[allow(missing_docs)]
8pub mod generator;
9#[allow(missing_docs)]
10pub mod import;
11
12use error::SshKeyExportError;
13use pkcs8::LineEnding;
14use ssh_key::{HashAlg, PrivateKey};
15
16#[cfg(feature = "uniffi")]
17uniffi::setup_scaffolding!();
18
19/// Decoded SSH key material returned by this crate's import/generate functions.
20#[derive(Debug)]
21pub struct SshKeyData {
22    /// SSH private key in unencrypted OpenSSH format.
23    pub private_key: String,
24    /// SSH public key according to RFC 4253.
25    pub public_key: String,
26    /// SSH fingerprint using SHA256 in the format: `SHA256:BASE64_ENCODED_FINGERPRINT`.
27    pub fingerprint: String,
28}
29
30fn ssh_private_key_to_data(value: PrivateKey) -> Result<SshKeyData, SshKeyExportError> {
31    let private_key_openssh = value
32        .to_openssh(LineEnding::LF)
33        .map_err(|_| SshKeyExportError::KeyConversion)?;
34
35    Ok(SshKeyData {
36        private_key: private_key_openssh.to_string(),
37        public_key: value.public_key().to_string(),
38        fingerprint: value.fingerprint(HashAlg::Sha256).to_string(),
39    })
40}