1
use std::sync::Arc;
2

            
3
use serde::{Deserialize, Serialize, ser::SerializeTuple};
4
use zbus::zvariant::{OwnedObjectPath, Type};
5
use zeroize::{Zeroize, ZeroizeOnDrop};
6

            
7
use super::Session;
8
use crate::{Key, Secret, crypto, dbus::Error, secret::ContentType};
9

            
10
#[derive(Debug, Serialize, Deserialize, Type)]
11
#[zvariant(signature = "(oayays)")]
12
/// Same as [`DBusSecret`] without tying the session path to a [`Session`] type.
13
pub struct DBusSecretInner(
14
    pub OwnedObjectPath,
15
    #[serde(with = "serde_bytes")] pub Vec<u8>,
16
    #[serde(with = "serde_bytes")] pub Vec<u8>,
17
    pub ContentType,
18
);
19

            
20
#[derive(Debug, Type, Zeroize, ZeroizeOnDrop)]
21
#[zvariant(signature = "(oayays)")]
22
pub struct DBusSecret {
23
    #[zeroize(skip)]
24
    pub(crate) session: Arc<Session>,
25
    pub(crate) parameters: Vec<u8>,
26
    pub(crate) value: Vec<u8>,
27
    #[zeroize(skip)]
28
    pub(crate) content_type: ContentType,
29
}
30

            
31
impl DBusSecret {
32
    /// Create a new plain (unencrypted) DBusSecret
33
2
    pub fn new(session: Arc<Session>, secret: impl Into<Secret>) -> Self {
34
2
        let secret = secret.into();
35
        Self {
36
            session,
37
2
            parameters: vec![],
38
4
            value: secret.as_bytes().to_vec(),
39
2
            content_type: secret.content_type(),
40
        }
41
    }
42

            
43
    /// Create a new encrypted DBusSecret
44
2
    pub fn new_encrypted(
45
        session: Arc<Session>,
46
        secret: impl Into<Secret>,
47
        aes_key: &Key,
48
    ) -> Result<Self, crate::dbus::Error> {
49
4
        let iv = crypto::generate_iv()?;
50
2
        let secret = secret.into();
51
2
        Ok(Self {
52
2
            session,
53
4
            value: crypto::encrypt(secret.as_bytes(), aes_key, &iv)?,
54
2
            parameters: iv,
55
2
            content_type: secret.content_type(),
56
        })
57
    }
58

            
59
2
    pub(crate) async fn from_inner(
60
        cnx: &zbus::Connection,
61
        inner: DBusSecretInner,
62
    ) -> Result<Self, Error> {
63
2
        Ok(Self {
64
4
            session: Arc::new(Session::new(cnx, inner.0).await?),
65
2
            parameters: inner.1,
66
2
            value: inner.2,
67
2
            content_type: inner.3,
68
        })
69
    }
70

            
71
2
    pub fn decrypt(&self, key: Option<&Arc<Key>>) -> Result<Secret, Error> {
72
2
        let value = match key {
73
6
            Some(key) => &crypto::decrypt(&self.value, key, &self.parameters)?,
74
2
            None => &self.value,
75
        };
76
4
        Ok(Secret::with_content_type(self.content_type, value))
77
    }
78

            
79
    /// Session used to encode the secret
80
2
    pub fn session(&self) -> &Session {
81
2
        &self.session
82
    }
83

            
84
    /// Algorithm dependent parameters for secret value encoding
85
2
    pub fn parameters(&self) -> &[u8] {
86
2
        &self.parameters
87
    }
88

            
89
    /// Possibly encoded secret value
90
2
    pub fn value(&self) -> &[u8] {
91
2
        &self.value
92
    }
93

            
94
    /// Content type of the secret
95
2
    pub fn content_type(&self) -> ContentType {
96
2
        self.content_type
97
    }
98
}
99

            
100
impl Serialize for DBusSecret {
101
4
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
102
    where
103
        S: serde::Serializer,
104
    {
105
4
        let mut tuple_serializer = serializer.serialize_tuple(4)?;
106
8
        tuple_serializer.serialize_element(self.session().inner().path())?;
107
4
        tuple_serializer.serialize_element(&serde_bytes::Bytes::new(self.parameters()))?;
108
4
        tuple_serializer.serialize_element(&serde_bytes::Bytes::new(self.value()))?;
109
4
        tuple_serializer.serialize_element(self.content_type().as_str())?;
110
5
        tuple_serializer.end()
111
    }
112
}
113

            
114
#[cfg(test)]
115
mod tests {
116
    use super::*;
117

            
118
    #[test]
119
    fn signature() {
120
        assert_eq!(DBusSecret::SIGNATURE, "(oayays)");
121
    }
122
}