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
use bitfield::bitfield;
use bytes::Buf;
use serde_derive::Serialize;

#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum KeyConfigType {
    Ecc,
    NotEcc,
}

impl From<u8> for KeyConfigType {
    fn from(v: u8) -> Self {
        match v & 4 == 4 {
            true => Self::Ecc,
            _ => Self::NotEcc,
        }
    }
}

impl From<KeyConfigType> for u8 {
    fn from(v: KeyConfigType) -> Self {
        match v {
            KeyConfigType::Ecc => 4,
            KeyConfigType::NotEcc => 7,
        }
    }
}

impl From<&[u8]> for KeyConfig {
    fn from(v: &[u8]) -> Self {
        let mut buf = v;
        Self(buf.get_u16())
    }
}

bitfield! {
    #[derive(PartialEq, Eq)]
    pub struct KeyConfig(u16);
    impl Debug;

    pub u8, auth_key, set_auth_key: 3, 0;
    pub intrusion_disable, set_intrusion_disable: 4;
    pub u8, x509_index, set_x509_index: 7, 6;

    pub private, set_private: 8;
    pub pub_info, set_pub_info: 9;
    pub u8, from into KeyConfigType, key_type, set_key_type: 12, 10;
    pub lockable, set_is_lockable: 13;
    pub req_random, set_req_random: 14;
    pub req_auth, set_req_auth: 15;
}

impl From<u16> for KeyConfig {
    fn from(v: u16) -> Self {
        Self(v)
    }
}

impl From<KeyConfig> for u16 {
    fn from(v: KeyConfig) -> Self {
        v.0
    }
}

impl From<&KeyConfig> for u16 {
    fn from(v: &KeyConfig) -> Self {
        v.0
    }
}

///  Returns a key configuration set up to store ECC key private keys.
impl Default for KeyConfig {
    fn default() -> Self {
        let mut result = KeyConfig(0);
        result.set_key_type(KeyConfigType::Ecc);
        result.set_is_lockable(true);
        result.set_private(true);
        result.set_pub_info(true);
        result
    }
}

impl serde::ser::Serialize for KeyConfig {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("key_config", 9)?;
        state.serialize_field("auth_key", &self.auth_key())?;
        state.serialize_field("intrusion_disable", &self.intrusion_disable())?;
        state.serialize_field("x509_index", &self.x509_index())?;
        state.serialize_field("private", &self.private())?;
        state.serialize_field("pub_info", &self.pub_info())?;
        state.serialize_field("key_type", &self.key_type())?;
        state.serialize_field("lockable", &self.lockable())?;
        state.serialize_field("req_random", &self.req_random())?;
        state.serialize_field("req_auth", &self.req_auth())?;

        state.end()
    }
}