bitwarden_ssh/
lib.rs

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
pub mod error;
pub mod generator;
pub mod import;

use error::SshKeyExportError;
use pkcs8::LineEnding;
use serde::{Deserialize, Serialize};
use ssh_key::{HashAlg, PrivateKey};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;

#[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct SshKey {
    /// The private key in OpenSSH format
    pub private_key: String,
    pub public_key: String,
    pub key_fingerprint: String,
}

impl TryFrom<PrivateKey> for SshKey {
    type Error = SshKeyExportError;

    fn try_from(value: PrivateKey) -> Result<Self, Self::Error> {
        let private_key_openssh = value
            .to_openssh(LineEnding::LF)
            .map_err(|_| SshKeyExportError::KeyConversionError)?;

        Ok(SshKey {
            private_key: private_key_openssh.to_string(),
            public_key: value.public_key().to_string(),
            key_fingerprint: value.fingerprint(HashAlg::Sha256).to_string(),
        })
    }
}