bitwarden_vault/cipher/
ssh_key.rs

1use bitwarden_core::key_management::{KeyIds, SymmetricKeyId};
2use bitwarden_crypto::{CryptoError, Decryptable, EncString, Encryptable, KeyStoreContext};
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "wasm")]
5use tsify_next::Tsify;
6
7#[derive(Serialize, Deserialize, Debug, Clone)]
8#[serde(rename_all = "camelCase", deny_unknown_fields)]
9#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
10#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
11pub struct SshKey {
12    /// SSH private key (ed25519/rsa) in unencrypted openssh private key format [OpenSSH private key](https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key)
13    pub private_key: EncString,
14    /// SSH public key (ed25519/rsa) according to [RFC4253](https://datatracker.ietf.org/doc/html/rfc4253#section-6.6)
15    pub public_key: EncString,
16    /// SSH fingerprint using SHA256 in the format: `SHA256:BASE64_ENCODED_FINGERPRINT`
17    pub fingerprint: EncString,
18}
19
20#[derive(Serialize, Deserialize, Debug, Clone)]
21#[serde(rename_all = "camelCase", deny_unknown_fields)]
22#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
23#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
24pub struct SshKeyView {
25    /// SSH private key (ed25519/rsa) in unencrypted openssh private key format [OpenSSH private key](https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key)
26    pub private_key: String,
27    /// SSH public key (ed25519/rsa) according to [RFC4253](https://datatracker.ietf.org/doc/html/rfc4253#section-6.6)
28    pub public_key: String,
29    /// SSH fingerprint using SHA256 in the format: `SHA256:BASE64_ENCODED_FINGERPRINT`
30    pub fingerprint: String,
31}
32
33impl Encryptable<KeyIds, SymmetricKeyId, SshKey> for SshKeyView {
34    fn encrypt(
35        &self,
36        ctx: &mut KeyStoreContext<KeyIds>,
37        key: SymmetricKeyId,
38    ) -> Result<SshKey, CryptoError> {
39        Ok(SshKey {
40            private_key: self.private_key.encrypt(ctx, key)?,
41            public_key: self.public_key.encrypt(ctx, key)?,
42            fingerprint: self.fingerprint.encrypt(ctx, key)?,
43        })
44    }
45}
46
47impl Decryptable<KeyIds, SymmetricKeyId, SshKeyView> for SshKey {
48    fn decrypt(
49        &self,
50        ctx: &mut KeyStoreContext<KeyIds>,
51        key: SymmetricKeyId,
52    ) -> Result<SshKeyView, CryptoError> {
53        Ok(SshKeyView {
54            private_key: self.private_key.decrypt(ctx, key)?,
55            public_key: self.public_key.decrypt(ctx, key)?,
56            fingerprint: self.fingerprint.decrypt(ctx, key)?,
57        })
58    }
59}